Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

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