-
Notifications
You must be signed in to change notification settings - Fork 7
/
jbig2_refinement.c
440 lines (389 loc) · 14.4 KB
/
jbig2_refinement.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
jbig2dec
Copyright (C) 2004 Artifex Software, Inc.
This software is provided AS-IS with no warranty,
either express or implied.
This software is distributed under license and may not
be copied, modified or distributed except as expressly
authorized under the terms of the license contained in
the file LICENSE in this distribution.
For further licensing information refer to http://artifex.com/ or
contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861.
*/
/**
* Generic Refinement region handlers.
**/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "os_types.h"
#include <stddef.h>
#include <string.h> /* memcpy(), memset() */
#include <stdio.h>
#include "jbig2.h"
#include "jbig2_priv.h"
#include "jbig2_arith.h"
#include "jbig2_generic.h"
#include "jbig2_image.h"
static int
jbig2_decode_refinement_template0(Jbig2Ctx *ctx,
Jbig2Segment *segment,
const Jbig2RefinementRegionParams *params,
Jbig2ArithState *as,
Jbig2Image *image,
Jbig2ArithCx *GR_stats)
{
return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
"refinement region template 0 NYI");
}
static int
jbig2_decode_refinement_template0_unopt(Jbig2Ctx *ctx,
Jbig2Segment *segment,
const Jbig2RefinementRegionParams *params,
Jbig2ArithState *as,
Jbig2Image *image,
Jbig2ArithCx *GR_stats)
{
const int GRW = image->width;
const int GRH = image->height;
const int dx = params->DX;
const int dy = params->DY;
Jbig2Image *ref = params->reference;
uint32_t CONTEXT;
int x,y;
bool bit;
for (y = 0; y < GRH; y++) {
for (x = 0; x < GRW; x++) {
CONTEXT = 0;
CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0],
y + params->grat[1]) << 3;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+1) << 4;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+1) << 5;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+1) << 6;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+0) << 7;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+0) << 8;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+0) << 9;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy-1) << 10;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy-1) << 11;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+params->grat[2],
y-dy+params->grat[3]) << 12;
bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
jbig2_image_set_pixel(image, x, y, bit);
}
}
#ifdef JBIG2_DEBUG_DUMP
{
static count = 0;
char name[32];
snprintf(name, 32, "refin-%d.pbm", count);
jbig2_image_write_pbm_file(ref, name);
snprintf(name, 32, "refout-%d.pbm", count);
jbig2_image_write_pbm_file(image, name);
count++;
}
#endif
return 0;
}
static int
jbig2_decode_refinement_template1_unopt(Jbig2Ctx *ctx,
Jbig2Segment *segment,
const Jbig2RefinementRegionParams *params,
Jbig2ArithState *as,
Jbig2Image *image,
Jbig2ArithCx *GR_stats)
{
const int GRW = image->width;
const int GRH = image->height;
const int dx = params->DX;
const int dy = params->DY;
Jbig2Image *ref = params->reference;
uint32_t CONTEXT;
int x,y;
bool bit;
for (y = 0; y < GRH; y++) {
for (x = 0; x < GRW; x++) {
CONTEXT = 0;
CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+1) << 4;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+1) << 5;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+0) << 6;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+0) << 7;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+0) << 8;
CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy-1) << 9;
bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
jbig2_image_set_pixel(image, x, y, bit);
}
}
#ifdef JBIG2_DEBUG_DUMP
{
static count = 0;
char name[32];
snprintf(name, 32, "refin-%d.pbm", count);
jbig2_image_write_pbm_file(ref, name);
snprintf(name, 32, "refout-%d.pbm", count);
jbig2_image_write_pbm_file(image, name);
count++;
}
#endif
return 0;
}
static int
jbig2_decode_refinement_template1(Jbig2Ctx *ctx,
Jbig2Segment *segment,
const Jbig2RefinementRegionParams *params,
Jbig2ArithState *as,
Jbig2Image *image,
Jbig2ArithCx *GR_stats)
{
const int GRW = image->width;
const int GRH = image->height;
const int stride = image->stride;
const int refstride = params->reference->stride;
const int dy = params->DY;
byte *grreg_line = (byte *)image->data;
byte *grref_line = (byte *)params->reference->data;
int x,y;
for (y = 0; y < GRH; y++) {
const int padded_width = (GRW + 7) & -8;
uint32_t CONTEXT;
uint32_t refline_m1; /* previous line of the reference bitmap */
uint32_t refline_0; /* current line of the reference bitmap */
uint32_t refline_1; /* next line of the reference bitmap */
uint32_t line_m1; /* previous line of the decoded bitmap */
line_m1 = (y >= 1) ? grreg_line[-stride] : 0;
refline_m1 = ((y-dy) >= 1) ? grref_line[(-1-dy)*stride] << 2: 0;
refline_0 = (((y-dy) > 0) && ((y-dy) < GRH)) ? grref_line[(0-dy)*stride] << 4 : 0;
refline_1 = (y < GRH - 1) ? grref_line[(+1-dy)*stride] << 7 : 0;
CONTEXT = ((line_m1 >> 5) & 0x00e) |
((refline_1 >> 5) & 0x030) |
((refline_0 >> 5) & 0x1c0) |
((refline_m1 >> 5) & 0x200);
for (x = 0; x < padded_width; x += 8) {
byte result = 0;
int x_minor;
const int minor_width = GRW - x > 8 ? 8 : GRW - x;
if (y >= 1) {
line_m1 = (line_m1 << 8) |
(x + 8 < GRW ? grreg_line[-stride + (x >> 3) + 1] : 0);
refline_m1 = (refline_m1 << 8) |
(x + 8 < GRW ? grref_line[-refstride + (x >> 3) + 1] << 2 : 0);
}
refline_0 = (refline_0 << 8) |
(x + 8 < GRW ? grref_line[(x >> 3) + 1] << 4 : 0);
if (y < GRH - 1)
refline_1 = (refline_1 << 8) |
(x + 8 < GRW ? grref_line[+refstride + (x >> 3) + 1] << 7 : 0);
else
refline_1 = 0;
/* this is the speed critical inner-loop */
for (x_minor = 0; x_minor < minor_width; x_minor++) {
bool bit;
bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
result |= bit << (7 - x_minor);
CONTEXT = ((CONTEXT & 0x0d6) << 1) | bit |
((line_m1 >> (9 - x_minor)) & 0x002) |
((refline_1 >> (9 - x_minor)) & 0x010) |
((refline_0 >> (9 - x_minor)) & 0x040) |
((refline_m1 >> (9 - x_minor)) & 0x200);
}
grreg_line[x>>3] = result;
}
grreg_line += stride;
grref_line += refstride;
}
return 0;
}
/**
* jbig2_decode_refinement_region: Decode a generic refinement region.
* @ctx: The context for allocation and error reporting.
* @segment: A segment reference for error reporting.
* @params: Decoding parameter set.
* @as: Arithmetic decoder state.
* @image: Where to store the decoded image.
* @GR_stats: Arithmetic stats.
*
* Decodes a generic refinement region, according to section 6.3.
* an already allocated Jbig2Image object in @image for the result.
*
* Because this API is based on an arithmetic decoding state, it is
* not suitable for MMR decoding.
*
* Return code: 0 on success.
**/
int
jbig2_decode_refinement_region(Jbig2Ctx *ctx,
Jbig2Segment *segment,
const Jbig2RefinementRegionParams *params,
Jbig2ArithState *as,
Jbig2Image *image,
Jbig2ArithCx *GR_stats)
{
{
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
"decoding generic refinement region with offset %d,%x,\n"
" GRTEMPLATE=%d, TPGRON=%d, RA1=(%d,%d) RA2=(%d,%d)\n",
params->DX, params->DY, params->GRTEMPLATE, params->TPGRON,
params->grat[0], params->grat[1], params->grat[2], params->grat[3]);
}
if (params->TPGRON)
return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
"decode_refinement_region: typical prediction coding NYI");
if (params->GRTEMPLATE)
return jbig2_decode_refinement_template1_unopt(ctx, segment, params,
as, image, GR_stats);
else
return jbig2_decode_refinement_template0_unopt(ctx, segment, params,
as, image, GR_stats);
}
/**
* Find the first referred-to intermediate region segment
* with a non-NULL result for use as a reference image
*/
Jbig2Segment *
jbig2_region_find_referred(Jbig2Ctx *ctx,Jbig2Segment *segment)
{
const int nsegments = segment->referred_to_segment_count;
Jbig2Segment *rsegment;
int index;
for (index = 0; index < nsegments; index++) {
rsegment = jbig2_find_segment(ctx,
segment->referred_to_segments[index]);
if (rsegment == NULL) {
jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
"could not find referred to segment %d",
segment->referred_to_segments[index]);
continue;
}
switch (rsegment->flags & 63) {
case 4: /* intermediate text region */
case 20: /* intermediate halftone region */
case 36: /* intermediate generic region */
case 40: /* intermediate generic refinement region */
if (rsegment->result) return rsegment;
break;
default: /* keep looking */
break;
}
}
/* no appropriate reference was found. */
return NULL;
}
/**
* Handler for generic refinement region segments
*/
int
jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
const byte *segment_data)
{
Jbig2RefinementRegionParams params;
Jbig2RegionSegmentInfo rsi;
int offset = 0;
byte seg_flags;
/* 7.4.7 */
if (segment->data_length < 18)
return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
"Segment too short");
jbig2_get_region_segment_info(&rsi, segment_data);
jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
"generic region: %d x %d @ (%d, %d), flags = %02x",
rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
/* 7.4.7.2 */
seg_flags = segment_data[17];
params.GRTEMPLATE = seg_flags & 0x01;
params.TPGRON = seg_flags & 0x02 ? 1 : 0;
jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
"segment flags = %02x %s%s", seg_flags,
params.GRTEMPLATE ? " GRTEMPLATE" :"",
params.TPGRON ? " TPGRON" : "" );
if (seg_flags & 0xFC)
jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
"reserved segment flag bits are non-zero");
offset += 18;
/* 7.4.7.3 */
if (!params.GRTEMPLATE) {
if (segment->data_length < 22)
return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
"Segment too short");
params.grat[0] = segment_data[offset + 0];
params.grat[1] = segment_data[offset + 1];
params.grat[2] = segment_data[offset + 2];
params.grat[3] = segment_data[offset + 3];
jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
"grat1: (%d, %d) grat2: (%d, %d)",
params.grat[0], params.grat[1],
params.grat[2], params.grat[3]);
offset += 4;
}
/* 7.4.7.4 - set up the reference image */
if (segment->referred_to_segment_count) {
Jbig2Segment *ref;
ref = jbig2_region_find_referred(ctx, segment);
if (ref == NULL)
return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
"could not find reference bitmap!");
/* the reference bitmap is the result of a previous
intermediate region segment; the reference selection
rules say to use the first one available, and not to
reuse any intermediate result, so we simply clone it
and free the original to keep track of this. */
params.reference = jbig2_image_clone(ctx, ref->result);
jbig2_image_release(ctx, ref->result);
ref->result = NULL;
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
"found reference bitmap in segment %d", ref->number);
} else {
/* the reference is just (a subset of) the page buffer */
params.reference = jbig2_image_clone(ctx,
ctx->pages[ctx->current_page].image);
/* TODO: subset the image if appropriate */
}
/* 7.4.7.5 */
params.DX = 0;
params.DY = 0;
{
Jbig2WordStream *ws;
Jbig2ArithState *as;
Jbig2ArithCx *GR_stats = NULL;
int stats_size;
Jbig2Image *image;
int code;
image = jbig2_image_new(ctx, rsi.width, rsi.height);
if (image == NULL)
return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
"unable to allocate refinement image");
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
"allocated %d x %d image buffer for region decode results",
rsi.width, rsi.height);
stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
GR_stats = jbig2_alloc(ctx->allocator, stats_size);
memset(GR_stats, 0, stats_size);
ws = jbig2_word_stream_buf_new(ctx, segment_data + offset,
segment->data_length - offset);
as = jbig2_arith_new(ctx, ws);
code = jbig2_decode_refinement_region(ctx, segment, ¶ms,
as, image, GR_stats);
jbig2_free(ctx->allocator, as);
jbig2_word_stream_buf_free(ctx, ws);
jbig2_free(ctx->allocator, GR_stats);
if ((segment->flags & 63) == 40) {
/* intermediate region. save the result for later */
segment->result = image;
} else {
/* immediate region. composite onto the page */
jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
"composing %dx%d decoded refinement region onto page at (%d, %d)",
rsi.width, rsi.height, rsi.x, rsi.y);
jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page],
image, rsi.x, rsi.y, rsi.op);
jbig2_image_release(ctx, image);
}
}
return 0;
}