Coverage Report

Created: 2023-06-07 08:11

/src/opencv/3rdparty/openjpeg/openjp2/tcd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The copyright in this software is being made available under the 2-clauses
3
 * BSD License, included below. This software may be subject to other third
4
 * party and contributor rights, including patent rights, and no such rights
5
 * are granted under this license.
6
 *
7
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8
 * Copyright (c) 2002-2014, Professor Benoit Macq
9
 * Copyright (c) 2001-2003, David Janssens
10
 * Copyright (c) 2002-2003, Yannick Verschueren
11
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12
 * Copyright (c) 2003-2014, Antonin Descampe
13
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14
 * Copyright (c) 2006-2007, Parvatha Elangovan
15
 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16
 * Copyright (c) 2012, CS Systemes d'Information, France
17
 * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
18
 * All rights reserved.
19
 *
20
 * Redistribution and use in source and binary forms, with or without
21
 * modification, are permitted provided that the following conditions
22
 * are met:
23
 * 1. Redistributions of source code must retain the above copyright
24
 *    notice, this list of conditions and the following disclaimer.
25
 * 2. Redistributions in binary form must reproduce the above copyright
26
 *    notice, this list of conditions and the following disclaimer in the
27
 *    documentation and/or other materials provided with the distribution.
28
 *
29
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
30
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39
 * POSSIBILITY OF SUCH DAMAGE.
40
 */
41
42
#include "opj_includes.h"
43
#include "opj_common.h"
44
45
/* ----------------------------------------------------------------------- */
46
47
/* TODO MSD: */
48
#ifdef TODO_MSD
49
void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img)
50
{
51
    int tileno, compno, resno, bandno, precno;/*, cblkno;*/
52
53
    fprintf(fd, "image {\n");
54
    fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
55
            img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0,
56
            tcd->image->y1);
57
58
    for (tileno = 0; tileno < img->th * img->tw; tileno++) {
59
        opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
60
        fprintf(fd, "  tile {\n");
61
        fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
62
                tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
63
        for (compno = 0; compno < tile->numcomps; compno++) {
64
            opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
65
            fprintf(fd, "    tilec {\n");
66
            fprintf(fd,
67
                    "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
68
                    tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
69
            for (resno = 0; resno < tilec->numresolutions; resno++) {
70
                opj_tcd_resolution_t *res = &tilec->resolutions[resno];
71
                fprintf(fd, "\n   res {\n");
72
                fprintf(fd,
73
                        "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
74
                        res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
75
                for (bandno = 0; bandno < res->numbands; bandno++) {
76
                    opj_tcd_band_t *band = &res->bands[bandno];
77
                    fprintf(fd, "        band {\n");
78
                    fprintf(fd,
79
                            "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
80
                            band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
81
                    for (precno = 0; precno < res->pw * res->ph; precno++) {
82
                        opj_tcd_precinct_t *prec = &band->precincts[precno];
83
                        fprintf(fd, "          prec {\n");
84
                        fprintf(fd,
85
                                "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
86
                                prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
87
                        /*
88
                        for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
89
                                opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
90
                                fprintf(fd, "            cblk {\n");
91
                                fprintf(fd,
92
                                        "              x0=%d, y0=%d, x1=%d, y1=%d\n",
93
                                        cblk->x0, cblk->y0, cblk->x1, cblk->y1);
94
                                fprintf(fd, "            }\n");
95
                        }
96
                        */
97
                        fprintf(fd, "          }\n");
98
                    }
99
                    fprintf(fd, "        }\n");
100
                }
101
                fprintf(fd, "      }\n");
102
            }
103
            fprintf(fd, "    }\n");
104
        }
105
        fprintf(fd, "  }\n");
106
    }
107
    fprintf(fd, "}\n");
108
}
109
#endif
110
111
/**
112
 * Initializes tile coding/decoding
113
 */
114
static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
115
        OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block,
116
        opj_event_mgr_t* manager);
117
118
/**
119
* Allocates memory for a decoding code block.
120
*/
121
static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
122
        p_code_block);
123
124
/**
125
 * Deallocates the decoding data of the given precinct.
126
 */
127
static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct);
128
129
/**
130
 * Allocates memory for an encoding code block (but not data).
131
 */
132
static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
133
        p_code_block);
134
135
/**
136
 * Allocates data for an encoding code block
137
 */
138
static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
139
        p_code_block);
140
141
/**
142
 * Deallocates the encoding data of the given precinct.
143
 */
144
static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct);
145
146
147
/**
148
Free the memory allocated for encoding
149
@param tcd TCD handle
150
*/
151
static void opj_tcd_free_tile(opj_tcd_t *tcd);
152
153
154
static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
155
                                  OPJ_BYTE * p_src_data,
156
                                  OPJ_UINT32 * p_data_read,
157
                                  OPJ_UINT32 p_max_src_size,
158
                                  opj_codestream_index_t *p_cstr_index,
159
                                  opj_event_mgr_t *p_manager);
160
161
static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd,
162
                                  opj_event_mgr_t *p_manager);
163
164
static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd);
165
166
static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd,
167
                                   opj_event_mgr_t *p_manager);
168
169
static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd);
170
171
172
static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd);
173
174
static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd);
175
176
static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd);
177
178
static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd);
179
180
static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
181
                                  OPJ_BYTE * p_dest_data,
182
                                  OPJ_UINT32 * p_data_written,
183
                                  OPJ_UINT32 p_max_dest_size,
184
                                  opj_codestream_info_t *p_cstr_info,
185
                                  opj_tcd_marker_info_t* p_marker_info,
186
                                  opj_event_mgr_t *p_manager);
187
188
static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
189
        OPJ_BYTE * p_dest_data,
190
        OPJ_UINT32 p_max_dest_size,
191
        opj_codestream_info_t *p_cstr_info,
192
        opj_event_mgr_t *p_manager);
193
194
195
static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *tcd,
196
        OPJ_UINT32 compno);
197
198
/* ----------------------------------------------------------------------- */
199
200
/**
201
Create a new TCD handle
202
*/
203
opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
204
1.23k
{
205
1.23k
    opj_tcd_t *l_tcd = 00;
206
207
    /* create the tcd structure */
208
1.23k
    l_tcd = (opj_tcd_t*) opj_calloc(1, sizeof(opj_tcd_t));
209
1.23k
    if (!l_tcd) {
210
0
        return 00;
211
0
    }
212
213
1.23k
    l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
214
215
1.23k
    l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1, sizeof(opj_tcd_image_t));
216
1.23k
    if (!l_tcd->tcd_image) {
217
0
        opj_free(l_tcd);
218
0
        return 00;
219
0
    }
220
221
1.23k
    return l_tcd;
222
1.23k
}
223
224
225
/* ----------------------------------------------------------------------- */
226
227
void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd)
228
0
{
229
0
    OPJ_UINT32 layno;
230
231
0
    for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
232
0
        opj_tcd_makelayer_fixed(tcd, layno, 1);
233
0
    }
234
0
}
235
236
237
void opj_tcd_makelayer(opj_tcd_t *tcd,
238
                       OPJ_UINT32 layno,
239
                       OPJ_FLOAT64 thresh,
240
                       OPJ_UINT32 final)
241
0
{
242
0
    OPJ_UINT32 compno, resno, bandno, precno, cblkno;
243
0
    OPJ_UINT32 passno;
244
245
0
    opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
246
247
0
    tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
248
249
0
    for (compno = 0; compno < tcd_tile->numcomps; compno++) {
250
0
        opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
251
252
0
        for (resno = 0; resno < tilec->numresolutions; resno++) {
253
0
            opj_tcd_resolution_t *res = &tilec->resolutions[resno];
254
255
0
            for (bandno = 0; bandno < res->numbands; bandno++) {
256
0
                opj_tcd_band_t *band = &res->bands[bandno];
257
258
                /* Skip empty bands */
259
0
                if (opj_tcd_is_band_empty(band)) {
260
0
                    continue;
261
0
                }
262
263
0
                for (precno = 0; precno < res->pw * res->ph; precno++) {
264
0
                    opj_tcd_precinct_t *prc = &band->precincts[precno];
265
266
0
                    for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
267
0
                        opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
268
0
                        opj_tcd_layer_t *layer = &cblk->layers[layno];
269
0
                        OPJ_UINT32 n;
270
271
0
                        if (layno == 0) {
272
0
                            cblk->numpassesinlayers = 0;
273
0
                        }
274
275
0
                        n = cblk->numpassesinlayers;
276
277
0
                        if (thresh < 0) {
278
                            /* Special value to indicate to use all passes */
279
0
                            n = cblk->totalpasses;
280
0
                        } else {
281
0
                            for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
282
0
                                OPJ_UINT32 dr;
283
0
                                OPJ_FLOAT64 dd;
284
0
                                opj_tcd_pass_t *pass = &cblk->passes[passno];
285
286
0
                                if (n == 0) {
287
0
                                    dr = pass->rate;
288
0
                                    dd = pass->distortiondec;
289
0
                                } else {
290
0
                                    dr = pass->rate - cblk->passes[n - 1].rate;
291
0
                                    dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
292
0
                                }
293
294
0
                                if (!dr) {
295
0
                                    if (dd != 0) {
296
0
                                        n = passno + 1;
297
0
                                    }
298
0
                                    continue;
299
0
                                }
300
0
                                if (thresh - (dd / dr) <
301
0
                                        DBL_EPSILON) { /* do not rely on float equality, check with DBL_EPSILON margin */
302
0
                                    n = passno + 1;
303
0
                                }
304
0
                            }
305
0
                        }
306
307
0
                        layer->numpasses = n - cblk->numpassesinlayers;
308
309
0
                        if (!layer->numpasses) {
310
0
                            layer->disto = 0;
311
0
                            continue;
312
0
                        }
313
314
0
                        if (cblk->numpassesinlayers == 0) {
315
0
                            layer->len = cblk->passes[n - 1].rate;
316
0
                            layer->data = cblk->data;
317
0
                            layer->disto = cblk->passes[n - 1].distortiondec;
318
0
                        } else {
319
0
                            layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
320
0
                                         1].rate;
321
0
                            layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
322
0
                            layer->disto = cblk->passes[n - 1].distortiondec -
323
0
                                           cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
324
0
                        }
325
326
0
                        tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
327
328
0
                        if (final) {
329
0
                            cblk->numpassesinlayers = n;
330
0
                        }
331
0
                    }
332
0
                }
333
0
            }
334
0
        }
335
0
    }
336
0
}
337
338
void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno,
339
                             OPJ_UINT32 final)
340
0
{
341
0
    OPJ_UINT32 compno, resno, bandno, precno, cblkno;
342
0
    OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
343
0
    OPJ_INT32 matrice[10][10][3];
344
0
    OPJ_UINT32 i, j, k;
345
346
0
    opj_cp_t *cp = tcd->cp;
347
0
    opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
348
0
    opj_tcp_t *tcd_tcp = tcd->tcp;
349
350
0
    for (compno = 0; compno < tcd_tile->numcomps; compno++) {
351
0
        opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
352
353
0
        for (i = 0; i < tcd_tcp->numlayers; i++) {
354
0
            for (j = 0; j < tilec->numresolutions; j++) {
355
0
                for (k = 0; k < 3; k++) {
356
0
                    matrice[i][j][k] =
357
0
                        (OPJ_INT32)((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i *
358
0
                                      tilec->numresolutions * 3 + j * 3 + k]
359
0
                                    * (OPJ_FLOAT32)(tcd->image->comps[compno].prec / 16.0));
360
0
                }
361
0
            }
362
0
        }
363
364
0
        for (resno = 0; resno < tilec->numresolutions; resno++) {
365
0
            opj_tcd_resolution_t *res = &tilec->resolutions[resno];
366
367
0
            for (bandno = 0; bandno < res->numbands; bandno++) {
368
0
                opj_tcd_band_t *band = &res->bands[bandno];
369
370
                /* Skip empty bands */
371
0
                if (opj_tcd_is_band_empty(band)) {
372
0
                    continue;
373
0
                }
374
375
0
                for (precno = 0; precno < res->pw * res->ph; precno++) {
376
0
                    opj_tcd_precinct_t *prc = &band->precincts[precno];
377
378
0
                    for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
379
0
                        opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
380
0
                        opj_tcd_layer_t *layer = &cblk->layers[layno];
381
0
                        OPJ_UINT32 n;
382
0
                        OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec -
383
0
                                                     cblk->numbps); /* number of bit-plan equal to zero */
384
385
                        /* Correction of the matrix of coefficient to include the IMSB information */
386
0
                        if (layno == 0) {
387
0
                            value = matrice[layno][resno][bandno];
388
0
                            if (imsb >= value) {
389
0
                                value = 0;
390
0
                            } else {
391
0
                                value -= imsb;
392
0
                            }
393
0
                        } else {
394
0
                            value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
395
0
                            if (imsb >= matrice[layno - 1][resno][bandno]) {
396
0
                                value -= (imsb - matrice[layno - 1][resno][bandno]);
397
0
                                if (value < 0) {
398
0
                                    value = 0;
399
0
                                }
400
0
                            }
401
0
                        }
402
403
0
                        if (layno == 0) {
404
0
                            cblk->numpassesinlayers = 0;
405
0
                        }
406
407
0
                        n = cblk->numpassesinlayers;
408
0
                        if (cblk->numpassesinlayers == 0) {
409
0
                            if (value != 0) {
410
0
                                n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
411
0
                            } else {
412
0
                                n = cblk->numpassesinlayers;
413
0
                            }
414
0
                        } else {
415
0
                            n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
416
0
                        }
417
418
0
                        layer->numpasses = n - cblk->numpassesinlayers;
419
420
0
                        if (!layer->numpasses) {
421
0
                            continue;
422
0
                        }
423
424
0
                        if (cblk->numpassesinlayers == 0) {
425
0
                            layer->len = cblk->passes[n - 1].rate;
426
0
                            layer->data = cblk->data;
427
0
                        } else {
428
0
                            layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers -
429
0
                                         1].rate;
430
0
                            layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
431
0
                        }
432
433
0
                        if (final) {
434
0
                            cblk->numpassesinlayers = n;
435
0
                        }
436
0
                    }
437
0
                }
438
0
            }
439
0
        }
440
0
    }
441
0
}
442
443
OPJ_BOOL opj_tcd_rateallocate(opj_tcd_t *tcd,
444
                              OPJ_BYTE *dest,
445
                              OPJ_UINT32 * p_data_written,
446
                              OPJ_UINT32 len,
447
                              opj_codestream_info_t *cstr_info,
448
                              opj_event_mgr_t *p_manager)
