Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/LibOpenJPEG/tcd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3
 * Copyright (c) 2002-2007, Professor Benoit Macq
4
 * Copyright (c) 2001-2003, David Janssens
5
 * Copyright (c) 2002-2003, Yannick Verschueren
6
 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8
 * Copyright (c) 2006-2007, Parvatha Elangovan
9
 * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France 
10
 * Copyright (c) 2012, CS Systemes d'Information, France
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions
15
 * are met:
16
 * 1. Redistributions of source code must retain the above copyright
17
 *    notice, this list of conditions and the following disclaimer.
18
 * 2. Redistributions in binary form must reproduce the above copyright
19
 *    notice, this list of conditions and the following disclaimer in the
20
 *    documentation and/or other materials provided with the distribution.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
23
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
#include "opj_includes.h"
36
37
/* ----------------------------------------------------------------------- */
38
39
/* TODO MSD: */
40
#ifdef TODO_MSD 
41
void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
42
        int tileno, compno, resno, bandno, precno;/*, cblkno;*/
43
44
        fprintf(fd, "image {\n");
45
        fprintf(fd, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", 
46
                img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
47
48
        for (tileno = 0; tileno < img->th * img->tw; tileno++) {
49
                opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
50
                fprintf(fd, "  tile {\n");
51
                fprintf(fd, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
52
                        tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
53
                for (compno = 0; compno < tile->numcomps; compno++) {
54
                        opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
55
                        fprintf(fd, "    tilec {\n");
56
                        fprintf(fd,
57
                                "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
58
                                tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
59
                        for (resno = 0; resno < tilec->numresolutions; resno++) {
60
                                opj_tcd_resolution_t *res = &tilec->resolutions[resno];
61
                                fprintf(fd, "\n   res {\n");
62
                                fprintf(fd,
63
                                        "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
64
                                        res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
65
                                for (bandno = 0; bandno < res->numbands; bandno++) {
66
                                        opj_tcd_band_t *band = &res->bands[bandno];
67
                                        fprintf(fd, "        band {\n");
68
                                        fprintf(fd,
69
                                                "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
70
                                                band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
71
                                        for (precno = 0; precno < res->pw * res->ph; precno++) {
72
                                                opj_tcd_precinct_t *prec = &band->precincts[precno];
73
                                                fprintf(fd, "          prec {\n");
74
                                                fprintf(fd,
75
                                                        "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
76
                                                        prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
77
                                                /*
78
                                                for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
79
                                                        opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
80
                                                        fprintf(fd, "            cblk {\n");
81
                                                        fprintf(fd,
82
                                                                "              x0=%d, y0=%d, x1=%d, y1=%d\n",
83
                                                                cblk->x0, cblk->y0, cblk->x1, cblk->y1);
84
                                                        fprintf(fd, "            }\n");
85
                                                }
86
                                                */
87
                                                fprintf(fd, "          }\n");
88
                                        }
89
                                        fprintf(fd, "        }\n");
90
                                }
91
                                fprintf(fd, "      }\n");
92
                        }
93
                        fprintf(fd, "    }\n");
94
                }
95
                fprintf(fd, "  }\n");
96
        }
97
        fprintf(fd, "}\n");
98
}
99
#endif
100
/**
101
* Allocates memory for a decoding code block.
102
*/
103
static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
104
105
/**
106
 * Deallocates the decoding data of the given precinct.
107
 */
108
static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
109
110
/**
111
 * Allocates memory for an encoding code block.
112
 */
113
static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
114
115
/**
116
 * Deallocates the encoding data of the given precinct.
117
 */
118
static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
119
120
121
/**
122
Free the memory allocated for encoding
123
@param tcd TCD handle
124
*/
125
static void opj_tcd_free_tile(opj_tcd_t *tcd);
126
127
128
static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
129
                                    OPJ_BYTE * p_src_data,
130
                                    OPJ_UINT32 * p_data_read,
131
                                    OPJ_UINT32 p_max_src_size,
132
                                    opj_codestream_index_t *p_cstr_index );
133
134
static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
135
136
static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
137
138
static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd);
139
140
static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
141
142
143
static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
144
145
static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
146
147
static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
148
149
static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
150
151
static OPJ_BOOL opj_tcd_t2_encode (     opj_tcd_t *p_tcd,
152
                                                                    OPJ_BYTE * p_dest_data,
153
                                                                    OPJ_UINT32 * p_data_written,
154
                                                                    OPJ_UINT32 p_max_dest_size,
155
                                                                    opj_codestream_info_t *p_cstr_info );
156
157
static OPJ_BOOL opj_tcd_rate_allocate_encode(   opj_tcd_t *p_tcd,
158
                                                                                        OPJ_BYTE * p_dest_data,
159
                                                                                        OPJ_UINT32 p_max_dest_size,
160
                                                                                        opj_codestream_info_t *p_cstr_info );
161
162
/* ----------------------------------------------------------------------- */
163
164
/**
165
Create a new TCD handle
166
*/
167
opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
168
0
{
169
0
        opj_tcd_t *l_tcd = 00;
170
171
        /* create the tcd structure */
172
0
        l_tcd = (opj_tcd_t*) opj_malloc(sizeof(opj_tcd_t));
173
0
        if (!l_tcd) {
174
0
                return 00;
175
0
        }
176
0
        memset(l_tcd,0,sizeof(opj_tcd_t));
177
178
0
        l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
179
180
0
        l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
181
0
        if (!l_tcd->tcd_image) {
182
0
                opj_free(l_tcd);
183
0
                return 00;
184
0
        }
185
0
        memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
186
187
0
        return l_tcd;
188
0
}
189
190
191
/* ----------------------------------------------------------------------- */
192
193
0
void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
194
0
        OPJ_UINT32 layno;
195
196
0
        for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
197
0
                opj_tcd_makelayer_fixed(tcd, layno, 1);
198
0
        }
199
0
}
200
201
202
void opj_tcd_makelayer( opj_tcd_t *tcd,
203
                                                OPJ_UINT32 layno,
204
                                                OPJ_FLOAT64 thresh,
205
                                                OPJ_UINT32 final)
206
0
{
207
0
        OPJ_UINT32 compno, resno, bandno, precno, cblkno;
208
0
        OPJ_UINT32 passno;
209
210
0
        opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
211
212
0
        tcd_tile->distolayer[layno] = 0;        /* fixed_quality */
213
214
0
        for (compno = 0; compno < tcd_tile->numcomps; compno++) {
215
0
                opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
216
217
0
                for (resno = 0; resno < tilec->numresolutions; resno++) {
218
0
                        opj_tcd_resolution_t *res = &tilec->resolutions[resno];
219
220
0
                        for (bandno = 0; bandno < res->numbands; bandno++) {
221
0
                                opj_tcd_band_t *band = &res->bands[bandno];
222
223
0
                                for (precno = 0; precno < res->pw * res->ph; precno++) {
224
0
                                        opj_tcd_precinct_t *prc = &band->precincts[precno];
225
226
0
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
227
0
                                                opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
228
0
                                                opj_tcd_layer_t *layer = &cblk->layers[layno];
229
0
                                                OPJ_UINT32 n;
230
231
0
                                                if (layno == 0) {
232
0
                                                        cblk->numpassesinlayers = 0;
233
0
                                                }
234
235
0
                                                n = cblk->numpassesinlayers;
236
237
0
                                                for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
238
0
                                                        OPJ_UINT32 dr;
239
0
                                                        OPJ_FLOAT64 dd;
240
0
                                                        opj_tcd_pass_t *pass = &cblk->passes[passno];
241
242
0
                                                        if (n == 0) {
243
0
                                                                dr = pass->rate;
244
0
                                                                dd = pass->distortiondec;
245
0
                                                        } else {
246
0
                                                                dr = pass->rate - cblk->passes[n - 1].rate;
247
0
                                                                dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
248
0
                                                        }
249
250
0
                                                        if (!dr) {
251
0
                                                                if (dd != 0)
252
0
                                                                        n = passno + 1;
253
0
                                                                continue;
254
0
                                                        }
255
0
                                                        if (dd / dr >= thresh)
256
0
                                                                n = passno + 1;
257
0
                                                }
258
259
0
                                                layer->numpasses = n - cblk->numpassesinlayers;
260
261
0
                                                if (!layer->numpasses) {
262
0
                                                        layer->disto = 0;
263
0
                                                        continue;
264
0
                                                }
265
266
0
                                                if (cblk->numpassesinlayers == 0) {
267
0
                                                        layer->len = cblk->passes[n - 1].rate;
268
0
                                                        layer->data = cblk->data;
269
0
                                                        layer->disto = cblk->passes[n - 1].distortiondec;
270
0
                                                } else {
271
0
                                                        layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
272
0
                                                        layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
273
0
                                                        layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
274
0
                                                }
275
276
0
                                                tcd_tile->distolayer[layno] += layer->disto;    /* fixed_quality */
277
278
0
                                                if (final)
279
0
                                                        cblk->numpassesinlayers = n;
280
0
                                        }
281
0
                                }
282
0
                        }
283
0
                }
284
0
        }
285
0
}
286
287
0
void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
288
0
        OPJ_UINT32 compno, resno, bandno, precno, cblkno;
289
0
        OPJ_INT32 value;                        /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
290
0
        OPJ_INT32 matrice[10][10][3];
291
0
        OPJ_UINT32 i, j, k;
292
293
0
        opj_cp_t *cp = tcd->cp;
294
0
        opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
295
0
        opj_tcp_t *tcd_tcp = tcd->tcp;
