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