Coverage Report

Created: 2026-04-01 07:49

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