296
297
0
        for (compno = 0; compno < tcd_tile->numcomps; compno++) {
298
0
                opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
299
300
0
                for (i = 0; i < tcd_tcp->numlayers; i++) {
301
0
                        for (j = 0; j < tilec->numresolutions; j++) {
302
0
                                for (k = 0; k < 3; k++) {
303
0
                                        matrice[i][j][k] =
304
0
                                                (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
305
0
                                                * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
306
0
                                }
307
0
                        }
308
0
                }
309
310
0
                for (resno = 0; resno < tilec->numresolutions; resno++) {
311
0
                        opj_tcd_resolution_t *res = &tilec->resolutions[resno];
312
313
0
                        for (bandno = 0; bandno < res->numbands; bandno++) {
314
0
                                opj_tcd_band_t *band = &res->bands[bandno];
315
316
0
                                for (precno = 0; precno < res->pw * res->ph; precno++) {
317
0
                                        opj_tcd_precinct_t *prc = &band->precincts[precno];
318
319
0
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
320
0
                                                opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
321
0
                                                opj_tcd_layer_t *layer = &cblk->layers[layno];
322
0
                                                OPJ_UINT32 n;
323
0
                                                OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
324
325
                                                /* Correction of the matrix of coefficient to include the IMSB information */
326
0
                                                if (layno == 0) {
327
0
                                                        value = matrice[layno][resno][bandno];
328
0
                                                        if (imsb >= value) {
329
0
                                                                value = 0;
330
0
                                                        } else {
331
0
                                                                value -= imsb;
332
0
                                                        }
333
0
                                                } else {
334
0
                                                        value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
335
0
                                                        if (imsb >= matrice[layno - 1][resno][bandno]) {
336
0
                                                                value -= (imsb - matrice[layno - 1][resno][bandno]);
337
0
                                                                if (value < 0) {
338
0
                                                                        value = 0;
339
0
                                                                }
340
0
                                                        }
341
0
                                                }
342
343
0
                                                if (layno == 0) {
344
0
                                                        cblk->numpassesinlayers = 0;
345
0
                                                }
346
347
0
                                                n = cblk->numpassesinlayers;
348
0
                                                if (cblk->numpassesinlayers == 0) {
349
0
                                                        if (value != 0) {
350
0
                                                                n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
351
0
                                                        } else {
352
0
                                                                n = cblk->numpassesinlayers;
353
0
                                                        }
354
0
                                                } else {
355
0
                                                        n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
356
0
                                                }
357
358
0
                                                layer->numpasses = n - cblk->numpassesinlayers;
359
360
0
                                                if (!layer->numpasses)
361
0
                                                        continue;
362
363
0
                                                if (cblk->numpassesinlayers == 0) {
364
0
                                                        layer->len = cblk->passes[n - 1].rate;
365
0
                                                        layer->data = cblk->data;
366
0
                                                } else {
367
0
                                                        layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
368
0
                                                        layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
369
0
                                                }
370
371
0
                                                if (final)
372
0
                                                        cblk->numpassesinlayers = n;
373
0
                                        }
374
0
                                }
375
0
                        }
376
0
                }
377
0
        }
378
0
}
379
380
OPJ_BOOL opj_tcd_rateallocate(  opj_tcd_t *tcd,
381
                                                                OPJ_BYTE *dest,
382
                                                                OPJ_UINT32 * p_data_written,
383
                                                                OPJ_UINT32 len,
384
                                                                opj_codestream_info_t *cstr_info)
385
0
{
386
0
        OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
387
0
        OPJ_UINT32 passno;
388
0
        OPJ_FLOAT64 min, max;
389
0
        OPJ_FLOAT64 cumdisto[100];      /* fixed_quality */
390
0
        const OPJ_FLOAT64 K = 1;                /* 1.1; fixed_quality */
391
0
        OPJ_FLOAT64 maxSE = 0;
392
393
0
        opj_cp_t *cp = tcd->cp;
394
0
        opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
395
0
        opj_tcp_t *tcd_tcp = tcd->tcp;
396
397
0
        min = DBL_MAX;
398
0
        max = 0;
399
400
0
        tcd_tile->numpix = 0;           /* fixed_quality */
401
402
0
        for (compno = 0; compno < tcd_tile->numcomps; compno++) {
403
0
                opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
404
0
                tilec->numpix = 0;
405
406
0
                for (resno = 0; resno < tilec->numresolutions; resno++) {
407
0
                        opj_tcd_resolution_t *res = &tilec->resolutions[resno];
408
409
0
                        for (bandno = 0; bandno < res->numbands; bandno++) {
410
0
                                opj_tcd_band_t *band = &res->bands[bandno];
411
412
0
                                for (precno = 0; precno < res->pw * res->ph; precno++) {
413
0
                                        opj_tcd_precinct_t *prc = &band->precincts[precno];
414
415
0
                                        for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
416
0
                                                opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
417
418
0
                                                for (passno = 0; passno < cblk->totalpasses; passno++) {
419
0
                                                        opj_tcd_pass_t *pass = &cblk->passes[passno];
420
0
                                                        OPJ_INT32 dr;
421
0
                                                        OPJ_FLOAT64 dd, rdslope;
422
423
0
                                                        if (passno == 0) {
424
0
                                                                dr = (OPJ_INT32)pass->rate;
425
0
                                                                dd = pass->distortiondec;
426
0
                                                        } else {
427
0
                                                                dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
428
0
                                                                dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
429
0
                                                        }
430
431
0
                                                        if (dr == 0) {
432
0
                                                                continue;
433
0
                                                        }
434
435
0
                                                        rdslope = dd / dr;
436
0
                                                        if (rdslope < min) {
437
0
                                                                min = rdslope;
438
0
                                                        }
439
440
0
                                                        if (rdslope > max) {
441
0
                                                                max = rdslope;
442
0
                                                        }
443
0
                                                } /* passno */
444
445
                                                /* fixed_quality */
446
0
                                                tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
447
0
                                                tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
448
0
                                        } /* cbklno */
449
0
                                } /* precno */
450
0
                        } /* bandno */
451
0
                } /* resno */
452
453
0
                maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
454
0
                        * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
455
0
                        * ((OPJ_FLOAT64)(tilec->numpix));
456
0
        } /* compno */
457
458
        /* index file */
459
0
        if(cstr_info) {
460
0
                opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
461
0
                tile_info->numpix = tcd_tile->numpix;
462
0
                tile_info->distotile = tcd_tile->distotile;
463
0
                tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
464
0
        }
465
466
0
        for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
467
0
                OPJ_FLOAT64 lo = min;
468
0
                OPJ_FLOAT64 hi = max;
469
0
                OPJ_BOOL success = OPJ_FALSE;
470
0
                OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
471
0
                OPJ_FLOAT64 goodthresh = 0;
472
0
                OPJ_FLOAT64 stable_thresh = 0;
473
0
                OPJ_UINT32 i;
474
0
                OPJ_FLOAT64 distotarget;                /* fixed_quality */
475
476
                /* fixed_quality */
477
0
                distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
478
479
                /* Don't try to find an optimal threshold but rather take everything not included yet, if
480
                  -r xx,yy,zz,0   (disto_alloc == 1 and rates == 0)
481
                  -q xx,yy,zz,0   (fixed_quality == 1 and distoratio == 0)
482
                  ==> possible to have some lossy layers and the last layer for sure lossless */
483
0
                if ( ((cp->m_specific_param.m_enc.m_disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->m_specific_param.m_enc.m_fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
484
0
                        opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
485
0
                        OPJ_FLOAT64 thresh = 0;
486
487
0
                        if (t2 == 00) {
488
0
                                return OPJ_FALSE;
489
0
                        }
490
491
0
                        for     (i = 0; i < 128; ++i) {
492
0
                                OPJ_FLOAT64 distoachieved = 0;  /* fixed_quality */
493
494
0
                                thresh = (lo + hi) / 2;
495
496
0
                                opj_tcd_makelayer(tcd, layno, thresh, 0);
497
498
0
                                if (cp->m_specific_param.m_enc.m_fixed_quality) {       /* fixed_quality */
499
0
                                        if(cp->m_specific_param.m_enc.m_cinema){
500
0
                                                if (! opj_t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC)) {
501
502
0
                                                        lo = thresh;
503
0
                                                        continue;
504
0
                                                }
505
0
                                                else {
506
0
                                                        distoachieved = layno == 0 ?
507
0
                                                                        tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
508
509
0
                                                        if (distoachieved < distotarget) {
510
0
                                                                hi=thresh;
511
0
                                                                stable_thresh = thresh;
512
0
                                                                continue;
513
0
                                                        }else{
514
0
                                                                lo=thresh;
515
0
                                                        }
516
0
                                                }
517
0
                                        }else{
518
0
                                                distoachieved = (layno == 0) ?
519
0
                                                                tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
520
521
0
                                                if (distoachieved < distotarget) {
522
0
                                                        hi = thresh;
523
0
                                                        stable_thresh = thresh;
524
0
                                                        continue;
525
0
                                                }
526
0
                                                lo = thresh;
527
0
                                        }
528
0
                                } else {
529
0
                                        if (! opj_t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest,p_data_written, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC))
530
0
                                        {
531
                                                /* TODO: what to do with l ??? seek / tell ??? */
532
                                                /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
533
0
                                                lo = thresh;
534
0
                                                continue;
535
0
                                        }
536
537
0
                                        hi = thresh;
538
0
                                        stable_thresh = thresh;
539
0
                                }
540
0
                        }
541
542
0
                        success = OPJ_TRUE;
543
0
                        goodthresh = stable_thresh == 0? thresh : stable_thresh;
544
545
0
                        opj_t2_destroy(t2);
546
0
                } else {
547
0
                        success = OPJ_TRUE;
548
0
                        goodthresh = min;
549
0
                }
550
551
0
                if (!success) {
552
0
                        return OPJ_FALSE;
553
0
                }
554
555
0
                if(cstr_info) { /* Threshold for Marcela Index */
556
0
                        cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
557
0
                }
558
559
0
                opj_tcd_makelayer(tcd, layno, goodthresh, 1);
560
561
                /* fixed_quality */
562
0
                cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
563
0
        }
564
565
0
        return OPJ_TRUE;
566
0
}
567
568
OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
569
                                           opj_image_t * p_image,
570
                                           opj_cp_t * p_cp )
571
0
{
572
0
        OPJ_UINT32 l_tile_comp_size;
573
574
0
        p_tcd->image = p_image;
575
0
        p_tcd->cp = p_cp;
576
577
0
        p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
578
0
        if (! p_tcd->tcd_image->tiles) {
579
0
                return OPJ_FALSE;
580
0
        }
581
0
        memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
582
583
0
        l_tile_comp_size = p_image->numcomps * (OPJ_UINT32)sizeof(opj_tcd_tilecomp_t);
584
0
        p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size);
585
0
        if (! p_tcd->tcd_image->tiles->comps ) {
586
0
                return OPJ_FALSE;
587
0
        }
588
0
        memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
589
590
0
        p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
591
0
        p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
592
593
0
        return OPJ_TRUE;
594
0
}
595
596
/**
597
Destroy a previously created TCD handle
598
*/
599
0
void opj_tcd_destroy(opj_tcd_t *tcd) {
600
0
        if (tcd) {
601
0
                opj_tcd_free_tile(tcd);
602
603
0
                if (tcd->tcd_image) {
604
0
                        opj_free(tcd->tcd_image);
605
0
                        tcd->tcd_image = 00;
606
0
                }
607
0
                opj_free(tcd);
608
0
        }
609
0
}
610
611
/* ----------------------------------------------------------------------- */
612
#define OPJ_MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT)                                                                                                                                       \
613
OPJ_BOOL FUNCTION (     opj_tcd_t *p_tcd,                        \
614
                        OPJ_UINT32 p_tile_no                        \
615
0
                        )                                           \
