forked from koreader/koreader-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mupdfimg.c
149 lines (121 loc) · 3.57 KB
/
mupdfimg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
KindlePDFViewer: MuPDF abstraction for Lua, only image part
Copyright (C) 2012 Hans-Werner Hilse <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mupdf/pdf.h>
#include "blitbuffer.h"
#include "mupdfimg.h"
#include <stdio.h>
#include <stddef.h>
typedef struct Image {
fz_pixmap *pixmap;
fz_context *context;
} Image;
static int newImage(lua_State *L) {
int cache_size = luaL_optint(L, 1, 8 << 20); // 8 MB limit default
Image *img = (Image*) lua_newuserdata(L, sizeof(Image));
img->pixmap = NULL;
luaL_getmetatable(L, "image");
lua_setmetatable(L, -2);
img->context = fz_new_context(NULL, NULL, cache_size);
return 1;
}
static int loadPNGData(lua_State *L) {
Image *img = (Image*) luaL_checkudata(L, 1, "image");
size_t length;
unsigned char *data = luaL_checklstring(L, 2, &length);
fz_try(img->context) {
img->pixmap = fz_load_png(img->context, data, length);
}
fz_catch(img->context) {
return luaL_error(L, "cannot load PNG data");
}
return 0;
}
static int toBlitBuffer(lua_State *L) {
Image *img = (Image*) luaL_checkudata(L, 1, "image");
BlitBuffer *bb;
int ret;
int w, h;
fz_pixmap *pix;
if(img->pixmap == NULL) {
return luaL_error(L, "no pixmap loaded that we could convert");
}
if(img->pixmap->n == 2) {
pix = img->pixmap;
} else {
fz_try(img->context) {
pix = fz_new_pixmap(img->context, fz_device_gray(img->context), img->pixmap->w, img->pixmap->h);
fz_convert_pixmap(img->context, pix, img->pixmap);
}
fz_catch(img->context) {
return luaL_error(L, "can't claim or convert new grayscale fz_pixmap");
}
}
ret = newBlitBufferNative(L, img->pixmap->w, img->pixmap->h, 0, &bb);
if(ret != 1) {
// TODO (?): fail more gracefully, clean up mem?
return ret;
}
uint8_t *bbptr = (uint8_t*)bb->data;
uint16_t *pmptr = (uint16_t*)pix->samples;
int x, y;
for(y = 0; y < bb->h; y++) {
for(x = 0; x < (bb->w / 2); x++) {
bbptr[x] = (((pmptr[x*2 + 1] & 0xF0) >> 4) | (pmptr[x*2] & 0xF0)) ^ 0xFF;
}
if(bb->w & 1) {
bbptr[x] = (pmptr[x*2] & 0xF0) ^ 0xF0;
}
bbptr += bb->pitch;
pmptr += bb->w;
}
if(pix != img->pixmap) {
fz_drop_pixmap(img->context, pix);
}
return 1;
}
static int freeImage(lua_State *L) {
Image *img = (Image*) luaL_checkudata(L, 1, "image");
// should be save if called twice
if(img->pixmap != NULL) {
fz_drop_pixmap(img->context, img->pixmap);
img->pixmap = NULL;
}
if(img->context != NULL) {
fz_free_context(img->context);
img->context = NULL;
}
return 0;
}
static const struct luaL_Reg mupdfimg_func[] = {
{"new", newImage},
{NULL, NULL}
};
static const struct luaL_Reg image_meth[] = {
{"loadPNGData", loadPNGData},
{"toBlitBuffer", toBlitBuffer},
{"free", freeImage},
{"__gc", freeImage},
{NULL, NULL}
};
int luaopen_mupdfimg(lua_State *L) {
luaL_newmetatable(L, "image");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);
luaL_register(L, NULL, image_meth);
lua_pop(L, 1);
luaL_register(L, "mupdfimg", mupdfimg_func);
return 1;
}