2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2001-2003, David Janssens
10 * Copyright (c) 2002-2003, Yannick Verschueren
11 * Copyright (c) 2003-2007, Francois-Olivier Devaux
12 * Copyright (c) 2003-2014, Antonin Descampe
13 * Copyright (c) 2005, Herve Drolon, FreeImage Team
14 * Copyright (c) 2006-2007, Parvatha Elangovan
15 * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
16 * Copyright (c) 2012, CS Systemes d'Information, France
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opj_includes.h"
43 /* ----------------------------------------------------------------------- */
47 void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
48 int tileno, compno, resno, bandno, precno;/*, cblkno;*/
50 fprintf(fd, "image {\n");
51 fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
52 img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
54 for (tileno = 0; tileno < img->th * img->tw; tileno++) {
55 opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
56 fprintf(fd, " tile {\n");
57 fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
58 tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
59 for (compno = 0; compno < tile->numcomps; compno++) {
60 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
61 fprintf(fd, " tilec {\n");
63 " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
64 tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
65 for (resno = 0; resno < tilec->numresolutions; resno++) {
66 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
67 fprintf(fd, "\n res {\n");
69 " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
70 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
71 for (bandno = 0; bandno < res->numbands; bandno++) {
72 opj_tcd_band_t *band = &res->bands[bandno];
73 fprintf(fd, " band {\n");
75 " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
76 band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
77 for (precno = 0; precno < res->pw * res->ph; precno++) {
78 opj_tcd_precinct_t *prec = &band->precincts[precno];
79 fprintf(fd, " prec {\n");
81 " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
82 prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
84 for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
85 opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
86 fprintf(fd, " cblk {\n");
88 " x0=%d, y0=%d, x1=%d, y1=%d\n",
89 cblk->x0, cblk->y0, cblk->x1, cblk->y1);
107 * Allocates memory for a decoding code block.
109 static OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block);
112 * Deallocates the decoding data of the given precinct.
114 static void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct);
117 * Allocates memory for an encoding code block.
119 static OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block);
122 * Deallocates the encoding data of the given precinct.
124 static void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct);
128 Free the memory allocated for encoding
129 @param tcd TCD handle
131 static void opj_tcd_free_tile(opj_tcd_t *tcd);
134 static OPJ_BOOL opj_tcd_t2_decode ( opj_tcd_t *p_tcd,
135 OPJ_BYTE * p_src_data,
136 OPJ_UINT32 * p_data_read,
137 OPJ_UINT32 p_max_src_size,
138 opj_codestream_index_t *p_cstr_index );
140 static OPJ_BOOL opj_tcd_t1_decode (opj_tcd_t *p_tcd);
142 static OPJ_BOOL opj_tcd_dwt_decode (opj_tcd_t *p_tcd);
144 static OPJ_BOOL opj_tcd_mct_decode (opj_tcd_t *p_tcd);
146 static OPJ_BOOL opj_tcd_dc_level_shift_decode (opj_tcd_t *p_tcd);
149 static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd );
151 static OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd );
153 static OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd );
155 static OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd );
157 static OPJ_BOOL opj_tcd_t2_encode ( opj_tcd_t *p_tcd,
158 OPJ_BYTE * p_dest_data,
159 OPJ_UINT32 * p_data_written,
160 OPJ_UINT32 p_max_dest_size,
161 opj_codestream_info_t *p_cstr_info );
163 static OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
164 OPJ_BYTE * p_dest_data,
165 OPJ_UINT32 p_max_dest_size,
166 opj_codestream_info_t *p_cstr_info );
168 /* ----------------------------------------------------------------------- */
171 Create a new TCD handle
173 opj_tcd_t* opj_tcd_create(OPJ_BOOL p_is_decoder)
175 opj_tcd_t *l_tcd = 00;
177 /* create the tcd structure */
178 l_tcd = (opj_tcd_t*) opj_calloc(1,sizeof(opj_tcd_t));
183 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
185 l_tcd->tcd_image = (opj_tcd_image_t*)opj_calloc(1,sizeof(opj_tcd_image_t));
186 if (!l_tcd->tcd_image) {
195 /* ----------------------------------------------------------------------- */
197 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
200 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
201 opj_tcd_makelayer_fixed(tcd, layno, 1);
206 void opj_tcd_makelayer( opj_tcd_t *tcd,
211 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
214 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
216 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
218 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
219 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
221 for (resno = 0; resno < tilec->numresolutions; resno++) {
222 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
224 for (bandno = 0; bandno < res->numbands; bandno++) {
225 opj_tcd_band_t *band = &res->bands[bandno];
227 for (precno = 0; precno < res->pw * res->ph; precno++) {
228 opj_tcd_precinct_t *prc = &band->precincts[precno];
230 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
231 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
232 opj_tcd_layer_t *layer = &cblk->layers[layno];
236 cblk->numpassesinlayers = 0;
239 n = cblk->numpassesinlayers;
241 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
244 opj_tcd_pass_t *pass = &cblk->passes[passno];
248 dd = pass->distortiondec;
250 dr = pass->rate - cblk->passes[n - 1].rate;
251 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
259 if (dd / dr >= thresh)
263 layer->numpasses = n - cblk->numpassesinlayers;
265 if (!layer->numpasses) {
270 if (cblk->numpassesinlayers == 0) {
271 layer->len = cblk->passes[n - 1].rate;
272 layer->data = cblk->data;
273 layer->disto = cblk->passes[n - 1].distortiondec;
275 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
276 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
277 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
280 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
283 cblk->numpassesinlayers = n;
291 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
292 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
293 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
294 OPJ_INT32 matrice[10][10][3];
297 opj_cp_t *cp = tcd->cp;
298 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
299 opj_tcp_t *tcd_tcp = tcd->tcp;
301 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
302 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
304 for (i = 0; i < tcd_tcp->numlayers; i++) {
305 for (j = 0; j < tilec->numresolutions; j++) {
306 for (k = 0; k < 3; k++) {
308 (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
309 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
314 for (resno = 0; resno < tilec->numresolutions; resno++) {
315 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
317 for (bandno = 0; bandno < res->numbands; bandno++) {
318 opj_tcd_band_t *band = &res->bands[bandno];
320 for (precno = 0; precno < res->pw * res->ph; precno++) {
321 opj_tcd_precinct_t *prc = &band->precincts[precno];
323 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
324 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
325 opj_tcd_layer_t *layer = &cblk->layers[layno];
327 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
329 /* Correction of the matrix of coefficient to include the IMSB information */
331 value = matrice[layno][resno][bandno];
338 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
339 if (imsb >= matrice[layno - 1][resno][bandno]) {
340 value -= (imsb - matrice[layno - 1][resno][bandno]);
348 cblk->numpassesinlayers = 0;
351 n = cblk->numpassesinlayers;
352 if (cblk->numpassesinlayers == 0) {
354 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
356 n = cblk->numpassesinlayers;
359 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
362 layer->numpasses = n - cblk->numpassesinlayers;
364 if (!layer->numpasses)
367 if (cblk->numpassesinlayers == 0) {
368 layer->len = cblk->passes[n - 1].rate;
369 layer->data = cblk->data;
371 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
372 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
376 cblk->numpassesinlayers = n;
384 OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
386 OPJ_UINT32 * p_data_written,
388 opj_codestream_info_t *cstr_info)
390 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
392 OPJ_FLOAT64 min, max;
393 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
394 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
395 OPJ_FLOAT64 maxSE = 0;
397 opj_cp_t *cp = tcd->cp;
398 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
399 opj_tcp_t *tcd_tcp = tcd->tcp;
404 tcd_tile->numpix = 0; /* fixed_quality */
406 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
407 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
410 for (resno = 0; resno < tilec->numresolutions; resno++) {
411 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
413 for (bandno = 0; bandno < res->numbands; bandno++) {
414 opj_tcd_band_t *band = &res->bands[bandno];
416 for (precno = 0; precno < res->pw * res->ph; precno++) {
417 opj_tcd_precinct_t *prc = &band->precincts[precno];
419 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
420 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
422 for (passno = 0; passno < cblk->totalpasses; passno++) {
423 opj_tcd_pass_t *pass = &cblk->passes[passno];
425 OPJ_FLOAT64 dd, rdslope;
428 dr = (OPJ_INT32)pass->rate;
429 dd = pass->distortiondec;
431 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
432 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
450 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
451 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
457 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
458 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
459 * ((OPJ_FLOAT64)(tilec->numpix));
464 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
465 tile_info->numpix = tcd_tile->numpix;
466 tile_info->distotile = tcd_tile->distotile;
467 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
468 if (!tile_info->thresh) {
469 /* FIXME event manager error callback */
474 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
475 OPJ_FLOAT64 lo = min;
476 OPJ_FLOAT64 hi = max;
477 OPJ_BOOL success = OPJ_FALSE;
478 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
479 OPJ_FLOAT64 goodthresh = 0;
480 OPJ_FLOAT64 stable_thresh = 0;
482 OPJ_FLOAT64 distotarget; /* fixed_quality */
485 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
487 /* Don't try to find an optimal threshold but rather take everything not included yet, if
488 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
489 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
490 ==> possible to have some lossy layers and the last layer for sure lossless */
491 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))) {
492 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
493 OPJ_FLOAT64 thresh = 0;
499 for (i = 0; i < 128; ++i) {
500 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
502 thresh = (lo + hi) / 2;
504 opj_tcd_makelayer(tcd, layno, thresh, 0);
506 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
507 if(OPJ_IS_CINEMA(cp->rsiz)){
508 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)) {
514 distoachieved = layno == 0 ?
515 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
517 if (distoachieved < distotarget) {
519 stable_thresh = thresh;
526 distoachieved = (layno == 0) ?
527 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
529 if (distoachieved < distotarget) {
531 stable_thresh = thresh;
537 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))
539 /* TODO: what to do with l ??? seek / tell ??? */
540 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
546 stable_thresh = thresh;
551 goodthresh = stable_thresh == 0? thresh : stable_thresh;
563 if(cstr_info) { /* Threshold for Marcela Index */
564 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
567 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
570 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
576 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
577 opj_image_t * p_image,
580 p_tcd->image = p_image;
583 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_calloc(1,sizeof(opj_tcd_tile_t));
584 if (! p_tcd->tcd_image->tiles) {
588 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_calloc(p_image->numcomps,sizeof(opj_tcd_tilecomp_t));
589 if (! p_tcd->tcd_image->tiles->comps ) {
593 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
594 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
600 Destroy a previously created TCD handle
602 void opj_tcd_destroy(opj_tcd_t *tcd) {
604 opj_tcd_free_tile(tcd);
606 if (tcd->tcd_image) {
607 opj_free(tcd->tcd_image);
614 /* ----------------------------------------------------------------------- */
615 #define OPJ_MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
616 OPJ_BOOL FUNCTION ( opj_tcd_t *p_tcd, \
617 OPJ_UINT32 p_tile_no \
620 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
621 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
622 opj_tcp_t * l_tcp = 00; \
623 opj_cp_t * l_cp = 00; \
624 opj_tcd_tile_t * l_tile = 00; \
625 opj_tccp_t *l_tccp = 00; \
626 opj_tcd_tilecomp_t *l_tilec = 00; \
627 opj_image_comp_t * l_image_comp = 00; \
628 opj_tcd_resolution_t *l_res = 00; \
629 opj_tcd_band_t *l_band = 00; \
630 opj_stepsize_t * l_step_size = 00; \
631 opj_tcd_precinct_t *l_current_precinct = 00; \
632 TYPE* l_code_block = 00; \
633 opj_image_t *l_image = 00; \
635 OPJ_UINT32 l_level_no; \
636 OPJ_UINT32 l_pdx, l_pdy; \
638 OPJ_INT32 l_x0b, l_y0b; \
639 /* extent of precincts , top left, bottom right**/ \
640 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
641 /* number of precinct for a resolution */ \
642 OPJ_UINT32 l_nb_precincts; \
643 /* room needed to store l_nb_precinct precinct for a resolution */ \
644 OPJ_UINT32 l_nb_precinct_size; \
645 /* number of code blocks for a precinct*/ \
646 OPJ_UINT32 l_nb_code_blocks; \
647 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
648 OPJ_UINT32 l_nb_code_blocks_size; \
649 /* size of data for a tile */ \
650 OPJ_UINT32 l_data_size; \
653 l_tcp = &(l_cp->tcps[p_tile_no]); \
654 l_tile = p_tcd->tcd_image->tiles; \
655 l_tccp = l_tcp->tccps; \
656 l_tilec = l_tile->comps; \
657 l_image = p_tcd->image; \
658 l_image_comp = p_tcd->image->comps; \
660 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
661 q = p_tile_no / l_cp->tw; \
662 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
664 /* 4 borders of the tile rescale on the image if necessary */ \
665 l_tile->x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + p * l_cp->tdx), (OPJ_INT32)l_image->x0); \
666 l_tile->y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + q * l_cp->tdy), (OPJ_INT32)l_image->y0); \
667 l_tile->x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (p + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1); \
668 l_tile->y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (q + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1); \
669 /* testcase 1888.pdf.asan.35.988 */ \
670 if (l_tccp->numresolutions == 0) { \
671 fprintf(stderr, "tiles require at least one resolution\n"); \
674 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \
676 /*tile->numcomps = image->numcomps; */ \
677 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
678 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
679 l_image_comp->resno_decoded = 0; \
680 /* border of each l_tile component (global) */ \
681 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx); \
682 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy); \
683 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx); \
684 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy); \
685 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \
687 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) \
688 * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0) * (OPJ_UINT32)sizeof(OPJ_UINT32 );\
689 l_tilec->numresolutions = l_tccp->numresolutions; \
690 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
691 l_tilec->minimum_num_resolutions = 1; \
694 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
695 - l_cp->m_specific_param.m_dec.m_reduce; \
698 if (l_tilec->data == 00) { \
699 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
700 if (! l_tilec->data ) { \
703 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
705 l_tilec->data_size = l_data_size; \
707 else if (l_data_size > l_tilec->data_size) { \
708 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
709 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \
710 fprintf(stderr, "Not enough memory to handle tile data\n"); \
712 opj_free(l_tilec->data); \
713 l_tilec->data = NULL; \
714 l_tilec->data_size = 0; \
717 l_tilec->data = new_data; \
718 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
719 l_tilec->data_size = l_data_size; \
722 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t); \
724 if (l_tilec->resolutions == 00) { \
725 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); \
726 if (! l_tilec->resolutions ) { \
729 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/ \
730 l_tilec->resolutions_size = l_data_size; \
731 memset(l_tilec->resolutions,0,l_data_size); \
733 else if (l_data_size > l_tilec->resolutions_size) { \
734 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
735 if (! new_resolutions) { \
736 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \
737 fprintf(stderr, "Not enough memory to tile resolutions\n"); \
738 opj_free(l_tilec->resolutions); \
739 l_tilec->resolutions = NULL; \
740 l_tilec->resolutions_size = 0; \
743 l_tilec->resolutions = new_resolutions; \
744 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
745 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
746 l_tilec->resolutions_size = l_data_size; \
749 l_level_no = l_tilec->numresolutions - 1; \
750 l_res = l_tilec->resolutions; \
751 l_step_size = l_tccp->stepsizes; \
752 if (l_tccp->qmfbid == 0) { \
753 l_gain_ptr = &opj_dwt_getgain_real; \
756 l_gain_ptr = &opj_dwt_getgain; \
758 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
760 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
761 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
762 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/; \
763 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
764 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
766 /* border for each resolution level (global) */ \
767 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
768 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
769 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
770 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
771 /*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);*/ \
772 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
773 l_pdx = l_tccp->prcw[resno]; \
774 l_pdy = l_tccp->prch[resno]; \
775 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
776 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
777 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx; \
778 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy; \
779 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx; \
780 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy; \
781 /*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 );*/ \
783 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
784 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
785 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
787 l_nb_precincts = l_res->pw * l_res->ph; \
788 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t); \
790 tlcbgxstart = l_tl_prc_x_start; \
791 tlcbgystart = l_tl_prc_y_start; \
792 /*brcbgxend = l_br_prc_x_end;*/ \
793 /* brcbgyend = l_br_prc_y_end;*/ \
794 cbgwidthexpn = l_pdx; \
795 cbgheightexpn = l_pdy; \
796 l_res->numbands = 1; \
799 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1); \
800 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1); \
801 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/ \
802 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/ \
803 cbgwidthexpn = l_pdx - 1; \
804 cbgheightexpn = l_pdy - 1; \
805 l_res->numbands = 3; \
808 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn); \
809 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn); \
810 l_band = l_res->bands; \
812 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
814 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
817 l_band->bandno = 0 ; \
818 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
819 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
820 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
821 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
824 l_band->bandno = bandno + 1; \
825 /* x0b = 1 if bandno = 1 or 3 */ \
826 l_x0b = l_band->bandno&1; \
827 /* y0b = 1 if bandno = 2 or 3 */ \
828 l_y0b = (OPJ_INT32)((l_band->bandno)>>1); \
829 /* l_band border (global) */ \
830 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
831 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
832 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
833 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
836 /** avoid an if with storing function pointer */ \
837 l_gain = (*l_gain_ptr) (l_band->bandno); \
838 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain); \
839 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \
840 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */ \
842 if (! l_band->precincts) { \
843 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \
844 if (! l_band->precincts) { \
847 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */ \
848 memset(l_band->precincts,0,l_nb_precinct_size); \
849 l_band->precincts_data_size = l_nb_precinct_size; \
851 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
853 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \
854 if (! new_precincts) { \
855 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \
856 fprintf(stderr, "Not enough memory to handle band precints\n"); \
857 opj_free(l_band->precincts); \
858 l_band->precincts = NULL; \
859 l_band->precincts_data_size = 0; \
862 l_band->precincts = new_precincts; \
863 /*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);*/ \
864 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \
865 l_band->precincts_data_size = l_nb_precinct_size; \
868 l_current_precinct = l_band->precincts; \
869 for (precno = 0; precno < l_nb_precincts; ++precno) { \
870 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
871 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn); \
872 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn); \
873 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
874 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
875 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \
876 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \
878 /* precinct size (global) */ \
879 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
881 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0); \
882 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0); \
883 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1); \
884 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1); \
885 /*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);*/ \
887 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
888 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
889 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
890 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
891 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
892 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
893 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
894 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
895 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn); \
896 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn); \
898 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
899 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
900 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof(TYPE); \
902 if (! l_current_precinct->cblks.ELEMENT) { \
903 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \
904 if (! l_current_precinct->cblks.ELEMENT ) { \
907 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/ \
909 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \
911 l_current_precinct->block_size = l_nb_code_blocks_size; \
913 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
914 TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
915 if (! new_ELEMENT) { \
916 opj_free(l_current_precinct->cblks.ELEMENT); \
917 l_current_precinct->cblks.ELEMENT = NULL; \
918 l_current_precinct->block_size = 0; \
919 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \
920 fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \
923 l_current_precinct->cblks.ELEMENT = new_ELEMENT; \
924 /*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); */\
926 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
928 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
930 l_current_precinct->block_size = l_nb_code_blocks_size; \
933 if (! l_current_precinct->incltree) { \
934 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, \
935 l_current_precinct->ch); \
938 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, \
939 l_current_precinct->cw, \
940 l_current_precinct->ch); \
943 if (! l_current_precinct->incltree) { \
944 fprintf(stderr, "WARNING: No incltree created.\n"); \
945 /*return OPJ_FALSE;*/ \
948 if (! l_current_precinct->imsbtree) { \
949 l_current_precinct->imsbtree = opj_tgt_create( \
950 l_current_precinct->cw, \
951 l_current_precinct->ch); \
954 l_current_precinct->imsbtree = opj_tgt_init( \
955 l_current_precinct->imsbtree, \
956 l_current_precinct->cw, \
957 l_current_precinct->ch); \
960 if (! l_current_precinct->imsbtree) { \
961 fprintf(stderr, "WARNING: No imsbtree created.\n"); \
962 /*return OPJ_FALSE;*/ \
965 l_code_block = l_current_precinct->cblks.ELEMENT; \
967 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
968 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
969 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
970 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
971 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
973 if (! FUNCTION_ELEMENT(l_code_block)) { \
976 /* code-block size (global) */ \
977 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); \
978 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); \
979 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); \
980 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); \
983 ++l_current_precinct; \
999 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
1000 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
1002 #undef OPJ_MACRO_TCD_ALLOCATE
1005 * Allocates memory for an encoding code block.
1007 OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1009 if (! p_code_block->data) {
1011 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
1012 if(! p_code_block->data) {
1016 p_code_block->data[0] = 0;
1017 p_code_block->data+=1;
1019 /* no memset since data */
1020 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
1021 if (! p_code_block->layers) {
1025 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
1026 if (! p_code_block->passes) {
1035 * Allocates memory for a decoding code block.
1037 OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1039 if (! p_code_block->data) {
1041 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1042 if (! p_code_block->data) {
1045 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1046 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1048 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t));
1049 if (! p_code_block->segs) {
1052 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1054 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1055 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1058 OPJ_BYTE* l_data = p_code_block->data;
1059 OPJ_UINT32 l_data_max_size = p_code_block->data_max_size;
1060 opj_tcd_seg_t * l_segs = p_code_block->segs;
1061 OPJ_UINT32 l_current_max_segs = p_code_block->m_current_max_segs;
1063 memset(p_code_block, 0, sizeof(opj_tcd_cblk_dec_t));
1064 p_code_block->data = l_data;
1065 p_code_block->data_max_size = l_data_max_size;
1066 p_code_block->segs = l_segs;
1067 p_code_block->m_current_max_segs = l_current_max_segs;
1073 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1076 OPJ_UINT32 l_data_size = 0;
1077 opj_image_comp_t * l_img_comp = 00;
1078 opj_tcd_tilecomp_t * l_tile_comp = 00;
1079 opj_tcd_resolution_t * l_res = 00;
1080 OPJ_UINT32 l_size_comp, l_remaining;
1082 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1083 l_img_comp = p_tcd->image->comps;
1085 for (i=0;i<p_tcd->image->numcomps;++i) {
1086 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1087 l_remaining = l_img_comp->prec & 7; /* (%8) */
1093 if (l_size_comp == 3) {
1097 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1098 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1106 OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1107 OPJ_UINT32 p_tile_no,
1109 OPJ_UINT32 * p_data_written,
1110 OPJ_UINT32 p_max_length,
1111 opj_codestream_info_t *p_cstr_info)
1114 if (p_tcd->cur_tp_num == 0) {
1116 p_tcd->tcd_tileno = p_tile_no;
1117 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1119 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1121 OPJ_UINT32 l_num_packs = 0;
1123 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1124 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1126 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1127 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1129 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1130 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1132 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1133 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1134 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1136 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));
1137 if (!p_cstr_info->tile[p_tile_no].packet) {
1138 /* FIXME event manager error callback */
1144 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1145 /*---------------TILE-------------------*/
1146 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1149 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1151 /* FIXME _ProfStart(PGROUP_MCT); */
1152 if (! opj_tcd_mct_encode(p_tcd)) {
1155 /* FIXME _ProfStop(PGROUP_MCT); */
1157 /* FIXME _ProfStart(PGROUP_DWT); */
1158 if (! opj_tcd_dwt_encode(p_tcd)) {
1161 /* FIXME _ProfStop(PGROUP_DWT); */
1163 /* FIXME _ProfStart(PGROUP_T1); */
1164 if (! opj_tcd_t1_encode(p_tcd)) {
1167 /* FIXME _ProfStop(PGROUP_T1); */
1169 /* FIXME _ProfStart(PGROUP_RATE); */
1170 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1173 /* FIXME _ProfStop(PGROUP_RATE); */
1176 /*--------------TIER2------------------*/
1180 p_cstr_info->index_write = 1;
1182 /* FIXME _ProfStart(PGROUP_T2); */
1184 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1187 /* FIXME _ProfStop(PGROUP_T2); */
1189 /*---------------CLEAN-------------------*/
1194 OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1196 OPJ_UINT32 p_max_length,
1197 OPJ_UINT32 p_tile_no,
1198 opj_codestream_index_t *p_cstr_index
1201 OPJ_UINT32 l_data_read;
1202 p_tcd->tcd_tileno = p_tile_no;
1203 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1205 #ifdef TODO_MSD /* FIXME */
1208 OPJ_UINT32 resno, compno, numprec = 0;
1209 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1210 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1211 opj_tccp_t *tccp = &tcp->tccps[compno];
1212 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1213 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1214 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1215 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1216 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1217 numprec += res_idx->pw * res_idx->ph;
1218 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1219 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1222 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1223 p_cstr_info->packno = 0;
1228 /*--------------TIER2------------------*/
1229 /* FIXME _ProfStart(PGROUP_T2); */
1231 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1235 /* FIXME _ProfStop(PGROUP_T2); */
1237 /*------------------TIER1-----------------*/
1239 /* FIXME _ProfStart(PGROUP_T1); */
1241 (! opj_tcd_t1_decode(p_tcd))
1245 /* FIXME _ProfStop(PGROUP_T1); */
1247 /*----------------DWT---------------------*/
1249 /* FIXME _ProfStart(PGROUP_DWT); */
1251 (! opj_tcd_dwt_decode(p_tcd))
1255 /* FIXME _ProfStop(PGROUP_DWT); */
1257 /*----------------MCT-------------------*/
1258 /* FIXME _ProfStart(PGROUP_MCT); */
1260 (! opj_tcd_mct_decode(p_tcd))
1264 /* FIXME _ProfStop(PGROUP_MCT); */
1266 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1268 (! opj_tcd_dc_level_shift_decode(p_tcd))
1272 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1275 /*---------------TILE-------------------*/
1279 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1281 OPJ_UINT32 p_dest_length
1284 OPJ_UINT32 i,j,k,l_data_size = 0;
1285 opj_image_comp_t * l_img_comp = 00;
1286 opj_tcd_tilecomp_t * l_tilec = 00;
1287 opj_tcd_resolution_t * l_res;
1288 OPJ_UINT32 l_size_comp, l_remaining;
1289 OPJ_UINT32 l_stride, l_width,l_height;
1291 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1292 if (l_data_size > p_dest_length) {
1296 l_tilec = p_tcd->tcd_image->tiles->comps;
1297 l_img_comp = p_tcd->image->comps;
1299 for (i=0;i<p_tcd->image->numcomps;++i) {
1300 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1301 l_remaining = l_img_comp->prec & 7; /* (%8) */
1302 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1303 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1304 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1305 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1311 if (l_size_comp == 3) {
1315 switch (l_size_comp)
1319 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1320 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1322 if (l_img_comp->sgnd) {
1323 for (j=0;j<l_height;++j) {
1324 for (k=0;k<l_width;++k) {
1325 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1327 l_src_ptr += l_stride;
1331 for (j=0;j<l_height;++j) {
1332 for (k=0;k<l_width;++k) {
1333 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1335 l_src_ptr += l_stride;
1339 p_dest = (OPJ_BYTE *)l_dest_ptr;
1344 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1345 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1347 if (l_img_comp->sgnd) {
1348 for (j=0;j<l_height;++j) {
1349 for (k=0;k<l_width;++k) {
1350 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1352 l_src_ptr += l_stride;
1356 for (j=0;j<l_height;++j) {
1357 for (k=0;k<l_width;++k) {
1358 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1360 l_src_ptr += l_stride;
1364 p_dest = (OPJ_BYTE*) l_dest_ptr;
1369 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1370 OPJ_INT32 * l_src_ptr = l_tilec->data;
1372 for (j=0;j<l_height;++j) {
1373 for (k=0;k<l_width;++k) {
1374 *(l_dest_ptr++) = (*(l_src_ptr++));
1376 l_src_ptr += l_stride;
1379 p_dest = (OPJ_BYTE*) l_dest_ptr;
1394 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1396 OPJ_UINT32 compno, resno, bandno, precno;
1397 opj_tcd_tile_t *l_tile = 00;
1398 opj_tcd_tilecomp_t *l_tile_comp = 00;
1399 opj_tcd_resolution_t *l_res = 00;
1400 opj_tcd_band_t *l_band = 00;
1401 opj_tcd_precinct_t *l_precinct = 00;
1402 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1403 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1409 if (! p_tcd->tcd_image) {
1413 if (p_tcd->m_is_decoder) {
1414 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1417 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1420 l_tile = p_tcd->tcd_image->tiles;
1425 l_tile_comp = l_tile->comps;
1427 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1428 l_res = l_tile_comp->resolutions;
1431 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1432 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1433 l_band = l_res->bands;
1434 for (bandno = 0; bandno < 3; ++bandno) {
1435 l_precinct = l_band->precincts;
1438 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1439 for (precno = 0; precno < l_nb_precincts; ++precno) {
1440 opj_tgt_destroy(l_precinct->incltree);
1441 l_precinct->incltree = 00;
1442 opj_tgt_destroy(l_precinct->imsbtree);
1443 l_precinct->imsbtree = 00;
1444 (*l_tcd_code_block_deallocate) (l_precinct);
1448 opj_free(l_band->precincts);
1449 l_band->precincts = 00;
1456 opj_free(l_tile_comp->resolutions);
1457 l_tile_comp->resolutions = 00;
1460 if (l_tile_comp->data) {
1461 opj_free(l_tile_comp->data);
1462 l_tile_comp->data = 00;
1467 opj_free(l_tile->comps);
1469 opj_free(p_tcd->tcd_image->tiles);
1470 p_tcd->tcd_image->tiles = 00;
1474 OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1475 OPJ_BYTE * p_src_data,
1476 OPJ_UINT32 * p_data_read,
1477 OPJ_UINT32 p_max_src_size,
1478 opj_codestream_index_t *p_cstr_index
1483 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1488 if (! opj_t2_decode_packets(
1491 p_tcd->tcd_image->tiles,
1496 opj_t2_destroy(l_t2);
1500 opj_t2_destroy(l_t2);
1502 /*---------------CLEAN-------------------*/
1506 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1510 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1511 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1512 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1515 l_t1 = opj_t1_create();
1520 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1521 /* The +3 is headroom required by the vectorized DWT */
1522 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1523 opj_t1_destroy(l_t1);
1530 opj_t1_destroy(l_t1);
1536 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1539 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1540 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1541 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1542 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1544 for (compno = 0; compno < l_tile->numcomps; compno++) {
1546 if (tcd->cp->reduce != 0) {
1547 tcd->image->comps[compno].resno_decoded =
1548 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1549 if (tcd->image->comps[compno].resno_decoded < 0)
1554 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1555 if(numres2decode > 0){
1558 if (l_tccp->qmfbid == 1) {
1559 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1564 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1576 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1578 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1579 opj_tcp_t * l_tcp = p_tcd->tcp;
1580 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1581 OPJ_UINT32 l_samples,i;
1587 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1589 if (l_tile->numcomps >= 3 ){
1590 /* testcase 1336.pdf.asan.47.376 */
1591 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 ||
1592 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1593 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1594 fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1597 else if (l_tcp->mct == 2) {
1600 if (! l_tcp->m_mct_decoding_matrix) {
1604 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1609 for (i=0;i<l_tile->numcomps;++i) {
1610 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1614 if (! opj_mct_decode_custom(/* MCT data */
1615 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1616 /* size of components */
1620 /* nb of components (i.e. size of pData) */
1622 /* tells if the data is signed */
1623 p_tcd->image->comps->sgnd)) {
1631 if (l_tcp->tccps->qmfbid == 1) {
1632 opj_mct_decode( l_tile->comps[0].data,
1633 l_tile->comps[1].data,
1634 l_tile->comps[2].data,
1638 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1639 (OPJ_FLOAT32*)l_tile->comps[1].data,
1640 (OPJ_FLOAT32*)l_tile->comps[2].data,
1646 /* FIXME need to use opj_event_msg function */
1647 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1654 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1657 opj_tcd_tilecomp_t * l_tile_comp = 00;
1658 opj_tccp_t * l_tccp = 00;
1659 opj_image_comp_t * l_img_comp = 00;
1660 opj_tcd_resolution_t* l_res = 00;
1661 opj_tcd_tile_t * l_tile;
1662 OPJ_UINT32 l_width,l_height,i,j;
1663 OPJ_INT32 * l_current_ptr;
1664 OPJ_INT32 l_min, l_max;
1665 OPJ_UINT32 l_stride;
1667 l_tile = p_tcd->tcd_image->tiles;
1668 l_tile_comp = l_tile->comps;
1669 l_tccp = p_tcd->tcp->tccps;
1670 l_img_comp = p_tcd->image->comps;
1672 for (compno = 0; compno < l_tile->numcomps; compno++) {
1673 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1674 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1675 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1676 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1678 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1680 if (l_img_comp->sgnd) {
1681 l_min = -(1 << (l_img_comp->prec - 1));
1682 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1686 l_max = (1 << l_img_comp->prec) - 1;
1689 l_current_ptr = l_tile_comp->data;
1691 if (l_tccp->qmfbid == 1) {
1692 for (j=0;j<l_height;++j) {
1693 for (i = 0; i < l_width; ++i) {
1694 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1697 l_current_ptr += l_stride;
1701 for (j=0;j<l_height;++j) {
1702 for (i = 0; i < l_width; ++i) {
1703 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1704 *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1707 l_current_ptr += l_stride;
1722 * Deallocates the encoding data of the given precinct.
1724 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1726 OPJ_UINT32 cblkno , l_nb_code_blocks;
1728 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1730 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1731 /*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);*/
1732 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1733 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 );*/
1736 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1737 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1739 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1741 if (l_code_block->data) {
1742 opj_free(l_code_block->data);
1743 l_code_block->data = 00;
1746 if (l_code_block->segs) {
1747 opj_free(l_code_block->segs );
1748 l_code_block->segs = 00;
1754 opj_free(p_precinct->cblks.dec);
1755 p_precinct->cblks.dec = 00;
1760 * Deallocates the encoding data of the given precinct.
1762 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1764 OPJ_UINT32 cblkno , l_nb_code_blocks;
1766 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1768 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1770 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1771 if (l_code_block->data) {
1772 opj_free(l_code_block->data - 1);
1773 l_code_block->data = 00;
1776 if (l_code_block->layers) {
1777 opj_free(l_code_block->layers );
1778 l_code_block->layers = 00;
1781 if (l_code_block->passes) {
1782 opj_free(l_code_block->passes );
1783 l_code_block->passes = 00;
1788 opj_free(p_precinct->cblks.enc);
1790 p_precinct->cblks.enc = 00;
1794 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1796 OPJ_UINT32 i,l_data_size = 0;
1797 opj_image_comp_t * l_img_comp = 00;
1798 opj_tcd_tilecomp_t * l_tilec = 00;
1799 OPJ_UINT32 l_size_comp, l_remaining;
1801 l_tilec = p_tcd->tcd_image->tiles->comps;
1802 l_img_comp = p_tcd->image->comps;
1803 for (i=0;i<p_tcd->image->numcomps;++i) {
1804 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1805 l_remaining = l_img_comp->prec & 7; /* (%8) */
1811 if (l_size_comp == 3) {
1815 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1823 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1826 opj_tcd_tilecomp_t * l_tile_comp = 00;
1827 opj_tccp_t * l_tccp = 00;
1828 opj_image_comp_t * l_img_comp = 00;
1829 opj_tcd_tile_t * l_tile;
1830 OPJ_UINT32 l_nb_elem,i;
1831 OPJ_INT32 * l_current_ptr;
1833 l_tile = p_tcd->tcd_image->tiles;
1834 l_tile_comp = l_tile->comps;
1835 l_tccp = p_tcd->tcp->tccps;
1836 l_img_comp = p_tcd->image->comps;
1838 for (compno = 0; compno < l_tile->numcomps; compno++) {
1839 l_current_ptr = l_tile_comp->data;
1840 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1842 if (l_tccp->qmfbid == 1) {
1843 for (i = 0; i < l_nb_elem; ++i) {
1844 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1849 for (i = 0; i < l_nb_elem; ++i) {
1850 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1863 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1865 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1866 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1867 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1869 OPJ_BYTE ** l_data = 00;
1870 opj_tcp_t * l_tcp = p_tcd->tcp;
1872 if(!p_tcd->tcp->mct) {
1876 if (p_tcd->tcp->mct == 2) {
1877 if (! p_tcd->tcp->m_mct_coding_matrix) {
1881 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1886 for (i=0;i<l_tile->numcomps;++i) {
1887 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1891 if (! opj_mct_encode_custom(/* MCT data */
1892 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1893 /* size of components */
1897 /* nb of components (i.e. size of pData) */
1899 /* tells if the data is signed */
1900 p_tcd->image->comps->sgnd) )
1908 else if (l_tcp->tccps->qmfbid == 0) {
1909 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1912 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1918 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1920 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1921 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1922 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1925 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1926 if (l_tccp->qmfbid == 1) {
1927 if (! opj_dwt_encode(l_tile_comp)) {
1931 else if (l_tccp->qmfbid == 0) {
1932 if (! opj_dwt_encode_real(l_tile_comp)) {
1944 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1947 const OPJ_FLOAT64 * l_mct_norms;
1948 opj_tcp_t * l_tcp = p_tcd->tcp;
1950 l_t1 = opj_t1_create();
1955 if (l_tcp->mct == 1) {
1956 /* irreversible encoding */
1957 if (l_tcp->tccps->qmfbid == 0) {
1958 l_mct_norms = opj_mct_get_mct_norms_real();
1961 l_mct_norms = opj_mct_get_mct_norms();
1965 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1968 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1969 opj_t1_destroy(l_t1);
1973 opj_t1_destroy(l_t1);
1978 OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
1979 OPJ_BYTE * p_dest_data,
1980 OPJ_UINT32 * p_data_written,
1981 OPJ_UINT32 p_max_dest_size,
1982 opj_codestream_info_t *p_cstr_info )
1986 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1991 if (! opj_t2_encode_packets(
1994 p_tcd->tcd_image->tiles,
1995 p_tcd->tcp->numlayers,
2005 opj_t2_destroy(l_t2);
2009 opj_t2_destroy(l_t2);
2011 /*---------------CLEAN-------------------*/
2016 OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
2017 OPJ_BYTE * p_dest_data,
2018 OPJ_UINT32 p_max_dest_size,
2019 opj_codestream_info_t *p_cstr_info )
2021 opj_cp_t * l_cp = p_tcd->cp;
2022 OPJ_UINT32 l_nb_written = 0;
2025 p_cstr_info->index_write = 0;
2028 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2030 /* Normal Rate/distortion allocation */
2031 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2036 /* Fixed layer allocation */
2037 opj_tcd_rateallocate_fixed(p_tcd);
2044 OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2046 OPJ_UINT32 p_src_length )
2048 OPJ_UINT32 i,j,l_data_size = 0;
2049 opj_image_comp_t * l_img_comp = 00;
2050 opj_tcd_tilecomp_t * l_tilec = 00;
2051 OPJ_UINT32 l_size_comp, l_remaining;
2052 OPJ_UINT32 l_nb_elem;
2054 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2055 if (l_data_size != p_src_length) {
2059 l_tilec = p_tcd->tcd_image->tiles->comps;
2060 l_img_comp = p_tcd->image->comps;
2061 for (i=0;i<p_tcd->image->numcomps;++i) {
2062 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2063 l_remaining = l_img_comp->prec & 7; /* (%8) */
2064 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2070 if (l_size_comp == 3) {
2074 switch (l_size_comp) {
2077 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2078 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2080 if (l_img_comp->sgnd) {
2081 for (j=0;j<l_nb_elem;++j) {
2082 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2086 for (j=0;j<l_nb_elem;++j) {
2087 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2091 p_src = (OPJ_BYTE*) l_src_ptr;
2096 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2097 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2099 if (l_img_comp->sgnd) {
2100 for (j=0;j<l_nb_elem;++j) {
2101 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2105 for (j=0;j<l_nb_elem;++j) {
2106 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2110 p_src = (OPJ_BYTE*) l_src_ptr;
2115 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2116 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2118 for (j=0;j<l_nb_elem;++j) {
2119 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2122 p_src = (OPJ_BYTE*) l_src_ptr;