616
0
{                                                                   \
617
0
        OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00;                  \
618
0
        OPJ_UINT32 compno, resno, bandno, precno, cblkno;           \
619
0
        opj_tcp_t * l_tcp = 00;                                  \
620
0
        opj_cp_t * l_cp = 00;                                    \
621
0
        opj_tcd_tile_t * l_tile = 00;                            \
622
0
        opj_tccp_t *l_tccp = 00;                                    \
623
0
        opj_tcd_tilecomp_t *l_tilec = 00;                        \
624
0
        opj_image_comp_t * l_image_comp = 00;                       \
625
0
        opj_tcd_resolution_t *l_res = 00;                        \
626
0
        opj_tcd_band_t *l_band = 00;                             \
627
0
        opj_stepsize_t * l_step_size = 00;                          \
628
0
        opj_tcd_precinct_t *l_current_precinct = 00;             \
629
0
        TYPE* l_code_block = 00;                                    \
630
0
        opj_image_t *l_image = 00;                                  \
631
0
        OPJ_UINT32 p,q;                                             \
632
0
        OPJ_UINT32 l_level_no;                                      \
633
0
        OPJ_UINT32 l_pdx, l_pdy;                                    \
634
0
        OPJ_UINT32 l_gain;                                          \
635
0
        OPJ_INT32 l_x0b, l_y0b;                                     \
636
0
        /* extent of precincts , top left, bottom right**/          \
637
0
        OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end;                                                                                                                             \
638
0
        /* number of precinct for a resolution */                   \
639
0
        OPJ_UINT32 l_nb_precincts;                                  \
640
0
        /* room needed to store l_nb_precinct precinct for a resolution */                                                                                                                                        \
641
0
        OPJ_UINT32 l_nb_precinct_size;                              \
642
0
        /* number of code blocks for a precinct*/                   \
643
0
        OPJ_UINT32 l_nb_code_blocks;                                \
644
0
        /* room needed to store l_nb_code_blocks code blocks for a precinct*/                                                                                                                                     \
645
0
        OPJ_UINT32 l_nb_code_blocks_size;                           \
646
0
        /* size of data for a tile */                               \
647
0
        OPJ_UINT32 l_data_size;                                     \
648
0
                                                                    \
649
0
        l_cp = p_tcd->cp;                                           \
650
0
        l_tcp = &(l_cp->tcps[p_tile_no]);                           \
651
0
        l_tile = p_tcd->tcd_image->tiles;                           \
652
0
        l_tccp = l_tcp->tccps;                                      \
653
0
        l_tilec = l_tile->comps;                                    \
654
0
        l_image = p_tcd->image;                                     \
655
0
        l_image_comp = p_tcd->image->comps;                         \
656
0
                                                                    \
657
0
        p = p_tile_no % l_cp->tw;       /* tile coordinates */      \
658
0
        q = p_tile_no / l_cp->tw;                                   \
659
0
        /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/     \
660
0
                                                                    \
661
0
        /* 4 borders of the tile rescale on the image if necessary */                                                                                                                                             \
662
0
        l_tile->x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + p * l_cp->tdx), (OPJ_INT32)l_image->x0);                                                                                                                                             \
663
0
        l_tile->y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + q * l_cp->tdy), (OPJ_INT32)l_image->y0);                                                                                                                                             \
664
0
        l_tile->x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (p + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);                                                                                                                                       \
665
0
        l_tile->y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (q + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);                                                                                                                                       \
666
0
        /* testcase 1888.pdf.asan.35.988 */ \
667
0
        if (l_tccp->numresolutions == 0) { \
668
0
            fprintf(stderr, "tiles require at least one resolution\n"); \
669
0
            return OPJ_FALSE; \
670
0
        } \
671
0
        /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/                                                                                                         \
672
0
                                                                    \
673
0
        /*tile->numcomps = image->numcomps; */                      \
674
0
        for(compno = 0; compno < l_tile->numcomps; ++compno) {      \
675
0
                /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/                                                                                                                                \
676
0
                                                                    \
677
0
                /* border of each l_tile component (global) */      \
678
0
                l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx);                                                                                                                                          \
679
0
                l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy);                                                                                                                                          \
680
0
                l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx);                                                                                                                                          \
681
0
                l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy);                                                                                                                                          \
682
0
                /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/                                                                                     \
683
0
                                                                    \
684
0
                l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0)           \
685
0
                * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0) * (OPJ_UINT32)sizeof(OPJ_UINT32 );\
686
0
                l_tilec->numresolutions = l_tccp->numresolutions;   \
687
0
                if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) {                                                                                                                             \
688
0
                        l_tilec->minimum_num_resolutions = 1;       \
689
0
                }                                                   \
690
0
                else {                                              \
691
0
                        l_tilec->minimum_num_resolutions = l_tccp->numresolutions                                                                                                                                 \
692
0
                        - l_cp->m_specific_param.m_dec.m_reduce;    \
693
0
                }                                                   \
694
0
                                                                    \
695
0
                if (l_tilec->data == 00) {                          \
696
0
                        l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size);                                                                                                                                    \
697
0
                        if (! l_tilec->data ) {                     \
698
0
                                return OPJ_FALSE;                   \
699
0
                        }                                           \
700
0
                        /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/                                                                                                     \
701
0
                                                                    \
702
0
                        l_tilec->data_size = l_data_size;           \
703
0
                }                                                   \
704
0
                else if (l_data_size > l_tilec->data_size) {        \
705
0
                        OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size);                                                                                                             \
706
0
                        /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n");                                                                                                 */  \
707
0
                        fprintf(stderr, "Not enough memory to handle tile data\n");                                                                                                                               \
708
0
                        if (! new_data) {                           \
709
0
                                opj_free(l_tilec->data);            \
710
0
                                l_tilec->data = NULL;               \
711
0
                                l_tilec->data_size = 0;             \
712
0
                                return OPJ_FALSE;                   \
713
0
                        }                                           \
714
0
                        l_tilec->data = new_data;                   \
715
0
                        /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/                                                                   \
716
0
                        l_tilec->data_size = l_data_size;           \
717
0
                }                                                   \
718
0
                                                                    \
719
0
                l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t);                                                                                                                          \
720
0
                                                                    \
721
0
                if (l_tilec->resolutions == 00) {                   \
722
0
                        l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size);                                                                                                               \
723
0
                        if (! l_tilec->resolutions ) {              \
724
0
                                return OPJ_FALSE;                   \
725
0
                        }                                           \
726
0
                        /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/                                                                                       \
727
0
                        l_tilec->resolutions_size = l_data_size;    \
728
0
                        memset(l_tilec->resolutions,0,l_data_size); \
729
0
                }                                                   \
730
0
                else if (l_data_size > l_tilec->resolutions_size) { \
731
0
                        opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size);                                                                    \
732
0
                        if (! new_resolutions) {                    \
733
0
                                /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n");                                                                                         */  \
734
0
                                fprintf(stderr, "Not enough memory to tile resolutions\n");                                                                                                                       \
735
0
                                opj_free(l_tilec->resolutions);     \
736
0
                                l_tilec->resolutions = NULL;        \
737
0
                                l_tilec->resolutions_size = 0;      \
738
0
                                return OPJ_FALSE;                   \
739
0
                        }                                           \
740
0
                        l_tilec->resolutions = new_resolutions;     \
741
0
                        /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/                                                            \
742
0
                        memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size);                                                                           \
743
0
                        l_tilec->resolutions_size = l_data_size;    \
744
0
                }                                                   \
745
0
                                                                    \
746
0
                l_level_no = l_tilec->numresolutions - 1;           \
747
0
                l_res = l_tilec->resolutions;                       \
748
0
                l_step_size = l_tccp->stepsizes;                    \
749
0
                if (l_tccp->qmfbid == 0) {                          \
750
0
                        l_gain_ptr = &opj_dwt_getgain_real;         \
751
0
                }                                                   \
752
0
                else {                                              \
753
0
                        l_gain_ptr  = &opj_dwt_getgain;             \
754
0
                }                                                   \
755
0
                /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/  \
756
0
                                                                                                                                                                                                                  \
757
0
                for(resno = 0; resno < l_tilec->numresolutions; ++resno) {                                                                                                                                        \
758
0
                        /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/                                                                                                               \
759
0
                        OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/;                                                                                                                                 \
760
0
                        OPJ_UINT32 cbgwidthexpn, cbgheightexpn;                                                                                                                                                   \
761
0
                        OPJ_UINT32 cblkwidthexpn, cblkheightexpn;                                                                                                                                                 \
762
0
                                                                                                                                                                                                                  \
763
0
                        /* border for each resolution level (global) */                                                                                                                                           \
764
0
                        l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);                                                                                                                                     \
765
0
                        l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);                                                                                                                                     \
766
0
                        l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);                                                                                                                                     \
767
0
                        l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);                                                                                                                                     \
768
0
                        /*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);*/                                                                  \
769
0
                        /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */                                                                                                                     \
770
0
                        l_pdx = l_tccp->prcw[resno];                                                                                                                                                              \
771
0
                        l_pdy = l_tccp->prch[resno];                                                                                                                                                              \
772
0
                        /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/                                                                                                                              \
773
0
                        /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */                                                                                                                            \
774
0
                        l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx;                                                                                                                           \
775
0
                        l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy;                                                                                                                           \
776
0
                        l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx;                                                                                                                              \
777
0
                        l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy;                                                                                                                              \
778
0
                        /*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 );*/                    \
779
0
                                                                                                                                                                                                                  \
780
0
                        l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx);                                                                                                \
781
0
                        l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy);                                                                                                \
782
0
                        /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/                                                                                                               \
783
0
                                                                                                                                                                                                                  \
784
0
                        l_nb_precincts = l_res->pw * l_res->ph;                                                                                                                                                   \
785
0
                        l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t);                                                                                                                      \
786
0
                        if (resno == 0) {                                                                                                                                                                         \
787
0
                                tlcbgxstart = l_tl_prc_x_start;                                                                                                                                                   \
788
0
                                tlcbgystart = l_tl_prc_y_start;                                                                                                                                                   \
789
0
                                /*brcbgxend = l_br_prc_x_end;*/                                                                                                                                                       \
790
0
                               /* brcbgyend = l_br_prc_y_end;*/                                                                                                                                                       \
791
0
                                cbgwidthexpn = l_pdx;                                                                                                                                                             \
792
0
                                cbgheightexpn = l_pdy;                                                                                                                                                            \
793
0
                                l_res->numbands = 1;                                                                                                                                                              \
794
0
                        }                                                                                                                                                                                         \