449
0
{
450
0
    OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
451
0
    OPJ_UINT32 passno;
452
0
    OPJ_FLOAT64 min, max;
453
0
    OPJ_FLOAT64 cumdisto[100];      /* fixed_quality */
454
0
    const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
455
0
    OPJ_FLOAT64 maxSE = 0;
456
457
0
    opj_cp_t *cp = tcd->cp;
458
0
    opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
459
0
    opj_tcp_t *tcd_tcp = tcd->tcp;
460
461
0
    min = DBL_MAX;
462
0
    max = 0;
463
464
0
    tcd_tile->numpix = 0;           /* fixed_quality */
465
466
0
    for (compno = 0; compno < tcd_tile->numcomps; compno++) {
467
0
        opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
468
0
        tilec->numpix = 0;
469
470
0
        for (resno = 0; resno < tilec->numresolutions; resno++) {
471
0
            opj_tcd_resolution_t *res = &tilec->resolutions[resno];
472
473
0
            for (bandno = 0; bandno < res->numbands; bandno++) {
474
0
                opj_tcd_band_t *band = &res->bands[bandno];
475
476
                /* Skip empty bands */
477
0
                if (opj_tcd_is_band_empty(band)) {
478
0
                    continue;
479
0
                }
480
481
0
                for (precno = 0; precno < res->pw * res->ph; precno++) {
482
0
                    opj_tcd_precinct_t *prc = &band->precincts[precno];
483
484
0
                    for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
485
0
                        opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
486
487
0
                        for (passno = 0; passno < cblk->totalpasses; passno++) {
488
0
                            opj_tcd_pass_t *pass = &cblk->passes[passno];
489
0
                            OPJ_INT32 dr;
490
0
                            OPJ_FLOAT64 dd, rdslope;
491
492
0
                            if (passno == 0) {
493
0
                                dr = (OPJ_INT32)pass->rate;
494
0
                                dd = pass->distortiondec;
495
0
                            } else {
496
0
                                dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
497
0
                                dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
498
0
                            }
499
500
0
                            if (dr == 0) {
501
0
                                continue;
502
0
                            }
503
504
0
                            rdslope = dd / dr;
505
0
                            if (rdslope < min) {
506
0
                                min = rdslope;
507
0
                            }
508
509
0
                            if (rdslope > max) {
510
0
                                max = rdslope;
511
0
                            }
512
0
                        } /* passno */
513
514
                        /* fixed_quality */
515
0
                        tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
516
0
                        tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
517
0
                    } /* cbklno */
518
0
                } /* precno */
519
0
            } /* bandno */
520
0
        } /* resno */
521
522
0
        maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
523
0
                  * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0))
524
0
                 * ((OPJ_FLOAT64)(tilec->numpix));
525
0
    } /* compno */
526
527
    /* index file */
528
0
    if (cstr_info) {
529
0
        opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
530
0
        tile_info->numpix = tcd_tile->numpix;
531
0
        tile_info->distotile = tcd_tile->distotile;
532
0
        tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(
533
0
                                OPJ_FLOAT64));
534
0
        if (!tile_info->thresh) {
535
            /* FIXME event manager error callback */
536
0
            return OPJ_FALSE;
537
0
        }
538
0
    }
539
540
0
    for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
541
0
        OPJ_FLOAT64 lo = min;
542
0
        OPJ_FLOAT64 hi = max;
543
0
        OPJ_UINT32 maxlen = tcd_tcp->rates[layno] > 0.0f ? opj_uint_min(((
544
0
                                OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
545
0
        OPJ_FLOAT64 goodthresh = 0;
546
0
        OPJ_FLOAT64 stable_thresh = 0;
547
0
        OPJ_UINT32 i;
548
0
        OPJ_FLOAT64 distotarget;                /* fixed_quality */
549
550
        /* fixed_quality */
551
0
        distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10,
552
0
                                             tcd_tcp->distoratio[layno] / 10));
553
554
        /* Don't try to find an optimal threshold but rather take everything not included yet, if
555
          -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
556
          -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
557
          ==> possible to have some lossy layers and the last layer for sure lossless */
558
0
        if (((cp->m_specific_param.m_enc.m_disto_alloc == 1) &&
559
0
                (tcd_tcp->rates[layno] > 0.0f)) ||
560
0
                ((cp->m_specific_param.m_enc.m_fixed_quality == 1) &&
561
0
                 (tcd_tcp->distoratio[layno] > 0.0))) {
562
0
            opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
563
0
            OPJ_FLOAT64 thresh = 0;
564
565
0
            if (t2 == 00) {
566
0
                return OPJ_FALSE;
567
0
            }
568
569
0
            for (i = 0; i < 128; ++i) {
570
0
                OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
571
572
0
                thresh = (lo + hi) / 2;
573
574
0
                opj_tcd_makelayer(tcd, layno, thresh, 0);
575
576
0
                if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* fixed_quality */
577
0
                    if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
578
0
                        if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
579
0
                                                    p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
580
0
                                                    tcd->cur_pino,
581
0
                                                    THRESH_CALC, p_manager)) {
582
583
0
                            lo = thresh;
584
0
                            continue;
585
0
                        } else {
586
0
                            distoachieved = layno == 0 ?
587
0
                                            tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
588
589
0
                            if (distoachieved < distotarget) {
590
0
                                hi = thresh;
591
0
                                stable_thresh = thresh;
592
0
                                continue;
593
0
                            } else {
594
0
                                lo = thresh;
595
0
                            }
596
0
                        }
597
0
                    } else {
598
0
                        distoachieved = (layno == 0) ?
599
0
                                        tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
600
601
0
                        if (distoachieved < distotarget) {
602
0
                            hi = thresh;
603
0
                            stable_thresh = thresh;
604
0
                            continue;
605
0
                        }
606
0
                        lo = thresh;
607
0
                    }
608
0
                } else {
609
0
                    if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,
610
0
                                                p_data_written, maxlen, cstr_info, NULL, tcd->cur_tp_num, tcd->tp_pos,
611
0
                                                tcd->cur_pino,
612
0
                                                THRESH_CALC, p_manager)) {
613
                        /* TODO: what to do with l ??? seek / tell ??? */
614
                        /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
615
0
                        lo = thresh;
616
0
                        continue;
617
0
                    }
618
619
0
                    hi = thresh;
620
0
                    stable_thresh = thresh;
621
0
                }
622
0
            }
623
624
0
            goodthresh = stable_thresh == 0 ? thresh : stable_thresh;
625
626
0
            opj_t2_destroy(t2);
627
0
        } else {
628
            /* Special value to indicate to use all passes */
629
0
            goodthresh = -1;
630
0
        }
631
632
0
        if (cstr_info) { /* Threshold for Marcela Index */
633
0
            cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
634
0
        }
635
636
0
        opj_tcd_makelayer(tcd, layno, goodthresh, 1);
637
638
        /* fixed_quality */
639
0
        cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] :
640
0
                          (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
641
0
    }
642
643
0
    return OPJ_TRUE;
644
0
}
645
646
OPJ_BOOL opj_tcd_init(opj_tcd_t *p_tcd,
647
                      opj_image_t * p_image,
648
                      opj_cp_t * p_cp,
649
                      opj_thread_pool_t* p_tp)
650
1.23k
{
651
1.23k
    p_tcd->image = p_image;
652
1.23k
    p_tcd->cp = p_cp;
653
654
1.23k
    p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,
655
1.23k
                              sizeof(opj_tcd_tile_t));
656
1.23k
    if (! p_tcd->tcd_image->tiles) {
657
0
        return OPJ_FALSE;
658
0
    }
659
660
1.23k
    p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(
661
1.23k
                                         p_image->numcomps, sizeof(opj_tcd_tilecomp_t));
662
1.23k
    if (! p_tcd->tcd_image->tiles->comps) {
663
0
        return OPJ_FALSE;
664
0
    }
665
666
1.23k
    p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
667
1.23k
    p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
668
1.23k
    p_tcd->thread_pool = p_tp;
669
670
1.23k
    return OPJ_TRUE;
671
1.23k
}
672
673
/**
674
Destroy a previously created TCD handle
675
*/
676
void opj_tcd_destroy(opj_tcd_t *tcd)
677
1.46k
{
678
1.46k
    if (tcd) {
679
1.23k
        opj_tcd_free_tile(tcd);
680
681
1.23k
        if (tcd->tcd_image) {
682
1.23k
            opj_free(tcd->tcd_image);
683
1.23k
            tcd->tcd_image = 00;
684
1.23k
        }
685
686
1.23k
        opj_free(tcd->used_component);
687
688
1.23k
        opj_free(tcd);
689
1.23k
    }
690
1.46k
}
691
692
OPJ_BOOL opj_alloc_tile_component_data(opj_tcd_tilecomp_t *l_tilec)
693
1.51k
{
694
1.51k
    if ((l_tilec->data == 00) ||
695
1.51k
            ((l_tilec->data_size_needed > l_tilec->data_size) &&
696
1.41k
             (l_tilec->ownsData == OPJ_FALSE))) {
697
1.41k
        l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
698
1.41k
        if (!l_tilec->data && l_tilec->data_size_needed != 0) {
699
0
            return OPJ_FALSE;
700
0
        }
701
        /*fprintf(stderr, "tAllocate data of tilec (int): %d x OPJ_UINT32n",l_data_size);*/
702
1.41k
        l_tilec->data_size = l_tilec->data_size_needed;
703
1.41k
        l_tilec->ownsData = OPJ_TRUE;
704
1.41k
    } else if (l_tilec->data_size_needed > l_tilec->data_size) {
705
        /* We don't need to keep old data */
706
3
        opj_image_data_free(l_tilec->data);
707
3
        l_tilec->data = (OPJ_INT32 *) opj_image_data_alloc(l_tilec->data_size_needed);
708
3
        if (! l_tilec->data) {
709
0
            l_tilec->data_size = 0;
710
0
            l_tilec->data_size_needed = 0;
711
0
            l_tilec->ownsData = OPJ_FALSE;
712
0
            return OPJ_FALSE;
713
0
        }
714
        /*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
715
3
        l_tilec->data_size = l_tilec->data_size_needed;
716
3
        l_tilec->ownsData = OPJ_TRUE;
717
3
    }
718
1.51k
    return OPJ_TRUE;
719
1.51k
}
720
721
/* ----------------------------------------------------------------------- */
722
723
static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
724
        OPJ_BOOL isEncoder, OPJ_SIZE_T sizeof_block,
725
        opj_event_mgr_t* manager)
726
1.22k
{
727
1.22k
    OPJ_UINT32 compno, resno, bandno, precno, cblkno;
728
1.22k
    opj_tcp_t * l_tcp = 00;
729
1.22k
    opj_cp_t * l_cp = 00;
730
1.22k
    opj_tcd_tile_t * l_tile = 00;
731
1.22k
    opj_tccp_t *l_tccp = 00;
732
1.22k
    opj_tcd_tilecomp_t *l_tilec = 00;
733
1.22k
    opj_image_comp_t * l_image_comp = 00;
734
1.22k
    opj_tcd_resolution_t *l_res = 00;
735
1.22k
    opj_tcd_band_t *l_band = 00;
736
1.22k
    opj_stepsize_t * l_step_size = 00;
737
1.22k
    opj_tcd_precinct_t *l_current_precinct = 00;
738
1.22k
    opj_image_t *l_image = 00;
739
1.22k
    OPJ_UINT32 p, q;
740
1.22k
    OPJ_UINT32 l_level_no;
741
1.22k
    OPJ_UINT32 l_pdx, l_pdy;
742
1.22k
    OPJ_INT32 l_x0b, l_y0b;
743
1.22k
    OPJ_UINT32 l_tx0, l_ty0;
744
    /* extent of precincts , top left, bottom right**/
745
1.22k
    OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;
746
    /* number of precinct for a resolution */
747
1.22k
    OPJ_UINT32 l_nb_precincts;
748
    /* room needed to store l_nb_precinct precinct for a resolution */
749
1.22k
    OPJ_UINT32 l_nb_precinct_size;
750
    /* number of code blocks for a precinct*/
751
1.22k
    OPJ_UINT32 l_nb_code_blocks;
752
    /* room needed to store l_nb_code_blocks code blocks for a precinct*/
753
1.22k
    OPJ_UINT32 l_nb_code_blocks_size;
754
    /* size of data for a tile */
755
1.22k
    OPJ_UINT32 l_data_size;
756
757
1.22k
    l_cp = p_tcd->cp;
758
1.22k
    l_tcp = &(l_cp->tcps[p_tile_no]);
759
1.22k
    l_tile = p_tcd->tcd_image->tiles;
760
1.22k
    l_tccp = l_tcp->tccps;
761
1.22k
    l_tilec = l_tile->comps;
762
1.22k
    l_image = p_tcd->image;
763
1.22k
    l_image_comp = p_tcd->image->comps;
764
765
1.22k
    p = p_tile_no % l_cp->tw;       /* tile coordinates */
766
1.22k
    q = p_tile_no / l_cp->tw;
767
    /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/
768
769
    /* 4 borders of the tile rescale on the image if necessary */
770
1.22k
    l_tx0 = l_cp->tx0 + p *
771
1.22k
            l_cp->tdx; /* can't be greater than l_image->x1 so won't overflow */
772
1.22k
    l_tile->x0 = (OPJ_INT32)opj_uint_max(l_tx0, l_image->x0);
773
1.22k
    l_tile->x1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_tx0, l_cp->tdx),
774
1.22k
                                         l_image->x1);
775
    /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
776
1.22k
    if ((l_tile->x0 < 0) || (l_tile->x1 <= l_tile->x0)) {
777
0
        opj_event_msg(manager, EVT_ERROR, "Tile X coordinates are not supported\n");
778
0
        return OPJ_FALSE;
779
0
    }
780
1.22k
    l_ty0 = l_cp->ty0 + q *
781
1.22k
            l_cp->tdy; /* can't be greater than l_image->y1 so won't overflow */
782
1.22k
    l_tile->y0 = (OPJ_INT32)opj_uint_max(l_ty0, l_image->y0);
783
1.22k
    l_tile->y1 = (OPJ_INT32)opj_uint_min(opj_uint_adds(l_ty0, l_cp->tdy),
784
1.22k
                                         l_image->y1);
785
    /* all those OPJ_UINT32 are casted to OPJ_INT32, let's do some sanity check */
786
1.22k
    if ((l_tile->y0 < 0) || (l_tile->y1 <= l_tile->y0)) {
787
0
        opj_event_msg(manager, EVT_ERROR, "Tile Y coordinates are not supported\n");
788
0
        return OPJ_FALSE;
789
0
    }
790
791
792
    /* testcase 1888.pdf.asan.35.988 */
793
1.22k
    if (l_tccp->numresolutions == 0) {
794
0
        opj_event_msg(manager, EVT_ERROR, "tiles require at least one resolution\n");
795
0
        return OPJ_FALSE;
796
0
    }
797
    /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/
798
799
    /*tile->numcomps = image->numcomps; */
800
2.74k
    for (compno = 0; compno < l_tile->numcomps; ++compno) {
801
        /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/
802
1.52k
        l_image_comp->resno_decoded = 0;
803
        /* border of each l_tile component (global) */
804
1.52k
        l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);
805
1.52k
        l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);
806
1.52k
        l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);
807
1.52k
        l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);
808
1.52k
        l_tilec->compno = compno;
809
        /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/
810
811
1.52k
        l_tilec->numresolutions = l_tccp->numresolutions;
812
1.52k
        if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {
813
0
            l_tilec->minimum_num_resolutions = 1;
814
1.52k
        } else {
815
1.52k
            l_tilec->minimum_num_resolutions = l_tccp->numresolutions -
816
1.52k
                                               l_cp->m_specific_param.m_dec.m_reduce;
817
1.52k
        }
818
819
1.52k
        if (isEncoder) {
820
0
            OPJ_SIZE_T l_tile_data_size;
821
822
            /* compute l_data_size with overflow check */
823
0
            OPJ_SIZE_T w = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0);
824
0
            OPJ_SIZE_T h = (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
825
826
            /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
827
0
            if (h > 0 && w > SIZE_MAX / h) {
828
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
829
0
                return OPJ_FALSE;
830
0
            }
831
0
            l_tile_data_size = w * h;
832
833
0
            if (SIZE_MAX / sizeof(OPJ_UINT32) < l_tile_data_size) {
834
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
835
0
                return OPJ_FALSE;
836
0
            }
837
0
            l_tile_data_size = l_tile_data_size * sizeof(OPJ_UINT32);
838
839
0
            l_tilec->data_size_needed = l_tile_data_size;
840
0
        }
841
842
1.52k
        l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(
843
1.52k
                          opj_tcd_resolution_t);
844
845
1.52k
        opj_image_data_free(l_tilec->data_win);
846
1.52k
        l_tilec->data_win = NULL;
847
1.52k
        l_tilec->win_x0 = 0;
848
1.52k
        l_tilec->win_y0 = 0;
849
1.52k
        l_tilec->win_x1 = 0;
850
1.52k
        l_tilec->win_y1 = 0;
