From e170200fe3b0201407fd25667989892907117ece Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Mon, 27 Jun 2011 15:29:23 +0430 Subject: [PATCH] add djvu file support via libdjvulibre Don't like either libdjvulibre or the djvu file format at all but sometimes there are no other options. --- Makefile | 14 ++++---- djvu.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ doc.c => pdf.c | 0 3 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 djvu.c rename doc.c => pdf.c (100%) diff --git a/Makefile b/Makefile index 40dde01..3b15836 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,14 @@ -MUPDF_PATH = . +PREFIX = . CC = cc -CFLAGS = -Wall -Os -I$(MUPDF_PATH)/include -LDFLAGS = -lm -L$(MUPDF_PATH)/lib -lmupdf -lfitz -lfreetype -ljbig2dec -ljpeg -lz -lopenjpeg +CFLAGS = -Wall -O2 -I$(PREFIX)/include +LDFLAGS = -L$(PREFIX)/lib all: fbpdf .c.o: $(CC) -c $(CFLAGS) $< -fbpdf: fbpdf.o doc.o draw.o - $(CC) -o $@ $^ $(LDFLAGS) +fbpdf: fbpdf.o pdf.o draw.o + $(CC) -o $@ $^ $(LDFLAGS) -lmupdf -lfitz -lfreetype -ljbig2dec -ljpeg -lz -lopenjpeg -lm +fbdjvu: fbpdf.o djvu.o draw.o + $(CC) -o $@ $^ $(LDFLAGS) -ldjvulibre -ljpeg -lm -lstdc++ clean: - -rm -f *.o fbpdf + -rm -f *.o fbpdf fbdjvu diff --git a/djvu.c b/djvu.c new file mode 100644 index 0000000..8e37789 --- /dev/null +++ b/djvu.c @@ -0,0 +1,104 @@ +#include +#include +#include "ddjvuapi.h" +#include "draw.h" +#include "doc.h" + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +struct doc { + ddjvu_context_t *ctx; + ddjvu_document_t *doc; +}; + +int djvu_handle(struct doc *doc) +{ + ddjvu_message_t *msg; + msg = ddjvu_message_wait(doc->ctx); + while ((msg = ddjvu_message_peek(doc->ctx))) { + if (msg->m_any.tag == DDJVU_ERROR) { + fprintf(stderr,"ddjvu: %s\n", msg->m_error.message); + return 1; + } + ddjvu_message_pop(doc->ctx); + } + return 0; +} + +static void djvu_render(ddjvu_page_t *page, int mode, void *bitmap, int cols, + ddjvu_rect_t *prect, ddjvu_rect_t *rrect) +{ + ddjvu_format_t *fmt; + fmt = ddjvu_format_create(DDJVU_FORMAT_GREY8, 0, 0); + ddjvu_format_set_row_order(fmt, 1); + + ddjvu_page_render(page, mode, prect, rrect, fmt, cols, bitmap); + ddjvu_format_release(fmt); +} + +#define SIZE (1 << 13) +static char img[SIZE * SIZE]; + +int doc_draw(struct doc *doc, fbval_t *bitmap, int p, int rows, int cols, + int zoom, int rotate) +{ + ddjvu_page_t *page; + ddjvu_rect_t rect; + ddjvu_pageinfo_t info; + int iw, ih; + int i, j; + page = ddjvu_page_create_by_pageno(doc->doc, p - 1); + if (!page) + return -1; + if (!ddjvu_page_decoding_done(page)) + if (djvu_handle(doc)) + return -1; + + ddjvu_document_get_pageinfo(doc->doc, p - 1, &info); + iw = ddjvu_page_get_width(page); + ih = ddjvu_page_get_height(page); + rect.x = 0; + rect.y = 0; + rect.w = iw; + rect.h = ih; + + /* mode: DDJVU_RENDER_(BLACK|COLOR|BACKGROUND|FOREGROUND) */ + djvu_render(page, DDJVU_RENDER_FOREGROUND, img, SIZE, &rect, &rect); + ddjvu_page_release(page); + for (i = 0; i < ih * zoom / 10; i++) + for (j = 0; j < iw * zoom / 10; j++) + bitmap[i * cols + j] = img[i * 10 / zoom * SIZE + j * 10 / zoom]; + return 0; +} + +int doc_pages(struct doc *doc) +{ + return ddjvu_document_get_pagenum(doc->doc); +} + +struct doc *doc_open(char *path) +{ + struct doc *doc = malloc(sizeof(*doc)); + doc->ctx = ddjvu_context_create("fbpdf"); + if (!doc->ctx) + goto fail; + doc->doc = ddjvu_document_create_by_filename(doc->ctx, path, 1); + if (!doc->doc) + goto fail; + if (!ddjvu_document_decoding_done(doc->doc)) + if (djvu_handle(doc)) + goto fail; + return doc; +fail: + doc_close(doc); + return NULL; +} + +void doc_close(struct doc *doc) +{ + if (doc->doc) + ddjvu_context_release(doc->ctx); + if (doc->ctx) + ddjvu_document_release(doc->doc); + free(doc); +} diff --git a/doc.c b/pdf.c similarity index 100% rename from doc.c rename to pdf.c -- 2.11.4.GIT