795
0
                        else {                                                                                                                                                                                    \
796
0
                                tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1);                                                                                                                               \
797
0
                                tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1);                                                                                                                               \
798
0
                                /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/                                                                                                                            \
799
0
                                /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/                                                                                                                            \
800
0
                                cbgwidthexpn = l_pdx - 1;                                                                                                                                                         \
801
0
                                cbgheightexpn = l_pdy - 1;                                                                                                                                                        \
802
0
                                l_res->numbands = 3;                                                                                                                                                              \
803
0
                        }                                                                                                                                                                                         \
804
0
                                                                                                                                                                                                                  \
805
0
                        cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn);                                                                                                                                    \
806
0
                        cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn);                                                                                                                                  \
807
0
                        l_band = l_res->bands;                                                                                                                                                                    \
808
0
                                                                                                                                                                                                                  \
809
0
                        for (bandno = 0; bandno < l_res->numbands; ++bandno) {                                                                                                                                    \
810
0
                                OPJ_INT32 numbps;                                                                                                                                                                 \
811
0
                                /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/                                                                                                           \
812
0
                                                                                                                                                                                                                  \
813
0
                                if (resno == 0) {                                                                                                                                                                 \
814
0
                                        l_band->bandno = 0 ;                                                                                                                                                      \
815
0
                                        l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no);                                                                                                                    \
816
0
                                        l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no);                                                                                                                    \
817
0
                                        l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no);                                                                                                                    \
818
0
                                        l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no);                                                                                                                    \
819
0
                                }                                                                                                                                                                                 \
820
0
                                else {                                                                                                                                                                            \
821
0
                                        l_band->bandno = bandno + 1;                                                                                                                                              \
822
0
                                        /* x0b = 1 if bandno = 1 or 3 */                                                                                                                                          \
823
0
                                        l_x0b = l_band->bandno&1;                                                                                                                                                 \
824
0
                                        /* y0b = 1 if bandno = 2 or 3 */                                                                                                                                          \
825
0
                                        l_y0b = (OPJ_INT32)((l_band->bandno)>>1);                                                                                                                                              \
826
0
                                        /* l_band border (global) */                                                                                                                                              \
827
0
                                        l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
828
0
                                        l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
829
0
                                        l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
830
0
                                        l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1));                                                                                    \
831
0
                                }                                                                                                                                                                                 \
832
0
                                                                                                                                                                                                                  \
833
0
                                /** avoid an if with storing function pointer */                                                                                                                                  \
834
0
                                l_gain = (*l_gain_ptr) (l_band->bandno);                                                                                                                                          \
835
0
                                numbps = (OPJ_INT32)(l_image_comp->prec + l_gain);                                                                                                                                             \
836
0
                                l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION;                                           \
837
0
                                l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1;      /* WHY -1 ? */                                                                                                    \
838
0
                                                                                                                                                                                                                  \
839
0
                                if (! l_band->precincts) {                                                                                                                                                        \
840
0
                                        l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size);                                                                                   \
841
0
                                        if (! l_band->precincts) {                                                                                                                                                \
842
0
                                                return OPJ_FALSE;                                                                                                                                                 \
843
0
                                        }                                                                                                                                                                         \
844
0
                                        /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size);     */                                                        \
845
0
                                        memset(l_band->precincts,0,l_nb_precinct_size);                                                                                                                           \
846
0
                                        l_band->precincts_data_size = l_nb_precinct_size;                                                                                                                         \
847
0
                                }                                                                                                                                                                                 \
848
0
                                else if (l_band->precincts_data_size < l_nb_precinct_size) {                                                                                                                      \
849
0
                                                                                                                                                                                                                  \
850
0
                                        opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size);                                             \
851
0
                                        if (! new_precincts) {                                                                                                                                                    \
852
0
                                                /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n");                                                                    */   \
853
0
                                                fprintf(stderr, "Not enough memory to handle band precints\n");                                                                                                   \
854
0
                                                opj_free(l_band->precincts);                                                                                                                                      \
855
0
                                                l_band->precincts = NULL;                                                                                                                                         \
856
0
                                                l_band->precincts_data_size = 0;                                                                                                                                  \
857
0
                                                return OPJ_FALSE;                                                                                                                                                 \
858
0
                                        }                                                                                                                                                                         \
859
0
                                        l_band->precincts = new_precincts;                                                                                                                                        \
860
0
                                        /*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);*/                   \
861
0
                                        memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size);                                                \
862
0
                                        l_band->precincts_data_size = l_nb_precinct_size;                                                                                                                         \
863
0
                                }                                                                                                                                                                                 \
864
0
                                                                                                                                                                                                                  \
865
0
                                l_current_precinct = l_band->precincts;                                                                                                                                           \
866
0
                                for     (precno = 0; precno < l_nb_precincts; ++precno) {                                                                                                                         \
867
0
                                        OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;                                                                                                             \
868
0
                                        OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn);                                                                                           \
869
0
                                        OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn);                                                                                          \
870
0
                                        OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn);                                                                                                                      \
871
0
                                        OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn);                                                                                                                     \
872
0
                                        /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/                                                                    \
873
0
                                        /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/                  \
874
0
                                                                                                                                                                                                                  \
875
0
                                        /* precinct size (global) */                                                                                                                                              \
876
0
                                        /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/                                                                                         \
877
0
                                                                                                                                                                                                                  \
878
0
                                        l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0);                                                                                                                  \
879
0
                                        l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0);                                                                                                                  \
880
0
                                        l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1);                                                                                                                    \
881
0
                                        l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1);                                                                                                                    \
882
0
                                        /*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);*/    \
883
0
                                                                                                                                                                                                                  \
884
0
                                        tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;                                                                                  \
885
0
                                        /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/                                                                                                               \
886
0
                                        tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;                                                                                \
887
0
                                        /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/                                                                                                               \
888
0
                                        brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn;                                                                                     \
889
0
                                        /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/                                                                                                                   \
890
0
                                        brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn;                                                                                   \
891
0
                                        /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/                                                                                                                   \
892
0
                                        l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn);                                                                                                    \
893
0
                                        l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn);                                                                                                   \
894
0
                                                                                                                                                                                                                  \
895
0
                                        l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch;                                                                                                       \
896
0
                                        /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch);      */                                                \
897
0
                                        l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof(TYPE);                                                                                                                  \
898
0
                                                                                                                                                                                                                  \
899
0
                                        if (! l_current_precinct->cblks.ELEMENT) {                                                                                                                                \
900
0
                                                l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size);                                                                                    \
901
0
                                                if (! l_current_precinct->cblks.ELEMENT ) {                                                                                                                       \
902
0
                                                        return OPJ_FALSE;                                                                                                                                         \
903
0
                                                }                                                                                                                                                                 \
904
0
                                                /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/                                                  \
905
0
                                                                                                                                                                                                                  \
906
0
                                                memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size);                                                                                                \
907
0
                                                                                                                                                                                                                  \
908
0
                                                l_current_precinct->block_size = l_nb_code_blocks_size;                                                                                                           \
909
0
                                        }                                                                                                                                                                         \
910
0
                                        else if (l_nb_code_blocks_size > l_current_precinct->block_size) {                                                                                                        \
911
0
                                                TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size);                                                                \
912
0
                                                if (! new_ELEMENT) {                                                                                                                                              \
913
0
                                                        opj_free(l_current_precinct->cblks.ELEMENT);                                                                                                              \
914
0
                                                        l_current_precinct->cblks.ELEMENT = NULL;                                                                                                                 \
915
0
                                                        l_current_precinct->block_size = 0;                                                                                                                       \
916
0
                                                        /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n");                                              */  \
917
0
                                                        fprintf(stderr, "Not enough memory for current precinct codeblock element\n");                                                                            \
918
0
                                                        return OPJ_FALSE;                                                                                                                                         \
919
0
                                                }                                                                                                                                                                 \
920
0
                                                l_current_precinct->cblks.ELEMENT = new_ELEMENT;                                                                                                                  \
921
0
                                                /*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);     */\
922
0
                                                                                                                                                                                                                  \
923
0
                                                memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size                                                                          \
924
0
                                                                ,0                                                                                                                                                \
925
0
                                                                ,l_nb_code_blocks_size - l_current_precinct->block_size);                                                                                         \
926
0
                                                                                                                                                                                                                  \
927
0
                                                l_current_precinct->block_size = l_nb_code_blocks_size;                                                                                                           \
928
0
                                        }                                                                                                                                                                         \
929
0
                                                                                                                                                                                                                  \
930
0
                                        if (! l_current_precinct->incltree) {                                                                                                                                     \
931
0
                                                l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw,                                                                                              \
932
0
                                                                l_current_precinct->ch);                                                                                                                          \
933
0
                                        }                                                                                                                                                                         \
934
0
                                        else{                                                                                                                                                                     \
935
0
                                                l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree,                                                                                             \
936
0
                                                                l_current_precinct->cw,                                                                                                                           \
937
0
                                                                l_current_precinct->ch);                                                                                                                          \
938
0
                                        }                                                                                                                                                                         \
939
0
                                                                                                                                                                                                                  \
940
0
                                        if (! l_current_precinct->incltree)     {                                                                                                                                 \
941
0
                                                fprintf(stderr, "WARNING: No incltree created.\n");                                                                                                               \
942
0
                                                /*return OPJ_FALSE;*/                                                                                                                                             \
943
0
                                        }                                                                                                                                                                         \
944
0
                                                                                                                                                                                                                  \
945
0
                                        if (! l_current_precinct->imsbtree) {                                                                                                                                     \
946
0
                                                l_current_precinct->imsbtree = opj_tgt_create(                                                                                                                     \
947
0
                                                                l_current_precinct->cw,                                                                                                                           \
948
0
                                                                l_current_precinct->ch);                                                                                                                          \
949
0
                                        }                                                                                                                                                                         \
950
0
                                        else {                                                                                                                                                                    \
951
0
                                                l_current_precinct->imsbtree = opj_tgt_init(                                                                                                                          \
952
0
                                                                l_current_precinct->imsbtree,                                                                                                                     \
953
0
                                                                l_current_precinct->cw,                                                                                                                           \
954
0
                                                                l_current_precinct->ch);                                                                                                                          \
955
0
                                        }                                                                                                                                                                         \
956
0
                                                                                                                                                                                                                  \