851
852
1.52k
        if (l_tilec->resolutions == 00) {
853
1.41k
            l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);
854
1.41k
            if (! l_tilec->resolutions) {
855
0
                return OPJ_FALSE;
856
0
            }
857
            /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/
858
1.41k
            l_tilec->resolutions_size = l_data_size;
859
1.41k
            memset(l_tilec->resolutions, 0, l_data_size);
860
1.41k
        } else if (l_data_size > l_tilec->resolutions_size) {
861
0
            opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(
862
0
                    l_tilec->resolutions, l_data_size);
863
0
            if (! new_resolutions) {
864
0
                opj_event_msg(manager, EVT_ERROR, "Not enough memory for tile resolutions\n");
865
0
                opj_free(l_tilec->resolutions);
866
0
                l_tilec->resolutions = NULL;
867
0
                l_tilec->resolutions_size = 0;
868
0
                return OPJ_FALSE;
869
0
            }
870
0
            l_tilec->resolutions = new_resolutions;
871
            /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/
872
0
            memset(((OPJ_BYTE*) l_tilec->resolutions) + l_tilec->resolutions_size, 0,
873
0
                   l_data_size - l_tilec->resolutions_size);
874
0
            l_tilec->resolutions_size = l_data_size;
875
0
        }
876
877
1.52k
        l_level_no = l_tilec->numresolutions;
878
1.52k
        l_res = l_tilec->resolutions;
879
1.52k
        l_step_size = l_tccp->stepsizes;
880
        /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/
881
882
10.0k
        for (resno = 0; resno < l_tilec->numresolutions; ++resno) {
883
            /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/
884
8.55k
            OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;
885
8.55k
            OPJ_UINT32 cbgwidthexpn, cbgheightexpn;
886
8.55k
            OPJ_UINT32 cblkwidthexpn, cblkheightexpn;
887
888
8.55k
            --l_level_no;
889
890
            /* border for each resolution level (global) */
891
8.55k
            l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
892
8.55k
            l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
893
8.55k
            l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
894
8.55k
            l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
895
896
            /*fprintf(stderr, "\t\t\tres_x0= %d, res_y0 =%d, res_x1=%d, res_y1=%d\n", l_res->x0, l_res->y0, l_res->x1, l_res->y1);*/
897
            /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
898
8.55k
            l_pdx = l_tccp->prcw[resno];
899
8.55k
            l_pdy = l_tccp->prch[resno];
900
            /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/
901
            /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
902
8.55k
            l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;
903
8.55k
            l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;
904
8.55k
            {
905
8.55k
                OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->x1,
906
8.55k
                                  (OPJ_INT32)l_pdx)) << l_pdx;
907
8.55k
                if (tmp > (OPJ_UINT32)INT_MAX) {
908
0
                    opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
909
0
                    return OPJ_FALSE;
910
0
                }
911
8.55k
                l_br_prc_x_end = (OPJ_INT32)tmp;
912
8.55k
            }
913
0
            {
914
8.55k
                OPJ_UINT32 tmp = ((OPJ_UINT32)opj_int_ceildivpow2(l_res->y1,
915
8.55k
                                  (OPJ_INT32)l_pdy)) << l_pdy;
916
8.55k
                if (tmp > (OPJ_UINT32)INT_MAX) {
917
0
                    opj_event_msg(manager, EVT_ERROR, "Integer overflow\n");
918
0
                    return OPJ_FALSE;
919
0
                }
920
8.55k
                l_br_prc_y_end = (OPJ_INT32)tmp;
921
8.55k
            }
922
            /*fprintf(stderr, "\t\t\tprc_x_start=%d, prc_y_start=%d, br_prc_x_end=%d, br_prc_y_end=%d \n", l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end ,l_br_prc_y_end );*/
923
924
8.55k
            l_res->pw = (l_res->x0 == l_res->x1) ? 0U : (OPJ_UINT32)((
925
5.75k
                            l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);
926
8.55k
            l_res->ph = (l_res->y0 == l_res->y1) ? 0U : (OPJ_UINT32)((
927
3.36k
                            l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);
928
            /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/
929
930
8.55k
            if ((l_res->pw != 0U) && ((((OPJ_UINT32) - 1) / l_res->pw) < l_res->ph)) {
931
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
932
0
                return OPJ_FALSE;
933
0
            }
934
8.55k
            l_nb_precincts = l_res->pw * l_res->ph;
935
936
8.55k
            if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof(opj_tcd_precinct_t)) <
937
8.55k
                    l_nb_precincts) {
938
0
                opj_event_msg(manager, EVT_ERROR, "Size of tile data exceeds system limits\n");
939
0
                return OPJ_FALSE;
940
0
            }
941
8.55k
            l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);
942
943
8.55k
            if (resno == 0) {
944
1.52k
                tlcbgxstart = l_tl_prc_x_start;
945
1.52k
                tlcbgystart = l_tl_prc_y_start;
946
                /*brcbgxend = l_br_prc_x_end;*/
947
                /* brcbgyend = l_br_prc_y_end;*/
948
1.52k
                cbgwidthexpn = l_pdx;
949
1.52k
                cbgheightexpn = l_pdy;
950
1.52k
                l_res->numbands = 1;
951
7.03k
            } else {
952
7.03k
                tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);
953
7.03k
                tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);
954
                /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/
955
                /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/
956
7.03k
                cbgwidthexpn = l_pdx - 1;
957
7.03k
                cbgheightexpn = l_pdy - 1;
958
7.03k
                l_res->numbands = 3;
959
7.03k
            }
960
961
8.55k
            cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);
962
8.55k
            cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);
963
8.55k
            l_band = l_res->bands;
964
965
31.1k
            for (bandno = 0; bandno < l_res->numbands; ++bandno, ++l_band, ++l_step_size) {
966
                /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/
967
968
22.6k
                if (resno == 0) {
969
1.52k
                    l_band->bandno = 0 ;
970
1.52k
                    l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);
971
1.52k
                    l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);
972
1.52k
                    l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);
973
1.52k
                    l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);
974
21.0k
                } else {
975
21.0k
                    l_band->bandno = bandno + 1;
976
                    /* x0b = 1 if bandno = 1 or 3 */
977
21.0k
                    l_x0b = l_band->bandno & 1;
978
                    /* y0b = 1 if bandno = 2 or 3 */
979
21.0k
                    l_y0b = (OPJ_INT32)((l_band->bandno) >> 1);
980
                    /* l_band border (global) */
981
21.0k
                    l_band->x0 = opj_int64_ceildivpow2(l_tilec->x0 - ((OPJ_INT64)l_x0b <<
982
21.0k
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
983
21.0k
                    l_band->y0 = opj_int64_ceildivpow2(l_tilec->y0 - ((OPJ_INT64)l_y0b <<
984
21.0k
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
985
21.0k
                    l_band->x1 = opj_int64_ceildivpow2(l_tilec->x1 - ((OPJ_INT64)l_x0b <<
986
21.0k
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
987
21.0k
                    l_band->y1 = opj_int64_ceildivpow2(l_tilec->y1 - ((OPJ_INT64)l_y0b <<
988
21.0k
                                                       l_level_no), (OPJ_INT32)(l_level_no + 1));
989
21.0k
                }
990
991
22.6k
                if (isEncoder) {
992
                    /* Skip empty bands */
993
0
                    if (opj_tcd_is_band_empty(l_band)) {
994
                        /* Do not zero l_band->precints to avoid leaks */
995
                        /* but make sure we don't use it later, since */
996
                        /* it will point to precincts of previous bands... */
997
0
                        continue;
998
0
                    }
999
0
                }
1000
1001
22.6k
                {
1002
                    /* Table E-1 - Sub-band gains */
1003
                    /* BUG_WEIRD_TWO_INVK (look for this identifier in dwt.c): */
1004
                    /* the test (!isEncoder && l_tccp->qmfbid == 0) is strongly */
1005
                    /* linked to the use of two_invK instead of invK */
1006
22.6k
                    const OPJ_INT32 log2_gain = (!isEncoder &&
1007
22.6k
                                                 l_tccp->qmfbid == 0) ? 0 : (l_band->bandno == 0) ? 0 :
1008
18.5k
                                                (l_band->bandno == 3) ? 2 : 1;
1009
1010
                    /* Nominal dynamic range. Equation E-4 */
1011
22.6k
                    const OPJ_INT32 Rb = (OPJ_INT32)l_image_comp->prec + log2_gain;
1012
1013
                    /* Delta_b value of Equation E-3 in "E.1 Inverse quantization
1014
                    * procedure" of the standard */
1015
22.6k
                    l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0,
1016
22.6k
                                                      (OPJ_INT32)(Rb - l_step_size->expn))));
1017
22.6k
                }
1018
1019
                /* Mb value of Equation E-2 in "E.1 Inverse quantization
1020
                 * procedure" of the standard */
1021
22.6k
                l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits -
1022
22.6k
                                 1;
1023
1024
22.6k
                if (!l_band->precincts && (l_nb_precincts > 0U)) {
1025
4.90k
                    l_band->precincts = (opj_tcd_precinct_t *) opj_malloc(/*3 * */
1026
4.90k
                                            l_nb_precinct_size);
1027
4.90k
                    if (! l_band->precincts) {
1028
0
                        opj_event_msg(manager, EVT_ERROR,
1029
0
                                      "Not enough memory to handle band precints\n");
1030
0
                        return OPJ_FALSE;
1031
0
                    }
1032
                    /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */
1033
4.90k
                    memset(l_band->precincts, 0, l_nb_precinct_size);
1034
4.90k
                    l_band->precincts_data_size = l_nb_precinct_size;
1035
17.7k
                } else if (l_band->precincts_data_size < l_nb_precinct_size) {
1036
1037
1
                    opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(
1038
1
                            l_band->precincts,/*3 * */ l_nb_precinct_size);
1039
1
                    if (! new_precincts) {
1040
0
                        opj_event_msg(manager, EVT_ERROR,
1041
0
                                      "Not enough memory to handle band precints\n");
1042
0
                        opj_free(l_band->precincts);
1043
0
                        l_band->precincts = NULL;
1044
0
                        l_band->precincts_data_size = 0;
1045
0
                        return OPJ_FALSE;
1046
0
                    }
1047
1
                    l_band->precincts = new_precincts;
1048
                    /*fprintf(stderr, "\t\t\t\tReallocate precincts of a band (opj_tcd_precinct_t): from %d to %d\n",l_band->precincts_data_size, l_nb_precinct_size);*/
1049
1
                    memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size, 0,
1050
1
                           l_nb_precinct_size - l_band->precincts_data_size);
1051
1
                    l_band->precincts_data_size = l_nb_precinct_size;
1052
1
                }
1053
1054
22.6k
                l_current_precinct = l_band->precincts;
1055
1.79M
                for (precno = 0; precno < l_nb_precincts; ++precno) {
1056
1.76M
                    OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
1057
1.76M
                    OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) *
1058
1.76M
                                          (1 << cbgwidthexpn);
1059
1.76M
                    OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) *
1060
1.76M
                                          (1 << cbgheightexpn);
1061
1.76M
                    OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);
1062
1.76M
                    OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);
1063
                    /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/
1064
                    /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/
1065
1066
                    /* precinct size (global) */
1067
                    /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/
1068
1069
1.76M
                    l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);
1070
1.76M
                    l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);
1071
1.76M
                    l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);
1072
1.76M
                    l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);
1073
                    /*fprintf(stderr, "\t prc_x0=%d; prc_y0=%d, prc_x1=%d; prc_y1=%d\n",l_current_precinct->x0, l_current_precinct->y0 ,l_current_precinct->x1, l_current_precinct->y1);*/
1074
1075
1.76M
                    tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0,
1076
1.76M
                                                        (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1077
                    /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/
1078
1.76M
                    tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0,
1079
1.76M
                                                        (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1080
                    /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/
1081
1.76M
                    brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1,
1082
1.76M
                                                     (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;
1083
                    /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/
1084
1.76M
                    brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1,
1085
1.76M
                                                     (OPJ_INT32)cblkheightexpn) << cblkheightexpn;
1086
                    /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/
1087
1.76M
                    l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >>
1088
1.76M
                                                          cblkwidthexpn);
1089
1.76M
                    l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >>
1090
1.76M
                                                          cblkheightexpn);
1091
1092
1.76M
                    l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;
1093
                    /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */
1094
1.76M
                    if ((((OPJ_UINT32) - 1) / (OPJ_UINT32)sizeof_block) <
1095
1.76M
                            l_nb_code_blocks) {
1096
0
                        opj_event_msg(manager, EVT_ERROR,
1097
0
                                      "Size of code block data exceeds system limits\n");
1098
0
                        return OPJ_FALSE;
1099
0
                    }
1100
1.76M
                    l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof_block;
1101
1102
1.76M
                    if (!l_current_precinct->cblks.blocks && (l_nb_code_blocks > 0U)) {
1103
1.76M
                        l_current_precinct->cblks.blocks = opj_malloc(l_nb_code_blocks_size);
1104
1.76M
                        if (! l_current_precinct->cblks.blocks) {
1105
0
                            return OPJ_FALSE;
1106
0
                        }
1107
                        /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/
1108
1109
1.76M
                        memset(l_current_precinct->cblks.blocks, 0, l_nb_code_blocks_size);
1110
1111
1.76M
                        l_current_precinct->block_size = l_nb_code_blocks_size;
1112
1.76M
                    } else if (l_nb_code_blocks_size > l_current_precinct->block_size) {
1113
30
                        void *new_blocks = opj_realloc(l_current_precinct->cblks.blocks,
1114
30
                                                       l_nb_code_blocks_size);
1115
30
                        if (! new_blocks) {
1116
0
                            opj_free(l_current_precinct->cblks.blocks);
1117
0
                            l_current_precinct->cblks.blocks = NULL;
1118
0
                            l_current_precinct->block_size = 0;
1119
0
                            opj_event_msg(manager, EVT_ERROR,
1120
0
                                          "Not enough memory for current precinct codeblock element\n");
1121
0
                            return OPJ_FALSE;
1122
0
                        }
1123
30
                        l_current_precinct->cblks.blocks = new_blocks;
1124
                        /*fprintf(stderr, "\t\t\t\tReallocate cblks of a precinct (opj_tcd_cblk_dec_t): from %d to %d\n",l_current_precinct->block_size, l_nb_code_blocks_size);     */
1125
1126
30
                        memset(((OPJ_BYTE *) l_current_precinct->cblks.blocks) +
1127
30
                               l_current_precinct->block_size
1128
30
                               , 0
1129
30
                               , l_nb_code_blocks_size - l_current_precinct->block_size);
1130
1131
30
                        l_current_precinct->block_size = l_nb_code_blocks_size;
1132
30
                    }
1133
1134
1.76M
                    if (! l_current_precinct->incltree) {
1135
1.76M
                        l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,
1136
1.76M
                                                       l_current_precinct->ch, manager);
1137
1.76M
                    } else {
1138
1.09k
                        l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,
1139
1.09k
                                                       l_current_precinct->cw, l_current_precinct->ch, manager);
1140
1.09k
                    }
1141
1142
1.76M
                    if (! l_current_precinct->imsbtree) {
1143
1.76M
                        l_current_precinct->imsbtree = opj_tgt_create(l_current_precinct->cw,
1144
1.76M
                                                       l_current_precinct->ch, manager);
1145
1.76M
                    } else {
1146
1.09k
                        l_current_precinct->imsbtree = opj_tgt_init(l_current_precinct->imsbtree,
1147
1.09k
                                                       l_current_precinct->cw, l_current_precinct->ch, manager);
1148
1.09k
                    }
1149
1150
4.24M
                    for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1151
2.48M
                        OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno %
1152
2.48M
                                               l_current_precinct->cw) * (1 << cblkwidthexpn);
1153
2.48M
                        OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno /
