1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2003 Michael David Adams.
5  * All rights reserved.
6  */
7 
8 /* __START_OF_JASPER_LICENSE__
9  *
10  * JasPer License Version 2.0
11  *
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  *
16  * All rights reserved.
17  *
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  *
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  *
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  *
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  *
61  * __END_OF_JASPER_LICENSE__
62  */
63 
64 /*
65  * Tier 2 Encoder
66  *
67  * $Id$
68  */
69 
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73 
74 #include "jpc_t2enc.h"
75 #include "jpc_t2cod.h"
76 #include "jpc_tagtree.h"
77 #include "jpc_enc.h"
78 #include "jpc_math.h"
79 
80 #include "jasper/jas_malloc.h"
81 #include "jasper/jas_math.h"
82 #include "jasper/jas_debug.h"
83 
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <assert.h>
87 
88 /******************************************************************************\
89 * Code.
90 \******************************************************************************/
91 
jpc_putcommacode(jpc_bitstream_t * out,int n)92 static int jpc_putcommacode(jpc_bitstream_t *out, int n)
93 {
94 	assert(n >= 0);
95 
96 	while (--n >= 0) {
97 		if (jpc_bitstream_putbit(out, 1) == EOF) {
98 			return -1;
99 		}
100 	}
101 	if (jpc_bitstream_putbit(out, 0) == EOF) {
102 		return -1;
103 	}
104 	return 0;
105 }
106 
jpc_putnumnewpasses(jpc_bitstream_t * out,int n)107 static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n)
108 {
109 	int ret;
110 
111 	if (n <= 0) {
112 		return -1;
113 	} else if (n == 1) {
114 		ret = jpc_bitstream_putbit(out, 0);
115 	} else if (n == 2) {
116 		ret = jpc_bitstream_putbits(out, 2, 2);
117 	} else if (n <= 5) {
118 		ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3));
119 	} else if (n <= 36) {
120 		ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6));
121 	} else if (n <= 164) {
122 		ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37));
123 	} else {
124 		/* The standard has no provision for encoding a larger value.
125 		In practice, however, it is highly unlikely that this
126 		limitation will ever be encountered. */
127 		return -1;
128 	}
129 
130 	return (ret != EOF) ? 0 : (-1);
131 }
132 
jpc_enc_encpkts(jpc_enc_t * enc,jas_stream_t * out)133 int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out)
134 {
135 	jpc_enc_tile_t *tile;
136 	jpc_pi_t *pi;
137 
138 	tile = enc->curtile;
139 
140 	jpc_init_t2state(enc, false);
141 	pi = tile->pi;
142 	jpc_pi_init(pi);
143 
144 	if (!jpc_pi_next(pi)) {
145 		for (;;) {
146 			if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi),
147 			  jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) {
148 				return -1;
149 			}
150 			if (jpc_pi_next(pi)) {
151 				break;
152 			}
153 		}
154 	}
155 
156 	return 0;
157 }
158 
jpc_enc_encpkt(jpc_enc_t * enc,jas_stream_t * out,unsigned compno,unsigned lvlno,unsigned prcno,unsigned lyrno)159 int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, unsigned compno, unsigned lvlno, unsigned prcno, unsigned lyrno)
160 {
161 	jpc_enc_tcmpt_t *comp;
162 	jpc_enc_rlvl_t *lvl;
163 	jpc_enc_band_t *band;
164 	jpc_enc_band_t *endbands;
165 	jpc_enc_cblk_t *cblk;
166 	jpc_enc_cblk_t *endcblks;
167 	jpc_bitstream_t *outb;
168 	jpc_enc_pass_t *pass;
169 	jpc_enc_pass_t *startpass;
170 	jpc_enc_pass_t *lastpass;
171 	jpc_enc_pass_t *endpass;
172 	jpc_enc_pass_t *endpasses;
173 	int i;
174 	int ret;
175 	jpc_tagtreenode_t *leaf;
176 	int t1;
177 	int t2;
178 	jpc_enc_tile_t *tile;
179 	jpc_enc_prc_t *prc;
180 	jpc_enc_cp_t *cp;
181 	jpc_ms_t *ms;
182 
183 	JAS_DBGLOG(10, ("encoding packet begin %d %d %d %d\n", compno, lvlno,
184 	  prcno, lyrno));
185 
186 	tile = enc->curtile;
187 	cp = enc->cp;
188 
189 	if (cp->tcp.csty & JPC_COD_SOP) {
190 		if (!(ms = jpc_ms_create(JPC_MS_SOP))) {
191 			return -1;
192 		}
193 		ms->parms.sop.seqno = jpc_pi_getind(tile->pi);
194 		if (jpc_putms(out, enc->cstate, ms)) {
195 			return -1;
196 		}
197 		jpc_ms_destroy(ms);
198 	}
199 
200 	if (!(outb = jpc_bitstream_sopen(out, "w+"))) {
201 		return -1;
202 	}
203 
204 	if (jpc_bitstream_putbit(outb, 1) == EOF) {
205 		goto error_close;
206 	}
207 	//JAS_DBGLOG(10, ("\n"));
208 	JAS_DBGLOG(10, ("present.\n"));
209 
210 	comp = &tile->tcmpts[compno];
211 	lvl = &comp->rlvls[lvlno];
212 	endbands = &lvl->bands[lvl->numbands];
213 	for (band = lvl->bands; band != endbands; ++band) {
214 		if (!band->data) {
215 			continue;
216 		}
217 		prc = &band->prcs[prcno];
218 		if (!prc->cblks) {
219 			continue;
220 		}
221 
222 		endcblks = &prc->cblks[prc->numcblks];
223 		for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
224 			if (!lyrno) {
225 				leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
226 				jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs);
227 			}
228 			pass = cblk->curpass;
229 			const bool included = (pass && pass->lyrno == lyrno);
230 			if (included && (!cblk->numencpasses)) {
231 				assert(pass->lyrno == lyrno);
232 				leaf = jpc_tagtree_getleaf(prc->incltree,
233 				  cblk - prc->cblks);
234 				jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno);
235 			}
236 		}
237 
238 		endcblks = &prc->cblks[prc->numcblks];
239 		for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
240 			pass = cblk->curpass;
241 			const bool included = (pass && pass->lyrno == lyrno);
242 			if (!cblk->numencpasses) {
243 				leaf = jpc_tagtree_getleaf(prc->incltree,
244 				  cblk - prc->cblks);
245 				if (jpc_tagtree_encode(prc->incltree, leaf, lyrno + 1, outb) <
246 				  0) {
247 					goto error_close;
248 				}
249 			} else {
250 				if (jpc_bitstream_putbit(outb, included) == EOF) {
251 					goto error_close;
252 				}
253 			}
254 			JAS_DBGLOG(10, ("included=%d ", included));
255 			if (!included) {
256 				continue;
257 			}
258 			if (!cblk->numencpasses) {
259 				i = 1;
260 				leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
261 				for (;;) {
262 					if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i,
263 					  outb)) < 0) {
264 						goto error_close;
265 					}
266 					if (ret) {
267 						break;
268 					}
269 					++i;
270 				}
271 				assert(leaf->known_ && i == leaf->value_ + 1);
272 			}
273 
274 			endpasses = &cblk->passes[cblk->numpasses];
275 			startpass = pass;
276 			endpass = startpass;
277 			while (endpass != endpasses && endpass->lyrno == lyrno){
278 				++endpass;
279 			}
280 			const unsigned numnewpasses = endpass - startpass;
281 			if (jpc_putnumnewpasses(outb, numnewpasses)) {
282 				goto error_close;
283 			}
284 			JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses));
285 
286 			lastpass = endpass - 1;
287 			unsigned n = startpass->start;
288 			unsigned passcount = 1;
289 			unsigned maxadjust = 0;
290 			for (pass = startpass; pass != endpass; ++pass) {
291 				if (pass->term || pass == lastpass) {
292 					unsigned datalen = pass->end - n;
293 					t1 = jpc_int_firstone(datalen) + 1;
294 					t2 = cblk->numlenbits + jpc_floorlog2(passcount);
295 					const unsigned adjust = JAS_MAX(t1 - t2, 0);
296 					maxadjust = JAS_MAX(adjust, maxadjust);
297 					n += datalen;
298 					passcount = 1;
299 				} else {
300 					++passcount;
301 				}
302 			}
303 			if (jpc_putcommacode(outb, maxadjust)) {
304 				goto error_close;
305 			}
306 			cblk->numlenbits += maxadjust;
307 
308 			lastpass = endpass - 1;
309 			n = startpass->start;
310 			passcount = 1;
311 			for (pass = startpass; pass != endpass; ++pass) {
312 				if (pass->term || pass == lastpass) {
313 					unsigned datalen = pass->end - n;
314 					assert(jpc_int_firstone(datalen) < cblk->numlenbits +
315 					  (int)jpc_floorlog2(passcount));
316 					if (jpc_bitstream_putbits(outb, cblk->numlenbits +
317 					  jpc_floorlog2(passcount), datalen) == EOF) {
318 						goto error_close;
319 					}
320 					n += datalen;
321 					passcount = 1;
322 				} else {
323 					++passcount;
324 				}
325 			}
326 		}
327 	}
328 
329 	jpc_bitstream_outalign(outb, 0);
330 	jpc_bitstream_close(outb);
331 
332 	if (cp->tcp.csty & JPC_COD_EPH) {
333 		if (!(ms = jpc_ms_create(JPC_MS_EPH))) {
334 			return -1;
335 		}
336 		if (jpc_putms(out, enc->cstate, ms)) {
337 			return -1;
338 		}
339 		jpc_ms_destroy(ms);
340 	}
341 
342 	comp = &tile->tcmpts[compno];
343 	lvl = &comp->rlvls[lvlno];
344 	endbands = &lvl->bands[lvl->numbands];
345 	for (band = lvl->bands; band != endbands; ++band) {
346 		if (!band->data) {
347 			continue;
348 		}
349 		prc = &band->prcs[prcno];
350 		if (!prc->cblks) {
351 			continue;
352 		}
353 		endcblks = &prc->cblks[prc->numcblks];
354 		for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
355 			pass = cblk->curpass;
356 
357 			if (!pass) {
358 				continue;
359 			}
360 			if (pass->lyrno != lyrno) {
361 				assert(pass->lyrno > lyrno);
362 				continue;
363 			}
364 
365 			endpasses = &cblk->passes[cblk->numpasses];
366 			startpass = pass;
367 			endpass = startpass;
368 			while (endpass != endpasses && endpass->lyrno == lyrno){
369 				++endpass;
370 			}
371 			lastpass = endpass - 1;
372 			const unsigned numnewpasses = endpass - startpass;
373 
374 			jas_stream_seek(cblk->stream, startpass->start, SEEK_SET);
375 			assert(jas_stream_tell(cblk->stream) == startpass->start);
376 			if (jas_stream_copy(out, cblk->stream, lastpass->end -
377 			  startpass->start)) {
378 				return -1;
379 			}
380 			cblk->curpass = (endpass != endpasses) ? endpass : 0;
381 			cblk->numencpasses += numnewpasses;
382 
383 		}
384 	}
385 
386 	JAS_DBGLOG(10, ("encoding packet end\n"));
387 
388 	return 0;
389 
390 error_close:
391 	jpc_bitstream_close(outb);
392 	return -1;
393 }
394 
jpc_save_t2state(jpc_enc_t * enc)395 void jpc_save_t2state(jpc_enc_t *enc)
396 {
397 /* stream pos in embedded T1 stream may be wrong since not saved/restored! */
398 
399 	jpc_enc_tcmpt_t *comp;
400 	jpc_enc_tcmpt_t *endcomps;
401 	jpc_enc_rlvl_t *lvl;
402 	jpc_enc_rlvl_t *endlvls;
403 	jpc_enc_band_t *band;
404 	jpc_enc_band_t *endbands;
405 	jpc_enc_cblk_t *cblk;
406 	jpc_enc_cblk_t *endcblks;
407 	jpc_enc_tile_t *tile;
408 	unsigned prcno;
409 	jpc_enc_prc_t *prc;
410 
411 	tile = enc->curtile;
412 
413 	endcomps = &tile->tcmpts[tile->numtcmpts];
414 	for (comp = tile->tcmpts; comp != endcomps; ++comp) {
415 		endlvls = &comp->rlvls[comp->numrlvls];
416 		for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
417 			if (!lvl->bands) {
418 				continue;
419 			}
420 			endbands = &lvl->bands[lvl->numbands];
421 			for (band = lvl->bands; band != endbands; ++band) {
422 				if (!band->data) {
423 					continue;
424 				}
425 				for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
426 					if (!prc->cblks) {
427 						continue;
428 					}
429 					jpc_tagtree_copy(prc->savincltree, prc->incltree);
430 					jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree);
431 					endcblks = &prc->cblks[prc->numcblks];
432 					for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
433 						cblk->savedcurpass = cblk->curpass;
434 						cblk->savednumencpasses = cblk->numencpasses;
435 						cblk->savednumlenbits = cblk->numlenbits;
436 					}
437 				}
438 			}
439 		}
440 	}
441 
442 }
443 
jpc_restore_t2state(jpc_enc_t * enc)444 void jpc_restore_t2state(jpc_enc_t *enc)
445 {
446 
447 	jpc_enc_tcmpt_t *comp;
448 	jpc_enc_tcmpt_t *endcomps;
449 	jpc_enc_rlvl_t *lvl;
450 	jpc_enc_rlvl_t *endlvls;
451 	jpc_enc_band_t *band;
452 	jpc_enc_band_t *endbands;
453 	jpc_enc_cblk_t *cblk;
454 	jpc_enc_cblk_t *endcblks;
455 	jpc_enc_tile_t *tile;
456 	unsigned prcno;
457 	jpc_enc_prc_t *prc;
458 
459 	tile = enc->curtile;
460 
461 	endcomps = &tile->tcmpts[tile->numtcmpts];
462 	for (comp = tile->tcmpts; comp != endcomps; ++comp) {
463 		endlvls = &comp->rlvls[comp->numrlvls];
464 		for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
465 			if (!lvl->bands) {
466 				continue;
467 			}
468 			endbands = &lvl->bands[lvl->numbands];
469 			for (band = lvl->bands; band != endbands; ++band) {
470 				if (!band->data) {
471 					continue;
472 				}
473 				for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
474 					if (!prc->cblks) {
475 						continue;
476 					}
477 					jpc_tagtree_copy(prc->incltree, prc->savincltree);
478 					jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree);
479 					endcblks = &prc->cblks[prc->numcblks];
480 					for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
481 						cblk->curpass = cblk->savedcurpass;
482 						cblk->numencpasses = cblk->savednumencpasses;
483 						cblk->numlenbits = cblk->savednumlenbits;
484 					}
485 				}
486 			}
487 		}
488 	}
489 }
490 
jpc_init_t2state(jpc_enc_t * enc,bool raflag)491 void jpc_init_t2state(jpc_enc_t *enc, bool raflag)
492 {
493 /* It is assumed that band->numbps and cblk->numbps precomputed */
494 
495 	jpc_enc_tcmpt_t *comp;
496 	jpc_enc_tcmpt_t *endcomps;
497 	jpc_enc_rlvl_t *lvl;
498 	jpc_enc_rlvl_t *endlvls;
499 	jpc_enc_band_t *band;
500 	jpc_enc_band_t *endbands;
501 	jpc_enc_cblk_t *cblk;
502 	jpc_enc_cblk_t *endcblks;
503 	jpc_enc_pass_t *pass;
504 	jpc_enc_pass_t *endpasses;
505 	jpc_tagtreenode_t *leaf;
506 	jpc_enc_tile_t *tile;
507 	unsigned prcno;
508 	jpc_enc_prc_t *prc;
509 
510 	tile = enc->curtile;
511 
512 	endcomps = &tile->tcmpts[tile->numtcmpts];
513 	for (comp = tile->tcmpts; comp != endcomps; ++comp) {
514 		endlvls = &comp->rlvls[comp->numrlvls];
515 		for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
516 			if (!lvl->bands) {
517 				continue;
518 			}
519 			endbands = &lvl->bands[lvl->numbands];
520 			for (band = lvl->bands; band != endbands; ++band) {
521 				if (!band->data) {
522 					continue;
523 				}
524 				for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
525 					if (!prc->cblks) {
526 						continue;
527 					}
528 					jpc_tagtree_reset(prc->incltree);
529 					jpc_tagtree_reset(prc->nlibtree);
530 					endcblks = &prc->cblks[prc->numcblks];
531 					for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
532 						if (jas_stream_rewind(cblk->stream)) {
533 							assert(0);
534 						}
535 						cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0;
536 						cblk->numencpasses = 0;
537 						cblk->numlenbits = 3;
538 						cblk->numimsbs = band->numbps - cblk->numbps;
539 						assert(cblk->numimsbs >= 0);
540 						leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
541 						jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs);
542 
543 						if (raflag) {
544 							endpasses = &cblk->passes[cblk->numpasses];
545 							for (pass = cblk->passes; pass != endpasses; ++pass) {
546 								pass->lyrno = 0;
547 							}
548 						}
549 					}
550 				}
551 			}
552 		}
553 	}
554 
555 }
556 
jpc_enc_pi_create(jpc_enc_cp_t * cp,jpc_enc_tile_t * tile)557 jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile)
558 {
559 	jpc_pi_t *pi;
560 	unsigned compno;
561 	jpc_picomp_t *picomp;
562 	jpc_pirlvl_t *pirlvl;
563 	jpc_enc_tcmpt_t *tcomp;
564 	unsigned rlvlno;
565 	jpc_enc_rlvl_t *rlvl;
566 	unsigned prcno;
567 	unsigned *prclyrno;
568 
569 	if (!(pi = jpc_pi_create0())) {
570 		return 0;
571 	}
572 	pi->pktno = -1;
573 	pi->numcomps = cp->numcmpts;
574 	if (!(pi->picomps = jas_alloc2(pi->numcomps, sizeof(jpc_picomp_t)))) {
575 		jpc_pi_destroy(pi);
576 		return 0;
577 	}
578 	for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno,
579 	  ++picomp) {
580 		picomp->pirlvls = 0;
581 	}
582 
583 	for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps;
584 	  compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
585 		picomp->numrlvls = tcomp->numrlvls;
586 		if (!(picomp->pirlvls = jas_alloc2(picomp->numrlvls,
587 		  sizeof(jpc_pirlvl_t)))) {
588 			jpc_pi_destroy(pi);
589 			return 0;
590 		}
591 		for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
592 		  picomp->numrlvls; ++rlvlno, ++pirlvl) {
593 			pirlvl->prclyrnos = 0;
594 		}
595 		for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
596 		  rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
597 /* XXX sizeof(long) should be sizeof different type */
598 			pirlvl->numprcs = rlvl->numprcs;
599 			if (rlvl->numprcs) {
600 				if (!(pirlvl->prclyrnos = jas_alloc2(pirlvl->numprcs,
601 				  sizeof(long)))) {
602 					jpc_pi_destroy(pi);
603 					return 0;
604 				}
605 			} else {
606 				pirlvl->prclyrnos = 0;
607 			}
608 		}
609 	}
610 
611 	pi->maxrlvls = 0;
612 	for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps;
613 	  compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
614 		picomp->hsamp = cp->ccps[compno].sampgrdstepx;
615 		picomp->vsamp = cp->ccps[compno].sampgrdstepy;
616 		for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
617 		  rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
618 			pirlvl->prcwidthexpn = rlvl->prcwidthexpn;
619 			pirlvl->prcheightexpn = rlvl->prcheightexpn;
620 			for (prcno = 0, prclyrno = pirlvl->prclyrnos;
621 			  prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
622 				*prclyrno = 0;
623 			}
624 			pirlvl->numhprcs = rlvl->numhprcs;
625 		}
626 		if (pi->maxrlvls < tcomp->numrlvls) {
627 			pi->maxrlvls = tcomp->numrlvls;
628 		}
629 	}
630 
631 	pi->numlyrs = tile->numlyrs;
632 	pi->xstart = tile->tlx;
633 	pi->ystart = tile->tly;
634 	pi->xend = tile->brx;
635 	pi->yend = tile->bry;
636 
637 	pi->picomp = 0;
638 	pi->pirlvl = 0;
639 	pi->x = 0;
640 	pi->y = 0;
641 	pi->compno = 0;
642 	pi->rlvlno = 0;
643 	pi->prcno = 0;
644 	pi->lyrno = 0;
645 	pi->xstep = 0;
646 	pi->ystep = 0;
647 
648 	pi->pchgno = -1;
649 
650 	pi->defaultpchg.prgord = tile->prg;
651 	pi->defaultpchg.compnostart = 0;
652 	pi->defaultpchg.compnoend = pi->numcomps;
653 	pi->defaultpchg.rlvlnostart = 0;
654 	pi->defaultpchg.rlvlnoend = pi->maxrlvls;
655 	pi->defaultpchg.lyrnoend = pi->numlyrs;
656 	pi->pchg = 0;
657 
658 	pi->valid = 0;
659 
660 	return pi;
661 }
662