Debian Codegraph @

converter/other/jpeg2000/libjasper/jpc/jpc_t2enc.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 * Tier 2 Encoder
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_malloc.h"
128 #include "jasper/jas_math.h"
129 #include "jasper/jas_debug.h"
130
131 #include "jpc_flt.h"
132 #include "jpc_t2enc.h"
133 #include "jpc_t2cod.h"
134 #include "jpc_tagtree.h"
135 #include "jpc_enc.h"
136 #include "jpc_math.h"
137
138 /******************************************************************************\
139 * Code.
140 \******************************************************************************/
141
142 static int jpc_putcommacode(jpc_bitstream_t *out, int n)
143 {
144 assert(n >= 0);
145
146 while (--n >= 0) {
147 if (jpc_bitstream_putbit(out, 1) == EOF) {
148 return -1;
149 }
150 }
151 if (jpc_bitstream_putbit(out, 0) == EOF) {
152 return -1;
153 }
154 return 0;
155 }
156
157 static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n)
158 {
159 int ret;
160
161 if (n <= 0) {
162 return -1;
163 } else if (n == 1) {
164 ret = jpc_bitstream_putbit(out, 0);
165 } else if (n == 2) {
166 ret = jpc_bitstream_putbits(out, 2, 2);
167 } else if (n <= 5) {
168 ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3));
169 } else if (n <= 36) {
170 ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6));
171 } else if (n <= 164) {
172 ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37));
173 } else {
174 /* The standard has no provision for encoding a larger value.
175 In practice, however, it is highly unlikely that this
176 limitation will ever be encountered. */
177 return -1;
178 }
179
180 return (ret != EOF) ? 0 : (-1);
181 }
182
183 int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out)
184 {
185 jpc_enc_tile_t *tile;
186 jpc_pi_t *pi;
187
188 tile = enc->curtile;
189
190 jpc_init_t2state(enc, 0);
191 pi = tile->pi;
192 jpc_pi_init(pi);
193
194 if (!jpc_pi_next(pi)) {
195 for (;;) {
196 if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi),
197 jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) {
198 return -1;
199 }
200 if (jpc_pi_next(pi)) {
201 break;
202 }
203 }
204 }
205
206 return 0;
207 }
208
209 int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, int compno, int lvlno, int prcno, int lyrno)
210 {
211 jpc_enc_tcmpt_t *comp;
212 jpc_enc_rlvl_t *lvl;
213 jpc_enc_band_t *band;
214 jpc_enc_band_t *endbands;
215 jpc_enc_cblk_t *cblk;
216 jpc_enc_cblk_t *endcblks;
217 jpc_bitstream_t *outb;
218 jpc_enc_pass_t *pass;
219 jpc_enc_pass_t *startpass;
220 jpc_enc_pass_t *lastpass;
221 jpc_enc_pass_t *endpass;
222 jpc_enc_pass_t *endpasses;
223 int i;
224 int included;
225 int ret;
226 jpc_tagtreenode_t *leaf;
227 int n;
228 int t1;
229 int t2;
230 int adjust;
231 int maxadjust;
232 int datalen;
233 int numnewpasses;
234 int passcount;
235 jpc_enc_tile_t *tile;
236 jpc_enc_prc_t *prc;
237 jpc_enc_cp_t *cp;
238 jpc_ms_t *ms;
239
240 tile = enc->curtile;
241 cp = enc->cp;
242
243 if (cp->tcp.csty & JPC_COD_SOP) {
244 if (!(ms = jpc_ms_create(JPC_MS_SOP))) {
245 return -1;
246 }
247 ms->parms.sop.seqno = jpc_pi_getind(tile->pi);
248 if (jpc_putms(out, enc->cstate, ms)) {
249 return -1;
250 }
251 jpc_ms_destroy(ms);
252 }
253
254 outb = jpc_bitstream_sopen(out, "w+");
255 assert(outb);
256
257 if (jpc_bitstream_putbit(outb, 1) == EOF) {
258 return -1;
259 }
260 JAS_DBGLOG(10, ("\n"));
261 JAS_DBGLOG(10, ("present. "));
262
263 comp = &tile->tcmpts[compno];
264 lvl = &comp->rlvls[lvlno];
265 endbands = &lvl->bands[lvl->numbands];
266 for (band = lvl->bands; band != endbands; ++band) {
267 if (!band->data) {
268 continue;
269 }
270 prc = &band->prcs[prcno];
271 if (!prc->cblks) {
272 continue;
273 }
274
275 endcblks = &prc->cblks[prc->numcblks];
276 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
277 if (!lyrno) {
278 leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
279 jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs);
280 }
281 pass = cblk->curpass;
282 included = (pass && pass->lyrno == lyrno);
283 if (included && (!cblk->numencpasses)) {
284 assert(pass->lyrno == lyrno);
285 leaf = jpc_tagtree_getleaf(prc->incltree,
286 cblk - prc->cblks);
287 jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno);
288 }
289 }
290
291 endcblks = &prc->cblks[prc->numcblks];
292 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
293 pass = cblk->curpass;
294 included = (pass && pass->lyrno == lyrno);
295 if (!cblk->numencpasses) {
296 leaf = jpc_tagtree_getleaf(prc->incltree,
297 cblk - prc->cblks);
298 if (jpc_tagtree_encode(prc->incltree, leaf, lyrno
299 + 1, outb) < 0) {
300 return -1;
301 }
302 } else {
303 if (jpc_bitstream_putbit(outb, included) == EOF) {
304 return -1;
305 }
306 }
307 JAS_DBGLOG(10, ("included=%d ", included));
308 if (!included) {
309 continue;
310 }
311 if (!cblk->numencpasses) {
312 i = 1;
313 leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
314 for (;;) {
315 if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i, outb)) < 0) {
316 return -1;
317 }
318 if (ret) {
319 break;
320 }
321 ++i;
322 }
323 assert(leaf->known_ && i == leaf->value_ + 1);
324 }
325
326 endpasses = &cblk->passes[cblk->numpasses];
327 startpass = pass;
328 endpass = startpass;
329 while (endpass != endpasses && endpass->lyrno == lyrno){
330 ++endpass;
331 }
332 numnewpasses = endpass - startpass;
333 if (jpc_putnumnewpasses(outb, numnewpasses)) {
334 return -1;
335 }
336 JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses));
337
338 lastpass = endpass - 1;
339 n = startpass->start;
340 passcount = 1;
341 maxadjust = 0;
342 for (pass = startpass; pass != endpass; ++pass) {
343 if (pass->term || pass == lastpass) {
344 datalen = pass->end - n;
345 t1 = jpc_firstone(datalen) + 1;
346 t2 = cblk->numlenbits + jpc_floorlog2(passcount);
347 adjust = JAS_MAX(t1 - t2, 0);
348 maxadjust = JAS_MAX(adjust, maxadjust);
349 n += datalen;
350 passcount = 1;
351 } else {
352 ++passcount;
353 }
354 }
355 if (jpc_putcommacode(outb, maxadjust)) {
356 return -1;
357 }
358 cblk->numlenbits += maxadjust;
359
360 lastpass = endpass - 1;
361 n = startpass->start;
362 passcount = 1;
363 for (pass = startpass; pass != endpass; ++pass) {
364 if (pass->term || pass == lastpass) {
365 datalen = pass->end - n;
366 assert(jpc_firstone(datalen) < cblk->numlenbits + jpc_floorlog2(passcount));
367 if (jpc_bitstream_putbits(outb, cblk->numlenbits + jpc_floorlog2(passcount), datalen) == EOF) {
368 return -1;
369 }
370 n += datalen;
371 passcount = 1;
372 } else {
373 ++passcount;
374 }
375 }
376 }
377 }
378
379 jpc_bitstream_outalign(outb, 0);
380 jpc_bitstream_close(outb);
381
382 if (cp->tcp.csty & JPC_COD_EPH) {
383 if (!(ms = jpc_ms_create(JPC_MS_EPH))) {
384 return -1;
385 }
386 jpc_putms(out, enc->cstate, ms);
387 jpc_ms_destroy(ms);
388 }
389
390 comp = &tile->tcmpts[compno];
391 lvl = &comp->rlvls[lvlno];
392 endbands = &lvl->bands[lvl->numbands];
393 for (band = lvl->bands; band != endbands; ++band) {
394 if (!band->data) {
395 continue;
396 }
397 prc = &band->prcs[prcno];
398 if (!prc->cblks) {
399 continue;
400 }
401 endcblks = &prc->cblks[prc->numcblks];
402 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
403 pass = cblk->curpass;
404
405 if (!pass) {
406 continue;
407 }
408 if (pass->lyrno != lyrno) {
409 assert(pass->lyrno < 0 || pass->lyrno > lyrno);
410 continue;
411 }
412
413 endpasses = &cblk->passes[cblk->numpasses];
414 startpass = pass;
415 endpass = startpass;
416 while (endpass != endpasses && endpass->lyrno == lyrno){
417 ++endpass;
418 }
419 lastpass = endpass - 1;
420 numnewpasses = endpass - startpass;
421
422 jas_stream_seek(cblk->stream, startpass->start, SEEK_SET);
423 assert(jas_stream_tell(cblk->stream) == startpass->start);
424 if (jas_stream_copy(out, cblk->stream, lastpass->end - startpass->start)) {
425 return -1;
426 }
427 cblk->curpass = (endpass != endpasses) ? endpass : 0;
428 cblk->numencpasses += numnewpasses;
429
430 }
431 }
432
433 return 0;
434 }
435
436 void jpc_save_t2state(jpc_enc_t *enc)
437 {
438 /* stream pos in embedded T1 stream may be wrong since not saved/restored! */
439
440 jpc_enc_tcmpt_t *comp;
441 jpc_enc_tcmpt_t *endcomps;
442 jpc_enc_rlvl_t *lvl;
443 jpc_enc_rlvl_t *endlvls;
444 jpc_enc_band_t *band;
445 jpc_enc_band_t *endbands;
446 jpc_enc_cblk_t *cblk;
447 jpc_enc_cblk_t *endcblks;
448 jpc_enc_tile_t *tile;
449 uint_fast32_t prcno;
450 jpc_enc_prc_t *prc;
451
452 tile = enc->curtile;
453
454 endcomps = &tile->tcmpts[tile->numtcmpts];
455 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
456 endlvls = &comp->rlvls[comp->numrlvls];
457 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
458 if (!lvl->bands) {
459 continue;
460 }
461 endbands = &lvl->bands[lvl->numbands];
462 for (band = lvl->bands; band != endbands; ++band) {
463 if (!band->data) {
464 continue;
465 }
466 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
467 if (!prc->cblks) {
468 continue;
469 }
470 jpc_tagtree_copy(prc->savincltree, prc->incltree);
471 jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree);
472 endcblks = &prc->cblks[prc->numcblks];
473 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
474 cblk->savedcurpass = cblk->curpass;
475 cblk->savednumencpasses = cblk->numencpasses;
476 cblk->savednumlenbits = cblk->numlenbits;
477 }
478 }
479 }
480 }
481 }
482
483 }
484
485 void jpc_restore_t2state(jpc_enc_t *enc)
486 {
487
488 jpc_enc_tcmpt_t *comp;
489 jpc_enc_tcmpt_t *endcomps;
490 jpc_enc_rlvl_t *lvl;
491 jpc_enc_rlvl_t *endlvls;
492 jpc_enc_band_t *band;
493 jpc_enc_band_t *endbands;
494 jpc_enc_cblk_t *cblk;
495 jpc_enc_cblk_t *endcblks;
496 jpc_enc_tile_t *tile;
497 uint_fast32_t prcno;
498 jpc_enc_prc_t *prc;
499
500 tile = enc->curtile;
501
502 endcomps = &tile->tcmpts[tile->numtcmpts];
503 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
504 endlvls = &comp->rlvls[comp->numrlvls];
505 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
506 if (!lvl->bands) {
507 continue;
508 }
509 endbands = &lvl->bands[lvl->numbands];
510 for (band = lvl->bands; band != endbands; ++band) {
511 if (!band->data) {
512 continue;
513 }
514 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
515 if (!prc->cblks) {
516 continue;
517 }
518 jpc_tagtree_copy(prc->incltree, prc->savincltree);
519 jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree);
520 endcblks = &prc->cblks[prc->numcblks];
521 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
522 cblk->curpass = cblk->savedcurpass;
523 cblk->numencpasses = cblk->savednumencpasses;
524 cblk->numlenbits = cblk->savednumlenbits;
525 }
526 }
527 }
528 }
529 }
530 }
531
532 void jpc_init_t2state(jpc_enc_t *enc, int raflag)
533 {
534 /* It is assumed that band->numbps and cblk->numbps precomputed */
535
536 jpc_enc_tcmpt_t *comp;
537 jpc_enc_tcmpt_t *endcomps;
538 jpc_enc_rlvl_t *lvl;
539 jpc_enc_rlvl_t *endlvls;
540 jpc_enc_band_t *band;
541 jpc_enc_band_t *endbands;
542 jpc_enc_cblk_t *cblk;
543 jpc_enc_cblk_t *endcblks;
544 jpc_enc_pass_t *pass;
545 jpc_enc_pass_t *endpasses;
546 jpc_tagtreenode_t *leaf;
547 jpc_enc_tile_t *tile;
548 uint_fast32_t prcno;
549 jpc_enc_prc_t *prc;
550
551 tile = enc->curtile;
552
553 endcomps = &tile->tcmpts[tile->numtcmpts];
554 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
555 endlvls = &comp->rlvls[comp->numrlvls];
556 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
557 if (!lvl->bands) {
558 continue;
559 }
560 endbands = &lvl->bands[lvl->numbands];
561 for (band = lvl->bands; band != endbands; ++band) {
562 if (!band->data) {
563 continue;
564 }
565 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
566 if (!prc->cblks) {
567 continue;
568 }
569 jpc_tagtree_reset(prc->incltree);
570 jpc_tagtree_reset(prc->nlibtree);
571 endcblks = &prc->cblks[prc->numcblks];
572 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
573 if (jas_stream_rewind(cblk->stream)) {
574 assert(0);
575 }
576 cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0;
577 cblk->numencpasses = 0;
578 cblk->numlenbits = 3;
579 cblk->numimsbs = band->numbps - cblk->numbps;
580 assert(cblk->numimsbs >= 0);
581 leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
582 jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs);
583
584 if (raflag) {
585 endpasses = &cblk->passes[cblk->numpasses];
586 for (pass = cblk->passes; pass != endpasses; ++pass) {
587 pass->lyrno = -1;
588 pass->lyrno = 0;
589 }
590 }
591 }
592 }
593 }
594 }
595 }
596
597 }
598
599 jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile)
600 {
601 jpc_pi_t *pi;
602 int compno;
603 jpc_picomp_t *picomp;
604 jpc_pirlvl_t *pirlvl;
605 jpc_enc_tcmpt_t *tcomp;
606 int rlvlno;
607 jpc_enc_rlvl_t *rlvl;
608 int prcno;
609 int *prclyrno;
610
611 if (!(pi = jpc_pi_create0())) {
612 return 0;
613 }
614 pi->pktno = -1;
615 pi->numcomps = cp->numcmpts;
616 if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) {
617 jpc_pi_destroy(pi);
618 return 0;
619 }
620 for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno,
621 ++picomp) {
622 picomp->pirlvls = 0;
623 }
624
625 for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps;
626 compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
627 picomp->numrlvls = tcomp->numrlvls;
628 if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls *
629 sizeof(jpc_pirlvl_t)))) {
630 jpc_pi_destroy(pi);
631 return 0;
632 }
633 for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
634 picomp->numrlvls; ++rlvlno, ++pirlvl) {
635 pirlvl->prclyrnos = 0;
636 }
637 for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
638 rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
639 /* XXX sizeof(long) should be sizeof different type */
640 pirlvl->numprcs = rlvl->numprcs;
641 if (rlvl->numprcs) {
642 if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs *
643 sizeof(long)))) {
644 jpc_pi_destroy(pi);
645 return 0;
646 }
647 } else {
648 pirlvl->prclyrnos = 0;
649 }
650 }
651 }
652
653 pi->maxrlvls = 0;
654 for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps;
655 compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
656 picomp->hsamp = cp->ccps[compno].sampgrdstepx;
657 picomp->vsamp = cp->ccps[compno].sampgrdstepy;
658 for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
659 rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
660 pirlvl->prcwidthexpn = rlvl->prcwidthexpn;
661 pirlvl->prcheightexpn = rlvl->prcheightexpn;
662 for (prcno = 0, prclyrno = pirlvl->prclyrnos;
663 prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
664 *prclyrno = 0;
665 }
666 pirlvl->numhprcs = rlvl->numhprcs;
667 }
668 if (pi->maxrlvls < tcomp->numrlvls) {
669 pi->maxrlvls = tcomp->numrlvls;
670 }
671 }
672
673 pi->numlyrs = tile->numlyrs;
674 pi->xstart = tile->tlx;
675 pi->ystart = tile->tly;
676 pi->xend = tile->brx;
677 pi->yend = tile->bry;
678
679 pi->picomp = 0;
680 pi->pirlvl = 0;
681 pi->x = 0;
682 pi->y = 0;
683 pi->compno = 0;
684 pi->rlvlno = 0;
685 pi->prcno = 0;
686 pi->lyrno = 0;
687 pi->xstep = 0;
688 pi->ystep = 0;
689
690 pi->pchgno = -1;
691
692 pi->defaultpchg.prgord = tile->prg;
693 pi->defaultpchg.compnostart = 0;
694 pi->defaultpchg.compnoend = pi->numcomps;
695 pi->defaultpchg.rlvlnostart = 0;
696 pi->defaultpchg.rlvlnoend = pi->maxrlvls;
697 pi->defaultpchg.lyrnoend = pi->numlyrs;
698 pi->pchg = 0;
699
700 pi->valid = 0;
701
702 return pi;
703 }