957
0
                                        if (! l_current_precinct->imsbtree) {                                                                                                                                     \
958
0
                                                fprintf(stderr, "WARNING: No imsbtree created.\n");                                                                                                               \
959
0
                                                /*return OPJ_FALSE;*/                                                                                                                                             \
960
0
                                        }                                                                                                                                                                         \
961
0
                                                                                                                                                                                                                  \
962
0
                                        l_code_block = l_current_precinct->cblks.ELEMENT;                                                                                                                         \
963
0
                                                                                                                                                                                                                  \
964
0
                                        for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {                                                                                                                   \
965
0
                                                OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn);                                                                   \
966
0
                                                OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn);                                                                  \
967
0
                                                OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn);                                                                                                           \
968
0
                                                OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn);                                                                                                          \
969
0
                                                                                                                                                                                                                  \
970
0
                                                /* code-block size (global) */                                                                                                                                    \
971
0
                                                l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0);                                                                                                   \
972
0
                                                l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0);                                                                                                   \
973
0
                                                l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1);                                                                                                     \
974
0
                                                l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1);                                                                                                     \
975
0
                                                                                                                                                                                                                  \
976
0
                                                if (! FUNCTION_ELEMENT(l_code_block)) {                                                                                                                           \
977
0
                                                        return OPJ_FALSE;                                                                                                                                         \
978
0
                                                }                                                                                                                                                                 \
979
0
                                                ++l_code_block;                                                                                                                                                   \
980
0
                                        }                                                                                                                                                                         \
981
0
                                        ++l_current_precinct;                                                                                                                                                     \
982
0
                                } /* precno */                                                                                                                                                                    \
983
0
                                ++l_band;                                                                                                                                                                         \
984
0
                                ++l_step_size;                                                                                                                                                                    \
985
0
                        } /* bandno */                                                                                                                                                                            \
986
0
                        ++l_res;                                                                                                                                                                                  \
987
0
                        --l_level_no;                                                                                                                                                                             \
988
0
                } /* resno */                                                                                                                                                                                     \
989
0
                ++l_tccp;                                                                                                                                                                                         \
990
0
                ++l_tilec;                                                                                                                                                                                        \
991
0
                ++l_image_comp;                                                                                                                                                                                   \
992
0
        } /* compno */                                                                                                                                                                                            \
993
0
        return OPJ_TRUE;                                                                                                                                                                                          \
994
0
}                                                                                                                                                                                                                 \
Unexecuted instantiation: opj_tcd_init_encode_tile
Unexecuted instantiation: opj_tcd_init_decode_tile
995
996
997
OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
998
OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
999
1000
#undef OPJ_MACRO_TCD_ALLOCATE
1001
1002
/**
1003
 * Allocates memory for an encoding code block.
1004
 */
1005
OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1006
0
{
1007
0
        if (! p_code_block->data) {
1008
1009
0
                p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
1010
0
                if(! p_code_block->data) {
1011
0
                        return OPJ_FALSE;
1012
0
                }
1013
1014
0
                p_code_block->data[0] = 0;
1015
0
                p_code_block->data+=1;
1016
1017
                /* no memset since data */
1018
0
                p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
1019
0
                if (! p_code_block->layers) {
1020
0
                        return OPJ_FALSE;
1021
0
                }
1022
1023
0
                p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
1024
0
                if (! p_code_block->passes) {
1025
0
                        return OPJ_FALSE;
1026
0
                }
1027
0
        }
1028
1029
0
        memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1030
0
        memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
1031
1032
0
        return OPJ_TRUE;
1033
0
}
1034
1035
/**
1036
 * Allocates memory for a decoding code block.
1037
 */
1038
OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1039
0
{
1040
0
        OPJ_UINT32 l_seg_size;
1041
1042
0
        if (! p_code_block->data) {
1043
1044
0
                p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1045
0
                if (! p_code_block->data) {
1046
0
                        return OPJ_FALSE;
1047
0
                }
1048
0
                p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1049
                /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1050
1051
0
                l_seg_size = OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1052
0
                p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1053
0
                if (! p_code_block->segs) {
1054
0
                        return OPJ_FALSE;
1055
0
                }
1056
0
                memset(p_code_block->segs,0,l_seg_size);
1057
                /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1058
1059
0
                p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1060
                /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1061
0
        }
1062
        /* TODO */
1063
        /*p_code_block->numsegs = 0; */
1064
1065
0
        return OPJ_TRUE;
1066
0
}
1067
1068
OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1069
0
{
1070
0
        OPJ_UINT32 i;
1071
0
        OPJ_UINT32 l_data_size = 0;
1072
0
        opj_image_comp_t * l_img_comp = 00;
1073
0
        opj_tcd_tilecomp_t * l_tile_comp = 00;
1074
0
        opj_tcd_resolution_t * l_res = 00;
1075
0
        OPJ_UINT32 l_size_comp, l_remaining;
1076
1077
0
        l_tile_comp = p_tcd->tcd_image->tiles->comps;
1078
0
        l_img_comp = p_tcd->image->comps;
1079
1080
0
        for (i=0;i<p_tcd->image->numcomps;++i) {
1081
0
                l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1082
0
                l_remaining = l_img_comp->prec & 7;  /* (%8) */
1083
1084
0
                if(l_remaining) {
1085
0
                        ++l_size_comp;
1086
0
                }
1087
1088
0
                if (l_size_comp == 3) {
1089
0
                        l_size_comp = 4;
1090
0
                }
1091
1092
0
                l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1093
0
                l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1094
0
                ++l_img_comp;
1095
0
                ++l_tile_comp;
1096
0
        }
1097
1098
0
        return l_data_size;
1099
0
}
1100
1101
OPJ_BOOL opj_tcd_encode_tile(   opj_tcd_t *p_tcd,
1102
                                                        OPJ_UINT32 p_tile_no,
1103
                                                        OPJ_BYTE *p_dest,
1104
                                                        OPJ_UINT32 * p_data_written,
1105
                                                        OPJ_UINT32 p_max_length,
1106
                                                        opj_codestream_info_t *p_cstr_info)
1107
0
{
1108
1109
0
        if (p_tcd->cur_tp_num == 0) {
1110
1111
0
                p_tcd->tcd_tileno = p_tile_no;
1112
0
                p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1113
1114
                /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1115
0
                if(p_cstr_info)  {
1116
0
                        OPJ_UINT32 l_num_packs = 0;
1117
0
                        OPJ_UINT32 i;
1118
0
                        opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0];        /* based on component 0 */
1119
0
                        opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1120
1121
0
                        for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1122
0
                                opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1123
1124
0
                                p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1125
0
                                p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1126
1127
0
                                l_num_packs += l_res_idx->pw * l_res_idx->ph;
1128
0
                                p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1129
0
                                p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1130
0
                        }
1131
0
                        p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t*) opj_calloc((size_t)p_cstr_info->numcomps * (size_t)p_cstr_info->numlayers * l_num_packs, sizeof(opj_packet_info_t));
1132
0
                }
1133
                /* << INDEX */
1134
1135
                /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1136
                /*---------------TILE-------------------*/
1137
0
                if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1138
0
                        return OPJ_FALSE;
1139
0
                }
1140
                /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1141
1142
                /* FIXME _ProfStart(PGROUP_MCT); */
1143
0
                if (! opj_tcd_mct_encode(p_tcd)) {
1144
0
                        return OPJ_FALSE;
1145
0
                }
1146
                /* FIXME _ProfStop(PGROUP_MCT); */
1147
1148
                /* FIXME _ProfStart(PGROUP_DWT); */
1149
0
                if (! opj_tcd_dwt_encode(p_tcd)) {
1150
0
                        return OPJ_FALSE;
1151
0
                }
1152
                /* FIXME  _ProfStop(PGROUP_DWT); */
1153
1154
                /* FIXME  _ProfStart(PGROUP_T1); */
1155
0
                if (! opj_tcd_t1_encode(p_tcd)) {
1156
0
                        return OPJ_FALSE;
1157
0
                }
1158
                /* FIXME _ProfStop(PGROUP_T1); */
1159
1160
                /* FIXME _ProfStart(PGROUP_RATE); */
1161
0
                if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1162
0
                        return OPJ_FALSE;
1163
0
                }
1164
                /* FIXME _ProfStop(PGROUP_RATE); */
1165
1166
0
        }
1167
        /*--------------TIER2------------------*/
1168
1169
        /* INDEX */
1170
0
        if (p_cstr_info) {
1171
0
                p_cstr_info->index_write = 1;
1172
0
        }
1173
        /* FIXME _ProfStart(PGROUP_T2); */
1174
1175
0
        if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1176
0
                return OPJ_FALSE;
1177
0
        }
1178
        /* FIXME _ProfStop(PGROUP_T2); */
1179
1180
        /*---------------CLEAN-------------------*/
1181
1182
0
        return OPJ_TRUE;
1183
0
}
1184
1185
OPJ_BOOL opj_tcd_decode_tile(   opj_tcd_t *p_tcd,
1186
                                OPJ_BYTE *p_src,
1187
                                OPJ_UINT32 p_max_length,
1188
                                OPJ_UINT32 p_tile_no,
1189
                                opj_codestream_index_t *p_cstr_index
1190
                                )
1191
0
{
1192
0
        OPJ_UINT32 l_data_read;
1193
0
        p_tcd->tcd_tileno = p_tile_no;
1194
0
        p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1195
1196
#ifdef TODO_MSD /* FIXME */
1197
        /* INDEX >>  */
1198
        if(p_cstr_info) {
1199
                OPJ_UINT32 resno, compno, numprec = 0;
1200
                for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1201
                        opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1202
                        opj_tccp_t *tccp = &tcp->tccps[compno];
1203
                        opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1204
                        for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1205
                                opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1206
                                p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1207
                                p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1208
                                numprec += res_idx->pw * res_idx->ph;
1209
                                p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1210
                                p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1211
                        }
1212
                }
1213
                p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1214
                p_cstr_info->packno = 0;
1215
        }
1216
        /* << INDEX */
1217
#endif
1218
1219
        /*--------------TIER2------------------*/
1220
        /* FIXME _ProfStart(PGROUP_T2); */
1221
0
        l_data_read = 0;
1222
0
        if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1223
0
        {
1224
0
                return OPJ_FALSE;
1225
0
        }
1226
        /* FIXME _ProfStop(PGROUP_T2); */
1227
1228
        /*------------------TIER1-----------------*/
1229
1230
        /* FIXME _ProfStart(PGROUP_T1); */
1231
0
        if
1232
0
                (! opj_tcd_t1_decode(p_tcd))
1233
0
        {
1234
0
                return OPJ_FALSE;
1235
0
        }
1236
        /* FIXME _ProfStop(PGROUP_T1); */
