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