1154
2.48M
                                               l_current_precinct->cw) * (1 << cblkheightexpn);
1155
2.48M
                        OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);
1156
2.48M
                        OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);
1157
1158
2.48M
                        if (isEncoder) {
1159
0
                            opj_tcd_cblk_enc_t* l_code_block = l_current_precinct->cblks.enc + cblkno;
1160
1161
0
                            if (! opj_tcd_code_block_enc_allocate(l_code_block)) {
1162
0
                                return OPJ_FALSE;
1163
0
                            }
1164
                            /* code-block size (global) */
1165
0
                            l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1166
0
                            l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1167
0
                            l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1168
0
                            l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1169
1170
0
                            if (! opj_tcd_code_block_enc_allocate_data(l_code_block)) {
1171
0
                                return OPJ_FALSE;
1172
0
                            }
1173
2.48M
                        } else {
1174
2.48M
                            opj_tcd_cblk_dec_t* l_code_block = l_current_precinct->cblks.dec + cblkno;
1175
1176
2.48M
                            if (! opj_tcd_code_block_dec_allocate(l_code_block)) {
1177
0
                                return OPJ_FALSE;
1178
0
                            }
1179
                            /* code-block size (global) */
1180
2.48M
                            l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);
1181
2.48M
                            l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);
1182
2.48M
                            l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);
1183
2.48M
                            l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);
1184
2.48M
                        }
1185
2.48M
                    }
1186
1.76M
                    ++l_current_precinct;
1187
1.76M
                } /* precno */
1188
22.6k
            } /* bandno */
1189
8.55k
            ++l_res;
1190
8.55k
        } /* resno */
1191
1.52k
        ++l_tccp;
1192
1.52k
        ++l_tilec;
1193
1.52k
        ++l_image_comp;
1194
1.52k
    } /* compno */
1195
1.22k
    return OPJ_TRUE;
1196
1.22k
}
1197
1198
OPJ_BOOL opj_tcd_init_encode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1199
                                  opj_event_mgr_t* p_manager)
1200
0
{
1201
0
    return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_TRUE,
1202
0
                             sizeof(opj_tcd_cblk_enc_t), p_manager);
1203
0
}
1204
1205
OPJ_BOOL opj_tcd_init_decode_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no,
1206
                                  opj_event_mgr_t* p_manager)
1207
1.22k
{
1208
1.22k
    return opj_tcd_init_tile(p_tcd, p_tile_no, OPJ_FALSE,
1209
1.22k
                             sizeof(opj_tcd_cblk_dec_t), p_manager);
1210
1.22k
}
1211
1212
/**
1213
 * Allocates memory for an encoding code block (but not data memory).
1214
 */
1215
static OPJ_BOOL opj_tcd_code_block_enc_allocate(opj_tcd_cblk_enc_t *
1216
        p_code_block)
1217
0
{
1218
0
    if (! p_code_block->layers) {
1219
        /* no memset since data */
1220
0
        p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100,
1221
0
                               sizeof(opj_tcd_layer_t));
1222
0
        if (! p_code_block->layers) {
1223
0
            return OPJ_FALSE;
1224
0
        }
1225
0
    }
1226
0
    if (! p_code_block->passes) {
1227
0
        p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100,
1228
0
                               sizeof(opj_tcd_pass_t));
1229
0
        if (! p_code_block->passes) {
1230
0
            return OPJ_FALSE;
1231
0
        }
1232
0
    }
1233
0
    return OPJ_TRUE;
1234
0
}
1235
1236
/**
1237
 * Allocates data memory for an encoding code block.
1238
 */
1239
static OPJ_BOOL opj_tcd_code_block_enc_allocate_data(opj_tcd_cblk_enc_t *
1240
        p_code_block)
1241
0
{
1242
0
    OPJ_UINT32 l_data_size;
1243
1244
    /* +1 is needed for https://github.com/uclouvain/openjpeg/issues/835 */
1245
    /* and actually +2 required for https://github.com/uclouvain/openjpeg/issues/982 */
1246
    /* and +7 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 3) */
1247
    /* and +26 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 7) */
1248
    /* and +28 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 44) */
1249
    /* and +33 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4) */
1250
    /* and +63 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -IMF 2K) */
1251
    /* and +74 for https://github.com/uclouvain/openjpeg/issues/1283 (-M 4 -n 8 -s 7,7 -I) */
1252
    /* TODO: is there a theoretical upper-bound for the compressed code */
1253
    /* block size ? */
1254
0
    l_data_size = 74 + (OPJ_UINT32)((p_code_block->x1 - p_code_block->x0) *
1255
0
                                    (p_code_block->y1 - p_code_block->y0) * (OPJ_INT32)sizeof(OPJ_UINT32));
1256
1257
0
    if (l_data_size > p_code_block->data_size) {
1258
0
        if (p_code_block->data) {
1259
            /* We refer to data - 1 since below we incremented it */
1260
0
            opj_free(p_code_block->data - 1);
1261
0
        }
1262
0
        p_code_block->data = (OPJ_BYTE*) opj_malloc(l_data_size + 1);
1263
0
        if (! p_code_block->data) {
1264
0
            p_code_block->data_size = 0U;
1265
0
            return OPJ_FALSE;
1266
0
        }
1267
0
        p_code_block->data_size = l_data_size;
1268
1269
        /* We reserve the initial byte as a fake byte to a non-FF value */
1270
        /* and increment the data pointer, so that opj_mqc_init_enc() */
1271
        /* can do bp = data - 1, and opj_mqc_byteout() can safely dereference */
1272
        /* it. */
1273
0
        p_code_block->data[0] = 0;
1274
0
        p_code_block->data += 1; /*why +1 ?*/
1275
0
    }
1276
0
    return OPJ_TRUE;
1277
0
}
1278
1279
1280
void opj_tcd_reinit_segment(opj_tcd_seg_t* seg)
1281
1.55M
{
1282
1.55M
    memset(seg, 0, sizeof(opj_tcd_seg_t));
1283
1.55M
}
1284
1285
/**
1286
 * Allocates memory for a decoding code block.
1287
 */
1288
static OPJ_BOOL opj_tcd_code_block_dec_allocate(opj_tcd_cblk_dec_t *
1289
        p_code_block)
1290
2.48M
{
1291
2.48M
    if (! p_code_block->segs) {
1292
1293
2.38M
        p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,
1294
2.38M
                             sizeof(opj_tcd_seg_t));
1295
2.38M
        if (! p_code_block->segs) {
1296
0
            return OPJ_FALSE;
1297
0
        }
1298
        /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1299
1300
2.38M
        p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1301
        /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1302
2.38M
    } else {
1303
        /* sanitize */
1304
97.4k
        opj_tcd_seg_t * l_segs = p_code_block->segs;
1305
97.4k
        OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1306
97.4k
        opj_tcd_seg_data_chunk_t* l_chunks = p_code_block->chunks;
1307
97.4k
        OPJ_UINT32 l_numchunksalloc = p_code_block->numchunksalloc;
1308
97.4k
        OPJ_UINT32 i;
1309
1310
97.4k
        opj_aligned_free(p_code_block->decoded_data);
1311
97.4k
        p_code_block->decoded_data = 00;
1312
1313
97.4k
        memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1314
97.4k
        p_code_block->segs = l_segs;
1315
97.4k
        p_code_block->m_current_max_segs = l_current_max_segs;
1316
1.07M
        for (i = 0; i < l_current_max_segs; ++i) {
1317
974k
            opj_tcd_reinit_segment(&l_segs[i]);
1318
974k
        }
1319
97.4k
        p_code_block->chunks = l_chunks;
1320
97.4k
        p_code_block->numchunksalloc = l_numchunksalloc;
1321
97.4k
    }
1322
1323
2.48M
    return OPJ_TRUE;
1324
2.48M
}
1325
1326
OPJ_UINT32 opj_tcd_get_decoded_tile_size(opj_tcd_t *p_tcd,
1327
        OPJ_BOOL take_into_account_partial_decoding)
1328
0
{
1329
0
    OPJ_UINT32 i;
1330
0
    OPJ_UINT32 l_data_size = 0;
1331
0
    opj_image_comp_t * l_img_comp = 00;
1332
0
    opj_tcd_tilecomp_t * l_tile_comp = 00;
1333
0
    opj_tcd_resolution_t * l_res = 00;
1334
0
    OPJ_UINT32 l_size_comp, l_remaining;
1335
0
    OPJ_UINT32 l_temp;
1336
1337
0
    l_tile_comp = p_tcd->tcd_image->tiles->comps;
1338
0
    l_img_comp = p_tcd->image->comps;
1339
1340
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
1341
0
        OPJ_UINT32 w, h;
1342
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1343
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
1344
1345
0
        if (l_remaining) {
1346
0
            ++l_size_comp;
1347
0
        }
1348
1349
0
        if (l_size_comp == 3) {
1350
0
            l_size_comp = 4;
1351
0
        }
1352
1353
0
        l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1354
0
        if (take_into_account_partial_decoding && !p_tcd->whole_tile_decoding) {
1355
0
            w = l_res->win_x1 - l_res->win_x0;
1356
0
            h = l_res->win_y1 - l_res->win_y0;
1357
0
        } else {
1358
0
            w = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1359
0
            h = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1360
0
        }
1361
0
        if (h > 0 && UINT_MAX / w < h) {
1362
0
            return UINT_MAX;
1363
0
        }
1364
0
        l_temp = w * h;
1365
0
        if (l_size_comp && UINT_MAX / l_size_comp < l_temp) {
1366
0
            return UINT_MAX;
1367
0
        }
1368
0
        l_temp *= l_size_comp;
1369
1370
0
        if (l_temp > UINT_MAX - l_data_size) {
1371
0
            return UINT_MAX;
1372
0
        }
1373
0
        l_data_size += l_temp;
1374
0
        ++l_img_comp;
1375
0
        ++l_tile_comp;
1376
0
    }
1377
1378
0
    return l_data_size;
1379
0
}
1380
1381
OPJ_BOOL opj_tcd_encode_tile(opj_tcd_t *p_tcd,
1382
                             OPJ_UINT32 p_tile_no,
1383
                             OPJ_BYTE *p_dest,
1384
                             OPJ_UINT32 * p_data_written,
1385
                             OPJ_UINT32 p_max_length,
1386
                             opj_codestream_info_t *p_cstr_info,
1387
                             opj_tcd_marker_info_t* p_marker_info,
1388
                             opj_event_mgr_t *p_manager)
1389
0
{
1390
1391
0
    if (p_tcd->cur_tp_num == 0) {
1392
1393
0
        p_tcd->tcd_tileno = p_tile_no;
1394
0
        p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1395
1396
        /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1397
0
        if (p_cstr_info)  {
1398
0
            OPJ_UINT32 l_num_packs = 0;
1399
0
            OPJ_UINT32 i;
1400
0
            opj_tcd_tilecomp_t *l_tilec_idx =
1401
0
                &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1402
0
            opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1403
1404
0
            for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1405
0
                opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1406
1407
0
                p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1408
0
                p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1409
1410
0
                l_num_packs += l_res_idx->pw * l_res_idx->ph;
1411
0
                p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1412
0
                p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1413
0
            }
1414
0
            p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((
1415
0
                    OPJ_SIZE_T)p_cstr_info->numcomps * (OPJ_SIZE_T)p_cstr_info->numlayers *
1416
0
                                                  l_num_packs,
1417
0
                                                  sizeof(opj_packet_info_t));
1418
0
            if (!p_cstr_info->tile[p_tile_no].packet) {
1419
                /* FIXME event manager error callback */
1420
0
                return OPJ_FALSE;
1421
0
            }
1422
0
        }
1423
        /* << INDEX */
1424
1425
        /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1426
        /*---------------TILE-------------------*/
1427
0
        if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1428
0
            return OPJ_FALSE;
1429
0
        }
1430
        /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1431
1432
        /* FIXME _ProfStart(PGROUP_MCT); */
1433
0
        if (! opj_tcd_mct_encode(p_tcd)) {
1434
0
            return OPJ_FALSE;
1435
0
        }
1436
        /* FIXME _ProfStop(PGROUP_MCT); */
1437
1438
        /* FIXME _ProfStart(PGROUP_DWT); */
1439
0
        if (! opj_tcd_dwt_encode(p_tcd)) {
1440
0
            return OPJ_FALSE;
1441
0
        }
1442
        /* FIXME  _ProfStop(PGROUP_DWT); */
1443
1444
        /* FIXME  _ProfStart(PGROUP_T1); */
1445
0
        if (! opj_tcd_t1_encode(p_tcd)) {
1446
0
            return OPJ_FALSE;
1447
0
        }
1448
        /* FIXME _ProfStop(PGROUP_T1); */
1449
1450
        /* FIXME _ProfStart(PGROUP_RATE); */
1451
0
        if (! opj_tcd_rate_allocate_encode(p_tcd, p_dest, p_max_length,
1452
0
                                           p_cstr_info, p_manager)) {
1453
0
            return OPJ_FALSE;
1454
0
        }
1455
        /* FIXME _ProfStop(PGROUP_RATE); */
1456
1457
0
    }
1458
    /*--------------TIER2------------------*/
1459
1460
    /* INDEX */
1461
0
    if (p_cstr_info) {
1462
0
        p_cstr_info->index_write = 1;
1463
0
    }
1464
    /* FIXME _ProfStart(PGROUP_T2); */
1465
1466
0
    if (! opj_tcd_t2_encode(p_tcd, p_dest, p_data_written, p_max_length,
1467
0
                            p_cstr_info, p_marker_info, p_manager)) {
1468
0
        return OPJ_FALSE;
1469
0
    }
1470
    /* FIXME _ProfStop(PGROUP_T2); */
1471
1472
    /*---------------CLEAN-------------------*/
1473
1474
0
    return OPJ_TRUE;
1475
0
}
1476
1477
OPJ_BOOL opj_tcd_decode_tile(opj_tcd_t *p_tcd,
1478
                             OPJ_UINT32 win_x0,
1479
                             OPJ_UINT32 win_y0,
1480
                             OPJ_UINT32 win_x1,
1481
                             OPJ_UINT32 win_y1,
1482
                             OPJ_UINT32 numcomps_to_decode,
1483
                             const OPJ_UINT32 *comps_indices,
1484
                             OPJ_BYTE *p_src,
1485
                             OPJ_UINT32 p_max_length,
1486
                             OPJ_UINT32 p_tile_no,
1487
                             opj_codestream_index_t *p_cstr_index,
1488
                             opj_event_mgr_t *p_manager
1489
                            )
1490
1.22k
{
1491
1.22k
    OPJ_UINT32 l_data_read;
1492
1.22k
    OPJ_UINT32 compno;
1493
1494
1.22k
    p_tcd->tcd_tileno = p_tile_no;
1495
1.22k
    p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1496
1.22k
    p_tcd->win_x0 = win_x0;
1497
1.22k
    p_tcd->win_y0 = win_y0;
1498
1.22k
    p_tcd->win_x1 = win_x1;
1499
1.22k
    p_tcd->win_y1 = win_y1;
1500
1.22k
    p_tcd->whole_tile_decoding = OPJ_TRUE;
1501
1502
1.22k
    opj_free(p_tcd->used_component);
1503
1.22k
    p_tcd->used_component = NULL;
1504
1505
1.22k
    if (numcomps_to_decode) {
1506
0
        OPJ_BOOL* used_component = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
1507
0
                                   p_tcd->image->numcomps);
1508
0
        if (used_component == NULL) {
1509
0
            return OPJ_FALSE;
1510
0
        }
1511
0
        for (compno = 0; compno < numcomps_to_decode; compno++) {
1512
0
            used_component[ comps_indices[compno] ] = OPJ_TRUE;
1513
0
        }
1514
1515
0
        p_tcd->used_component = used_component;
1516
0
    }
1517
1518
2.73k
    for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1519
1.51k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1520
0
            continue;
