Skip to content

Commit 8f26cc3

Browse files
committed
Merge pull request koreader#10 from chrox/kctx_fix
update libk2pdfopt for new koptcontext
2 parents 47ba71a + 9f353b3 commit 8f26cc3

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

djvu.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18+
1819
#include <math.h>
1920
#include <string.h>
2021
#include <errno.h>
2122
#include <pthread.h>
23+
#include <assert.h>
2224
#include <libdjvu/miniexp.h>
2325
#include <libdjvu/ddjvuapi.h>
2426

@@ -488,7 +490,7 @@ static int getAutoBBox(lua_State *L) {
488490
prect.h = ddjvu_page_get_height(page->page_ref);
489491
rrect = prect;
490492

491-
WILLUSBITMAP *src = malloc(sizeof(WILLUSBITMAP));
493+
WILLUSBITMAP *src = &kctx->src;
492494
bmp_init(src);
493495
src->width = rrect.w;
494496
src->height = rrect.h;
@@ -504,7 +506,6 @@ static int getAutoBBox(lua_State *L) {
504506
ddjvu_page_render(page->page_ref, 0, &prect, &rrect, page->doc->pixelformat,
505507
bmp_bytewidth(src), (char *) src->data);
506508

507-
kctx->src = src;
508509
k2pdfopt_crop_bmp(kctx);
509510

510511
lua_pushnumber(L, ((double)kctx->bbox.x0));
@@ -548,7 +549,7 @@ static int reflowPage(lua_State *L) {
548549
printf("rendering page:%d,%d,%d,%d\n",rrect.x,rrect.y,rrect.w,rrect.h);
549550
kctx->zoom = scale;
550551

551-
WILLUSBITMAP *src = malloc(sizeof(WILLUSBITMAP));
552+
WILLUSBITMAP *src = &kctx->src;
552553
bmp_init(src);
553554
src->width = rrect.w;
554555
src->height = rrect.h;
@@ -566,7 +567,6 @@ static int reflowPage(lua_State *L) {
566567
status = ddjvu_page_render(page->page_ref, mode, &prect, &rrect, page->doc->pixelformat,
567568
bmp_bytewidth(src), (char *) src->data);
568569

569-
kctx->src = src;
570570
if (kctx->precache) {
571571
pthread_t rf_thread;
572572
pthread_attr_t attr;
@@ -586,7 +586,11 @@ static int drawReflowedPage(lua_State *L) {
586586
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
587587
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
588588

589-
uint8_t *koptr = kc->data;
589+
assert(kc->dst.data != NULL);
590+
assert(kc->dst.width >= bb->w);
591+
assert(kc->dst.height >= bb->h);
592+
593+
uint8_t *koptr = kc->dst.data;
590594
uint8_t *bbptr = bb->data;
591595

592596
int x_offset = 0;

koptcontext.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ static int newKOPTContext(lua_State *L) {
4545
double word_spacing = 1.375;
4646
double shrink_factor = 0.9;
4747

48-
uint8_t *data = NULL;
4948
BBox bbox = {0, 0, 0, 0};
50-
WILLUSBITMAP *src;
5149
int precache = 0;
5250

5351
KOPTContext *kc = (KOPTContext*) lua_newuserdata(L, sizeof(KOPTContext));
@@ -78,17 +76,28 @@ static int newKOPTContext(lua_State *L) {
7876
kc->word_spacing = word_spacing;
7977
kc->shrink_factor = shrink_factor;
8078

81-
kc->data = data;
8279
kc->bbox = bbox;
83-
kc->src = src;
8480
kc->precache = precache;
8581

82+
bmp_init(&kc->src);
83+
bmp_init(&kc->dst);
84+
8685
luaL_getmetatable(L, "koptcontext");
8786
lua_setmetatable(L, -2);
8887

8988
return 1;
9089
}
9190

91+
static int freeContext(lua_State *L) {
92+
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
93+
/* Don't worry about the src bitmap in context. It's freed as soon as it's
94+
* been used in either reflow or autocrop. But we should take care of dst
95+
* bitmap since the usage of dst bitmap is delayed most of the times.
96+
*/
97+
bmp_free(&kc->dst);
98+
return 0;
99+
}
100+
92101
static int kcSetBBox(lua_State *L) {
93102
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 1, "koptcontext");
94103
kc->bbox.x0 = luaL_checknumber(L, 2);
@@ -267,6 +276,9 @@ static const struct luaL_Reg koptcontext_meth[] = {
267276

268277
{"setPreCache", kcSetPreCache},
269278
{"isPreCache", kcIsPreCache},
279+
280+
{"free", freeContext},
281+
{"__gc", freeContext},
270282
{NULL, NULL}
271283
};
272284

libk2pdfopt

lua_gettext.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121

22-
22+
#include <locale.h>
2323
#include "lua_gettext.h"
2424

25-
2625
extern int _nl_msg_cat_cntr;
2726

28-
29-
3027
static int lua_gettext_init(lua_State *L) {
3128
const char* locale_dir = luaL_checkstring(L, 1);
3229
const char* package = luaL_checkstring(L, 2);

pdf.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <math.h>
2121
#include <stddef.h>
2222
#include <pthread.h>
23+
#include <assert.h>
2324
#include <fitz/fitz-internal.h>
2425

2526
#include "blitbuffer.h"
@@ -586,14 +587,12 @@ static int getAutoBBox(lua_State *L) {
586587
return luaL_error(L, "cannot calculate bbox for page");
587588
}
588589

589-
WILLUSBITMAP *src = malloc(sizeof(WILLUSBITMAP));
590+
WILLUSBITMAP *src = &kctx->src;
590591
bmp_init(src);
591592

592593
bmpmupdf_pixmap_to_bmp(src, page->doc->context, pix);
593594
fz_drop_pixmap(page->doc->context, pix);
594595

595-
kctx->src = src;
596-
597596
k2pdfopt_crop_bmp(kctx);
598597

599598
lua_pushnumber(L, ((double)kctx->bbox.x0));
@@ -652,13 +651,12 @@ static int reflowPage(lua_State *L) {
652651
fz_run_page(page->doc->xref, page->page, dev, ctm, NULL);
653652
fz_free_device(dev);
654653

655-
WILLUSBITMAP *src = malloc(sizeof(WILLUSBITMAP));
654+
WILLUSBITMAP *src = &kctx->src;
656655
bmp_init(src);
657656

658657
int status = bmpmupdf_pixmap_to_bmp(src, page->doc->context, pix);
659658
fz_drop_pixmap(page->doc->context, pix);
660659

661-
kctx->src = src;
662660
if (kctx->precache) {
663661
pthread_t rf_thread;
664662
pthread_attr_t attr;
@@ -678,7 +676,11 @@ static int drawReflowedPage(lua_State *L) {
678676
KOPTContext *kc = (KOPTContext*) luaL_checkudata(L, 2, "koptcontext");
679677
BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 3, "blitbuffer");
680678

681-
uint8_t *koptr = kc->data;
679+
assert(kc->dst.data != NULL);
680+
assert(kc->dst.width >= bb->w);
681+
assert(kc->dst.height >= bb->h);
682+
683+
uint8_t *koptr = kc->dst.data;
682684
uint8_t *bbptr = bb->data;
683685

684686
int x_offset = 0;

0 commit comments

Comments
 (0)