1237
1238
        /*----------------DWT---------------------*/
1239
1240
        /* FIXME _ProfStart(PGROUP_DWT); */
1241
0
        if
1242
0
                (! opj_tcd_dwt_decode(p_tcd))
1243
0
        {
1244
0
                return OPJ_FALSE;
1245
0
        }
1246
        /* FIXME _ProfStop(PGROUP_DWT); */
1247
1248
        /*----------------MCT-------------------*/
1249
        /* FIXME _ProfStart(PGROUP_MCT); */
1250
0
        if
1251
0
                (! opj_tcd_mct_decode(p_tcd))
1252
0
        {
1253
0
                return OPJ_FALSE;
1254
0
        }
1255
        /* FIXME _ProfStop(PGROUP_MCT); */
1256
1257
        /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1258
0
        if
1259
0
                (! opj_tcd_dc_level_shift_decode(p_tcd))
1260
0
        {
1261
0
                return OPJ_FALSE;
1262
0
        }
1263
        /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1264
1265
1266
        /*---------------TILE-------------------*/
1267
0
        return OPJ_TRUE;
1268
0
}
1269
1270
OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1271
                                    OPJ_BYTE * p_dest,
1272
                                    OPJ_UINT32 p_dest_length
1273
                                    )
1274
0
{
1275
0
        OPJ_UINT32 i,j,k,l_data_size = 0;
1276
0
        opj_image_comp_t * l_img_comp = 00;
1277
0
        opj_tcd_tilecomp_t * l_tilec = 00;
1278
0
        opj_tcd_resolution_t * l_res;
1279
0
        OPJ_UINT32 l_size_comp, l_remaining;
1280
0
        OPJ_UINT32 l_stride, l_width,l_height;
1281
1282
0
        l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1283
0
        if (l_data_size > p_dest_length) {
1284
0
                return OPJ_FALSE;
1285
0
        }
1286
1287
0
        l_tilec = p_tcd->tcd_image->tiles->comps;
1288
0
        l_img_comp = p_tcd->image->comps;
1289
1290
0
        for (i=0;i<p_tcd->image->numcomps;++i) {
1291
0
                l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1292
0
                l_remaining = l_img_comp->prec & 7;  /* (%8) */
1293
0
                l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1294
0
                l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1295
0
                l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1296
0
                l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1297
1298
0
                if (l_remaining) {
1299
0
                        ++l_size_comp;
1300
0
                }
1301
1302
0
                if (l_size_comp == 3) {
1303
0
                        l_size_comp = 4;
1304
0
                }
1305
1306
0
                switch (l_size_comp)
1307
0
                        {
1308
0
                        case 1:
1309
0
                                {
1310
0
                                        OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1311
0
                                        const OPJ_INT32 * l_src_ptr = l_tilec->data;
1312
1313
0
                                        if (l_img_comp->sgnd) {
1314
0
                                                for (j=0;j<l_height;++j) {
1315
0
                                                        for (k=0;k<l_width;++k) {
1316
0
                                                                *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1317
0
                                                        }
1318
0
                                                        l_src_ptr += l_stride;
1319
0
                                                }
1320
0
                                        }
1321
0
                                        else {
1322
0
                                                for (j=0;j<l_height;++j) {
1323
0
                                                        for     (k=0;k<l_width;++k) {
1324
0
                                                                *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1325
0
                                                        }
1326
0
                                                        l_src_ptr += l_stride;
1327
0
                                                }
1328
0
                                        }
1329
1330
0
                                        p_dest = (OPJ_BYTE *)l_dest_ptr;
1331
0
                                }
1332
0
                                break;
1333
0
                        case 2:
1334
0
                                {
1335
0
                                        const OPJ_INT32 * l_src_ptr = l_tilec->data;
1336
0
                                        OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1337
1338
0
                                        if (l_img_comp->sgnd) {
1339
0
                                                for (j=0;j<l_height;++j) {
1340
0
                                                        for (k=0;k<l_width;++k) {
1341
0
                                                                *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1342
0
                                                        }
1343
0
                                                        l_src_ptr += l_stride;
1344
0
                                                }
1345
0
                                        }
1346
0
                                        else {
1347
0
                                                for (j=0;j<l_height;++j) {
1348
0
                                                        for (k=0;k<l_width;++k) {
1349
0
                                                                *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1350
0
                                                        }
1351
0
                                                        l_src_ptr += l_stride;
1352
0
                                                }
1353
0
                                        }
1354
1355
0
                                        p_dest = (OPJ_BYTE*) l_dest_ptr;
1356
0
                                }
1357
0
                                break;
1358
0
                        case 4:
1359
0
                                {
1360
0
                                        OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1361
0
                                        OPJ_INT32 * l_src_ptr = l_tilec->data;
1362
1363
0
                                        for (j=0;j<l_height;++j) {
1364
0
                                                for (k=0;k<l_width;++k) {
1365
0
                                                        *(l_dest_ptr++) = (*(l_src_ptr++));
1366
0
                                                }
1367
0
                                                l_src_ptr += l_stride;
1368
0
                                        }
1369
1370
0
                                        p_dest = (OPJ_BYTE*) l_dest_ptr;
1371
0
                                }
1372
0
                                break;
1373
0
                }
1374
1375
0
                ++l_img_comp;
1376
0
                ++l_tilec;
1377
0
        }
1378
1379
0
        return OPJ_TRUE;
1380
0
}
1381
1382
1383
1384
1385
void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1386
0
{
1387
0
        OPJ_UINT32 compno, resno, bandno, precno;
1388
0
        opj_tcd_tile_t *l_tile = 00;
1389
0
        opj_tcd_tilecomp_t *l_tile_comp = 00;
1390
0
        opj_tcd_resolution_t *l_res = 00;
1391
0
        opj_tcd_band_t *l_band = 00;
1392
0
        opj_tcd_precinct_t *l_precinct = 00;
1393
0
        OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1394
0
        void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1395
1396
0
        if (! p_tcd) {
1397
0
                return;
1398
0
        }
1399
1400
0
        if (! p_tcd->tcd_image) {
1401
0
                return;
1402
0
        }
1403
1404
0
        if (p_tcd->m_is_decoder) {
1405
0
                l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1406
0
        }
1407
0
        else {
1408
0
                l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1409
0
        }
1410
1411
0
        l_tile = p_tcd->tcd_image->tiles;
1412
0
        if (! l_tile) {
1413
0
                return;
1414
0
        }
1415
1416
0
        l_tile_comp = l_tile->comps;
1417
1418
0
        for (compno = 0; compno < l_tile->numcomps; ++compno) {
1419
0
                l_res = l_tile_comp->resolutions;
1420
0
                if (l_res) {
1421
1422
0
                        l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1423
0
                        for (resno = 0; resno < l_nb_resolutions; ++resno) {
1424
0
                                l_band = l_res->bands;
1425
0
                                for     (bandno = 0; bandno < 3; ++bandno) {
1426
0
                                        l_precinct = l_band->precincts;
1427
0
                                        if (l_precinct) {
1428
1429
0
                                                l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1430
0
                                                for (precno = 0; precno < l_nb_precincts; ++precno) {
1431
0
                                                        opj_tgt_destroy(l_precinct->incltree);
1432
0
                                                        l_precinct->incltree = 00;
1433
0
                                                        opj_tgt_destroy(l_precinct->imsbtree);
1434
0
                                                        l_precinct->imsbtree = 00;
1435
0
                                                        (*l_tcd_code_block_deallocate) (l_precinct);
1436
0
                                                        ++l_precinct;
1437
0
                                                }
1438
1439
0
                                                opj_free(l_band->precincts);
1440
0
                                                l_band->precincts = 00;
1441
0
                                        }
1442
0
                                        ++l_band;
1443
0
                                } /* for (resno */
1444
0
                                ++l_res;
1445
0
                        }
1446
1447
0
                        opj_free(l_tile_comp->resolutions);
1448
0
                        l_tile_comp->resolutions = 00;
1449
0
                }
1450
1451
0
                if (l_tile_comp->data) {
1452
0
                        opj_free(l_tile_comp->data);
1453
0
                        l_tile_comp->data = 00;
1454
0
                }
1455
0
                ++l_tile_comp;
1456
0
        }
1457
1458
0
        opj_free(l_tile->comps);
1459
0
        l_tile->comps = 00;
1460
0
        opj_free(p_tcd->tcd_image->tiles);
1461
0
        p_tcd->tcd_image->tiles = 00;
1462
0
}
1463
1464
1465
OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1466
                            OPJ_BYTE * p_src_data,
1467
                            OPJ_UINT32 * p_data_read,
1468
                            OPJ_UINT32 p_max_src_size,
1469
                            opj_codestream_index_t *p_cstr_index
1470
                            )
1471
0
{
1472
0
        opj_t2_t * l_t2;
1473
1474
0
        l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1475
0
        if (l_t2 == 00) {
1476
0
                return OPJ_FALSE;
1477
0
        }
1478
1479
0
        if (! opj_t2_decode_packets(
1480
0
                                        l_t2,
1481
0
                                        p_tcd->tcd_tileno,
1482
0
                                        p_tcd->tcd_image->tiles,
1483
0
                                        p_src_data,
1484
0
                                        p_data_read,
1485
0
                                        p_max_src_size,
1486
0
                                        p_cstr_index)) {
1487
0
                opj_t2_destroy(l_t2);
1488
0
                return OPJ_FALSE;
1489
0
        }
1490
1491
0
        opj_t2_destroy(l_t2);
1492
1493
        /*---------------CLEAN-------------------*/
1494
0
        return OPJ_TRUE;
1495
0
}
1496
1497
OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1498
0
{
1499
0
        OPJ_UINT32 compno;
1500
0
        opj_t1_t * l_t1;
1501
0
        opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1502
0
        opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1503
0
        opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1504
1505
1506
0
        l_t1 = opj_t1_create();
1507
0
        if (l_t1 == 00) {
1508
0
                return OPJ_FALSE;
1509
0
        }
1510
1511
0
        for (compno = 0; compno < l_tile->numcomps; ++compno) {
1512
                /* The +3 is headroom required by the vectorized DWT */
1513
0
                if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1514
0
                        opj_t1_destroy(l_t1);
1515
0
                        return OPJ_FALSE;
1516
0
                }
1517
0
                ++l_tile_comp;
1518
0
                ++l_tccp;
1519
0
        }
1520
1521
0
        opj_t1_destroy(l_t1);
1522
1523
0
        return OPJ_TRUE;
