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