Debian Codegraph @

src/jb2/bmpcoder.cpp

raw
1 /*
2 * bmpcoder.cpp - encoding/decoding bitmaps (DjVu 2 specification, 8.5.9-10)
3 */
4
5 #include "../base/mdjvucfg.h"
6 #include "bmpcoder.h"
7 #include <stdlib.h>
8 #include <string.h>
9
10 // JB2BitmapCoder implementation {{{
11
12 JB2BitmapCoder::JB2BitmapCoder(ZPMemoryWatcher *w) :
13 symbol_width(0, jb2_big_positive_number, w),
14 symbol_height(0, jb2_big_positive_number, w),
15 symbol_width_difference
16 (jb2_big_negative_number, jb2_big_positive_number, w),
17 symbol_height_difference
18 (jb2_big_negative_number, jb2_big_positive_number, w)
19 {
20 }
21
22 JB2BitmapCoder::~JB2BitmapCoder()
23 {
24 }
25
26 void JB2BitmapCoder::reset_numcontexts()
27 {
28 symbol_width.reset();
29 symbol_height.reset();
30 symbol_width_difference.reset();
31 symbol_height_difference.reset();
32 }
33
34 void JB2BitmapCoder::code_row_directly
35 (int32 n, unsigned char *up2, unsigned char *up1, unsigned char *target,
36 unsigned char *erosion)
37 {
38 // demands right margin 2 from up2 and 3 from up1
39
40 /* CONTEXT is 10-bit integer organized like this:
41 *
42 * up2 -> | |A|B|C| |
43 * up1 -> |D|E|F|G|H| - picture
44 * target -> |I|J|.| | |
45 *
46 * CONTEXT: most significant -> |J|I|H|G|F|E|D|C|B|A| <- least significant
47 */
48
49 uint16 context = 0;
50
51 // initialize bits B, C, F, G and H
52 if (up2[0]) context = 2;
53 if (up2[1]) context |= 4;
54 if (up1[0]) context |= 0x20;
55 if (up1[1]) context |= 0x40;
56 if (up1[2]) context |= 0x80;
57
58 for (int32 i = n; i--;)
59 {
60 int pixel = code_pixel(bitmap_direct[context], target++, *erosion++);
61 context >>= 1;
62 context &= 0x17B; // clear H, C and J
63
64 up1++; up2++;
65
66 if (up2[1]) context |= 4; // fill C
67 if (up1[2]) context |= 0x80; // fill H
68 if (pixel) context |= 0x200; // fill J
69 }
70 }
71
72 // TODO: optimize it by unpacking "0 or 1" and ||ing with shifts
73 void JB2BitmapCoder::code_row_by_refinement
74 (int32 n, unsigned char *up1, unsigned char *target, unsigned char *p_up, unsigned char *p_sm, unsigned char *p_dn,
75 unsigned char *erosion)
76 {
77 // demands right margin 2 from all but target and left margin 1 from p_
78
79 /* CONTEXT is 11-bit integer organized like this:
80 *
81 * up1 -> |A|B|C|
82 * target -> |D|.| | - picture
83 *
84 * p_up -> | |E| |
85 * p_sm -> |F|G|H| - prototype
86 * p_dn -> |I|J|K|
87 *
88 * CONTEXT: 0 0 0 0 0 K J I H G F E D C B A
89 */
90
91 uint16 context = 0;
92 if (up1[0]) context = 2; // B
93 if (up1[1]) context |= 4; // C
94 if (p_up[0]) context |= 0x10; // E
95 if (p_sm[-1]) context |= 0x20; // F
96 if (p_sm[0]) context |= 0x40; // G
97 if (p_sm[1]) context |= 0x80; // H
98 if (p_dn[-1]) context |= 0x100; // I
99 if (p_dn[0]) context |= 0x200; // J
100 if (p_dn[1]) context |= 0x400; // K
101
102
103 int32 x = n;
104 while (x--)
105 {
106 int pixel = code_pixel(bitmap_refine[context], target++, *erosion++);
107 context >>= 1;
108 context &= 0x363; // clear C, D, E, H and K
109
110 up1++; p_up++; p_sm++; p_dn++;
111
112 if (up1[1]) context |= 4; // C
113 if (pixel) context |= 8; // D
114 if (p_up[0]) context |= 0x10; // E
115 if (p_sm[1]) context |= 0x80; // H
116 if (p_dn[1]) context |= 0x400; // K
117 }
118 }
119
120 void JB2BitmapCoder::code_image_directly(mdjvu_bitmap_t shape, mdjvu_bitmap_t erosion_mask)
121 {
122 int32 w = mdjvu_bitmap_get_width(shape);
123 int32 h = mdjvu_bitmap_get_height(shape);
124 unsigned char *up2 = (unsigned char *) calloc(w + 3, 1); // 3 bytes are right margin
125 unsigned char *up1 = (unsigned char *) calloc(w + 3, 1);
126 unsigned char *target = (unsigned char *) malloc(w + 3);
127 unsigned char *erosion = (unsigned char *) calloc(w, 1);
128 assert(!erosion_mask || mdjvu_bitmap_get_width(erosion_mask) == w);
129 target[w] = target[w + 1] = target[w + 2] = 0;
130
131 for (int32 y = 0; y < h; y++)
132 {
133 load_row(shape, y, target);
134 if (erosion_mask)
135 mdjvu_bitmap_unpack_row(erosion_mask, erosion, y);
136 code_row_directly(w, up2, up1, target, erosion);
137 save_row(shape, y, target, erosion_mask != NULL);
138
139 unsigned char *t = up2;
140 up2 = up1;
141 up1 = target;
142 target = t;
143 }
144
145 free(up2);
146 free(up1);
147 free(target);
148 free(erosion);
149 }
150
151 void JB2BitmapCoder::code_image_by_refinement/*{{{*/
152 (mdjvu_bitmap_t shape, mdjvu_bitmap_t prototype, mdjvu_bitmap_t erosion_mask)
153 {
154 int32 w = mdjvu_bitmap_get_width(shape);
155 int32 h = mdjvu_bitmap_get_height(shape);
156 int32 pw = mdjvu_bitmap_get_width(prototype);
157 int32 ph = mdjvu_bitmap_get_height(prototype);
158
159 int32 max_width = w > pw ? w : pw;
160 unsigned char *up1 = (unsigned char *) calloc(max_width + 2, 1);
161 unsigned char *target = (unsigned char *) calloc(max_width + 2, 1);
162 unsigned char *erosion = (unsigned char *) calloc(max_width, 1);
163 unsigned char *buf_prototype_up = (unsigned char *) calloc(max_width + 3, 1);
164 unsigned char *buf_prototype_sm = (unsigned char *) calloc(max_width + 3, 1);
165 unsigned char *buf_prototype_dn = (unsigned char *) calloc(max_width + 3, 1);
166 unsigned char *prototype_up = buf_prototype_up + 1; // to have left margin of 1
167 unsigned char *prototype_sm = buf_prototype_sm + 1; // to have left margin of 1
168 unsigned char *prototype_dn = buf_prototype_dn + 1; // to have left margin of 1
169
170 // align (see DjVu2 specs, page 32, bottom)
171 int center_x = w - w / 2; // this favors right (but that agrees with specs)
172 int center_y = h / 2; // this favors top (but that agrees with specs)
173 int proto_center_x = pw - pw / 2;
174 int proto_center_y = ph / 2;
175
176 // (shift_x, shift_y) is a top left corner shift of symbol being decoded
177 // with respect to prototype's top left corner.
178 int shift_x = proto_center_x - center_x;
179 int shift_y = proto_center_y - center_y;
180
181 // prepare upper row -> sm, same row -> dn (to be raised in the loop)
182 int y;
183 int proto_unpack = shift_x < 0 ? -shift_x : 0;
184 int code_shift = shift_x > 0 ? shift_x : 0;
185
186 y = shift_y - 1;
187 if (y >= 0 && y < ph)
188 mdjvu_bitmap_unpack_row_0_or_1(prototype, prototype_sm + proto_unpack, y);
189 y = shift_y;
190 if (y >= 0 && y < ph)
191 mdjvu_bitmap_unpack_row_0_or_1(prototype, prototype_dn + proto_unpack, y);
192
193 for (y = 0; y < (int) h; y++)
194 {
195 // prepare three prototype rows by unpacking lower one
196 // and shifting all others
197
198 int proto_y_dn = y + shift_y + 1;
199
200 if (proto_y_dn >= 0)
201 {
202 // we have to get prototype_dn or clear it
203 if (proto_y_dn < ph)
204 {
205 // rotate prototype rows to get a place for prototype_dn
206 unsigned char *t = prototype_up;
207 prototype_up = prototype_sm;
208 prototype_sm = prototype_dn;
209 prototype_dn = t;
210
211 mdjvu_bitmap_unpack_row_0_or_1(prototype,
212 prototype_dn + proto_unpack,
213 proto_y_dn);
214 }
215 else if (proto_y_dn < ph + 3)
216 {
217 unsigned char *t = prototype_up;
218 prototype_up = prototype_sm;
219 prototype_sm = prototype_dn;
220 prototype_dn = t;
221 memset(prototype_dn, 0, max_width);
222 }
223 // else all three prototype rows are and will be empty - do nothing
224 }
225
226 // code y-th row
227 load_row(shape, y, target);
228 if (erosion_mask)
229 mdjvu_bitmap_unpack_row(erosion_mask, erosion, y);
230 code_row_by_refinement(w, up1, target,
231 prototype_up + code_shift,
232 prototype_sm + code_shift,
233 prototype_dn + code_shift, erosion);
234 save_row(shape, y, target, erosion_mask != NULL);
235
236 unsigned char *t = up1;
237 up1 = target;
238 target = t;
239 }
240
241 free(up1);
242 free(target);
243 free(erosion);
244 free(buf_prototype_up);
245 free(buf_prototype_sm);
246 free(buf_prototype_dn);
247 }/*}}}*/
248
249 // JB2BitmapCoder }}}
250
251 // JB2BitmapDecoder implementation {{{
252
253 JB2BitmapDecoder::JB2BitmapDecoder(ZPDecoder &z, ZPMemoryWatcher *w)
254 : JB2BitmapCoder(w), zp(z) {}
255
256 int JB2BitmapDecoder::code_pixel(ZPBitContext &context, unsigned char *pixel, int erosion)
257 {
258 return *pixel = zp.decode(context);
259 }
260
261 mdjvu_bitmap_t JB2BitmapDecoder::decode(mdjvu_image_t img, mdjvu_bitmap_t proto)
262 {
263 if (proto)
264 {
265 int32 pw = mdjvu_bitmap_get_width(proto);
266 int32 ph = mdjvu_bitmap_get_height(proto);
267 int32 w = pw + zp.decode(symbol_width_difference);
268 int32 h = ph + zp.decode(symbol_height_difference);
269 mdjvu_bitmap_t shape = mdjvu_image_new_bitmap(img, w, h);
270
271 code_image_by_refinement(shape, proto, NULL);
272
273 return shape;
274 }
275 else
276 {
277 int32 w = zp.decode(symbol_width);
278 int32 h = zp.decode(symbol_height);
279 mdjvu_bitmap_t shape = mdjvu_image_new_bitmap(img, w, h);
280
281 code_image_directly(shape, NULL);
282
283 return shape;
284 }
285 }
286
287 void JB2BitmapDecoder::load_row(mdjvu_bitmap_t sh, int32 y, unsigned char *row){}
288
289 void JB2BitmapDecoder::save_row(mdjvu_bitmap_t sh, int32 y, unsigned char *row, int erosion)
290 {
291 mdjvu_bitmap_pack_row(sh, row, y);
292 }
293
294 // JB2BitmapDecoder }}}
295
296 // JB2BitmapEncoder implementation {{{
297
298 JB2BitmapEncoder::JB2BitmapEncoder(ZPEncoder &z, ZPMemoryWatcher *w):
299 JB2BitmapCoder(w), zp(z) {}
300
301 int JB2BitmapEncoder::code_pixel(ZPBitContext &context, unsigned char *pixel, int erosion)
302 {
303 if (erosion)
304 *pixel = context.get_more_probable_bit();
305 zp.encode(*pixel, context);
306 return *pixel;
307 }
308
309 void JB2BitmapEncoder::encode(mdjvu_bitmap_t sh, mdjvu_bitmap_t proto, mdjvu_bitmap_t erosion_mask)
310 {
311 if (proto)
312 {
313 int32 pw = mdjvu_bitmap_get_width(proto);
314 int32 ph = mdjvu_bitmap_get_height(proto);
315 int32 w = mdjvu_bitmap_get_width(sh);
316 int32 h = mdjvu_bitmap_get_height(sh);
317 zp.encode(w - pw, symbol_width_difference);
318 zp.encode(h - ph, symbol_height_difference);
319
320 code_image_by_refinement(sh, proto, erosion_mask);
321 }
322 else
323 {
324 int32 w = mdjvu_bitmap_get_width(sh);
325 int32 h = mdjvu_bitmap_get_height(sh);
326 zp.encode(w, symbol_width);
327 zp.encode(h, symbol_height);
328
329 code_image_directly(sh, erosion_mask);
330 }
331 }
332
333 void JB2BitmapEncoder::save_row(mdjvu_bitmap_t sh, int32 y, unsigned char *row, int erosion)
334 {
335 if (erosion)
336 mdjvu_bitmap_pack_row(sh, row, y);
337 }
338
339 void JB2BitmapEncoder::load_row(mdjvu_bitmap_t sh, int32 y, unsigned char *row)
340 {
341 mdjvu_bitmap_unpack_row_0_or_1(sh, row, y);
342 }
343
344 // JB2BitmapEncoder }}}