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  * Tier 2 Decoder
115  *
116  * $Id$
117  */
118 
119 /******************************************************************************\
120 * Includes.
121 \******************************************************************************/
122 
123 #include <stdio.h>
124 #include <stdlib.h>
125 #include <assert.h>
126 
127 #include "jasper/jas_types.h"
128 #include "jasper/jas_fix.h"
129 #include "jasper/jas_malloc.h"
130 #include "jasper/jas_math.h"
131 #include "jasper/jas_stream.h"
132 #include "jasper/jas_debug.h"
133 
134 #include "jpc_bs.h"
135 #include "jpc_dec.h"
136 #include "jpc_cs.h"
137 #include "jpc_mqdec.h"
138 #include "jpc_t2dec.h"
139 #include "jpc_t1cod.h"
140 #include "jpc_math.h"
141 
142 /******************************************************************************\
143 *
144 \******************************************************************************/
145 
146 long jpc_dec_lookahead(jas_stream_t *in);
147 static int jpc_getcommacode(jpc_bitstream_t *in);
148 static int jpc_getnumnewpasses(jpc_bitstream_t *in);
149 static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int lvlno,
150   int prcno, int lyrno);
151 
152 /******************************************************************************\
153 * Code.
154 \******************************************************************************/
155 
jpc_getcommacode(jpc_bitstream_t * in)156 static int jpc_getcommacode(jpc_bitstream_t *in)
157 {
158 	int n;
159 	int v;
160 
161 	n = 0;
162 	for (;;) {
163 		if ((v = jpc_bitstream_getbit(in)) < 0) {
164 			return -1;
165 		}
166 		if (jpc_bitstream_eof(in)) {
167 			return -1;
168 		}
169 		if (!v) {
170 			break;
171 		}
172 		++n;
173 	}
174 
175 	return n;
176 }
177 
jpc_getnumnewpasses(jpc_bitstream_t * in)178 static int jpc_getnumnewpasses(jpc_bitstream_t *in)
179 {
180 	int n;
181 
182 	if ((n = jpc_bitstream_getbit(in)) > 0) {
183 		if ((n = jpc_bitstream_getbit(in)) > 0) {
184 			if ((n = jpc_bitstream_getbits(in, 2)) == 3) {
185 				if ((n = jpc_bitstream_getbits(in, 5)) == 31) {
186 					if ((n = jpc_bitstream_getbits(in, 7)) >= 0) {
187 						n += 36 + 1;
188 					}
189 				} else if (n >= 0) {
190 					n += 5 + 1;
191 				}
192 			} else if (n >= 0) {
193 				n += 2 + 1;
194 			}
195 		} else if (!n) {
196 			n += 2;
197 		}
198 	} else if (!n) {
199 		++n;
200 	}
201 
202 	return n;
203 }
204 
jpc_dec_decodepkt(jpc_dec_t * dec,jas_stream_t * pkthdrstream,jas_stream_t * in,int compno,int rlvlno,int prcno,int lyrno)205 static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int rlvlno,
206   int prcno, int lyrno)
207 {
208 	jpc_bitstream_t *inb;
209 	jpc_dec_tcomp_t *tcomp;
210 	jpc_dec_rlvl_t *rlvl;
211 	jpc_dec_band_t *band;
212 	jpc_dec_cblk_t *cblk;
213 	int n;
214 	int m;
215 	int i;
216 	jpc_tagtreenode_t *leaf;
217 	int included;
218 	int ret;
219 	int numnewpasses;
220 	jpc_dec_seg_t *seg;
221 	int len;
222 	int present;
223 	int savenumnewpasses;
224 	int mycounter;
225 	jpc_ms_t *ms;
226 	jpc_dec_tile_t *tile;
227 	jpc_dec_ccp_t *ccp;
228 	jpc_dec_cp_t *cp;
229 	int bandno;
230 	jpc_dec_prc_t *prc;
231 	int usedcblkcnt;
232 	int cblkno;
233 	uint_fast32_t bodylen;
234 	bool discard;
235 	int passno;
236 	int maxpasses;
237 	int hdrlen;
238 	int hdroffstart;
239 	int hdroffend;
240 
241 	discard = (lyrno >= dec->maxlyrs);
242 
243 	tile = dec->curtile;
244 	cp = tile->cp;
245 	ccp = &cp->ccps[compno];
246 
247 	/*
248 	 * Decode the packet header.
249 	 */
250 
251 	/* Decode the SOP marker segment if present. */
252 	if (cp->csty & JPC_COD_SOP) {
253 		if (jpc_dec_lookahead(in) == JPC_MS_SOP) {
254 			if (!(ms = jpc_getms(in, dec->cstate))) {
255 				return -1;
256 			}
257 			if (jpc_ms_gettype(ms) != JPC_MS_SOP) {
258 				jpc_ms_destroy(ms);
259 				fprintf(stderr, "missing SOP marker segment\n");
260 				return -1;
261 			}
262 			jpc_ms_destroy(ms);
263 		}
264 	}
265 
266 hdroffstart = jas_stream_getrwcount(pkthdrstream);
267 
268 	if (!(inb = jpc_bitstream_sopen(pkthdrstream, "r"))) {
269 		return -1;
270 	}
271 
272 	if ((present = jpc_bitstream_getbit(inb)) < 0) {
273 		return 1;
274 	}
275 	JAS_DBGLOG(10, ("\n", present));
276 	JAS_DBGLOG(10, ("present=%d ", present));
277 
278 	/* Is the packet non-empty? */
279 	if (present) {
280 		/* The packet is non-empty. */
281 		tcomp = &tile->tcomps[compno];
282 		rlvl = &tcomp->rlvls[rlvlno];
283 		bodylen = 0;
284 		for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
285 		  ++bandno, ++band) {
286 			if (!band->data) {
287 				continue;
288 			}
289 			prc = &band->prcs[prcno];
290 			if (!prc->cblks) {
291 				continue;
292 			}
293 			usedcblkcnt = 0;
294 			for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
295 			  ++cblkno, ++cblk) {
296 				++usedcblkcnt;
297 				if (!cblk->numpasses) {
298 					leaf = jpc_tagtree_getleaf(prc->incltagtree, usedcblkcnt - 1);
299 					if ((included = jpc_tagtree_decode(prc->incltagtree, leaf, lyrno + 1, inb)) < 0) {
300 						return -1;
301 					}
302 				} else {
303 					if ((included = jpc_bitstream_getbit(inb)) < 0) {
304 						return -1;
305 					}
306 				}
307 				JAS_DBGLOG(10, ("\n"));
308 				JAS_DBGLOG(10, ("included=%d ", included));
309 				if (!included) {
310 					continue;
311 				}
312 				if (!cblk->numpasses) {
313 					i = 1;
314 					leaf = jpc_tagtree_getleaf(prc->numimsbstagtree, usedcblkcnt - 1);
315 					for (;;) {
316 						if ((ret = jpc_tagtree_decode(prc->numimsbstagtree, leaf, i, inb)) < 0) {
317 							return -1;
318 						}
319 						if (ret) {
320 							break;
321 						}
322 						++i;
323 					}
324 					cblk->numimsbs = i - 1;
325 					cblk->firstpassno = cblk->numimsbs * 3;
326 				}
327 				if ((numnewpasses = jpc_getnumnewpasses(inb)) < 0) {
328 					return -1;
329 				}
330 				JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses));
331 				seg = cblk->curseg;
332 				savenumnewpasses = numnewpasses;
333 				mycounter = 0;
334 				if (numnewpasses > 0) {
335 					if ((m = jpc_getcommacode(inb)) < 0) {
336 						return -1;
337 					}
338 					cblk->numlenbits += m;
339 					JAS_DBGLOG(10, ("increment=%d ", m));
340 					while (numnewpasses > 0) {
341 						passno = cblk->firstpassno + cblk->numpasses + mycounter;
342 	/* XXX - the maxpasses is not set precisely but this doesn't matter... */
343 						maxpasses = JPC_SEGPASSCNT(passno, cblk->firstpassno, 10000, (ccp->cblkctx & JPC_COX_LAZY) != 0, (ccp->cblkctx & JPC_COX_TERMALL) != 0);
344 						if (!discard && !seg) {
345 							if (!(seg = jpc_seg_alloc())) {
346 								return -1;
347 							}
348 							jpc_seglist_insert(&cblk->segs, cblk->segs.tail, seg);
349 							if (!cblk->curseg) {
350 								cblk->curseg = seg;
351 							}
352 							seg->passno = passno;
353 							seg->type = JPC_SEGTYPE(seg->passno, cblk->firstpassno, (ccp->cblkctx & JPC_COX_LAZY) != 0);
354 							seg->maxpasses = maxpasses;
355 						}
356 						n = JAS_MIN(numnewpasses, maxpasses);
357 						mycounter += n;
358 						numnewpasses -= n;
359 						if ((len = jpc_bitstream_getbits(inb, cblk->numlenbits + jpc_floorlog2(n))) < 0) {
360 							return -1;
361 						}
362 						JAS_DBGLOG(10, ("len=%d ", len));
363 						if (!discard) {
364 							seg->lyrno = lyrno;
365 							seg->numpasses += n;
366 							seg->cnt = len;
367 							seg = seg->next;
368 						}
369 						bodylen += len;
370 					}
371 				}
372 				cblk->numpasses += savenumnewpasses;
373 			}
374 		}
375 
376 		jpc_bitstream_inalign(inb, 0, 0);
377 
378 	} else {
379 		if (jpc_bitstream_inalign(inb, 0x7f, 0)) {
380 			fprintf(stderr, "alignment failed\n");
381 			return -1;
382 		}
383 	}
384 	jpc_bitstream_close(inb);
385 
386 	hdroffend = jas_stream_getrwcount(pkthdrstream);
387 	hdrlen = hdroffend - hdroffstart;
388 	if (jas_getdbglevel() >= 5) {
389 		fprintf(stderr, "hdrlen=%lu bodylen=%lu \n", (unsigned long) hdrlen,
390 		  (unsigned long) bodylen);
391 	}
392 
393 	if (cp->csty & JPC_COD_EPH) {
394 		if (jpc_dec_lookahead(pkthdrstream) == JPC_MS_EPH) {
395 			if (!(ms = jpc_getms(pkthdrstream, dec->cstate))) {
396 				fprintf(stderr, "cannot get (EPH) marker segment\n");
397 				return -1;
398 			}
399 			if (jpc_ms_gettype(ms) != JPC_MS_EPH) {
400 				jpc_ms_destroy(ms);
401 				fprintf(stderr, "missing EPH marker segment\n");
402 				return -1;
403 			}
404 			jpc_ms_destroy(ms);
405 		}
406 	}
407 
408 	/* decode the packet body. */
409 
410 	if (jas_getdbglevel() >= 1) {
411 		fprintf(stderr, "packet body offset=%06ld\n", (long) jas_stream_getrwcount(in));
412 	}
413 
414 	if (!discard) {
415 		tcomp = &tile->tcomps[compno];
416 		rlvl = &tcomp->rlvls[rlvlno];
417 		for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
418 		  ++bandno, ++band) {
419 			if (!band->data) {
420 				continue;
421 			}
422 			prc = &band->prcs[prcno];
423 			if (!prc->cblks) {
424 				continue;
425 			}
426 			for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
427 			  ++cblkno, ++cblk) {
428 				seg = cblk->curseg;
429 				while (seg) {
430 					if (!seg->stream) {
431 						if (!(seg->stream = jas_stream_memopen(0, 0))) {
432 							return -1;
433 						}
434 					}
435 #if 0
436 fprintf(stderr, "lyrno=%02d, compno=%02d, lvlno=%02d, prcno=%02d, bandno=%02d, cblkno=%02d, passno=%02d numpasses=%02d cnt=%d numbps=%d, numimsbs=%d\n", lyrno, compno, rlvlno, prcno, band - rlvl->bands, cblk - prc->cblks, seg->passno, seg->numpasses, seg->cnt, band->numbps, cblk->numimsbs);
437 #endif
438 					if (seg->cnt > 0) {
439 						if (jpc_getdata(in, seg->stream, seg->cnt) < 0) {
440 							return -1;
441 						}
442 						seg->cnt = 0;
443 					}
444 					if (seg->numpasses >= seg->maxpasses) {
445 						cblk->curseg = seg->next;
446 					}
447 					seg = seg->next;
448 				}
449 			}
450 		}
451 	} else {
452 		if (jas_stream_gobble(in, bodylen) != bodylen) {
453 			return -1;
454 		}
455 	}
456 	return 0;
457 }
458 
459 /********************************************************************************************/
460 /********************************************************************************************/
461 
jpc_dec_decodepkts(jpc_dec_t * dec,jas_stream_t * pkthdrstream,jas_stream_t * in)462 int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in)
463 {
464 	jpc_dec_tile_t *tile;
465 	jpc_pi_t *pi;
466 	int ret;
467 
468 	tile = dec->curtile;
469 	pi = tile->pi;
470 	for (;;) {
471 if (!tile->pkthdrstream || jas_stream_peekc(tile->pkthdrstream) == EOF) {
472 		switch (jpc_dec_lookahead(in)) {
473 		case JPC_MS_EOC:
474 		case JPC_MS_SOT:
475 			return 0;
476 			break;
477 		case JPC_MS_SOP:
478 		case JPC_MS_EPH:
479 		case 0:
480 			break;
481 		default:
482 			return -1;
483 			break;
484 		}
485 }
486 		if ((ret = jpc_pi_next(pi))) {
487 			return ret;
488 		}
489 if (dec->maxpkts >= 0 && dec->numpkts >= dec->maxpkts) {
490 	fprintf(stderr, "warning: stopping decode prematurely as requested\n");
491 	return 0;
492 }
493 		if (jas_getdbglevel() >= 1) {
494 			fprintf(stderr, "packet offset=%08ld prg=%d cmptno=%02d "
495 			  "rlvlno=%02d prcno=%03d lyrno=%02d\n", (long)
496 			  jas_stream_getrwcount(in), jpc_pi_prg(pi), jpc_pi_cmptno(pi),
497 			  jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi));
498 		}
499 		if (jpc_dec_decodepkt(dec, pkthdrstream, in, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi),
500 		  jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) {
501 			return -1;
502 		}
503 ++dec->numpkts;
504 	}
505 
506 	return 0;
507 }
508 
jpc_dec_pi_create(jpc_dec_t * dec,jpc_dec_tile_t * tile)509 jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile)
510 {
511 	jpc_pi_t *pi;
512 	int compno;
513 	jpc_picomp_t *picomp;
514 	jpc_pirlvl_t *pirlvl;
515 	jpc_dec_tcomp_t *tcomp;
516 	int rlvlno;
517 	jpc_dec_rlvl_t *rlvl;
518 	int prcno;
519 	int *prclyrno;
520 	jpc_dec_cmpt_t *cmpt;
521 
522 	if (!(pi = jpc_pi_create0())) {
523 		return 0;
524 	}
525 	pi->numcomps = dec->numcomps;
526 	if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) {
527 		jpc_pi_destroy(pi);
528 		return 0;
529 	}
530 	for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno,
531 	  ++picomp) {
532 		picomp->pirlvls = 0;
533 	}
534 
535 	for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps;
536 	  compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
537 		picomp->numrlvls = tcomp->numrlvls;
538 		if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls *
539 		  sizeof(jpc_pirlvl_t)))) {
540 			jpc_pi_destroy(pi);
541 			return 0;
542 		}
543 		for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
544 		  picomp->numrlvls; ++rlvlno, ++pirlvl) {
545 			pirlvl->prclyrnos = 0;
546 		}
547 		for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
548 		  rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
549 /* XXX sizeof(long) should be sizeof different type */
550 			pirlvl->numprcs = rlvl->numprcs;
551 			if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs *
552 			  sizeof(long)))) {
553 				jpc_pi_destroy(pi);
554 				return 0;
555 			}
556 		}
557 	}
558 
559 	pi->maxrlvls = 0;
560 	for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps, cmpt =
561 	  dec->cmpts; compno < pi->numcomps; ++compno, ++tcomp, ++picomp,
562 	  ++cmpt) {
563 		picomp->hsamp = cmpt->hstep;
564 		picomp->vsamp = cmpt->vstep;
565 		for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
566 		  rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
567 			pirlvl->prcwidthexpn = rlvl->prcwidthexpn;
568 			pirlvl->prcheightexpn = rlvl->prcheightexpn;
569 			for (prcno = 0, prclyrno = pirlvl->prclyrnos;
570 			  prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
571 				*prclyrno = 0;
572 			}
573 			pirlvl->numhprcs = rlvl->numhprcs;
574 		}
575 		if (pi->maxrlvls < tcomp->numrlvls) {
576 			pi->maxrlvls = tcomp->numrlvls;
577 		}
578 	}
579 
580 	pi->numlyrs = tile->cp->numlyrs;
581 	pi->xstart = tile->xstart;
582 	pi->ystart = tile->ystart;
583 	pi->xend = tile->xend;
584 	pi->yend = tile->yend;
585 
586 	pi->picomp = 0;
587 	pi->pirlvl = 0;
588 	pi->x = 0;
589 	pi->y = 0;
590 	pi->compno = 0;
591 	pi->rlvlno = 0;
592 	pi->prcno = 0;
593 	pi->lyrno = 0;
594 	pi->xstep = 0;
595 	pi->ystep = 0;
596 
597 	pi->pchgno = -1;
598 
599 	pi->defaultpchg.prgord = tile->cp->prgord;
600 	pi->defaultpchg.compnostart = 0;
601 	pi->defaultpchg.compnoend = pi->numcomps;
602 	pi->defaultpchg.rlvlnostart = 0;
603 	pi->defaultpchg.rlvlnoend = pi->maxrlvls;
604 	pi->defaultpchg.lyrnoend = pi->numlyrs;
605 	pi->pchg = 0;
606 
607 	pi->valid = 0;
608 
609 	return pi;
610 }
611 
jpc_dec_lookahead(jas_stream_t * in)612 long jpc_dec_lookahead(jas_stream_t *in)
613 {
614 	uint_fast16_t x;
615 	if (jpc_getuint16(in, &x)) {
616 		return -1;
617 	}
618 	if (jas_stream_ungetc(in, x & 0xff) == EOF ||
619 	  jas_stream_ungetc(in, x >> 8) == EOF) {
620 		return -1;
621 	}
622 	if (x >= JPC_MS_INMIN && x <= JPC_MS_INMAX) {
623 		return x;
624 	}
625 	return 0;
626 }
627