1524
0
}
1525
1526
1527
OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1528
0
{
1529
0
        OPJ_UINT32 compno;
1530
0
        opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1531
0
        opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1532
0
        opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1533
0
        opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1534
1535
0
        for (compno = 0; compno < l_tile->numcomps; compno++) {
1536
                /*
1537
                if (tcd->cp->reduce != 0) {
1538
                        tcd->image->comps[compno].resno_decoded =
1539
                                tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1540
                        if (tcd->image->comps[compno].resno_decoded < 0)
1541
                        {
1542
                                return false;
1543
                        }
1544
                }
1545
                numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1546
                if(numres2decode > 0){
1547
                */
1548
1549
0
                if (l_tccp->qmfbid == 1) {
1550
0
                        if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1551
0
                                return OPJ_FALSE;
1552
0
                        }
1553
0
                }
1554
0
                else {
1555
0
                        if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1556
0
                                return OPJ_FALSE;
1557
0
                        }
1558
0
                }
1559
1560
0
                ++l_tile_comp;
1561
0
                ++l_img_comp;
1562
0
                ++l_tccp;
1563
0
        }
1564
1565
0
        return OPJ_TRUE;
1566
0
}
1567
OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1568
0
{
1569
0
        opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1570
0
        opj_tcp_t * l_tcp = p_tcd->tcp;
1571
0
        opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1572
0
        OPJ_UINT32 l_samples,i;
1573
1574
0
        if (! l_tcp->mct) {
1575
0
                return OPJ_TRUE;
1576
0
        }
1577
1578
0
        l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1579
1580
0
        if (l_tile->numcomps >= 3 ){
1581
                /* testcase 1336.pdf.asan.47.376 */
1582
0
                if ((l_tile->comps[0].x1 - l_tile->comps[0].x0) * (l_tile->comps[0].y1 - l_tile->comps[0].y0) < (OPJ_INT32)l_samples ||
1583
0
                    (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1584
0
                    (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1585
0
                        fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1586
0
                        return OPJ_FALSE;
1587
0
                }
1588
0
                else if (l_tcp->mct == 2) {
1589
0
                        OPJ_BYTE ** l_data;
1590
1591
0
                        if (! l_tcp->m_mct_decoding_matrix) {
1592
0
                                return OPJ_TRUE;
1593
0
                        }
1594
1595
0
                        l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1596
0
                        if (! l_data) {
1597
0
                                return OPJ_FALSE;
1598
0
                        }
1599
1600
0
                        for (i=0;i<l_tile->numcomps;++i) {
1601
0
                                l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1602
0
                                ++l_tile_comp;
1603
0
                        }
1604
1605
0
                        if (! opj_mct_decode_custom(/* MCT data */
1606
0
                                                                        (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1607
                                                                        /* size of components */
1608
0
                                                                        l_samples,
1609
                                                                        /* components */
1610
0
                                                                        l_data,
1611
                                                                        /* nb of components (i.e. size of pData) */
1612
0
                                                                        l_tile->numcomps,
1613
                                                                        /* tells if the data is signed */
1614
0
                                                                        p_tcd->image->comps->sgnd)) {
1615
0
                                opj_free(l_data);
1616
0
                                return OPJ_FALSE;
1617
0
                        }
1618
1619
0
                        opj_free(l_data);
1620
0
                }
1621
0
                else {
1622
0
                        if (l_tcp->tccps->qmfbid == 1) {
1623
0
                                opj_mct_decode(     l_tile->comps[0].data,
1624
0
                                                        l_tile->comps[1].data,
1625
0
                                                        l_tile->comps[2].data,
1626
0
                                                        l_samples);
1627
0
                        }
1628
0
                        else {
1629
0
                            opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1630
0
                                                (OPJ_FLOAT32*)l_tile->comps[1].data,
1631
0
                                                (OPJ_FLOAT32*)l_tile->comps[2].data,
1632
0
                                                l_samples);
1633
0
                        }
1634
0
                }
1635
0
        }
1636
0
        else {
1637
                /* FIXME need to use opj_event_msg function */
1638
0
                fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1639
0
        }
1640
1641
0
        return OPJ_TRUE;
1642
0
}
1643
1644
1645
OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1646
0
{
1647
0
        OPJ_UINT32 compno;
1648
0
        opj_tcd_tilecomp_t * l_tile_comp = 00;
1649
0
        opj_tccp_t * l_tccp = 00;
1650
0
        opj_image_comp_t * l_img_comp = 00;
1651
0
        opj_tcd_resolution_t* l_res = 00;
1652
0
        opj_tcd_tile_t * l_tile;
1653
0
        OPJ_UINT32 l_width,l_height,i,j;
1654
0
        OPJ_INT32 * l_current_ptr;
1655
0
        OPJ_INT32 l_min, l_max;
1656
0
        OPJ_UINT32 l_stride;
1657
1658
0
        l_tile = p_tcd->tcd_image->tiles;
1659
0
        l_tile_comp = l_tile->comps;
1660
0
        l_tccp = p_tcd->tcp->tccps;
1661
0
        l_img_comp = p_tcd->image->comps;
1662
1663
0
        for (compno = 0; compno < l_tile->numcomps; compno++) {
1664
0
                l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1665
0
                l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1666
0
                l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1667
0
                l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1668
1669
0
                assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1670
1671
0
                if (l_img_comp->sgnd) {
1672
0
                        l_min = -(1 << (l_img_comp->prec - 1));
1673
0
                        l_max = (1 << (l_img_comp->prec - 1)) - 1;
1674
0
                }
1675
0
                else {
1676
0
            l_min = 0;
1677
0
                        l_max = (1 << l_img_comp->prec) - 1;
1678
0
                }
1679
1680
0
                l_current_ptr = l_tile_comp->data;
1681
1682
0
                if (l_tccp->qmfbid == 1) {
1683
0
                        for (j=0;j<l_height;++j) {
1684
0
                                for (i = 0; i < l_width; ++i) {
1685
0
                                        *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1686
0
                                        ++l_current_ptr;
1687
0
                                }
1688
0
                                l_current_ptr += l_stride;
1689
0
                        }
1690
0
                }
1691
0
                else {
1692
0
                        for (j=0;j<l_height;++j) {
1693
0
                                for (i = 0; i < l_width; ++i) {
1694
0
                                        OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1695
0
                                        *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1696
0
                                        ++l_current_ptr;
1697
0
                                }
1698
0
                                l_current_ptr += l_stride;
1699
0
                        }
1700
0
                }
1701
1702
0
                ++l_img_comp;
1703
0
                ++l_tccp;
1704
0
                ++l_tile_comp;
1705
0
        }
1706
1707
0
        return OPJ_TRUE;
1708
0
}
1709
1710
1711
1712
/**
1713
 * Deallocates the encoding data of the given precinct.
1714
 */
1715
void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1716
0
{
1717
0
        OPJ_UINT32 cblkno , l_nb_code_blocks;
1718
1719
0
        opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1720
0
        if (l_code_block) {
1721
                /*fprintf(stderr,"deallocate codeblock:{\n");*/
1722
                /*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);*/
1723
                /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1724
                                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 );*/
1725
1726
1727
0
                l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1728
                /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1729
1730
0
                for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1731
1732
0
                        if (l_code_block->data) {
1733
0
                                opj_free(l_code_block->data);
1734
0
                                l_code_block->data = 00;
1735
0
                        }
1736
1737
0
                        if (l_code_block->segs) {
1738
0
                                opj_free(l_code_block->segs );
1739
0
                                l_code_block->segs = 00;
1740
0
                        }
1741
1742
0
                        ++l_code_block;
1743
0
                }
1744
1745
0
                opj_free(p_precinct->cblks.dec);
1746
0
                p_precinct->cblks.dec = 00;
1747
0
        }
1748
0
}
1749
1750
/**
1751
 * Deallocates the encoding data of the given precinct.
1752
 */
1753
void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1754
0
{       
1755
0
        OPJ_UINT32 cblkno , l_nb_code_blocks;
1756
1757
0
        opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1758
0
        if (l_code_block) {
1759
0
                l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1760
                
1761
0
                for     (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno)  {
1762
0
                        if (l_code_block->data) {
1763
0
                                opj_free(l_code_block->data - 1);
1764
0
                                l_code_block->data = 00;
1765
0
                        }
1766
1767
0
                        if (l_code_block->layers) {
1768
0
                                opj_free(l_code_block->layers );
1769
0
                                l_code_block->layers = 00;
1770
0
                        }
1771
1772
0
                        if (l_code_block->passes) {
1773
0
                                opj_free(l_code_block->passes );
1774
0
                                l_code_block->passes = 00;
1775
0
                        }
1776
0
                        ++l_code_block;
1777
0
                }
1778
1779
0
                opj_free(p_precinct->cblks.enc);
1780
                
1781
0
                p_precinct->cblks.enc = 00;
1782
0
        }
1783
0
}
1784
1785
OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1786
0
{
1787
0
        OPJ_UINT32 i,l_data_size = 0;
1788
0
        opj_image_comp_t * l_img_comp = 00;
1789
0
        opj_tcd_tilecomp_t * l_tilec = 00;
1790
0
        OPJ_UINT32 l_size_comp, l_remaining;
1791
1792
0
        l_tilec = p_tcd->tcd_image->tiles->comps;
1793
0
        l_img_comp = p_tcd->image->comps;
1794
0
        for (i=0;i<p_tcd->image->numcomps;++i) {
1795
0
                l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1796
0
                l_remaining = l_img_comp->prec & 7;  /* (%8) */
1797
1798
0
                if (l_remaining) {
1799
0
                        ++l_size_comp;
1800
0
                }
1801
1802
0
                if (l_size_comp == 3) {
1803
0
                        l_size_comp = 4;
1804
0
                }
1805
1806
0
                l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1807
0
                ++l_img_comp;
1808
0
                ++l_tilec;
1809
0
        }
1810
1811
0
        return l_data_size;
1812
0
}
1813
                
