2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00

build libk2pdfopt as a shared library

This commit is contained in:
chrox 2012-11-09 23:25:01 +08:00
parent 4494213363
commit 44361faeee
4 changed files with 188 additions and 31 deletions

View File

@ -115,7 +115,7 @@ LUALIB := $(LUADIR)/src/libluajit.a
POPENNSLIB := $(POPENNSDIR)/libpopen_noshell.a
K2PDFOPTLIB := $(K2PDFOPTLIBDIR)/libk2pdfopt.a
K2PDFOPTLIB := $(LIBDIR)/libk2pdfopt.so
all: kpdfview extr
@ -136,7 +136,6 @@ kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o drawcontext.o koptcontext.o inp
ft.o \
lfs.o \
mupdfimg.o \
$(K2PDFOPTLIB) \
$(MUPDFLIBS) \
$(THIRDPARTYLIBS) \
$(LUALIB) \
@ -148,7 +147,7 @@ kpdfview: kpdfview.o einkfb.o pdf.o blitbuffer.o drawcontext.o koptcontext.o inp
$(STATICLIBSTDCPP) \
$(LDFLAGS) \
-o $@ \
-lm -ldl -lpthread -ldjvulibre -ljpeg -L$(MUPDFLIBDIR) -L$(LIBDIR)\
-lm -ldl -lpthread -lk2pdfopt -ldjvulibre -ljpeg -L$(MUPDFLIBDIR) -L$(LIBDIR)\
$(EMU_LDFLAGS) \
$(DYNAMICLIBSTDCPP)
@ -280,7 +279,9 @@ $(POPENNSLIB):
$(MAKE) -C $(POPENNSDIR) CC="$(CC)" AR="$(AR)"
$(K2PDFOPTLIB):
$(MAKE) -C $(K2PDFOPTLIBDIR) BUILDMODE=static CC="$(CC)" CFLAGS="$(CFLAGS) -I../$(DJVUDIR)/ -I../$(MUPDFDIR)/" AR="$(AR)"
$(MAKE) -C $(K2PDFOPTLIBDIR) BUILDMODE=shared CC="$(CC)" CFLAGS="$(CFLAGS)" AR="$(AR)" all
test -d $(LIBDIR) || mkdir $(LIBDIR)
cp -a $(K2PDFOPTLIBDIR)/libk2pdfopt.so* $(LIBDIR)
thirdparty: $(MUPDFLIBS) $(THIRDPARTYLIBS) $(LUALIB) $(DJVULIBS) $(CRENGINELIBS) $(POPENNSLIB) $(K2PDFOPTLIB)

57
djvu.c
View File

@ -473,12 +473,63 @@ static int closePage(lua_State *L) {
}
static int reflowPage(lua_State *L) {
DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage");
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
KOPTContext *kctx = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
ddjvu_render_mode_t mode = (int) luaL_checkint(L, 3);
WILLUSBITMAP _src, *src;
ddjvu_rect_t prect;
ddjvu_rect_t rrect;
k2pdfopt_djvu_reflow(kc, page->page_ref, page->doc->context, mode, page->doc->pixelformat);
int px, py, pw, ph, rx, ry, rw, rh, idpi, status;
double zoom = kctx->zoom;
double dpi = 250*zoom;
px = 0;
py = 0;
pw = ddjvu_page_get_width(page->page_ref);
ph = ddjvu_page_get_height(page->page_ref);
idpi = ddjvu_page_get_resolution(page->page_ref);
prect.x = px;
prect.y = py;
rx = (int)kctx->bbox.x0;
ry = (int)kctx->bbox.y0;
rw = (int)(kctx->bbox.x1 - kctx->bbox.x0);
rh = (int)(kctx->bbox.y1 - kctx->bbox.y0);
do {
prect.w = pw * dpi / idpi;
prect.h = ph * dpi / idpi;
rrect.x = rx * dpi / idpi;
rrect.y = ry * dpi / idpi;
rrect.w = rw * dpi / idpi;
rrect.h = rh * dpi / idpi;
printf("rendering page:%d,%d,%d,%d dpi:%.0f idpi:%.0d\n",rrect.x,rrect.y,rrect.w,rrect.h,dpi,idpi);
kctx->zoom = zoom;
zoom *= kctx->shrink_factor;
dpi *= kctx->shrink_factor;
} while (rrect.w > kctx->read_max_width | rrect.h > kctx->read_max_height);
src = &_src;
bmp_init(src);
src->width = rrect.w;
src->height = rrect.h;
src->bpp = 8;
bmp_alloc(src);
if (src->bpp == 8) {
int ii;
for (ii = 0; ii < 256; ii++)
src->red[ii] = src->blue[ii] = src->green[ii] = ii;
}
ddjvu_format_set_row_order(page->doc->pixelformat, 1);
status = ddjvu_page_render(page->page_ref, mode, &prect, &rrect, page->doc->pixelformat,
bmp_bytewidth(src), (char *) src->data);
k2pdfopt_reflow_bmp(kctx, src);
bmp_free(src);
return 0;
}