1521
0
        }
1522
1523
1.51k
        if (!opj_tcd_is_whole_tilecomp_decoding(p_tcd, compno)) {
1524
0
            p_tcd->whole_tile_decoding = OPJ_FALSE;
1525
0
            break;
1526
0
        }
1527
1.51k
    }
1528
1529
1.22k
    if (p_tcd->whole_tile_decoding) {
1530
2.73k
        for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1531
1.51k
            opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1532
1.51k
            opj_tcd_resolution_t *l_res = &
1533
1.51k
                                          (tilec->resolutions[tilec->minimum_num_resolutions - 1]);
1534
1.51k
            OPJ_SIZE_T l_data_size;
1535
1536
            /* compute l_data_size with overflow check */
1537
1.51k
            OPJ_SIZE_T res_w = (OPJ_SIZE_T)(l_res->x1 - l_res->x0);
1538
1.51k
            OPJ_SIZE_T res_h = (OPJ_SIZE_T)(l_res->y1 - l_res->y0);
1539
1540
1.51k
            if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1541
0
                continue;
1542
0
            }
1543
1544
            /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */
1545
1.51k
            if (res_h > 0 && res_w > SIZE_MAX / res_h) {
1546
0
                opj_event_msg(p_manager, EVT_ERROR,
1547
0
                              "Size of tile data exceeds system limits\n");
1548
0
                return OPJ_FALSE;
1549
0
            }
1550
1.51k
            l_data_size = res_w * res_h;
1551
1552
1.51k
            if (SIZE_MAX / sizeof(OPJ_UINT32) < l_data_size) {
1553
0
                opj_event_msg(p_manager, EVT_ERROR,
1554
0
                              "Size of tile data exceeds system limits\n");
1555
0
                return OPJ_FALSE;
1556
0
            }
1557
1.51k
            l_data_size *= sizeof(OPJ_UINT32);
1558
1559
1.51k
            tilec->data_size_needed = l_data_size;
1560
1561
1.51k
            if (!opj_alloc_tile_component_data(tilec)) {
1562
0
                opj_event_msg(p_manager, EVT_ERROR,
1563
0
                              "Size of tile data exceeds system limits\n");
1564
0
                return OPJ_FALSE;
1565
0
            }
1566
1.51k
        }
1567
1.22k
    } else {
1568
        /* Compute restricted tile-component and tile-resolution coordinates */
1569
        /* of the window of interest, but defer the memory allocation until */
1570
        /* we know the resno_decoded */
1571
0
        for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1572
0
            OPJ_UINT32 resno;
1573
0
            opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1574
0
            opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1575
1576
0
            if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1577
0
                continue;
1578
0
            }
1579
1580
            /* Compute the intersection of the area of interest, expressed in tile coordinates */
1581
            /* with the tile coordinates */
1582
0
            tilec->win_x0 = opj_uint_max(
1583
0
                                (OPJ_UINT32)tilec->x0,
1584
0
                                opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
1585
0
            tilec->win_y0 = opj_uint_max(
1586
0
                                (OPJ_UINT32)tilec->y0,
1587
0
                                opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
1588
0
            tilec->win_x1 = opj_uint_min(
1589
0
                                (OPJ_UINT32)tilec->x1,
1590
0
                                opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
1591
0
            tilec->win_y1 = opj_uint_min(
1592
0
                                (OPJ_UINT32)tilec->y1,
1593
0
                                opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
1594
0
            if (tilec->win_x1 < tilec->win_x0 ||
1595
0
                    tilec->win_y1 < tilec->win_y0) {
1596
                /* We should not normally go there. The circumstance is when */
1597
                /* the tile coordinates do not intersect the area of interest */
1598
                /* Upper level logic should not even try to decode that tile */
1599
0
                opj_event_msg(p_manager, EVT_ERROR,
1600
0
                              "Invalid tilec->win_xxx values\n");
1601
0
                return OPJ_FALSE;
1602
0
            }
1603
1604
0
            for (resno = 0; resno < tilec->numresolutions; ++resno) {
1605
0
                opj_tcd_resolution_t *res = tilec->resolutions + resno;
1606
0
                res->win_x0 = opj_uint_ceildivpow2(tilec->win_x0,
1607
0
                                                   tilec->numresolutions - 1 - resno);
1608
0
                res->win_y0 = opj_uint_ceildivpow2(tilec->win_y0,
1609
0
                                                   tilec->numresolutions - 1 - resno);
1610
0
                res->win_x1 = opj_uint_ceildivpow2(tilec->win_x1,
1611
0
                                                   tilec->numresolutions - 1 - resno);
1612
0
                res->win_y1 = opj_uint_ceildivpow2(tilec->win_y1,
1613
0
                                                   tilec->numresolutions - 1 - resno);
1614
0
            }
1615
0
        }
1616
0
    }
1617
1618
#ifdef TODO_MSD /* FIXME */
1619
    /* INDEX >>  */
1620
    if (p_cstr_info) {
1621
        OPJ_UINT32 resno, compno, numprec = 0;
1622
        for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1623
            opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1624
            opj_tccp_t *tccp = &tcp->tccps[compno];
1625
            opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1626
            for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1627
                opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1628
                p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1629
                p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1630
                numprec += res_idx->pw * res_idx->ph;
1631
                p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1632
                p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1633
            }
1634
        }
1635
        p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(
1636
                p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1637
        p_cstr_info->packno = 0;
1638
    }
1639
    /* << INDEX */
1640
#endif
1641
1642
    /*--------------TIER2------------------*/
1643
    /* FIXME _ProfStart(PGROUP_T2); */
1644
1.22k
    l_data_read = 0;
1645
1.22k
    if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index,
1646
1.22k
                            p_manager)) {
1647
57
        return OPJ_FALSE;
1648
57
    }
1649
    /* FIXME _ProfStop(PGROUP_T2); */
1650
1651
    /*------------------TIER1-----------------*/
1652
1653
    /* FIXME _ProfStart(PGROUP_T1); */
1654
1.16k
    if (! opj_tcd_t1_decode(p_tcd, p_manager)) {
1655
16
        return OPJ_FALSE;
1656
16
    }
1657
    /* FIXME _ProfStop(PGROUP_T1); */
1658
1659
1660
    /* For subtile decoding, now we know the resno_decoded, we can allocate */
1661
    /* the tile data buffer */
1662
1.14k
    if (!p_tcd->whole_tile_decoding) {
1663
0
        for (compno = 0; compno < p_tcd->image->numcomps; compno++) {
1664
0
            opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
1665
0
            opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
1666
0
            opj_tcd_resolution_t *res = tilec->resolutions + image_comp->resno_decoded;
1667
0
            OPJ_SIZE_T w = res->win_x1 - res->win_x0;
1668
0
            OPJ_SIZE_T h = res->win_y1 - res->win_y0;
1669
0
            OPJ_SIZE_T l_data_size;
1670
1671
0
            opj_image_data_free(tilec->data_win);
1672
0
            tilec->data_win = NULL;
1673
1674
0
            if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1675
0
                continue;
1676
0
            }
1677
1678
0
            if (w > 0 && h > 0) {
1679
0
                if (w > SIZE_MAX / h) {
1680
0
                    opj_event_msg(p_manager, EVT_ERROR,
1681
0
                                  "Size of tile data exceeds system limits\n");
1682
0
                    return OPJ_FALSE;
1683
0
                }
1684
0
                l_data_size = w * h;
1685
0
                if (l_data_size > SIZE_MAX / sizeof(OPJ_INT32)) {
1686
0
                    opj_event_msg(p_manager, EVT_ERROR,
1687
0
                                  "Size of tile data exceeds system limits\n");
1688
0
                    return OPJ_FALSE;
1689
0
                }
1690
0
                l_data_size *= sizeof(OPJ_INT32);
1691
1692
0
                tilec->data_win = (OPJ_INT32*) opj_image_data_alloc(l_data_size);
1693
0
                if (tilec->data_win == NULL) {
1694
0
                    opj_event_msg(p_manager, EVT_ERROR,
1695
0
                                  "Size of tile data exceeds system limits\n");
1696
0
                    return OPJ_FALSE;
1697
0
                }
1698
0
            }
1699
0
        }
1700
0
    }
1701
1702
    /*----------------DWT---------------------*/
1703
1704
    /* FIXME _ProfStart(PGROUP_DWT); */
1705
1.14k
    if
1706
1.14k
    (! opj_tcd_dwt_decode(p_tcd)) {
1707
0
        return OPJ_FALSE;
1708
0
    }
1709
    /* FIXME _ProfStop(PGROUP_DWT); */
1710
1711
    /*----------------MCT-------------------*/
1712
    /* FIXME _ProfStart(PGROUP_MCT); */
1713
1.14k
    if
1714
1.14k
    (! opj_tcd_mct_decode(p_tcd, p_manager)) {
1715
34
        return OPJ_FALSE;
1716
34
    }
1717
    /* FIXME _ProfStop(PGROUP_MCT); */
1718
1719
    /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1720
1.11k
    if
1721
1.11k
    (! opj_tcd_dc_level_shift_decode(p_tcd)) {
1722
0
        return OPJ_FALSE;
1723
0
    }
1724
    /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1725
1726
1727
    /*---------------TILE-------------------*/
1728
1.11k
    return OPJ_TRUE;
1729
1.11k
}
1730
1731
OPJ_BOOL opj_tcd_update_tile_data(opj_tcd_t *p_tcd,
1732
                                  OPJ_BYTE * p_dest,
1733
                                  OPJ_UINT32 p_dest_length
1734
                                 )
1735
0
{
1736
0
    OPJ_UINT32 i, j, k, l_data_size = 0;
1737
0
    opj_image_comp_t * l_img_comp = 00;
1738
0
    opj_tcd_tilecomp_t * l_tilec = 00;
1739
0
    opj_tcd_resolution_t * l_res;
1740
0
    OPJ_UINT32 l_size_comp, l_remaining;
1741
0
    OPJ_UINT32 l_stride, l_width, l_height;
1742
1743
0
    l_data_size = opj_tcd_get_decoded_tile_size(p_tcd, OPJ_TRUE);
1744
0
    if (l_data_size == UINT_MAX || l_data_size > p_dest_length) {
1745
0
        return OPJ_FALSE;
1746
0
    }
1747
1748
0
    l_tilec = p_tcd->tcd_image->tiles->comps;
1749
0
    l_img_comp = p_tcd->image->comps;
1750
1751
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
1752
0
        const OPJ_INT32* l_src_data;
1753
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1754
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
1755
0
        l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1756
0
        if (p_tcd->whole_tile_decoding) {
1757
0
            l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1758
0
            l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1759
0
            l_stride = (OPJ_UINT32)(l_tilec->resolutions[l_tilec->minimum_num_resolutions -
1760
0
                                                                     1].x1 -
1761
0
                                    l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0) - l_width;
1762
0
            l_src_data = l_tilec->data;
1763
0
        } else {
1764
0
            l_width = l_res->win_x1 - l_res->win_x0;
1765
0
            l_height = l_res->win_y1 - l_res->win_y0;
1766
0
            l_stride = 0;
1767
0
            l_src_data = l_tilec->data_win;
1768
0
        }
1769
1770
0
        if (l_remaining) {
1771
0
            ++l_size_comp;
1772
0
        }
1773
1774
0
        if (l_size_comp == 3) {
1775
0
            l_size_comp = 4;
1776
0
        }
1777
1778
0
        switch (l_size_comp) {
1779
0
        case 1: {
1780
0
            OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1781
0
            const OPJ_INT32 * l_src_ptr = l_src_data;
1782
1783
0
            if (l_img_comp->sgnd) {
1784
0
                for (j = 0; j < l_height; ++j) {
1785
0
                    for (k = 0; k < l_width; ++k) {
1786
0
                        *(l_dest_ptr++) = (OPJ_CHAR)(*(l_src_ptr++));
1787
0
                    }
1788
0
                    l_src_ptr += l_stride;
1789
0
                }
1790
0
            } else {
1791
0
                for (j = 0; j < l_height; ++j) {
1792
0
                    for (k = 0; k < l_width; ++k) {
1793
0
                        *(l_dest_ptr++) = (OPJ_CHAR)((*(l_src_ptr++)) & 0xff);
1794
0
                    }
1795
0
                    l_src_ptr += l_stride;
1796
0
                }
1797
0
            }
1798
1799
0
            p_dest = (OPJ_BYTE *)l_dest_ptr;
1800
0
        }
1801
0
        break;
1802
0
        case 2: {
1803
0
            const OPJ_INT32 * l_src_ptr = l_src_data;
1804
0
            OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1805
1806
0
            if (l_img_comp->sgnd) {
1807
0
                for (j = 0; j < l_height; ++j) {
1808
0
                    for (k = 0; k < l_width; ++k) {
1809
0
                        OPJ_INT16 val = (OPJ_INT16)(*(l_src_ptr++));
1810
0
                        memcpy(l_dest_ptr, &val, sizeof(val));
1811
0
                        l_dest_ptr ++;
1812
0
                    }
1813
0
                    l_src_ptr += l_stride;
1814
0
                }
1815
0
            } else {
1816
0
                for (j = 0; j < l_height; ++j) {
1817
0
                    for (k = 0; k < l_width; ++k) {
1818
0
                        OPJ_INT16 val = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
1819
0
                        memcpy(l_dest_ptr, &val, sizeof(val));
1820
0
                        l_dest_ptr ++;
1821
0
                    }
1822
0
                    l_src_ptr += l_stride;
1823
0
                }
1824
0
            }
1825
1826
0
            p_dest = (OPJ_BYTE*) l_dest_ptr;
1827
0
        }
1828
0
        break;
1829
0
        case 4: {
1830
0
            OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1831
0
            const OPJ_INT32 * l_src_ptr = l_src_data;
1832
1833
0
            for (j = 0; j < l_height; ++j) {
1834
0
                memcpy(l_dest_ptr, l_src_ptr, l_width * sizeof(OPJ_INT32));
1835
0
                l_dest_ptr += l_width;
1836
0
                l_src_ptr += l_width + l_stride;
1837
0
            }
1838
1839
0
            p_dest = (OPJ_BYTE*) l_dest_ptr;
1840
0
        }
1841
0
        break;
1842
0
        }
1843
1844
0
        ++l_img_comp;
1845
0
        ++l_tilec;
1846
0
    }
1847
1848
0
    return OPJ_TRUE;