1814
OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1815
0
{
1816
0
        OPJ_UINT32 compno;
1817
0
        opj_tcd_tilecomp_t * l_tile_comp = 00;
1818
0
        opj_tccp_t * l_tccp = 00;
1819
0
        opj_image_comp_t * l_img_comp = 00;
1820
0
        opj_tcd_tile_t * l_tile;
1821
0
        OPJ_UINT32 l_nb_elem,i;
1822
0
        OPJ_INT32 * l_current_ptr;
1823
1824
0
        l_tile = p_tcd->tcd_image->tiles;
1825
0
        l_tile_comp = l_tile->comps;
1826
0
        l_tccp = p_tcd->tcp->tccps;
1827
0
        l_img_comp = p_tcd->image->comps;
1828
1829
0
        for (compno = 0; compno < l_tile->numcomps; compno++) {
1830
0
                l_current_ptr = l_tile_comp->data;
1831
0
                l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1832
1833
0
                if (l_tccp->qmfbid == 1) {
1834
0
                        for     (i = 0; i < l_nb_elem; ++i) {
1835
0
                                *l_current_ptr -= l_tccp->m_dc_level_shift ;
1836
0
                                ++l_current_ptr;
1837
0
                        }
1838
0
                }
1839
0
                else {
1840
0
                        for (i = 0; i < l_nb_elem; ++i) {
1841
0
                                *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1842
0
                                ++l_current_ptr;
1843
0
                        }
1844
0
                }
1845
1846
0
                ++l_img_comp;
1847
0
                ++l_tccp;
1848
0
                ++l_tile_comp;
1849
0
        }
1850
1851
0
        return OPJ_TRUE;
1852
0
}
1853
1854
OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1855
0
{
1856
0
        opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1857
0
        opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1858
0
        OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1859
0
        OPJ_UINT32 i;
1860
0
        OPJ_BYTE ** l_data = 00;
1861
0
        opj_tcp_t * l_tcp = p_tcd->tcp;
1862
1863
0
        if(!p_tcd->tcp->mct) {
1864
0
                return OPJ_TRUE;
1865
0
        }
1866
1867
0
        if (p_tcd->tcp->mct == 2) {
1868
0
                if (! p_tcd->tcp->m_mct_coding_matrix) {
1869
0
                        return OPJ_TRUE;
1870
0
                }
1871
1872
0
        l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1873
0
                if (! l_data) {
1874
0
                        return OPJ_FALSE;
1875
0
                }
1876
1877
0
                for (i=0;i<l_tile->numcomps;++i) {
1878
0
                        l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1879
0
                        ++l_tile_comp;
1880
0
                }
1881
1882
0
                if (! opj_mct_encode_custom(/* MCT data */
1883
0
                                        (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1884
                                        /* size of components */
1885
0
                                        samples,
1886
                                        /* components */
1887
0
                                        l_data,
1888
                                        /* nb of components (i.e. size of pData) */
1889
0
                                        l_tile->numcomps,
1890
                                        /* tells if the data is signed */
1891
0
                                        p_tcd->image->comps->sgnd) )
1892
0
                {
1893
0
            opj_free(l_data);
1894
0
                        return OPJ_FALSE;
1895
0
                }
1896
1897
0
                opj_free(l_data);
1898
0
        }
1899
0
        else if (l_tcp->tccps->qmfbid == 0) {
1900
0
                opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1901
0
        }
1902
0
        else {
1903
0
                opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1904
0
        }
1905
1906
0
        return OPJ_TRUE;
1907
0
}
1908
1909
OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1910
0
{
1911
0
        opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1912
0
        opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1913
0
        opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1914
0
        OPJ_UINT32 compno;
1915
1916
0
        for (compno = 0; compno < l_tile->numcomps; ++compno) {
1917
0
                if (l_tccp->qmfbid == 1) {
1918
0
                        if (! opj_dwt_encode(l_tile_comp)) {
1919
0
                                return OPJ_FALSE;
1920
0
                        }
1921
0
                }
1922
0
                else if (l_tccp->qmfbid == 0) {
1923
0
                        if (! opj_dwt_encode_real(l_tile_comp)) {
1924
0
                                return OPJ_FALSE;
1925
0
                        }
1926
0
                }
1927
1928
0
                ++l_tile_comp;
1929
0
                ++l_tccp;
1930
0
        }
1931
1932
0
        return OPJ_TRUE;
1933
0
}
1934
1935
OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1936
0
{
1937
0
        opj_t1_t * l_t1;
1938
0
        const OPJ_FLOAT64 * l_mct_norms;
1939
0
        opj_tcp_t * l_tcp = p_tcd->tcp;
1940
1941
0
        l_t1 = opj_t1_create();
1942
0
        if (l_t1 == 00) {
1943
0
                return OPJ_FALSE;
1944
0
        }
1945
1946
0
        if (l_tcp->mct == 1) {
1947
                /* irreversible encoding */
1948
0
                if (l_tcp->tccps->qmfbid == 0) {
1949
0
                        l_mct_norms = opj_mct_get_mct_norms_real();
1950
0
                }
1951
0
                else {
1952
0
                        l_mct_norms = opj_mct_get_mct_norms();
1953
0
                }
1954
0
        }
1955
0
        else {
1956
0
                l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1957
0
        }
1958
1959
0
        if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1960
0
        opj_t1_destroy(l_t1);
1961
0
                return OPJ_FALSE;
1962
0
        }
1963
1964
0
        opj_t1_destroy(l_t1);
1965
1966
0
        return OPJ_TRUE;
1967
0
}
1968
1969
OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
1970
                                                OPJ_BYTE * p_dest_data,
1971
                                                OPJ_UINT32 * p_data_written,
1972
                                                OPJ_UINT32 p_max_dest_size,
1973
                                                opj_codestream_info_t *p_cstr_info )
1974
0
{
1975
0
        opj_t2_t * l_t2;
1976
1977
0
        l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1978
0
        if (l_t2 == 00) {
1979
0
                return OPJ_FALSE;
1980
0
        }
1981
1982
0
        if (! opj_t2_encode_packets(
1983
0
                                        l_t2,
1984
0
                                        p_tcd->tcd_tileno,
1985
0
                                        p_tcd->tcd_image->tiles,
1986
0
                                        p_tcd->tcp->numlayers,
1987
0
                                        p_dest_data,
1988
0
                                        p_data_written,
1989
0
                                        p_max_dest_size,
1990
0
                                        p_cstr_info,
1991
0
                                        p_tcd->tp_num,
1992
0
                                        p_tcd->tp_pos,
1993
0
                                        p_tcd->cur_pino,
1994
0
                                        FINAL_PASS))
1995
0
        {
1996
0
                opj_t2_destroy(l_t2);
1997
0
                return OPJ_FALSE;
1998
0
        }
1999
2000
0
        opj_t2_destroy(l_t2);
2001
2002
        /*---------------CLEAN-------------------*/
2003
0
        return OPJ_TRUE;
2004
0
}
2005
2006
2007
OPJ_BOOL opj_tcd_rate_allocate_encode(  opj_tcd_t *p_tcd,
2008
                                                                            OPJ_BYTE * p_dest_data,
2009
                                                                            OPJ_UINT32 p_max_dest_size,
2010
                                                                            opj_codestream_info_t *p_cstr_info )
2011
0
{
2012
0
        opj_cp_t * l_cp = p_tcd->cp;
2013
0
        OPJ_UINT32 l_nb_written = 0;
2014
2015
0
        if (p_cstr_info)  {
2016
0
                p_cstr_info->index_write = 0;
2017
0
        }
2018
2019
0
        if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality)  {
2020
                /* fixed_quality */
2021
                /* Normal Rate/distortion allocation */
2022
0
                if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2023
0
                        return OPJ_FALSE;
2024
0
                }
2025
0
        }
2026
0
        else {
2027
                /* Fixed layer allocation */
2028
0
                opj_tcd_rateallocate_fixed(p_tcd);
2029
0
        }
2030
2031
0
        return OPJ_TRUE;
2032
0
}
2033
2034
2035
OPJ_BOOL opj_tcd_copy_tile_data (       opj_tcd_t *p_tcd,
2036
                                                                    OPJ_BYTE * p_src,
2037
                                                                    OPJ_UINT32 p_src_length )
2038
0
{
2039
0
        OPJ_UINT32 i,j,l_data_size = 0;
2040
0
        opj_image_comp_t * l_img_comp = 00;
2041
0
        opj_tcd_tilecomp_t * l_tilec = 00;
2042
0
        OPJ_UINT32 l_size_comp, l_remaining;
2043
0
        OPJ_UINT32 l_nb_elem;
2044
2045
0
        l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2046
0
        if (l_data_size != p_src_length) {
2047
0
                return OPJ_FALSE;
2048
0
        }
2049
2050
0
        l_tilec = p_tcd->tcd_image->tiles->comps;
2051
0
        l_img_comp = p_tcd->image->comps;
2052
0
        for (i=0;i<p_tcd->image->numcomps;++i) {
2053
0
                l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2054
0
                l_remaining = l_img_comp->prec & 7;  /* (%8) */
2055
0
                l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2056
2057
0
                if (l_remaining) {
2058
0
                        ++l_size_comp;
2059
0
                }
2060
2061
0
                if (l_size_comp == 3) {
2062
0
                        l_size_comp = 4;
2063
0
                }
2064
2065
0
                switch (l_size_comp) {
2066
0
                        case 1:
2067
0
                                {
2068
0
                                        OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2069
0
                                        OPJ_INT32 * l_dest_ptr = l_tilec->data;
2070
2071
0
                                        if (l_img_comp->sgnd) {
2072
0
                                                for (j=0;j<l_nb_elem;++j) {
2073
0
                                                        *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2074
0
                                                }
2075
0
                                        }
2076
0
                                        else {
2077
0
                                                for (j=0;j<l_nb_elem;++j) {
2078
0
                                                        *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2079
0
                                                }
2080
0
                                        }
2081
2082
0
                                        p_src = (OPJ_BYTE*) l_src_ptr;
2083
0
                                }
2084
0
                                break;
2085
0
                        case 2:
2086
0
                                {
2087
0
                                        OPJ_INT32 * l_dest_ptr = l_tilec->data;
2088
0
                                        OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2089
2090
0
                                        if (l_img_comp->sgnd) {
2091
0
                                                for (j=0;j<l_nb_elem;++j) {
2092
0
                                                        *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2093
0
                                                }
2094
0
                                        }
2095
0
                                        else {
2096
0
                                                for (j=0;j<l_nb_elem;++j) {
2097
0
                                                        *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2098
0
                                                }
2099
0
                                        }
2100
2101
0
                                        p_src = (OPJ_BYTE*) l_src_ptr;
2102
0
                                }
2103
0
                                break;
2104
0
                        case 4:
2105
0
                                {
2106
0
                                        OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2107
0
                                        OPJ_INT32 * l_dest_ptr = l_tilec->data;
2108
2109
0
                                        for (j=0;j<l_nb_elem;++j) {
2110
0
                                                *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2111
0
                                        }
2112
2113
0
                                        p_src = (OPJ_BYTE*) l_src_ptr;
2114
0
                                }
2115
0
                                break;
2116
0
                }
2117
2118
0
                ++l_img_comp;
2119
0
                ++l_tilec;
2120
0
        }
2121
2122
0
        return OPJ_TRUE;
2123
0
}