1 /*
2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3 * British Columbia.
4 * Copyright (c) 2001-2003 Michael David Adams.
5 * All rights reserved.
6 */
7
8 /* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer License Version 2.0
11 *
12 * Copyright (c) 2001-2006 Michael David Adams
13 * Copyright (c) 1999-2000 Image Power, Inc.
14 * Copyright (c) 1999-2000 The University of British Columbia
15 *
16 * All rights reserved.
17 *
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
25 *
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
29 *
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
32 * written permission.
33 *
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60 *
61 * __END_OF_JASPER_LICENSE__
62 */
63
64 /*
65 * $Id$
66 */
67
68 /******************************************************************************\
69 * Includes.
70 \******************************************************************************/
71
72 #include "jpc_dec.h"
73 #include "jpc_fix.h"
74 #include "jpc_cs.h"
75 #include "jpc_mct.h"
76 #include "jpc_t2dec.h"
77 #include "jpc_t1cod.h"
78 #include "jpc_t1dec.h"
79 #include "jpc_math.h"
80
81 #include "jasper/jas_types.h"
82 #include "jasper/jas_math.h"
83 #include "jasper/jas_tvp.h"
84 #include "jasper/jas_malloc.h"
85 #include "jasper/jas_debug.h"
86
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <assert.h>
90
91 /******************************************************************************\
92 *
93 \******************************************************************************/
94
95 #define JPC_MHSOC 0x0001
96 /* In the main header, expecting a SOC marker segment. */
97 #define JPC_MHSIZ 0x0002
98 /* In the main header, expecting a SIZ marker segment. */
99 #define JPC_MH 0x0004
100 /* In the main header, expecting "other" marker segments. */
101 #define JPC_TPHSOT 0x0008
102 /* In a tile-part header, expecting a SOT marker segment. */
103 #define JPC_TPH 0x0010
104 /* In a tile-part header, expecting "other" marker segments. */
105 #define JPC_MT 0x0020
106 /* In the main trailer. */
107
108 typedef struct {
109
110 uint_least16_t id;
111 /* The marker segment type. */
112
113 uint_least16_t validstates;
114 /* The states in which this type of marker segment can be
115 validly encountered. */
116
117 int (*action)(jpc_dec_t *dec, jpc_ms_t *ms);
118 /* The action to take upon encountering this type of marker segment. */
119
120 } jpc_dec_mstabent_t;
121
122 /******************************************************************************\
123 *
124 \******************************************************************************/
125
126 /* COD/COC parameters have been specified. */
127 #define JPC_CSET 0x0001
128 /* QCD/QCC parameters have been specified. */
129 #define JPC_QSET 0x0002
130 /* COD/COC parameters set from a COC marker segment. */
131 #define JPC_COC 0x0004
132 /* QCD/QCC parameters set from a QCC marker segment. */
133 #define JPC_QCC 0x0008
134
135 /******************************************************************************\
136 * Local function prototypes.
137 \******************************************************************************/
138
139 static int jpc_dec_dump(const jpc_dec_t *dec, FILE *out);
140
141 static jpc_ppxstab_t *jpc_ppxstab_create(void);
142 static void jpc_ppxstab_destroy(jpc_ppxstab_t *tab);
143 static int jpc_ppxstab_grow(jpc_ppxstab_t *tab, unsigned maxents);
144 static int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent);
145 static jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab);
146 static int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab);
147 static jpc_ppxstabent_t *jpc_ppxstabent_create(void);
148 static void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent);
149
150 static int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist);
151 static jpc_streamlist_t *jpc_streamlist_create(void);
152
153 static int jpc_streamlist_insert(jpc_streamlist_t *streamlist, unsigned streamno,
154 jas_stream_t *stream);
155 static jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, unsigned streamno);
156 static void jpc_streamlist_destroy(jpc_streamlist_t *streamlist);
157
158 static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp);
159 static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps);
160 static int jpc_dec_cp_isvalid(const jpc_dec_cp_t *cp);
161 static jpc_dec_cp_t *jpc_dec_cp_copy(const jpc_dec_cp_t *cp);
162 static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, const jpc_cod_t *cod);
163 static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, const jpc_coc_t *coc);
164 static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
165 const jpc_coxcp_t *compparms, unsigned flags);
166 static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, const jpc_qcd_t *qcd);
167 static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, const jpc_qcc_t *qcc);
168 static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
169 const jpc_qcxcp_t *compparms, unsigned flags);
170 static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, const jpc_rgn_t *rgn);
171 static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp);
172 static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp);
173 static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, const jpc_poc_t *poc, int reset);
174 static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, const jpc_poc_t *poc);
175
176 static int jpc_dec_decode(jpc_dec_t *dec);
177 static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts,
178 jas_stream_t *in);
179 static void jpc_dec_destroy(jpc_dec_t *dec);
180 static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize);
181 static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, unsigned numbps);
182 static jpc_fix_t jpc_calcabsstepsize(unsigned stepsize, unsigned numbits);
183 static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile);
184 static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile);
185 static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile);
186 static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms);
187 static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms);
188 static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms);
189 static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms);
190 static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms);
191 static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms);
192 static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms);
193 static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms);
194 static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms);
195 static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms);
196 static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms);
197 static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms);
198 static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms);
199 static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms);
200 static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms);
201 static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms);
202 static jpc_dec_importopts_t *jpc_dec_opts_create(const char *optstr);
203 static void jpc_dec_opts_destroy(jpc_dec_importopts_t *opts);
204
205 static const jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id);
206
207 /******************************************************************************\
208 * Global data.
209 \******************************************************************************/
210
211 static const jpc_dec_mstabent_t jpc_dec_mstab[] = {
212 {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc},
213 {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot},
214 {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod},
215 {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc},
216 {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz},
217 {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod},
218 {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc},
219 {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn},
220 {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd},
221 {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc},
222 {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc},
223 {JPC_MS_TLM, JPC_MH, 0},
224 {JPC_MS_PLM, JPC_MH, 0},
225 {JPC_MS_PLT, JPC_TPH, 0},
226 {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm},
227 {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt},
228 {JPC_MS_SOP, 0, 0},
229 {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg},
230 {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com},
231 {0, JPC_MH | JPC_TPH, jpc_dec_process_unk}
232 };
233
234 /******************************************************************************\
235 * The main entry point for the JPEG-2000 decoder.
236 \******************************************************************************/
237
jpc_decode(jas_stream_t * in,const char * optstr)238 jas_image_t *jpc_decode(jas_stream_t *in, const char *optstr)
239 {
240 jpc_dec_importopts_t *opts;
241 jpc_dec_t *dec;
242 jas_image_t *image;
243
244 dec = 0;
245 opts = 0;
246
247 JAS_DBGLOG(100, ("jpc_decode(%p, \"%s\")\n", in, optstr));
248
249 if (!(opts = jpc_dec_opts_create(optstr))) {
250 goto error;
251 }
252
253 jpc_initluts();
254
255 if (!(dec = jpc_dec_create(opts, in))) {
256 goto error;
257 }
258 jpc_dec_opts_destroy(opts);
259 opts = 0;
260
261 /* Do most of the work. */
262 if (jpc_dec_decode(dec)) {
263 goto error;
264 }
265
266 if (jas_image_numcmpts(dec->image) >= 3) {
267 jas_image_setclrspc(dec->image, JAS_CLRSPC_SRGB);
268 jas_image_setcmpttype(dec->image, 0,
269 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
270 jas_image_setcmpttype(dec->image, 1,
271 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
272 jas_image_setcmpttype(dec->image, 2,
273 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
274 } else {
275 jas_image_setclrspc(dec->image, JAS_CLRSPC_SGRAY);
276 jas_image_setcmpttype(dec->image, 0,
277 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
278 }
279
280 /* Save the return value. */
281 image = dec->image;
282
283 /* Stop the image from being discarded. */
284 dec->image = 0;
285
286 /* Destroy decoder. */
287 jpc_dec_destroy(dec);
288
289 return image;
290
291 error:
292 if (opts) {
293 jpc_dec_opts_destroy(opts);
294 }
295 if (dec) {
296 jpc_dec_destroy(dec);
297 }
298 return 0;
299 }
300
301 typedef enum {
302 OPT_MAXLYRS,
303 OPT_MAXPKTS,
304 OPT_MAXSAMPLES,
305 OPT_DEBUG
306 } optid_t;
307
308 static const jas_taginfo_t decopts[] = {
309 {OPT_MAXLYRS, "maxlyrs"},
310 {OPT_MAXPKTS, "maxpkts"},
311 {OPT_MAXSAMPLES, "max_samples"},
312 {OPT_DEBUG, "debug"},
313 {-1, 0}
314 };
315
jpc_dec_opts_create(const char * optstr)316 static jpc_dec_importopts_t *jpc_dec_opts_create(const char *optstr)
317 {
318 jpc_dec_importopts_t *opts;
319 jas_tvparser_t *tvp;
320
321 opts = 0;
322
323 if (!(opts = jas_malloc(sizeof(jpc_dec_importopts_t)))) {
324 goto error;
325 }
326
327 opts->debug = 0;
328 opts->maxlyrs = JPC_MAXLYRS;
329 opts->maxpkts = -1;
330 opts->max_samples = JAS_DEC_DEFAULT_MAX_SAMPLES;
331
332 if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) {
333 goto error;
334 }
335
336 while (!jas_tvparser_next(tvp)) {
337 switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts,
338 jas_tvparser_gettag(tvp)))->id) {
339 case OPT_MAXLYRS:
340 opts->maxlyrs = atoi(jas_tvparser_getval(tvp));
341 break;
342 case OPT_DEBUG:
343 opts->debug = atoi(jas_tvparser_getval(tvp));
344 break;
345 case OPT_MAXPKTS:
346 opts->maxpkts = atoi(jas_tvparser_getval(tvp));
347 break;
348 case OPT_MAXSAMPLES:
349 opts->max_samples = strtoull(jas_tvparser_getval(tvp), 0, 10);
350 break;
351 default:
352 jas_eprintf("warning: ignoring invalid option %s\n",
353 jas_tvparser_gettag(tvp));
354 break;
355 }
356 }
357
358 jas_tvparser_destroy(tvp);
359
360 return opts;
361
362 error:
363 if (opts) {
364 jpc_dec_opts_destroy(opts);
365 }
366 return 0;
367 }
368
jpc_dec_opts_destroy(jpc_dec_importopts_t * opts)369 static void jpc_dec_opts_destroy(jpc_dec_importopts_t *opts)
370 {
371 jas_free(opts);
372 }
373
374 /******************************************************************************\
375 * Code for table-driven code stream decoder.
376 \******************************************************************************/
377
378 JAS_ATTRIBUTE_CONST
jpc_dec_mstab_lookup(uint_fast16_t id)379 static const jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id)
380 {
381 const jpc_dec_mstabent_t *mstabent;
382 for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) {
383 if (mstabent->id == id) {
384 break;
385 }
386 }
387 return mstabent;
388 }
389
jpc_dec_decode(jpc_dec_t * dec)390 static int jpc_dec_decode(jpc_dec_t *dec)
391 {
392 jpc_ms_t *ms;
393 const jpc_dec_mstabent_t *mstabent;
394 int ret;
395 jpc_cstate_t *cstate;
396
397 if (!(cstate = jpc_cstate_create())) {
398 return -1;
399 }
400 dec->cstate = cstate;
401
402 /* Initially, we should expect to encounter a SOC marker segment. */
403 dec->state = JPC_MHSOC;
404
405 for (;;) {
406
407 /* Get the next marker segment in the code stream. */
408 if (!(ms = jpc_getms(dec->in, cstate))) {
409 jas_eprintf("cannot get marker segment\n");
410 return -1;
411 }
412
413 mstabent = jpc_dec_mstab_lookup(ms->id);
414 assert(mstabent);
415
416 /* Ensure that this type of marker segment is permitted
417 at this point in the code stream. */
418 if (!(dec->state & mstabent->validstates)) {
419 jas_eprintf("unexpected marker segment type\n");
420 jpc_ms_destroy(ms);
421 return -1;
422 }
423
424 /* Process the marker segment. */
425 if (mstabent->action) {
426 ret = (*mstabent->action)(dec, ms);
427 } else {
428 /* No explicit action is required. */
429 ret = 0;
430 }
431
432 /* Destroy the marker segment. */
433 jpc_ms_destroy(ms);
434
435 if (ret < 0) {
436 return -1;
437 } else if (ret > 0) {
438 break;
439 }
440
441 }
442
443 return 0;
444 }
445
jpc_dec_process_crg(jpc_dec_t * dec,jpc_ms_t * ms)446 static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms)
447 {
448 jpc_dec_cmpt_t *cmpt;
449 jpc_crg_t *crg;
450
451 crg = &ms->parms.crg;
452 unsigned cmptno;
453 for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno,
454 ++cmpt) {
455 /* Ignore the information in the CRG marker segment for now.
456 This information serves no useful purpose for decoding anyhow.
457 Some other parts of the code need to be changed if these lines
458 are uncommented.
459 cmpt->hsubstep = crg->comps[cmptno].hoff;
460 cmpt->vsubstep = crg->comps[cmptno].voff;
461 */
462 /* suppress -Wunused-but-set-variable */
463 (void)crg;
464 }
465 return 0;
466 }
467
jpc_dec_process_soc(jpc_dec_t * dec,jpc_ms_t * ms)468 static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms)
469 {
470 /* Eliminate warnings about unused variables. */
471 (void)ms;
472
473 /* We should expect to encounter a SIZ marker segment next. */
474 dec->state = JPC_MHSIZ;
475
476 return 0;
477 }
478
jpc_dec_process_sot(jpc_dec_t * dec,jpc_ms_t * ms)479 static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms)
480 {
481 jpc_dec_tile_t *tile;
482 jpc_sot_t *sot = &ms->parms.sot;
483 jas_image_cmptparm_t *compinfos;
484 jas_image_cmptparm_t *compinfo;
485 jpc_dec_cmpt_t *cmpt;
486
487 if (dec->state == JPC_MH) {
488
489 if (!(compinfos = jas_alloc2(dec->numcomps,
490 sizeof(jas_image_cmptparm_t)))) {
491 return -1;
492 }
493 unsigned cmptno;
494 for (cmptno = 0, cmpt = dec->cmpts, compinfo = compinfos;
495 cmptno < dec->numcomps; ++cmptno, ++cmpt, ++compinfo) {
496 compinfo->tlx = 0;
497 compinfo->tly = 0;
498 compinfo->prec = cmpt->prec;
499 compinfo->sgnd = cmpt->sgnd;
500 compinfo->width = cmpt->width;
501 compinfo->height = cmpt->height;
502 compinfo->hstep = cmpt->hstep;
503 compinfo->vstep = cmpt->vstep;
504 }
505
506 if (!(dec->image = jas_image_create(dec->numcomps, compinfos,
507 JAS_CLRSPC_UNKNOWN))) {
508 jas_free(compinfos);
509 return -1;
510 }
511 jas_free(compinfos);
512
513 /* Is the packet header information stored in PPM marker segments in
514 the main header? */
515 if (dec->ppmstab) {
516 /* Convert the PPM marker segment data into a collection of streams
517 (one stream per tile-part). */
518 if (!(dec->pkthdrstreams = jpc_ppmstabtostreams(dec->ppmstab))) {
519 return -1;
520 }
521 jpc_ppxstab_destroy(dec->ppmstab);
522 dec->ppmstab = 0;
523 }
524 }
525
526 if (sot->len > 0) {
527 dec->curtileendoff = jas_stream_getrwcount(dec->in) - ms->len -
528 4 + sot->len;
529 } else {
530 dec->curtileendoff = 0;
531 }
532
533 if (sot->tileno >= dec->numtiles) {
534 jas_eprintf("invalid tile number in SOT marker segment\n");
535 return -1;
536 }
537 /* Set the current tile. */
538 dec->curtile = &dec->tiles[sot->tileno];
539 tile = dec->curtile;
540 /* Ensure that this is the expected part number. */
541 if (sot->partno != tile->partno) {
542 return -1;
543 }
544 if (tile->numparts > 0 && sot->partno >= tile->numparts) {
545 return -1;
546 }
547 if (!tile->numparts && sot->numparts > 0) {
548 tile->numparts = sot->numparts;
549 }
550
551 tile->pptstab = 0;
552
553 switch (tile->state) {
554 case JPC_TILE_INIT:
555 /* This is the first tile-part for this tile. */
556 tile->state = JPC_TILE_ACTIVE;
557 assert(!tile->cp);
558 if (!(tile->cp = jpc_dec_cp_copy(dec->cp))) {
559 return -1;
560 }
561 jpc_dec_cp_resetflags(dec->cp);
562 break;
563 default:
564 if (sot->numparts == sot->partno - 1) {
565 tile->state = JPC_TILE_ACTIVELAST;
566 }
567 break;
568 }
569
570 /* Note: We do not increment the expected tile-part number until
571 all processing for this tile-part is complete. */
572
573 /* We should expect to encounter other tile-part header marker
574 segments next. */
575 dec->state = JPC_TPH;
576
577 return 0;
578 }
579
jpc_dec_process_sod(jpc_dec_t * dec,jpc_ms_t * ms)580 static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms)
581 {
582 jpc_dec_tile_t *tile;
583 int pos;
584
585 /* Eliminate compiler warnings about unused variables. */
586 (void)ms;
587
588 if (!(tile = dec->curtile)) {
589 return -1;
590 }
591
592 if (!tile->partno) {
593 if (!jpc_dec_cp_isvalid(tile->cp)) {
594 return -1;
595 }
596 if (jpc_dec_cp_prepare(tile->cp)) {
597 return -1;
598 }
599 if (jpc_dec_tileinit(dec, tile)) {
600 return -1;
601 }
602 }
603
604 /* Are packet headers stored in the main header or tile-part header? */
605 if (dec->pkthdrstreams) {
606 /* Get the stream containing the packet header data for this
607 tile-part. */
608 if (!(tile->pkthdrstream = jpc_streamlist_remove(dec->pkthdrstreams, 0))) {
609 return -1;
610 }
611 }
612
613 if (tile->pptstab) {
614 if (!tile->pkthdrstream) {
615 if (!(tile->pkthdrstream = jas_stream_memopen(0, 0))) {
616 return -1;
617 }
618 }
619 pos = jas_stream_tell(tile->pkthdrstream);
620 jas_stream_seek(tile->pkthdrstream, 0, SEEK_END);
621 if (jpc_pptstabwrite(tile->pkthdrstream, tile->pptstab)) {
622 return -1;
623 }
624 jas_stream_seek(tile->pkthdrstream, pos, SEEK_SET);
625 jpc_ppxstab_destroy(tile->pptstab);
626 tile->pptstab = 0;
627 }
628
629 if (jas_getdbglevel() >= 10) {
630 jpc_dec_dump(dec, stderr);
631 }
632
633 if (jpc_dec_decodepkts(dec, (tile->pkthdrstream) ? tile->pkthdrstream :
634 dec->in, dec->in)) {
635 jas_eprintf("jpc_dec_decodepkts failed\n");
636 return -1;
637 }
638
639 /* Gobble any unconsumed tile data. */
640 if (dec->curtileendoff > 0) {
641 long curoff;
642 uint_fast32_t n;
643 curoff = jas_stream_getrwcount(dec->in);
644 if (curoff < dec->curtileendoff) {
645 n = dec->curtileendoff - curoff;
646 jas_eprintf("warning: ignoring trailing garbage (%lu bytes)\n",
647 (unsigned long) n);
648
649 while (n-- > 0) {
650 if (jas_stream_getc(dec->in) == EOF) {
651 jas_eprintf("read error\n");
652 return -1;
653 }
654 }
655 } else if (curoff > dec->curtileendoff) {
656 jas_eprintf("warning: not enough tile data (%lu bytes)\n",
657 (unsigned long) curoff - dec->curtileendoff);
658 }
659
660 }
661
662 if (tile->numparts > 0 && tile->partno == tile->numparts - 1) {
663 if (jpc_dec_tiledecode(dec, tile)) {
664 return -1;
665 }
666 jpc_dec_tilefini(dec, tile);
667 }
668
669 dec->curtile = 0;
670
671 /* Increment the expected tile-part number. */
672 ++tile->partno;
673
674 /* We should expect to encounter a SOT marker segment next. */
675 dec->state = JPC_TPHSOT;
676
677 return 0;
678 }
679
jpc_dec_tileinit(jpc_dec_t * dec,jpc_dec_tile_t * tile)680 static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile)
681 {
682 jpc_dec_tcomp_t *tcomp;
683 unsigned rlvlno;
684 jpc_dec_rlvl_t *rlvl;
685 jpc_dec_band_t *band;
686 jpc_dec_prc_t *prc;
687 jpc_dec_cblk_t *cblk;
688 uint_fast32_t tlprcxstart;
689 uint_fast32_t tlprcystart;
690 uint_fast32_t brprcxend;
691 uint_fast32_t brprcyend;
692 uint_fast32_t tlcbgxstart;
693 uint_fast32_t tlcbgystart;
694 uint_fast32_t brcbgxend;
695 uint_fast32_t cbgxstart;
696 uint_fast32_t cbgystart;
697 uint_fast32_t cbgxend;
698 uint_fast32_t cbgyend;
699 uint_fast32_t tlcblkxstart;
700 uint_fast32_t tlcblkystart;
701 uint_fast32_t brcblkxend;
702 uint_fast32_t brcblkyend;
703 uint_fast32_t cblkxstart;
704 uint_fast32_t cblkystart;
705 uint_fast32_t cblkxend;
706 uint_fast32_t cblkyend;
707 uint_fast32_t tmpxstart;
708 uint_fast32_t tmpystart;
709 uint_fast32_t tmpxend;
710 uint_fast32_t tmpyend;
711 jpc_tsfb_band_t bnds[JPC_MAXBANDS];
712 jpc_pchg_t *pchg;
713 int retval = 0;
714
715 if (jas_getdbglevel() >= 1) {
716 jas_eprintf("jpc_dec_tileinit called\n");
717 }
718
719 const jpc_dec_cp_t *cp = tile->cp;
720 tile->realmode = 0;
721 if (cp->mctid == JPC_MCT_ICT) {
722 tile->realmode = 1;
723 }
724
725 unsigned compno;
726 const jpc_dec_cmpt_t *cmpt;
727 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
728 dec->numcomps; ++compno, ++tcomp, ++cmpt) {
729 const jpc_dec_ccp_t *ccp = &tile->cp->ccps[compno];
730 if (jas_getdbglevel() >= 10) {
731 jas_eprintf("[compno %d]\n", compno);
732 }
733 if (ccp->qmfbid == JPC_COX_INS) {
734 tile->realmode = 1;
735 }
736 tcomp->numrlvls = ccp->numrlvls;
737 if (!(tcomp->rlvls = jas_alloc2(tcomp->numrlvls,
738 sizeof(jpc_dec_rlvl_t)))) {
739 retval = -1;
740 goto done;
741 }
742 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
743 ++rlvlno, ++rlvl) {
744 rlvl->bands = NULL;
745 }
746 if (!(tcomp->data = jas_seq2d_create(JPC_CEILDIV(tile->xstart,
747 cmpt->hstep), JPC_CEILDIV(tile->ystart, cmpt->vstep),
748 JPC_CEILDIV(tile->xend, cmpt->hstep), JPC_CEILDIV(tile->yend,
749 cmpt->vstep)))) {
750 retval = -1;
751 goto done;
752 }
753 if (!(tcomp->tsfb = jpc_cod_gettsfb(ccp->qmfbid,
754 tcomp->numrlvls - 1))) {
755 retval = -1;
756 goto done;
757 }
758 {
759 jpc_tsfb_getbands(tcomp->tsfb, jas_seq2d_xstart(tcomp->data),
760 jas_seq2d_ystart(tcomp->data), jas_seq2d_xend(tcomp->data),
761 jas_seq2d_yend(tcomp->data), bnds);
762 }
763 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
764 ++rlvlno, ++rlvl) {
765 rlvl->bands = 0;
766 rlvl->xstart = JPC_CEILDIVPOW2(tcomp->xstart,
767 tcomp->numrlvls - 1 - rlvlno);
768 rlvl->ystart = JPC_CEILDIVPOW2(tcomp->ystart,
769 tcomp->numrlvls - 1 - rlvlno);
770 rlvl->xend = JPC_CEILDIVPOW2(tcomp->xend,
771 tcomp->numrlvls - 1 - rlvlno);
772 rlvl->yend = JPC_CEILDIVPOW2(tcomp->yend,
773 tcomp->numrlvls - 1 - rlvlno);
774 rlvl->prcwidthexpn = ccp->prcwidthexpns[rlvlno];
775 rlvl->prcheightexpn = ccp->prcheightexpns[rlvlno];
776 tlprcxstart = JPC_FLOORDIVPOW2(rlvl->xstart,
777 rlvl->prcwidthexpn) << rlvl->prcwidthexpn;
778 tlprcystart = JPC_FLOORDIVPOW2(rlvl->ystart,
779 rlvl->prcheightexpn) << rlvl->prcheightexpn;
780 brprcxend = JPC_CEILDIVPOW2(rlvl->xend,
781 rlvl->prcwidthexpn) << rlvl->prcwidthexpn;
782 brprcyend = JPC_CEILDIVPOW2(rlvl->yend,
783 rlvl->prcheightexpn) << rlvl->prcheightexpn;
784 rlvl->numhprcs = (brprcxend - tlprcxstart) >>
785 rlvl->prcwidthexpn;
786 rlvl->numvprcs = (brprcyend - tlprcystart) >>
787 rlvl->prcheightexpn;
788 rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
789
790 if (jas_getdbglevel() >= 10) {
791 jas_eprintf(
792 "[compno %d rlvlno %d]: xstart %d ystart %d "
793 "xend %d yend %d prcwidthexpn %d prcheightexpn %d "
794 "numhprcs %d numvprcs %d numprcs %d\n",
795 compno, rlvlno, rlvl->xstart, rlvl->ystart, rlvl->xend,
796 rlvl->yend, rlvl->prcwidthexpn, rlvl->prcheightexpn,
797 rlvl->numhprcs, rlvl->numvprcs, rlvl->numprcs);
798 }
799
800 if (rlvl->numprcs >= 64 * 1024) {
801 /* avoid out-of-memory due to
802 malicious file; this limit is
803 rather arbitrary; "good" files I
804 have seen have values 1..12 */
805 return -1;
806 }
807
808 if (rlvl->xstart >= rlvl->xend || rlvl->ystart >= rlvl->yend) {
809 rlvl->bands = 0;
810 rlvl->numprcs = 0;
811 rlvl->numhprcs = 0;
812 rlvl->numvprcs = 0;
813 continue;
814 }
815 if (!rlvlno) {
816 tlcbgxstart = tlprcxstart;
817 tlcbgystart = tlprcystart;
818 brcbgxend = brprcxend;
819 rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
820 rlvl->cbgheightexpn = rlvl->prcheightexpn;
821 } else {
822 tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1);
823 tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1);
824 brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1);
825 if (rlvl->prcwidthexpn < 1 || rlvl->prcheightexpn < 1) {
826 /* the control block width/height offset
827 exponent must not be negative */
828 retval = -1;
829 goto done;
830 }
831 rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
832 rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
833 }
834 rlvl->cblkwidthexpn = JAS_MIN(ccp->cblkwidthexpn,
835 rlvl->cbgwidthexpn);
836 rlvl->cblkheightexpn = JAS_MIN(ccp->cblkheightexpn,
837 rlvl->cbgheightexpn);
838
839 rlvl->numbands = (!rlvlno) ? 1 : 3;
840 if (!(rlvl->bands = jas_alloc2(rlvl->numbands,
841 sizeof(jpc_dec_band_t)))) {
842 retval = -1;
843 goto done;
844 }
845 unsigned bandno;
846 for (bandno = 0, band = rlvl->bands;
847 bandno < rlvl->numbands; ++bandno, ++band) {
848 unsigned bndno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) +
849 bandno + 1);
850 const jpc_tsfb_band_t *bnd = &bnds[bndno];
851
852 if (jas_getdbglevel() >= 10) {
853 jas_eprintf("[compno %d rlvlno %d bandno %d]\n", compno, rlvlno, bandno);
854 }
855
856 band->orient = bnd->orient;
857 band->stepsize = ccp->stepsizes[bndno];
858 band->analgain = JPC_NOMINALGAIN(ccp->qmfbid,
859 tcomp->numrlvls - 1, rlvlno, band->orient);
860 band->absstepsize = jpc_calcabsstepsize(band->stepsize,
861 cmpt->prec + band->analgain);
862 band->numbps = ccp->numguardbits +
863 JPC_QCX_GETEXPN(band->stepsize) - 1;
864 band->roishift = (ccp->roishift + band->numbps >= JPC_PREC) ?
865 (JPC_PREC - 1 - band->numbps) : ccp->roishift;
866 band->data = 0;
867 band->prcs = 0;
868 if (bnd->xstart == bnd->xend || bnd->ystart == bnd->yend) {
869 continue;
870 }
871 if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
872 retval = -1;
873 goto done;
874 }
875 if (jas_seq2d_bindsub(band->data, tcomp->data, bnd->locxstart,
876 bnd->locystart, bnd->locxend, bnd->locyend)) {
877 retval = -1;
878 goto done;
879 }
880 jas_seq2d_setshift(band->data, bnd->xstart, bnd->ystart);
881
882 assert(rlvl->numprcs);
883
884 if (!(band->prcs = jas_alloc2(rlvl->numprcs,
885 sizeof(jpc_dec_prc_t)))) {
886 retval = -1;
887 goto done;
888 }
889
890 /************************************************/
891 cbgxstart = tlcbgxstart;
892 cbgystart = tlcbgystart;
893 unsigned prccnt;
894 for (prccnt = rlvl->numprcs, prc = band->prcs;
895 prccnt > 0; --prccnt, ++prc) {
896 if (jas_getdbglevel() >= 10) {
897 jas_eprintf("[compno %d rlvlno %d bandno %d prccnt %d]\n", compno, rlvlno, bandno, prccnt);
898 }
899 cbgxend = cbgxstart + (1 << rlvl->cbgwidthexpn);
900 cbgyend = cbgystart + (1 << rlvl->cbgheightexpn);
901 prc->xstart = JAS_MAX(cbgxstart, JAS_CAST(uint_fast32_t,
902 jas_seq2d_xstart(band->data)));
903 prc->ystart = JAS_MAX(cbgystart, JAS_CAST(uint_fast32_t,
904 jas_seq2d_ystart(band->data)));
905 prc->xend = JAS_MIN(cbgxend, JAS_CAST(uint_fast32_t,
906 jas_seq2d_xend(band->data)));
907 prc->yend = JAS_MIN(cbgyend, JAS_CAST(uint_fast32_t,
908 jas_seq2d_yend(band->data)));
909 if (prc->xend > prc->xstart && prc->yend > prc->ystart) {
910 tlcblkxstart = JPC_FLOORDIVPOW2(prc->xstart,
911 rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn;
912 tlcblkystart = JPC_FLOORDIVPOW2(prc->ystart,
913 rlvl->cblkheightexpn) << rlvl->cblkheightexpn;
914 brcblkxend = JPC_CEILDIVPOW2(prc->xend,
915 rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn;
916 brcblkyend = JPC_CEILDIVPOW2(prc->yend,
917 rlvl->cblkheightexpn) << rlvl->cblkheightexpn;
918 prc->numhcblks = (brcblkxend - tlcblkxstart) >>
919 rlvl->cblkwidthexpn;
920 prc->numvcblks = (brcblkyend - tlcblkystart) >>
921 rlvl->cblkheightexpn;
922 prc->numcblks = prc->numhcblks * prc->numvcblks;
923 assert(prc->numcblks > 0);
924
925 if (jas_getdbglevel() >= 10) {
926 jas_eprintf("[compno %d rlvlno %d bandno %d prccnt %d]: "
927 "numhcblks %d numvcblks %d numcblks %d\n",
928 compno, rlvlno, bandno, prccnt, prc->numhcblks, prc->numvcblks,
929 prc->numcblks);
930 }
931
932 if (!(prc->incltagtree = jpc_tagtree_create(
933 prc->numhcblks, prc->numvcblks))) {
934 retval = -1;
935 goto done;
936 }
937 if (!(prc->numimsbstagtree = jpc_tagtree_create(
938 prc->numhcblks, prc->numvcblks))) {
939 retval = -1;
940 goto done;
941 }
942 if (!(prc->cblks = jas_alloc2(prc->numcblks,
943 sizeof(jpc_dec_cblk_t)))) {
944 retval = -1;
945 goto done;
946 }
947
948 cblkxstart = cbgxstart;
949 cblkystart = cbgystart;
950 unsigned cblkcnt;
951 for (cblkcnt = prc->numcblks, cblk = prc->cblks;
952 cblkcnt > 0;) {
953 if (jas_getdbglevel() >= 10000) {
954 jas_eprintf("[compno %d rlvlno %d bandno %d prcno %d cblkcnt %d]\n", compno, rlvlno, bandno, prccnt, cblkcnt);
955 }
956 cblkxend = cblkxstart + (1 << rlvl->cblkwidthexpn);
957 cblkyend = cblkystart + (1 << rlvl->cblkheightexpn);
958 tmpxstart = JAS_MAX(cblkxstart, prc->xstart);
959 tmpystart = JAS_MAX(cblkystart, prc->ystart);
960 tmpxend = JAS_MIN(cblkxend, prc->xend);
961 tmpyend = JAS_MIN(cblkyend, prc->yend);
962 if (tmpxend > tmpxstart && tmpyend > tmpystart) {
963 cblk->firstpassno = -1;
964 cblk->numpasses = 0;
965 cblk->segs.head = 0;
966 cblk->segs.tail = 0;
967 cblk->curseg = 0;
968 cblk->numimsbs = 0;
969 cblk->numlenbits = 3;
970 if (!(cblk->data = jas_seq2d_create(0, 0, 0,
971 0))) {
972 retval = -1;
973 goto done;
974 }
975 if (jas_seq2d_bindsub(cblk->data, band->data,
976 tmpxstart, tmpystart, tmpxend, tmpyend)) {
977 retval = -1;
978 goto done;
979 }
980 ++cblk;
981 --cblkcnt;
982 }
983 cblkxstart += 1 << rlvl->cblkwidthexpn;
984 if (cblkxstart >= cbgxend) {
985 cblkxstart = cbgxstart;
986 cblkystart += 1 << rlvl->cblkheightexpn;
987 }
988 }
989
990 } else {
991 prc->cblks = 0;
992 prc->incltagtree = 0;
993 prc->numimsbstagtree = 0;
994 }
995 cbgxstart += 1 << rlvl->cbgwidthexpn;
996 if (cbgxstart >= brcbgxend) {
997 cbgxstart = tlcbgxstart;
998 cbgystart += 1 << rlvl->cbgheightexpn;
999 }
1000
1001 }
1002 /********************************************/
1003 }
1004 }
1005 }
1006
1007 if (!(tile->pi = jpc_dec_pi_create(dec, tile))) {
1008 retval = -1;
1009 goto done;
1010 }
1011
1012 for (unsigned pchgno = 0; pchgno < jpc_pchglist_numpchgs(tile->cp->pchglist);
1013 ++pchgno) {
1014 pchg = jpc_pchg_copy(jpc_pchglist_get(tile->cp->pchglist, pchgno));
1015 assert(pchg);
1016 jpc_pi_addpchg(tile->pi, pchg);
1017 }
1018 jpc_pi_init(tile->pi);
1019
1020 done:
1021
1022 if (jas_getdbglevel() >= 1) {
1023 jas_eprintf("jpc_dec_tileinit returning %d\n", retval);
1024 }
1025
1026 return retval;
1027 }
1028
jpc_dec_tilefini(jpc_dec_t * dec,jpc_dec_tile_t * tile)1029 static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile)
1030 {
1031 jpc_dec_tcomp_t *tcomp;
1032 unsigned rlvlno;
1033 jpc_dec_seg_t *seg;
1034 jpc_dec_cblk_t *cblk;
1035
1036 if (jas_getdbglevel() >= 1) {
1037 jas_eprintf("jpc_dec_tilefini called\n");
1038 }
1039
1040 if (tile->tcomps) {
1041
1042 unsigned compno;
1043 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1044 ++compno, ++tcomp) {
1045 const jpc_dec_rlvl_t *rlvl;
1046 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
1047 ++rlvlno, ++rlvl) {
1048 if (!rlvl->bands) {
1049 continue;
1050 }
1051 unsigned bandno;
1052 const jpc_dec_band_t *band;
1053 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
1054 ++bandno, ++band) {
1055 if (band->prcs) {
1056 unsigned prcno;
1057 const jpc_dec_prc_t *prc;
1058 for (prcno = 0, prc = band->prcs; prcno <
1059 rlvl->numprcs; ++prcno, ++prc) {
1060 if (!prc->cblks) {
1061 continue;
1062 }
1063 unsigned cblkno;
1064 for (cblkno = 0, cblk = prc->cblks; cblkno <
1065 prc->numcblks; ++cblkno, ++cblk) {
1066
1067 while (cblk->segs.head) {
1068 seg = cblk->segs.head;
1069 jpc_seglist_remove(&cblk->segs, seg);
1070 jpc_seg_destroy(seg);
1071 }
1072 jas_matrix_destroy(cblk->data);
1073 }
1074 if (prc->incltagtree) {
1075 jpc_tagtree_destroy(prc->incltagtree);
1076 }
1077 if (prc->numimsbstagtree) {
1078 jpc_tagtree_destroy(prc->numimsbstagtree);
1079 }
1080 if (prc->cblks) {
1081 jas_free(prc->cblks);
1082 }
1083 }
1084 }
1085 if (band->data) {
1086 jas_matrix_destroy(band->data);
1087 }
1088 if (band->prcs) {
1089 jas_free(band->prcs);
1090 }
1091 }
1092 if (rlvl->bands) {
1093 jas_free(rlvl->bands);
1094 }
1095 }
1096 if (tcomp->rlvls) {
1097 jas_free(tcomp->rlvls);
1098 }
1099 if (tcomp->data) {
1100 jas_matrix_destroy(tcomp->data);
1101 }
1102 if (tcomp->tsfb) {
1103 jpc_tsfb_destroy(tcomp->tsfb);
1104 }
1105 }
1106 }
1107
1108 if (tile->cp) {
1109 jpc_dec_cp_destroy(tile->cp);
1110 //tile->cp = 0;
1111 }
1112 if (tile->tcomps) {
1113 jas_free(tile->tcomps);
1114 //tile->tcomps = 0;
1115 }
1116 if (tile->pi) {
1117 jpc_pi_destroy(tile->pi);
1118 //tile->pi = 0;
1119 }
1120 if (tile->pkthdrstream) {
1121 jas_stream_close(tile->pkthdrstream);
1122 //tile->pkthdrstream = 0;
1123 }
1124 if (tile->pptstab) {
1125 jpc_ppxstab_destroy(tile->pptstab);
1126 //tile->pptstab = 0;
1127 }
1128
1129 tile->state = JPC_TILE_DONE;
1130
1131 return 0;
1132 }
1133
jpc_dec_tiledecode(jpc_dec_t * dec,jpc_dec_tile_t * tile)1134 static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile)
1135 {
1136 unsigned rlvlno;
1137 int v;
1138
1139 if (jpc_dec_decodecblks(dec, tile)) {
1140 jas_eprintf("jpc_dec_decodecblks failed\n");
1141 return -1;
1142 }
1143
1144 /* Perform dequantization. */
1145 unsigned compno;
1146 const jpc_dec_tcomp_t *tcomp;
1147 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1148 ++compno, ++tcomp) {
1149 const jpc_dec_ccp_t *ccp = &tile->cp->ccps[compno];
1150 const jpc_dec_rlvl_t *rlvl;
1151 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
1152 ++rlvlno, ++rlvl) {
1153 if (!rlvl->bands) {
1154 continue;
1155 }
1156 unsigned bandno;
1157 const jpc_dec_band_t *band;
1158 for (bandno = 0, band = rlvl->bands;
1159 bandno < rlvl->numbands; ++bandno, ++band) {
1160 if (!band->data) {
1161 continue;
1162 }
1163 jpc_undo_roi(band->data, band->roishift, ccp->roishift -
1164 band->roishift, band->numbps);
1165 if (ccp->qmfbid == JPC_COX_INS) {
1166 jas_matrix_asl(band->data, JPC_FIX_FRACBITS);
1167 jpc_dequantize(band->data, band->absstepsize);
1168 }
1169
1170 }
1171 }
1172 }
1173
1174 /* Apply an inverse wavelet transform if necessary. */
1175 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1176 ++compno, ++tcomp) {
1177 jpc_tsfb_synthesize(tcomp->tsfb, tcomp->data);
1178 }
1179
1180
1181 /* Apply an inverse intercomponent transform if necessary. */
1182 switch (tile->cp->mctid) {
1183 case JPC_MCT_RCT:
1184 if (dec->numcomps < 3) {
1185 jas_eprintf("RCT requires at least three components\n");
1186 return -1;
1187 }
1188 if (!jas_image_cmpt_domains_same(dec->image)) {
1189 jas_eprintf("RCT requires all components have the same domain\n");
1190 return -1;
1191 }
1192 jpc_irct(tile->tcomps[0].data, tile->tcomps[1].data,
1193 tile->tcomps[2].data);
1194 break;
1195 case JPC_MCT_ICT:
1196 if (dec->numcomps < 3) {
1197 jas_eprintf("ICT requires at least three components\n");
1198 return -1;
1199 }
1200 if (!jas_image_cmpt_domains_same(dec->image)) {
1201 jas_eprintf("RCT requires all components have the same domain\n");
1202 return -1;
1203 }
1204 jpc_iict(tile->tcomps[0].data, tile->tcomps[1].data,
1205 tile->tcomps[2].data);
1206 break;
1207 }
1208
1209 /* Perform rounding and convert to integer values. */
1210 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1211 ++compno, ++tcomp) {
1212 const jpc_dec_ccp_t *ccp = &tile->cp->ccps[compno];
1213 if (ccp->qmfbid == JPC_COX_INS) {
1214 jas_matrix_t *const data = tcomp->data;
1215 const jas_matind_t height = jas_matrix_numrows(data);
1216 const jas_matind_t numcols = jas_matrix_numcols(data);
1217 for (jas_matind_t i = 0; i < height; ++i) {
1218 jpc_fix_t *p = jas_matrix_getref(data, i, 0);
1219 for (jas_matind_t j = 0; j < numcols; ++j) {
1220 v = p[j];
1221 v = jpc_fix_round(v);
1222 p[j] = jpc_fixtoint(v);
1223 }
1224 }
1225 }
1226 }
1227
1228 /* Perform level shift. */
1229 const jpc_dec_cmpt_t *cmpt;
1230 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
1231 dec->numcomps; ++compno, ++tcomp, ++cmpt) {
1232 if (cmpt->sgnd)
1233 continue;
1234
1235 jas_matrix_t *const data = tcomp->data;
1236 const jas_matind_t width = jas_matrix_numcols(data);
1237 const jas_matind_t height = jas_matrix_numrows(data);
1238 const jas_seqent_t adjust = (jas_seqent_t)1 << (cmpt->prec - 1);
1239 for (jas_matind_t i = 0; i < height; ++i) {
1240 jpc_fix_t *p = jas_matrix_getref(data, i, 0);
1241 for (jas_matind_t j = 0; j < width; ++j) {
1242 p[j] += adjust;
1243 }
1244 }
1245 }
1246
1247 /* Perform clipping. */
1248 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
1249 dec->numcomps; ++compno, ++tcomp, ++cmpt) {
1250 if (cmpt->prec >= sizeof(jpc_fix_t) * 8 - 2 + cmpt->sgnd)
1251 /* no need to clip, because the calculated
1252 minimum/maximum values would overflow our
1253 integer type anyway */
1254 continue;
1255
1256 const jas_seqent_t mn = cmpt->sgnd
1257 ? (-((jpc_fix_t)1 << (cmpt->prec - 1)))
1258 : (0);
1259 const jas_seqent_t mx = cmpt->sgnd
1260 ? (((jpc_fix_t)1 << (cmpt->prec - 1)) - 1)
1261 : (((jpc_fix_t)1 << cmpt->prec) - 1);
1262 jas_matrix_clip(tcomp->data, mn, mx);
1263 }
1264
1265 /* XXX need to free tsfb struct */
1266
1267 /* Write the data for each component of the image. */
1268 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
1269 dec->numcomps; ++compno, ++tcomp, ++cmpt) {
1270 if (jas_image_writecmpt(dec->image, compno, tcomp->xstart -
1271 JPC_CEILDIV(dec->xstart, cmpt->hstep), tcomp->ystart -
1272 JPC_CEILDIV(dec->ystart, cmpt->vstep), jas_matrix_numcols(
1273 tcomp->data), jas_matrix_numrows(tcomp->data), tcomp->data)) {
1274 jas_eprintf("write component failed\n");
1275 return -1;
1276 }
1277 }
1278
1279 return 0;
1280 }
1281
jpc_dec_process_eoc(jpc_dec_t * dec,jpc_ms_t * ms)1282 static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms)
1283 {
1284 jpc_dec_tile_t *tile;
1285
1286 /* Eliminate compiler warnings about unused variables. */
1287 (void)ms;
1288
1289 unsigned tileno;
1290 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
1291 ++tile) {
1292 if (tile->state == JPC_TILE_ACTIVE ||
1293 tile->state == JPC_TILE_ACTIVELAST) {
1294 if (jpc_dec_tiledecode(dec, tile)) {
1295 return -1;
1296 }
1297 }
1298 /* If the tile has not yet been finalized, finalize it. */
1299 // OLD CODE: jpc_dec_tilefini(dec, tile);
1300 if (tile->state != JPC_TILE_DONE) {
1301 jpc_dec_tilefini(dec, tile);
1302 }
1303 }
1304
1305 /* We are done processing the code stream. */
1306 dec->state = JPC_MT;
1307
1308 return 1;
1309 }
1310
jpc_dec_process_siz(jpc_dec_t * dec,jpc_ms_t * ms)1311 static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms)
1312 {
1313 jpc_siz_t *siz = &ms->parms.siz;
1314 jpc_dec_tile_t *tile;
1315 jpc_dec_tcomp_t *tcomp;
1316 jpc_dec_cmpt_t *cmpt;
1317 size_t size;
1318 size_t num_samples;
1319 size_t num_samples_delta;
1320
1321 size_t total_samples;
1322 if (!jas_safe_size_mul(siz->width, siz->height, &total_samples) ||
1323 (dec->max_samples > 0 && total_samples > dec->max_samples)) {
1324 jas_eprintf("image too large\n");
1325 return -1;
1326 }
1327
1328 size_t tile_samples;
1329 if (!jas_safe_size_mul(siz->tilewidth, siz->tileheight, &tile_samples) ||
1330 (dec->max_samples > 0 && tile_samples > dec->max_samples)) {
1331 jas_eprintf("tile too large\n");
1332 return -1;
1333 }
1334
1335 dec->xstart = siz->xoff;
1336 dec->ystart = siz->yoff;
1337 dec->xend = siz->width;
1338 dec->yend = siz->height;
1339 dec->tilewidth = siz->tilewidth;
1340 dec->tileheight = siz->tileheight;
1341 dec->tilexoff = siz->tilexoff;
1342 dec->tileyoff = siz->tileyoff;
1343 dec->numcomps = siz->numcomps;
1344
1345 if (!(dec->cp = jpc_dec_cp_create(dec->numcomps))) {
1346 return -1;
1347 }
1348
1349 if (!(dec->cmpts = jas_alloc2(dec->numcomps, sizeof(jpc_dec_cmpt_t)))) {
1350 return -1;
1351 }
1352
1353 num_samples = 0;
1354 unsigned compno;
1355 for (compno = 0, cmpt = dec->cmpts; compno < dec->numcomps; ++compno,
1356 ++cmpt) {
1357 cmpt->prec = siz->comps[compno].prec;
1358 cmpt->sgnd = siz->comps[compno].sgnd;
1359 cmpt->hstep = siz->comps[compno].hsamp;
1360 cmpt->vstep = siz->comps[compno].vsamp;
1361 cmpt->width = JPC_CEILDIV(dec->xend, cmpt->hstep) -
1362 JPC_CEILDIV(dec->xstart, cmpt->hstep);
1363 cmpt->height = JPC_CEILDIV(dec->yend, cmpt->vstep) -
1364 JPC_CEILDIV(dec->ystart, cmpt->vstep);
1365 cmpt->hsubstep = 0;
1366 cmpt->vsubstep = 0;
1367
1368 if (!cmpt->width || !cmpt->height) {
1369 jas_eprintf("image component has no samples\n");
1370 return -1;
1371 }
1372 if (!jas_safe_size_mul(cmpt->width, cmpt->height, &num_samples_delta)) {
1373 jas_eprintf("image too large\n");
1374 return -1;
1375 }
1376 if (!jas_safe_size_add(num_samples, num_samples_delta, &num_samples)) {
1377 jas_eprintf("image too large\n");
1378 return -1;
1379 }
1380 }
1381
1382 if (dec->max_samples > 0 && num_samples > dec->max_samples) {
1383 jas_eprintf("maximum number of samples exceeded (%zu > %zu)\n",
1384 num_samples, dec->max_samples);
1385 return -1;
1386 }
1387
1388 dec->image = 0;
1389
1390 dec->numhtiles = JPC_CEILDIV(dec->xend - dec->tilexoff, dec->tilewidth);
1391 dec->numvtiles = JPC_CEILDIV(dec->yend - dec->tileyoff, dec->tileheight);
1392 if (!jas_safe_size_mul(dec->numhtiles, dec->numvtiles, &size) ||
1393 size > INT_MAX) {
1394 return -1;
1395 }
1396 if (dec->max_samples > 0 && size > dec->max_samples / 16 / 16) {
1397 /* avoid Denial of Service by a malicious input file
1398 with millions of tiny tiles; if max_samples is
1399 configured, then assume the tiles are at least
1400 16x16, and don't allow more than this number of
1401 tiles */
1402 return -1;
1403 }
1404 if (dec->max_samples > 0 && size > dec->max_samples / dec->numcomps / 16) {
1405 /* another DoS check: since each tile allocates an
1406 array of components, this check attempts to catch
1407 excessive tile*component numbers */
1408 return -1;
1409 }
1410 dec->numtiles = size;
1411 JAS_DBGLOG(10, ("numtiles = %d; numhtiles = %d; numvtiles = %d;\n",
1412 dec->numtiles, dec->numhtiles, dec->numvtiles));
1413 if (!(dec->tiles = jas_alloc2(dec->numtiles, sizeof(jpc_dec_tile_t)))) {
1414 return -1;
1415 }
1416
1417 unsigned tileno;
1418 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
1419 ++tile) {
1420 /* initialize all tiles with JPC_TILE_DONE so
1421 jpc_dec_destroy() knows which ones need a
1422 jpc_dec_tilefini() call; they are not actually
1423 "done", of course */
1424 tile->state = JPC_TILE_DONE;
1425 }
1426
1427 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
1428 ++tile) {
1429 const unsigned htileno = tileno % dec->numhtiles;
1430 const unsigned vtileno = tileno / dec->numhtiles;
1431 tile->realmode = 0;
1432 tile->state = JPC_TILE_INIT;
1433 tile->xstart = JAS_MAX(dec->tilexoff + htileno * dec->tilewidth,
1434 dec->xstart);
1435 tile->ystart = JAS_MAX(dec->tileyoff + vtileno * dec->tileheight,
1436 dec->ystart);
1437 tile->xend = JAS_MIN(dec->tilexoff + (htileno + 1) *
1438 dec->tilewidth, dec->xend);
1439 tile->yend = JAS_MIN(dec->tileyoff + (vtileno + 1) *
1440 dec->tileheight, dec->yend);
1441 tile->numparts = 0;
1442 tile->partno = 0;
1443 tile->pkthdrstream = 0;
1444 tile->pptstab = 0;
1445 tile->cp = 0;
1446 tile->pi = 0;
1447 if (!(tile->tcomps = jas_alloc2(dec->numcomps,
1448 sizeof(jpc_dec_tcomp_t)))) {
1449 return -1;
1450 }
1451 for (compno = 0, cmpt = dec->cmpts, tcomp = tile->tcomps;
1452 compno < dec->numcomps; ++compno, ++cmpt, ++tcomp) {
1453 tcomp->rlvls = 0;
1454 tcomp->numrlvls = 0;
1455 tcomp->data = 0;
1456 tcomp->xstart = JPC_CEILDIV(tile->xstart, cmpt->hstep);
1457 tcomp->ystart = JPC_CEILDIV(tile->ystart, cmpt->vstep);
1458 tcomp->xend = JPC_CEILDIV(tile->xend, cmpt->hstep);
1459 tcomp->yend = JPC_CEILDIV(tile->yend, cmpt->vstep);
1460 tcomp->tsfb = 0;
1461 }
1462 }
1463
1464 dec->pkthdrstreams = 0;
1465
1466 /* We should expect to encounter other main header marker segments
1467 or an SOT marker segment next. */
1468 dec->state = JPC_MH;
1469
1470 return 0;
1471 }
1472
jpc_dec_process_cod(jpc_dec_t * dec,jpc_ms_t * ms)1473 static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms)
1474 {
1475 const jpc_cod_t *cod = &ms->parms.cod;
1476 jpc_dec_tile_t *tile;
1477
1478 switch (dec->state) {
1479 case JPC_MH:
1480 jpc_dec_cp_setfromcod(dec->cp, cod);
1481 break;
1482 case JPC_TPH:
1483 if (!(tile = dec->curtile)) {
1484 return -1;
1485 }
1486 if (tile->partno != 0) {
1487 return -1;
1488 }
1489 jpc_dec_cp_setfromcod(tile->cp, cod);
1490 break;
1491 }
1492 return 0;
1493 }
1494
jpc_dec_process_coc(jpc_dec_t * dec,jpc_ms_t * ms)1495 static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms)
1496 {
1497 const jpc_coc_t *coc = &ms->parms.coc;
1498 jpc_dec_tile_t *tile;
1499
1500 if (coc->compno >= dec->numcomps) {
1501 jas_eprintf("invalid component number in COC marker segment\n");
1502 return -1;
1503 }
1504 switch (dec->state) {
1505 case JPC_MH:
1506 jpc_dec_cp_setfromcoc(dec->cp, coc);
1507 break;
1508 case JPC_TPH:
1509 if (!(tile = dec->curtile)) {
1510 return -1;
1511 }
1512 if (tile->partno > 0) {
1513 return -1;
1514 }
1515 jpc_dec_cp_setfromcoc(tile->cp, coc);
1516 break;
1517 }
1518 return 0;
1519 }
1520
jpc_dec_process_rgn(jpc_dec_t * dec,jpc_ms_t * ms)1521 static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms)
1522 {
1523 const jpc_rgn_t *rgn = &ms->parms.rgn;
1524 jpc_dec_tile_t *tile;
1525
1526 if (rgn->compno >= dec->numcomps) {
1527 jas_eprintf("invalid component number in RGN marker segment\n");
1528 return -1;
1529 }
1530 switch (dec->state) {
1531 case JPC_MH:
1532 jpc_dec_cp_setfromrgn(dec->cp, rgn);
1533 break;
1534 case JPC_TPH:
1535 if (!(tile = dec->curtile)) {
1536 return -1;
1537 }
1538 if (tile->partno > 0) {
1539 return -1;
1540 }
1541 jpc_dec_cp_setfromrgn(tile->cp, rgn);
1542 break;
1543 }
1544
1545 return 0;
1546 }
1547
jpc_dec_process_qcd(jpc_dec_t * dec,jpc_ms_t * ms)1548 static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms)
1549 {
1550 const jpc_qcd_t *qcd = &ms->parms.qcd;
1551 jpc_dec_tile_t *tile;
1552
1553 switch (dec->state) {
1554 case JPC_MH:
1555 jpc_dec_cp_setfromqcd(dec->cp, qcd);
1556 break;
1557 case JPC_TPH:
1558 if (!(tile = dec->curtile)) {
1559 return -1;
1560 }
1561 if (tile->partno > 0) {
1562 return -1;
1563 }
1564 jpc_dec_cp_setfromqcd(tile->cp, qcd);
1565 break;
1566 }
1567 return 0;
1568 }
1569
jpc_dec_process_qcc(jpc_dec_t * dec,jpc_ms_t * ms)1570 static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms)
1571 {
1572 const jpc_qcc_t *qcc = &ms->parms.qcc;
1573 jpc_dec_tile_t *tile;
1574
1575 if (qcc->compno >= dec->numcomps) {
1576 jas_eprintf("invalid component number in QCC marker segment\n");
1577 return -1;
1578 }
1579 switch (dec->state) {
1580 case JPC_MH:
1581 jpc_dec_cp_setfromqcc(dec->cp, qcc);
1582 break;
1583 case JPC_TPH:
1584 if (!(tile = dec->curtile)) {
1585 return -1;
1586 }
1587 if (tile->partno > 0) {
1588 return -1;
1589 }
1590 jpc_dec_cp_setfromqcc(tile->cp, qcc);
1591 break;
1592 }
1593 return 0;
1594 }
1595
jpc_dec_process_poc(jpc_dec_t * dec,jpc_ms_t * ms)1596 static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms)
1597 {
1598 const jpc_poc_t *poc = &ms->parms.poc;
1599 jpc_dec_tile_t *tile;
1600 switch (dec->state) {
1601 case JPC_MH:
1602 if (jpc_dec_cp_setfrompoc(dec->cp, poc, 1)) {
1603 return -1;
1604 }
1605 break;
1606 case JPC_TPH:
1607 if (!(tile = dec->curtile)) {
1608 return -1;
1609 }
1610 if (!tile->partno) {
1611 if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) {
1612 return -1;
1613 }
1614 } else {
1615 jpc_pi_addpchgfrompoc(tile->pi, poc);
1616 }
1617 break;
1618 }
1619 return 0;
1620 }
1621
jpc_dec_process_ppm(jpc_dec_t * dec,jpc_ms_t * ms)1622 static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms)
1623 {
1624 jpc_ppm_t *ppm = &ms->parms.ppm;
1625 jpc_ppxstabent_t *ppmstabent;
1626
1627 if (!dec->ppmstab) {
1628 if (!(dec->ppmstab = jpc_ppxstab_create())) {
1629 return -1;
1630 }
1631 }
1632
1633 if (!(ppmstabent = jpc_ppxstabent_create())) {
1634 return -1;
1635 }
1636 ppmstabent->ind = ppm->ind;
1637 ppmstabent->data = ppm->data;
1638 ppm->data = 0;
1639 ppmstabent->len = ppm->len;
1640 if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) {
1641 jpc_ppxstabent_destroy(ppmstabent);
1642 return -1;
1643 }
1644 return 0;
1645 }
1646
jpc_dec_process_ppt(jpc_dec_t * dec,jpc_ms_t * ms)1647 static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms)
1648 {
1649 jpc_ppt_t *ppt = &ms->parms.ppt;
1650 jpc_dec_tile_t *tile;
1651 jpc_ppxstabent_t *pptstabent;
1652
1653 tile = dec->curtile;
1654 if (!tile->pptstab) {
1655 if (!(tile->pptstab = jpc_ppxstab_create())) {
1656 return -1;
1657 }
1658 }
1659 if (!(pptstabent = jpc_ppxstabent_create())) {
1660 return -1;
1661 }
1662 pptstabent->ind = ppt->ind;
1663 pptstabent->data = ppt->data;
1664 ppt->data = 0;
1665 pptstabent->len = ppt->len;
1666 if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) {
1667 jpc_ppxstabent_destroy(pptstabent);
1668 return -1;
1669 }
1670 return 0;
1671 }
1672
jpc_dec_process_com(jpc_dec_t * dec,jpc_ms_t * ms)1673 static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms)
1674 {
1675 /* Eliminate compiler warnings about unused variables. */
1676 (void)dec;
1677 (void)ms;
1678
1679 return 0;
1680 }
1681
jpc_dec_process_unk(jpc_dec_t * dec,jpc_ms_t * ms)1682 static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms)
1683 {
1684 /* Eliminate compiler warnings about unused variables. */
1685 (void)dec;
1686
1687 jas_eprintf("warning: ignoring unknown marker segment (0x%x)\n",
1688 ms->id);
1689 return 0;
1690 }
1691
1692 /******************************************************************************\
1693 *
1694 \******************************************************************************/
1695
jpc_dec_cp_create(uint_fast16_t numcomps)1696 static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps)
1697 {
1698 jpc_dec_cp_t *cp;
1699 jpc_dec_ccp_t *ccp;
1700
1701 if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) {
1702 return 0;
1703 }
1704 cp->flags = 0;
1705 cp->numcomps = numcomps;
1706 cp->prgord = 0;
1707 cp->numlyrs = 0;
1708 cp->mctid = 0;
1709 cp->csty = 0;
1710 if (!(cp->ccps = jas_alloc2(cp->numcomps, sizeof(jpc_dec_ccp_t)))) {
1711 goto error;
1712 }
1713 if (!(cp->pchglist = jpc_pchglist_create())) {
1714 goto error;
1715 }
1716 unsigned compno;
1717 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1718 ++compno, ++ccp) {
1719 ccp->flags = 0;
1720 ccp->numrlvls = 0;
1721 ccp->cblkwidthexpn = 0;
1722 ccp->cblkheightexpn = 0;
1723 ccp->qmfbid = 0;
1724 ccp->numstepsizes = 0;
1725 ccp->numguardbits = 0;
1726 ccp->roishift = 0;
1727 ccp->cblkctx = 0;
1728 }
1729 return cp;
1730 error:
1731 if (cp) {
1732 jpc_dec_cp_destroy(cp);
1733 }
1734 return 0;
1735 }
1736
jpc_dec_cp_copy(const jpc_dec_cp_t * cp)1737 static jpc_dec_cp_t *jpc_dec_cp_copy(const jpc_dec_cp_t *cp)
1738 {
1739 jpc_dec_cp_t *newcp;
1740 jpc_dec_ccp_t *newccp;
1741 const jpc_dec_ccp_t *ccp;
1742
1743 if (!(newcp = jpc_dec_cp_create(cp->numcomps))) {
1744 return 0;
1745 }
1746 newcp->flags = cp->flags;
1747 newcp->prgord = cp->prgord;
1748 newcp->numlyrs = cp->numlyrs;
1749 newcp->mctid = cp->mctid;
1750 newcp->csty = cp->csty;
1751 jpc_pchglist_destroy(newcp->pchglist);
1752 newcp->pchglist = 0;
1753 if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) {
1754 jas_free(newcp);
1755 return 0;
1756 }
1757 unsigned compno;
1758 for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps;
1759 compno < cp->numcomps;
1760 ++compno, ++newccp, ++ccp) {
1761 *newccp = *ccp;
1762 }
1763 return newcp;
1764 }
1765
jpc_dec_cp_resetflags(jpc_dec_cp_t * cp)1766 static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp)
1767 {
1768 jpc_dec_ccp_t *ccp;
1769 cp->flags &= (JPC_CSET | JPC_QSET);
1770
1771 unsigned compno;
1772 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1773 ++compno, ++ccp) {
1774 ccp->flags = 0;
1775 }
1776 }
1777
jpc_dec_cp_destroy(jpc_dec_cp_t * cp)1778 static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp)
1779 {
1780 if (cp->ccps) {
1781 jas_free(cp->ccps);
1782 }
1783 if (cp->pchglist) {
1784 jpc_pchglist_destroy(cp->pchglist);
1785 }
1786 jas_free(cp);
1787 }
1788
jpc_dec_cp_isvalid(const jpc_dec_cp_t * cp)1789 static int jpc_dec_cp_isvalid(const jpc_dec_cp_t *cp)
1790 {
1791 uint_fast16_t compcnt;
1792 const jpc_dec_ccp_t *ccp;
1793
1794 if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) {
1795 return 0;
1796 }
1797 for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt,
1798 ++ccp) {
1799 /* Is there enough step sizes for the number of bands? */
1800 if ((ccp->qsty != JPC_QCX_SIQNT && JAS_CAST(int, ccp->numstepsizes) < 3 *
1801 ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT &&
1802 ccp->numstepsizes != 1)) {
1803 return 0;
1804 }
1805 }
1806 return 1;
1807 }
1808
calcstepsizes(uint_fast16_t refstepsize,unsigned numrlvls,uint_fast16_t * stepsizes)1809 static int calcstepsizes(uint_fast16_t refstepsize, unsigned numrlvls,
1810 uint_fast16_t *stepsizes)
1811 {
1812 uint_fast16_t expn;
1813 uint_fast16_t mant;
1814 expn = JPC_QCX_GETEXPN(refstepsize);
1815 mant = JPC_QCX_GETMANT(refstepsize);
1816 const unsigned numbands = 3 * numrlvls - 2;
1817 for (unsigned bandno = 0; bandno < numbands; ++bandno) {
1818 //jas_eprintf("DEBUG %d %d %d %d %d\n", bandno, expn, numrlvls, bandno, ((numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3) : (0)))));
1819 const unsigned r = (bandno + 2) / 3;
1820 const unsigned nb = (r == 0) ? (numrlvls - 1) - r : (numrlvls - 1) - r + 1;
1821 uint_fast16_t e = expn - (numrlvls - 1) + nb;
1822 if (e >= 0x20)
1823 return -1;
1824 stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(e);
1825 }
1826 return 0;
1827 }
1828
jpc_dec_cp_prepare(jpc_dec_cp_t * cp)1829 static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp)
1830 {
1831 jpc_dec_ccp_t *ccp;
1832 unsigned compno;
1833 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1834 ++compno, ++ccp) {
1835 if (!(ccp->csty & JPC_COX_PRT)) {
1836 for (unsigned i = 0; i < JPC_MAXRLVLS; ++i) {
1837 ccp->prcwidthexpns[i] = 15;
1838 ccp->prcheightexpns[i] = 15;
1839 }
1840 }
1841 if (ccp->qsty == JPC_QCX_SIQNT) {
1842 if (calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes)) {
1843 return -1;
1844 }
1845 }
1846 }
1847 return 0;
1848 }
1849
jpc_dec_cp_setfromcod(jpc_dec_cp_t * cp,const jpc_cod_t * cod)1850 static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, const jpc_cod_t *cod)
1851 {
1852 jpc_dec_ccp_t *ccp;
1853 cp->flags |= JPC_CSET;
1854 cp->prgord = cod->prg;
1855 if (cod->mctrans) {
1856 cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT);
1857 } else {
1858 cp->mctid = JPC_MCT_NONE;
1859 }
1860 cp->numlyrs = cod->numlyrs;
1861 cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH);
1862 unsigned compno;
1863 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1864 ++compno, ++ccp) {
1865 jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0);
1866 }
1867 cp->flags |= JPC_CSET;
1868 return 0;
1869 }
1870
jpc_dec_cp_setfromcoc(jpc_dec_cp_t * cp,const jpc_coc_t * coc)1871 static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, const jpc_coc_t *coc)
1872 {
1873 jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC);
1874 return 0;
1875 }
1876
jpc_dec_cp_setfromcox(jpc_dec_cp_t * cp,jpc_dec_ccp_t * ccp,const jpc_coxcp_t * compparms,unsigned flags)1877 static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
1878 const jpc_coxcp_t *compparms, unsigned flags)
1879 {
1880 int rlvlno;
1881
1882 /* Eliminate compiler warnings about unused variables. */
1883 (void)cp;
1884
1885 if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) {
1886 ccp->numrlvls = compparms->numdlvls + 1;
1887 ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN(
1888 compparms->cblkwidthval);
1889 ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN(
1890 compparms->cblkheightval);
1891 ccp->qmfbid = compparms->qmfbid;
1892 ccp->cblkctx = compparms->cblksty;
1893 ccp->csty = compparms->csty & JPC_COX_PRT;
1894 for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) {
1895 ccp->prcwidthexpns[rlvlno] =
1896 compparms->rlvls[rlvlno].parwidthval;
1897 ccp->prcheightexpns[rlvlno] =
1898 compparms->rlvls[rlvlno].parheightval;
1899 }
1900 ccp->flags |= flags | JPC_CSET;
1901 }
1902 return 0;
1903 }
1904
jpc_dec_cp_setfromqcd(jpc_dec_cp_t * cp,const jpc_qcd_t * qcd)1905 static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, const jpc_qcd_t *qcd)
1906 {
1907 unsigned compno;
1908 jpc_dec_ccp_t *ccp;
1909 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1910 ++compno, ++ccp) {
1911 jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0);
1912 }
1913 cp->flags |= JPC_QSET;
1914 return 0;
1915 }
1916
jpc_dec_cp_setfromqcc(jpc_dec_cp_t * cp,const jpc_qcc_t * qcc)1917 static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, const jpc_qcc_t *qcc)
1918 {
1919 return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC);
1920 }
1921
jpc_dec_cp_setfromqcx(jpc_dec_cp_t * cp,jpc_dec_ccp_t * ccp,const jpc_qcxcp_t * compparms,unsigned flags)1922 static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
1923 const jpc_qcxcp_t *compparms, unsigned flags)
1924 {
1925 int bandno;
1926
1927 /* Eliminate compiler warnings about unused variables. */
1928 (void)cp;
1929
1930 /* Sanity check to prevent buffer overflow */
1931 if (compparms->numstepsizes > (3 * JPC_MAXRLVLS + 1)) {
1932 return -1;
1933 }
1934
1935 if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) {
1936 ccp->flags |= flags | JPC_QSET;
1937 for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) {
1938 ccp->stepsizes[bandno] = compparms->stepsizes[bandno];
1939 }
1940 ccp->numstepsizes = compparms->numstepsizes;
1941 ccp->numguardbits = compparms->numguard;
1942 ccp->qsty = compparms->qntsty;
1943 }
1944 return 0;
1945 }
1946
jpc_dec_cp_setfromrgn(jpc_dec_cp_t * cp,const jpc_rgn_t * rgn)1947 static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, const jpc_rgn_t *rgn)
1948 {
1949 jpc_dec_ccp_t *ccp;
1950 ccp = &cp->ccps[rgn->compno];
1951 ccp->roishift = rgn->roishift;
1952 return 0;
1953 }
1954
jpc_pi_addpchgfrompoc(jpc_pi_t * pi,const jpc_poc_t * poc)1955 static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, const jpc_poc_t *poc)
1956 {
1957 int pchgno;
1958 jpc_pchg_t *pchg;
1959 for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) {
1960 if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) {
1961 return -1;
1962 }
1963 if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) {
1964 return -1;
1965 }
1966 }
1967 return 0;
1968 }
1969
jpc_dec_cp_setfrompoc(jpc_dec_cp_t * cp,const jpc_poc_t * poc,int reset)1970 static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, const jpc_poc_t *poc, int reset)
1971 {
1972 int pchgno;
1973 jpc_pchg_t *pchg;
1974 if (reset) {
1975 while (jpc_pchglist_numpchgs(cp->pchglist) > 0) {
1976 pchg = jpc_pchglist_remove(cp->pchglist, 0);
1977 jpc_pchg_destroy(pchg);
1978 }
1979 }
1980 for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) {
1981 if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) {
1982 return -1;
1983 }
1984 if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) {
1985 return -1;
1986 }
1987 }
1988 return 0;
1989 }
1990
jpc_calcabsstepsize(unsigned stepsize,unsigned numbits)1991 static jpc_fix_t jpc_calcabsstepsize(unsigned stepsize, unsigned numbits)
1992 {
1993 jpc_fix_t absstepsize;
1994 int n;
1995
1996 absstepsize = jpc_inttofix(1);
1997 n = JPC_FIX_FRACBITS - 11;
1998 absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) :
1999 (JPC_QCX_GETMANT(stepsize) >> (-n));
2000 n = numbits - JPC_QCX_GETEXPN(stepsize);
2001 absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n));
2002 return absstepsize;
2003 }
2004
jpc_dequantize(jas_matrix_t * x,jpc_fix_t absstepsize)2005 static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize)
2006 {
2007 // a reconstruction parameter defined in E 1.1.2 of the ISO/IEC 15444-1
2008 jpc_fix_t recparam = JPC_FIX_HALF;
2009
2010 // Note:
2011 // Should anything special be done to handle the error case of
2012 // absstepsize < 0 (which can arise due to numerical overflow).
2013 // Using an assert is not appropriate, since this results in program
2014 // termination when this type of error occurs.
2015 // Simply allowing a decoded image with very high distortion would appear
2016 // to be consistent with the policy adopted elsewhere in the codec.
2017 // assert(absstepsize >= 0);
2018
2019 if (absstepsize == jpc_inttofix(1)) {
2020 return;
2021 }
2022
2023 const jas_matind_t height = jas_matrix_numrows(x);
2024 const size_t width = jas_matrix_numcols(x);
2025
2026 for (jas_matind_t i = 0; i < height; ++i) {
2027 jpc_fix_t *p = jas_matrix_getref(x, i, 0);
2028 for (size_t j = 0; j < width; ++j) {
2029 jas_seqent_t t = p[j];
2030 if (t) {
2031 // mid-point reconstruction
2032 t = (t > 0) ? jpc_fix_add(t, recparam) : jpc_fix_sub(t, recparam);
2033 t = jpc_fix_mul(t, absstepsize);
2034 p[j] = t;
2035 }
2036 }
2037 }
2038
2039 }
2040
jpc_undo_roi(jas_matrix_t * x,int roishift,int bgshift,unsigned numbps)2041 static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, unsigned numbps)
2042 {
2043 int thresh;
2044 jpc_fix_t val;
2045 jpc_fix_t mag;
2046 bool warn;
2047 uint_fast32_t mask;
2048
2049 if (roishift < 0) {
2050 /* We could instead return an error here. */
2051 /* I do not think it matters much. */
2052 jas_eprintf("warning: forcing negative ROI shift to zero "
2053 "(bitstream is probably corrupt)\n");
2054 roishift = 0;
2055 }
2056 if (roishift == 0 && bgshift == 0) {
2057 return;
2058 }
2059 thresh = 1 << roishift;
2060
2061 warn = false;
2062
2063 const jas_matind_t width = jas_matrix_numcols(x);
2064 const jas_matind_t height = jas_matrix_numrows(x);
2065 for (jas_matind_t i = 0; i < height; ++i) {
2066 jpc_fix_t *p = jas_matrix_getref(x, i, 0);
2067 for (jas_matind_t j = 0; j < width; ++j, ++p) {
2068 val = *p;
2069 mag = JAS_ABS(val);
2070 if (mag >= thresh) {
2071 /* We are dealing with ROI data. */
2072 mag >>= roishift;
2073 val = (val < 0) ? (-mag) : mag;
2074 *p = val;
2075 } else {
2076 /* We are dealing with non-ROI (i.e., background) data. */
2077 mag <<= bgshift;
2078 mask = (JAS_CAST(uint_fast32_t, 1) << numbps) - 1;
2079 /* Perform a basic sanity check on the sample value. */
2080 /* Some implementations write garbage in the unused
2081 most-significant bit planes introduced by ROI shifting.
2082 Here we ensure that any such bits are masked off. */
2083 if (mag & (~mask)) {
2084 if (!warn) {
2085 jas_eprintf("warning: possibly corrupt code stream\n");
2086 warn = true;
2087 }
2088 mag &= mask;
2089 }
2090 val = (val < 0) ? (-mag) : mag;
2091 *p = val;
2092 }
2093 }
2094 }
2095 }
2096
jpc_dec_create(jpc_dec_importopts_t * impopts,jas_stream_t * in)2097 static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in)
2098 {
2099 jpc_dec_t *dec;
2100
2101 if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) {
2102 return 0;
2103 }
2104
2105 dec->image = 0;
2106 dec->xstart = 0;
2107 dec->ystart = 0;
2108 dec->xend = 0;
2109 dec->yend = 0;
2110 dec->tilewidth = 0;
2111 dec->tileheight = 0;
2112 dec->tilexoff = 0;
2113 dec->tileyoff = 0;
2114 dec->numhtiles = 0;
2115 dec->numvtiles = 0;
2116 dec->numtiles = 0;
2117 dec->tiles = 0;
2118 dec->curtile = 0;
2119 dec->numcomps = 0;
2120 dec->in = in;
2121 dec->cp = 0;
2122 dec->maxlyrs = impopts->maxlyrs;
2123 dec->maxpkts = impopts->maxpkts;
2124 dec->numpkts = 0;
2125 dec->ppmseqno = 0;
2126 dec->state = 0;
2127 dec->cmpts = 0;
2128 dec->pkthdrstreams = 0;
2129 dec->ppmstab = 0;
2130 dec->curtileendoff = 0;
2131 dec->max_samples = impopts->max_samples;
2132
2133 if (jas_getdbglevel() >= 1) {
2134 jas_eprintf("debug %d\n", jas_getdbglevel());
2135 }
2136
2137 return dec;
2138 }
2139
jpc_dec_destroy(jpc_dec_t * dec)2140 static void jpc_dec_destroy(jpc_dec_t *dec)
2141 {
2142 if (dec->cstate) {
2143 jpc_cstate_destroy(dec->cstate);
2144 }
2145 if (dec->pkthdrstreams) {
2146 jpc_streamlist_destroy(dec->pkthdrstreams);
2147 }
2148 if (dec->ppmstab) {
2149 jpc_ppxstab_destroy(dec->ppmstab);
2150 }
2151 if (dec->image) {
2152 jas_image_destroy(dec->image);
2153 }
2154
2155 if (dec->cp) {
2156 jpc_dec_cp_destroy(dec->cp);
2157 }
2158
2159 if (dec->cmpts) {
2160 jas_free(dec->cmpts);
2161 }
2162
2163 if (dec->tiles) {
2164 unsigned tileno;
2165 jpc_dec_tile_t *tile;
2166
2167 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
2168 ++tile) {
2169 if (tile->state != JPC_TILE_DONE) {
2170 jpc_dec_tilefini(dec, tile);
2171 }
2172 }
2173
2174 jas_free(dec->tiles);
2175 }
2176
2177 jas_free(dec);
2178 }
2179
2180 /******************************************************************************\
2181 *
2182 \******************************************************************************/
2183
jpc_seglist_insert(jpc_dec_seglist_t * list,jpc_dec_seg_t * ins,jpc_dec_seg_t * node)2184 void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, jpc_dec_seg_t *node)
2185 {
2186 jpc_dec_seg_t *prev;
2187 jpc_dec_seg_t *next;
2188
2189 prev = ins;
2190 node->prev = prev;
2191 next = prev ? (prev->next) : 0;
2192 node->prev = prev;
2193 node->next = next;
2194 if (prev) {
2195 prev->next = node;
2196 } else {
2197 list->head = node;
2198 }
2199 if (next) {
2200 next->prev = node;
2201 } else {
2202 list->tail = node;
2203 }
2204 }
2205
jpc_seglist_remove(jpc_dec_seglist_t * list,jpc_dec_seg_t * seg)2206 void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *seg)
2207 {
2208 jpc_dec_seg_t *prev;
2209 jpc_dec_seg_t *next;
2210
2211 prev = seg->prev;
2212 next = seg->next;
2213 if (prev) {
2214 prev->next = next;
2215 } else {
2216 list->head = next;
2217 }
2218 if (next) {
2219 next->prev = prev;
2220 } else {
2221 list->tail = prev;
2222 }
2223 seg->prev = 0;
2224 seg->next = 0;
2225 }
2226
jpc_seg_alloc()2227 jpc_dec_seg_t *jpc_seg_alloc()
2228 {
2229 jpc_dec_seg_t *seg;
2230
2231 if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) {
2232 return 0;
2233 }
2234 seg->prev = 0;
2235 seg->next = 0;
2236 seg->passno = -1;
2237 seg->numpasses = 0;
2238 seg->maxpasses = 0;
2239 seg->type = JPC_SEG_INVALID;
2240 seg->stream = 0;
2241 seg->cnt = 0;
2242 seg->complete = 0;
2243 seg->lyrno = -1;
2244 return seg;
2245 }
2246
jpc_seg_destroy(jpc_dec_seg_t * seg)2247 void jpc_seg_destroy(jpc_dec_seg_t *seg)
2248 {
2249 if (seg->stream) {
2250 jas_stream_close(seg->stream);
2251 }
2252 jas_free(seg);
2253 }
2254
jpc_dec_dump(const jpc_dec_t * dec,FILE * out)2255 static int jpc_dec_dump(const jpc_dec_t *dec, FILE *out)
2256 {
2257 assert(!dec->numtiles || dec->tiles);
2258 unsigned tileno;
2259 const jpc_dec_tile_t *tile;
2260 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles;
2261 ++tileno, ++tile) {
2262 assert(!dec->numcomps || tile->tcomps);
2263 unsigned compno;
2264 const jpc_dec_tcomp_t *tcomp;
2265 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
2266 ++compno, ++tcomp) {
2267 unsigned rlvlno;
2268 const jpc_dec_rlvl_t *rlvl;
2269 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno <
2270 tcomp->numrlvls; ++rlvlno, ++rlvl) {
2271 fprintf(out, "RESOLUTION LEVEL %d\n", rlvlno);
2272 fprintf(out, "xs = %"PRIuFAST32", ys = %"PRIuFAST32", xe = %"PRIuFAST32", ye = %"PRIuFAST32", w = %"PRIuFAST32", h = %"PRIuFAST32"\n",
2273 rlvl->xstart, rlvl->ystart, rlvl->xend, rlvl->yend,
2274 rlvl->xend - rlvl->xstart, rlvl->yend - rlvl->ystart);
2275 assert(!rlvl->numbands || rlvl->bands);
2276 unsigned bandno;
2277 const jpc_dec_band_t *band;
2278 for (bandno = 0, band = rlvl->bands;
2279 bandno < rlvl->numbands; ++bandno, ++band) {
2280 fprintf(out, "BAND %d\n", bandno);
2281 if (!band->data) {
2282 fprintf(out, "band has no data (null pointer)\n");
2283 assert(!band->prcs);
2284 continue;
2285 }
2286 fprintf(out, "xs = %"PRIjas_seqent", ys = %"PRIjas_seqent", xe = %"PRIjas_seqent", ye = %"PRIjas_seqent", w = %"PRIjas_seqent", h = %"PRIjas_seqent"\n",
2287 jas_seq2d_xstart(band->data),
2288 jas_seq2d_ystart(band->data),
2289 jas_seq2d_xend(band->data),
2290 jas_seq2d_yend(band->data),
2291 jas_seq2d_xend(band->data) -
2292 jas_seq2d_xstart(band->data),
2293 jas_seq2d_yend(band->data) -
2294 jas_seq2d_ystart(band->data));
2295 assert(!rlvl->numprcs || band->prcs);
2296 unsigned prcno;
2297 const jpc_dec_prc_t *prc;
2298 for (prcno = 0, prc = band->prcs;
2299 prcno < rlvl->numprcs; ++prcno,
2300 ++prc) {
2301 fprintf(out, "CODE BLOCK GROUP %d\n", prcno);
2302 fprintf(out, "xs = %"PRIuFAST32", ys = %"PRIuFAST32", xe = %"PRIuFAST32", ye = %"PRIuFAST32", w = %"PRIuFAST32", h = %"PRIuFAST32"\n",
2303 prc->xstart, prc->ystart, prc->xend, prc->yend,
2304 prc->xend - prc->xstart, prc->yend - prc->ystart);
2305 assert(!prc->numcblks || prc->cblks);
2306 unsigned cblkno;
2307 const jpc_dec_cblk_t *cblk;
2308 for (cblkno = 0, cblk =
2309 prc->cblks; cblkno <
2310 prc->numcblks; ++cblkno,
2311 ++cblk) {
2312 fprintf(out, "CODE BLOCK %d\n", cblkno);
2313 fprintf(out, "xs = %"PRIjas_seqent", ys = %"PRIjas_seqent", xe = %"PRIjas_seqent", ye = %"PRIjas_seqent", w = %"PRIjas_seqent", h = %"PRIjas_seqent"\n",
2314 jas_seq2d_xstart(cblk->data),
2315 jas_seq2d_ystart(cblk->data),
2316 jas_seq2d_xend(cblk->data),
2317 jas_seq2d_yend(cblk->data),
2318 jas_seq2d_xend(cblk->data) -
2319 jas_seq2d_xstart(cblk->data),
2320 jas_seq2d_yend(cblk->data) -
2321 jas_seq2d_ystart(cblk->data));
2322 }
2323 }
2324 }
2325 }
2326 }
2327 }
2328
2329 return 0;
2330 }
2331
jpc_streamlist_create()2332 static jpc_streamlist_t *jpc_streamlist_create()
2333 {
2334 jpc_streamlist_t *streamlist;
2335
2336 if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) {
2337 return 0;
2338 }
2339 streamlist->numstreams = 0;
2340 streamlist->maxstreams = 100;
2341 if (!(streamlist->streams = jas_alloc2(streamlist->maxstreams,
2342 sizeof(jas_stream_t *)))) {
2343 jas_free(streamlist);
2344 return 0;
2345 }
2346 for (unsigned i = 0; i < streamlist->maxstreams; ++i) {
2347 streamlist->streams[i] = 0;
2348 }
2349 return streamlist;
2350 }
2351
jpc_streamlist_insert(jpc_streamlist_t * streamlist,unsigned streamno,jas_stream_t * stream)2352 static int jpc_streamlist_insert(jpc_streamlist_t *streamlist, unsigned streamno,
2353 jas_stream_t *stream)
2354 {
2355 jas_stream_t **newstreams;
2356 /* Grow the array of streams if necessary. */
2357 if (streamlist->numstreams >= streamlist->maxstreams) {
2358 const unsigned newmaxstreams = streamlist->maxstreams + 1024;
2359 if (!(newstreams = jas_realloc2(streamlist->streams,
2360 (newmaxstreams + 1024), sizeof(jas_stream_t *)))) {
2361 return -1;
2362 }
2363 for (unsigned i = streamlist->numstreams; i < streamlist->maxstreams; ++i) {
2364 streamlist->streams[i] = 0;
2365 }
2366 streamlist->maxstreams = newmaxstreams;
2367 streamlist->streams = newstreams;
2368 }
2369 if (streamno != streamlist->numstreams) {
2370 /* Can only handle insertion at start of list. */
2371 return -1;
2372 }
2373 streamlist->streams[streamno] = stream;
2374 ++streamlist->numstreams;
2375 return 0;
2376 }
2377
jpc_streamlist_remove(jpc_streamlist_t * streamlist,unsigned streamno)2378 static jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, unsigned streamno)
2379 {
2380 assert(streamno < streamlist->numstreams);
2381
2382 jas_stream_t *stream;
2383 stream = streamlist->streams[streamno];
2384 for (unsigned i = streamno + 1; i < streamlist->numstreams; ++i) {
2385 streamlist->streams[i - 1] = streamlist->streams[i];
2386 }
2387 --streamlist->numstreams;
2388 return stream;
2389 }
2390
jpc_streamlist_destroy(jpc_streamlist_t * streamlist)2391 static void jpc_streamlist_destroy(jpc_streamlist_t *streamlist)
2392 {
2393 if (streamlist->streams) {
2394 for (unsigned streamno = 0; streamno < streamlist->numstreams;
2395 ++streamno) {
2396 jas_stream_close(streamlist->streams[streamno]);
2397 }
2398 jas_free(streamlist->streams);
2399 }
2400 jas_free(streamlist);
2401 }
2402
jpc_streamlist_numstreams(jpc_streamlist_t * streamlist)2403 static int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist)
2404 {
2405 return streamlist->numstreams;
2406 }
2407
jpc_ppxstab_create()2408 static jpc_ppxstab_t *jpc_ppxstab_create()
2409 {
2410 jpc_ppxstab_t *tab;
2411
2412 if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) {
2413 return 0;
2414 }
2415 tab->numents = 0;
2416 tab->maxents = 0;
2417 tab->ents = 0;
2418 return tab;
2419 }
2420
jpc_ppxstab_destroy(jpc_ppxstab_t * tab)2421 static void jpc_ppxstab_destroy(jpc_ppxstab_t *tab)
2422 {
2423 for (unsigned i = 0; i < tab->numents; ++i) {
2424 jpc_ppxstabent_destroy(tab->ents[i]);
2425 }
2426 if (tab->ents) {
2427 jas_free(tab->ents);
2428 }
2429 jas_free(tab);
2430 }
2431
jpc_ppxstab_grow(jpc_ppxstab_t * tab,unsigned maxents)2432 static int jpc_ppxstab_grow(jpc_ppxstab_t *tab, unsigned maxents)
2433 {
2434 jpc_ppxstabent_t **newents;
2435 if (tab->maxents < maxents) {
2436 newents = (tab->ents) ? jas_realloc2(tab->ents, maxents,
2437 sizeof(jpc_ppxstabent_t *)) : jas_alloc2(maxents, sizeof(jpc_ppxstabent_t *));
2438 if (!newents) {
2439 return -1;
2440 }
2441 tab->ents = newents;
2442 tab->maxents = maxents;
2443 }
2444 return 0;
2445 }
2446
jpc_ppxstab_insert(jpc_ppxstab_t * tab,jpc_ppxstabent_t * ent)2447 static int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent)
2448 {
2449 unsigned i;
2450
2451 for (i = 0; i < tab->numents; ++i) {
2452 if (tab->ents[i]->ind > ent->ind) {
2453 break;
2454 }
2455 }
2456 const unsigned inspt = i;
2457
2458 if (tab->numents >= tab->maxents) {
2459 if (jpc_ppxstab_grow(tab, tab->maxents + 128)) {
2460 return -1;
2461 }
2462 }
2463
2464 for (i = tab->numents; i > inspt; --i) {
2465 tab->ents[i] = tab->ents[i - 1];
2466 }
2467 tab->ents[i] = ent;
2468 ++tab->numents;
2469
2470 return 0;
2471 }
2472
jpc_ppmstabtostreams(jpc_ppxstab_t * tab)2473 static jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab)
2474 {
2475 jpc_streamlist_t *streams;
2476 uint_fast32_t datacnt;
2477 uint_fast32_t tpcnt;
2478 jas_stream_t *stream;
2479
2480 if (!(streams = jpc_streamlist_create())) {
2481 goto error;
2482 }
2483
2484 if (!tab->numents) {
2485 return streams;
2486 }
2487
2488 unsigned entno = 0;
2489 const jpc_ppxstabent_t *ent = tab->ents[entno];
2490 const jas_uchar *dataptr = ent->data;
2491 datacnt = ent->len;
2492 for (;;) {
2493
2494 /* Get the length of the packet header data for the current
2495 tile-part. */
2496 if (datacnt < 4) {
2497 goto error;
2498 }
2499 if (!(stream = jas_stream_memopen(0, 0))) {
2500 goto error;
2501 }
2502 if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams),
2503 stream)) {
2504 goto error;
2505 }
2506 tpcnt = ((uint_least32_t)dataptr[0] << 24) | ((uint_least32_t)dataptr[1] << 16) | ((uint_least32_t)dataptr[2] << 8)
2507 | (uint_least32_t)dataptr[3];
2508 datacnt -= 4;
2509 dataptr += 4;
2510
2511 /* Get the packet header data for the current tile-part. */
2512 while (tpcnt) {
2513 if (!datacnt) {
2514 if (++entno >= tab->numents) {
2515 goto error;
2516 }
2517 ent = tab->ents[entno];
2518 dataptr = ent->data;
2519 datacnt = ent->len;
2520 }
2521 const size_t n = JAS_MIN(tpcnt, datacnt);
2522 if (jas_stream_write(stream, dataptr, n) != n) {
2523 goto error;
2524 }
2525 tpcnt -= n;
2526 dataptr += n;
2527 datacnt -= n;
2528 }
2529 jas_stream_rewind(stream);
2530 if (!datacnt) {
2531 if (++entno >= tab->numents) {
2532 break;
2533 }
2534 ent = tab->ents[entno];
2535 dataptr = ent->data;
2536 datacnt = ent->len;
2537 }
2538 }
2539
2540 return streams;
2541
2542 error:
2543 if (streams) {
2544 jpc_streamlist_destroy(streams);
2545 }
2546 return 0;
2547 }
2548
jpc_pptstabwrite(jas_stream_t * out,jpc_ppxstab_t * tab)2549 static int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab)
2550 {
2551 for (unsigned i = 0; i < tab->numents; ++i) {
2552 const jpc_ppxstabent_t *ent = tab->ents[i];
2553 if (jas_stream_write(out, ent->data, ent->len) != ent->len) {
2554 return -1;
2555 }
2556 }
2557 return 0;
2558 }
2559
jpc_ppxstabent_create()2560 static jpc_ppxstabent_t *jpc_ppxstabent_create()
2561 {
2562 jpc_ppxstabent_t *ent;
2563 if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) {
2564 return 0;
2565 }
2566 ent->data = 0;
2567 ent->len = 0;
2568 ent->ind = 0;
2569 return ent;
2570 }
2571
jpc_ppxstabent_destroy(jpc_ppxstabent_t * ent)2572 static void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent)
2573 {
2574 if (ent->data) {
2575 jas_free(ent->data);
2576 }
2577 jas_free(ent);
2578 }
2579