1849
0
}
1850
1851
1852
1853
1854
static void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1855
1.23k
{
1856
1.23k
    OPJ_UINT32 compno, resno, bandno, precno;
1857
1.23k
    opj_tcd_tile_t *l_tile = 00;
1858
1.23k
    opj_tcd_tilecomp_t *l_tile_comp = 00;
1859
1.23k
    opj_tcd_resolution_t *l_res = 00;
1860
1.23k
    opj_tcd_band_t *l_band = 00;
1861
1.23k
    opj_tcd_precinct_t *l_precinct = 00;
1862
1.23k
    OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1863
1.23k
    void (* l_tcd_code_block_deallocate)(opj_tcd_precinct_t *) = 00;
1864
1865
1.23k
    if (! p_tcd) {
1866
0
        return;
1867
0
    }
1868
1869
1.23k
    if (! p_tcd->tcd_image) {
1870
0
        return;
1871
0
    }
1872
1873
1.23k
    if (p_tcd->m_is_decoder) {
1874
1.23k
        l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1875
1.23k
    } else {
1876
0
        l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1877
0
    }
1878
1879
1.23k
    l_tile = p_tcd->tcd_image->tiles;
1880
1.23k
    if (! l_tile) {
1881
0
        return;
1882
0
    }
1883
1884
1.23k
    l_tile_comp = l_tile->comps;
1885
1886
2.72k
    for (compno = 0; compno < l_tile->numcomps; ++compno) {
1887
1.49k
        l_res = l_tile_comp->resolutions;
1888
1.49k
        if (l_res) {
1889
1890
1.41k
            l_nb_resolutions = l_tile_comp->resolutions_size / (OPJ_UINT32)sizeof(
1891
1.41k
                                   opj_tcd_resolution_t);
1892
7.99k
            for (resno = 0; resno < l_nb_resolutions; ++resno) {
1893
6.57k
                l_band = l_res->bands;
1894
26.2k
                for (bandno = 0; bandno < 3; ++bandno) {
1895
19.7k
                    l_precinct = l_band->precincts;
1896
19.7k
                    if (l_precinct) {
1897
1898
4.90k
                        l_nb_precincts = l_band->precincts_data_size / (OPJ_UINT32)sizeof(
1899
4.90k
                                             opj_tcd_precinct_t);
1900
1.77M
                        for (precno = 0; precno < l_nb_precincts; ++precno) {
1901
1.76M
                            opj_tgt_destroy(l_precinct->incltree);
1902
1.76M
                            l_precinct->incltree = 00;
1903
1.76M
                            opj_tgt_destroy(l_precinct->imsbtree);
1904
1.76M
                            l_precinct->imsbtree = 00;
1905
1.76M
                            (*l_tcd_code_block_deallocate)(l_precinct);
1906
1.76M
                            ++l_precinct;
1907
1.76M
                        }
1908
1909
4.90k
                        opj_free(l_band->precincts);
1910
4.90k
                        l_band->precincts = 00;
1911
4.90k
                    }
1912
19.7k
                    ++l_band;
1913
19.7k
                } /* for (resno */
1914
6.57k
                ++l_res;
1915
6.57k
            }
1916
1917
1.41k
            opj_free(l_tile_comp->resolutions);
1918
1.41k
            l_tile_comp->resolutions = 00;
1919
1.41k
        }
1920
1921
1.49k
        if (l_tile_comp->ownsData && l_tile_comp->data) {
1922
631
            opj_image_data_free(l_tile_comp->data);
1923
631
            l_tile_comp->data = 00;
1924
631
            l_tile_comp->ownsData = 0;
1925
631
            l_tile_comp->data_size = 0;
1926
631
            l_tile_comp->data_size_needed = 0;
1927
631
        }
1928
1929
1.49k
        opj_image_data_free(l_tile_comp->data_win);
1930
1931
1.49k
        ++l_tile_comp;
1932
1.49k
    }
1933
1934
1.23k
    opj_free(l_tile->comps);
1935
1.23k
    l_tile->comps = 00;
1936
1.23k
    opj_free(p_tcd->tcd_image->tiles);
1937
1.23k
    p_tcd->tcd_image->tiles = 00;
1938
1.23k
}
1939
1940
1941
static OPJ_BOOL opj_tcd_t2_decode(opj_tcd_t *p_tcd,
1942
                                  OPJ_BYTE * p_src_data,
1943
                                  OPJ_UINT32 * p_data_read,
1944
                                  OPJ_UINT32 p_max_src_size,
1945
                                  opj_codestream_index_t *p_cstr_index,
1946
                                  opj_event_mgr_t *p_manager
1947
                                 )
1948
1.22k
{
1949
1.22k
    opj_t2_t * l_t2;
1950
1951
1.22k
    l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1952
1.22k
    if (l_t2 == 00) {
1953
0
        return OPJ_FALSE;
1954
0
    }
1955
1956
1.22k
    if (! opj_t2_decode_packets(
1957
1.22k
                p_tcd,
1958
1.22k
                l_t2,
1959
1.22k
                p_tcd->tcd_tileno,
1960
1.22k
                p_tcd->tcd_image->tiles,
1961
1.22k
                p_src_data,
1962
1.22k
                p_data_read,
1963
1.22k
                p_max_src_size,
1964
1.22k
                p_cstr_index,
1965
1.22k
                p_manager)) {
1966
57
        opj_t2_destroy(l_t2);
1967
57
        return OPJ_FALSE;
1968
57
    }
1969
1970
1.16k
    opj_t2_destroy(l_t2);
1971
1972
    /*---------------CLEAN-------------------*/
1973
1.16k
    return OPJ_TRUE;
1974
1.22k
}
1975
1976
static OPJ_BOOL opj_tcd_t1_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
1977
1.16k
{
1978
1.16k
    OPJ_UINT32 compno;
1979
1.16k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1980
1.16k
    opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1981
1.16k
    opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1982
1.16k
    volatile OPJ_BOOL ret = OPJ_TRUE;
1983
1.16k
    OPJ_BOOL check_pterm = OPJ_FALSE;
1984
1.16k
    opj_mutex_t* p_manager_mutex = NULL;
1985
1986
1.16k
    p_manager_mutex = opj_mutex_create();
1987
1988
    /* Only enable PTERM check if we decode all layers */
1989
1.16k
    if (p_tcd->tcp->num_layers_to_decode == p_tcd->tcp->numlayers &&
1990
1.16k
            (l_tccp->cblksty & J2K_CCP_CBLKSTY_PTERM) != 0) {
1991
366
        check_pterm = OPJ_TRUE;
1992
366
    }
1993
1994
2.56k
    for (compno = 0; compno < l_tile->numcomps;
1995
1.41k
            ++compno, ++l_tile_comp, ++l_tccp) {
1996
1.41k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
1997
0
            continue;
1998
0
        }
1999
2000
1.41k
        opj_t1_decode_cblks(p_tcd, &ret, l_tile_comp, l_tccp,
2001
1.41k
                            p_manager, p_manager_mutex, check_pterm);
2002
1.41k
        if (!ret) {
2003
16
            break;
2004
16
        }
2005
1.41k
    }
2006
2007
1.16k
    opj_thread_pool_wait_completion(p_tcd->thread_pool, 0);
2008
1.16k
    if (p_manager_mutex) {
2009
1.16k
        opj_mutex_destroy(p_manager_mutex);
2010
1.16k
    }
2011
1.16k
    return ret;
2012
1.16k
}
2013
2014
2015
static OPJ_BOOL opj_tcd_dwt_decode(opj_tcd_t *p_tcd)
2016
1.14k
{
2017
1.14k
    OPJ_UINT32 compno;
2018
1.14k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2019
1.14k
    opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2020
1.14k
    opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2021
1.14k
    opj_image_comp_t * l_img_comp = p_tcd->image->comps;
2022
2023
2.54k
    for (compno = 0; compno < l_tile->numcomps;
2024
1.39k
            compno++, ++l_tile_comp, ++l_img_comp, ++l_tccp) {
2025
1.39k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2026
0
            continue;
2027
0
        }
2028
2029
1.39k
        if (l_tccp->qmfbid == 1) {
2030
1.28k
            if (! opj_dwt_decode(p_tcd, l_tile_comp,
2031
1.28k
                                 l_img_comp->resno_decoded + 1)) {
2032
0
                return OPJ_FALSE;
2033
0
            }
2034
1.28k
        } else {
2035
111
            if (! opj_dwt_decode_real(p_tcd, l_tile_comp,
2036
111
                                      l_img_comp->resno_decoded + 1)) {
2037
0
                return OPJ_FALSE;
2038
0
            }
2039
111
        }
2040
2041
1.39k
    }
2042
2043
1.14k
    return OPJ_TRUE;
2044
1.14k
}
2045
2046
static OPJ_BOOL opj_tcd_mct_decode(opj_tcd_t *p_tcd, opj_event_mgr_t *p_manager)
2047
1.14k
{
2048
1.14k
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2049
1.14k
    opj_tcp_t * l_tcp = p_tcd->tcp;
2050
1.14k
    opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
2051
1.14k
    OPJ_SIZE_T l_samples;
2052
1.14k
    OPJ_UINT32 i;
2053
2054
1.14k
    if (l_tcp->mct == 0 || p_tcd->used_component != NULL) {
2055
1.01k
        return OPJ_TRUE;
2056
1.01k
    }
2057
2058
137
    if (p_tcd->whole_tile_decoding) {
2059
137
        opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2060
137
                                          l_tile_comp->minimum_num_resolutions - 1;
2061
2062
        /* A bit inefficient: we process more data than needed if */
2063
        /* resno_decoded < l_tile_comp->minimum_num_resolutions-1, */
2064
        /* but we would need to take into account a stride then */
2065
137
        l_samples = (OPJ_SIZE_T)(res_comp0->x1 - res_comp0->x0) *
2066
137
                    (OPJ_SIZE_T)(res_comp0->y1 - res_comp0->y0);
2067
137
        if (l_tile->numcomps >= 3) {
2068
73
            if (l_tile_comp->minimum_num_resolutions !=
2069
73
                    l_tile->comps[1].minimum_num_resolutions ||
2070
73
                    l_tile_comp->minimum_num_resolutions !=
2071
73
                    l_tile->comps[2].minimum_num_resolutions) {
2072
0
                opj_event_msg(p_manager, EVT_ERROR,
2073
0
                              "Tiles don't all have the same dimension. Skip the MCT step.\n");
2074
0
                return OPJ_FALSE;
2075
0
            }
2076
73
        }
2077
137
        if (l_tile->numcomps >= 3) {
2078
73
            opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2079
73
                                              l_tile_comp->minimum_num_resolutions - 1;
2080
73
            opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2081
73
                                              l_tile_comp->minimum_num_resolutions - 1;
2082
            /* testcase 1336.pdf.asan.47.376 */
2083
73
            if (p_tcd->image->comps[0].resno_decoded !=
2084
73
                    p_tcd->image->comps[1].resno_decoded ||
2085
73
                    p_tcd->image->comps[0].resno_decoded !=
2086
51
                    p_tcd->image->comps[2].resno_decoded ||
2087
73
                    (OPJ_SIZE_T)(res_comp1->x1 - res_comp1->x0) *
2088
47
                    (OPJ_SIZE_T)(res_comp1->y1 - res_comp1->y0) != l_samples ||
2089
73
                    (OPJ_SIZE_T)(res_comp2->x1 - res_comp2->x0) *
2090
39
                    (OPJ_SIZE_T)(res_comp2->y1 - res_comp2->y0) != l_samples) {
2091
34
                opj_event_msg(p_manager, EVT_ERROR,
2092
34
                              "Tiles don't all have the same dimension. Skip the MCT step.\n");
2093
34
                return OPJ_FALSE;
2094
34
            }
2095
73
        }
2096
137
    } else {
2097
0
        opj_tcd_resolution_t* res_comp0 = l_tile->comps[0].resolutions +
2098
0
                                          p_tcd->image->comps[0].resno_decoded;
2099
2100
0
        l_samples = (OPJ_SIZE_T)(res_comp0->win_x1 - res_comp0->win_x0) *
2101
0
                    (OPJ_SIZE_T)(res_comp0->win_y1 - res_comp0->win_y0);
2102
0
        if (l_tile->numcomps >= 3) {
2103
0
            opj_tcd_resolution_t* res_comp1 = l_tile->comps[1].resolutions +
2104
0
                                              p_tcd->image->comps[1].resno_decoded;
2105
0
            opj_tcd_resolution_t* res_comp2 = l_tile->comps[2].resolutions +
2106
0
                                              p_tcd->image->comps[2].resno_decoded;
2107
            /* testcase 1336.pdf.asan.47.376 */
2108
0
            if (p_tcd->image->comps[0].resno_decoded !=
2109
0
                    p_tcd->image->comps[1].resno_decoded ||
2110
0
                    p_tcd->image->comps[0].resno_decoded !=
2111
0
                    p_tcd->image->comps[2].resno_decoded ||
2112
0
                    (OPJ_SIZE_T)(res_comp1->win_x1 - res_comp1->win_x0) *
2113
0
                    (OPJ_SIZE_T)(res_comp1->win_y1 - res_comp1->win_y0) != l_samples ||
2114
0
                    (OPJ_SIZE_T)(res_comp2->win_x1 - res_comp2->win_x0) *
2115
0
                    (OPJ_SIZE_T)(res_comp2->win_y1 - res_comp2->win_y0) != l_samples) {
2116
0
                opj_event_msg(p_manager, EVT_ERROR,
2117
0
                              "Tiles don't all have the same dimension. Skip the MCT step.\n");
2118
0
                return OPJ_FALSE;
2119
0
            }
2120
0
        }
2121
0
    }
2122
2123
103
    if (l_tile->numcomps >= 3) {
2124
39
        if (l_tcp->mct == 2) {
2125
0
            OPJ_BYTE ** l_data;
2126
2127
0
            if (! l_tcp->m_mct_decoding_matrix) {
2128
0
                return OPJ_TRUE;
2129
0
            }
2130
2131
0
            l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2132
0
            if (! l_data) {
2133
0
                return OPJ_FALSE;
2134
0
            }
2135
2136
0
            for (i = 0; i < l_tile->numcomps; ++i) {
2137
0
                if (p_tcd->whole_tile_decoding) {
2138
0
                    l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2139
0
                } else {
2140
0
                    l_data[i] = (OPJ_BYTE*) l_tile_comp->data_win;
2141
0
                }
2142
0
                ++l_tile_comp;
2143
0
            }
2144
2145
0
            if (! opj_mct_decode_custom(/* MCT data */
2146
0
                        (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
2147
                        /* size of components */
2148
0
                        l_samples,
2149
                        /* components */
2150
0
                        l_data,
2151
                        /* nb of components (i.e. size of pData) */
2152
0
                        l_tile->numcomps,
2153
                        /* tells if the data is signed */
2154
0
                        p_tcd->image->comps->sgnd)) {
2155
0
                opj_free(l_data);
2156
0
                return OPJ_FALSE;
2157
0
            }
2158
2159
0
            opj_free(l_data);
2160
39
        } else {
2161
39
            if (l_tcp->tccps->qmfbid == 1) {
2162
29
                if (p_tcd->whole_tile_decoding) {
2163
29
                    opj_mct_decode(l_tile->comps[0].data,
2164
29
                                   l_tile->comps[1].data,
2165
29
                                   l_tile->comps[2].data,
2166
29
                                   l_samples);
2167
29
                } else {
2168
0
                    opj_mct_decode(l_tile->comps[0].data_win,
2169
0
                                   l_tile->comps[1].data_win,
2170
0
                                   l_tile->comps[2].data_win,
2171
0
                                   l_samples);
2172
0
                }
2173
29
            } else {
2174
10
                if (p_tcd->whole_tile_decoding) {
2175
10
                    opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
2176
10
                                        (OPJ_FLOAT32*)l_tile->comps[1].data,
2177
10
                                        (OPJ_FLOAT32*)l_tile->comps[2].data,
2178
10
                                        l_samples);
2179
10
                } else {
2180
0
                    opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data_win,
2181
0
                                        (OPJ_FLOAT32*)l_tile->comps[1].data_win,
2182
0
                                        (OPJ_FLOAT32*)l_tile->comps[2].data_win,
2183
0
                                        l_samples);
2184
0
                }
2185
10
            }
2186
39
        }
2187
64
    } else {
2188
64
        opj_event_msg(p_manager, EVT_ERROR,
2189
64
                      "Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",
2190
64
                      l_tile->numcomps);
2191
64
    }
2192
2193
103
    return OPJ_TRUE;
