/src/xpdf-4.05/xpdf/JPXStream.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //======================================================================== |
2 | | // |
3 | | // JPXStream.cc |
4 | | // |
5 | | // Copyright 2002-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include <limits.h> |
12 | | #include "gmem.h" |
13 | | #include "gmempp.h" |
14 | | #include "Error.h" |
15 | | #include "JArithmeticDecoder.h" |
16 | | #include "JPXStream.h" |
17 | | |
18 | | //~ to do: |
19 | | // - ROI |
20 | | // - progression order changes |
21 | | // - packed packet headers |
22 | | // - support for palettes, channel maps, etc. |
23 | | // - make sure all needed JP2/JPX subboxes are parsed (readBoxes) |
24 | | // - can we assume that QCC segments must come after the QCD segment? |
25 | | // - handle tilePartToEOC in readTilePartData |
26 | | // - in coefficient decoding (readCodeBlockData): |
27 | | // - selective arithmetic coding bypass |
28 | | // (this also affects reading the cb->dataLen array) |
29 | | // - coeffs longer than 31 bits (should just ignore the extra bits?) |
30 | | // - handle boxes larger than 2^32 bytes |
31 | | // - the fixed-point arithmetic won't handle 16-bit pixels |
32 | | |
33 | | //------------------------------------------------------------------------ |
34 | | |
35 | | // number of contexts for the arithmetic decoder |
36 | 204k | #define jpxNContexts 19 |
37 | | |
38 | 765k | #define jpxContextSigProp 0 // 0 - 8: significance prop and cleanup |
39 | | #define jpxContextSign 9 // 9 - 13: sign |
40 | | #define jpxContextMagRef 14 // 14 - 16: magnitude refinement |
41 | 3.00M | #define jpxContextRunLength 17 // cleanup: run length |
42 | 1.04M | #define jpxContextUniform 18 // cleanup: first signif coeff |
43 | | |
44 | | //------------------------------------------------------------------------ |
45 | | |
46 | 614k | #define jpxPassSigProp 0 |
47 | 192k | #define jpxPassMagRef 1 |
48 | 2.47M | #define jpxPassCleanup 2 |
49 | | |
50 | | //------------------------------------------------------------------------ |
51 | | |
52 | | // arithmetic decoder context for the significance propagation and |
53 | | // cleanup passes: |
54 | | // [horiz][vert][diag][subband] |
55 | | // where subband = 0 for HL |
56 | | // = 1 for LH and LL |
57 | | // = 2 for HH |
58 | | static Guint sigPropContext[3][3][5][3] = { |
59 | | {{{ 0, 0, 0 }, // horiz=0, vert=0, diag=0 |
60 | | { 1, 1, 3 }, // horiz=0, vert=0, diag=1 |
61 | | { 2, 2, 6 }, // horiz=0, vert=0, diag=2 |
62 | | { 2, 2, 8 }, // horiz=0, vert=0, diag=3 |
63 | | { 2, 2, 8 }}, // horiz=0, vert=0, diag=4 |
64 | | {{ 5, 3, 1 }, // horiz=0, vert=1, diag=0 |
65 | | { 6, 3, 4 }, // horiz=0, vert=1, diag=1 |
66 | | { 6, 3, 7 }, // horiz=0, vert=1, diag=2 |
67 | | { 6, 3, 8 }, // horiz=0, vert=1, diag=3 |
68 | | { 6, 3, 8 }}, // horiz=0, vert=1, diag=4 |
69 | | {{ 8, 4, 2 }, // horiz=0, vert=2, diag=0 |
70 | | { 8, 4, 5 }, // horiz=0, vert=2, diag=1 |
71 | | { 8, 4, 7 }, // horiz=0, vert=2, diag=2 |
72 | | { 8, 4, 8 }, // horiz=0, vert=2, diag=3 |
73 | | { 8, 4, 8 }}}, // horiz=0, vert=2, diag=4 |
74 | | {{{ 3, 5, 1 }, // horiz=1, vert=0, diag=0 |
75 | | { 3, 6, 4 }, // horiz=1, vert=0, diag=1 |
76 | | { 3, 6, 7 }, // horiz=1, vert=0, diag=2 |
77 | | { 3, 6, 8 }, // horiz=1, vert=0, diag=3 |
78 | | { 3, 6, 8 }}, // horiz=1, vert=0, diag=4 |
79 | | {{ 7, 7, 2 }, // horiz=1, vert=1, diag=0 |
80 | | { 7, 7, 5 }, // horiz=1, vert=1, diag=1 |
81 | | { 7, 7, 7 }, // horiz=1, vert=1, diag=2 |
82 | | { 7, 7, 8 }, // horiz=1, vert=1, diag=3 |
83 | | { 7, 7, 8 }}, // horiz=1, vert=1, diag=4 |
84 | | {{ 8, 7, 2 }, // horiz=1, vert=2, diag=0 |
85 | | { 8, 7, 5 }, // horiz=1, vert=2, diag=1 |
86 | | { 8, 7, 7 }, // horiz=1, vert=2, diag=2 |
87 | | { 8, 7, 8 }, // horiz=1, vert=2, diag=3 |
88 | | { 8, 7, 8 }}}, // horiz=1, vert=2, diag=4 |
89 | | {{{ 4, 8, 2 }, // horiz=2, vert=0, diag=0 |
90 | | { 4, 8, 5 }, // horiz=2, vert=0, diag=1 |
91 | | { 4, 8, 7 }, // horiz=2, vert=0, diag=2 |
92 | | { 4, 8, 8 }, // horiz=2, vert=0, diag=3 |
93 | | { 4, 8, 8 }}, // horiz=2, vert=0, diag=4 |
94 | | {{ 7, 8, 2 }, // horiz=2, vert=1, diag=0 |
95 | | { 7, 8, 5 }, // horiz=2, vert=1, diag=1 |
96 | | { 7, 8, 7 }, // horiz=2, vert=1, diag=2 |
97 | | { 7, 8, 8 }, // horiz=2, vert=1, diag=3 |
98 | | { 7, 8, 8 }}, // horiz=2, vert=1, diag=4 |
99 | | {{ 8, 8, 2 }, // horiz=2, vert=2, diag=0 |
100 | | { 8, 8, 5 }, // horiz=2, vert=2, diag=1 |
101 | | { 8, 8, 7 }, // horiz=2, vert=2, diag=2 |
102 | | { 8, 8, 8 }, // horiz=2, vert=2, diag=3 |
103 | | { 8, 8, 8 }}} // horiz=2, vert=2, diag=4 |
104 | | }; |
105 | | |
106 | | // arithmetic decoder context and xor bit for the sign bit in the |
107 | | // significance propagation pass: |
108 | | // [horiz][vert][k] |
109 | | // where horiz/vert are offset by 2 (i.e., range is -2 .. 2) |
110 | | // and k = 0 for the context |
111 | | // = 1 for the xor bit |
112 | | static Guint signContext[5][5][2] = { |
113 | | {{ 13, 1 }, // horiz=-2, vert=-2 |
114 | | { 13, 1 }, // horiz=-2, vert=-1 |
115 | | { 12, 1 }, // horiz=-2, vert= 0 |
116 | | { 11, 1 }, // horiz=-2, vert=+1 |
117 | | { 11, 1 }}, // horiz=-2, vert=+2 |
118 | | {{ 13, 1 }, // horiz=-1, vert=-2 |
119 | | { 13, 1 }, // horiz=-1, vert=-1 |
120 | | { 12, 1 }, // horiz=-1, vert= 0 |
121 | | { 11, 1 }, // horiz=-1, vert=+1 |
122 | | { 11, 1 }}, // horiz=-1, vert=+2 |
123 | | {{ 10, 1 }, // horiz= 0, vert=-2 |
124 | | { 10, 1 }, // horiz= 0, vert=-1 |
125 | | { 9, 0 }, // horiz= 0, vert= 0 |
126 | | { 10, 0 }, // horiz= 0, vert=+1 |
127 | | { 10, 0 }}, // horiz= 0, vert=+2 |
128 | | {{ 11, 0 }, // horiz=+1, vert=-2 |
129 | | { 11, 0 }, // horiz=+1, vert=-1 |
130 | | { 12, 0 }, // horiz=+1, vert= 0 |
131 | | { 13, 0 }, // horiz=+1, vert=+1 |
132 | | { 13, 0 }}, // horiz=+1, vert=+2 |
133 | | {{ 11, 0 }, // horiz=+2, vert=-2 |
134 | | { 11, 0 }, // horiz=+2, vert=-1 |
135 | | { 12, 0 }, // horiz=+2, vert= 0 |
136 | | { 13, 0 }, // horiz=+2, vert=+1 |
137 | | { 13, 0 }}, // horiz=+2, vert=+2 |
138 | | }; |
139 | | |
140 | | //------------------------------------------------------------------------ |
141 | | |
142 | | // constants used in the IDWT |
143 | 102M | #define idwtAlpha -1.586134342059924 |
144 | 107M | #define idwtBeta -0.052980118572961 |
145 | 111M | #define idwtGamma 0.882911075530934 |
146 | 115M | #define idwtDelta 0.443506852043971 |
147 | 235M | #define idwtKappa 1.230174104914001 |
148 | 120M | #define idwtIKappa (1.0 / idwtKappa) |
149 | | |
150 | | // sum of the sample size (number of bits) and the number of bits to |
151 | | // the right of the decimal point for the fixed point arithmetic used |
152 | | // in the IDWT |
153 | 10.8M | #define fracBits 24 |
154 | | |
155 | | //------------------------------------------------------------------------ |
156 | | |
157 | | // floor(x / y) |
158 | 40.5M | #define jpxFloorDiv(x, y) ((x) / (y)) |
159 | | |
160 | | // floor(x / 2^y) |
161 | 1.15M | #define jpxFloorDivPow2(x, y) ((x) >> (y)) |
162 | | |
163 | | // ceil(x / y) |
164 | 102k | #define jpxCeilDiv(x, y) (((x) + (y) - 1) / (y)) |
165 | | |
166 | | // ceil(x / 2^y) |
167 | 11.6M | #define jpxCeilDivPow2(x, y) (((x) + (1 << (y)) - 1) >> (y)) |
168 | | |
169 | | //------------------------------------------------------------------------ |
170 | | |
171 | | #if 1 //----- disable coverage tracking |
172 | | |
173 | | #define cover(idx) |
174 | | |
175 | | #else //----- enable coverage tracking |
176 | | |
177 | | class JPXCover { |
178 | | public: |
179 | | |
180 | | JPXCover(int sizeA); |
181 | | ~JPXCover(); |
182 | | void incr(int idx); |
183 | | |
184 | | private: |
185 | | |
186 | | int size, used; |
187 | | int *data; |
188 | | }; |
189 | | |
190 | | JPXCover::JPXCover(int sizeA) { |
191 | | size = sizeA; |
192 | | used = -1; |
193 | | data = (int *)gmallocn(size, sizeof(int)); |
194 | | memset(data, 0, size * sizeof(int)); |
195 | | } |
196 | | |
197 | | JPXCover::~JPXCover() { |
198 | | int i; |
199 | | |
200 | | printf("JPX coverage:\n"); |
201 | | for (i = 0; i <= used; ++i) { |
202 | | printf(" %4d: %8d\n", i, data[i]); |
203 | | } |
204 | | gfree(data); |
205 | | } |
206 | | |
207 | | void JPXCover::incr(int idx) { |
208 | | if (idx < size) { |
209 | | ++data[idx]; |
210 | | if (idx > used) { |
211 | | used = idx; |
212 | | } |
213 | | } |
214 | | } |
215 | | |
216 | | JPXCover jpxCover(150); |
217 | | |
218 | | #define cover(idx) jpxCover.incr(idx) |
219 | | |
220 | | #endif //----- coverage tracking |
221 | | |
222 | | //------------------------------------------------------------------------ |
223 | | |
224 | | JPXStream::JPXStream(Stream *strA): |
225 | 157k | FilterStream(strA) |
226 | 157k | { |
227 | 157k | bufStr = new BufStream(str, 3); |
228 | | |
229 | 157k | decoded = gFalse; |
230 | 157k | nComps = 0; |
231 | 157k | bpc = NULL; |
232 | 157k | width = height = 0; |
233 | 157k | reduction = 0; |
234 | 157k | haveCS = gFalse; |
235 | | |
236 | 157k | palette.bpc = NULL; |
237 | 157k | palette.c = NULL; |
238 | 157k | havePalette = gFalse; |
239 | | |
240 | 157k | compMap.comp = NULL; |
241 | 157k | compMap.type = NULL; |
242 | 157k | compMap.pComp = NULL; |
243 | 157k | haveCompMap = gFalse; |
244 | | |
245 | 157k | channelDefn.idx = NULL; |
246 | 157k | channelDefn.type = NULL; |
247 | 157k | channelDefn.assoc = NULL; |
248 | 157k | haveChannelDefn = gFalse; |
249 | | |
250 | 157k | img.tiles = NULL; |
251 | | |
252 | 157k | bitBuf = 0; |
253 | 157k | bitBufLen = 0; |
254 | 157k | bitBufSkip = gFalse; |
255 | 157k | byteCount = 0; |
256 | 157k | } |
257 | | |
258 | 157k | JPXStream::~JPXStream() { |
259 | 157k | close(); |
260 | 157k | delete bufStr; |
261 | 157k | } |
262 | | |
263 | 154k | Stream *JPXStream::copy() { |
264 | 154k | return new JPXStream(str->copy()); |
265 | 154k | } |
266 | | |
267 | 51.5k | void JPXStream::reset() { |
268 | 51.5k | img.ySize = 0; |
269 | 51.5k | bufStr->reset(); |
270 | 51.5k | decoded = gFalse; |
271 | 51.5k | } |
272 | | |
273 | 208k | void JPXStream::close() { |
274 | 208k | JPXTile *tile; |
275 | 208k | JPXTileComp *tileComp; |
276 | 208k | JPXResLevel *resLevel; |
277 | 208k | JPXPrecinct *precinct; |
278 | 208k | JPXSubband *subband; |
279 | 208k | JPXCodeBlock *cb; |
280 | 208k | Guint comp, i, k, r, pre, sb; |
281 | | |
282 | 208k | gfree(bpc); |
283 | 208k | bpc = NULL; |
284 | 208k | if (havePalette) { |
285 | 12 | gfree(palette.bpc); |
286 | 12 | gfree(palette.c); |
287 | 12 | havePalette = gFalse; |
288 | 12 | } |
289 | 208k | if (haveCompMap) { |
290 | 821 | gfree(compMap.comp); |
291 | 821 | gfree(compMap.type); |
292 | 821 | gfree(compMap.pComp); |
293 | 821 | haveCompMap = gFalse; |
294 | 821 | } |
295 | 208k | if (haveChannelDefn) { |
296 | 0 | gfree(channelDefn.idx); |
297 | 0 | gfree(channelDefn.type); |
298 | 0 | gfree(channelDefn.assoc); |
299 | 0 | haveChannelDefn = gFalse; |
300 | 0 | } |
301 | | |
302 | 208k | if (img.tiles) { |
303 | 123k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
304 | 93.8k | tile = &img.tiles[i]; |
305 | 93.8k | if (tile->tileComps) { |
306 | 357k | for (comp = 0; comp < img.nComps; ++comp) { |
307 | 263k | tileComp = &tile->tileComps[comp]; |
308 | 263k | gfree(tileComp->quantSteps); |
309 | 263k | gfree(tileComp->data); |
310 | 263k | gfree(tileComp->buf); |
311 | 263k | if (tileComp->resLevels) { |
312 | 1.26M | for (r = 0; r <= tileComp->nDecompLevels; ++r) { |
313 | 1.01M | resLevel = &tileComp->resLevels[r]; |
314 | 1.01M | if (resLevel->precincts) { |
315 | 312k | for (pre = 0; pre < resLevel->nPrecincts; ++pre) { |
316 | 158k | precinct = &resLevel->precincts[pre]; |
317 | 158k | if (precinct->subbands) { |
318 | 583k | for (sb = 0; sb < (Guint)(r == 0 ? 1 : 3); ++sb) { |
319 | 424k | subband = &precinct->subbands[sb]; |
320 | 424k | gfree(subband->inclusion); |
321 | 424k | gfree(subband->zeroBitPlane); |
322 | 424k | if (subband->cbs) { |
323 | 2.42M | for (k = 0; k < subband->nXCBs * subband->nYCBs; ++k) { |
324 | 2.10M | cb = &subband->cbs[k]; |
325 | 2.10M | gfree(cb->dataLen); |
326 | 2.10M | gfree(cb->touched); |
327 | 2.10M | if (cb->arithDecoder) { |
328 | 204k | delete cb->arithDecoder; |
329 | 204k | } |
330 | 2.10M | if (cb->stats) { |
331 | 204k | delete cb->stats; |
332 | 204k | } |
333 | 2.10M | } |
334 | 320k | gfree(subband->cbs); |
335 | 320k | } |
336 | 424k | } |
337 | 158k | gfree(precinct->subbands); |
338 | 158k | } |
339 | 158k | } |
340 | 153k | gfree(resLevel->precincts); |
341 | 153k | } |
342 | 1.01M | } |
343 | 243k | gfree(tileComp->resLevels); |
344 | 243k | } |
345 | 263k | } |
346 | 93.8k | gfree(tile->tileComps); |
347 | 93.8k | } |
348 | 93.8k | } |
349 | 29.7k | gfree(img.tiles); |
350 | 29.7k | img.tiles = NULL; |
351 | 29.7k | } |
352 | 208k | bufStr->close(); |
353 | 208k | } |
354 | | |
355 | 51.3k | void JPXStream::decodeImage() { |
356 | 51.3k | if (readBoxes() == jpxDecodeFatalError) { |
357 | | // readBoxes reported an error, so we go immediately to EOF |
358 | 43.8k | curY = img.ySize >> reduction; |
359 | 43.8k | } else { |
360 | 7.51k | curY = img.yOffsetR; |
361 | 7.51k | } |
362 | 51.3k | curX = img.xOffsetR; |
363 | 51.3k | curComp = 0; |
364 | 51.3k | readBufLen = 0; |
365 | 51.3k | decoded = gTrue; |
366 | 51.3k | } |
367 | | |
368 | 191M | int JPXStream::getChar() { |
369 | 191M | int c; |
370 | | |
371 | 191M | if (!decoded) { |
372 | 51.3k | decodeImage(); |
373 | 51.3k | } |
374 | 191M | if (readBufLen < 8) { |
375 | 14.1M | fillReadBuf(); |
376 | 14.1M | } |
377 | 191M | if (readBufLen == 8) { |
378 | 2.87M | c = readBuf & 0xff; |
379 | 2.87M | readBufLen = 0; |
380 | 188M | } else if (readBufLen > 8) { |
381 | 188M | c = (readBuf >> (readBufLen - 8)) & 0xff; |
382 | 188M | readBufLen -= 8; |
383 | 188M | } else if (readBufLen == 0) { |
384 | 47.6k | c = EOF; |
385 | 47.6k | } else { |
386 | 1 | c = (readBuf << (8 - readBufLen)) & 0xff; |
387 | 1 | readBufLen = 0; |
388 | 1 | } |
389 | 191M | return c; |
390 | 191M | } |
391 | | |
392 | 1.78M | int JPXStream::lookChar() { |
393 | 1.78M | int c; |
394 | | |
395 | 1.78M | if (!decoded) { |
396 | 0 | decodeImage(); |
397 | 0 | } |
398 | 1.78M | if (readBufLen < 8) { |
399 | 483k | fillReadBuf(); |
400 | 483k | } |
401 | 1.78M | if (readBufLen == 8) { |
402 | 379k | c = readBuf & 0xff; |
403 | 1.40M | } else if (readBufLen > 8) { |
404 | 1.40M | c = (readBuf >> (readBufLen - 8)) & 0xff; |
405 | 1.40M | } else if (readBufLen == 0) { |
406 | 354 | c = EOF; |
407 | 354 | } else { |
408 | 0 | c = (readBuf << (8 - readBufLen)) & 0xff; |
409 | 0 | } |
410 | 1.78M | return c; |
411 | 1.78M | } |
412 | | |
413 | 14.6M | void JPXStream::fillReadBuf() { |
414 | 14.6M | JPXTileComp *tileComp; |
415 | 14.6M | Guint tileIdx, tx, ty; |
416 | 14.6M | int pix, pixBits, k; |
417 | 14.6M | GBool eol; |
418 | | |
419 | 20.3M | do { |
420 | 20.3M | if (curY >= (img.ySize >> reduction)) { |
421 | 48.0k | return; |
422 | 48.0k | } |
423 | 20.2M | tileIdx = (((curY << reduction) - img.yTileOffset) / img.yTileSize) |
424 | 20.2M | * img.nXTiles |
425 | 20.2M | + ((curX << reduction) - img.xTileOffset) / img.xTileSize; |
426 | 20.2M | #if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid |
427 | 20.2M | tileComp = &img.tiles[tileIdx].tileComps[curComp]; |
428 | | #else |
429 | | tileComp = &img.tiles[tileIdx].tileComps[havePalette ? 0 : curComp]; |
430 | | #endif |
431 | 20.2M | tx = jpxFloorDiv(curX, tileComp->hSep); |
432 | 20.2M | if (tx < tileComp->x0r) { |
433 | 79.5k | tx = 0; |
434 | 20.1M | } else { |
435 | 20.1M | tx -= tileComp->x0r; |
436 | 20.1M | } |
437 | 20.2M | ty = jpxFloorDiv(curY, tileComp->vSep); |
438 | 20.2M | if (ty < tileComp->y0r) { |
439 | 6.24M | ty = 0; |
440 | 14.0M | } else { |
441 | 14.0M | ty -= tileComp->y0r; |
442 | 14.0M | } |
443 | 20.2M | pix = (int)tileComp->data[ty * tileComp->w + tx]; |
444 | 20.2M | pixBits = tileComp->prec; |
445 | 20.2M | eol = gFalse; |
446 | 20.2M | #if 1 //~ ignore the palette, assume the PDF ColorSpace object is valid |
447 | 20.2M | if (++curComp == img.nComps) { |
448 | | #else |
449 | | if (havePalette) { |
450 | | if (pix >= 0 && pix < palette.nEntries) { |
451 | | pix = palette.c[pix * palette.nComps + curComp]; |
452 | | } else { |
453 | | pix = 0; |
454 | | } |
455 | | pixBits = palette.bpc[curComp]; |
456 | | } |
457 | | if (++curComp == (Guint)(havePalette ? palette.nComps : img.nComps)) { |
458 | | #endif |
459 | 6.92M | curComp = 0; |
460 | 6.92M | if (++curX == (img.xSize >> reduction)) { |
461 | 19.8k | curX = img.xOffsetR; |
462 | 19.8k | ++curY; |
463 | 19.8k | eol = gTrue; |
464 | 19.8k | } |
465 | 6.92M | } |
466 | 20.2M | if (pixBits == 8) { |
467 | 387k | readBuf = (readBuf << 8) | (pix & 0xff); |
468 | 19.8M | } else { |
469 | 19.8M | readBuf = (readBuf << pixBits) | (pix & ((1 << pixBits) - 1)); |
470 | 19.8M | } |
471 | 20.2M | readBufLen += pixBits; |
472 | 20.2M | if (eol && (k = readBufLen & 7)) { |
473 | 19.4k | readBuf <<= 8 - k; |
474 | 19.4k | readBufLen += 8 - k; |
475 | 19.4k | } |
476 | 20.2M | } while (readBufLen < 8); |
477 | 14.6M | } |
478 | | |
479 | | GString *JPXStream::getPSFilter(int psLevel, const char *indent, |
480 | 0 | GBool okToReadStream) { |
481 | 0 | return NULL; |
482 | 0 | } |
483 | | |
484 | 0 | GBool JPXStream::isBinary(GBool last) { |
485 | 0 | return str->isBinary(gTrue); |
486 | 0 | } |
487 | | |
488 | | void JPXStream::getImageParams(int *bitsPerComponent, |
489 | 0 | StreamColorSpaceMode *csMode) { |
490 | 0 | Guint boxType, boxLen, dataLen, csEnum; |
491 | 0 | Guint bpc1, dummy; |
492 | 0 | int csMeth, csPrec, csPrec1, dummy2; |
493 | 0 | StreamColorSpaceMode csMode1; |
494 | 0 | GBool haveBPC, haveCSMode; |
495 | |
|
496 | 0 | csPrec = 0; // make gcc happy |
497 | 0 | haveBPC = haveCSMode = gFalse; |
498 | 0 | bufStr->reset(); |
499 | 0 | if (bufStr->lookChar() == 0xff) { |
500 | 0 | getImageParams2(bitsPerComponent, csMode); |
501 | 0 | } else { |
502 | 0 | while (readBoxHdr(&boxType, &boxLen, &dataLen)) { |
503 | 0 | if (boxType == 0x6a703268) { // JP2 header |
504 | 0 | cover(0); |
505 | | // skip the superbox |
506 | 0 | } else if (boxType == 0x69686472) { // image header |
507 | 0 | cover(1); |
508 | 0 | if (readULong(&dummy) && |
509 | 0 | readULong(&dummy) && |
510 | 0 | readUWord(&dummy) && |
511 | 0 | readUByte(&bpc1) && |
512 | 0 | readUByte(&dummy) && |
513 | 0 | readUByte(&dummy) && |
514 | 0 | readUByte(&dummy)) { |
515 | 0 | *bitsPerComponent = bpc1 + 1; |
516 | 0 | haveBPC = gTrue; |
517 | 0 | } |
518 | 0 | } else if (boxType == 0x636F6C72) { // color specification |
519 | 0 | cover(2); |
520 | 0 | if (readByte(&csMeth) && |
521 | 0 | readByte(&csPrec1) && |
522 | 0 | readByte(&dummy2)) { |
523 | 0 | if (csMeth == 1) { |
524 | 0 | if (readULong(&csEnum)) { |
525 | 0 | csMode1 = streamCSNone; |
526 | 0 | if (csEnum == jpxCSBiLevel || |
527 | 0 | csEnum == jpxCSGrayscale) { |
528 | 0 | csMode1 = streamCSDeviceGray; |
529 | 0 | } else if (csEnum == jpxCSCMYK) { |
530 | 0 | csMode1 = streamCSDeviceCMYK; |
531 | 0 | } else if (csEnum == jpxCSsRGB || |
532 | 0 | csEnum == jpxCSCISesRGB || |
533 | 0 | csEnum == jpxCSROMMRGB) { |
534 | 0 | csMode1 = streamCSDeviceRGB; |
535 | 0 | } |
536 | 0 | if (csMode1 != streamCSNone && |
537 | 0 | (!haveCSMode || csPrec1 > csPrec)) { |
538 | 0 | *csMode = csMode1; |
539 | 0 | csPrec = csPrec1; |
540 | 0 | haveCSMode = gTrue; |
541 | 0 | } |
542 | 0 | if (dataLen > 7) { |
543 | 0 | bufStr->discardChars(dataLen - 7); |
544 | 0 | } |
545 | 0 | } |
546 | 0 | } else { |
547 | 0 | if (dataLen > 3) { |
548 | 0 | bufStr->discardChars(dataLen - 3); |
549 | 0 | } |
550 | 0 | } |
551 | 0 | } |
552 | 0 | } else if (boxType == 0x6A703263) { // codestream |
553 | 0 | cover(3); |
554 | 0 | if (!(haveBPC && haveCSMode)) { |
555 | 0 | getImageParams2(bitsPerComponent, csMode); |
556 | 0 | } |
557 | 0 | break; |
558 | 0 | } else { |
559 | 0 | cover(4); |
560 | 0 | bufStr->discardChars(dataLen); |
561 | 0 | } |
562 | 0 | } |
563 | 0 | } |
564 | 0 | bufStr->close(); |
565 | 0 | } |
566 | | |
567 | | // Get image parameters from the codestream. |
568 | | void JPXStream::getImageParams2(int *bitsPerComponent, |
569 | 0 | StreamColorSpaceMode *csMode) { |
570 | 0 | int segType; |
571 | 0 | Guint segLen, nComps1, bpc1, dummy; |
572 | |
|
573 | 0 | while (readMarkerHdr(&segType, &segLen)) { |
574 | 0 | if (segType == 0x51) { // SIZ - image and tile size |
575 | 0 | cover(5); |
576 | 0 | if (readUWord(&dummy) && |
577 | 0 | readULong(&dummy) && |
578 | 0 | readULong(&dummy) && |
579 | 0 | readULong(&dummy) && |
580 | 0 | readULong(&dummy) && |
581 | 0 | readULong(&dummy) && |
582 | 0 | readULong(&dummy) && |
583 | 0 | readULong(&dummy) && |
584 | 0 | readULong(&dummy) && |
585 | 0 | readUWord(&nComps1) && |
586 | 0 | readUByte(&bpc1)) { |
587 | 0 | *bitsPerComponent = (bpc1 & 0x7f) + 1; |
588 | | // if there's no color space info, take a guess |
589 | 0 | if (nComps1 == 1) { |
590 | 0 | *csMode = streamCSDeviceGray; |
591 | 0 | } else if (nComps1 == 3) { |
592 | 0 | *csMode = streamCSDeviceRGB; |
593 | 0 | } else if (nComps1 == 4) { |
594 | 0 | *csMode = streamCSDeviceCMYK; |
595 | 0 | } |
596 | 0 | } |
597 | 0 | break; |
598 | 0 | } else { |
599 | 0 | cover(6); |
600 | 0 | if (segLen > 2) { |
601 | 0 | bufStr->discardChars(segLen - 2); |
602 | 0 | } |
603 | 0 | } |
604 | 0 | } |
605 | 0 | } |
606 | | |
607 | 51.3k | JPXDecodeResult JPXStream::readBoxes() { |
608 | 51.3k | JPXDecodeResult result; |
609 | 51.3k | GBool haveCodestream; |
610 | 51.3k | Guint boxType, boxLen, dataLen; |
611 | 51.3k | Guint w, h, n, bpc1, compression, unknownColorspace, ipr; |
612 | 51.3k | Guint i, j; |
613 | | |
614 | 51.3k | haveImgHdr = gFalse; |
615 | 51.3k | haveCodestream = gFalse; |
616 | | |
617 | | // check for a naked JPEG 2000 codestream (without the JP2/JPX |
618 | | // wrapper) -- this appears to be a violation of the PDF spec, but |
619 | | // Acrobat allows it |
620 | 51.3k | if (bufStr->lookChar() == 0xff) { |
621 | 40.6k | cover(7); |
622 | 40.6k | error(errSyntaxWarning, getPos(), |
623 | 40.6k | "Naked JPEG 2000 codestream, missing JP2/JPX wrapper"); |
624 | 40.6k | if ((result = readCodestream(0)) == jpxDecodeFatalError) { |
625 | 36.5k | return result; |
626 | 36.5k | } |
627 | 4.06k | nComps = img.nComps; |
628 | 4.06k | bpc = (Guint *)gmallocn(nComps, sizeof(Guint)); |
629 | 18.3k | for (i = 0; i < nComps; ++i) { |
630 | 14.2k | bpc[i] = img.tiles[0].tileComps[i].prec; |
631 | 14.2k | } |
632 | 4.06k | width = img.xSize - img.xOffset; |
633 | 4.06k | height = img.ySize - img.yOffset; |
634 | 4.06k | return result; |
635 | 40.6k | } |
636 | | |
637 | 44.7k | while (readBoxHdr(&boxType, &boxLen, &dataLen)) { |
638 | 41.3k | switch (boxType) { |
639 | 6.32k | case 0x6a703268: // JP2 header |
640 | | // this is a grouping box ('superbox') which has no real |
641 | | // contents and doesn't appear to be used consistently, i.e., |
642 | | // some things which should be subboxes of the JP2 header box |
643 | | // show up outside of it - so we simply ignore the JP2 header |
644 | | // box |
645 | 6.32k | cover(8); |
646 | 6.32k | break; |
647 | 5.23k | case 0x69686472: // image header |
648 | 5.23k | cover(9); |
649 | 5.23k | if (!readULong(&h) || |
650 | 5.23k | !readULong(&w) || |
651 | 5.23k | !readUWord(&n) || |
652 | 5.23k | !readUByte(&bpc1) || |
653 | 5.23k | !readUByte(&compression) || |
654 | 5.23k | !readUByte(&unknownColorspace) || |
655 | 5.23k | !readUByte(&ipr)) { |
656 | 1.81k | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
657 | 1.81k | return jpxDecodeFatalError; |
658 | 1.81k | } |
659 | 3.42k | if (compression != 7) { |
660 | 207 | error(errSyntaxError, getPos(), |
661 | 207 | "Unknown compression type in JPX stream"); |
662 | 207 | return jpxDecodeFatalError; |
663 | 207 | } |
664 | 3.21k | height = h; |
665 | 3.21k | width = w; |
666 | 3.21k | nComps = n; |
667 | 3.21k | bpc = (Guint *)gmallocn(nComps, sizeof(Guint)); |
668 | 12.0k | for (i = 0; i < nComps; ++i) { |
669 | 8.82k | bpc[i] = bpc1; |
670 | 8.82k | } |
671 | 3.21k | haveImgHdr = gTrue; |
672 | 3.21k | break; |
673 | 0 | case 0x62706363: // bits per component |
674 | 0 | cover(10); |
675 | 0 | if (!haveImgHdr) { |
676 | 0 | error(errSyntaxError, getPos(), |
677 | 0 | "Found bits per component box before image header box in JPX stream"); |
678 | 0 | return jpxDecodeFatalError; |
679 | 0 | } |
680 | 0 | if (dataLen != nComps) { |
681 | 0 | error(errSyntaxError, getPos(), |
682 | 0 | "Invalid bits per component box in JPX stream"); |
683 | 0 | return jpxDecodeFatalError; |
684 | 0 | } |
685 | 0 | for (i = 0; i < nComps; ++i) { |
686 | 0 | if (!readUByte(&bpc[i])) { |
687 | 0 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
688 | 0 | return jpxDecodeFatalError; |
689 | 0 | } |
690 | 0 | } |
691 | 0 | break; |
692 | 4.51k | case 0x636F6C72: // color specification |
693 | 4.51k | cover(11); |
694 | 4.51k | if (!readColorSpecBox(dataLen)) { |
695 | 2.32k | return jpxDecodeFatalError; |
696 | 2.32k | } |
697 | 2.18k | break; |
698 | 2.18k | case 0x70636c72: // palette |
699 | 12 | cover(12); |
700 | 12 | if (!readUWord(&palette.nEntries) || |
701 | 12 | !readUByte(&palette.nComps)) { |
702 | 0 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
703 | 0 | return jpxDecodeFatalError; |
704 | 0 | } |
705 | 12 | havePalette = gTrue; |
706 | 12 | palette.bpc = (Guint *)gmallocn(palette.nComps, sizeof(Guint)); |
707 | 12 | palette.c = |
708 | 12 | (int *)gmallocn(palette.nEntries * palette.nComps, sizeof(int)); |
709 | 2.57k | for (i = 0; i < palette.nComps; ++i) { |
710 | 2.56k | if (!readUByte(&palette.bpc[i])) { |
711 | 0 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
712 | 0 | return jpxDecodeFatalError; |
713 | 0 | } |
714 | 2.56k | ++palette.bpc[i]; |
715 | 2.56k | } |
716 | 46 | for (i = 0; i < palette.nEntries; ++i) { |
717 | 4.25k | for (j = 0; j < palette.nComps; ++j) { |
718 | 4.21k | if (!readNBytes(((palette.bpc[j] & 0x7f) + 7) >> 3, |
719 | 4.21k | (palette.bpc[j] & 0x80) ? gTrue : gFalse, |
720 | 4.21k | &palette.c[i * palette.nComps + j])) { |
721 | 12 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
722 | 12 | return jpxDecodeFatalError; |
723 | 12 | } |
724 | 4.21k | } |
725 | 46 | } |
726 | 0 | break; |
727 | 821 | case 0x636d6170: // component mapping |
728 | 821 | cover(13); |
729 | 821 | haveCompMap = gTrue; |
730 | 821 | compMap.nChannels = dataLen / 4; |
731 | 821 | compMap.comp = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint)); |
732 | 821 | compMap.type = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint)); |
733 | 821 | compMap.pComp = (Guint *)gmallocn(compMap.nChannels, sizeof(Guint)); |
734 | 547k | for (i = 0; i < compMap.nChannels; ++i) { |
735 | 547k | if (!readUWord(&compMap.comp[i]) || |
736 | 547k | !readUByte(&compMap.type[i]) || |
737 | 547k | !readUByte(&compMap.pComp[i])) { |
738 | 523 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
739 | 523 | return jpxDecodeFatalError; |
740 | 523 | } |
741 | 547k | } |
742 | 298 | break; |
743 | 298 | case 0x63646566: // channel definition |
744 | 0 | cover(14); |
745 | 0 | if (!readUWord(&channelDefn.nChannels)) { |
746 | 0 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
747 | 0 | return jpxDecodeFatalError; |
748 | 0 | } |
749 | 0 | haveChannelDefn = gTrue; |
750 | 0 | channelDefn.idx = |
751 | 0 | (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint)); |
752 | 0 | channelDefn.type = |
753 | 0 | (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint)); |
754 | 0 | channelDefn.assoc = |
755 | 0 | (Guint *)gmallocn(channelDefn.nChannels, sizeof(Guint)); |
756 | 0 | for (i = 0; i < channelDefn.nChannels; ++i) { |
757 | 0 | if (!readUWord(&channelDefn.idx[i]) || |
758 | 0 | !readUWord(&channelDefn.type[i]) || |
759 | 0 | !readUWord(&channelDefn.assoc[i])) { |
760 | 0 | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
761 | 0 | return jpxDecodeFatalError; |
762 | 0 | } |
763 | 0 | } |
764 | 0 | break; |
765 | 380 | case 0x6A703263: // contiguous codestream |
766 | 380 | cover(15); |
767 | 380 | if (!bpc) { |
768 | 377 | error(errSyntaxError, getPos(), |
769 | 377 | "JPX stream is missing the image header box"); |
770 | 377 | } |
771 | 380 | if (!haveCS) { |
772 | 160 | error(errSyntaxError, getPos(), |
773 | 160 | "JPX stream has no supported color spec"); |
774 | 160 | } |
775 | 380 | if ((result = readCodestream(dataLen)) != jpxDecodeOk) { |
776 | 352 | return result; |
777 | 352 | } |
778 | 28 | haveCodestream = gTrue; |
779 | 28 | break; |
780 | 24.0k | default: |
781 | 24.0k | cover(16); |
782 | 24.0k | if (bufStr->discardChars(dataLen) != dataLen) { |
783 | 2.05k | error(errSyntaxError, getPos(), "Unexpected EOF in JPX stream"); |
784 | 2.05k | return haveCodestream ? jpxDecodeNonFatalError : jpxDecodeFatalError; |
785 | 2.05k | } |
786 | 22.0k | break; |
787 | 41.3k | } |
788 | 41.3k | } |
789 | 3.42k | return jpxDecodeOk; |
790 | 10.7k | } |
791 | | |
792 | 4.51k | GBool JPXStream::readColorSpecBox(Guint dataLen) { |
793 | 4.51k | JPXColorSpec newCS; |
794 | 4.51k | Guint csApprox, csEnum; |
795 | 4.51k | GBool ok; |
796 | | |
797 | 4.51k | ok = gFalse; |
798 | 4.51k | if (!readUByte(&newCS.meth) || |
799 | 4.51k | !readByte(&newCS.prec) || |
800 | 4.51k | !readUByte(&csApprox)) { |
801 | 571 | goto err; |
802 | 571 | } |
803 | 3.94k | switch (newCS.meth) { |
804 | 3.32k | case 1: // enumerated colorspace |
805 | 3.32k | cover(17); |
806 | 3.32k | if (!readULong(&csEnum)) { |
807 | 581 | goto err; |
808 | 581 | } |
809 | 2.74k | newCS.enumerated.type = (JPXColorSpaceType)csEnum; |
810 | 2.74k | switch (newCS.enumerated.type) { |
811 | 279 | case jpxCSBiLevel: |
812 | 279 | ok = gTrue; |
813 | 279 | break; |
814 | 28 | case jpxCSYCbCr1: |
815 | 28 | ok = gTrue; |
816 | 28 | break; |
817 | 299 | case jpxCSYCbCr2: |
818 | 299 | ok = gTrue; |
819 | 299 | break; |
820 | 0 | case jpxCSYCBCr3: |
821 | 0 | ok = gTrue; |
822 | 0 | break; |
823 | 0 | case jpxCSPhotoYCC: |
824 | 0 | ok = gTrue; |
825 | 0 | break; |
826 | 12 | case jpxCSCMY: |
827 | 12 | ok = gTrue; |
828 | 12 | break; |
829 | 0 | case jpxCSCMYK: |
830 | 0 | ok = gTrue; |
831 | 0 | break; |
832 | 277 | case jpxCSYCCK: |
833 | 277 | ok = gTrue; |
834 | 277 | break; |
835 | 1.65k | case jpxCSCIELab: |
836 | 1.65k | if (dataLen == 7 + 7*4) { |
837 | 1.52k | if (!readULong(&newCS.enumerated.cieLab.rl) || |
838 | 1.52k | !readULong(&newCS.enumerated.cieLab.ol) || |
839 | 1.52k | !readULong(&newCS.enumerated.cieLab.ra) || |
840 | 1.52k | !readULong(&newCS.enumerated.cieLab.oa) || |
841 | 1.52k | !readULong(&newCS.enumerated.cieLab.rb) || |
842 | 1.52k | !readULong(&newCS.enumerated.cieLab.ob) || |
843 | 1.52k | !readULong(&newCS.enumerated.cieLab.il)) { |
844 | 644 | goto err; |
845 | 644 | } |
846 | 1.52k | } else if (dataLen == 7) { |
847 | | //~ this assumes the 8-bit case |
848 | 0 | cover(92); |
849 | 0 | newCS.enumerated.cieLab.rl = 100; |
850 | 0 | newCS.enumerated.cieLab.ol = 0; |
851 | 0 | newCS.enumerated.cieLab.ra = 255; |
852 | 0 | newCS.enumerated.cieLab.oa = 128; |
853 | 0 | newCS.enumerated.cieLab.rb = 255; |
854 | 0 | newCS.enumerated.cieLab.ob = 96; |
855 | 0 | newCS.enumerated.cieLab.il = 0x00443530; |
856 | 128 | } else { |
857 | 128 | goto err; |
858 | 128 | } |
859 | 882 | ok = gTrue; |
860 | 882 | break; |
861 | 192 | case jpxCSsRGB: |
862 | 192 | ok = gTrue; |
863 | 192 | break; |
864 | 0 | case jpxCSGrayscale: |
865 | 0 | ok = gTrue; |
866 | 0 | break; |
867 | 0 | case jpxCSBiLevel2: |
868 | 0 | ok = gTrue; |
869 | 0 | break; |
870 | 0 | case jpxCSCIEJab: |
871 | | // not allowed in PDF |
872 | 0 | goto err; |
873 | 0 | case jpxCSCISesRGB: |
874 | 0 | ok = gTrue; |
875 | 0 | break; |
876 | 0 | case jpxCSROMMRGB: |
877 | 0 | ok = gTrue; |
878 | 0 | break; |
879 | 0 | case jpxCSsRGBYCbCr: |
880 | 0 | ok = gTrue; |
881 | 0 | break; |
882 | 0 | case jpxCSYPbPr1125: |
883 | 0 | ok = gTrue; |
884 | 0 | break; |
885 | 0 | case jpxCSYPbPr1250: |
886 | 0 | ok = gTrue; |
887 | 0 | break; |
888 | 4 | default: |
889 | 4 | goto err; |
890 | 2.74k | } |
891 | 1.96k | break; |
892 | 1.96k | case 2: // restricted ICC profile |
893 | 6 | case 3: // any ICC profile (JPX) |
894 | 463 | case 4: // vendor color (JPX) |
895 | 463 | cover(18); |
896 | 463 | if (dataLen > 3 && |
897 | 463 | bufStr->discardChars(dataLen - 3) != dataLen - 3) { |
898 | 398 | goto err; |
899 | 398 | } |
900 | 65 | break; |
901 | 3.94k | } |
902 | | |
903 | 2.18k | if (ok && (!haveCS || newCS.prec > cs.prec)) { |
904 | 1.81k | cs = newCS; |
905 | 1.81k | haveCS = gTrue; |
906 | 1.81k | } |
907 | | |
908 | 2.18k | return gTrue; |
909 | | |
910 | 2.32k | err: |
911 | 2.32k | error(errSyntaxError, getPos(), "Error in JPX color spec"); |
912 | 2.32k | return gFalse; |
913 | 3.94k | } |
914 | | |
915 | 40.9k | JPXDecodeResult JPXStream::readCodestream(Guint len) { |
916 | 40.9k | JPXTile *tile; |
917 | 40.9k | JPXTileComp *tileComp; |
918 | 40.9k | int segType; |
919 | 40.9k | GBool haveSIZ, haveCOD, haveQCD, haveSOT, ok; |
920 | 40.9k | Guint style, progOrder, nLayers, multiComp, nDecompLevels; |
921 | 40.9k | Guint codeBlockW, codeBlockH, codeBlockStyle, transform; |
922 | 40.9k | Guint precinctSize; |
923 | 40.9k | Guint segLen, capabilities, comp, i, j, r; |
924 | | |
925 | | //----- main header |
926 | 40.9k | haveSIZ = haveCOD = haveQCD = haveSOT = gFalse; |
927 | 326k | do { |
928 | 326k | if (!readMarkerHdr(&segType, &segLen)) { |
929 | 1.90k | error(errSyntaxError, getPos(), "Error in JPX codestream"); |
930 | 1.90k | return jpxDecodeFatalError; |
931 | 1.90k | } |
932 | 324k | switch (segType) { |
933 | 31.5k | case 0x4f: // SOC - start of codestream |
934 | | // marker only |
935 | 31.5k | cover(19); |
936 | 31.5k | break; |
937 | 38.4k | case 0x51: // SIZ - image and tile size |
938 | 38.4k | cover(20); |
939 | 38.4k | if (haveSIZ) { |
940 | 201 | error(errSyntaxError, getPos(), |
941 | 201 | "Duplicate SIZ marker segment in JPX stream"); |
942 | 201 | return jpxDecodeFatalError; |
943 | 201 | } |
944 | 38.2k | if (!readUWord(&capabilities) || |
945 | 38.2k | !readULong(&img.xSize) || |
946 | 38.2k | !readULong(&img.ySize) || |
947 | 38.2k | !readULong(&img.xOffset) || |
948 | 38.2k | !readULong(&img.yOffset) || |
949 | 38.2k | !readULong(&img.xTileSize) || |
950 | 38.2k | !readULong(&img.yTileSize) || |
951 | 38.2k | !readULong(&img.xTileOffset) || |
952 | 38.2k | !readULong(&img.yTileOffset) || |
953 | 38.2k | !readUWord(&img.nComps)) { |
954 | 6.73k | error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment"); |
955 | 6.73k | return jpxDecodeFatalError; |
956 | 6.73k | } |
957 | 31.5k | if (haveImgHdr && img.nComps != nComps) { |
958 | 0 | error(errSyntaxError, getPos(), |
959 | 0 | "Different number of components in JPX SIZ marker segment"); |
960 | 0 | return jpxDecodeFatalError; |
961 | 0 | } |
962 | 31.5k | if (img.xSize == 0 || img.ySize == 0 || |
963 | 31.5k | img.xOffset >= img.xSize || img.yOffset >= img.ySize || |
964 | 31.5k | img.xTileSize == 0 || img.yTileSize == 0 || |
965 | 31.5k | img.xTileOffset > img.xOffset || |
966 | 31.5k | img.yTileOffset > img.yOffset || |
967 | 31.5k | img.xTileSize + img.xTileOffset <= img.xOffset || |
968 | 31.5k | img.yTileSize + img.yTileOffset <= img.yOffset || |
969 | 31.5k | img.nComps == 0) { |
970 | 1.51k | error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment"); |
971 | 1.51k | return jpxDecodeFatalError; |
972 | 1.51k | } |
973 | 30.0k | img.xSizeR = jpxCeilDivPow2(img.xSize, reduction); |
974 | 30.0k | img.ySizeR = jpxCeilDivPow2(img.ySize, reduction); |
975 | 30.0k | img.xOffsetR = jpxCeilDivPow2(img.xOffset, reduction); |
976 | 30.0k | img.yOffsetR = jpxCeilDivPow2(img.yOffset, reduction); |
977 | 30.0k | img.nXTiles = (img.xSize - img.xTileOffset + img.xTileSize - 1) |
978 | 30.0k | / img.xTileSize; |
979 | 30.0k | img.nYTiles = (img.ySize - img.yTileOffset + img.yTileSize - 1) |
980 | 30.0k | / img.yTileSize; |
981 | | // check for overflow before allocating memory |
982 | 30.0k | if (img.nXTiles <= 0 || img.nYTiles <= 0 || |
983 | 30.0k | img.nXTiles >= INT_MAX / img.nYTiles) { |
984 | 288 | error(errSyntaxError, getPos(), |
985 | 288 | "Bad tile count in JPX SIZ marker segment"); |
986 | 288 | return jpxDecodeFatalError; |
987 | 288 | } |
988 | 29.7k | img.tiles = (JPXTile *)gmallocn(img.nXTiles * img.nYTiles, |
989 | 29.7k | sizeof(JPXTile)); |
990 | 123k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
991 | 93.8k | img.tiles[i].init = gFalse; |
992 | 93.8k | img.tiles[i].nextTilePart = 0; |
993 | 93.8k | img.tiles[i].tileComps = NULL; |
994 | 93.8k | } |
995 | 123k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
996 | 93.8k | img.tiles[i].tileComps = (JPXTileComp *)gmallocn(img.nComps, |
997 | 93.8k | sizeof(JPXTileComp)); |
998 | 357k | for (comp = 0; comp < img.nComps; ++comp) { |
999 | 263k | img.tiles[i].tileComps[comp].quantSteps = NULL; |
1000 | 263k | img.tiles[i].tileComps[comp].data = NULL; |
1001 | 263k | img.tiles[i].tileComps[comp].buf = NULL; |
1002 | 263k | img.tiles[i].tileComps[comp].resLevels = NULL; |
1003 | 263k | } |
1004 | 93.8k | } |
1005 | 121k | for (comp = 0; comp < img.nComps; ++comp) { |
1006 | 91.4k | if (!readUByte(&img.tiles[0].tileComps[comp].prec) || |
1007 | 91.4k | !readUByte(&img.tiles[0].tileComps[comp].hSep) || |
1008 | 91.4k | !readUByte(&img.tiles[0].tileComps[comp].vSep)) { |
1009 | 15 | error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment"); |
1010 | 15 | return jpxDecodeFatalError; |
1011 | 15 | } |
1012 | 91.3k | if (img.tiles[0].tileComps[comp].hSep == 0 || |
1013 | 91.3k | img.tiles[0].tileComps[comp].vSep == 0) { |
1014 | 129 | error(errSyntaxError, getPos(), "Error in JPX SIZ marker segment"); |
1015 | 129 | return jpxDecodeFatalError; |
1016 | 129 | } |
1017 | 91.2k | img.tiles[0].tileComps[comp].sgned = |
1018 | 91.2k | (img.tiles[0].tileComps[comp].prec & 0x80) ? gTrue : gFalse; |
1019 | 91.2k | img.tiles[0].tileComps[comp].prec = |
1020 | 91.2k | (img.tiles[0].tileComps[comp].prec & 0x7f) + 1; |
1021 | 262k | for (i = 1; i < img.nXTiles * img.nYTiles; ++i) { |
1022 | 171k | img.tiles[i].tileComps[comp] = img.tiles[0].tileComps[comp]; |
1023 | 171k | } |
1024 | 91.2k | } |
1025 | 29.6k | haveSIZ = gTrue; |
1026 | 29.6k | break; |
1027 | 30.3k | case 0x52: // COD - coding style default |
1028 | 30.3k | cover(21); |
1029 | 30.3k | if (!haveSIZ) { |
1030 | 11 | error(errSyntaxError, getPos(), |
1031 | 11 | "JPX COD marker segment before SIZ segment"); |
1032 | 11 | return jpxDecodeFatalError; |
1033 | 11 | } |
1034 | 30.3k | if (!readUByte(&style) || |
1035 | 30.3k | !readUByte(&progOrder) || |
1036 | 30.3k | !readUWord(&nLayers) || |
1037 | 30.3k | !readUByte(&multiComp) || |
1038 | 30.3k | !readUByte(&nDecompLevels) || |
1039 | 30.3k | !readUByte(&codeBlockW) || |
1040 | 30.3k | !readUByte(&codeBlockH) || |
1041 | 30.3k | !readUByte(&codeBlockStyle) || |
1042 | 30.3k | !readUByte(&transform)) { |
1043 | 1.56k | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1044 | 1.56k | return jpxDecodeFatalError; |
1045 | 1.56k | } |
1046 | 28.7k | if (nDecompLevels < 1 || |
1047 | 28.7k | nDecompLevels > 31 || |
1048 | 28.7k | codeBlockW > 8 || |
1049 | 28.7k | codeBlockH > 8) { |
1050 | 227 | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1051 | 227 | return jpxDecodeFatalError; |
1052 | 227 | } |
1053 | 28.5k | codeBlockW += 2; |
1054 | 28.5k | codeBlockH += 2; |
1055 | 117k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
1056 | 89.0k | img.tiles[i].progOrder = progOrder; |
1057 | 89.0k | img.tiles[i].nLayers = nLayers; |
1058 | 89.0k | img.tiles[i].multiComp = multiComp; |
1059 | 338k | for (comp = 0; comp < img.nComps; ++comp) { |
1060 | 249k | img.tiles[i].tileComps[comp].style = style; |
1061 | 249k | img.tiles[i].tileComps[comp].nDecompLevels = nDecompLevels; |
1062 | 249k | img.tiles[i].tileComps[comp].codeBlockW = codeBlockW; |
1063 | 249k | img.tiles[i].tileComps[comp].codeBlockH = codeBlockH; |
1064 | 249k | img.tiles[i].tileComps[comp].codeBlockStyle = codeBlockStyle; |
1065 | 249k | img.tiles[i].tileComps[comp].transform = transform; |
1066 | 249k | img.tiles[i].tileComps[comp].resLevels = |
1067 | 249k | (JPXResLevel *)gmallocn(nDecompLevels + 1, sizeof(JPXResLevel)); |
1068 | 1.09M | for (r = 0; r <= nDecompLevels; ++r) { |
1069 | 844k | img.tiles[i].tileComps[comp].resLevels[r].precincts = NULL; |
1070 | 844k | } |
1071 | 249k | } |
1072 | 89.0k | } |
1073 | 135k | for (r = 0; r <= nDecompLevels; ++r) { |
1074 | 106k | if (style & 0x01) { |
1075 | 1.61k | cover(91); |
1076 | 1.61k | if (!readUByte(&precinctSize)) { |
1077 | 169 | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1078 | 169 | return jpxDecodeFatalError; |
1079 | 169 | } |
1080 | 1.44k | if (r > 0 && ((precinctSize & 0x0f) == 0 || |
1081 | 936 | (precinctSize & 0xf0) == 0)) { |
1082 | 138 | error(errSyntaxError, getPos(), |
1083 | 138 | "Invalid precinct size in JPX COD marker segment"); |
1084 | 138 | return jpxDecodeFatalError; |
1085 | 138 | } |
1086 | 1.30k | img.tiles[0].tileComps[0].resLevels[r].precinctWidth = |
1087 | 1.30k | precinctSize & 0x0f; |
1088 | 1.30k | img.tiles[0].tileComps[0].resLevels[r].precinctHeight = |
1089 | 1.30k | (precinctSize >> 4) & 0x0f; |
1090 | 105k | } else { |
1091 | 105k | img.tiles[0].tileComps[0].resLevels[r].precinctWidth = 15; |
1092 | 105k | img.tiles[0].tileComps[0].resLevels[r].precinctHeight = 15; |
1093 | 105k | } |
1094 | 106k | } |
1095 | 116k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
1096 | 333k | for (comp = 0; comp < img.nComps; ++comp) { |
1097 | 245k | if (!(i == 0 && comp == 0)) { |
1098 | 944k | for (r = 0; r <= nDecompLevels; ++r) { |
1099 | 727k | img.tiles[i].tileComps[comp].resLevels[r].precinctWidth = |
1100 | 727k | img.tiles[0].tileComps[0].resLevels[r].precinctWidth; |
1101 | 727k | img.tiles[i].tileComps[comp].resLevels[r].precinctHeight = |
1102 | 727k | img.tiles[0].tileComps[0].resLevels[r].precinctHeight; |
1103 | 727k | } |
1104 | 217k | } |
1105 | 245k | } |
1106 | 87.8k | } |
1107 | 28.2k | haveCOD = gTrue; |
1108 | 28.2k | break; |
1109 | 5.48k | case 0x53: // COC - coding style component |
1110 | 5.48k | cover(22); |
1111 | 5.48k | if (!haveCOD) { |
1112 | 130 | error(errSyntaxError, getPos(), |
1113 | 130 | "JPX COC marker segment before COD segment"); |
1114 | 130 | return jpxDecodeFatalError; |
1115 | 130 | } |
1116 | 5.35k | comp = 0; |
1117 | 5.35k | if ((img.nComps > 256 && !readUWord(&comp)) || |
1118 | 5.35k | (img.nComps <= 256 && !readUByte(&comp)) || |
1119 | 5.35k | comp >= img.nComps || |
1120 | 5.35k | !readUByte(&style) || |
1121 | 5.35k | !readUByte(&nDecompLevels) || |
1122 | 5.35k | !readUByte(&codeBlockW) || |
1123 | 5.35k | !readUByte(&codeBlockH) || |
1124 | 5.35k | !readUByte(&codeBlockStyle) || |
1125 | 5.35k | !readUByte(&transform)) { |
1126 | 1.50k | error(errSyntaxError, getPos(), "Error in JPX COC marker segment"); |
1127 | 1.50k | return jpxDecodeFatalError; |
1128 | 1.50k | } |
1129 | 3.85k | if (nDecompLevels < 1 || |
1130 | 3.85k | nDecompLevels > 31 || |
1131 | 3.85k | codeBlockW > 8 || |
1132 | 3.85k | codeBlockH > 8) { |
1133 | 85 | error(errSyntaxError, getPos(), "Error in JPX COC marker segment"); |
1134 | 85 | return jpxDecodeFatalError; |
1135 | 85 | } |
1136 | 3.77k | style = (img.tiles[0].tileComps[comp].style & ~1) | (style & 1); |
1137 | 3.77k | codeBlockW += 2; |
1138 | 3.77k | codeBlockH += 2; |
1139 | 17.5k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
1140 | 13.7k | img.tiles[i].tileComps[comp].style = style; |
1141 | 13.7k | img.tiles[i].tileComps[comp].nDecompLevels = nDecompLevels; |
1142 | 13.7k | img.tiles[i].tileComps[comp].codeBlockW = codeBlockW; |
1143 | 13.7k | img.tiles[i].tileComps[comp].codeBlockH = codeBlockH; |
1144 | 13.7k | img.tiles[i].tileComps[comp].codeBlockStyle = codeBlockStyle; |
1145 | 13.7k | img.tiles[i].tileComps[comp].transform = transform; |
1146 | 13.7k | img.tiles[i].tileComps[comp].resLevels = |
1147 | 13.7k | (JPXResLevel *)greallocn( |
1148 | 13.7k | img.tiles[i].tileComps[comp].resLevels, |
1149 | 13.7k | nDecompLevels + 1, |
1150 | 13.7k | sizeof(JPXResLevel)); |
1151 | 386k | for (r = 0; r <= nDecompLevels; ++r) { |
1152 | 372k | img.tiles[i].tileComps[comp].resLevels[r].precincts = NULL; |
1153 | 372k | } |
1154 | 13.7k | } |
1155 | 98.4k | for (r = 0; r <= nDecompLevels; ++r) { |
1156 | 95.0k | if (style & 0x01) { |
1157 | 1.00k | if (!readUByte(&precinctSize)) { |
1158 | 130 | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1159 | 130 | return jpxDecodeFatalError; |
1160 | 130 | } |
1161 | 874 | if (r > 0 && ((precinctSize & 0x0f) == 0 || |
1162 | 372 | (precinctSize & 0xf0) == 0)) { |
1163 | 238 | error(errSyntaxError, getPos(), |
1164 | 238 | "Invalid precinct size in JPX COD marker segment"); |
1165 | 238 | return jpxDecodeFatalError; |
1166 | 238 | } |
1167 | 636 | img.tiles[0].tileComps[comp].resLevels[r].precinctWidth = |
1168 | 636 | precinctSize & 0x0f; |
1169 | 636 | img.tiles[0].tileComps[comp].resLevels[r].precinctHeight = |
1170 | 636 | (precinctSize >> 4) & 0x0f; |
1171 | 94.0k | } else { |
1172 | 94.0k | img.tiles[0].tileComps[comp].resLevels[r].precinctWidth = 15; |
1173 | 94.0k | img.tiles[0].tileComps[comp].resLevels[r].precinctHeight = 15; |
1174 | 94.0k | } |
1175 | 95.0k | } |
1176 | 12.3k | for (i = 1; i < img.nXTiles * img.nYTiles; ++i) { |
1177 | 284k | for (r = 0; r <= img.tiles[i].tileComps[comp].nDecompLevels; ++r) { |
1178 | 275k | img.tiles[i].tileComps[comp].resLevels[r].precinctWidth = |
1179 | 275k | img.tiles[0].tileComps[comp].resLevels[r].precinctWidth; |
1180 | 275k | img.tiles[i].tileComps[comp].resLevels[r].precinctHeight = |
1181 | 275k | img.tiles[0].tileComps[comp].resLevels[r].precinctHeight; |
1182 | 275k | } |
1183 | 8.98k | } |
1184 | 3.40k | break; |
1185 | 27.7k | case 0x5c: // QCD - quantization default |
1186 | 27.7k | cover(23); |
1187 | 27.7k | if (!haveSIZ) { |
1188 | 4 | error(errSyntaxError, getPos(), |
1189 | 4 | "JPX QCD marker segment before SIZ segment"); |
1190 | 4 | return jpxDecodeFatalError; |
1191 | 4 | } |
1192 | 27.7k | if (!readUByte(&img.tiles[0].tileComps[0].quantStyle)) { |
1193 | 131 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1194 | 131 | return jpxDecodeFatalError; |
1195 | 131 | } |
1196 | 27.6k | if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x00) { |
1197 | 9.75k | if (segLen <= 3) { |
1198 | 0 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1199 | 0 | return jpxDecodeFatalError; |
1200 | 0 | } |
1201 | 9.75k | img.tiles[0].tileComps[0].nQuantSteps = segLen - 3; |
1202 | 9.75k | img.tiles[0].tileComps[0].quantSteps = |
1203 | 9.75k | (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps, |
1204 | 9.75k | img.tiles[0].tileComps[0].nQuantSteps, |
1205 | 9.75k | sizeof(Guint)); |
1206 | 510k | for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) { |
1207 | 500k | if (!readUByte(&img.tiles[0].tileComps[0].quantSteps[i])) { |
1208 | 189 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1209 | 189 | return jpxDecodeFatalError; |
1210 | 189 | } |
1211 | 500k | } |
1212 | 17.9k | } else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x01) { |
1213 | 279 | img.tiles[0].tileComps[0].nQuantSteps = 1; |
1214 | 279 | img.tiles[0].tileComps[0].quantSteps = |
1215 | 279 | (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps, |
1216 | 279 | img.tiles[0].tileComps[0].nQuantSteps, |
1217 | 279 | sizeof(Guint)); |
1218 | 279 | if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[0])) { |
1219 | 253 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1220 | 253 | return jpxDecodeFatalError; |
1221 | 253 | } |
1222 | 17.6k | } else if ((img.tiles[0].tileComps[0].quantStyle & 0x1f) == 0x02) { |
1223 | 17.4k | if (segLen < 5) { |
1224 | 133 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1225 | 133 | return jpxDecodeFatalError; |
1226 | 133 | } |
1227 | 17.3k | img.tiles[0].tileComps[0].nQuantSteps = (segLen - 3) / 2; |
1228 | 17.3k | img.tiles[0].tileComps[0].quantSteps = |
1229 | 17.3k | (Guint *)greallocn(img.tiles[0].tileComps[0].quantSteps, |
1230 | 17.3k | img.tiles[0].tileComps[0].nQuantSteps, |
1231 | 17.3k | sizeof(Guint)); |
1232 | 291k | for (i = 0; i < img.tiles[0].tileComps[0].nQuantSteps; ++i) { |
1233 | 274k | if (!readUWord(&img.tiles[0].tileComps[0].quantSteps[i])) { |
1234 | 516 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1235 | 516 | return jpxDecodeFatalError; |
1236 | 516 | } |
1237 | 274k | } |
1238 | 17.3k | } else { |
1239 | 128 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1240 | 128 | return jpxDecodeFatalError; |
1241 | 128 | } |
1242 | 109k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
1243 | 312k | for (comp = 0; comp < img.nComps; ++comp) { |
1244 | 229k | if (!(i == 0 && comp == 0)) { |
1245 | 203k | img.tiles[i].tileComps[comp].quantStyle = |
1246 | 203k | img.tiles[0].tileComps[0].quantStyle; |
1247 | 203k | img.tiles[i].tileComps[comp].nQuantSteps = |
1248 | 203k | img.tiles[0].tileComps[0].nQuantSteps; |
1249 | 203k | img.tiles[i].tileComps[comp].quantSteps = |
1250 | 203k | (Guint *)greallocn(img.tiles[i].tileComps[comp].quantSteps, |
1251 | 203k | img.tiles[0].tileComps[0].nQuantSteps, |
1252 | 203k | sizeof(Guint)); |
1253 | 3.95M | for (j = 0; j < img.tiles[0].tileComps[0].nQuantSteps; ++j) { |
1254 | 3.75M | img.tiles[i].tileComps[comp].quantSteps[j] = |
1255 | 3.75M | img.tiles[0].tileComps[0].quantSteps[j]; |
1256 | 3.75M | } |
1257 | 203k | } |
1258 | 229k | } |
1259 | 82.7k | } |
1260 | 26.4k | haveQCD = gTrue; |
1261 | 26.4k | break; |
1262 | 2.67k | case 0x5d: // QCC - quantization component |
1263 | 2.67k | cover(24); |
1264 | 2.67k | if (!haveQCD) { |
1265 | 145 | error(errSyntaxError, getPos(), |
1266 | 145 | "JPX QCC marker segment before QCD segment"); |
1267 | 145 | return jpxDecodeFatalError; |
1268 | 145 | } |
1269 | 2.53k | comp = 0; |
1270 | 2.53k | if ((img.nComps > 256 && !readUWord(&comp)) || |
1271 | 2.53k | (img.nComps <= 256 && !readUByte(&comp)) || |
1272 | 2.53k | comp >= img.nComps || |
1273 | 2.53k | !readUByte(&img.tiles[0].tileComps[comp].quantStyle)) { |
1274 | 335 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1275 | 335 | return jpxDecodeFatalError; |
1276 | 335 | } |
1277 | 2.19k | if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x00) { |
1278 | 640 | if (segLen <= (img.nComps > 256 ? 5U : 4U)) { |
1279 | 236 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1280 | 236 | return jpxDecodeFatalError; |
1281 | 236 | } |
1282 | 404 | img.tiles[0].tileComps[comp].nQuantSteps = |
1283 | 404 | segLen - (img.nComps > 256 ? 5 : 4); |
1284 | 404 | img.tiles[0].tileComps[comp].quantSteps = |
1285 | 404 | (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps, |
1286 | 404 | img.tiles[0].tileComps[comp].nQuantSteps, |
1287 | 404 | sizeof(Guint)); |
1288 | 119k | for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) { |
1289 | 118k | if (!readUByte(&img.tiles[0].tileComps[comp].quantSteps[i])) { |
1290 | 134 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1291 | 134 | return jpxDecodeFatalError; |
1292 | 134 | } |
1293 | 118k | } |
1294 | 1.55k | } else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x01) { |
1295 | 465 | img.tiles[0].tileComps[comp].nQuantSteps = 1; |
1296 | 465 | img.tiles[0].tileComps[comp].quantSteps = |
1297 | 465 | (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps, |
1298 | 465 | img.tiles[0].tileComps[comp].nQuantSteps, |
1299 | 465 | sizeof(Guint)); |
1300 | 465 | if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[0])) { |
1301 | 26 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1302 | 26 | return jpxDecodeFatalError; |
1303 | 26 | } |
1304 | 1.09k | } else if ((img.tiles[0].tileComps[comp].quantStyle & 0x1f) == 0x02) { |
1305 | 889 | if (segLen < (img.nComps > 256 ? 5U : 4U) + 2) { |
1306 | 68 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1307 | 68 | return jpxDecodeFatalError; |
1308 | 68 | } |
1309 | 821 | img.tiles[0].tileComps[comp].nQuantSteps = |
1310 | 821 | (segLen - (img.nComps > 256 ? 5 : 4)) / 2; |
1311 | 821 | img.tiles[0].tileComps[comp].quantSteps = |
1312 | 821 | (Guint *)greallocn(img.tiles[0].tileComps[comp].quantSteps, |
1313 | 821 | img.tiles[0].tileComps[comp].nQuantSteps, |
1314 | 821 | sizeof(Guint)); |
1315 | 1.32M | for (i = 0; i < img.tiles[0].tileComps[comp].nQuantSteps; ++i) { |
1316 | 1.32M | if (!readUWord(&img.tiles[0].tileComps[comp].quantSteps[i])) { |
1317 | 256 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1318 | 256 | return jpxDecodeFatalError; |
1319 | 256 | } |
1320 | 1.32M | } |
1321 | 821 | } else { |
1322 | 203 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1323 | 203 | return jpxDecodeFatalError; |
1324 | 203 | } |
1325 | 2.89k | for (i = 1; i < img.nXTiles * img.nYTiles; ++i) { |
1326 | 1.62k | img.tiles[i].tileComps[comp].quantStyle = |
1327 | 1.62k | img.tiles[0].tileComps[comp].quantStyle; |
1328 | 1.62k | img.tiles[i].tileComps[comp].nQuantSteps = |
1329 | 1.62k | img.tiles[0].tileComps[comp].nQuantSteps; |
1330 | 1.62k | img.tiles[i].tileComps[comp].quantSteps = |
1331 | 1.62k | (Guint *)greallocn(img.tiles[i].tileComps[comp].quantSteps, |
1332 | 1.62k | img.tiles[0].tileComps[comp].nQuantSteps, |
1333 | 1.62k | sizeof(Guint)); |
1334 | 2.44M | for (j = 0; j < img.tiles[0].tileComps[comp].nQuantSteps; ++j) { |
1335 | 2.44M | img.tiles[i].tileComps[comp].quantSteps[j] = |
1336 | 2.44M | img.tiles[0].tileComps[comp].quantSteps[j]; |
1337 | 2.44M | } |
1338 | 1.62k | } |
1339 | 1.27k | break; |
1340 | 124k | case 0x5e: // RGN - region of interest |
1341 | 124k | cover(25); |
1342 | 124k | #if 1 //~ ROI is unimplemented |
1343 | 124k | error(errUnimplemented, -1, "got a JPX RGN segment"); |
1344 | 124k | if (segLen > 2 && |
1345 | 124k | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1346 | 829 | error(errSyntaxError, getPos(), "Error in JPX RGN marker segment"); |
1347 | 829 | return jpxDecodeFatalError; |
1348 | 829 | } |
1349 | | #else |
1350 | | if ((img.nComps > 256 && !readUWord(&comp)) || |
1351 | | (img.nComps <= 256 && !readUByte(&comp)) || |
1352 | | comp >= img.nComps || |
1353 | | !readUByte(&compInfo[comp].defROI.style) || |
1354 | | !readUByte(&compInfo[comp].defROI.shift)) { |
1355 | | error(errSyntaxError, getPos(), "Error in JPX RGN marker segment"); |
1356 | | return jpxDecodeFatalError; |
1357 | | } |
1358 | | #endif |
1359 | 123k | break; |
1360 | 123k | case 0x5f: // POC - progression order change |
1361 | 390 | cover(26); |
1362 | 390 | #if 1 //~ progression order changes are unimplemented |
1363 | 390 | error(errUnimplemented, -1, "got a JPX POC segment"); |
1364 | 390 | if (segLen > 2 && |
1365 | 390 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1366 | 148 | error(errSyntaxError, getPos(), "Error in JPX POC marker segment"); |
1367 | 148 | return jpxDecodeFatalError; |
1368 | 148 | } |
1369 | | #else |
1370 | | nProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7); |
1371 | | progs = (JPXProgOrder *)gmallocn(nProgs, sizeof(JPXProgOrder)); |
1372 | | for (i = 0; i < nProgs; ++i) { |
1373 | | if (!readUByte(&progs[i].startRes) || |
1374 | | !(img.nComps > 256 && readUWord(&progs[i].startComp)) || |
1375 | | !(img.nComps <= 256 && readUByte(&progs[i].startComp)) || |
1376 | | !readUWord(&progs[i].endLayer) || |
1377 | | !readUByte(&progs[i].endRes) || |
1378 | | !(img.nComps > 256 && readUWord(&progs[i].endComp)) || |
1379 | | !(img.nComps <= 256 && readUByte(&progs[i].endComp)) || |
1380 | | !readUByte(&progs[i].progOrder)) { |
1381 | | error(errSyntaxError, getPos(), "Error in JPX POC marker segment"); |
1382 | | return jpxDecodeFatalError; |
1383 | | } |
1384 | | } |
1385 | | #endif |
1386 | 242 | break; |
1387 | 5.00k | case 0x60: // PPM - packed packet headers, main header |
1388 | 5.00k | cover(27); |
1389 | 5.00k | #if 1 //~ packed packet headers are unimplemented |
1390 | 5.00k | error(errUnimplemented, -1, "Got a JPX PPM segment"); |
1391 | 5.00k | if (segLen > 2 && |
1392 | 5.00k | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1393 | 130 | error(errSyntaxError, getPos(), "Error in JPX PPM marker segment"); |
1394 | 130 | return jpxDecodeFatalError; |
1395 | 130 | } |
1396 | 4.87k | #endif |
1397 | 4.87k | break; |
1398 | 4.87k | case 0x55: // TLM - tile-part lengths |
1399 | | // skipped |
1400 | 534 | cover(28); |
1401 | 534 | if (segLen > 2 && |
1402 | 534 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1403 | 273 | error(errSyntaxError, getPos(), "Error in JPX TLM marker segment"); |
1404 | 273 | return jpxDecodeFatalError; |
1405 | 273 | } |
1406 | 261 | break; |
1407 | 261 | case 0x57: // PLM - packet length, main header |
1408 | | // skipped |
1409 | 157 | cover(29); |
1410 | 157 | if (segLen > 2 && |
1411 | 157 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1412 | 128 | error(errSyntaxError, getPos(), "Error in JPX PLM marker segment"); |
1413 | 128 | return jpxDecodeFatalError; |
1414 | 128 | } |
1415 | 29 | break; |
1416 | 151 | case 0x63: // CRG - component registration |
1417 | | // skipped |
1418 | 151 | cover(30); |
1419 | 151 | if (segLen > 2 && |
1420 | 151 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1421 | 134 | error(errSyntaxError, getPos(), "Error in JPX CRG marker segment"); |
1422 | 134 | return jpxDecodeFatalError; |
1423 | 134 | } |
1424 | 17 | break; |
1425 | 106 | case 0x64: // COM - comment |
1426 | | // skipped |
1427 | 106 | cover(31); |
1428 | 106 | if (segLen > 2 && |
1429 | 106 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1430 | 0 | error(errSyntaxError, getPos(), "Error in JPX COM marker segment"); |
1431 | 0 | return jpxDecodeFatalError; |
1432 | 0 | } |
1433 | 106 | break; |
1434 | 21.6k | case 0x90: // SOT - start of tile |
1435 | 21.6k | cover(32); |
1436 | 21.6k | haveSOT = gTrue; |
1437 | 21.6k | break; |
1438 | 35.6k | default: |
1439 | 35.6k | cover(33); |
1440 | 35.6k | error(errSyntaxError, getPos(), |
1441 | 35.6k | "Unknown marker segment {0:02x} in JPX stream", segType); |
1442 | 35.6k | if (segLen > 2) { |
1443 | 22.2k | bufStr->discardChars(segLen - 2); |
1444 | 22.2k | } |
1445 | 35.6k | break; |
1446 | 324k | } |
1447 | 324k | } while (!haveSOT); |
1448 | | |
1449 | 21.6k | if (!haveSIZ) { |
1450 | 135 | error(errSyntaxError, getPos(), |
1451 | 135 | "Missing SIZ marker segment in JPX stream"); |
1452 | 135 | return jpxDecodeFatalError; |
1453 | 135 | } |
1454 | 21.4k | if (!haveCOD) { |
1455 | 0 | error(errSyntaxError, getPos(), |
1456 | 0 | "Missing COD marker segment in JPX stream"); |
1457 | 0 | return jpxDecodeFatalError; |
1458 | 0 | } |
1459 | 21.4k | if (!haveQCD) { |
1460 | 0 | error(errSyntaxError, getPos(), |
1461 | 0 | "Missing QCD marker segment in JPX stream"); |
1462 | 0 | return jpxDecodeFatalError; |
1463 | 0 | } |
1464 | | |
1465 | | //----- read the tile-parts |
1466 | 21.4k | ok = gTrue; |
1467 | 23.0k | while (1) { |
1468 | 23.0k | if (!readTilePart()) { |
1469 | 19.3k | ok = gFalse; |
1470 | 19.3k | break; |
1471 | 19.3k | } |
1472 | 3.70k | if (!readMarkerHdr(&segType, &segLen)) { |
1473 | 515 | error(errSyntaxError, getPos(), "Error in JPX codestream"); |
1474 | 515 | ok = gFalse; |
1475 | 515 | break; |
1476 | 515 | } |
1477 | 3.19k | if (segType != 0x90) { // SOT - start of tile |
1478 | 1.61k | break; |
1479 | 1.61k | } |
1480 | 3.19k | } |
1481 | | |
1482 | 21.4k | if (segType != 0xd9) { // EOC - end of codestream |
1483 | 21.4k | error(errSyntaxError, getPos(), "Missing EOC marker in JPX codestream"); |
1484 | 21.4k | ok = gFalse; |
1485 | 21.4k | } |
1486 | | |
1487 | | //----- finish decoding the image |
1488 | 25.8k | for (i = 0; i < img.nXTiles * img.nYTiles; ++i) { |
1489 | 21.7k | tile = &img.tiles[i]; |
1490 | 21.7k | if (!tile->init) { |
1491 | 15.0k | error(errSyntaxError, getPos(), "Uninitialized tile in JPX codestream"); |
1492 | 15.0k | return jpxDecodeFatalError; |
1493 | 15.0k | } |
1494 | 31.9k | for (comp = 0; comp < img.nComps; ++comp) { |
1495 | 25.2k | tileComp = &tile->tileComps[comp]; |
1496 | 25.2k | inverseTransform(tileComp); |
1497 | 25.2k | } |
1498 | 6.70k | if (!inverseMultiCompAndDC(tile)) { |
1499 | 2.35k | return jpxDecodeFatalError; |
1500 | 2.35k | } |
1501 | 6.70k | } |
1502 | | |
1503 | | //~ can free memory below tileComps here, and also tileComp.buf |
1504 | | |
1505 | 4.09k | return ok ? jpxDecodeOk : jpxDecodeNonFatalError; |
1506 | 21.4k | } |
1507 | | |
1508 | 23.0k | GBool JPXStream::readTilePart() { |
1509 | 23.0k | JPXTile *tile; |
1510 | 23.0k | JPXTileComp *tileComp; |
1511 | 23.0k | JPXResLevel *resLevel; |
1512 | 23.0k | JPXPrecinct *precinct; |
1513 | 23.0k | JPXSubband *subband; |
1514 | 23.0k | JPXCodeBlock *cb; |
1515 | 23.0k | int *sbCoeffs; |
1516 | 23.0k | GBool haveSOD; |
1517 | 23.0k | Guint tileIdx, tilePartLen, tilePartIdx, nTileParts; |
1518 | 23.0k | GBool tilePartToEOC; |
1519 | 23.0k | Guint style, progOrder, nLayers, multiComp, nDecompLevels; |
1520 | 23.0k | Guint codeBlockW, codeBlockH, codeBlockStyle, transform; |
1521 | 23.0k | Guint precinctSize, qStyle; |
1522 | 23.0k | Guint px0, py0, px1, py1; |
1523 | 23.0k | Guint preCol0, preCol1, preRow0, preRow1, preCol, preRow; |
1524 | 23.0k | Guint cbCol0, cbCol1, cbRow0, cbRow1, cbX, cbY; |
1525 | 23.0k | Guint n, nSBs, nx, ny, comp, segLen; |
1526 | 23.0k | Guint i, j, k, r, pre, sb, cbi, cbj; |
1527 | 23.0k | int segType, level; |
1528 | | |
1529 | | // process the SOT marker segment |
1530 | 23.0k | if (!readUWord(&tileIdx) || |
1531 | 23.0k | !readULong(&tilePartLen) || |
1532 | 23.0k | !readUByte(&tilePartIdx) || |
1533 | 23.0k | !readUByte(&nTileParts)) { |
1534 | 1.67k | error(errSyntaxError, getPos(), "Error in JPX SOT marker segment"); |
1535 | 1.67k | return gFalse; |
1536 | 1.67k | } |
1537 | | |
1538 | | // check tileIdx and tilePartIdx |
1539 | | // (this ignores nTileParts, because some encoders get it wrong) |
1540 | 21.3k | if (tileIdx >= img.nXTiles * img.nYTiles || |
1541 | 21.3k | tilePartIdx != img.tiles[tileIdx].nextTilePart || |
1542 | 21.3k | (tilePartIdx > 0 && !img.tiles[tileIdx].init) || |
1543 | 21.3k | (tilePartIdx == 0 && img.tiles[tileIdx].init)) { |
1544 | 541 | error(errSyntaxError, getPos(), "Weird tile-part header in JPX stream"); |
1545 | 541 | return gFalse; |
1546 | 541 | } |
1547 | 20.8k | ++img.tiles[tileIdx].nextTilePart; |
1548 | | |
1549 | 20.8k | tilePartToEOC = tilePartLen == 0; |
1550 | 20.8k | tilePartLen -= 12; // subtract size of SOT segment |
1551 | | |
1552 | 20.8k | haveSOD = gFalse; |
1553 | 163k | do { |
1554 | 163k | if (!readMarkerHdr(&segType, &segLen)) { |
1555 | 2.31k | error(errSyntaxError, getPos(), "Error in JPX tile-part codestream"); |
1556 | 2.31k | return gFalse; |
1557 | 2.31k | } |
1558 | 160k | tilePartLen -= 2 + segLen; |
1559 | 160k | switch (segType) { |
1560 | 15.6k | case 0x52: // COD - coding style default |
1561 | 15.6k | cover(34); |
1562 | 15.6k | if (tilePartIdx != 0) { |
1563 | 150 | error(errSyntaxError, getPos(), "Extraneous JPX COD marker segment"); |
1564 | 150 | return gFalse; |
1565 | 150 | } |
1566 | 15.4k | if (!readUByte(&style) || |
1567 | 15.4k | !readUByte(&progOrder) || |
1568 | 15.4k | !readUWord(&nLayers) || |
1569 | 15.4k | !readUByte(&multiComp) || |
1570 | 15.4k | !readUByte(&nDecompLevels) || |
1571 | 15.4k | !readUByte(&codeBlockW) || |
1572 | 15.4k | !readUByte(&codeBlockH) || |
1573 | 15.4k | !readUByte(&codeBlockStyle) || |
1574 | 15.4k | !readUByte(&transform)) { |
1575 | 1.83k | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1576 | 1.83k | return gFalse; |
1577 | 1.83k | } |
1578 | 13.6k | if (nDecompLevels < 1 || |
1579 | 13.6k | nDecompLevels > 31 || |
1580 | 13.6k | codeBlockW > 8 || |
1581 | 13.6k | codeBlockH > 8) { |
1582 | 373 | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1583 | 373 | return gFalse; |
1584 | 373 | } |
1585 | 13.2k | codeBlockW += 2; |
1586 | 13.2k | codeBlockH += 2; |
1587 | 13.2k | img.tiles[tileIdx].progOrder = progOrder; |
1588 | 13.2k | img.tiles[tileIdx].nLayers = nLayers; |
1589 | 13.2k | img.tiles[tileIdx].multiComp = multiComp; |
1590 | 51.4k | for (comp = 0; comp < img.nComps; ++comp) { |
1591 | 38.1k | img.tiles[tileIdx].tileComps[comp].style = style; |
1592 | 38.1k | img.tiles[tileIdx].tileComps[comp].nDecompLevels = nDecompLevels; |
1593 | 38.1k | img.tiles[tileIdx].tileComps[comp].codeBlockW = codeBlockW; |
1594 | 38.1k | img.tiles[tileIdx].tileComps[comp].codeBlockH = codeBlockH; |
1595 | 38.1k | img.tiles[tileIdx].tileComps[comp].codeBlockStyle = codeBlockStyle; |
1596 | 38.1k | img.tiles[tileIdx].tileComps[comp].transform = transform; |
1597 | 38.1k | img.tiles[tileIdx].tileComps[comp].resLevels = |
1598 | 38.1k | (JPXResLevel *)greallocn( |
1599 | 38.1k | img.tiles[tileIdx].tileComps[comp].resLevels, |
1600 | 38.1k | nDecompLevels + 1, |
1601 | 38.1k | sizeof(JPXResLevel)); |
1602 | 152k | for (r = 0; r <= nDecompLevels; ++r) { |
1603 | 114k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = NULL; |
1604 | 114k | } |
1605 | 38.1k | } |
1606 | 52.8k | for (r = 0; r <= nDecompLevels; ++r) { |
1607 | 40.0k | if (style & 0x01) { |
1608 | 1.16k | if (!readUByte(&precinctSize)) { |
1609 | 132 | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1610 | 132 | return gFalse; |
1611 | 132 | } |
1612 | 1.03k | if (r > 0 && ((precinctSize & 0x0f) == 0 || |
1613 | 644 | (precinctSize & 0xf0) == 0)) { |
1614 | 256 | error(errSyntaxError, getPos(), |
1615 | 256 | "Invalid precinct size in JPX COD marker segment"); |
1616 | 256 | return gFalse; |
1617 | 256 | } |
1618 | 776 | img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth = |
1619 | 776 | precinctSize & 0x0f; |
1620 | 776 | img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight = |
1621 | 776 | (precinctSize >> 4) & 0x0f; |
1622 | 38.8k | } else { |
1623 | 38.8k | img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth = 15; |
1624 | 38.8k | img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight = 15; |
1625 | 38.8k | } |
1626 | 40.0k | } |
1627 | 36.9k | for (comp = 1; comp < img.nComps; ++comp) { |
1628 | 96.1k | for (r = 0; r <= nDecompLevels; ++r) { |
1629 | 71.9k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth = |
1630 | 71.9k | img.tiles[tileIdx].tileComps[0].resLevels[r].precinctWidth; |
1631 | 71.9k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight = |
1632 | 71.9k | img.tiles[tileIdx].tileComps[0].resLevels[r].precinctHeight; |
1633 | 71.9k | } |
1634 | 24.1k | } |
1635 | 12.8k | break; |
1636 | 34.5k | case 0x53: // COC - coding style component |
1637 | 34.5k | cover(35); |
1638 | 34.5k | if (tilePartIdx != 0) { |
1639 | 259 | error(errSyntaxError, getPos(), "Extraneous JPX COC marker segment"); |
1640 | 259 | return gFalse; |
1641 | 259 | } |
1642 | 34.3k | comp = 0; |
1643 | 34.3k | if ((img.nComps > 256 && !readUWord(&comp)) || |
1644 | 34.3k | (img.nComps <= 256 && !readUByte(&comp)) || |
1645 | 34.3k | comp >= img.nComps || |
1646 | 34.3k | !readUByte(&style) || |
1647 | 34.3k | !readUByte(&nDecompLevels) || |
1648 | 34.3k | !readUByte(&codeBlockW) || |
1649 | 34.3k | !readUByte(&codeBlockH) || |
1650 | 34.3k | !readUByte(&codeBlockStyle) || |
1651 | 34.3k | !readUByte(&transform)) { |
1652 | 1.71k | error(errSyntaxError, getPos(), "Error in JPX COC marker segment"); |
1653 | 1.71k | return gFalse; |
1654 | 1.71k | } |
1655 | 32.6k | if (nDecompLevels < 1 || |
1656 | 32.6k | nDecompLevels > 31 || |
1657 | 32.6k | codeBlockW > 8 || |
1658 | 32.6k | codeBlockH > 8) { |
1659 | 453 | error(errSyntaxError, getPos(), "Error in JPX COC marker segment"); |
1660 | 453 | return gFalse; |
1661 | 453 | } |
1662 | 32.1k | img.tiles[tileIdx].tileComps[comp].style = |
1663 | 32.1k | (img.tiles[tileIdx].tileComps[comp].style & ~1) | (style & 1); |
1664 | 32.1k | img.tiles[tileIdx].tileComps[comp].nDecompLevels = nDecompLevels; |
1665 | 32.1k | img.tiles[tileIdx].tileComps[comp].codeBlockW = codeBlockW + 2; |
1666 | 32.1k | img.tiles[tileIdx].tileComps[comp].codeBlockH = codeBlockH + 2; |
1667 | 32.1k | img.tiles[tileIdx].tileComps[comp].codeBlockStyle = codeBlockStyle; |
1668 | 32.1k | img.tiles[tileIdx].tileComps[comp].transform = transform; |
1669 | 32.1k | img.tiles[tileIdx].tileComps[comp].resLevels = |
1670 | 32.1k | (JPXResLevel *)greallocn( |
1671 | 32.1k | img.tiles[tileIdx].tileComps[comp].resLevels, |
1672 | 32.1k | nDecompLevels + 1, |
1673 | 32.1k | sizeof(JPXResLevel)); |
1674 | 754k | for (r = 0; r <= nDecompLevels; ++r) { |
1675 | 722k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precincts = NULL; |
1676 | 722k | } |
1677 | 753k | for (r = 0; r <= nDecompLevels; ++r) { |
1678 | 721k | if (style & 0x01) { |
1679 | 20.3k | if (!readUByte(&precinctSize)) { |
1680 | 160 | error(errSyntaxError, getPos(), "Error in JPX COD marker segment"); |
1681 | 160 | return gFalse; |
1682 | 160 | } |
1683 | 20.1k | if (r > 0 && ((precinctSize & 0x0f) == 0 || |
1684 | 10.0k | (precinctSize & 0xf0) == 0)) { |
1685 | 257 | error(errSyntaxError, getPos(), |
1686 | 257 | "Invalid precinct size in JPX COD marker segment"); |
1687 | 257 | return gFalse; |
1688 | 257 | } |
1689 | 19.9k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth = |
1690 | 19.9k | precinctSize & 0x0f; |
1691 | 19.9k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight = |
1692 | 19.9k | (precinctSize >> 4) & 0x0f; |
1693 | 701k | } else { |
1694 | 701k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctWidth = 15; |
1695 | 701k | img.tiles[tileIdx].tileComps[comp].resLevels[r].precinctHeight = 15; |
1696 | 701k | } |
1697 | 721k | } |
1698 | 31.7k | break; |
1699 | 31.7k | case 0x5c: // QCD - quantization default |
1700 | 17.5k | cover(36); |
1701 | 17.5k | if (tilePartIdx != 0) { |
1702 | 436 | error(errSyntaxError, getPos(), "Extraneous JPX QCD marker segment"); |
1703 | 436 | return gFalse; |
1704 | 436 | } |
1705 | 17.0k | if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantStyle)) { |
1706 | 171 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1707 | 171 | return gFalse; |
1708 | 171 | } |
1709 | 16.9k | if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x00) { |
1710 | 7.27k | if (segLen <= 3) { |
1711 | 256 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1712 | 256 | return gFalse; |
1713 | 256 | } |
1714 | 7.02k | img.tiles[tileIdx].tileComps[0].nQuantSteps = segLen - 3; |
1715 | 7.02k | img.tiles[tileIdx].tileComps[0].quantSteps = |
1716 | 7.02k | (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps, |
1717 | 7.02k | img.tiles[tileIdx].tileComps[0].nQuantSteps, |
1718 | 7.02k | sizeof(Guint)); |
1719 | 293k | for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) { |
1720 | 286k | if (!readUByte(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) { |
1721 | 57 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1722 | 57 | return gFalse; |
1723 | 57 | } |
1724 | 286k | } |
1725 | 9.62k | } else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x01) { |
1726 | 34 | img.tiles[tileIdx].tileComps[0].nQuantSteps = 1; |
1727 | 34 | img.tiles[tileIdx].tileComps[0].quantSteps = |
1728 | 34 | (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps, |
1729 | 34 | img.tiles[tileIdx].tileComps[0].nQuantSteps, |
1730 | 34 | sizeof(Guint)); |
1731 | 34 | if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[0])) { |
1732 | 34 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1733 | 34 | return gFalse; |
1734 | 34 | } |
1735 | 9.59k | } else if ((img.tiles[tileIdx].tileComps[0].quantStyle & 0x1f) == 0x02) { |
1736 | 9.41k | if (segLen < 5) { |
1737 | 260 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1738 | 260 | return gFalse; |
1739 | 260 | } |
1740 | 9.15k | img.tiles[tileIdx].tileComps[0].nQuantSteps = (segLen - 3) / 2; |
1741 | 9.15k | img.tiles[tileIdx].tileComps[0].quantSteps = |
1742 | 9.15k | (Guint *)greallocn(img.tiles[tileIdx].tileComps[0].quantSteps, |
1743 | 9.15k | img.tiles[tileIdx].tileComps[0].nQuantSteps, |
1744 | 9.15k | sizeof(Guint)); |
1745 | 259k | for (i = 0; i < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++i) { |
1746 | 250k | if (!readUWord(&img.tiles[tileIdx].tileComps[0].quantSteps[i])) { |
1747 | 502 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1748 | 502 | return gFalse; |
1749 | 502 | } |
1750 | 250k | } |
1751 | 9.15k | } else { |
1752 | 183 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1753 | 183 | return gFalse; |
1754 | 183 | } |
1755 | 45.6k | for (comp = 1; comp < img.nComps; ++comp) { |
1756 | 30.0k | img.tiles[tileIdx].tileComps[comp].quantStyle = |
1757 | 30.0k | img.tiles[tileIdx].tileComps[0].quantStyle; |
1758 | 30.0k | img.tiles[tileIdx].tileComps[comp].nQuantSteps = |
1759 | 30.0k | img.tiles[tileIdx].tileComps[0].nQuantSteps; |
1760 | 30.0k | img.tiles[tileIdx].tileComps[comp].quantSteps = |
1761 | 30.0k | (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps, |
1762 | 30.0k | img.tiles[tileIdx].tileComps[0].nQuantSteps, |
1763 | 30.0k | sizeof(Guint)); |
1764 | 780k | for (j = 0; j < img.tiles[tileIdx].tileComps[0].nQuantSteps; ++j) { |
1765 | 750k | img.tiles[tileIdx].tileComps[comp].quantSteps[j] = |
1766 | 750k | img.tiles[tileIdx].tileComps[0].quantSteps[j]; |
1767 | 750k | } |
1768 | 30.0k | } |
1769 | 15.6k | break; |
1770 | 7.68k | case 0x5d: // QCC - quantization component |
1771 | 7.68k | cover(37); |
1772 | 7.68k | if (tilePartIdx != 0) { |
1773 | 186 | error(errSyntaxError, getPos(), "Extraneous JPX QCC marker segment"); |
1774 | 186 | return gFalse; |
1775 | 186 | } |
1776 | 7.50k | comp = 0; |
1777 | 7.50k | if ((img.nComps > 256 && !readUWord(&comp)) || |
1778 | 7.50k | (img.nComps <= 256 && !readUByte(&comp)) || |
1779 | 7.50k | comp >= img.nComps || |
1780 | 7.50k | !readUByte(&img.tiles[tileIdx].tileComps[comp].quantStyle)) { |
1781 | 588 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1782 | 588 | return gFalse; |
1783 | 588 | } |
1784 | 6.91k | if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f) == 0x00) { |
1785 | 130 | if (segLen <= (img.nComps > 256 ? 5U : 4U)) { |
1786 | 0 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1787 | 0 | return gFalse; |
1788 | 0 | } |
1789 | 130 | img.tiles[tileIdx].tileComps[comp].nQuantSteps = |
1790 | 130 | segLen - (img.nComps > 256 ? 5 : 4); |
1791 | 130 | img.tiles[tileIdx].tileComps[comp].quantSteps = |
1792 | 130 | (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps, |
1793 | 130 | img.tiles[tileIdx].tileComps[comp].nQuantSteps, |
1794 | 130 | sizeof(Guint)); |
1795 | 32.7k | for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) { |
1796 | 32.6k | if (!readUByte(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) { |
1797 | 0 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1798 | 0 | return gFalse; |
1799 | 0 | } |
1800 | 32.6k | } |
1801 | 6.78k | } else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f) |
1802 | 6.78k | == 0x01) { |
1803 | 5.07k | img.tiles[tileIdx].tileComps[comp].nQuantSteps = 1; |
1804 | 5.07k | img.tiles[tileIdx].tileComps[comp].quantSteps = |
1805 | 5.07k | (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps, |
1806 | 5.07k | img.tiles[tileIdx].tileComps[comp].nQuantSteps, |
1807 | 5.07k | sizeof(Guint)); |
1808 | 5.07k | if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[0])) { |
1809 | 565 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1810 | 565 | return gFalse; |
1811 | 565 | } |
1812 | 5.07k | } else if ((img.tiles[tileIdx].tileComps[comp].quantStyle & 0x1f) |
1813 | 1.70k | == 0x02) { |
1814 | 1.48k | if (segLen < (img.nComps > 256 ? 5U : 4U) + 2) { |
1815 | 198 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1816 | 198 | return gFalse; |
1817 | 198 | } |
1818 | 1.28k | img.tiles[tileIdx].tileComps[comp].nQuantSteps = |
1819 | 1.28k | (segLen - (img.nComps > 256 ? 5 : 4)) / 2; |
1820 | 1.28k | img.tiles[tileIdx].tileComps[comp].quantSteps = |
1821 | 1.28k | (Guint *)greallocn(img.tiles[tileIdx].tileComps[comp].quantSteps, |
1822 | 1.28k | img.tiles[tileIdx].tileComps[comp].nQuantSteps, |
1823 | 1.28k | sizeof(Guint)); |
1824 | 163k | for (i = 0; i < img.tiles[tileIdx].tileComps[comp].nQuantSteps; ++i) { |
1825 | 162k | if (!readUWord(&img.tiles[tileIdx].tileComps[comp].quantSteps[i])) { |
1826 | 379 | error(errSyntaxError, getPos(), "Error in JPX QCD marker segment"); |
1827 | 379 | return gFalse; |
1828 | 379 | } |
1829 | 162k | } |
1830 | 1.28k | } else { |
1831 | 226 | error(errSyntaxError, getPos(), "Error in JPX QCC marker segment"); |
1832 | 226 | return gFalse; |
1833 | 226 | } |
1834 | 5.54k | break; |
1835 | 24.7k | case 0x5e: // RGN - region of interest |
1836 | 24.7k | cover(38); |
1837 | 24.7k | if (tilePartIdx != 0) { |
1838 | 1 | error(errSyntaxError, getPos(), "Extraneous JPX RGN marker segment"); |
1839 | 1 | return gFalse; |
1840 | 1 | } |
1841 | 24.7k | #if 1 //~ ROI is unimplemented |
1842 | 24.7k | error(errUnimplemented, -1, "Got a JPX RGN segment"); |
1843 | 24.7k | if (segLen > 2 && |
1844 | 24.7k | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1845 | 266 | error(errSyntaxError, getPos(), "Error in JPX RGN marker segment"); |
1846 | 266 | return gFalse; |
1847 | 266 | } |
1848 | | #else |
1849 | | if ((img.nComps > 256 && !readUWord(&comp)) || |
1850 | | (img.nComps <= 256 && !readUByte(&comp)) || |
1851 | | comp >= img.nComps || |
1852 | | !readUByte(&compInfo[comp].roi.style) || |
1853 | | !readUByte(&compInfo[comp].roi.shift)) { |
1854 | | error(errSyntaxError, getPos(), "Error in JPX RGN marker segment"); |
1855 | | return gFalse; |
1856 | | } |
1857 | | #endif |
1858 | 24.4k | break; |
1859 | 24.4k | case 0x5f: // POC - progression order change |
1860 | 593 | cover(39); |
1861 | 593 | #if 1 //~ progression order changes are unimplemented |
1862 | 593 | error(errUnimplemented, -1, "Got a JPX POC segment"); |
1863 | 593 | if (segLen > 2 && |
1864 | 593 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1865 | 255 | error(errSyntaxError, getPos(), "Error in JPX POC marker segment"); |
1866 | 255 | return gFalse; |
1867 | 255 | } |
1868 | | #else |
1869 | | nTileProgs = (segLen - 2) / (img.nComps > 256 ? 9 : 7); |
1870 | | tileProgs = (JPXProgOrder *)gmallocn(nTileProgs, sizeof(JPXProgOrder)); |
1871 | | for (i = 0; i < nTileProgs; ++i) { |
1872 | | if (!readUByte(&tileProgs[i].startRes) || |
1873 | | !(img.nComps > 256 && readUWord(&tileProgs[i].startComp)) || |
1874 | | !(img.nComps <= 256 && readUByte(&tileProgs[i].startComp)) || |
1875 | | !readUWord(&tileProgs[i].endLayer) || |
1876 | | !readUByte(&tileProgs[i].endRes) || |
1877 | | !(img.nComps > 256 && readUWord(&tileProgs[i].endComp)) || |
1878 | | !(img.nComps <= 256 && readUByte(&tileProgs[i].endComp)) || |
1879 | | !readUByte(&tileProgs[i].progOrder)) { |
1880 | | error(errSyntaxError, getPos(), "Error in JPX POC marker segment"); |
1881 | | return gFalse; |
1882 | | } |
1883 | | } |
1884 | | #endif |
1885 | 338 | break; |
1886 | 1.30k | case 0x61: // PPT - packed packet headers, tile-part hdr |
1887 | 1.30k | cover(40); |
1888 | 1.30k | #if 1 //~ packed packet headers are unimplemented |
1889 | 1.30k | error(errUnimplemented, -1, "Got a JPX PPT segment"); |
1890 | 1.30k | if (segLen > 2 && |
1891 | 1.30k | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1892 | 258 | error(errSyntaxError, getPos(), "Error in JPX PPT marker segment"); |
1893 | 258 | return gFalse; |
1894 | 258 | } |
1895 | 1.04k | #endif |
1896 | 1.31k | case 0x58: // PLT - packet length, tile-part header |
1897 | | // skipped |
1898 | 1.31k | cover(41); |
1899 | 1.31k | if (segLen > 2 && |
1900 | 1.31k | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1901 | 307 | error(errSyntaxError, getPos(), "Error in JPX PLT marker segment"); |
1902 | 307 | return gFalse; |
1903 | 307 | } |
1904 | 1.01k | break; |
1905 | 1.01k | case 0x64: // COM - comment |
1906 | | // skipped |
1907 | 715 | cover(42); |
1908 | 715 | if (segLen > 2 && |
1909 | 715 | bufStr->discardChars(segLen - 2) != segLen - 2) { |
1910 | 359 | error(errSyntaxError, getPos(), "Error in JPX COM marker segment"); |
1911 | 359 | return gFalse; |
1912 | 359 | } |
1913 | 356 | break; |
1914 | 7.43k | case 0x93: // SOD - start of data |
1915 | 7.43k | cover(43); |
1916 | 7.43k | haveSOD = gTrue; |
1917 | 7.43k | break; |
1918 | 50.4k | default: |
1919 | 50.4k | cover(44); |
1920 | 50.4k | error(errSyntaxError, getPos(), |
1921 | 50.4k | "Unknown marker segment {0:02x} in JPX tile-part stream", |
1922 | 50.4k | segType); |
1923 | 50.4k | if (segLen > 2) { |
1924 | 24.0k | bufStr->discardChars(segLen - 2); |
1925 | 24.0k | } |
1926 | 50.4k | break; |
1927 | 160k | } |
1928 | 160k | } while (!haveSOD); |
1929 | | |
1930 | 33.6k | for (comp = 0; comp < img.nComps; ++comp) { |
1931 | 26.5k | tileComp = &img.tiles[tileIdx].tileComps[comp]; |
1932 | 26.5k | qStyle = tileComp->quantStyle & 0x1f; |
1933 | 26.5k | if ((qStyle == 0 && tileComp->nQuantSteps < 3 * tileComp->nDecompLevels + 1) || |
1934 | 26.5k | (qStyle == 1 && tileComp->nQuantSteps < 1) || |
1935 | 26.5k | (qStyle == 2 && tileComp->nQuantSteps < 3 * tileComp->nDecompLevels + 1)) { |
1936 | 286 | error(errSyntaxError, getPos(), "Too few quant steps in JPX tile part"); |
1937 | 286 | return gFalse; |
1938 | 286 | } |
1939 | 26.5k | } |
1940 | | |
1941 | | //----- initialize the tile, precincts, and code-blocks |
1942 | 7.14k | if (tilePartIdx == 0) { |
1943 | 6.97k | tile = &img.tiles[tileIdx]; |
1944 | 6.97k | i = tileIdx / img.nXTiles; |
1945 | 6.97k | j = tileIdx % img.nXTiles; |
1946 | 6.97k | if ((tile->x0 = img.xTileOffset + j * img.xTileSize) < img.xOffset) { |
1947 | 2.80k | tile->x0 = img.xOffset; |
1948 | 2.80k | } |
1949 | 6.97k | if ((tile->y0 = img.yTileOffset + i * img.yTileSize) < img.yOffset) { |
1950 | 6.41k | tile->y0 = img.yOffset; |
1951 | 6.41k | } |
1952 | 6.97k | if ((tile->x1 = img.xTileOffset + (j + 1) * img.xTileSize) > img.xSize) { |
1953 | 6.58k | tile->x1 = img.xSize; |
1954 | 6.58k | } |
1955 | 6.97k | if ((tile->y1 = img.yTileOffset + (i + 1) * img.yTileSize) > img.ySize) { |
1956 | 6.68k | tile->y1 = img.ySize; |
1957 | 6.68k | } |
1958 | 6.97k | tile->comp = 0; |
1959 | 6.97k | tile->res = 0; |
1960 | 6.97k | tile->precinct = 0; |
1961 | 6.97k | tile->layer = 0; |
1962 | 6.97k | tile->done = gFalse; |
1963 | 6.97k | tile->maxNDecompLevels = 0; |
1964 | 6.97k | tile->maxNPrecincts = 0; |
1965 | 32.7k | for (comp = 0; comp < img.nComps; ++comp) { |
1966 | 25.7k | tileComp = &tile->tileComps[comp]; |
1967 | 25.7k | if (tileComp->nDecompLevels > tile->maxNDecompLevels) { |
1968 | 7.11k | tile->maxNDecompLevels = tileComp->nDecompLevels; |
1969 | 7.11k | } |
1970 | 25.7k | tileComp->x0 = jpxCeilDiv(tile->x0, tileComp->hSep); |
1971 | 25.7k | tileComp->y0 = jpxCeilDiv(tile->y0, tileComp->vSep); |
1972 | 25.7k | tileComp->x1 = jpxCeilDiv(tile->x1, tileComp->hSep); |
1973 | 25.7k | tileComp->y1 = jpxCeilDiv(tile->y1, tileComp->vSep); |
1974 | 25.7k | tileComp->x0r = jpxCeilDivPow2(tileComp->x0, reduction); |
1975 | 25.7k | tileComp->w = jpxCeilDivPow2(tileComp->x1, reduction) - tileComp->x0r; |
1976 | 25.7k | tileComp->y0r = jpxCeilDivPow2(tileComp->y0, reduction); |
1977 | 25.7k | tileComp->h = jpxCeilDivPow2(tileComp->y1, reduction) - tileComp->y0r; |
1978 | 25.7k | if (tileComp->w == 0 || tileComp->h == 0 || |
1979 | 25.7k | tileComp->w > INT_MAX / tileComp->h) { |
1980 | 0 | error(errSyntaxError, getPos(), |
1981 | 0 | "Invalid tile size or sample separation in JPX stream"); |
1982 | 0 | return gFalse; |
1983 | 0 | } |
1984 | 25.7k | tileComp->data = (int *)gmallocn(tileComp->w * tileComp->h, sizeof(int)); |
1985 | 25.7k | if (tileComp->x1 - tileComp->x0 > tileComp->y1 - tileComp->y0) { |
1986 | 14.0k | n = tileComp->x1 - tileComp->x0; |
1987 | 14.0k | } else { |
1988 | 11.7k | n = tileComp->y1 - tileComp->y0; |
1989 | 11.7k | } |
1990 | 25.7k | tileComp->buf = (int *)gmallocn(n + 8, sizeof(int)); |
1991 | 179k | for (r = 0; r <= tileComp->nDecompLevels; ++r) { |
1992 | 153k | resLevel = &tileComp->resLevels[r]; |
1993 | 153k | resLevel->x0 = jpxCeilDivPow2(tileComp->x0, |
1994 | 153k | tileComp->nDecompLevels - r); |
1995 | 153k | resLevel->y0 = jpxCeilDivPow2(tileComp->y0, |
1996 | 153k | tileComp->nDecompLevels - r); |
1997 | 153k | resLevel->x1 = jpxCeilDivPow2(tileComp->x1, |
1998 | 153k | tileComp->nDecompLevels - r); |
1999 | 153k | resLevel->y1 = jpxCeilDivPow2(tileComp->y1, |
2000 | 153k | tileComp->nDecompLevels - r); |
2001 | 153k | resLevel->codeBlockW = r == 0 ? resLevel->precinctWidth |
2002 | 153k | : resLevel->precinctWidth - 1; |
2003 | 153k | if (resLevel->codeBlockW > tileComp->codeBlockW) { |
2004 | 152k | resLevel->codeBlockW = tileComp->codeBlockW; |
2005 | 152k | } |
2006 | 153k | resLevel->cbW = 1 << resLevel->codeBlockW; |
2007 | 153k | resLevel->codeBlockH = r == 0 ? resLevel->precinctHeight |
2008 | 153k | : resLevel->precinctHeight - 1; |
2009 | 153k | if (resLevel->codeBlockH > tileComp->codeBlockH) { |
2010 | 152k | resLevel->codeBlockH = tileComp->codeBlockH; |
2011 | 152k | } |
2012 | 153k | resLevel->cbH = 1 << resLevel->codeBlockH; |
2013 | | // the JPEG 2000 spec says that packets for empty res levels |
2014 | | // should all be present in the codestream (B.6, B.9, B.10), |
2015 | | // but it appears that encoders drop packets if the res level |
2016 | | // AND the subbands are all completely empty |
2017 | 153k | resLevel->empty = resLevel->x0 == resLevel->x1 || |
2018 | 153k | resLevel->y0 == resLevel->y1; |
2019 | 153k | if (r == 0) { |
2020 | 25.7k | nSBs = 1; |
2021 | 25.7k | resLevel->bx0[0] = resLevel->x0; |
2022 | 25.7k | resLevel->by0[0] = resLevel->y0; |
2023 | 25.7k | resLevel->bx1[0] = resLevel->x1; |
2024 | 25.7k | resLevel->by1[0] = resLevel->y1; |
2025 | 25.7k | resLevel->empty = resLevel->empty && |
2026 | 25.7k | (resLevel->bx0[0] == resLevel->bx1[0] || |
2027 | 17.4k | resLevel->by0[0] == resLevel->by1[0]); |
2028 | 128k | } else { |
2029 | 128k | nSBs = 3; |
2030 | 128k | resLevel->bx0[0] = jpxCeilDivPow2(resLevel->x0 - 1, 1); |
2031 | 128k | resLevel->by0[0] = jpxCeilDivPow2(resLevel->y0, 1); |
2032 | 128k | resLevel->bx1[0] = jpxCeilDivPow2(resLevel->x1 - 1, 1); |
2033 | 128k | resLevel->by1[0] = jpxCeilDivPow2(resLevel->y1, 1); |
2034 | 128k | resLevel->bx0[1] = jpxCeilDivPow2(resLevel->x0, 1); |
2035 | 128k | resLevel->by0[1] = jpxCeilDivPow2(resLevel->y0 - 1, 1); |
2036 | 128k | resLevel->bx1[1] = jpxCeilDivPow2(resLevel->x1, 1); |
2037 | 128k | resLevel->by1[1] = jpxCeilDivPow2(resLevel->y1 - 1, 1); |
2038 | 128k | resLevel->bx0[2] = jpxCeilDivPow2(resLevel->x0 - 1, 1); |
2039 | 128k | resLevel->by0[2] = jpxCeilDivPow2(resLevel->y0 - 1, 1); |
2040 | 128k | resLevel->bx1[2] = jpxCeilDivPow2(resLevel->x1 - 1, 1); |
2041 | 128k | resLevel->by1[2] = jpxCeilDivPow2(resLevel->y1 - 1, 1); |
2042 | 128k | resLevel->empty = resLevel->empty && |
2043 | 128k | (resLevel->bx0[0] == resLevel->bx1[0] || |
2044 | 42.1k | resLevel->by0[0] == resLevel->by1[0]) && |
2045 | 128k | (resLevel->bx0[1] == resLevel->bx1[1] || |
2046 | 42.1k | resLevel->by0[1] == resLevel->by1[1]) && |
2047 | 128k | (resLevel->bx0[2] == resLevel->bx1[2] || |
2048 | 42.1k | resLevel->by0[2] == resLevel->by1[2]); |
2049 | 128k | } |
2050 | 153k | preCol0 = jpxFloorDivPow2(resLevel->x0, resLevel->precinctWidth); |
2051 | 153k | preCol1 = jpxCeilDivPow2(resLevel->x1, resLevel->precinctWidth); |
2052 | 153k | preRow0 = jpxFloorDivPow2(resLevel->y0, resLevel->precinctHeight); |
2053 | 153k | preRow1 = jpxCeilDivPow2(resLevel->y1, resLevel->precinctHeight); |
2054 | 153k | resLevel->nPrecincts = (preCol1 - preCol0) * (preRow1 - preRow0); |
2055 | 153k | resLevel->precincts = (JPXPrecinct *)gmallocn(resLevel->nPrecincts, |
2056 | 153k | sizeof(JPXPrecinct)); |
2057 | 153k | if (resLevel->nPrecincts > tile->maxNPrecincts) { |
2058 | 7.18k | tile->maxNPrecincts = resLevel->nPrecincts; |
2059 | 7.18k | } |
2060 | 312k | for (pre = 0; pre < resLevel->nPrecincts; ++pre) { |
2061 | 158k | resLevel->precincts[pre].subbands = NULL; |
2062 | 158k | } |
2063 | 153k | precinct = resLevel->precincts; |
2064 | 308k | for (preRow = preRow0; preRow < preRow1; ++preRow) { |
2065 | 313k | for (preCol = preCol0; preCol < preCol1; ++preCol) { |
2066 | 158k | precinct->subbands = |
2067 | 158k | (JPXSubband *)gmallocn(nSBs, sizeof(JPXSubband)); |
2068 | 583k | for (sb = 0; sb < nSBs; ++sb) { |
2069 | 424k | precinct->subbands[sb].inclusion = NULL; |
2070 | 424k | precinct->subbands[sb].zeroBitPlane = NULL; |
2071 | 424k | precinct->subbands[sb].cbs = NULL; |
2072 | 424k | } |
2073 | 583k | for (sb = 0; sb < nSBs; ++sb) { |
2074 | 424k | subband = &precinct->subbands[sb]; |
2075 | 424k | if (r == 0) { |
2076 | 26.0k | px0 = preCol << resLevel->precinctWidth; |
2077 | 26.0k | px1 = (preCol + 1) << resLevel->precinctWidth; |
2078 | 26.0k | py0 = preRow << resLevel->precinctHeight; |
2079 | 26.0k | py1 = (preRow + 1) << resLevel->precinctHeight; |
2080 | 398k | } else { |
2081 | 398k | px0 = preCol << (resLevel->precinctWidth - 1); |
2082 | 398k | px1 = (preCol + 1) << (resLevel->precinctWidth - 1); |
2083 | 398k | py0 = preRow << (resLevel->precinctHeight - 1); |
2084 | 398k | py1 = (preRow + 1) << (resLevel->precinctHeight - 1); |
2085 | 398k | } |
2086 | 424k | if (px0 < resLevel->bx0[sb]) { |
2087 | 81.9k | px0 = resLevel->bx0[sb]; |
2088 | 81.9k | } |
2089 | 424k | if (px1 > resLevel->bx1[sb]) { |
2090 | 410k | px1 = resLevel->bx1[sb]; |
2091 | 410k | } |
2092 | 424k | if (py0 < resLevel->by0[sb]) { |
2093 | 227k | py0 = resLevel->by0[sb]; |
2094 | 227k | } |
2095 | 424k | if (py1 > resLevel->by1[sb]) { |
2096 | 410k | py1 = resLevel->by1[sb]; |
2097 | 410k | } |
2098 | 424k | if (r == 0) { // (NL)LL |
2099 | 26.0k | sbCoeffs = tileComp->data; |
2100 | 398k | } else if (sb == 0) { // (NL-r+1)HL |
2101 | 132k | sbCoeffs = tileComp->data |
2102 | 132k | + resLevel->bx1[1] - resLevel->bx0[1]; |
2103 | 265k | } else if (sb == 1) { // (NL-r+1)LH |
2104 | 132k | sbCoeffs = tileComp->data |
2105 | 132k | + (resLevel->by1[0] - resLevel->by0[0]) * tileComp->w; |
2106 | 132k | } else { // (NL-r+1)HH |
2107 | 132k | sbCoeffs = tileComp->data |
2108 | 132k | + (resLevel->by1[0] - resLevel->by0[0]) * tileComp->w |
2109 | 132k | + (resLevel->bx1[1] - resLevel->bx0[1]); |
2110 | 132k | } |
2111 | 424k | cbCol0 = jpxFloorDivPow2(px0, resLevel->codeBlockW); |
2112 | 424k | cbCol1 = jpxCeilDivPow2(px1, resLevel->codeBlockW); |
2113 | 424k | cbRow0 = jpxFloorDivPow2(py0, resLevel->codeBlockH); |
2114 | 424k | cbRow1 = jpxCeilDivPow2(py1, resLevel->codeBlockH); |
2115 | 424k | subband->nXCBs = cbCol1 - cbCol0; |
2116 | 424k | subband->nYCBs = cbRow1 - cbRow0; |
2117 | 424k | n = subband->nXCBs > subband->nYCBs ? subband->nXCBs |
2118 | 424k | : subband->nYCBs; |
2119 | 424k | for (subband->maxTTLevel = 0, --n; |
2120 | 1.10M | n; |
2121 | 676k | ++subband->maxTTLevel, n >>= 1) ; |
2122 | 424k | n = 0; |
2123 | 1.52M | for (level = subband->maxTTLevel; level >= 0; --level) { |
2124 | 1.10M | nx = jpxCeilDivPow2(subband->nXCBs, level); |
2125 | 1.10M | ny = jpxCeilDivPow2(subband->nYCBs, level); |
2126 | 1.10M | n += nx * ny; |
2127 | 1.10M | } |
2128 | 424k | subband->inclusion = |
2129 | 424k | (JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode)); |
2130 | 424k | subband->zeroBitPlane = |
2131 | 424k | (JPXTagTreeNode *)gmallocn(n, sizeof(JPXTagTreeNode)); |
2132 | 3.71M | for (k = 0; k < n; ++k) { |
2133 | 3.28M | subband->inclusion[k].finished = gFalse; |
2134 | 3.28M | subband->inclusion[k].val = 0; |
2135 | 3.28M | subband->zeroBitPlane[k].finished = gFalse; |
2136 | 3.28M | subband->zeroBitPlane[k].val = 0; |
2137 | 3.28M | } |
2138 | 424k | subband->cbs = (JPXCodeBlock *)gmallocn(subband->nXCBs * |
2139 | 424k | subband->nYCBs, |
2140 | 424k | sizeof(JPXCodeBlock)); |
2141 | 2.53M | for (k = 0; k < subband->nXCBs * subband->nYCBs; ++k) { |
2142 | 2.10M | subband->cbs[k].dataLen = NULL; |
2143 | 2.10M | subband->cbs[k].touched = NULL; |
2144 | 2.10M | subband->cbs[k].arithDecoder = NULL; |
2145 | 2.10M | subband->cbs[k].stats = NULL; |
2146 | 2.10M | } |
2147 | 424k | cb = subband->cbs; |
2148 | 1.69M | for (cbY = cbRow0; cbY < cbRow1; ++cbY) { |
2149 | 3.37M | for (cbX = cbCol0; cbX < cbCol1; ++cbX) { |
2150 | 2.10M | cb->x0 = cbX << resLevel->codeBlockW; |
2151 | 2.10M | cb->x1 = cb->x0 + resLevel->cbW; |
2152 | 2.10M | if (cb->x0 < px0) { |
2153 | 194k | cb->x0 = px0; |
2154 | 194k | } |
2155 | 2.10M | if (cb->x1 > px1) { |
2156 | 700k | cb->x1 = px1; |
2157 | 700k | } |
2158 | 2.10M | cb->y0 = cbY << resLevel->codeBlockH; |
2159 | 2.10M | cb->y1 = cb->y0 + resLevel->cbH; |
2160 | 2.10M | if (cb->y0 < py0) { |
2161 | 527k | cb->y0 = py0; |
2162 | 527k | } |
2163 | 2.10M | if (cb->y1 > py1) { |
2164 | 710k | cb->y1 = py1; |
2165 | 710k | } |
2166 | 2.10M | cb->seen = gFalse; |
2167 | 2.10M | cb->lBlock = 3; |
2168 | 2.10M | cb->nextPass = jpxPassCleanup; |
2169 | 2.10M | cb->nZeroBitPlanes = 0; |
2170 | 2.10M | cb->dataLenSize = 1; |
2171 | 2.10M | cb->dataLen = (Guint *)gmalloc(sizeof(Guint)); |
2172 | 2.10M | if (r <= tileComp->nDecompLevels - reduction) { |
2173 | 2.10M | cb->coeffs = sbCoeffs |
2174 | 2.10M | + (cb->y0 - resLevel->by0[sb]) * tileComp->w |
2175 | 2.10M | + (cb->x0 - resLevel->bx0[sb]); |
2176 | 2.10M | cb->touched = (char *)gmalloc(1 << (resLevel->codeBlockW |
2177 | 2.10M | + resLevel->codeBlockH)); |
2178 | 2.10M | cb->len = 0; |
2179 | 22.8M | for (cbj = 0; cbj < cb->y1 - cb->y0; ++cbj) { |
2180 | 175M | for (cbi = 0; cbi < cb->x1 - cb->x0; ++cbi) { |
2181 | 154M | cb->coeffs[cbj * tileComp->w + cbi] = 0; |
2182 | 154M | } |
2183 | 20.7M | } |
2184 | 2.10M | memset(cb->touched, 0, |
2185 | 2.10M | ((size_t)1 << (resLevel->codeBlockW |
2186 | 2.10M | + resLevel->codeBlockH))); |
2187 | 2.10M | } else { |
2188 | 0 | cb->coeffs = NULL; |
2189 | 0 | cb->touched = NULL; |
2190 | 0 | cb->len = 0; |
2191 | 0 | } |
2192 | 2.10M | ++cb; |
2193 | 2.10M | } |
2194 | 1.27M | } |
2195 | 424k | } |
2196 | 158k | ++precinct; |
2197 | 158k | } |
2198 | 154k | } |
2199 | 153k | } |
2200 | 25.7k | } |
2201 | 6.97k | tile->init = gTrue; |
2202 | 6.97k | } |
2203 | | |
2204 | 7.14k | return readTilePartData(tileIdx, tilePartLen, tilePartToEOC); |
2205 | 7.14k | } |
2206 | | |
2207 | | GBool JPXStream::readTilePartData(Guint tileIdx, |
2208 | 7.14k | Guint tilePartLen, GBool tilePartToEOC) { |
2209 | 7.14k | JPXTile *tile; |
2210 | 7.14k | JPXTileComp *tileComp; |
2211 | 7.14k | JPXResLevel *resLevel; |
2212 | 7.14k | JPXPrecinct *precinct; |
2213 | 7.14k | JPXSubband *subband; |
2214 | 7.14k | JPXCodeBlock *cb; |
2215 | 7.14k | Guint ttVal; |
2216 | 7.14k | Guint bits, cbX, cbY, nx, ny, i, j, n, sb; |
2217 | 7.14k | int level; |
2218 | | |
2219 | 7.14k | tile = &img.tiles[tileIdx]; |
2220 | | |
2221 | | // read all packets from this tile-part |
2222 | 1.17M | while (1) { |
2223 | | |
2224 | | // if the tile is finished, skip any remaining data |
2225 | 1.17M | if (tile->done) { |
2226 | 2.74k | bufStr->discardChars(tilePartLen); |
2227 | 2.74k | return gTrue; |
2228 | 2.74k | } |
2229 | | |
2230 | 1.17M | if (tilePartToEOC) { |
2231 | | //~ peek for an EOC marker |
2232 | 51.2k | cover(93); |
2233 | 1.12M | } else if (tilePartLen == 0) { |
2234 | 961 | break; |
2235 | 961 | } |
2236 | | |
2237 | 1.17M | tileComp = &tile->tileComps[tile->comp]; |
2238 | 1.17M | resLevel = &tileComp->resLevels[tile->res]; |
2239 | 1.17M | precinct = &resLevel->precincts[tile->precinct]; |
2240 | | |
2241 | 1.17M | if (resLevel->empty) { |
2242 | 550k | goto nextPacket; |
2243 | 550k | } |
2244 | | |
2245 | | //----- packet header |
2246 | | |
2247 | | // setup |
2248 | 622k | startBitBuf(tilePartLen); |
2249 | 622k | if (tileComp->style & 0x02) { |
2250 | 110k | skipSOP(); |
2251 | 110k | } |
2252 | | |
2253 | | // zero-length flag |
2254 | 622k | if (!readBits(1, &bits)) { |
2255 | 1.21k | goto err; |
2256 | 1.21k | } |
2257 | 621k | if (!bits) { |
2258 | | // packet is empty -- clear all code-block inclusion flags |
2259 | 577k | cover(45); |
2260 | 2.12M | for (sb = 0; sb < (Guint)(tile->res == 0 ? 1 : 3); ++sb) { |
2261 | 1.54M | subband = &precinct->subbands[sb]; |
2262 | 5.80M | for (cbY = 0; cbY < subband->nYCBs; ++cbY) { |
2263 | 17.5M | for (cbX = 0; cbX < subband->nXCBs; ++cbX) { |
2264 | 13.2M | cb = &subband->cbs[cbY * subband->nXCBs + cbX]; |
2265 | 13.2M | cb->included = gFalse; |
2266 | 13.2M | } |
2267 | 4.25M | } |
2268 | 1.54M | } |
2269 | 577k | } else { |
2270 | | |
2271 | 159k | for (sb = 0; sb < (Guint)(tile->res == 0 ? 1 : 3); ++sb) { |
2272 | 117k | subband = &precinct->subbands[sb]; |
2273 | 537k | for (cbY = 0; cbY < subband->nYCBs; ++cbY) { |
2274 | 1.14M | for (cbX = 0; cbX < subband->nXCBs; ++cbX) { |
2275 | 721k | cb = &subband->cbs[cbY * subband->nXCBs + cbX]; |
2276 | | |
2277 | | // skip code-blocks with no coefficients |
2278 | 721k | if (cb->x0 >= cb->x1 || cb->y0 >= cb->y1) { |
2279 | 21.0k | cover(46); |
2280 | 21.0k | cb->included = gFalse; |
2281 | 21.0k | continue; |
2282 | 21.0k | } |
2283 | | |
2284 | | // code-block inclusion |
2285 | 700k | if (cb->seen) { |
2286 | 92.1k | cover(47); |
2287 | 92.1k | if (!readBits(1, &cb->included)) { |
2288 | 136 | goto err; |
2289 | 136 | } |
2290 | 608k | } else { |
2291 | 608k | cover(48); |
2292 | 608k | ttVal = 0; |
2293 | 608k | i = 0; |
2294 | 2.22M | for (level = subband->maxTTLevel; level >= 0; --level) { |
2295 | 1.99M | nx = jpxCeilDivPow2(subband->nXCBs, level); |
2296 | 1.99M | ny = jpxCeilDivPow2(subband->nYCBs, level); |
2297 | 1.99M | j = i + (cbY >> level) * nx + (cbX >> level); |
2298 | 1.99M | if (!subband->inclusion[j].finished && |
2299 | 1.99M | !subband->inclusion[j].val) { |
2300 | 398k | subband->inclusion[j].val = ttVal; |
2301 | 1.60M | } else { |
2302 | 1.60M | ttVal = subband->inclusion[j].val; |
2303 | 1.60M | } |
2304 | 2.83M | while (!subband->inclusion[j].finished && |
2305 | 2.83M | ttVal <= tile->layer) { |
2306 | 830k | if (!readBits(1, &bits)) { |
2307 | 0 | goto err; |
2308 | 0 | } |
2309 | 830k | if (bits == 1) { |
2310 | 363k | subband->inclusion[j].finished = gTrue; |
2311 | 466k | } else { |
2312 | 466k | ++ttVal; |
2313 | 466k | } |
2314 | 830k | } |
2315 | 1.99M | subband->inclusion[j].val = ttVal; |
2316 | 1.99M | if (ttVal > tile->layer) { |
2317 | 386k | break; |
2318 | 386k | } |
2319 | 1.61M | i += nx * ny; |
2320 | 1.61M | } |
2321 | 608k | cb->included = level < 0; |
2322 | 608k | } |
2323 | | |
2324 | 700k | if (cb->included) { |
2325 | 257k | cover(49); |
2326 | | |
2327 | | // zero bit-plane count |
2328 | 257k | if (!cb->seen) { |
2329 | 221k | cover(50); |
2330 | 221k | ttVal = 0; |
2331 | 221k | i = 0; |
2332 | 1.16M | for (level = subband->maxTTLevel; level >= 0; --level) { |
2333 | 947k | nx = jpxCeilDivPow2(subband->nXCBs, level); |
2334 | 947k | ny = jpxCeilDivPow2(subband->nYCBs, level); |
2335 | 947k | j = i + (cbY >> level) * nx + (cbX >> level); |
2336 | 947k | if (!subband->zeroBitPlane[j].finished && |
2337 | 947k | !subband->zeroBitPlane[j].val) { |
2338 | 355k | subband->zeroBitPlane[j].val = ttVal; |
2339 | 591k | } else { |
2340 | 591k | ttVal = subband->zeroBitPlane[j].val; |
2341 | 591k | } |
2342 | 1.66M | while (!subband->zeroBitPlane[j].finished) { |
2343 | 721k | if (!readBits(1, &bits)) { |
2344 | 5 | goto err; |
2345 | 5 | } |
2346 | 721k | if (bits == 1) { |
2347 | 355k | subband->zeroBitPlane[j].finished = gTrue; |
2348 | 365k | } else { |
2349 | 365k | ++ttVal; |
2350 | 365k | } |
2351 | 721k | } |
2352 | 947k | subband->zeroBitPlane[j].val = ttVal; |
2353 | 947k | i += nx * ny; |
2354 | 947k | } |
2355 | 221k | cb->nZeroBitPlanes = ttVal; |
2356 | 221k | } |
2357 | | |
2358 | | // number of coding passes |
2359 | 257k | if (!readBits(1, &bits)) { |
2360 | 144 | goto err; |
2361 | 144 | } |
2362 | 257k | if (bits == 0) { |
2363 | 150k | cover(51); |
2364 | 150k | cb->nCodingPasses = 1; |
2365 | 150k | } else { |
2366 | 106k | if (!readBits(1, &bits)) { |
2367 | 398 | goto err; |
2368 | 398 | } |
2369 | 105k | if (bits == 0) { |
2370 | 54.1k | cover(52); |
2371 | 54.1k | cb->nCodingPasses = 2; |
2372 | 54.1k | } else { |
2373 | 51.8k | cover(53); |
2374 | 51.8k | if (!readBits(2, &bits)) { |
2375 | 140 | goto err; |
2376 | 140 | } |
2377 | 51.6k | if (bits < 3) { |
2378 | 36.2k | cover(54); |
2379 | 36.2k | cb->nCodingPasses = 3 + bits; |
2380 | 36.2k | } else { |
2381 | 15.4k | cover(55); |
2382 | 15.4k | if (!readBits(5, &bits)) { |
2383 | 17 | goto err; |
2384 | 17 | } |
2385 | 15.4k | if (bits < 31) { |
2386 | 12.0k | cover(56); |
2387 | 12.0k | cb->nCodingPasses = 6 + bits; |
2388 | 12.0k | } else { |
2389 | 3.37k | cover(57); |
2390 | 3.37k | if (!readBits(7, &bits)) { |
2391 | 395 | goto err; |
2392 | 395 | } |
2393 | 2.97k | cb->nCodingPasses = 37 + bits; |
2394 | 2.97k | } |
2395 | 15.4k | } |
2396 | 51.6k | } |
2397 | 105k | } |
2398 | | |
2399 | | // update Lblock |
2400 | 554k | while (1) { |
2401 | 554k | if (!readBits(1, &bits)) { |
2402 | 201 | goto err; |
2403 | 201 | } |
2404 | 554k | if (!bits) { |
2405 | 255k | break; |
2406 | 255k | } |
2407 | 298k | ++cb->lBlock; |
2408 | 298k | } |
2409 | | |
2410 | | // one codeword segment for each of the coding passes |
2411 | 255k | if (tileComp->codeBlockStyle & 0x04) { |
2412 | 212k | if (cb->nCodingPasses > cb->dataLenSize) { |
2413 | 79.8k | cb->dataLenSize = cb->nCodingPasses; |
2414 | 79.8k | cb->dataLen = (Guint *)greallocn(cb->dataLen, |
2415 | 79.8k | cb->dataLenSize, |
2416 | 79.8k | sizeof(Guint)); |
2417 | 79.8k | } |
2418 | | |
2419 | | // read the lengths |
2420 | 876k | for (i = 0; i < cb->nCodingPasses; ++i) { |
2421 | 664k | if (!readBits(cb->lBlock, &cb->dataLen[i])) { |
2422 | 602 | goto err; |
2423 | 602 | } |
2424 | 664k | } |
2425 | | |
2426 | | // one codeword segment for all of the coding passes |
2427 | 212k | } else { |
2428 | | |
2429 | | // read the length |
2430 | 43.2k | for (n = cb->lBlock, i = cb->nCodingPasses >> 1; |
2431 | 83.6k | i; |
2432 | 43.2k | ++n, i >>= 1) ; |
2433 | 43.2k | if (!readBits(n, &cb->dataLen[0])) { |
2434 | 193 | goto err; |
2435 | 193 | } |
2436 | 43.2k | } |
2437 | 255k | } |
2438 | 700k | } |
2439 | 422k | } |
2440 | 117k | } |
2441 | 44.0k | } |
2442 | 619k | if (tileComp->style & 0x04) { |
2443 | 134k | skipEPH(); |
2444 | 134k | } |
2445 | 619k | tilePartLen = finishBitBuf(); |
2446 | | |
2447 | | //----- packet data |
2448 | | |
2449 | 2.28M | for (sb = 0; sb < (Guint)(tile->res == 0 ? 1 : 3); ++sb) { |
2450 | 1.66M | subband = &precinct->subbands[sb]; |
2451 | 6.32M | for (cbY = 0; cbY < subband->nYCBs; ++cbY) { |
2452 | 18.6M | for (cbX = 0; cbX < subband->nXCBs; ++cbX) { |
2453 | 13.9M | cb = &subband->cbs[cbY * subband->nXCBs + cbX]; |
2454 | 13.9M | if (cb->included) { |
2455 | 236k | if (!readCodeBlockData(tileComp, resLevel, precinct, subband, |
2456 | 236k | tile->res, sb, cb)) { |
2457 | 0 | return gFalse; |
2458 | 0 | } |
2459 | 236k | if (tileComp->codeBlockStyle & 0x04) { |
2460 | 773k | for (i = 0; i < cb->nCodingPasses; ++i) { |
2461 | 567k | tilePartLen -= cb->dataLen[i]; |
2462 | 567k | } |
2463 | 205k | } else { |
2464 | 30.2k | tilePartLen -= cb->dataLen[0]; |
2465 | 30.2k | } |
2466 | 236k | cb->seen = gTrue; |
2467 | 236k | } |
2468 | 13.9M | } |
2469 | 4.66M | } |
2470 | 1.66M | } |
2471 | | |
2472 | | //----- next packet |
2473 | | |
2474 | 1.17M | nextPacket: |
2475 | 1.17M | switch (tile->progOrder) { |
2476 | 777k | case 0: // layer, resolution level, component, precinct |
2477 | 777k | cover(58); |
2478 | 891k | do { |
2479 | 891k | if (++tile->precinct == tile->maxNPrecincts) { |
2480 | 805k | tile->precinct = 0; |
2481 | 805k | if (++tile->comp == img.nComps) { |
2482 | 288k | tile->comp = 0; |
2483 | 288k | if (++tile->res == tile->maxNDecompLevels + 1) { |
2484 | 53.4k | tile->res = 0; |
2485 | 53.4k | if (++tile->layer == tile->nLayers) { |
2486 | 275 | tile->layer = 0; |
2487 | 275 | tile->done = gTrue; |
2488 | 275 | } |
2489 | 53.4k | } |
2490 | 288k | } |
2491 | 805k | } |
2492 | 891k | } while (!tile->done && |
2493 | 891k | (tile->res > tile->tileComps[tile->comp].nDecompLevels || |
2494 | 891k | tile->precinct >= tile->tileComps[tile->comp] |
2495 | 887k | .resLevels[tile->res].nPrecincts)); |
2496 | 777k | break; |
2497 | 202k | case 1: // resolution level, layer, component, precinct |
2498 | 202k | cover(59); |
2499 | 233k | do { |
2500 | 233k | if (++tile->precinct == tile->maxNPrecincts) { |
2501 | 199k | tile->precinct = 0; |
2502 | 199k | if (++tile->comp == img.nComps) { |
2503 | 73.3k | tile->comp = 0; |
2504 | 73.3k | if (++tile->layer == tile->nLayers) { |
2505 | 9.38k | tile->layer = 0; |
2506 | 9.38k | if (++tile->res == tile->maxNDecompLevels + 1) { |
2507 | 1.56k | tile->res = 0; |
2508 | 1.56k | tile->done = gTrue; |
2509 | 1.56k | } |
2510 | 9.38k | } |
2511 | 73.3k | } |
2512 | 199k | } |
2513 | 233k | } while (!tile->done && |
2514 | 233k | (tile->res > tile->tileComps[tile->comp].nDecompLevels || |
2515 | 231k | tile->precinct >= tile->tileComps[tile->comp] |
2516 | 231k | .resLevels[tile->res].nPrecincts)); |
2517 | 202k | break; |
2518 | 18.5k | case 2: // resolution level, precinct, component, layer |
2519 | 18.5k | cover(60); |
2520 | | //~ this is incorrect if there are subsampled components (?) |
2521 | 22.9k | do { |
2522 | 22.9k | if (++tile->layer == tile->nLayers) { |
2523 | 3.92k | tile->layer = 0; |
2524 | 3.92k | if (++tile->comp == img.nComps) { |
2525 | 1.38k | tile->comp = 0; |
2526 | 1.38k | if (++tile->precinct == tile->maxNPrecincts) { |
2527 | 1.32k | tile->precinct = 0; |
2528 | 1.32k | if (++tile->res == tile->maxNDecompLevels + 1) { |
2529 | 166 | tile->res = 0; |
2530 | 166 | tile->done = gTrue; |
2531 | 166 | } |
2532 | 1.32k | } |
2533 | 1.38k | } |
2534 | 3.92k | } |
2535 | 22.9k | } while (!tile->done && |
2536 | 22.9k | (tile->res > tile->tileComps[tile->comp].nDecompLevels || |
2537 | 22.8k | tile->precinct >= tile->tileComps[tile->comp] |
2538 | 18.6k | .resLevels[tile->res].nPrecincts)); |
2539 | 18.5k | break; |
2540 | 99.3k | case 3: // precinct, component, resolution level, layer |
2541 | 99.3k | cover(61); |
2542 | | //~ this is incorrect if there are subsampled components (?) |
2543 | 99.3k | do { |
2544 | 99.3k | if (++tile->layer == tile->nLayers) { |
2545 | 14.1k | tile->layer = 0; |
2546 | 14.1k | if (++tile->res == tile->maxNDecompLevels + 1) { |
2547 | 1.81k | tile->res = 0; |
2548 | 1.81k | if (++tile->comp == img.nComps) { |
2549 | 353 | tile->comp = 0; |
2550 | 353 | if (++tile->precinct == tile->maxNPrecincts) { |
2551 | 347 | tile->precinct = 0; |
2552 | 347 | tile->done = gTrue; |
2553 | 347 | } |
2554 | 353 | } |
2555 | 1.81k | } |
2556 | 14.1k | } |
2557 | 99.3k | } while (!tile->done && |
2558 | 99.3k | (tile->res > tile->tileComps[tile->comp].nDecompLevels || |
2559 | 99.0k | tile->precinct >= tile->tileComps[tile->comp] |
2560 | 99.0k | .resLevels[tile->res].nPrecincts)); |
2561 | 99.3k | break; |
2562 | 45.6k | case 4: // component, precinct, resolution level, layer |
2563 | 45.6k | cover(62); |
2564 | 52.3k | do { |
2565 | 52.3k | if (++tile->layer == tile->nLayers) { |
2566 | 4.66k | tile->layer = 0; |
2567 | 4.66k | if (++tile->res == tile->maxNDecompLevels + 1) { |
2568 | 762 | tile->res = 0; |
2569 | 762 | if (++tile->precinct == tile->maxNPrecincts) { |
2570 | 692 | tile->precinct = 0; |
2571 | 692 | if (++tile->comp == img.nComps) { |
2572 | 219 | tile->comp = 0; |
2573 | 219 | tile->done = gTrue; |
2574 | 219 | } |
2575 | 692 | } |
2576 | 762 | } |
2577 | 4.66k | } |
2578 | 52.3k | } while (!tile->done && |
2579 | 52.3k | (tile->res > tile->tileComps[tile->comp].nDecompLevels || |
2580 | 52.1k | tile->precinct >= tile->tileComps[tile->comp] |
2581 | 52.1k | .resLevels[tile->res].nPrecincts)); |
2582 | 45.6k | break; |
2583 | 1.17M | } |
2584 | 1.17M | } |
2585 | | |
2586 | 961 | return gTrue; |
2587 | | |
2588 | 3.44k | err: |
2589 | 3.44k | error(errSyntaxError, getPos(), "Error in JPX stream"); |
2590 | 3.44k | return gFalse; |
2591 | 7.14k | } |
2592 | | |
2593 | | GBool JPXStream::readCodeBlockData(JPXTileComp *tileComp, |
2594 | | JPXResLevel *resLevel, |
2595 | | JPXPrecinct *precinct, |
2596 | | JPXSubband *subband, |
2597 | | Guint res, Guint sb, |
2598 | 236k | JPXCodeBlock *cb) { |
2599 | 236k | int *coeff0, *coeff1, *coeff; |
2600 | 236k | char *touched0, *touched1, *touched; |
2601 | 236k | Guint horiz, vert, diag, all, cx, xorBit; |
2602 | 236k | int horizSign, vertSign, bit; |
2603 | 236k | int segSym; |
2604 | 236k | Guint n, i, x, y0, y1; |
2605 | | |
2606 | 236k | if (res > tileComp->nDecompLevels - reduction) { |
2607 | | // skip the codeblock data |
2608 | 0 | if (tileComp->codeBlockStyle & 0x04) { |
2609 | 0 | n = 0; |
2610 | 0 | for (i = 0; i < cb->nCodingPasses; ++i) { |
2611 | 0 | n += cb->dataLen[i]; |
2612 | 0 | } |
2613 | 0 | } else { |
2614 | 0 | n = cb->dataLen[0]; |
2615 | 0 | } |
2616 | 0 | bufStr->discardChars(n); |
2617 | 0 | return gTrue; |
2618 | 0 | } |
2619 | | |
2620 | 236k | if (cb->arithDecoder) { |
2621 | 32.1k | cover(63); |
2622 | 32.1k | if (tileComp->codeBlockStyle & 0x04) { |
2623 | 23.8k | cb->arithDecoder->setStream(bufStr, cb->dataLen[0]); |
2624 | 23.8k | cb->arithDecoder->start(); |
2625 | 23.8k | } else { |
2626 | 8.23k | cb->arithDecoder->restart(cb->dataLen[0]); |
2627 | 8.23k | } |
2628 | 204k | } else { |
2629 | 204k | cover(64); |
2630 | 204k | cb->arithDecoder = new JArithmeticDecoder(); |
2631 | 204k | cb->arithDecoder->setStream(bufStr, cb->dataLen[0]); |
2632 | 204k | cb->arithDecoder->start(); |
2633 | 204k | cb->stats = new JArithmeticDecoderStats(jpxNContexts); |
2634 | 204k | cb->stats->setEntry(jpxContextSigProp, 4, 0); |
2635 | 204k | cb->stats->setEntry(jpxContextRunLength, 3, 0); |
2636 | 204k | cb->stats->setEntry(jpxContextUniform, 46, 0); |
2637 | 204k | } |
2638 | | |
2639 | 1.04M | for (i = 0; i < cb->nCodingPasses; ++i) { |
2640 | 806k | if ((tileComp->codeBlockStyle & 0x04) && i > 0) { |
2641 | 361k | cb->arithDecoder->setStream(bufStr, cb->dataLen[i]); |
2642 | 361k | cb->arithDecoder->start(); |
2643 | 361k | } |
2644 | | |
2645 | 806k | switch (cb->nextPass) { |
2646 | | |
2647 | | //----- significance propagation pass |
2648 | 246k | case jpxPassSigProp: |
2649 | 246k | cover(65); |
2650 | 246k | for (y0 = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched; |
2651 | 934k | y0 < cb->y1; |
2652 | 687k | y0 += 4, coeff0 += 4 * tileComp->w, |
2653 | 687k | touched0 += 4 << resLevel->codeBlockW) { |
2654 | 687k | for (x = cb->x0, coeff1 = coeff0, touched1 = touched0; |
2655 | 4.05M | x < cb->x1; |
2656 | 3.36M | ++x, ++coeff1, ++touched1) { |
2657 | 3.36M | for (y1 = 0, coeff = coeff1, touched = touched1; |
2658 | 15.6M | y1 < 4 && y0+y1 < cb->y1; |
2659 | 12.2M | ++y1, coeff += tileComp->w, touched += resLevel->cbW) { |
2660 | 12.2M | if (!*coeff) { |
2661 | 6.59M | horiz = vert = diag = 0; |
2662 | 6.59M | horizSign = vertSign = 2; |
2663 | 6.59M | if (x > cb->x0) { |
2664 | 5.23M | if (coeff[-1]) { |
2665 | 813k | ++horiz; |
2666 | 813k | horizSign += coeff[-1] < 0 ? -1 : 1; |
2667 | 813k | } |
2668 | 5.23M | if (y0+y1 > cb->y0) { |
2669 | 4.58M | diag += coeff[-(int)tileComp->w - 1] ? 1 : 0; |
2670 | 4.58M | } |
2671 | 5.23M | if (y0+y1 < cb->y1 - 1 && |
2672 | 5.23M | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2673 | 4.44M | diag += coeff[tileComp->w - 1] ? 1 : 0; |
2674 | 4.44M | } |
2675 | 5.23M | } |
2676 | 6.59M | if (x < cb->x1 - 1) { |
2677 | 5.29M | if (coeff[1]) { |
2678 | 491k | ++horiz; |
2679 | 491k | horizSign += coeff[1] < 0 ? -1 : 1; |
2680 | 491k | } |
2681 | 5.29M | if (y0+y1 > cb->y0) { |
2682 | 4.63M | diag += coeff[-(int)tileComp->w + 1] ? 1 : 0; |
2683 | 4.63M | } |
2684 | 5.29M | if (y0+y1 < cb->y1 - 1 && |
2685 | 5.29M | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2686 | 4.50M | diag += coeff[tileComp->w + 1] ? 1 : 0; |
2687 | 4.50M | } |
2688 | 5.29M | } |
2689 | 6.59M | if (y0+y1 > cb->y0) { |
2690 | 5.79M | if (coeff[-(int)tileComp->w]) { |
2691 | 895k | ++vert; |
2692 | 895k | vertSign += coeff[-(int)tileComp->w] < 0 ? -1 : 1; |
2693 | 895k | } |
2694 | 5.79M | } |
2695 | 6.59M | if (y0+y1 < cb->y1 - 1 && |
2696 | 6.59M | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2697 | 5.55M | if (coeff[tileComp->w]) { |
2698 | 464k | ++vert; |
2699 | 464k | vertSign += coeff[tileComp->w] < 0 ? -1 : 1; |
2700 | 464k | } |
2701 | 5.55M | } |
2702 | 6.59M | cx = sigPropContext[horiz][vert][diag][res == 0 ? 1 : sb]; |
2703 | 6.59M | if (cx != 0) { |
2704 | 1.56M | if (cb->arithDecoder->decodeBit(cx, cb->stats)) { |
2705 | 740k | cx = signContext[horizSign][vertSign][0]; |
2706 | 740k | xorBit = signContext[horizSign][vertSign][1]; |
2707 | 740k | if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) { |
2708 | 378k | *coeff = -1; |
2709 | 378k | } else { |
2710 | 361k | *coeff = 1; |
2711 | 361k | } |
2712 | 740k | } |
2713 | 1.56M | *touched = 1; |
2714 | 1.56M | } |
2715 | 6.59M | } |
2716 | 12.2M | } |
2717 | 3.36M | } |
2718 | 687k | } |
2719 | 246k | ++cb->nextPass; |
2720 | 246k | break; |
2721 | | |
2722 | | //----- magnitude refinement pass |
2723 | 192k | case jpxPassMagRef: |
2724 | 192k | cover(66); |
2725 | 192k | for (y0 = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched; |
2726 | 759k | y0 < cb->y1; |
2727 | 567k | y0 += 4, coeff0 += 4 * tileComp->w, |
2728 | 567k | touched0 += 4 << resLevel->codeBlockW) { |
2729 | 567k | for (x = cb->x0, coeff1 = coeff0, touched1 = touched0; |
2730 | 3.20M | x < cb->x1; |
2731 | 2.63M | ++x, ++coeff1, ++touched1) { |
2732 | 2.63M | for (y1 = 0, coeff = coeff1, touched = touched1; |
2733 | 12.3M | y1 < 4 && y0+y1 < cb->y1; |
2734 | 9.68M | ++y1, coeff += tileComp->w, touched += resLevel->cbW) { |
2735 | 9.68M | if (*coeff && !*touched) { |
2736 | 5.14M | if (*coeff == 1 || *coeff == -1) { |
2737 | 725k | all = 0; |
2738 | 725k | if (x > cb->x0) { |
2739 | 600k | all += coeff[-1] ? 1 : 0; |
2740 | 600k | if (y0+y1 > cb->y0) { |
2741 | 563k | all += coeff[-(int)tileComp->w - 1] ? 1 : 0; |
2742 | 563k | } |
2743 | 600k | if (y0+y1 < cb->y1 - 1 && |
2744 | 600k | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2745 | 535k | all += coeff[tileComp->w - 1] ? 1 : 0; |
2746 | 535k | } |
2747 | 600k | } |
2748 | 725k | if (x < cb->x1 - 1) { |
2749 | 574k | all += coeff[1] ? 1 : 0; |
2750 | 574k | if (y0+y1 > cb->y0) { |
2751 | 539k | all += coeff[-(int)tileComp->w + 1] ? 1 : 0; |
2752 | 539k | } |
2753 | 574k | if (y0+y1 < cb->y1 - 1 && |
2754 | 574k | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2755 | 512k | all += coeff[tileComp->w + 1] ? 1 : 0; |
2756 | 512k | } |
2757 | 574k | } |
2758 | 725k | if (y0+y1 > cb->y0) { |
2759 | 679k | all += coeff[-(int)tileComp->w] ? 1 : 0; |
2760 | 679k | } |
2761 | 725k | if (y0+y1 < cb->y1 - 1 && |
2762 | 725k | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2763 | 643k | all += coeff[tileComp->w] ? 1 : 0; |
2764 | 643k | } |
2765 | 725k | cx = all ? 15 : 14; |
2766 | 4.41M | } else { |
2767 | 4.41M | cx = 16; |
2768 | 4.41M | } |
2769 | 5.14M | bit = cb->arithDecoder->decodeBit(cx, cb->stats); |
2770 | 5.14M | if (*coeff < 0) { |
2771 | 2.57M | *coeff = (*coeff << 1) - bit; |
2772 | 2.57M | } else { |
2773 | 2.56M | *coeff = (*coeff << 1) + bit; |
2774 | 2.56M | } |
2775 | 5.14M | *touched = 1; |
2776 | 5.14M | } |
2777 | 9.68M | } |
2778 | 2.63M | } |
2779 | 567k | } |
2780 | 192k | ++cb->nextPass; |
2781 | 192k | break; |
2782 | | |
2783 | | //----- cleanup pass |
2784 | 367k | case jpxPassCleanup: |
2785 | 367k | cover(67); |
2786 | 367k | for (y0 = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched; |
2787 | 1.28M | y0 < cb->y1; |
2788 | 920k | y0 += 4, coeff0 += 4 * tileComp->w, |
2789 | 920k | touched0 += 4 << resLevel->codeBlockW) { |
2790 | 920k | for (x = cb->x0, coeff1 = coeff0, touched1 = touched0; |
2791 | 5.84M | x < cb->x1; |
2792 | 4.92M | ++x, ++coeff1, ++touched1) { |
2793 | 4.92M | y1 = 0; |
2794 | 4.92M | if (y0 + 3 < cb->y1 && |
2795 | 4.92M | !(*touched1) && |
2796 | 4.92M | !(touched1[resLevel->cbW]) && |
2797 | 4.92M | !(touched1[2 * resLevel->cbW]) && |
2798 | 4.92M | !(touched1[3 * resLevel->cbW]) && |
2799 | 4.92M | (x == cb->x0 || y0 == cb->y0 || |
2800 | 2.78M | !coeff1[-(int)tileComp->w - 1]) && |
2801 | 4.92M | (y0 == cb->y0 || |
2802 | 2.63M | !coeff1[-(int)tileComp->w]) && |
2803 | 4.92M | (x == cb->x1 - 1 || y0 == cb->y0 || |
2804 | 2.54M | !coeff1[-(int)tileComp->w + 1]) && |
2805 | 4.92M | (x == cb->x0 || |
2806 | 2.50M | (!coeff1[-1] && |
2807 | 2.04M | !coeff1[tileComp->w - 1] && |
2808 | 2.04M | !coeff1[2 * tileComp->w - 1] && |
2809 | 2.04M | !coeff1[3 * tileComp->w - 1])) && |
2810 | 4.92M | (x == cb->x1 - 1 || |
2811 | 2.25M | (!coeff1[1] && |
2812 | 1.85M | !coeff1[tileComp->w + 1] && |
2813 | 1.85M | !coeff1[2 * tileComp->w + 1] && |
2814 | 1.85M | !coeff1[3 * tileComp->w + 1])) && |
2815 | 4.92M | ((tileComp->codeBlockStyle & 0x08) || |
2816 | 2.24M | ((x == cb->x0 || y0+4 == cb->y1 || |
2817 | 1.96M | !coeff1[4 * tileComp->w - 1]) && |
2818 | 1.96M | (y0+4 == cb->y1 || |
2819 | 1.96M | !coeff1[4 * tileComp->w]) && |
2820 | 1.96M | (x == cb->x1 - 1 || y0+4 == cb->y1 || |
2821 | 2.24M | !coeff1[4 * tileComp->w + 1])))) { |
2822 | 2.24M | if (cb->arithDecoder->decodeBit(jpxContextRunLength, cb->stats)) { |
2823 | 92.0k | y1 = cb->arithDecoder->decodeBit(jpxContextUniform, cb->stats); |
2824 | 92.0k | y1 = (y1 << 1) | |
2825 | 92.0k | cb->arithDecoder->decodeBit(jpxContextUniform, cb->stats); |
2826 | 92.0k | coeff = &coeff1[y1 * tileComp->w]; |
2827 | 92.0k | cx = signContext[2][2][0]; |
2828 | 92.0k | xorBit = signContext[2][2][1]; |
2829 | 92.0k | if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) { |
2830 | 64.7k | *coeff = -1; |
2831 | 64.7k | } else { |
2832 | 27.3k | *coeff = 1; |
2833 | 27.3k | } |
2834 | 92.0k | ++y1; |
2835 | 2.15M | } else { |
2836 | 2.15M | y1 = 4; |
2837 | 2.15M | } |
2838 | 2.24M | } |
2839 | 4.92M | for (coeff = &coeff1[y1 * tileComp->w], |
2840 | 4.92M | touched = &touched1[y1 << resLevel->codeBlockW]; |
2841 | 14.0M | y1 < 4 && y0 + y1 < cb->y1; |
2842 | 9.09M | ++y1, coeff += tileComp->w, touched += resLevel->cbW) { |
2843 | 9.09M | if (!*touched) { |
2844 | 3.49M | horiz = vert = diag = 0; |
2845 | 3.49M | horizSign = vertSign = 2; |
2846 | 3.49M | if (x > cb->x0) { |
2847 | 3.07M | if (coeff[-1]) { |
2848 | 958k | ++horiz; |
2849 | 958k | horizSign += coeff[-1] < 0 ? -1 : 1; |
2850 | 958k | } |
2851 | 3.07M | if (y0+y1 > cb->y0) { |
2852 | 2.59M | diag += coeff[-(int)tileComp->w - 1] ? 1 : 0; |
2853 | 2.59M | } |
2854 | 3.07M | if (y0+y1 < cb->y1 - 1 && |
2855 | 3.07M | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2856 | 2.36M | diag += coeff[tileComp->w - 1] ? 1 : 0; |
2857 | 2.36M | } |
2858 | 3.07M | } |
2859 | 3.49M | if (x < cb->x1 - 1) { |
2860 | 2.90M | if (coeff[1]) { |
2861 | 33.9k | ++horiz; |
2862 | 33.9k | horizSign += coeff[1] < 0 ? -1 : 1; |
2863 | 33.9k | } |
2864 | 2.90M | if (y0+y1 > cb->y0) { |
2865 | 2.45M | diag += coeff[-(int)tileComp->w + 1] ? 1 : 0; |
2866 | 2.45M | } |
2867 | 2.90M | if (y0+y1 < cb->y1 - 1 && |
2868 | 2.90M | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2869 | 2.21M | diag += coeff[tileComp->w + 1] ? 1 : 0; |
2870 | 2.21M | } |
2871 | 2.90M | } |
2872 | 3.49M | if (y0+y1 > cb->y0) { |
2873 | 2.92M | if (coeff[-(int)tileComp->w]) { |
2874 | 1.01M | ++vert; |
2875 | 1.01M | vertSign += coeff[-(int)tileComp->w] < 0 ? -1 : 1; |
2876 | 1.01M | } |
2877 | 2.92M | } |
2878 | 3.49M | if (y0+y1 < cb->y1 - 1 && |
2879 | 3.49M | (!(tileComp->codeBlockStyle & 0x08) || y1 < 3)) { |
2880 | 2.62M | if (coeff[tileComp->w]) { |
2881 | 22.3k | ++vert; |
2882 | 22.3k | vertSign += coeff[tileComp->w] < 0 ? -1 : 1; |
2883 | 22.3k | } |
2884 | 2.62M | } |
2885 | 3.49M | cx = sigPropContext[horiz][vert][diag][res == 0 ? 1 : sb]; |
2886 | 3.49M | if (cb->arithDecoder->decodeBit(cx, cb->stats)) { |
2887 | 1.09M | cx = signContext[horizSign][vertSign][0]; |
2888 | 1.09M | xorBit = signContext[horizSign][vertSign][1]; |
2889 | 1.09M | if (cb->arithDecoder->decodeBit(cx, cb->stats) ^ xorBit) { |
2890 | 553k | *coeff = -1; |
2891 | 553k | } else { |
2892 | 539k | *coeff = 1; |
2893 | 539k | } |
2894 | 1.09M | } |
2895 | 5.59M | } else { |
2896 | 5.59M | *touched = 0; |
2897 | 5.59M | } |
2898 | 9.09M | } |
2899 | 4.92M | } |
2900 | 920k | } |
2901 | 367k | ++cb->len; |
2902 | | // look for a segmentation symbol |
2903 | 367k | if (tileComp->codeBlockStyle & 0x20) { |
2904 | 22.9k | segSym = cb->arithDecoder->decodeBit(jpxContextUniform, |
2905 | 22.9k | cb->stats) << 3; |
2906 | 22.9k | segSym |= cb->arithDecoder->decodeBit(jpxContextUniform, |
2907 | 22.9k | cb->stats) << 2; |
2908 | 22.9k | segSym |= cb->arithDecoder->decodeBit(jpxContextUniform, |
2909 | 22.9k | cb->stats) << 1; |
2910 | 22.9k | segSym |= cb->arithDecoder->decodeBit(jpxContextUniform, |
2911 | 22.9k | cb->stats); |
2912 | 22.9k | if (segSym != 0x0a) { |
2913 | | // in theory this should be a fatal error, but it seems to |
2914 | | // be problematic |
2915 | 21.3k | error(errSyntaxWarning, getPos(), |
2916 | 21.3k | "Missing or invalid segmentation symbol in JPX stream"); |
2917 | 21.3k | } |
2918 | 22.9k | } |
2919 | 367k | cb->nextPass = jpxPassSigProp; |
2920 | 367k | break; |
2921 | 806k | } |
2922 | | |
2923 | 806k | if (tileComp->codeBlockStyle & 0x02) { |
2924 | 561k | cb->stats->reset(); |
2925 | 561k | cb->stats->setEntry(jpxContextSigProp, 4, 0); |
2926 | 561k | cb->stats->setEntry(jpxContextRunLength, 3, 0); |
2927 | 561k | cb->stats->setEntry(jpxContextUniform, 46, 0); |
2928 | 561k | } |
2929 | | |
2930 | 806k | if (tileComp->codeBlockStyle & 0x04) { |
2931 | 567k | cb->arithDecoder->cleanup(); |
2932 | 567k | } |
2933 | 806k | } |
2934 | | |
2935 | 236k | cb->arithDecoder->cleanup(); |
2936 | 236k | return gTrue; |
2937 | 236k | } |
2938 | | |
2939 | | // Inverse quantization, and wavelet transform (IDWT). This also does |
2940 | | // the initial shift to convert to fixed point format. |
2941 | 25.2k | void JPXStream::inverseTransform(JPXTileComp *tileComp) { |
2942 | 25.2k | JPXResLevel *resLevel; |
2943 | 25.2k | JPXPrecinct *precinct; |
2944 | 25.2k | JPXSubband *subband; |
2945 | 25.2k | JPXCodeBlock *cb; |
2946 | 25.2k | int *coeff0, *coeff; |
2947 | 25.2k | char *touched0, *touched; |
2948 | 25.2k | Guint qStyle, guard, eps, shift; |
2949 | 25.2k | int shift2; |
2950 | 25.2k | double mu; |
2951 | 25.2k | int val; |
2952 | 25.2k | Guint r, pre, cbX, cbY, x, y; |
2953 | | |
2954 | 25.2k | cover(68); |
2955 | | |
2956 | | //----- (NL)LL subband (resolution level 0) |
2957 | | |
2958 | 25.2k | resLevel = &tileComp->resLevels[0]; |
2959 | | |
2960 | | // i-quant parameters |
2961 | 25.2k | qStyle = tileComp->quantStyle & 0x1f; |
2962 | 25.2k | guard = (tileComp->quantStyle >> 5) & 7; |
2963 | 25.2k | if (qStyle == 0) { |
2964 | 13.3k | cover(69); |
2965 | 13.3k | eps = (tileComp->quantSteps[0] >> 3) & 0x1f; |
2966 | 13.3k | shift = guard + eps - 1; |
2967 | 13.3k | mu = 0; // make gcc happy |
2968 | 13.3k | } else { |
2969 | 11.8k | cover(70); |
2970 | 11.8k | shift = guard - 1 + tileComp->prec; |
2971 | 11.8k | mu = (double)(0x800 + (tileComp->quantSteps[0] & 0x7ff)) / 2048.0; |
2972 | 11.8k | } |
2973 | 25.2k | if (tileComp->transform == 0) { |
2974 | 13.3k | cover(71); |
2975 | 13.3k | shift += fracBits - tileComp->prec; |
2976 | 13.3k | } |
2977 | | |
2978 | | // do fixed point adjustment and dequantization on (NL)LL |
2979 | 50.7k | for (pre = 0; pre < resLevel->nPrecincts; ++pre) { |
2980 | 25.5k | precinct = &resLevel->precincts[pre]; |
2981 | 25.5k | subband = &precinct->subbands[0]; |
2982 | 25.5k | cb = subband->cbs; |
2983 | 79.2k | for (cbY = 0; cbY < subband->nYCBs; ++cbY) { |
2984 | 171k | for (cbX = 0; cbX < subband->nXCBs; ++cbX) { |
2985 | 117k | for (y = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched; |
2986 | 826k | y < cb->y1; |
2987 | 708k | ++y, coeff0 += tileComp->w, touched0 += resLevel->cbW) { |
2988 | 708k | for (x = cb->x0, coeff = coeff0, touched = touched0; |
2989 | 3.13M | x < cb->x1; |
2990 | 2.42M | ++x, ++coeff, ++touched) { |
2991 | 2.42M | val = *coeff; |
2992 | 2.42M | if (val != 0) { |
2993 | 473k | shift2 = shift - (cb->nZeroBitPlanes + cb->len + *touched); |
2994 | 473k | if (shift2 > 0) { |
2995 | 455k | cover(94); |
2996 | 455k | if (val < 0) { |
2997 | 225k | val = (val << shift2) - (1 << (shift2 - 1)); |
2998 | 230k | } else { |
2999 | 230k | val = (val << shift2) + (1 << (shift2 - 1)); |
3000 | 230k | } |
3001 | 455k | } else { |
3002 | 17.4k | cover(95); |
3003 | 17.4k | val >>= -shift2; |
3004 | 17.4k | } |
3005 | 473k | if (qStyle == 0) { |
3006 | 287k | cover(96); |
3007 | 287k | if (tileComp->transform == 0) { |
3008 | 287k | cover(97); |
3009 | 287k | val &= -1 << (fracBits - tileComp->prec); |
3010 | 287k | } |
3011 | 287k | } else { |
3012 | 185k | cover(98); |
3013 | 185k | val = (int)((double)val * mu); |
3014 | 185k | } |
3015 | 473k | } |
3016 | 2.42M | *coeff = val; |
3017 | 2.42M | } |
3018 | 708k | } |
3019 | 117k | ++cb; |
3020 | 117k | } |
3021 | 53.6k | } |
3022 | 25.5k | } |
3023 | | |
3024 | | //----- IDWT for each level |
3025 | | |
3026 | 152k | for (r = 1; r <= tileComp->nDecompLevels - reduction; ++r) { |
3027 | 127k | resLevel = &tileComp->resLevels[r]; |
3028 | | |
3029 | | // (n)LL is already in the upper-left corner of the |
3030 | | // tile-component data array -- interleave with (n)HL/LH/HH |
3031 | | // and inverse transform to get (n-1)LL, which will be stored |
3032 | | // in the upper-left corner of the tile-component data array |
3033 | 127k | inverseTransformLevel(tileComp, r, resLevel); |
3034 | 127k | } |
3035 | 25.2k | } |
3036 | | |
3037 | | // Do one level of the inverse transform: |
3038 | | // - take (n)LL, (n)HL, (n)LH, and (n)HH from the upper-left corner |
3039 | | // of the tile-component data array |
3040 | | // - leave the resulting (n-1)LL in the same place |
3041 | | void JPXStream::inverseTransformLevel(JPXTileComp *tileComp, |
3042 | 127k | Guint r, JPXResLevel *resLevel) { |
3043 | 127k | JPXPrecinct *precinct; |
3044 | 127k | JPXSubband *subband; |
3045 | 127k | JPXCodeBlock *cb; |
3046 | 127k | int *coeff0, *coeff; |
3047 | 127k | char *touched0, *touched; |
3048 | 127k | Guint qStyle, guard, eps, shift, t; |
3049 | 127k | int shift2; |
3050 | 127k | double mu; |
3051 | 127k | int val; |
3052 | 127k | int *dataPtr, *bufPtr; |
3053 | 127k | Guint nx1, nx2, ny1, ny2, offset; |
3054 | 127k | Guint x, y, sb, pre, cbX, cbY; |
3055 | | |
3056 | 127k | qStyle = tileComp->quantStyle & 0x1f; |
3057 | 127k | guard = (tileComp->quantStyle >> 5) & 7; |
3058 | | |
3059 | | //----- compute subband bounds |
3060 | | |
3061 | | // 0 nx1 nx2 |
3062 | | // | | | |
3063 | | // v v v |
3064 | | // +----+----+ |
3065 | | // | LL | HL | <- 0 |
3066 | | // +----+----+ |
3067 | | // | LH | HH | <- ny1 |
3068 | | // +----+----+ |
3069 | | // <- ny2 |
3070 | 127k | nx1 = resLevel->bx1[1] - resLevel->bx0[1]; |
3071 | 127k | nx2 = nx1 + resLevel->bx1[0] - resLevel->bx0[0]; |
3072 | 127k | ny1 = resLevel->by1[0] - resLevel->by0[0]; |
3073 | 127k | ny2 = ny1 + resLevel->by1[1] - resLevel->by0[1]; |
3074 | 127k | if (nx2 == 0 || ny2 == 0) { |
3075 | 42.1k | return; |
3076 | 42.1k | } |
3077 | | |
3078 | | //----- fixed-point adjustment and dequantization |
3079 | | |
3080 | 339k | for (sb = 0; sb < 3; ++sb) { |
3081 | | |
3082 | | // i-quant parameters |
3083 | 254k | if (qStyle == 0) { |
3084 | 95.9k | cover(100); |
3085 | 95.9k | eps = (tileComp->quantSteps[3*r - 2 + sb] >> 3) & 0x1f; |
3086 | 95.9k | shift = guard + eps - 1; |
3087 | 95.9k | mu = 0; // make gcc happy |
3088 | 158k | } else { |
3089 | 158k | cover(101); |
3090 | 158k | shift = guard + tileComp->prec; |
3091 | 158k | if (sb == 2) { |
3092 | 52.8k | cover(102); |
3093 | 52.8k | ++shift; |
3094 | 52.8k | } |
3095 | 158k | t = tileComp->quantSteps[qStyle == 1 ? 0 : (3*r - 2 + sb)]; |
3096 | 158k | mu = (double)(0x800 + (t & 0x7ff)) / 2048.0; |
3097 | 158k | } |
3098 | 254k | if (tileComp->transform == 0) { |
3099 | 160k | cover(103); |
3100 | 160k | shift += fracBits - tileComp->prec; |
3101 | 160k | } |
3102 | | |
3103 | | // fixed point adjustment and dequantization |
3104 | | |
3105 | 522k | for (pre = 0; pre < resLevel->nPrecincts; ++pre) { |
3106 | 268k | precinct = &resLevel->precincts[pre]; |
3107 | 268k | subband = &precinct->subbands[sb]; |
3108 | 268k | cb = subband->cbs; |
3109 | 1.38M | for (cbY = 0; cbY < subband->nYCBs; ++cbY) { |
3110 | 2.70M | for (cbX = 0; cbX < subband->nXCBs; ++cbX) { |
3111 | 1.58M | for (y = cb->y0, coeff0 = cb->coeffs, touched0 = cb->touched; |
3112 | 20.1M | y < cb->y1; |
3113 | 18.5M | ++y, coeff0 += tileComp->w, touched0 += resLevel->cbW) { |
3114 | 18.5M | for (x = cb->x0, coeff = coeff0, touched = touched0; |
3115 | 159M | x < cb->x1; |
3116 | 140M | ++x, ++coeff, ++touched) { |
3117 | 140M | val = *coeff; |
3118 | 140M | if (val != 0) { |
3119 | 1.41M | shift2 = shift - (cb->nZeroBitPlanes + cb->len + *touched); |
3120 | 1.41M | if (shift2 > 0) { |
3121 | 900k | cover(74); |
3122 | 900k | if (val < 0) { |
3123 | 480k | val = (val << shift2) - (1 << (shift2 - 1)); |
3124 | 480k | } else { |
3125 | 420k | val = (val << shift2) + (1 << (shift2 - 1)); |
3126 | 420k | } |
3127 | 900k | } else { |
3128 | 511k | cover(75); |
3129 | 511k | val >>= -shift2; |
3130 | 511k | } |
3131 | 1.41M | if (qStyle == 0) { |
3132 | 801k | cover(76); |
3133 | 801k | if (tileComp->transform == 0) { |
3134 | 855 | val &= -1 << (fracBits - tileComp->prec); |
3135 | 855 | } |
3136 | 801k | } else { |
3137 | 610k | cover(77); |
3138 | 610k | val = (int)((double)val * mu); |
3139 | 610k | } |
3140 | 1.41M | } |
3141 | 140M | *coeff = val; |
3142 | 140M | } |
3143 | 18.5M | } |
3144 | 1.58M | ++cb; |
3145 | 1.58M | } |
3146 | 1.11M | } |
3147 | 268k | } |
3148 | 254k | } |
3149 | | |
3150 | | //----- inverse transform |
3151 | | |
3152 | | // horizontal (row) transforms |
3153 | 84.8k | offset = 3 + (resLevel->x0 & 1); |
3154 | 17.4M | for (y = 0, dataPtr = tileComp->data; y < ny2; ++y, dataPtr += tileComp->w) { |
3155 | 17.3M | if (resLevel->bx0[0] == resLevel->bx0[1]) { |
3156 | | // fetch LL/LH |
3157 | 17.0M | for (x = 0, bufPtr = tileComp->buf + offset; |
3158 | 101M | x < nx1; |
3159 | 84.6M | ++x, bufPtr += 2) { |
3160 | 84.6M | *bufPtr = dataPtr[x]; |
3161 | 84.6M | } |
3162 | | // fetch HL/HH |
3163 | 17.0M | for (x = nx1, bufPtr = tileComp->buf + offset + 1; |
3164 | 85.8M | x < nx2; |
3165 | 68.8M | ++x, bufPtr += 2) { |
3166 | 68.8M | *bufPtr = dataPtr[x]; |
3167 | 68.8M | } |
3168 | 17.0M | } else { |
3169 | | // fetch LL/LH |
3170 | 323k | for (x = 0, bufPtr = tileComp->buf + offset + 1; |
3171 | 19.5M | x < nx1; |
3172 | 19.2M | ++x, bufPtr += 2) { |
3173 | 19.2M | *bufPtr = dataPtr[x]; |
3174 | 19.2M | } |
3175 | | // fetch HL/HH |
3176 | 323k | for (x = nx1, bufPtr = tileComp->buf + offset; |
3177 | 19.7M | x < nx2; |
3178 | 19.4M | ++x, bufPtr += 2) { |
3179 | 19.4M | *bufPtr = dataPtr[x]; |
3180 | 19.4M | } |
3181 | 323k | } |
3182 | 17.3M | inverseTransform1D(tileComp, tileComp->buf, offset, nx2); |
3183 | 209M | for (x = 0, bufPtr = tileComp->buf + offset; x < nx2; ++x, ++bufPtr) { |
3184 | 192M | dataPtr[x] = *bufPtr; |
3185 | 192M | } |
3186 | 17.3M | } |
3187 | | |
3188 | | // vertical (column) transforms |
3189 | 84.8k | offset = 3 + (resLevel->y0 & 1); |
3190 | 5.49M | for (x = 0, dataPtr = tileComp->data; x < nx2; ++x, ++dataPtr) { |
3191 | 5.41M | if (resLevel->by0[0] == resLevel->by0[1]) { |
3192 | | // fetch LL/HL |
3193 | 1.14M | for (y = 0, bufPtr = tileComp->buf + offset; |
3194 | 36.5M | y < ny1; |
3195 | 35.4M | ++y, bufPtr += 2) { |
3196 | 35.4M | *bufPtr = dataPtr[y * tileComp->w]; |
3197 | 35.4M | } |
3198 | | // fetch LH/HH |
3199 | 1.14M | for (y = ny1, bufPtr = tileComp->buf + offset + 1; |
3200 | 35.7M | y < ny2; |
3201 | 34.5M | ++y, bufPtr += 2) { |
3202 | 34.5M | *bufPtr = dataPtr[y * tileComp->w]; |
3203 | 34.5M | } |
3204 | 4.27M | } else { |
3205 | | // fetch LL/HL |
3206 | 4.27M | for (y = 0, bufPtr = tileComp->buf + offset + 1; |
3207 | 63.7M | y < ny1; |
3208 | 59.4M | ++y, bufPtr += 2) { |
3209 | 59.4M | *bufPtr = dataPtr[y * tileComp->w]; |
3210 | 59.4M | } |
3211 | | // fetch LH/HH |
3212 | 4.27M | for (y = ny1, bufPtr = tileComp->buf + offset; |
3213 | 67.0M | y < ny2; |
3214 | 62.7M | ++y, bufPtr += 2) { |
3215 | 62.7M | *bufPtr = dataPtr[y * tileComp->w]; |
3216 | 62.7M | } |
3217 | 4.27M | } |
3218 | 5.41M | inverseTransform1D(tileComp, tileComp->buf, offset, ny2); |
3219 | 197M | for (y = 0, bufPtr = tileComp->buf + offset; y < ny2; ++y, ++bufPtr) { |
3220 | 192M | dataPtr[y * tileComp->w] = *bufPtr; |
3221 | 192M | } |
3222 | 5.41M | } |
3223 | 84.8k | } |
3224 | | |
3225 | | void JPXStream::inverseTransform1D(JPXTileComp *tileComp, int *data, |
3226 | 22.7M | Guint offset, Guint n) { |
3227 | 22.7M | Guint end, i; |
3228 | | |
3229 | | //----- special case for length = 1 |
3230 | 22.7M | if (n == 1) { |
3231 | 14.9M | cover(79); |
3232 | 14.9M | if (offset == 4) { |
3233 | 1.11M | cover(104); |
3234 | 1.11M | *data >>= 1; |
3235 | 1.11M | } |
3236 | | |
3237 | 14.9M | } else { |
3238 | 7.79M | cover(80); |
3239 | | |
3240 | 7.79M | end = offset + n; |
3241 | | |
3242 | | //----- extend right |
3243 | 7.79M | data[end] = data[end - 2]; |
3244 | 7.79M | if (n == 2) { |
3245 | 296k | cover(81); |
3246 | 296k | data[end+1] = data[offset + 1]; |
3247 | 296k | data[end+2] = data[offset]; |
3248 | 296k | data[end+3] = data[offset + 1]; |
3249 | 7.50M | } else { |
3250 | 7.50M | cover(82); |
3251 | 7.50M | data[end+1] = data[end - 3]; |
3252 | 7.50M | if (n == 3) { |
3253 | 337k | cover(105); |
3254 | 337k | data[end+2] = data[offset + 1]; |
3255 | 337k | data[end+3] = data[offset + 2]; |
3256 | 7.16M | } else { |
3257 | 7.16M | cover(106); |
3258 | 7.16M | data[end+2] = data[end - 4]; |
3259 | 7.16M | if (n == 4) { |
3260 | 272k | cover(107); |
3261 | 272k | data[end+3] = data[offset + 1]; |
3262 | 6.89M | } else { |
3263 | 6.89M | cover(108); |
3264 | 6.89M | data[end+3] = data[end - 5]; |
3265 | 6.89M | } |
3266 | 7.16M | } |
3267 | 7.50M | } |
3268 | | |
3269 | | //----- extend left |
3270 | 7.79M | data[offset - 1] = data[offset + 1]; |
3271 | 7.79M | data[offset - 2] = data[offset + 2]; |
3272 | 7.79M | data[offset - 3] = data[offset + 3]; |
3273 | 7.79M | if (offset == 4) { |
3274 | 3.48M | cover(83); |
3275 | 3.48M | data[0] = data[offset + 4]; |
3276 | 3.48M | } |
3277 | | |
3278 | | //----- 9-7 irreversible filter |
3279 | | |
3280 | 7.79M | if (tileComp->transform == 0) { |
3281 | 4.38M | cover(84); |
3282 | | // step 1 (even) |
3283 | 120M | for (i = 1; i <= end + 2; i += 2) { |
3284 | 115M | data[i] = (int)(idwtKappa * data[i]); |
3285 | 115M | } |
3286 | | // step 2 (odd) |
3287 | 124M | for (i = 0; i <= end + 3; i += 2) { |
3288 | 120M | data[i] = (int)(idwtIKappa * data[i]); |
3289 | 120M | } |
3290 | | // step 3 (even) |
3291 | 120M | for (i = 1; i <= end + 2; i += 2) { |
3292 | 115M | data[i] = (int)(data[i] - idwtDelta * (data[i-1] + data[i+1])); |
3293 | 115M | } |
3294 | | // step 4 (odd) |
3295 | 115M | for (i = 2; i <= end + 1; i += 2) { |
3296 | 111M | data[i] = (int)(data[i] - idwtGamma * (data[i-1] + data[i+1])); |
3297 | 111M | } |
3298 | | // step 5 (even) |
3299 | 111M | for (i = 3; i <= end; i += 2) { |
3300 | 107M | data[i] = (int)(data[i] - idwtBeta * (data[i-1] + data[i+1])); |
3301 | 107M | } |
3302 | | // step 6 (odd) |
3303 | 107M | for (i = 4; i <= end - 1; i += 2) { |
3304 | 102M | data[i] = (int)(data[i] - idwtAlpha * (data[i-1] + data[i+1])); |
3305 | 102M | } |
3306 | | |
3307 | | //----- 5-3 reversible filter |
3308 | | |
3309 | 4.38M | } else { |
3310 | 3.41M | cover(85); |
3311 | | // step 1 (even) |
3312 | 88.6M | for (i = 3; i <= end; i += 2) { |
3313 | 85.2M | data[i] -= (data[i-1] + data[i+1] + 2) >> 2; |
3314 | 85.2M | } |
3315 | | // step 2 (odd) |
3316 | 85.2M | for (i = 4; i < end; i += 2) { |
3317 | 81.8M | data[i] += (data[i-1] + data[i+1]) >> 1; |
3318 | 81.8M | } |
3319 | 3.41M | } |
3320 | 7.79M | } |
3321 | 22.7M | } |
3322 | | |
3323 | | // Inverse multi-component transform and DC level shift. This also |
3324 | | // converts fixed point samples back to integers. |
3325 | 6.70k | GBool JPXStream::inverseMultiCompAndDC(JPXTile *tile) { |
3326 | 6.70k | JPXTileComp *tileComp; |
3327 | 6.70k | int coeff, d0, d1, d2, t, minVal, maxVal, zeroVal, shift, rounding; |
3328 | 6.70k | int *dataPtr; |
3329 | 6.70k | Guint j, comp, x, y; |
3330 | | |
3331 | | //----- inverse multi-component transform |
3332 | | |
3333 | 6.70k | if (tile->multiComp == 1) { |
3334 | 3.10k | cover(86); |
3335 | 3.10k | if (img.nComps < 3 || |
3336 | 3.10k | tile->tileComps[0].hSep != tile->tileComps[1].hSep || |
3337 | 3.10k | tile->tileComps[0].vSep != tile->tileComps[1].vSep || |
3338 | 3.10k | tile->tileComps[1].hSep != tile->tileComps[2].hSep || |
3339 | 3.10k | tile->tileComps[1].vSep != tile->tileComps[2].vSep) { |
3340 | 2.35k | return gFalse; |
3341 | 2.35k | } |
3342 | | |
3343 | | // inverse irreversible multiple component transform |
3344 | 747 | if (tile->tileComps[0].transform == 0) { |
3345 | 393 | cover(87); |
3346 | 393 | j = 0; |
3347 | 5.19k | for (y = 0; y < tile->tileComps[0].h; ++y) { |
3348 | 174k | for (x = 0; x < tile->tileComps[0].w; ++x) { |
3349 | 169k | d0 = tile->tileComps[0].data[j]; |
3350 | 169k | d1 = tile->tileComps[1].data[j]; |
3351 | 169k | d2 = tile->tileComps[2].data[j]; |
3352 | 169k | tile->tileComps[0].data[j] = (int)(d0 + 1.402 * d2 + 0.5); |
3353 | 169k | tile->tileComps[1].data[j] = |
3354 | 169k | (int)(d0 - 0.34413 * d1 - 0.71414 * d2 + 0.5); |
3355 | 169k | tile->tileComps[2].data[j] = (int)(d0 + 1.772 * d1 + 0.5); |
3356 | 169k | ++j; |
3357 | 169k | } |
3358 | 4.80k | } |
3359 | | |
3360 | | // inverse reversible multiple component transform |
3361 | 393 | } else { |
3362 | 354 | cover(88); |
3363 | 354 | j = 0; |
3364 | 708 | for (y = 0; y < tile->tileComps[0].h; ++y) { |
3365 | 121k | for (x = 0; x < tile->tileComps[0].w; ++x) { |
3366 | 120k | d0 = tile->tileComps[0].data[j]; |
3367 | 120k | d1 = tile->tileComps[1].data[j]; |
3368 | 120k | d2 = tile->tileComps[2].data[j]; |
3369 | 120k | tile->tileComps[1].data[j] = t = d0 - ((d2 + d1) >> 2); |
3370 | 120k | tile->tileComps[0].data[j] = d2 + t; |
3371 | 120k | tile->tileComps[2].data[j] = d1 + t; |
3372 | 120k | ++j; |
3373 | 120k | } |
3374 | 354 | } |
3375 | 354 | } |
3376 | 747 | } |
3377 | | |
3378 | | //----- DC level shift |
3379 | 19.0k | for (comp = 0; comp < img.nComps; ++comp) { |
3380 | 14.7k | tileComp = &tile->tileComps[comp]; |
3381 | | |
3382 | | // signed: clip |
3383 | 14.7k | if (tileComp->sgned) { |
3384 | 7.85k | cover(89); |
3385 | 7.85k | minVal = -(1 << (tileComp->prec - 1)); |
3386 | 7.85k | maxVal = (1 << (tileComp->prec - 1)) - 1; |
3387 | 7.85k | dataPtr = tileComp->data; |
3388 | 484k | for (y = 0; y < tileComp->h; ++y) { |
3389 | 26.7M | for (x = 0; x < tileComp->w; ++x) { |
3390 | 26.3M | coeff = *dataPtr; |
3391 | 26.3M | if (tileComp->transform == 0) { |
3392 | 10.4M | cover(109); |
3393 | 10.4M | coeff >>= fracBits - tileComp->prec; |
3394 | 10.4M | } |
3395 | 26.3M | if (coeff < minVal) { |
3396 | 188k | cover(110); |
3397 | 188k | coeff = minVal; |
3398 | 26.1M | } else if (coeff > maxVal) { |
3399 | 16.0M | cover(111); |
3400 | 16.0M | coeff = maxVal; |
3401 | 16.0M | } |
3402 | 26.3M | *dataPtr++ = coeff; |
3403 | 26.3M | } |
3404 | 476k | } |
3405 | | |
3406 | | // unsigned: inverse DC level shift and clip |
3407 | 7.85k | } else { |
3408 | 6.87k | cover(90); |
3409 | 6.87k | maxVal = (1 << tileComp->prec) - 1; |
3410 | 6.87k | zeroVal = 1 << (tileComp->prec - 1); |
3411 | 6.87k | dataPtr = tileComp->data; |
3412 | 6.87k | shift = fracBits - tileComp->prec; |
3413 | 6.87k | rounding = 1 << (shift - 1); |
3414 | 483k | for (y = 0; y < tileComp->h; ++y) { |
3415 | 51.3M | for (x = 0; x < tileComp->w; ++x) { |
3416 | 50.8M | coeff = *dataPtr; |
3417 | 50.8M | if (tileComp->transform == 0) { |
3418 | 22.9M | cover(112); |
3419 | 22.9M | coeff = (coeff + rounding) >> shift; |
3420 | 22.9M | } |
3421 | 50.8M | coeff += zeroVal; |
3422 | 50.8M | if (coeff < 0) { |
3423 | 5.39M | cover(113); |
3424 | 5.39M | coeff = 0; |
3425 | 45.4M | } else if (coeff > maxVal) { |
3426 | 5.61M | cover(114); |
3427 | 5.61M | coeff = maxVal; |
3428 | 5.61M | } |
3429 | 50.8M | *dataPtr++ = coeff; |
3430 | 50.8M | } |
3431 | 476k | } |
3432 | 6.87k | } |
3433 | 14.7k | } |
3434 | | |
3435 | 4.35k | return gTrue; |
3436 | 6.70k | } |
3437 | | |
3438 | 44.7k | GBool JPXStream::readBoxHdr(Guint *boxType, Guint *boxLen, Guint *dataLen) { |
3439 | 44.7k | Guint len, lenH; |
3440 | | |
3441 | 44.7k | if (!readULong(&len) || |
3442 | 44.7k | !readULong(boxType)) { |
3443 | 1.52k | return gFalse; |
3444 | 1.52k | } |
3445 | 43.2k | if (len == 1) { |
3446 | 2.04k | if (!readULong(&lenH) || !readULong(&len)) { |
3447 | 1.90k | return gFalse; |
3448 | 1.90k | } |
3449 | 147 | if (lenH) { |
3450 | 0 | error(errSyntaxError, getPos(), |
3451 | 0 | "JPX stream contains a box larger than 2^32 bytes"); |
3452 | 0 | return gFalse; |
3453 | 0 | } |
3454 | 147 | *boxLen = len; |
3455 | 147 | *dataLen = len - 16; |
3456 | 41.2k | } else if (len == 0) { |
3457 | 684 | *boxLen = 0; |
3458 | 684 | *dataLen = 0; |
3459 | 40.5k | } else { |
3460 | 40.5k | *boxLen = len; |
3461 | 40.5k | *dataLen = len - 8; |
3462 | 40.5k | } |
3463 | 41.3k | return gTrue; |
3464 | 43.2k | } |
3465 | | |
3466 | 493k | int JPXStream::readMarkerHdr(int *segType, Guint *segLen) { |
3467 | 493k | int c; |
3468 | | |
3469 | 513k | do { |
3470 | 47.0M | do { |
3471 | 47.0M | if ((c = bufStr->getChar()) == EOF) { |
3472 | 3.20k | return gFalse; |
3473 | 3.20k | } |
3474 | 47.0M | } while (c != 0xff); |
3475 | 814k | do { |
3476 | 814k | if ((c = bufStr->getChar()) == EOF) { |
3477 | 411 | return gFalse; |
3478 | 411 | } |
3479 | 814k | } while (c == 0xff); |
3480 | 510k | } while (c == 0x00); |
3481 | 489k | *segType = c; |
3482 | 489k | if ((c >= 0x30 && c <= 0x3f) || |
3483 | 489k | c == 0x4f || c == 0x92 || c == 0x93 || c == 0xd9) { |
3484 | 61.0k | *segLen = 0; |
3485 | 61.0k | return gTrue; |
3486 | 61.0k | } |
3487 | 428k | return readUWord(segLen); |
3488 | 489k | } |
3489 | | |
3490 | 3.08M | GBool JPXStream::readUByte(Guint *x) { |
3491 | 3.08M | int c0; |
3492 | | |
3493 | 3.08M | if ((c0 = bufStr->getChar()) == EOF) { |
3494 | 9.03k | return gFalse; |
3495 | 9.03k | } |
3496 | 3.07M | *x = (Guint)c0; |
3497 | 3.07M | return gTrue; |
3498 | 3.08M | } |
3499 | | |
3500 | 4.32k | GBool JPXStream::readByte(int *x) { |
3501 | 4.32k | int c0; |
3502 | | |
3503 | 4.32k | if ((c0 = bufStr->getChar()) == EOF) { |
3504 | 128 | return gFalse; |
3505 | 128 | } |
3506 | 4.19k | *x = c0; |
3507 | 4.19k | if (c0 & 0x80) { |
3508 | 3.20k | *x |= -1 - 0xff; |
3509 | 3.20k | } |
3510 | 4.19k | return gTrue; |
3511 | 4.32k | } |
3512 | | |
3513 | 3.13M | GBool JPXStream::readUWord(Guint *x) { |
3514 | 3.13M | int c0, c1; |
3515 | | |
3516 | 3.13M | if ((c0 = bufStr->getChar()) == EOF || |
3517 | 3.13M | (c1 = bufStr->getChar()) == EOF) { |
3518 | 6.16k | return gFalse; |
3519 | 6.16k | } |
3520 | 3.12M | *x = (Guint)((c0 << 8) | c1); |
3521 | 3.12M | return gTrue; |
3522 | 3.13M | } |
3523 | | |
3524 | 418k | GBool JPXStream::readULong(Guint *x) { |
3525 | 418k | int c0, c1, c2, c3; |
3526 | | |
3527 | 418k | if ((c0 = bufStr->getChar()) == EOF || |
3528 | 418k | (c1 = bufStr->getChar()) == EOF || |
3529 | 418k | (c2 = bufStr->getChar()) == EOF || |
3530 | 418k | (c3 = bufStr->getChar()) == EOF) { |
3531 | 12.5k | return gFalse; |
3532 | 12.5k | } |
3533 | 405k | *x = (Guint)((c0 << 24) | (c1 << 16) | (c2 << 8) | c3); |
3534 | 405k | return gTrue; |
3535 | 418k | } |
3536 | | |
3537 | 4.21k | GBool JPXStream::readNBytes(int nBytes, GBool signd, int *x) { |
3538 | 4.21k | int y, c, i; |
3539 | | |
3540 | 4.21k | y = 0; |
3541 | 39.1k | for (i = 0; i < nBytes; ++i) { |
3542 | 34.9k | if ((c = bufStr->getChar()) == EOF) { |
3543 | 12 | return gFalse; |
3544 | 12 | } |
3545 | 34.9k | y = (y << 8) + c; |
3546 | 34.9k | } |
3547 | 4.20k | if (signd) { |
3548 | 466 | if (y & (1 << (8 * nBytes - 1))) { |
3549 | 101 | y |= -1 << (8 * nBytes); |
3550 | 101 | } |
3551 | 466 | } |
3552 | 4.20k | *x = y; |
3553 | 4.20k | return gTrue; |
3554 | 4.21k | } |
3555 | | |
3556 | 622k | void JPXStream::startBitBuf(Guint byteCountA) { |
3557 | 622k | bitBufLen = 0; |
3558 | 622k | bitBufSkip = gFalse; |
3559 | 622k | byteCount = byteCountA; |
3560 | 622k | } |
3561 | | |
3562 | 3.96M | GBool JPXStream::readBits(int nBits, Guint *x) { |
3563 | 3.96M | int c; |
3564 | | |
3565 | 5.38M | while (bitBufLen < nBits) { |
3566 | 1.43M | if (byteCount == 0 || (c = bufStr->getChar()) == EOF) { |
3567 | 3.44k | return gFalse; |
3568 | 3.44k | } |
3569 | 1.42M | --byteCount; |
3570 | 1.42M | if (bitBufSkip) { |
3571 | 41.2k | bitBuf = (bitBuf << 7) | (c & 0x7f); |
3572 | 41.2k | bitBufLen += 7; |
3573 | 1.38M | } else { |
3574 | 1.38M | bitBuf = (bitBuf << 8) | (c & 0xff); |
3575 | 1.38M | bitBufLen += 8; |
3576 | 1.38M | } |
3577 | 1.42M | bitBufSkip = c == 0xff; |
3578 | 1.42M | } |
3579 | 3.95M | *x = (bitBuf >> (bitBufLen - nBits)) & ((1 << nBits) - 1); |
3580 | 3.95M | bitBufLen -= nBits; |
3581 | 3.95M | return gTrue; |
3582 | 3.96M | } |
3583 | | |
3584 | 110k | void JPXStream::skipSOP() { |
3585 | | // SOP occurs at the start of the packet header, so we don't need to |
3586 | | // worry about bit-stuff prior to it |
3587 | 110k | if (byteCount >= 6 && |
3588 | 110k | bufStr->lookChar(0) == 0xff && |
3589 | 110k | bufStr->lookChar(1) == 0x91) { |
3590 | 219 | bufStr->discardChars(6); |
3591 | 219 | byteCount -= 6; |
3592 | 219 | bitBufLen = 0; |
3593 | 219 | bitBufSkip = gFalse; |
3594 | 219 | } |
3595 | 110k | } |
3596 | | |
3597 | 134k | void JPXStream::skipEPH() { |
3598 | 134k | int k; |
3599 | | |
3600 | 134k | k = bitBufSkip ? 1 : 0; |
3601 | 134k | if (byteCount >= (Guint)(k + 2) && |
3602 | 134k | bufStr->lookChar(k) == 0xff && |
3603 | 134k | bufStr->lookChar(k + 1) == 0x92) { |
3604 | 253 | bufStr->discardChars(k + 2); |
3605 | 253 | byteCount -= k + 2; |
3606 | 253 | bitBufLen = 0; |
3607 | 253 | bitBufSkip = gFalse; |
3608 | 253 | } |
3609 | 134k | } |
3610 | | |
3611 | 619k | Guint JPXStream::finishBitBuf() { |
3612 | 619k | if (bitBufSkip) { |
3613 | 800 | bufStr->getChar(); |
3614 | 800 | --byteCount; |
3615 | 800 | } |
3616 | 619k | return byteCount; |
3617 | 619k | } |