ccb16e5230f6d17d2b0daea0e659d8e1beaee299
[opencv] / otherlibs / _graphics / src / libjasper / jpc_enc.c
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2003 Michael David Adams.
5  * All rights reserved.
6  */
7
8 /* __START_OF_JASPER_LICENSE__
9  * 
10  * JasPer License Version 2.0
11  * 
12  * Copyright (c) 1999-2000 Image Power, Inc.
13  * Copyright (c) 1999-2000 The University of British Columbia
14  * Copyright (c) 2001-2003 Michael David Adams
15  * 
16  * All rights reserved.
17  * 
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  * 
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  * 
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  * 
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * $Id: jpc_enc.c,v 1.1 2007/01/15 16:09:28 vp153 Exp $
66  */
67
68 /******************************************************************************\
69 * Includes.
70 \******************************************************************************/
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <assert.h>
75 #include <math.h>
76 #include <float.h>
77
78 #include "jasper/jas_string.h"
79 #include "jasper/jas_malloc.h"
80 #include "jasper/jas_image.h"
81 #include "jasper/jas_fix.h"
82 #include "jasper/jas_tvp.h"
83 #include "jasper/jas_version.h"
84 #include "jasper/jas_math.h"
85 #include "jasper/jas_debug.h"
86
87 #include "jpc_flt.h"
88 #include "jpc_fix.h"
89 #include "jpc_tagtree.h"
90 #include "jpc_enc.h"
91 #include "jpc_cs.h"
92 #include "jpc_mct.h"
93 #include "jpc_tsfb.h"
94 #include "jpc_qmfb.h"
95 #include "jpc_t1enc.h"
96 #include "jpc_t2enc.h"
97 #include "jpc_cod.h"
98 #include "jpc_math.h"
99 #include "jpc_util.h"
100
101 /******************************************************************************\
102 *
103 \******************************************************************************/
104
105 #define JPC_POW2(n) \
106         (1 << (n))
107
108 #define JPC_FLOORTOMULTPOW2(x, n) \
109   (((n) > 0) ? ((x) & (~((1 << n) - 1))) : (x))
110 /* Round to the nearest multiple of the specified power of two in the
111   direction of negative infinity. */
112
113 #define JPC_CEILTOMULTPOW2(x, n) \
114   (((n) > 0) ? JPC_FLOORTOMULTPOW2(((x) + (1 << (n)) - 1), n) : (x))
115 /* Round to the nearest multiple of the specified power of two in the
116   direction of positive infinity. */
117
118 #define JPC_POW2(n)     \
119   (1 << (n))
120
121 jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno);
122 void jpc_enc_tile_destroy(jpc_enc_tile_t *tile);
123
124 static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
125   jas_image_t *image, jpc_enc_tile_t *tile);
126 static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt);
127 static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
128   jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos);
129 static void rlvl_destroy(jpc_enc_rlvl_t *rlvl);
130 static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
131   jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos);
132 static void band_destroy(jpc_enc_band_t *bands);
133 static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp,
134   jpc_enc_band_t *band);
135 static void prc_destroy(jpc_enc_prc_t *prcs);
136 static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp,
137   jpc_enc_prc_t *prc);
138 static void cblk_destroy(jpc_enc_cblk_t *cblks);
139 int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size);
140 static void pass_destroy(jpc_enc_pass_t *pass);
141 void jpc_enc_dump(jpc_enc_t *enc);
142
143 /******************************************************************************\
144 * Local prototypes.
145 \******************************************************************************/
146
147 int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk);
148 void calcrdslopes(jpc_enc_cblk_t *cblk);
149 void dump_layeringinfo(jpc_enc_t *enc);
150 static int jpc_calcssexp(jpc_fix_t stepsize);
151 static int jpc_calcssmant(jpc_fix_t stepsize);
152 void quantize(jas_matrix_t *data, jpc_fix_t stepsize);
153 static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
154 static int jpc_enc_encodemainbody(jpc_enc_t *enc);
155 int jpc_enc_encodetiledata(jpc_enc_t *enc);
156 jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image);
157 void jpc_enc_destroy(jpc_enc_t *enc);
158 static int jpc_enc_encodemainhdr(jpc_enc_t *enc);
159 static int jpc_enc_encodemainbody(jpc_enc_t *enc);
160 int jpc_enc_encodetiledata(jpc_enc_t *enc);
161 int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens);
162 int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value);
163 static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image);
164 void jpc_enc_cp_destroy(jpc_enc_cp_t *cp);
165 static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn);
166
167 static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn)
168 {
169         int p;
170         uint_fast32_t mant;
171         uint_fast32_t expn;
172         int n;
173
174         if (absdelta < 0) {
175                 abort();
176         }
177
178         p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS;
179         n = 11 - jpc_firstone(absdelta);
180         mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff;
181         expn = scaleexpn - p;
182         if (scaleexpn < p) {
183                 abort();
184         }
185         return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant);
186 }
187
188 typedef enum {
189         OPT_DEBUG,
190         OPT_IMGAREAOFFX,
191         OPT_IMGAREAOFFY,
192         OPT_TILEGRDOFFX,
193         OPT_TILEGRDOFFY,
194         OPT_TILEWIDTH,
195         OPT_TILEHEIGHT,
196         OPT_PRCWIDTH,
197         OPT_PRCHEIGHT,
198         OPT_CBLKWIDTH,
199         OPT_CBLKHEIGHT,
200         OPT_MODE,
201         OPT_PRG,
202         OPT_NOMCT,
203         OPT_MAXRLVLS,
204         OPT_SOP,
205         OPT_EPH,
206         OPT_LAZY,
207         OPT_TERMALL,
208         OPT_SEGSYM,
209         OPT_VCAUSAL,
210         OPT_RESET,
211         OPT_PTERM,
212         OPT_NUMGBITS,
213         OPT_RATE,
214         OPT_ILYRRATES,
215         OPT_JP2OVERHEAD
216 } optid_t;
217
218 jas_taginfo_t encopts[] = {
219         {OPT_DEBUG, "debug"},
220         {OPT_IMGAREAOFFX, "imgareatlx"},
221         {OPT_IMGAREAOFFY, "imgareatly"},
222         {OPT_TILEGRDOFFX, "tilegrdtlx"},
223         {OPT_TILEGRDOFFY, "tilegrdtly"},
224         {OPT_TILEWIDTH, "tilewidth"},
225         {OPT_TILEHEIGHT, "tileheight"},
226         {OPT_PRCWIDTH, "prcwidth"},
227         {OPT_PRCHEIGHT, "prcheight"},
228         {OPT_CBLKWIDTH, "cblkwidth"},
229         {OPT_CBLKHEIGHT, "cblkheight"},
230         {OPT_MODE, "mode"},
231         {OPT_PRG, "prg"},
232         {OPT_NOMCT, "nomct"},
233         {OPT_MAXRLVLS, "numrlvls"},
234         {OPT_SOP, "sop"},
235         {OPT_EPH, "eph"},
236         {OPT_LAZY, "lazy"},
237         {OPT_TERMALL, "termall"},
238         {OPT_SEGSYM, "segsym"},
239         {OPT_VCAUSAL, "vcausal"},
240         {OPT_PTERM, "pterm"},
241         {OPT_RESET, "resetprob"},
242         {OPT_NUMGBITS, "numgbits"},
243         {OPT_RATE, "rate"},
244         {OPT_ILYRRATES, "ilyrrates"},
245         {OPT_JP2OVERHEAD, "_jp2overhead"},
246         {-1, 0}
247 };
248
249 typedef enum {
250         PO_L = 0,
251         PO_R
252 } poid_t;
253
254
255 jas_taginfo_t prgordtab[] = {
256         {JPC_COD_LRCPPRG, "lrcp"},
257         {JPC_COD_RLCPPRG, "rlcp"},
258         {JPC_COD_RPCLPRG, "rpcl"},
259         {JPC_COD_PCRLPRG, "pcrl"},
260         {JPC_COD_CPRLPRG, "cprl"},
261         {-1, 0}
262 };
263
264 typedef enum {
265         MODE_INT,
266         MODE_REAL
267 } modeid_t;
268
269 jas_taginfo_t modetab[] = {
270         {MODE_INT, "int"},
271         {MODE_REAL, "real"},
272         {-1, 0}
273 };
274
275 /******************************************************************************\
276 * The main encoder entry point.
277 \******************************************************************************/
278
279 int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
280 {
281         jpc_enc_t *enc;
282         jpc_enc_cp_t *cp;
283
284         enc = 0;
285         cp = 0;
286
287         jpc_initluts();
288
289         if (!(cp = cp_create(optstr, image))) {
290                 fprintf(stderr, "invalid JP encoder options\n");
291                 goto error;
292         }
293
294         if (!(enc = jpc_enc_create(cp, out, image))) {
295                 goto error;
296         }
297         cp = 0;
298
299         /* Encode the main header. */
300         if (jpc_enc_encodemainhdr(enc)) {
301                 goto error;
302         }
303
304         /* Encode the main body.  This constitutes most of the encoding work. */
305         if (jpc_enc_encodemainbody(enc)) {
306                 goto error;
307         }
308
309         /* Write EOC marker segment. */
310         if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) {
311                 goto error;
312         }
313         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
314                 fprintf(stderr, "cannot write EOI marker\n");
315                 goto error;
316         }
317         jpc_ms_destroy(enc->mrk);
318         enc->mrk = 0;
319
320         if (jas_stream_flush(enc->out)) {
321                 goto error;
322         }
323
324         jpc_enc_destroy(enc);
325
326         return 0;
327
328 error:
329         if (cp) {
330                 jpc_enc_cp_destroy(cp);
331         }
332         if (enc) {
333                 jpc_enc_destroy(enc);
334         }
335         return -1;
336 }
337
338 /******************************************************************************\
339 * Option parsing code.
340 \******************************************************************************/
341
342 static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image)
343 {
344         jpc_enc_cp_t *cp;
345         jas_tvparser_t *tvp;
346         int ret;
347         int numilyrrates;
348         double *ilyrrates;
349         int i;
350         int tagid;
351         jpc_enc_tcp_t *tcp;
352         jpc_enc_tccp_t *tccp;
353         jpc_enc_ccp_t *ccp;
354         int cmptno;
355         uint_fast16_t rlvlno;
356         uint_fast16_t prcwidthexpn;
357         uint_fast16_t prcheightexpn;
358         bool enablemct;
359         uint_fast32_t jp2overhead;
360         uint_fast16_t lyrno;
361         uint_fast32_t hsteplcm;
362         uint_fast32_t vsteplcm;
363         bool mctvalid;
364
365         tvp = 0;
366         cp = 0;
367         ilyrrates = 0;
368         numilyrrates = 0;
369
370         if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) {
371                 goto error;
372         }
373
374         prcwidthexpn = 15;
375         prcheightexpn = 15;
376         enablemct = true;
377         jp2overhead = 0;
378
379         cp->ccps = 0;
380         cp->debug = 0;
381         cp->imgareatlx = UINT_FAST32_MAX;
382         cp->imgareatly = UINT_FAST32_MAX;
383         cp->refgrdwidth = 0;
384         cp->refgrdheight = 0;
385         cp->tilegrdoffx = UINT_FAST32_MAX;
386         cp->tilegrdoffy = UINT_FAST32_MAX;
387         cp->tilewidth = 0;
388         cp->tileheight = 0;
389         cp->numcmpts = jas_image_numcmpts(image);
390
391         hsteplcm = 1;
392         vsteplcm = 1;
393         for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
394                 if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <=
395                   jas_image_brx(image) || jas_image_cmptbry(image, cmptno) +
396                   jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) {
397                         fprintf(stderr, "unsupported image type\n");
398                         goto error;
399                 }
400                 /* Note: We ought to be calculating the LCMs here.  Fix some day. */
401                 hsteplcm *= jas_image_cmpthstep(image, cmptno);
402                 vsteplcm *= jas_image_cmptvstep(image, cmptno);
403         }
404
405         if (!(cp->ccps = jas_malloc(cp->numcmpts * sizeof(jpc_enc_ccp_t)))) {
406                 goto error;
407         }
408         for (cmptno = 0, ccp = cp->ccps; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno,
409           ++ccp) {
410                 ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno);
411                 ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno);
412                 /* XXX - this isn't quite correct for more general image */
413                 ccp->sampgrdsubstepx = 0;
414                 ccp->sampgrdsubstepx = 0;
415                 ccp->prec = jas_image_cmptprec(image, cmptno);
416                 ccp->sgnd = jas_image_cmptsgnd(image, cmptno);
417                 ccp->numstepsizes = 0;
418                 memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes));
419         }
420
421         cp->rawsize = jas_image_rawsize(image);
422         cp->totalsize = UINT_FAST32_MAX;
423
424         tcp = &cp->tcp;
425         tcp->csty = 0;
426         tcp->intmode = true;
427         tcp->prg = JPC_COD_LRCPPRG;
428         tcp->numlyrs = 1;
429         tcp->ilyrrates = 0;
430
431         tccp = &cp->tccp;
432         tccp->csty = 0;
433         tccp->maxrlvls = 6;
434         tccp->cblkwidthexpn = 6;
435         tccp->cblkheightexpn = 6;
436         tccp->cblksty = 0;
437         tccp->numgbits = 2;
438
439         if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) {
440                 goto error;
441         }
442
443         while (!(ret = jas_tvparser_next(tvp))) {
444                 switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts,
445                   jas_tvparser_gettag(tvp)))->id) {
446                 case OPT_DEBUG:
447                         cp->debug = atoi(jas_tvparser_getval(tvp));
448                         break;
449                 case OPT_IMGAREAOFFX:
450                         cp->imgareatlx = atoi(jas_tvparser_getval(tvp));
451                         break;
452                 case OPT_IMGAREAOFFY:
453                         cp->imgareatly = atoi(jas_tvparser_getval(tvp));
454                         break;
455                 case OPT_TILEGRDOFFX:
456                         cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp));
457                         break;
458                 case OPT_TILEGRDOFFY:
459                         cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp));
460                         break;
461                 case OPT_TILEWIDTH:
462                         cp->tilewidth = atoi(jas_tvparser_getval(tvp));
463                         break;
464                 case OPT_TILEHEIGHT:
465                         cp->tileheight = atoi(jas_tvparser_getval(tvp));
466                         break;
467                 case OPT_PRCWIDTH:
468                         prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
469                         break;
470                 case OPT_PRCHEIGHT:
471                         prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
472                         break;
473                 case OPT_CBLKWIDTH:
474                         tccp->cblkwidthexpn =
475                           jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
476                         break;
477                 case OPT_CBLKHEIGHT:
478                         tccp->cblkheightexpn =
479                           jpc_floorlog2(atoi(jas_tvparser_getval(tvp)));
480                         break;
481                 case OPT_MODE:
482                         if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab,
483                           jas_tvparser_getval(tvp)))->id) < 0) {
484                                 fprintf(stderr,
485                                   "ignoring invalid mode %s\n",
486                                   jas_tvparser_getval(tvp));
487                         } else {
488                                 tcp->intmode = (tagid == MODE_INT);
489                         }
490                         break;
491                 case OPT_PRG:
492                         if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab,
493                           jas_tvparser_getval(tvp)))->id) < 0) {
494                                 fprintf(stderr,
495                                   "ignoring invalid progression order %s\n",
496                                   jas_tvparser_getval(tvp));
497                         } else {
498                                 tcp->prg = tagid;
499                         }
500                         break;
501                 case OPT_NOMCT:
502                         enablemct = false;
503                         break;
504                 case OPT_MAXRLVLS:
505                         tccp->maxrlvls = atoi(jas_tvparser_getval(tvp));
506                         break;
507                 case OPT_SOP:
508                         cp->tcp.csty |= JPC_COD_SOP;
509                         break;
510                 case OPT_EPH:
511                         cp->tcp.csty |= JPC_COD_EPH;
512                         break;
513                 case OPT_LAZY:
514                         tccp->cblksty |= JPC_COX_LAZY;
515                         break;
516                 case OPT_TERMALL:
517                         tccp->cblksty |= JPC_COX_TERMALL;
518                         break;
519                 case OPT_SEGSYM:
520                         tccp->cblksty |= JPC_COX_SEGSYM;
521                         break;
522                 case OPT_VCAUSAL:
523                         tccp->cblksty |= JPC_COX_VSC;
524                         break;
525                 case OPT_RESET:
526                         tccp->cblksty |= JPC_COX_RESET;
527                         break;
528                 case OPT_PTERM:
529                         tccp->cblksty |= JPC_COX_PTERM;
530                         break;
531                 case OPT_NUMGBITS:
532                         cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp));
533                         break;
534                 case OPT_RATE:
535                         if (ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize,
536                           &cp->totalsize)) {
537                                 fprintf(stderr,
538                                   "ignoring bad rate specifier %s\n",
539                                   jas_tvparser_getval(tvp));
540                         }
541                         break;
542                 case OPT_ILYRRATES:
543                         if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates,
544                           &ilyrrates)) {
545                                 fprintf(stderr,
546                                   "warning: invalid intermediate layer rates specifier ignored (%s)\n",
547                                   jas_tvparser_getval(tvp));
548                         }
549                         break;
550
551                 case OPT_JP2OVERHEAD:
552                         jp2overhead = atoi(jas_tvparser_getval(tvp));
553                         break;
554                 default:
555                         fprintf(stderr, "warning: ignoring invalid option %s\n",
556                          jas_tvparser_gettag(tvp));
557                         break;
558                 }
559         }
560
561         jas_tvparser_destroy(tvp);
562         tvp = 0;
563
564         if (cp->totalsize != UINT_FAST32_MAX) {
565                 cp->totalsize = (cp->totalsize > jp2overhead) ?
566                   (cp->totalsize - jp2overhead) : 0;
567         }
568
569         if (cp->imgareatlx == UINT_FAST32_MAX) {
570                 cp->imgareatlx = 0;
571         } else {
572                 if (hsteplcm != 1) {
573                         fprintf(stderr, "warning: overriding imgareatlx value\n");
574                 }
575                 cp->imgareatlx *= hsteplcm;
576         }
577         if (cp->imgareatly == UINT_FAST32_MAX) {
578                 cp->imgareatly = 0;
579         } else {
580                 if (vsteplcm != 1) {
581                         fprintf(stderr, "warning: overriding imgareatly value\n");
582                 }
583                 cp->imgareatly *= vsteplcm;
584         }
585         cp->refgrdwidth = cp->imgareatlx + jas_image_width(image);
586         cp->refgrdheight = cp->imgareatly + jas_image_height(image);
587         if (cp->tilegrdoffx == UINT_FAST32_MAX) {
588                 cp->tilegrdoffx = cp->imgareatlx;
589         }
590         if (cp->tilegrdoffy == UINT_FAST32_MAX) {
591                 cp->tilegrdoffy = cp->imgareatly;
592         }
593         if (!cp->tilewidth) {
594                 cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx;
595         }
596         if (!cp->tileheight) {
597                 cp->tileheight = cp->refgrdheight - cp->tilegrdoffy;
598         }
599
600         if (cp->numcmpts == 3) {
601                 mctvalid = true;
602                 for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) {
603                         if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) ||
604                           jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) ||
605                           jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) ||
606                           jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) {
607                                 mctvalid = false;
608                         }
609                 }
610         } else {
611                 mctvalid = false;
612         }
613         if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) != JAS_CLRSPC_FAM_RGB) {
614                 fprintf(stderr, "warning: color space apparently not RGB\n");
615         }
616         if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) == JAS_CLRSPC_FAM_RGB) {
617                 tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT);
618         } else {
619                 tcp->mctid = JPC_MCT_NONE;
620         }
621         tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS);
622
623         for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
624                 tccp->prcwidthexpns[rlvlno] = prcwidthexpn;
625                 tccp->prcheightexpns[rlvlno] = prcheightexpn;
626         }
627         if (prcwidthexpn != 15 || prcheightexpn != 15) {
628                 tccp->csty |= JPC_COX_PRT;
629         }
630
631         /* Ensure that the tile width and height is valid. */
632         if (!cp->tilewidth) {
633                 fprintf(stderr, "invalid tile width %lu\n", (unsigned long)
634                   cp->tilewidth);
635                 goto error;
636         }
637         if (!cp->tileheight) {
638                 fprintf(stderr, "invalid tile height %lu\n", (unsigned long)
639                   cp->tileheight);
640                 goto error;
641         }
642
643         /* Ensure that the tile grid offset is valid. */
644         if (cp->tilegrdoffx > cp->imgareatlx ||
645           cp->tilegrdoffy > cp->imgareatly ||
646           cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx ||
647           cp->tilegrdoffy + cp->tileheight < cp->imgareatly) {
648                 fprintf(stderr, "invalid tile grid offset (%lu, %lu)\n",
649                   (unsigned long) cp->tilegrdoffx, (unsigned long)
650                   cp->tilegrdoffy);
651                 goto error;
652         }
653
654         cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx,
655           cp->tilewidth);
656         cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy,
657           cp->tileheight);
658         cp->numtiles = cp->numhtiles * cp->numvtiles;
659
660         if (ilyrrates && numilyrrates > 0) {
661                 tcp->numlyrs = numilyrrates + 1;
662                 if (!(tcp->ilyrrates = jas_malloc((tcp->numlyrs - 1) *
663                   sizeof(jpc_fix_t)))) {
664                         goto error;
665                 }
666                 for (i = 0; i < JAS_CAST(int, tcp->numlyrs - 1); ++i) {
667                         tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]);
668                 }
669         }
670
671         /* Ensure that the integer mode is used in the case of lossless
672           coding. */
673         if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) {
674                 fprintf(stderr, "cannot use real mode for lossless coding\n");
675                 goto error;
676         }
677
678         /* Ensure that the precinct width is valid. */
679         if (prcwidthexpn > 15) {
680                 fprintf(stderr, "invalid precinct width\n");
681                 goto error;
682         }
683
684         /* Ensure that the precinct height is valid. */
685         if (prcheightexpn > 15) {
686                 fprintf(stderr, "invalid precinct height\n");
687                 goto error;
688         }
689
690         /* Ensure that the code block width is valid. */
691         if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) {
692                 fprintf(stderr, "invalid code block width %d\n",
693                   JPC_POW2(cp->tccp.cblkwidthexpn));
694                 goto error;
695         }
696
697         /* Ensure that the code block height is valid. */
698         if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) {
699                 fprintf(stderr, "invalid code block height %d\n",
700                   JPC_POW2(cp->tccp.cblkheightexpn));
701                 goto error;
702         }
703
704         /* Ensure that the code block size is not too large. */
705         if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) {
706                 fprintf(stderr, "code block size too large\n");
707                 goto error;
708         }
709
710         /* Ensure that the number of layers is valid. */
711         if (cp->tcp.numlyrs > 16384) {
712                 fprintf(stderr, "too many layers\n");
713                 goto error;
714         }
715
716         /* There must be at least one resolution level. */
717         if (cp->tccp.maxrlvls < 1) {
718                 fprintf(stderr, "must be at least one resolution level\n");
719                 goto error;
720         }
721
722         /* Ensure that the number of guard bits is valid. */
723         if (cp->tccp.numgbits > 8) {
724                 fprintf(stderr, "invalid number of guard bits\n");
725                 goto error;
726         }
727
728         /* Ensure that the rate is within the legal range. */
729         if (cp->totalsize != UINT_FAST32_MAX && cp->totalsize > cp->rawsize) {
730                 fprintf(stderr, "warning: specified rate is unreasonably large (%lu > %lu)\n", (unsigned long) cp->totalsize, (unsigned long) cp->rawsize);
731         }
732
733         /* Ensure that the intermediate layer rates are valid. */
734         if (tcp->numlyrs > 1) {
735                 /* The intermediate layers rates must increase monotonically. */
736                 for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) {
737                         if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) {
738                                 fprintf(stderr, "intermediate layer rates must increase monotonically\n");
739                                 goto error;
740                         }
741                 }
742                 /* The intermediate layer rates must be less than the overall rate. */
743                 if (cp->totalsize != UINT_FAST32_MAX) {
744                         for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) {
745                                 if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize)
746                                   / cp->rawsize) {
747                                         fprintf(stderr, "warning: intermediate layer rates must be less than overall rate\n");
748                                         goto error;
749                                 }
750                         }
751                 }
752         }
753
754         if (ilyrrates) {
755                 jas_free(ilyrrates);
756         }
757
758         return cp;
759
760 error:
761
762         if (ilyrrates) {
763                 jas_free(ilyrrates);
764         }
765         if (tvp) {
766                 jas_tvparser_destroy(tvp);
767         }
768         if (cp) {
769                 jpc_enc_cp_destroy(cp);
770         }
771         return 0;
772 }
773
774 void jpc_enc_cp_destroy(jpc_enc_cp_t *cp)
775 {
776         if (cp->ccps) {
777                 if (cp->tcp.ilyrrates) {
778                         jas_free(cp->tcp.ilyrrates);
779                 }
780                 jas_free(cp->ccps);
781         }
782         jas_free(cp);
783 }
784
785 int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size)
786 {
787         char *cp;
788         jpc_flt_t f;
789
790         /* Note: This function must not modify output size on failure. */
791         if ((cp = strchr(s, 'B'))) {
792                 *size = atoi(s);
793         } else {
794                 f = atof(s);
795                 if (f < 0) {
796                         *size = 0;
797                 } else if (f > 1.0) {
798                         *size = rawsize + 1;
799                 } else {
800                         *size = f * rawsize;
801                 }
802         }
803         return 0;
804 }
805
806 /******************************************************************************\
807 * Encoder constructor and destructor.
808 \******************************************************************************/
809
810 jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image)
811 {
812         jpc_enc_t *enc;
813
814         enc = 0;
815
816         if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) {
817                 goto error;
818         }
819
820         enc->image = image;
821         enc->out = out;
822         enc->cp = cp;
823         enc->cstate = 0;
824         enc->tmpstream = 0;
825         enc->mrk = 0;
826         enc->curtile = 0;
827
828         if (!(enc->cstate = jpc_cstate_create())) {
829                 goto error;
830         }
831         enc->len = 0;
832         enc->mainbodysize = 0;
833
834         return enc;
835
836 error:
837
838         if (enc) {
839                 jpc_enc_destroy(enc);
840         }
841         return 0;
842 }
843
844 void jpc_enc_destroy(jpc_enc_t *enc)
845 {
846         /* The image object (i.e., enc->image) and output stream object
847         (i.e., enc->out) are created outside of the encoder.
848         Therefore, they must not be destroyed here. */
849
850         if (enc->curtile) {
851                 jpc_enc_tile_destroy(enc->curtile);
852         }
853         if (enc->cp) {
854                 jpc_enc_cp_destroy(enc->cp);
855         }
856         if (enc->cstate) {
857                 jpc_cstate_destroy(enc->cstate);
858         }
859         if (enc->tmpstream) {
860                 jas_stream_close(enc->tmpstream);
861         }
862
863         jas_free(enc);
864 }
865
866 /******************************************************************************\
867 * Code.
868 \******************************************************************************/
869
870 static int jpc_calcssmant(jpc_fix_t stepsize)
871 {
872         int n;
873         int e;
874         int m;
875
876         n = jpc_firstone(stepsize);
877         e = n - JPC_FIX_FRACBITS;
878         if (n >= 11) {
879                 m = (stepsize >> (n - 11)) & 0x7ff;
880         } else {
881                 m = (stepsize & ((1 << n) - 1)) << (11 - n);
882         }
883         return m;
884 }
885
886 static int jpc_calcssexp(jpc_fix_t stepsize)
887 {
888         return jpc_firstone(stepsize) - JPC_FIX_FRACBITS;
889 }
890
891 static int jpc_enc_encodemainhdr(jpc_enc_t *enc)
892 {
893         jpc_siz_t *siz;
894         jpc_cod_t *cod;
895         jpc_qcd_t *qcd;
896         int i;
897 long startoff;
898 long mainhdrlen;
899         jpc_enc_cp_t *cp;
900         jpc_qcc_t *qcc;
901         jpc_enc_tccp_t *tccp;
902         uint_fast16_t cmptno;
903         jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
904         jpc_fix_t mctsynweight;
905         jpc_enc_tcp_t *tcp;
906         jpc_tsfb_t *tsfb;
907         jpc_tsfb_band_t *bandinfo;
908         uint_fast16_t numbands;
909         uint_fast16_t bandno;
910         uint_fast16_t rlvlno;
911         uint_fast16_t analgain;
912         jpc_fix_t absstepsize;
913         char buf[1024];
914         jpc_com_t *com;
915
916         cp = enc->cp;
917
918 startoff = jas_stream_getrwcount(enc->out);
919
920         /* Write SOC marker segment. */
921         if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) {
922                 return -1;
923         }
924         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
925                 fprintf(stderr, "cannot write SOC marker\n");
926                 return -1;
927         }
928         jpc_ms_destroy(enc->mrk);
929         enc->mrk = 0;
930
931         /* Write SIZ marker segment. */
932         if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) {
933                 return -1;
934         }
935         siz = &enc->mrk->parms.siz;
936         siz->caps = 0;
937         siz->xoff = cp->imgareatlx;
938         siz->yoff = cp->imgareatly;
939         siz->width = cp->refgrdwidth;
940         siz->height = cp->refgrdheight;
941         siz->tilexoff = cp->tilegrdoffx;
942         siz->tileyoff = cp->tilegrdoffy;
943         siz->tilewidth = cp->tilewidth;
944         siz->tileheight = cp->tileheight;
945         siz->numcomps = cp->numcmpts;
946         siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t));
947         assert(siz->comps);
948         for (i = 0; i < JAS_CAST(int, cp->numcmpts); ++i) {
949                 siz->comps[i].prec = cp->ccps[i].prec;
950                 siz->comps[i].sgnd = cp->ccps[i].sgnd;
951                 siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx;
952                 siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy;
953         }
954         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
955                 fprintf(stderr, "cannot write SIZ marker\n");
956                 return -1;
957         }
958         jpc_ms_destroy(enc->mrk);
959         enc->mrk = 0;
960
961         if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) {
962                 return -1;
963         }
964         sprintf(buf, "Creator: JasPer Version %s", jas_getversion());
965         com = &enc->mrk->parms.com;
966         com->len = strlen(buf);
967         com->regid = JPC_COM_LATIN;
968         if (!(com->data = JAS_CAST(uchar *, jas_strdup(buf)))) {
969                 abort();
970         }
971         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
972                 fprintf(stderr, "cannot write COM marker\n");
973                 return -1;
974         }
975         jpc_ms_destroy(enc->mrk);
976         enc->mrk = 0;
977
978 #if 0
979         if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) {
980                 return -1;
981         }
982         crg = &enc->mrk->parms.crg;
983         crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t));
984         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
985                 fprintf(stderr, "cannot write CRG marker\n");
986                 return -1;
987         }
988         jpc_ms_destroy(enc->mrk);
989         enc->mrk = 0;
990 #endif
991
992         tcp = &cp->tcp;
993         tccp = &cp->tccp;
994         for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) {
995                 tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1);
996                 jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls,
997                   bandinfos);
998                 jpc_tsfb_destroy(tsfb);
999                 mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno);
1000                 numbands = 3 * tccp->maxrlvls - 2;
1001                 for (bandno = 0, bandinfo = bandinfos; bandno < numbands;
1002                   ++bandno, ++bandinfo) {
1003                         rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0;
1004                         analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls,
1005                           rlvlno, bandinfo->orient);
1006                         if (!tcp->intmode) {
1007                                 absstepsize = jpc_fix_div(jpc_inttofix(1 <<
1008                                   (analgain + 1)), bandinfo->synenergywt);
1009                         } else {
1010                                 absstepsize = jpc_inttofix(1);
1011                         }       
1012                         cp->ccps[cmptno].stepsizes[bandno] =
1013                           jpc_abstorelstepsize(absstepsize,
1014                           cp->ccps[cmptno].prec + analgain);
1015                 }
1016                 cp->ccps[cmptno].numstepsizes = numbands;
1017         }
1018
1019         if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
1020                 return -1;
1021         }
1022         cod = &enc->mrk->parms.cod;
1023         cod->csty = cp->tccp.csty | cp->tcp.csty;
1024         cod->compparms.csty = cp->tccp.csty | cp->tcp.csty;
1025         cod->compparms.numdlvls = cp->tccp.maxrlvls - 1;
1026         cod->compparms.numrlvls = cp->tccp.maxrlvls;
1027         cod->prg = cp->tcp.prg;
1028         cod->numlyrs = cp->tcp.numlyrs;
1029         cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn);
1030         cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn);
1031         cod->compparms.cblksty = cp->tccp.cblksty;
1032         cod->compparms.qmfbid = cp->tccp.qmfbid;
1033         cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE);
1034         if (tccp->csty & JPC_COX_PRT) {
1035                 for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) {
1036                         cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno];
1037                         cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno];
1038                 }
1039         }
1040         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1041                 fprintf(stderr, "cannot write COD marker\n");
1042                 return -1;
1043         }
1044         jpc_ms_destroy(enc->mrk);
1045         enc->mrk = 0;
1046
1047         if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) {
1048                 return -1;
1049         }
1050         qcd = &enc->mrk->parms.qcd;
1051         qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
1052           JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1053         qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes;
1054         qcd->compparms.numguard = cp->tccp.numgbits;
1055         qcd->compparms.stepsizes = cp->ccps[0].stepsizes;
1056         if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1057                 return -1;
1058         }
1059         /* We do not want the step size array to be freed! */
1060         qcd->compparms.stepsizes = 0;
1061         jpc_ms_destroy(enc->mrk);
1062         enc->mrk = 0;
1063
1064         tccp = &cp->tccp;
1065         for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) {
1066                 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
1067                         return -1;
1068                 }
1069                 qcc = &enc->mrk->parms.qcc;
1070                 qcc->compno = cmptno;
1071                 qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ?
1072                   JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1073                 qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes;
1074                 qcc->compparms.numguard = cp->tccp.numgbits;
1075                 qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes;
1076                 if (jpc_putms(enc->out, enc->cstate, enc->mrk)) {
1077                         return -1;
1078                 }
1079                 /* We do not want the step size array to be freed! */
1080                 qcc->compparms.stepsizes = 0;
1081                 jpc_ms_destroy(enc->mrk);
1082                 enc->mrk = 0;
1083         }
1084
1085 #define MAINTLRLEN      2
1086         mainhdrlen = jas_stream_getrwcount(enc->out) - startoff;
1087         enc->len += mainhdrlen;
1088         if (enc->cp->totalsize != UINT_FAST32_MAX) {
1089                 uint_fast32_t overhead;
1090                 overhead = mainhdrlen + MAINTLRLEN;
1091                 enc->mainbodysize = (enc->cp->totalsize >= overhead) ?
1092                   (enc->cp->totalsize - overhead) : 0;
1093         } else {
1094                 enc->mainbodysize = UINT_FAST32_MAX;
1095         }
1096
1097         return 0;
1098 }
1099
1100 static int jpc_enc_encodemainbody(jpc_enc_t *enc)
1101 {
1102         int tileno;
1103         int tilex;
1104         int tiley;
1105         int i;
1106         jpc_sot_t *sot;
1107         jpc_enc_tcmpt_t *comp;
1108         jpc_enc_tcmpt_t *endcomps;
1109         jpc_enc_band_t *band;
1110         jpc_enc_band_t *endbands;
1111         jpc_enc_rlvl_t *lvl;
1112         int rlvlno;
1113         jpc_qcc_t *qcc;
1114         jpc_cod_t *cod;
1115         int adjust;
1116         int j;
1117         int absbandno;
1118         long numbytes;
1119         long tilehdrlen;
1120         long tilelen;
1121         jpc_enc_tile_t *tile;
1122         jpc_enc_cp_t *cp;
1123         double rho;
1124         int lyrno;
1125         int cmptno;
1126         int samestepsizes;
1127         jpc_enc_ccp_t *ccps;
1128         jpc_enc_tccp_t *tccp;
1129 int bandno;
1130 uint_fast32_t x;
1131 uint_fast32_t y;
1132 int mingbits;
1133 int actualnumbps;
1134 jpc_fix_t mxmag;
1135 jpc_fix_t mag;
1136 int numgbits;
1137
1138         cp = enc->cp;
1139
1140         /* Avoid compile warnings. */
1141         numbytes = 0;
1142
1143         for (tileno = 0; tileno < JAS_CAST(int, cp->numtiles); ++tileno) {
1144                 tilex = tileno % cp->numhtiles;
1145                 tiley = tileno / cp->numhtiles;
1146
1147                 if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) {
1148                         abort();
1149                 }
1150
1151                 tile = enc->curtile;
1152
1153                 if (jas_getdbglevel() >= 10) {
1154                         jpc_enc_dump(enc);
1155                 }
1156
1157                 endcomps = &tile->tcmpts[tile->numtcmpts];
1158                 for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) {
1159                         if (!cp->ccps[cmptno].sgnd) {
1160                                 adjust = 1 << (cp->ccps[cmptno].prec - 1);
1161                                 for (i = 0; i < jas_matrix_numrows(comp->data); ++i) {
1162                                         for (j = 0; j < jas_matrix_numcols(comp->data); ++j) {
1163                                                 *jas_matrix_getref(comp->data, i, j) -= adjust;
1164                                         }
1165                                 }
1166                         }
1167                 }
1168
1169                 if (!tile->intmode) {
1170                                 endcomps = &tile->tcmpts[tile->numtcmpts];
1171                                 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1172                                         jas_matrix_asl(comp->data, JPC_FIX_FRACBITS);
1173                                 }
1174                 }
1175
1176                 switch (tile->mctid) {
1177                 case JPC_MCT_RCT:
1178 assert(jas_image_numcmpts(enc->image) == 3);
1179                         jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data,
1180                           tile->tcmpts[2].data);
1181                         break;
1182                 case JPC_MCT_ICT:
1183 assert(jas_image_numcmpts(enc->image) == 3);
1184                         jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data,
1185                           tile->tcmpts[2].data);
1186                         break;
1187                 default:
1188                         break;
1189                 }
1190
1191                 for (i = 0; i < jas_image_numcmpts(enc->image); ++i) {
1192                         comp = &tile->tcmpts[i];
1193                         jpc_tsfb_analyze(comp->tsfb, ((comp->qmfbid == JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), comp->data);
1194
1195                 }
1196
1197
1198                 endcomps = &tile->tcmpts[tile->numtcmpts];
1199                 for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) {
1200                         mingbits = 0;
1201                         absbandno = 0;
1202                         /* All bands must have a corresponding quantizer step size,
1203                           even if they contain no samples and are never coded. */
1204                         /* Some bands may not be hit by the loop below, so we must
1205                           initialize all of the step sizes to a sane value. */
1206                         memset(comp->stepsizes, 0, sizeof(comp->stepsizes));
1207                         for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) {
1208                                 if (!lvl->bands) {
1209                                         absbandno += rlvlno ? 3 : 1;
1210                                         continue;
1211                                 }
1212                                 endbands = &lvl->bands[lvl->numbands];
1213                                 for (band = lvl->bands; band != endbands; ++band) {
1214                                         if (!band->data) {
1215                                                 ++absbandno;
1216                                                 continue;
1217                                         }
1218                                         actualnumbps = 0;
1219                                         mxmag = 0;
1220                                         for (y = 0; y < JAS_CAST(uint_fast32_t, jas_matrix_numrows(band->data)); ++y) {
1221                                                 for (x = 0; x < JAS_CAST(uint_fast32_t, jas_matrix_numcols(band->data)); ++x) {
1222                                                         mag = abs(jas_matrix_get(band->data, y, x));
1223                                                         if (mag > mxmag) {
1224                                                                 mxmag = mag;
1225                                                         }
1226                                                 }
1227                                         }
1228                                         if (tile->intmode) {
1229                                                 actualnumbps = jpc_firstone(mxmag) + 1;
1230                                         } else {
1231                                                 actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS;
1232                                         }
1233                                         numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 +
1234                                           band->analgain);
1235 #if 0
1236 fprintf(stderr, "%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band->analgain, mxmag, actualnumbps, numgbits);
1237 #endif
1238                                         if (numgbits > mingbits) {
1239                                                 mingbits = numgbits;
1240                                         }
1241                                         if (!tile->intmode) {
1242                                                 band->absstepsize = jpc_fix_div(jpc_inttofix(1
1243                                                   << (band->analgain + 1)),
1244                                                   band->synweight);
1245                                         } else {
1246                                                 band->absstepsize = jpc_inttofix(1);
1247                                         }
1248                                         band->stepsize = jpc_abstorelstepsize(
1249                                           band->absstepsize, cp->ccps[cmptno].prec +
1250                                           band->analgain);
1251                                         band->numbps = cp->tccp.numgbits +
1252                                           JPC_QCX_GETEXPN(band->stepsize) - 1;
1253
1254                                         if ((!tile->intmode) && band->data) {
1255                                                 quantize(band->data, band->absstepsize);
1256                                         }
1257
1258                                         comp->stepsizes[absbandno] = band->stepsize;
1259                                         ++absbandno;
1260                                 }
1261                         }
1262
1263                         assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS);
1264                         if (!tile->intmode) {
1265                                 jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS);
1266                         } else {
1267                                 jas_matrix_asl(comp->data, JPC_NUMEXTRABITS);
1268                         }
1269
1270 #if 0
1271 fprintf(stderr, "mingbits %d\n", mingbits);
1272 #endif
1273                         if (mingbits > cp->tccp.numgbits) {
1274                                 fprintf(stderr, "error: too few guard bits (need at least %d)\n",
1275                                   mingbits);
1276                                 return -1;
1277                         }
1278                 }
1279
1280                 if (!(enc->tmpstream = jas_stream_memopen(0, 0))) {
1281                         fprintf(stderr, "cannot open tmp file\n");
1282                         return -1;
1283                 }
1284
1285                 /* Write the tile header. */
1286                 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) {
1287                         return -1;
1288                 }
1289                 sot = &enc->mrk->parms.sot;
1290                 sot->len = 0;
1291                 sot->tileno = tileno;
1292                 sot->partno = 0;
1293                 sot->numparts = 1;
1294                 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1295                         fprintf(stderr, "cannot write SOT marker\n");
1296                         return -1;
1297                 }
1298                 jpc_ms_destroy(enc->mrk);
1299                 enc->mrk = 0;
1300
1301 /************************************************************************/
1302 /************************************************************************/
1303 /************************************************************************/
1304
1305                 tccp = &cp->tccp;
1306                 for (cmptno = 0; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno) {
1307                         comp = &tile->tcmpts[cmptno];
1308                         if (comp->numrlvls != tccp->maxrlvls) {
1309                                 if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) {
1310                                         return -1;
1311                                 }
1312 /* XXX = this is not really correct. we are using comp #0's precint sizes
1313 and other characteristics */
1314                                 comp = &tile->tcmpts[0];
1315                                 cod = &enc->mrk->parms.cod;
1316                                 cod->compparms.csty = 0;
1317                                 cod->compparms.numdlvls = comp->numrlvls - 1;
1318                                 cod->prg = tile->prg;
1319                                 cod->numlyrs = tile->numlyrs;
1320                                 cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn);
1321                                 cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn);
1322                                 cod->compparms.cblksty = comp->cblksty;
1323                                 cod->compparms.qmfbid = comp->qmfbid;
1324                                 cod->mctrans = (tile->mctid != JPC_MCT_NONE);
1325                                 for (i = 0; i < comp->numrlvls; ++i) {
1326                                         cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn;
1327                                         cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn;
1328                                 }
1329                                 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1330                                         return -1;
1331                                 }
1332                                 jpc_ms_destroy(enc->mrk);
1333                                 enc->mrk = 0;
1334                         }
1335                 }
1336
1337                 for (cmptno = 0, comp = tile->tcmpts; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno, ++comp) {
1338                         ccps = &cp->ccps[cmptno];
1339                         if (JAS_CAST(int, ccps->numstepsizes) == comp->numstepsizes) {
1340                                 samestepsizes = 1;
1341                                 for (bandno = 0; bandno < JAS_CAST(int, ccps->numstepsizes); ++bandno) {
1342                                         if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) {
1343                                                 samestepsizes = 0;
1344                                                 break;
1345                                         }
1346                                 }
1347                         } else {
1348                                 samestepsizes = 0;
1349                         }
1350                         if (!samestepsizes) {
1351                                 if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) {
1352                                         return -1;
1353                                 }
1354                                 qcc = &enc->mrk->parms.qcc;
1355                                 qcc->compno = cmptno;
1356                                 qcc->compparms.numguard = cp->tccp.numgbits;
1357                                 qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ?
1358                                   JPC_QCX_SEQNT : JPC_QCX_NOQNT;
1359                                 qcc->compparms.numstepsizes = comp->numstepsizes;
1360                                 qcc->compparms.stepsizes = comp->stepsizes;
1361                                 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1362                                         return -1;
1363                                 }
1364                                 qcc->compparms.stepsizes = 0;
1365                                 jpc_ms_destroy(enc->mrk);
1366                                 enc->mrk = 0;
1367                         }
1368                 }
1369
1370                 /* Write a SOD marker to indicate the end of the tile header. */
1371                 if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) {
1372                         return -1;
1373                 }
1374                 if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) {
1375                         fprintf(stderr, "cannot write SOD marker\n");
1376                         return -1;
1377                 }
1378                 jpc_ms_destroy(enc->mrk);
1379                 enc->mrk = 0;
1380 tilehdrlen = jas_stream_getrwcount(enc->tmpstream);
1381
1382 /************************************************************************/
1383 /************************************************************************/
1384 /************************************************************************/
1385
1386 if (jpc_enc_enccblks(enc)) {
1387         abort();
1388         return -1;
1389 }
1390
1391                 cp = enc->cp;
1392                 rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) /
1393                   ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight -
1394                   cp->imgareatly));
1395                 tile->rawsize = cp->rawsize * rho;
1396
1397                 for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) {
1398                         tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl(
1399                           cp->tcp.ilyrrates[lyrno]);
1400                 }
1401                 tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ?
1402                   (rho * enc->mainbodysize) : UINT_FAST32_MAX;
1403                 for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1404                         if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) {
1405                                 if (tilehdrlen <= JAS_CAST(long, tile->lyrsizes[lyrno])) {
1406                                         tile->lyrsizes[lyrno] -= tilehdrlen;
1407                                 } else {
1408                                         tile->lyrsizes[lyrno] = 0;
1409                                 }
1410                         }
1411                 }
1412
1413                 if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) {
1414                         return -1;
1415                 }
1416
1417 #if 0
1418 fprintf(stderr, "ENCODE TILE DATA\n");
1419 #endif
1420                 if (jpc_enc_encodetiledata(enc)) {
1421                         fprintf(stderr, "dotile failed\n");
1422                         return -1;
1423                 }
1424
1425 /************************************************************************/
1426 /************************************************************************/
1427 /************************************************************************/
1428
1429 /************************************************************************/
1430 /************************************************************************/
1431 /************************************************************************/
1432
1433                 tilelen = jas_stream_tell(enc->tmpstream);
1434
1435                 if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) {
1436                         return -1;
1437                 }
1438                 jpc_putuint32(enc->tmpstream, tilelen);
1439
1440                 if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) {
1441                         return -1;
1442                 }
1443                 if (jpc_putdata(enc->out, enc->tmpstream, -1)) {
1444                         return -1;
1445                 }
1446                 enc->len += tilelen;
1447
1448                 jas_stream_close(enc->tmpstream);
1449                 enc->tmpstream = 0;
1450
1451                 jpc_enc_tile_destroy(enc->curtile);
1452                 enc->curtile = 0;
1453
1454         }
1455
1456         return 0;
1457 }
1458
1459 int jpc_enc_encodetiledata(jpc_enc_t *enc)
1460 {
1461 assert(enc->tmpstream);
1462         if (jpc_enc_encpkts(enc, enc->tmpstream)) {
1463                 return -1;
1464         }
1465         return 0;
1466 }
1467
1468 int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk)
1469 {
1470         jpc_enc_pass_t *pass;
1471         int i;
1472         jas_stream_memobj_t *smo;
1473
1474         smo = cblk->stream->obj_;
1475
1476         pass = passes;
1477         for (i = 0; i < numpasses; ++i) {
1478                 fprintf(stderr, "start=%d end=%d type=%d term=%d lyrno=%d firstchar=%02x size=%ld pos=%ld\n",
1479                   (int)pass->start, (int)pass->end, (int)pass->type, (int)pass->term, (int)pass->lyrno,
1480                   smo->buf_[pass->start], (long)smo->len_, (long)smo->pos_);
1481 #if 0
1482                 jas_memdump(stderr, &smo->buf_[pass->start], pass->end - pass->start);
1483 #endif
1484                 ++pass;
1485         }
1486         return 0;
1487 }
1488
1489 void quantize(jas_matrix_t *data, jpc_fix_t stepsize)
1490 {
1491         int i;
1492         int j;
1493         jpc_fix_t t;
1494
1495         if (stepsize == jpc_inttofix(1)) {
1496                 return;
1497         }
1498
1499         for (i = 0; i < jas_matrix_numrows(data); ++i) {
1500                 for (j = 0; j < jas_matrix_numcols(data); ++j) {
1501                         t = jas_matrix_get(data, i, j);
1502
1503 {
1504         if (t < 0) {
1505                 t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize));
1506         } else {
1507                 t = jpc_fix_div(t, stepsize);
1508         }
1509 }
1510
1511                         jas_matrix_set(data, i, j, t);
1512                 }
1513         }
1514 }
1515
1516 void calcrdslopes(jpc_enc_cblk_t *cblk)
1517 {
1518         jpc_enc_pass_t *endpasses;
1519         jpc_enc_pass_t *pass0;
1520         jpc_enc_pass_t *pass1;
1521         jpc_enc_pass_t *pass2;
1522         jpc_flt_t slope0;
1523         jpc_flt_t slope;
1524         jpc_flt_t dd;
1525         long dr;
1526
1527         endpasses = &cblk->passes[cblk->numpasses];
1528         pass2 = cblk->passes;
1529         slope0 = 0;
1530         while (pass2 != endpasses) {
1531                 pass0 = 0;
1532                 for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) {
1533                         dd = pass1->cumwmsedec;
1534                         dr = pass1->end;
1535                         if (pass0) {
1536                                 dd -= pass0->cumwmsedec;
1537                                 dr -= pass0->end;
1538                         }
1539                         if (dd <= 0) {
1540                                 pass1->rdslope = JPC_BADRDSLOPE;
1541                                 if (pass1 >= pass2) {
1542                                         pass2 = &pass1[1];
1543                                 }
1544                                 continue;
1545                         }
1546                         if (pass1 < pass2 && pass1->rdslope <= 0) {
1547                                 continue;
1548                         }
1549                         if (!dr) {
1550                                 assert(pass0);
1551                                 pass0->rdslope = 0;
1552                                 break;
1553                         }
1554                         slope = dd / dr;
1555                         if (pass0 && slope >= slope0) {
1556                                 pass0->rdslope = 0;
1557                                 break;
1558                         }
1559                         pass1->rdslope = slope;
1560                         if (pass1 >= pass2) {
1561                                 pass2 = &pass1[1];
1562                         }
1563                         pass0 = pass1;
1564                         slope0 = slope;
1565                 }
1566         }
1567
1568 #if 0
1569         for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) {
1570 if (pass0->rdslope > 0.0) {
1571                 fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes,
1572                   fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope);
1573 }
1574         }
1575 #endif
1576 }
1577
1578 void dump_layeringinfo(jpc_enc_t *enc)
1579 {
1580
1581         jpc_enc_tcmpt_t *tcmpt;
1582         int tcmptno;
1583         jpc_enc_rlvl_t *rlvl;
1584         int rlvlno;
1585         jpc_enc_band_t *band;
1586         int bandno;
1587         jpc_enc_prc_t *prc;
1588         int prcno;
1589         jpc_enc_cblk_t *cblk;
1590         int cblkno;
1591         jpc_enc_pass_t *pass;
1592         int passno;
1593         int lyrno;
1594         jpc_enc_tile_t *tile;
1595
1596         tile = enc->curtile;
1597
1598         for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1599                 fprintf(stderr, "lyrno = %02d\n", lyrno);
1600                 for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts;
1601                   ++tcmptno, ++tcmpt) {
1602                         for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
1603                           ++rlvlno, ++rlvl) {
1604                                 if (!rlvl->bands) {
1605                                         continue;
1606                                 }
1607                                 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
1608                                   ++bandno, ++band) {
1609                                         if (!band->data) {
1610                                                 continue;
1611                                         }
1612                                         for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
1613                                           ++prcno, ++prc) {
1614                                                 if (!prc->cblks) {
1615                                                         continue;
1616                                                 }
1617                                                 for (cblkno = 0, cblk = prc->cblks; cblkno <
1618                                                   prc->numcblks; ++cblkno, ++cblk) {
1619                                                         for (passno = 0, pass = cblk->passes; passno <
1620                                                           cblk->numpasses && pass->lyrno == lyrno;
1621                                                           ++passno, ++pass) {
1622                                                                 fprintf(stderr, "lyrno=%02d cmptno=%02d rlvlno=%02d bandno=%02d prcno=%02d cblkno=%03d passno=%03d\n", lyrno, tcmptno, rlvlno, bandno, prcno, cblkno, passno);
1623                                                         }
1624                                                 }
1625                                         }
1626                                 }
1627                         }
1628                 }
1629         }
1630 }
1631
1632 int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens)
1633 {
1634         jpc_flt_t lo;
1635         jpc_flt_t hi;
1636         jas_stream_t *out;
1637         long cumlen;
1638         int lyrno;
1639         jpc_flt_t thresh;
1640         jpc_flt_t goodthresh;
1641         int success;
1642         long pos;
1643         long oldpos;
1644         int numiters;
1645
1646         jpc_enc_tcmpt_t *comp;
1647         jpc_enc_tcmpt_t *endcomps;
1648         jpc_enc_rlvl_t *lvl;
1649         jpc_enc_rlvl_t *endlvls;
1650         jpc_enc_band_t *band;
1651         jpc_enc_band_t *endbands;
1652         jpc_enc_cblk_t *cblk;
1653         jpc_enc_cblk_t *endcblks;
1654         jpc_enc_pass_t *pass;
1655         jpc_enc_pass_t *endpasses;
1656         jpc_enc_pass_t *pass1;
1657         jpc_flt_t mxrdslope;
1658         jpc_flt_t mnrdslope;
1659         jpc_enc_tile_t *tile;
1660         jpc_enc_prc_t *prc;
1661         int prcno;
1662
1663         tile = enc->curtile;
1664
1665         for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) {
1666                 if (cumlens[lyrno - 1] > cumlens[lyrno]) {
1667                         abort();
1668                 }
1669         }
1670
1671         if (!(out = jas_stream_memopen(0, 0))) {
1672                 return -1;
1673         }
1674
1675
1676         /* Find minimum and maximum R-D slope values. */
1677         mnrdslope = DBL_MAX;
1678         mxrdslope = 0;
1679         endcomps = &tile->tcmpts[tile->numtcmpts];
1680         for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1681                 endlvls = &comp->rlvls[comp->numrlvls];
1682                 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1683                         if (!lvl->bands) {
1684                                 continue;
1685                         }
1686                         endbands = &lvl->bands[lvl->numbands];
1687                         for (band = lvl->bands; band != endbands; ++band) {
1688                                 if (!band->data) {
1689                                         continue;
1690                                 }
1691                                 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1692                                         if (!prc->cblks) {
1693                                                 continue;
1694                                         }
1695                                         endcblks = &prc->cblks[prc->numcblks];
1696                                         for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1697                                                 calcrdslopes(cblk);
1698                                                 endpasses = &cblk->passes[cblk->numpasses];
1699                                                 for (pass = cblk->passes; pass != endpasses; ++pass) {
1700                                                         if (pass->rdslope > 0) {
1701                                                                 if (pass->rdslope < mnrdslope) {
1702                                                                         mnrdslope = pass->rdslope;
1703                                                                 }
1704                                                                 if (pass->rdslope > mxrdslope) {
1705                                                                         mxrdslope = pass->rdslope;
1706                                                                 }
1707                                                         }
1708                                                 }
1709                                         }
1710                                 }
1711                         }
1712                 }
1713         }
1714 if (jas_getdbglevel()) {
1715         fprintf(stderr, "min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope);
1716 }
1717
1718         jpc_init_t2state(enc, 1);
1719
1720         for (lyrno = 0; lyrno < numlyrs; ++lyrno) {
1721
1722                 lo = mnrdslope;
1723                 hi = mxrdslope;
1724
1725                 success = 0;
1726                 goodthresh = 0;
1727                 numiters = 0;
1728
1729                 do {
1730
1731                         cumlen = cumlens[lyrno];
1732                         if (cumlen == UINT_FAST32_MAX) {
1733                                 /* Only the last layer can be free of a rate
1734                                   constraint (e.g., for lossless coding). */
1735                                 assert(lyrno == numlyrs - 1);
1736                                 goodthresh = -1;
1737                                 success = 1;
1738                                 break;
1739                         }
1740
1741                         thresh = (lo + hi) / 2;
1742
1743                         /* Save the tier 2 coding state. */
1744                         jpc_save_t2state(enc);
1745                         oldpos = jas_stream_tell(out);
1746                         assert(oldpos >= 0);
1747
1748                         /* Assign all passes with R-D slopes greater than or
1749                           equal to the current threshold to this layer. */
1750                         endcomps = &tile->tcmpts[tile->numtcmpts];
1751                         for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1752                                 endlvls = &comp->rlvls[comp->numrlvls];
1753                                 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1754                                         if (!lvl->bands) {
1755                                                 continue;
1756                                         }
1757                                         endbands = &lvl->bands[lvl->numbands];
1758                                         for (band = lvl->bands; band != endbands; ++band) {
1759                                                 if (!band->data) {
1760                                                         continue;
1761                                                 }
1762                                                 for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1763                                                         if (!prc->cblks) {
1764                                                                 continue;
1765                                                         }
1766                                                         endcblks = &prc->cblks[prc->numcblks];
1767                                                         for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1768                                                                 if (cblk->curpass) {
1769                                                                         endpasses = &cblk->passes[cblk->numpasses];
1770                                                                         pass1 = cblk->curpass;
1771                                                                         for (pass = cblk->curpass; pass != endpasses; ++pass) {
1772                                                                                 if (pass->rdslope >= thresh) {
1773                                                                                         pass1 = &pass[1];
1774                                                                                 }
1775                                                                         }
1776                                                                         for (pass = cblk->curpass; pass != pass1; ++pass) {
1777                                                                                 pass->lyrno = lyrno;
1778                                                                         }
1779                                                                         for (; pass != endpasses; ++pass) {
1780                                                                                 pass->lyrno = -1;
1781                                                                         }
1782                                                                 }
1783                                                         }
1784                                                 }
1785                                         }
1786                                 }
1787                         }
1788
1789                         /* Perform tier 2 coding. */
1790                         endcomps = &tile->tcmpts[tile->numtcmpts];
1791                         for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1792                                 endlvls = &comp->rlvls[comp->numrlvls];
1793                                 for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1794                                         if (!lvl->bands) {
1795                                                 continue;
1796                                         }
1797                                         for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
1798                                                 if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
1799                                                         return -1;
1800                                                 }
1801                                         }
1802                                 }
1803                         }
1804
1805                         pos = jas_stream_tell(out);
1806
1807                         /* Check the rate constraint. */
1808                         assert(pos >= 0);
1809                         if (pos > cumlen) {
1810                                 /* The rate is too high. */
1811                                 lo = thresh;
1812                         } else if (pos <= cumlen) {
1813                                 /* The rate is low enough, so try higher. */
1814                                 hi = thresh;
1815                                 if (!success || thresh < goodthresh) {
1816                                         goodthresh = thresh;
1817                                         success = 1;
1818                                 }
1819                         }
1820
1821                         /* Save the tier 2 coding state. */
1822                         jpc_restore_t2state(enc);
1823                         if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) {
1824                                 abort();
1825                         }
1826
1827 if (jas_getdbglevel()) {
1828 fprintf(stderr, "maxlen=%08ld actuallen=%08ld thresh=%f\n", cumlen, pos, thresh);
1829 }
1830
1831                         ++numiters;
1832                 } while (lo < hi - 1e-3 && numiters < 32);
1833
1834                 if (!success) {
1835                         fprintf(stderr, "warning: empty layer generated\n");
1836                 }
1837
1838 if (jas_getdbglevel()) {
1839 fprintf(stderr, "success %d goodthresh %f\n", success, goodthresh);
1840 }
1841
1842                 /* Assign all passes with R-D slopes greater than or
1843                   equal to the selected threshold to this layer. */
1844                 endcomps = &tile->tcmpts[tile->numtcmpts];
1845                 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1846                         endlvls = &comp->rlvls[comp->numrlvls];
1847                         for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1848 if (!lvl->bands) {
1849         continue;
1850 }
1851                                 endbands = &lvl->bands[lvl->numbands];
1852                                 for (band = lvl->bands; band != endbands; ++band) {
1853                                         if (!band->data) {
1854                                                 continue;
1855                                         }
1856                                         for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
1857                                                 if (!prc->cblks) {
1858                                                         continue;
1859                                                 }
1860                                                 endcblks = &prc->cblks[prc->numcblks];
1861                                                 for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
1862                                                         if (cblk->curpass) {
1863                                                                 endpasses = &cblk->passes[cblk->numpasses];
1864                                                                 pass1 = cblk->curpass;
1865                                                                 if (success) {
1866                                                                         for (pass = cblk->curpass; pass != endpasses; ++pass) {
1867                                                                                 if (pass->rdslope >= goodthresh) {
1868                                                                                         pass1 = &pass[1];
1869                                                                                 }
1870                                                                         }
1871                                                                 }
1872                                                                 for (pass = cblk->curpass; pass != pass1; ++pass) {
1873                                                                         pass->lyrno = lyrno;
1874                                                                 }
1875                                                                 for (; pass != endpasses; ++pass) {
1876                                                                         pass->lyrno = -1;
1877                                                                 }
1878                                                         }
1879                                                 }
1880                                         }
1881                                 }
1882                         }
1883                 }
1884
1885                 /* Perform tier 2 coding. */
1886                 endcomps = &tile->tcmpts[tile->numtcmpts];
1887                 for (comp = tile->tcmpts; comp != endcomps; ++comp) {
1888                         endlvls = &comp->rlvls[comp->numrlvls];
1889                         for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
1890                                 if (!lvl->bands) {
1891                                         continue;
1892                                 }
1893                                 for (prcno = 0; prcno < lvl->numprcs; ++prcno) {
1894                                         if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) {
1895                                                 return -1;
1896                                         }
1897                                 }
1898                         }
1899                 }
1900         }
1901
1902         if (jas_getdbglevel() >= 5) {
1903                 dump_layeringinfo(enc);
1904         }
1905
1906         jas_stream_close(out);
1907
1908         JAS_DBGLOG(10, ("done doing rateallocation\n"));
1909 #if 0
1910 fprintf(stderr, "DONE RATE ALLOCATE\n");
1911 #endif
1912
1913         return 0;
1914 }
1915
1916 /******************************************************************************\
1917 * Tile constructors and destructors.
1918 \******************************************************************************/
1919
1920 jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno)
1921 {
1922         jpc_enc_tile_t *tile;
1923         uint_fast32_t htileno;
1924         uint_fast32_t vtileno;
1925         uint_fast16_t lyrno;
1926         uint_fast16_t cmptno;
1927         jpc_enc_tcmpt_t *tcmpt;
1928
1929         if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) {
1930                 goto error;
1931         }
1932
1933         /* Initialize a few members used in error recovery. */
1934         tile->tcmpts = 0;
1935         tile->lyrsizes = 0;
1936         tile->numtcmpts = cp->numcmpts;
1937         tile->pi = 0;
1938
1939         tile->tileno = tileno;
1940         htileno = tileno % cp->numhtiles;
1941         vtileno = tileno / cp->numhtiles;
1942
1943         /* Calculate the coordinates of the top-left and bottom-right
1944           corners of the tile. */
1945         tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth,
1946           cp->imgareatlx);
1947         tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight,
1948           cp->imgareatly);
1949         tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth,
1950           cp->refgrdwidth);
1951         tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight,
1952           cp->refgrdheight);
1953
1954         /* Initialize some tile coding parameters. */
1955         tile->intmode = cp->tcp.intmode;
1956         tile->csty = cp->tcp.csty;
1957         tile->prg = cp->tcp.prg;
1958         tile->mctid = cp->tcp.mctid;
1959
1960         tile->numlyrs = cp->tcp.numlyrs;
1961         if (!(tile->lyrsizes = jas_malloc(tile->numlyrs *
1962           sizeof(uint_fast32_t)))) {
1963                 goto error;
1964         }
1965         for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) {
1966                 tile->lyrsizes[lyrno] = 0;
1967         }
1968
1969         /* Allocate an array for the per-tile-component information. */
1970         if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) {
1971                 goto error;
1972         }
1973         /* Initialize a few members critical for error recovery. */
1974         for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
1975           ++cmptno, ++tcmpt) {
1976                 tcmpt->rlvls = 0;
1977                 tcmpt->tsfb = 0;
1978                 tcmpt->data = 0;
1979         }
1980         /* Initialize the per-tile-component information. */
1981         for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
1982           ++cmptno, ++tcmpt) {
1983                 if (!tcmpt_create(tcmpt, cp, image, tile)) {
1984                         goto error;
1985                 }
1986         }
1987
1988         /* Initialize the synthesis weights for the MCT. */
1989         switch (tile->mctid) {
1990         case JPC_MCT_RCT:
1991                 tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0));
1992                 tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875));
1993                 tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875));
1994                 break;
1995         case JPC_MCT_ICT:
1996                 tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000));
1997                 tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584));
1998                 tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755));
1999                 break;
2000         default:
2001         case JPC_MCT_NONE:
2002                 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts;
2003                   ++cmptno, ++tcmpt) {
2004                         tcmpt->synweight = JPC_FIX_ONE;
2005                 }
2006                 break;
2007         }
2008
2009         if (!(tile->pi = jpc_enc_pi_create(cp, tile))) {
2010                 goto error;
2011         }
2012
2013         return tile;
2014
2015 error:
2016
2017         if (tile) {
2018                 jpc_enc_tile_destroy(tile);
2019         }
2020         return 0;
2021 }
2022
2023 void jpc_enc_tile_destroy(jpc_enc_tile_t *tile)
2024 {
2025         jpc_enc_tcmpt_t *tcmpt;
2026         uint_fast16_t cmptno;
2027
2028         if (tile->tcmpts) {
2029                 for (cmptno = 0, tcmpt = tile->tcmpts; cmptno <
2030                   tile->numtcmpts; ++cmptno, ++tcmpt) {
2031                         tcmpt_destroy(tcmpt);
2032                 }
2033                 jas_free(tile->tcmpts);
2034         }
2035         if (tile->lyrsizes) {
2036                 jas_free(tile->lyrsizes);
2037         }
2038         if (tile->pi) {
2039                 jpc_pi_destroy(tile->pi);
2040         }
2041         jas_free(tile);
2042 }
2043
2044 static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp,
2045   jas_image_t *image, jpc_enc_tile_t *tile)
2046 {
2047         uint_fast16_t cmptno;
2048         uint_fast16_t rlvlno;
2049         jpc_enc_rlvl_t *rlvl;
2050         uint_fast32_t tlx;
2051         uint_fast32_t tly;
2052         uint_fast32_t brx;
2053         uint_fast32_t bry;
2054         uint_fast32_t cmpttlx;
2055         uint_fast32_t cmpttly;
2056         jpc_enc_ccp_t *ccp;
2057         jpc_tsfb_band_t bandinfos[JPC_MAXBANDS];
2058
2059         tcmpt->tile = tile;
2060         tcmpt->tsfb = 0;
2061         tcmpt->data = 0;
2062         tcmpt->rlvls = 0;
2063
2064         /* Deduce the component number. */
2065         cmptno = tcmpt - tile->tcmpts;
2066
2067         ccp = &cp->ccps[cmptno];
2068
2069         /* Compute the coordinates of the top-left and bottom-right
2070           corners of this tile-component. */
2071         tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx);
2072         tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy);
2073         brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx);
2074         bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy);
2075
2076         /* Create a sequence to hold the tile-component sample data. */
2077         if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) {
2078                 goto error;
2079         }
2080
2081         /* Get the image data associated with this tile-component. */
2082         cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx);
2083         cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy);
2084         if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly,
2085           brx - tlx, bry - tly, tcmpt->data)) {
2086                 goto error;
2087         }
2088
2089         tcmpt->synweight = 0;
2090         tcmpt->qmfbid = cp->tccp.qmfbid;
2091         tcmpt->numrlvls = cp->tccp.maxrlvls;
2092         tcmpt->numbands = 3 * tcmpt->numrlvls - 2;
2093         if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) {
2094                 goto error;
2095         }
2096
2097         for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) {
2098                 tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno];
2099                 tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno];
2100         }
2101         tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn;
2102         tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn;
2103         tcmpt->cblksty = cp->tccp.cblksty;
2104         tcmpt->csty = cp->tccp.csty;
2105
2106         tcmpt->numstepsizes = tcmpt->numbands;
2107         assert(tcmpt->numstepsizes <= JPC_MAXBANDS);
2108         memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes *
2109           sizeof(uint_fast16_t)));
2110
2111         /* Retrieve information about the various bands. */
2112         jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data),
2113           jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data),
2114           jas_seq2d_yend(tcmpt->data), bandinfos);
2115
2116         if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) {
2117                 goto error;
2118         }
2119         for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2120           ++rlvlno, ++rlvl) {
2121                 rlvl->bands = 0;
2122                 rlvl->tcmpt = tcmpt;
2123         }
2124         for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2125           ++rlvlno, ++rlvl) {
2126                 if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) {
2127                         goto error;
2128                 }
2129         }
2130
2131         return tcmpt;
2132
2133 error:
2134
2135         tcmpt_destroy(tcmpt);
2136         return 0;
2137
2138 }
2139
2140 static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt)
2141 {
2142         jpc_enc_rlvl_t *rlvl;
2143         uint_fast16_t rlvlno;
2144
2145         if (tcmpt->rlvls) {
2146                 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2147                   ++rlvlno, ++rlvl) {
2148                         rlvl_destroy(rlvl);
2149                 }
2150                 jas_free(tcmpt->rlvls);
2151         }
2152
2153         if (tcmpt->data) {
2154                 jas_seq2d_destroy(tcmpt->data);
2155         }
2156         if (tcmpt->tsfb) {
2157                 jpc_tsfb_destroy(tcmpt->tsfb);
2158         }
2159 }
2160
2161 static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp,
2162   jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos)
2163 {
2164         uint_fast16_t rlvlno;
2165         uint_fast32_t tlprctlx;
2166         uint_fast32_t tlprctly;
2167         uint_fast32_t brprcbrx;
2168         uint_fast32_t brprcbry;
2169         uint_fast16_t bandno;
2170         jpc_enc_band_t *band;
2171
2172         /* Deduce the resolution level. */
2173         rlvlno = rlvl - tcmpt->rlvls;
2174
2175         /* Initialize members required for error recovery. */
2176         rlvl->bands = 0;
2177         rlvl->tcmpt = tcmpt;
2178
2179         /* Compute the coordinates of the top-left and bottom-right
2180           corners of the tile-component at this resolution. */
2181         rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls -
2182           1 - rlvlno);
2183         rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls -
2184           1 - rlvlno);
2185         rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls -
2186           1 - rlvlno);
2187         rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls -
2188           1 - rlvlno);
2189
2190         if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) {
2191                 rlvl->numhprcs = 0;
2192                 rlvl->numvprcs = 0;
2193                 rlvl->numprcs = 0;
2194                 return rlvl;
2195         }
2196
2197         rlvl->numbands = (!rlvlno) ? 1 : 3;
2198         rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno];
2199         rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno];
2200         if (!rlvlno) {
2201                 rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
2202                 rlvl->cbgheightexpn = rlvl->prcheightexpn;
2203         } else {
2204                 rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
2205                 rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
2206         }
2207         rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn);
2208         rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn);
2209
2210         /* Compute the number of precincts. */
2211         tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
2212         tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
2213         brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn);
2214         brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn);
2215         rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn);
2216         rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn);
2217         rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
2218
2219         if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) {
2220                 goto error;
2221         }
2222         for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2223           ++bandno, ++band) {
2224                 band->prcs = 0;
2225                 band->data = 0;
2226                 band->rlvl = rlvl;
2227         }
2228         for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2229           ++bandno, ++band) {
2230                 if (!band_create(band, cp, rlvl, bandinfos)) {
2231                         goto error;
2232                 }
2233         }
2234
2235         return rlvl;
2236 error:
2237
2238         rlvl_destroy(rlvl);
2239         return 0;
2240 }
2241
2242 static void rlvl_destroy(jpc_enc_rlvl_t *rlvl)
2243 {
2244         jpc_enc_band_t *band;
2245         uint_fast16_t bandno;
2246
2247         if (rlvl->bands) {
2248                 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2249                   ++bandno, ++band) {
2250                         band_destroy(band);
2251                 }
2252                 jas_free(rlvl->bands);
2253         }
2254 }
2255
2256 static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp,
2257   jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos)
2258 {
2259         uint_fast16_t bandno;
2260         uint_fast16_t gblbandno;
2261         uint_fast16_t rlvlno;
2262         jpc_tsfb_band_t *bandinfo;
2263         jpc_enc_tcmpt_t *tcmpt;
2264         uint_fast32_t prcno;
2265         jpc_enc_prc_t *prc;
2266
2267         tcmpt = rlvl->tcmpt;
2268         band->data = 0;
2269         band->prcs = 0;
2270         band->rlvl = rlvl;
2271
2272         /* Deduce the resolution level and band number. */
2273         rlvlno = rlvl - rlvl->tcmpt->rlvls;
2274         bandno = band - rlvl->bands;
2275         gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1);
2276
2277         bandinfo = &bandinfos[gblbandno];
2278
2279 if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) {
2280         if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
2281                 goto error;
2282         }
2283         jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart,
2284           bandinfo->locystart, bandinfo->locxend, bandinfo->locyend);
2285         jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart);
2286 }
2287         band->orient = bandinfo->orient;
2288         band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno,
2289           band->orient);
2290         band->numbps = 0;
2291         band->absstepsize = 0;
2292         band->stepsize = 0;
2293         band->synweight = bandinfo->synenergywt;
2294
2295 if (band->data) {
2296         if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) {
2297                 goto error;
2298         }
2299         for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
2300           ++prc) {
2301                 prc->cblks = 0;
2302                 prc->incltree = 0;
2303                 prc->nlibtree = 0;
2304                 prc->savincltree = 0;
2305                 prc->savnlibtree = 0;
2306                 prc->band = band;
2307         }
2308         for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno,
2309           ++prc) {
2310                 if (!prc_create(prc, cp, band)) {
2311                         goto error;
2312                 }
2313         }
2314 }
2315
2316         return band;
2317
2318 error:
2319         band_destroy(band);
2320         return 0;
2321 }
2322
2323 static void band_destroy(jpc_enc_band_t *band)
2324 {
2325         jpc_enc_prc_t *prc;
2326         jpc_enc_rlvl_t *rlvl;
2327         uint_fast32_t prcno;
2328
2329         if (band->prcs) {
2330                 rlvl = band->rlvl;
2331                 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
2332                   ++prcno, ++prc) {
2333                         prc_destroy(prc);
2334                 }
2335                 jas_free(band->prcs);
2336         }
2337         if (band->data) {
2338                 jas_seq2d_destroy(band->data);
2339         }
2340 }
2341
2342 static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band)
2343 {
2344         uint_fast32_t prcno;
2345         uint_fast32_t prcxind;
2346         uint_fast32_t prcyind;
2347         uint_fast32_t cbgtlx;
2348         uint_fast32_t cbgtly;
2349         uint_fast32_t tlprctlx;
2350         uint_fast32_t tlprctly;
2351         uint_fast32_t tlcbgtlx;
2352         uint_fast32_t tlcbgtly;
2353         uint_fast16_t rlvlno;
2354         jpc_enc_rlvl_t *rlvl;
2355         uint_fast32_t tlcblktlx;
2356         uint_fast32_t tlcblktly;
2357         uint_fast32_t brcblkbrx;
2358         uint_fast32_t brcblkbry;
2359         uint_fast32_t cblkno;
2360         jpc_enc_cblk_t *cblk;
2361         jpc_enc_tcmpt_t *tcmpt;
2362
2363         prc->cblks = 0;
2364         prc->incltree = 0;
2365         prc->savincltree = 0;
2366         prc->nlibtree = 0;
2367         prc->savnlibtree = 0;
2368
2369         rlvl = band->rlvl;
2370         tcmpt = rlvl->tcmpt;
2371 rlvlno = rlvl - tcmpt->rlvls;
2372         prcno = prc - band->prcs;
2373         prcxind = prcno % rlvl->numhprcs;
2374         prcyind = prcno / rlvl->numhprcs;
2375         prc->band = band;
2376
2377 tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn);
2378 tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn);
2379 if (!rlvlno) {
2380         tlcbgtlx = tlprctlx;
2381         tlcbgtly = tlprctly;
2382 } else {
2383         tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1);
2384         tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1);
2385 }
2386
2387         /* Compute the coordinates of the top-left and bottom-right
2388           corners of the precinct. */
2389         cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn);
2390         cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn);
2391         prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx);
2392         prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly);
2393         prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx +
2394           (1 << rlvl->cbgwidthexpn));
2395         prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly +
2396           (1 << rlvl->cbgheightexpn));
2397
2398         if (prc->tlx < prc->brx && prc->tly < prc->bry) {
2399                 /* The precinct contains at least one code block. */
2400
2401                 tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
2402                 tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
2403                 brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn);
2404                 brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn);
2405                 prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx,
2406                   rlvl->cblkwidthexpn);
2407                 prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly,
2408                   rlvl->cblkheightexpn);
2409                 prc->numcblks = prc->numhcblks * prc->numvcblks;
2410
2411                 if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks,
2412                   prc->numvcblks))) {
2413                         goto error;
2414                 }
2415                 if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks,
2416                   prc->numvcblks))) {
2417                         goto error;
2418                 }
2419                 if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks,
2420                   prc->numvcblks))) {
2421                         goto error;
2422                 }
2423                 if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks,
2424                   prc->numvcblks))) {
2425                         goto error;
2426                 }
2427
2428                 if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) {
2429                         goto error;
2430                 }
2431                 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2432                   ++cblkno, ++cblk) {
2433                         cblk->passes = 0;
2434                         cblk->stream = 0;
2435                         cblk->mqenc = 0;
2436                         cblk->data = 0;
2437                         cblk->flags = 0;
2438                         cblk->prc = prc;
2439                 }
2440                 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2441                   ++cblkno, ++cblk) {
2442                         if (!cblk_create(cblk, cp, prc)) {
2443                                 goto error;
2444                         }
2445                 }
2446         } else {
2447                 /* The precinct does not contain any code blocks. */
2448                 prc->tlx = prc->brx;
2449                 prc->tly = prc->bry;
2450                 prc->numcblks = 0;
2451                 prc->numhcblks = 0;
2452                 prc->numvcblks = 0;
2453                 prc->cblks = 0;
2454                 prc->incltree = 0;
2455                 prc->nlibtree = 0;
2456                 prc->savincltree = 0;
2457                 prc->savnlibtree = 0;
2458         }
2459
2460         return prc;
2461
2462 error:
2463         prc_destroy(prc);
2464         return 0;
2465 }
2466
2467 static void prc_destroy(jpc_enc_prc_t *prc)
2468 {
2469         jpc_enc_cblk_t *cblk;
2470         uint_fast32_t cblkno;
2471
2472         if (prc->cblks) {
2473                 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2474                   ++cblkno, ++cblk) {
2475                         cblk_destroy(cblk);
2476                 }
2477                 jas_free(prc->cblks);
2478         }
2479         if (prc->incltree) {
2480                 jpc_tagtree_destroy(prc->incltree);
2481         }
2482         if (prc->nlibtree) {
2483                 jpc_tagtree_destroy(prc->nlibtree);
2484         }
2485         if (prc->savincltree) {
2486                 jpc_tagtree_destroy(prc->savincltree);
2487         }
2488         if (prc->savnlibtree) {
2489                 jpc_tagtree_destroy(prc->savnlibtree);
2490         }
2491 }
2492
2493 static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc)
2494 {
2495         jpc_enc_band_t *band;
2496         uint_fast32_t cblktlx;
2497         uint_fast32_t cblktly;
2498         uint_fast32_t cblkbrx;
2499         uint_fast32_t cblkbry;
2500         jpc_enc_rlvl_t *rlvl;
2501         uint_fast32_t cblkxind;
2502         uint_fast32_t cblkyind;
2503         uint_fast32_t cblkno;
2504         uint_fast32_t tlcblktlx;
2505         uint_fast32_t tlcblktly;
2506
2507         cblkno = cblk - prc->cblks;
2508         cblkxind = cblkno % prc->numhcblks;
2509         cblkyind = cblkno / prc->numhcblks;
2510         rlvl = prc->band->rlvl;
2511         cblk->prc = prc;
2512
2513         cblk->numpasses = 0;
2514         cblk->passes = 0;
2515         cblk->numencpasses = 0;
2516         cblk->numimsbs = 0;
2517         cblk->numlenbits = 0;
2518         cblk->stream = 0;
2519         cblk->mqenc = 0;
2520         cblk->flags = 0;
2521         cblk->numbps = 0;
2522         cblk->curpass = 0;
2523         cblk->data = 0;
2524         cblk->savedcurpass = 0;
2525         cblk->savednumlenbits = 0;
2526         cblk->savednumencpasses = 0;
2527
2528         band = prc->band;
2529         tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn);
2530         tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn);
2531         cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx);
2532         cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly);
2533         cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn),
2534           prc->brx);
2535         cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn),
2536           prc->bry);
2537
2538         assert(cblktlx < cblkbrx && cblktly < cblkbry);
2539         if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) {
2540                 goto error;
2541         }
2542         jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry);
2543
2544         return cblk;
2545
2546 error:
2547         cblk_destroy(cblk);
2548         return 0;
2549 }
2550
2551 static void cblk_destroy(jpc_enc_cblk_t *cblk)
2552 {
2553         uint_fast16_t passno;
2554         jpc_enc_pass_t *pass;
2555         if (cblk->passes) {
2556                 for (passno = 0, pass = cblk->passes; passno < cblk->numpasses;
2557                   ++passno, ++pass) {
2558                         pass_destroy(pass);
2559                 }
2560                 jas_free(cblk->passes);
2561         }
2562         if (cblk->stream) {
2563                 jas_stream_close(cblk->stream);
2564         }
2565         if (cblk->mqenc) {
2566                 jpc_mqenc_destroy(cblk->mqenc);
2567         }
2568         if (cblk->data) {
2569                 jas_seq2d_destroy(cblk->data);
2570         }
2571         if (cblk->flags) {
2572                 jas_seq2d_destroy(cblk->flags);
2573         }
2574 }
2575
2576 static void pass_destroy(jpc_enc_pass_t *pass)
2577 {
2578         /* XXX - need to free resources here */
2579 }
2580
2581 void jpc_enc_dump(jpc_enc_t *enc)
2582 {
2583         jpc_enc_tile_t *tile;
2584         jpc_enc_tcmpt_t *tcmpt;
2585         jpc_enc_rlvl_t *rlvl;
2586         jpc_enc_band_t *band;
2587         jpc_enc_prc_t *prc;
2588         jpc_enc_cblk_t *cblk;
2589         uint_fast16_t cmptno;
2590         uint_fast16_t rlvlno;
2591         uint_fast16_t bandno;
2592         uint_fast32_t prcno;
2593         uint_fast32_t cblkno;
2594
2595         tile = enc->curtile;
2596
2597         for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno,
2598           ++tcmpt) {
2599                 fprintf(stderr, "  tcmpt %5d %5d %5d %5d\n", jas_seq2d_xstart(tcmpt->data), jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), jas_seq2d_yend(tcmpt->data));
2600                 for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls;
2601                   ++rlvlno, ++rlvl) {
2602                         fprintf(stderr, "    rlvl %5d %5d %5d %5d\n", rlvl->tlx, rlvl->tly, rlvl->brx, rlvl->bry);
2603                         for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
2604                           ++bandno, ++band) {
2605                                 if (!band->data) {
2606                                         continue;
2607                                 }
2608                                 fprintf(stderr, "      band %5d %5d %5d %5d\n", jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), jas_seq2d_yend(band->data));
2609                                 for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs;
2610                                   ++prcno, ++prc) {
2611                                         fprintf(stderr, "        prc %5d %5d %5d %5d (%5d %5d)\n", prc->tlx, prc->tly, prc->brx, prc->bry, prc->brx - prc->tlx, prc->bry - prc->tly);
2612                                         if (!prc->cblks) {
2613                                                 continue;
2614                                         }
2615                                         for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks;
2616                                           ++cblkno, ++cblk) {
2617                                                 fprintf(stderr, "         cblk %5d %5d %5d %5d\n", jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), jas_seq2d_yend(cblk->data));
2618                                         }
2619                                 }
2620                         }
2621                 }
2622         }
2623 }