2194
103
}
2195
2196
2197
static OPJ_BOOL opj_tcd_dc_level_shift_decode(opj_tcd_t *p_tcd)
2198
1.11k
{
2199
1.11k
    OPJ_UINT32 compno;
2200
1.11k
    opj_tcd_tilecomp_t * l_tile_comp = 00;
2201
1.11k
    opj_tccp_t * l_tccp = 00;
2202
1.11k
    opj_image_comp_t * l_img_comp = 00;
2203
1.11k
    opj_tcd_resolution_t* l_res = 00;
2204
1.11k
    opj_tcd_tile_t * l_tile;
2205
1.11k
    OPJ_UINT32 l_width, l_height, i, j;
2206
1.11k
    OPJ_INT32 * l_current_ptr;
2207
1.11k
    OPJ_INT32 l_min, l_max;
2208
1.11k
    OPJ_UINT32 l_stride;
2209
2210
1.11k
    l_tile = p_tcd->tcd_image->tiles;
2211
1.11k
    l_tile_comp = l_tile->comps;
2212
1.11k
    l_tccp = p_tcd->tcp->tccps;
2213
1.11k
    l_img_comp = p_tcd->image->comps;
2214
2215
2.40k
    for (compno = 0; compno < l_tile->numcomps;
2216
1.29k
            compno++, ++l_img_comp, ++l_tccp, ++l_tile_comp) {
2217
2218
1.29k
        if (p_tcd->used_component != NULL && !p_tcd->used_component[compno]) {
2219
0
            continue;
2220
0
        }
2221
2222
1.29k
        l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
2223
2224
1.29k
        if (!p_tcd->whole_tile_decoding) {
2225
0
            l_width = l_res->win_x1 - l_res->win_x0;
2226
0
            l_height = l_res->win_y1 - l_res->win_y0;
2227
0
            l_stride = 0;
2228
0
            l_current_ptr = l_tile_comp->data_win;
2229
1.29k
        } else {
2230
1.29k
            l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
2231
1.29k
            l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
2232
1.29k
            l_stride = (OPJ_UINT32)(
2233
1.29k
                           l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x1 -
2234
1.29k
                           l_tile_comp->resolutions[l_tile_comp->minimum_num_resolutions - 1].x0)
2235
1.29k
                       - l_width;
2236
1.29k
            l_current_ptr = l_tile_comp->data;
2237
2238
1.29k
            assert(l_height == 0 ||
2239
1.29k
                   l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
2240
1.29k
        }
2241
2242
1.29k
        if (l_img_comp->sgnd) {
2243
0
            l_min = -(1 << (l_img_comp->prec - 1));
2244
0
            l_max = (1 << (l_img_comp->prec - 1)) - 1;
2245
1.29k
        } else {
2246
1.29k
            l_min = 0;
2247
1.29k
            l_max = (OPJ_INT32)((1U << l_img_comp->prec) - 1);
2248
1.29k
        }
2249
2250
2251
1.29k
        if (l_tccp->qmfbid == 1) {
2252
14.0M
            for (j = 0; j < l_height; ++j) {
2253
6.36G
                for (i = 0; i < l_width; ++i) {
2254
                    /* TODO: do addition on int64 ? */
2255
6.34G
                    *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min,
2256
6.34G
                                                   l_max);
2257
6.34G
                    ++l_current_ptr;
2258
6.34G
                }
2259
14.0M
                l_current_ptr += l_stride;
2260
14.0M
            }
2261
1.20k
        } else {
2262
1.36M
            for (j = 0; j < l_height; ++j) {
2263
8.68M
                for (i = 0; i < l_width; ++i) {
2264
7.31M
                    OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
2265
7.31M
                    if (l_value > INT_MAX) {
2266
15
                        *l_current_ptr = l_max;
2267
7.31M
                    } else if (l_value < INT_MIN) {
2268
9
                        *l_current_ptr = l_min;
2269
7.31M
                    } else {
2270
                        /* Do addition on int64 to avoid overflows */
2271
7.31M
                        OPJ_INT64 l_value_int = (OPJ_INT64)opj_lrintf(l_value);
2272
7.31M
                        *l_current_ptr = (OPJ_INT32)opj_int64_clamp(
2273
7.31M
                                             l_value_int + l_tccp->m_dc_level_shift, l_min, l_max);
2274
7.31M
                    }
2275
7.31M
                    ++l_current_ptr;
2276
7.31M
                }
2277
1.36M
                l_current_ptr += l_stride;
2278
1.36M
            }
2279
90
        }
2280
1.29k
    }
2281
2282
1.11k
    return OPJ_TRUE;
2283
1.11k
}
2284
2285
2286
2287
/**
2288
 * Deallocates the encoding data of the given precinct.
2289
 */
2290
static void opj_tcd_code_block_dec_deallocate(opj_tcd_precinct_t * p_precinct)
2291
1.76M
{
2292
1.76M
    OPJ_UINT32 cblkno, l_nb_code_blocks;
2293
2294
1.76M
    opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
2295
1.76M
    if (l_code_block) {
2296
        /*fprintf(stderr,"deallocate codeblock:{\n");*/
2297
        /*fprintf(stderr,"\t x0=%d, y0=%d, x1=%d, y1=%d\n",l_code_block->x0, l_code_block->y0, l_code_block->x1, l_code_block->y1);*/
2298
        /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
2299
                        l_code_block->numbps, l_code_block->numlenbits, l_code_block->len, l_code_block->numnewpasses, l_code_block->real_num_segs, l_code_block->m_current_max_segs );*/
2300
2301
2302
1.76M
        l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2303
1.76M
                               opj_tcd_cblk_dec_t);
2304
        /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
2305
2306
4.14M
        for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
2307
2308
2.38M
            if (l_code_block->segs) {
2309
2.38M
                opj_free(l_code_block->segs);
2310
2.38M
                l_code_block->segs = 00;
2311
2.38M
            }
2312
2313
2.38M
            if (l_code_block->chunks) {
2314
461k
                opj_free(l_code_block->chunks);
2315
461k
                l_code_block->chunks = 00;
2316
461k
            }
2317
2318
2.38M
            opj_aligned_free(l_code_block->decoded_data);
2319
2.38M
            l_code_block->decoded_data = NULL;
2320
2321
2.38M
            ++l_code_block;
2322
2.38M
        }
2323
2324
1.76M
        opj_free(p_precinct->cblks.dec);
2325
1.76M
        p_precinct->cblks.dec = 00;
2326
1.76M
    }
2327
1.76M
}
2328
2329
/**
2330
 * Deallocates the encoding data of the given precinct.
2331
 */
2332
static void opj_tcd_code_block_enc_deallocate(opj_tcd_precinct_t * p_precinct)
2333
0
{
2334
0
    OPJ_UINT32 cblkno, l_nb_code_blocks;
2335
2336
0
    opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
2337
0
    if (l_code_block) {
2338
0
        l_nb_code_blocks = p_precinct->block_size / (OPJ_UINT32)sizeof(
2339
0
                               opj_tcd_cblk_enc_t);
2340
2341
0
        for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
2342
0
            if (l_code_block->data) {
2343
                /* We refer to data - 1 since below we incremented it */
2344
                /* in opj_tcd_code_block_enc_allocate_data() */
2345
0
                opj_free(l_code_block->data - 1);
2346
0
                l_code_block->data = 00;
2347
0
            }
2348
2349
0
            if (l_code_block->layers) {
2350
0
                opj_free(l_code_block->layers);
2351
0
                l_code_block->layers = 00;
2352
0
            }
2353
2354
0
            if (l_code_block->passes) {
2355
0
                opj_free(l_code_block->passes);
2356
0
                l_code_block->passes = 00;
2357
0
            }
2358
0
            ++l_code_block;
2359
0
        }
2360
2361
0
        opj_free(p_precinct->cblks.enc);
2362
2363
0
        p_precinct->cblks.enc = 00;
2364
0
    }
2365
0
}
2366
2367
OPJ_SIZE_T opj_tcd_get_encoder_input_buffer_size(opj_tcd_t *p_tcd)
2368
0
{
2369
0
    OPJ_UINT32 i;
2370
0
    OPJ_SIZE_T l_data_size = 0;
2371
0
    opj_image_comp_t * l_img_comp = 00;
2372
0
    opj_tcd_tilecomp_t * l_tilec = 00;
2373
0
    OPJ_UINT32 l_size_comp, l_remaining;
2374
2375
0
    l_tilec = p_tcd->tcd_image->tiles->comps;
2376
0
    l_img_comp = p_tcd->image->comps;
2377
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
2378
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2379
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
2380
2381
0
        if (l_remaining) {
2382
0
            ++l_size_comp;
2383
0
        }
2384
2385
0
        if (l_size_comp == 3) {
2386
0
            l_size_comp = 4;
2387
0
        }
2388
2389
0
        l_data_size += l_size_comp * ((OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2390
0
                                      (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0));
2391
0
        ++l_img_comp;
2392
0
        ++l_tilec;
2393
0
    }
2394
2395
0
    return l_data_size;
2396
0
}
2397
2398
static OPJ_BOOL opj_tcd_dc_level_shift_encode(opj_tcd_t *p_tcd)
2399
0
{
2400
0
    OPJ_UINT32 compno;
2401
0
    opj_tcd_tilecomp_t * l_tile_comp = 00;
2402
0
    opj_tccp_t * l_tccp = 00;
2403
0
    opj_image_comp_t * l_img_comp = 00;
2404
0
    opj_tcd_tile_t * l_tile;
2405
0
    OPJ_SIZE_T l_nb_elem, i;
2406
0
    OPJ_INT32 * l_current_ptr;
2407
2408
0
    l_tile = p_tcd->tcd_image->tiles;
2409
0
    l_tile_comp = l_tile->comps;
2410
0
    l_tccp = p_tcd->tcp->tccps;
2411
0
    l_img_comp = p_tcd->image->comps;
2412
2413
0
    for (compno = 0; compno < l_tile->numcomps; compno++) {
2414
0
        l_current_ptr = l_tile_comp->data;
2415
0
        l_nb_elem = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2416
0
                    (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2417
2418
0
        if (l_tccp->qmfbid == 1) {
2419
0
            for (i = 0; i < l_nb_elem; ++i) {
2420
0
                *l_current_ptr -= l_tccp->m_dc_level_shift ;
2421
0
                ++l_current_ptr;
2422
0
            }
2423
0
        } else {
2424
0
            for (i = 0; i < l_nb_elem; ++i) {
2425
0
                *((OPJ_FLOAT32 *) l_current_ptr) = (OPJ_FLOAT32)(*l_current_ptr -
2426
0
                                                   l_tccp->m_dc_level_shift);
2427
0
                ++l_current_ptr;
2428
0
            }
2429
0
        }
2430
2431
0
        ++l_img_comp;
2432
0
        ++l_tccp;
2433
0
        ++l_tile_comp;
2434
0
    }
2435
2436
0
    return OPJ_TRUE;
2437
0
}
2438
2439
static OPJ_BOOL opj_tcd_mct_encode(opj_tcd_t *p_tcd)
2440
0
{
2441
0
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2442
0
    opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2443
0
    OPJ_SIZE_T samples = (OPJ_SIZE_T)(l_tile_comp->x1 - l_tile_comp->x0) *
2444
0
                         (OPJ_SIZE_T)(l_tile_comp->y1 - l_tile_comp->y0);
2445
0
    OPJ_UINT32 i;
2446
0
    OPJ_BYTE ** l_data = 00;
2447
0
    opj_tcp_t * l_tcp = p_tcd->tcp;
2448
2449
0
    if (!p_tcd->tcp->mct) {
2450
0
        return OPJ_TRUE;
2451
0
    }
2452
2453
0
    if (p_tcd->tcp->mct == 2) {
2454
0
        if (! p_tcd->tcp->m_mct_coding_matrix) {
2455
0
            return OPJ_TRUE;
2456
0
        }
2457
2458
0
        l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps * sizeof(OPJ_BYTE*));
2459
0
        if (! l_data) {
2460
0
            return OPJ_FALSE;
2461
0
        }
2462
2463
0
        for (i = 0; i < l_tile->numcomps; ++i) {
2464
0
            l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
2465
0
            ++l_tile_comp;
2466
0
        }
2467
2468
0
        if (! opj_mct_encode_custom(/* MCT data */
2469
0
                    (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
2470
                    /* size of components */
2471
0
                    samples,
2472
                    /* components */
2473
0
                    l_data,
2474
                    /* nb of components (i.e. size of pData) */
2475
0
                    l_tile->numcomps,
2476
                    /* tells if the data is signed */
2477
0
                    p_tcd->image->comps->sgnd)) {
2478
0
            opj_free(l_data);
2479
0
            return OPJ_FALSE;
2480
0
        }
2481
2482
0
        opj_free(l_data);
2483
0
    } else if (l_tcp->tccps->qmfbid == 0) {
2484
0
        opj_mct_encode_real(
2485
0
            (OPJ_FLOAT32*)l_tile->comps[0].data,
2486
0
            (OPJ_FLOAT32*)l_tile->comps[1].data,
2487
0
            (OPJ_FLOAT32*)l_tile->comps[2].data,
2488
0
            samples);
2489
0
    } else {
2490
0
        opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data,
2491
0
                       l_tile->comps[2].data, samples);
2492
0
    }
2493
2494
0
    return OPJ_TRUE;
2495
0
}
2496
2497
static OPJ_BOOL opj_tcd_dwt_encode(opj_tcd_t *p_tcd)
2498
0
{
2499
0
    opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
2500
0
    opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
2501
0
    opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
2502
0
    OPJ_UINT32 compno;
2503
2504
0
    for (compno = 0; compno < l_tile->numcomps; ++compno) {
2505
0
        if (l_tccp->qmfbid == 1) {
2506
0
            if (! opj_dwt_encode(p_tcd, l_tile_comp)) {
2507
0
                return OPJ_FALSE;
2508
0
            }
2509
0
        } else if (l_tccp->qmfbid == 0) {
2510
0
            if (! opj_dwt_encode_real(p_tcd, l_tile_comp)) {
2511
0
                return OPJ_FALSE;
2512
0
            }
2513
0
        }
2514
2515
0
        ++l_tile_comp;
2516
0
        ++l_tccp;
2517
0
    }
2518
2519
0
    return OPJ_TRUE;
2520
0
}
2521
2522
static OPJ_BOOL opj_tcd_t1_encode(opj_tcd_t *p_tcd)
2523
0
{
2524
0
    const OPJ_FLOAT64 * l_mct_norms;
2525
0
    OPJ_UINT32 l_mct_numcomps = 0U;
2526
0
    opj_tcp_t * l_tcp = p_tcd->tcp;
2527
2528
0
    if (l_tcp->mct == 1) {
2529
0
        l_mct_numcomps = 3U;
2530
        /* irreversible encoding */
2531
0
        if (l_tcp->tccps->qmfbid == 0) {
2532
0
            l_mct_norms = opj_mct_get_mct_norms_real();
2533
0
        } else {
2534
0
            l_mct_norms = opj_mct_get_mct_norms();
2535
0
        }
2536
0
    } else {
2537
0
        l_mct_numcomps = p_tcd->image->numcomps;
2538
0
        l_mct_norms = (const OPJ_FLOAT64 *)(l_tcp->mct_norms);
2539
0
    }
2540
2541
0
    return opj_t1_encode_cblks(p_tcd,
2542
0
                               p_tcd->tcd_image->tiles, l_tcp, l_mct_norms,
2543
0
                               l_mct_numcomps);
2544
2545
0
    return OPJ_TRUE;
2546
0
}
2547
2548
static OPJ_BOOL opj_tcd_t2_encode(opj_tcd_t *p_tcd,
2549
                                  OPJ_BYTE * p_dest_data,
2550
                                  OPJ_UINT32 * p_data_written,
2551
                                  OPJ_UINT32 p_max_dest_size,
2552
                                  opj_codestream_info_t *p_cstr_info,
2553
                                  opj_tcd_marker_info_t* p_marker_info,
2554
                                  opj_event_mgr_t *p_manager)