View File

@ -19,30 +19,33 @@
#include "koptcontext.h"
static int newKOPTContext(lua_State *L) {
int trim = luaL_optint(L, 1, 1);
int wrap = luaL_optint(L, 2, 1);
int indent = luaL_optint(L, 3, 1);
int rotate = luaL_optint(L, 4, 0);
int columns = luaL_optint(L, 5, 2);
int offset_x = luaL_optint(L, 6, 0);
int offset_y = luaL_optint(L, 7, 0);
int dev_width = luaL_optint(L, 8, 600);
int dev_height = luaL_optint(L, 9, 800);
int page_width = luaL_optint(L, 10, 600);
int page_height = luaL_optint(L, 11, 800);
int straighten = luaL_optint(L, 12, 0);
int justification = luaL_optint(L, 13, -1);
int trim = 1;
int wrap = 1;
int indent = 1;
int rotate = 0;
int columns = 2;
int offset_x = 0;
int offset_y = 0;
int dev_width = 600;
int dev_height = 800;
int page_width = 600;
int page_height = 800;
int straighten = 0;
int justification = -1;
int read_max_width = 3000;
int read_max_height = 4000;
double zoom = luaL_optnumber(L, 14, (double) 1.0);
double margin = luaL_optnumber(L, 15, (double) 0.06);
double quality = luaL_optnumber(L, 16, (double) 1.0);
double contrast = luaL_optnumber(L, 17, (double) 1.0);
double defect_size = luaL_optnumber(L, 18, (double) 1.0);
double line_spacing = luaL_optnumber(L, 19, (double) 1.2);
double word_spacing = luaL_optnumber(L, 20, (double) 1.375);
double zoom = 1.0;
double margin = 0.06;
double quality = 1.0;
double contrast = 1.0;
double defect_size = 1.0;
double line_spacing = 1.2;
double word_spacing = 1.375;
double shrink_factor = 0.9;
uint8_t *data = NULL;
fz_rect bbox = {0, 0, 0, 0};
BBox bbox = {0, 0, 0, 0};
KOPTContext *kc = (KOPTContext*) lua_newuserdata(L, sizeof(KOPTContext));
@ -59,6 +62,8 @@ static int newKOPTContext(lua_State *L) {
kc->page_height = page_height;
kc->straighten = straighten;
kc->justification = justification;
kc->read_max_width = read_max_width;
kc->read_max_height = read_max_height;
kc->zoom = zoom;
kc->margin = margin;
@ -67,6 +72,7 @@ static int newKOPTContext(lua_State *L) {
kc->defect_size = defect_size;
kc->line_spacing = line_spacing;
kc->word_spacing = word_spacing;
kc->shrink_factor = shrink_factor;
kc->data = data;
kc->bbox = bbox;

105
pdf.c
View File

@ -513,12 +513,111 @@ static int closePage(lua_State *L) {
return 0;
}
/* bmpmupdf.c from willuslib */
static int bmpmupdf_pixmap_to_bmp(WILLUSBITMAP *bmp, fz_context *ctx, fz_pixmap *pixmap) {
unsigned char *p;
int ncomp, i, row, col;
bmp->width = fz_pixmap_width(ctx, pixmap);
bmp->height = fz_pixmap_height(ctx, pixmap);
ncomp = fz_pixmap_components(ctx, pixmap);
/* Has to be 8-bit or RGB */
if (ncomp != 2 && ncomp != 4)
return (-1);
bmp->bpp = (ncomp == 2) ? 8 : 24;
bmp_alloc(bmp);
if (ncomp == 2)
for (i = 0; i < 256; i++)
bmp->red[i] = bmp->green[i] = bmp->blue[i] = i;
p = fz_pixmap_samples(ctx, pixmap);
if (ncomp == 1)
for (row = 0; row < bmp->height; row++) {
unsigned char *dest;
dest = bmp_rowptr_from_top(bmp, row);
memcpy(dest, p, bmp->width);
p += bmp->width;
}
else if (ncomp == 2)
for (row = 0; row < bmp->height; row++) {
unsigned char *dest;
dest = bmp_rowptr_from_top(bmp, row);
for (col = 0; col < bmp->width; col++, dest++, p += 2)
dest[0] = p[0];
}
else
for (row = 0; row < bmp->height; row++) {
unsigned char *dest;
dest = bmp_rowptr_from_top(bmp, row);
for (col = 0; col < bmp->width;
col++, dest += ncomp - 1, p += ncomp)
memcpy(dest, p, ncomp - 1);
}
return (0);
}
static int reflowPage(lua_State *L) {
PdfPage *page = (PdfPage*) luaL_checkudata(L, 1, "pdfpage");
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
KOPTContext *kctx = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
fz_device *dev;
fz_pixmap *pix;
fz_rect bounds,bounds2;
fz_matrix ctm;
fz_bbox bbox;
WILLUSBITMAP _src, *src;
k2pdfopt_mupdf_reflow(kc, page->doc->xref, page->page, page->doc->context);
pix = NULL;
fz_var(pix);
bounds.x0 = kctx->bbox.x0;
bounds.y0 = kctx->bbox.y0;
bounds.x1 = kctx->bbox.x1;
bounds.y1 = kctx->bbox.y1;
double dpp,zoom;
zoom = kctx->zoom;
double dpi = 250*zoom*kctx->quality;
do {
dpp = dpi / 72.;
ctm = fz_scale(dpp, dpp);
// ctm=fz_concat(ctm,fz_rotate(rotation));
bounds2 = fz_transform_rect(ctm, bounds);
bbox = fz_round_rect(bounds2);
printf("reading page:%d,%d,%d,%d zoom:%.2f dpi:%.0f\n",bbox.x0,bbox.y0,bbox.x1,bbox.y1,zoom,dpi);
kctx->zoom = zoom;
zoom *= kctx->shrink_factor;
dpi *= kctx->shrink_factor;
} while (bbox.x1 > kctx->read_max_width | bbox.y1 > kctx->read_max_height);
pix = fz_new_pixmap_with_bbox(page->doc->context, fz_device_gray, bbox);
fz_clear_pixmap_with_value(page->doc->context, pix, 0xff);
dev = fz_new_draw_device(page->doc->context, pix);
#ifdef MUPDF_TRACE
fz_device *tdev;
fz_try(page->doc->context) {
tdev = fz_new_trace_device(page->doc->context);
fz_run_page(page->doc->xref, page->page, tdev, ctm, NULL);
}
fz_always(page->doc->context) {
fz_free_device(tdev);
}
#endif
fz_run_page(page->doc->xref, page->page, dev, ctm, NULL);
fz_free_device(dev);
if (kctx->contrast >= 0.0) {
fz_gamma_pixmap(page->doc->context, pix, kctx->contrast);
}
src = &_src;
bmp_init(src);
int status = bmpmupdf_pixmap_to_bmp(src, page->doc->context, pix);
fz_drop_pixmap(page->doc->context, pix);
k2pdfopt_reflow_bmp(kctx, src);
bmp_free(src);
return 0;
}