Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_dec.c

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