2555
0
{
2556
0
    opj_t2_t * l_t2;
2557
2558
0
    l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
2559
0
    if (l_t2 == 00) {
2560
0
        return OPJ_FALSE;
2561
0
    }
2562
2563
0
    if (! opj_t2_encode_packets(
2564
0
                l_t2,
2565
0
                p_tcd->tcd_tileno,
2566
0
                p_tcd->tcd_image->tiles,
2567
0
                p_tcd->tcp->numlayers,
2568
0
                p_dest_data,
2569
0
                p_data_written,
2570
0
                p_max_dest_size,
2571
0
                p_cstr_info,
2572
0
                p_marker_info,
2573
0
                p_tcd->tp_num,
2574
0
                p_tcd->tp_pos,
2575
0
                p_tcd->cur_pino,
2576
0
                FINAL_PASS,
2577
0
                p_manager)) {
2578
0
        opj_t2_destroy(l_t2);
2579
0
        return OPJ_FALSE;
2580
0
    }
2581
2582
0
    opj_t2_destroy(l_t2);
2583
2584
    /*---------------CLEAN-------------------*/
2585
0
    return OPJ_TRUE;
2586
0
}
2587
2588
2589
static OPJ_BOOL opj_tcd_rate_allocate_encode(opj_tcd_t *p_tcd,
2590
        OPJ_BYTE * p_dest_data,
2591
        OPJ_UINT32 p_max_dest_size,
2592
        opj_codestream_info_t *p_cstr_info,
2593
        opj_event_mgr_t *p_manager)
2594
0
{
2595
0
    opj_cp_t * l_cp = p_tcd->cp;
2596
0
    OPJ_UINT32 l_nb_written = 0;
2597
2598
0
    if (p_cstr_info)  {
2599
0
        p_cstr_info->index_write = 0;
2600
0
    }
2601
2602
0
    if (l_cp->m_specific_param.m_enc.m_disto_alloc ||
2603
0
            l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2604
        /* fixed_quality */
2605
        /* Normal Rate/distortion allocation */
2606
0
        if (! opj_tcd_rateallocate(p_tcd, p_dest_data, &l_nb_written, p_max_dest_size,
2607
0
                                   p_cstr_info, p_manager)) {
2608
0
            return OPJ_FALSE;
2609
0
        }
2610
0
    } else {
2611
        /* Fixed layer allocation */
2612
0
        opj_tcd_rateallocate_fixed(p_tcd);
2613
0
    }
2614
2615
0
    return OPJ_TRUE;
2616
0
}
2617
2618
2619
OPJ_BOOL opj_tcd_copy_tile_data(opj_tcd_t *p_tcd,
2620
                                OPJ_BYTE * p_src,
2621
                                OPJ_SIZE_T p_src_length)
2622
0
{
2623
0
    OPJ_UINT32 i;
2624
0
    OPJ_SIZE_T j;
2625
0
    OPJ_SIZE_T l_data_size = 0;
2626
0
    opj_image_comp_t * l_img_comp = 00;
2627
0
    opj_tcd_tilecomp_t * l_tilec = 00;
2628
0
    OPJ_UINT32 l_size_comp, l_remaining;
2629
0
    OPJ_SIZE_T l_nb_elem;
2630
2631
0
    l_data_size = opj_tcd_get_encoder_input_buffer_size(p_tcd);
2632
0
    if (l_data_size != p_src_length) {
2633
0
        return OPJ_FALSE;
2634
0
    }
2635
2636
0
    l_tilec = p_tcd->tcd_image->tiles->comps;
2637
0
    l_img_comp = p_tcd->image->comps;
2638
0
    for (i = 0; i < p_tcd->image->numcomps; ++i) {
2639
0
        l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2640
0
        l_remaining = l_img_comp->prec & 7;  /* (%8) */
2641
0
        l_nb_elem = (OPJ_SIZE_T)(l_tilec->x1 - l_tilec->x0) *
2642
0
                    (OPJ_SIZE_T)(l_tilec->y1 - l_tilec->y0);
2643
2644
0
        if (l_remaining) {
2645
0
            ++l_size_comp;
2646
0
        }
2647
2648
0
        if (l_size_comp == 3) {
2649
0
            l_size_comp = 4;
2650
0
        }
2651
2652
0
        switch (l_size_comp) {
2653
0
        case 1: {
2654
0
            OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2655
0
            OPJ_INT32 * l_dest_ptr = l_tilec->data;
2656
2657
0
            if (l_img_comp->sgnd) {
2658
0
                for (j = 0; j < l_nb_elem; ++j) {
2659
0
                    *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2660
0
                }
2661
0
            } else {
2662
0
                for (j = 0; j < l_nb_elem; ++j) {
2663
0
                    *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xff;
2664
0
                }
2665
0
            }
2666
2667
0
            p_src = (OPJ_BYTE*) l_src_ptr;
2668
0
        }
2669
0
        break;
2670
0
        case 2: {
2671
0
            OPJ_INT32 * l_dest_ptr = l_tilec->data;
2672
0
            OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2673
2674
0
            if (l_img_comp->sgnd) {
2675
0
                for (j = 0; j < l_nb_elem; ++j) {
2676
0
                    *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2677
0
                }
2678
0
            } else {
2679
0
                for (j = 0; j < l_nb_elem; ++j) {
2680
0
                    *(l_dest_ptr++) = (*(l_src_ptr++)) & 0xffff;
2681
0
                }
2682
0
            }
2683
2684
0
            p_src = (OPJ_BYTE*) l_src_ptr;
2685
0
        }
2686
0
        break;
2687
0
        case 4: {
2688
0
            OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2689
0
            OPJ_INT32 * l_dest_ptr = l_tilec->data;
2690
2691
0
            for (j = 0; j < l_nb_elem; ++j) {
2692
0
                *(l_dest_ptr++) = (OPJ_INT32)(*(l_src_ptr++));
2693
0
            }
2694
2695
0
            p_src = (OPJ_BYTE*) l_src_ptr;
2696
0
        }
2697
0
        break;
2698
0
        }
2699
2700
0
        ++l_img_comp;
2701
0
        ++l_tilec;
2702
0
    }
2703
2704
0
    return OPJ_TRUE;
2705
0
}
2706
2707
OPJ_BOOL opj_tcd_is_band_empty(opj_tcd_band_t* band)
2708
3.01M
{
2709
3.01M
    return (band->x1 - band->x0 == 0) || (band->y1 - band->y0 == 0);
2710
3.01M
}
2711
2712
OPJ_BOOL opj_tcd_is_subband_area_of_interest(opj_tcd_t *tcd,
2713
        OPJ_UINT32 compno,
2714
        OPJ_UINT32 resno,
2715
        OPJ_UINT32 bandno,
2716
        OPJ_UINT32 band_x0,
2717
        OPJ_UINT32 band_y0,
2718
        OPJ_UINT32 band_x1,
2719
        OPJ_UINT32 band_y1)
2720
22.8M
{
2721
    /* Note: those values for filter_margin are in part the result of */
2722
    /* experimentation. The value 2 for QMFBID=1 (5x3 filter) can be linked */
2723
    /* to the maximum left/right extension given in tables F.2 and F.3 of the */
2724
    /* standard. The value 3 for QMFBID=0 (9x7 filter) is more suspicious, */
2725
    /* since F.2 and F.3 would lead to 4 instead, so the current 3 might be */
2726
    /* needed to be bumped to 4, in case inconsistencies are found while */
2727
    /* decoding parts of irreversible coded images. */
2728
    /* See opj_dwt_decode_partial_53 and opj_dwt_decode_partial_97 as well */
2729
22.8M
    OPJ_UINT32 filter_margin = (tcd->tcp->tccps[compno].qmfbid == 1) ? 2 : 3;
2730
22.8M
    opj_tcd_tilecomp_t *tilec = &(tcd->tcd_image->tiles->comps[compno]);
2731
22.8M
    opj_image_comp_t* image_comp = &(tcd->image->comps[compno]);
2732
    /* Compute the intersection of the area of interest, expressed in tile coordinates */
2733
    /* with the tile coordinates */
2734
22.8M
    OPJ_UINT32 tcx0 = opj_uint_max(
2735
22.8M
                          (OPJ_UINT32)tilec->x0,
2736
22.8M
                          opj_uint_ceildiv(tcd->win_x0, image_comp->dx));
2737
22.8M
    OPJ_UINT32 tcy0 = opj_uint_max(
2738
22.8M
                          (OPJ_UINT32)tilec->y0,
2739
22.8M
                          opj_uint_ceildiv(tcd->win_y0, image_comp->dy));
2740
22.8M
    OPJ_UINT32 tcx1 = opj_uint_min(
2741
22.8M
                          (OPJ_UINT32)tilec->x1,
2742
22.8M
                          opj_uint_ceildiv(tcd->win_x1, image_comp->dx));
2743
22.8M
    OPJ_UINT32 tcy1 = opj_uint_min(
2744
22.8M
                          (OPJ_UINT32)tilec->y1,
2745
22.8M
                          opj_uint_ceildiv(tcd->win_y1, image_comp->dy));
2746
    /* Compute number of decomposition for this band. See table F-1 */
2747
22.8M
    OPJ_UINT32 nb = (resno == 0) ?
2748
22.4M
                    tilec->numresolutions - 1 :
2749
22.8M
                    tilec->numresolutions - resno;
2750
    /* Map above tile-based coordinates to sub-band-based coordinates per */
2751
    /* equation B-15 of the standard */
2752
22.8M
    OPJ_UINT32 x0b = bandno & 1;
2753
22.8M
    OPJ_UINT32 y0b = bandno >> 1;
2754
22.8M
    OPJ_UINT32 tbx0 = (nb == 0) ? tcx0 :
2755
22.8M
                      (tcx0 <= (1U << (nb - 1)) * x0b) ? 0 :
2756
335k
                      opj_uint_ceildivpow2(tcx0 - (1U << (nb - 1)) * x0b, nb);
2757
22.8M
    OPJ_UINT32 tby0 = (nb == 0) ? tcy0 :
2758
22.8M
                      (tcy0 <= (1U << (nb - 1)) * y0b) ? 0 :
2759
335k
                      opj_uint_ceildivpow2(tcy0 - (1U << (nb - 1)) * y0b, nb);
2760
22.8M
    OPJ_UINT32 tbx1 = (nb == 0) ? tcx1 :
2761
22.8M
                      (tcx1 <= (1U << (nb - 1)) * x0b) ? 0 :
2762
335k
                      opj_uint_ceildivpow2(tcx1 - (1U << (nb - 1)) * x0b, nb);
2763
22.8M
    OPJ_UINT32 tby1 = (nb == 0) ? tcy1 :
2764
22.8M
                      (tcy1 <= (1U << (nb - 1)) * y0b) ? 0 :
2765
335k
                      opj_uint_ceildivpow2(tcy1 - (1U << (nb - 1)) * y0b, nb);
2766
22.8M
    OPJ_BOOL intersects;
2767
2768
22.8M
    if (tbx0 < filter_margin) {
2769
16.3M
        tbx0 = 0;
2770
16.3M
    } else {
2771
6.50M
        tbx0 -= filter_margin;
2772
6.50M
    }
2773
22.8M
    if (tby0 < filter_margin) {
2774
12.3M
        tby0 = 0;
2775
12.3M
    } else {
2776
10.4M
        tby0 -= filter_margin;
2777
10.4M
    }
2778
22.8M
    tbx1 = opj_uint_adds(tbx1, filter_margin);
2779
22.8M
    tby1 = opj_uint_adds(tby1, filter_margin);
2780
2781
22.8M
    intersects = band_x0 < tbx1 && band_y0 < tby1 && band_x1 > tbx0 &&
2782
22.8M
                 band_y1 > tby0;
2783
2784
#ifdef DEBUG_VERBOSE
2785
    printf("compno=%u resno=%u nb=%u bandno=%u x0b=%u y0b=%u band=%u,%u,%u,%u tb=%u,%u,%u,%u -> %u\n",
2786
           compno, resno, nb, bandno, x0b, y0b,
2787
           band_x0, band_y0, band_x1, band_y1,
2788
           tbx0, tby0, tbx1, tby1, intersects);
2789
#endif
2790
22.8M
    return intersects;
2791
22.8M
}
2792
2793
/** Returns whether a tile componenent is fully decoded, taking into account
2794
 * p_tcd->win_* members.
2795
 *
2796
 * @param p_tcd    TCD handle.
2797
 * @param compno Component number
2798
 * @return OPJ_TRUE whether the tile componenent is fully decoded
2799
 */
2800
static OPJ_BOOL opj_tcd_is_whole_tilecomp_decoding(opj_tcd_t *p_tcd,
2801
        OPJ_UINT32 compno)
2802
1.51k
{
2803
1.51k
    opj_tcd_tilecomp_t* tilec = &(p_tcd->tcd_image->tiles->comps[compno]);
2804
1.51k
    opj_image_comp_t* image_comp = &(p_tcd->image->comps[compno]);
2805
    /* Compute the intersection of the area of interest, expressed in tile coordinates */
2806
    /* with the tile coordinates */
2807
1.51k
    OPJ_UINT32 tcx0 = opj_uint_max(
2808
1.51k
                          (OPJ_UINT32)tilec->x0,
2809
1.51k
                          opj_uint_ceildiv(p_tcd->win_x0, image_comp->dx));
2810
1.51k
    OPJ_UINT32 tcy0 = opj_uint_max(
2811
1.51k
                          (OPJ_UINT32)tilec->y0,
2812
1.51k
                          opj_uint_ceildiv(p_tcd->win_y0, image_comp->dy));
2813
1.51k
    OPJ_UINT32 tcx1 = opj_uint_min(
2814
1.51k
                          (OPJ_UINT32)tilec->x1,
2815
1.51k
                          opj_uint_ceildiv(p_tcd->win_x1, image_comp->dx));
2816
1.51k
    OPJ_UINT32 tcy1 = opj_uint_min(
2817
1.51k
                          (OPJ_UINT32)tilec->y1,
2818
1.51k
                          opj_uint_ceildiv(p_tcd->win_y1, image_comp->dy));
2819
2820
1.51k
    OPJ_UINT32 shift = tilec->numresolutions - tilec->minimum_num_resolutions;
2821
    /* Tolerate small margin within the reduced resolution factor to consider if */
2822
    /* the whole tile path must be taken */
2823
1.51k
    return (tcx0 >= (OPJ_UINT32)tilec->x0 &&
2824
1.51k
            tcy0 >= (OPJ_UINT32)tilec->y0 &&
2825
1.51k
            tcx1 <= (OPJ_UINT32)tilec->x1 &&
2826
1.51k
            tcy1 <= (OPJ_UINT32)tilec->y1 &&
2827
1.51k
            (shift >= 32 ||
2828
1.51k
             (((tcx0 - (OPJ_UINT32)tilec->x0) >> shift) == 0 &&
2829
1.51k
              ((tcy0 - (OPJ_UINT32)tilec->y0) >> shift) == 0 &&
2830
1.51k
              (((OPJ_UINT32)tilec->x1 - tcx1) >> shift) == 0 &&
2831
1.51k
              (((OPJ_UINT32)tilec->y1 - tcy1) >> shift) == 0)));
2832
1.51k
}
2833
2834
/* ----------------------------------------------------------------------- */
2835
2836
opj_tcd_marker_info_t* opj_tcd_marker_info_create(OPJ_BOOL need_PLT)
2837
0
{
2838
0
    opj_tcd_marker_info_t *l_tcd_marker_info =
2839
0
        (opj_tcd_marker_info_t*) opj_calloc(1, sizeof(opj_tcd_marker_info_t));
2840
0
    if (!l_tcd_marker_info) {
2841
0
        return NULL;
2842
0
    }
2843
2844
0
    l_tcd_marker_info->need_PLT = need_PLT;
2845
2846
0
    return l_tcd_marker_info;
2847
0
}
2848
2849
/* ----------------------------------------------------------------------- */
2850
2851
void opj_tcd_marker_info_destroy(opj_tcd_marker_info_t *p_tcd_marker_info)
2852
0
{
2853
0
    if (p_tcd_marker_info) {
2854
0
        opj_free(p_tcd_marker_info->p_packet_size);
2855
0
        opj_free(p_tcd_marker_info);
2856
0
    }
2857
0
}
2858
2859
/* ----------------------------------------------------------------------- */