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);*/ \
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 /* code-block size (global) */ \
974 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); \
975 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); \
976 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); \
977 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); \
979 if (! FUNCTION_ELEMENT(l_code_block)) { \
984 ++l_current_precinct; \
1000 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
1001 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
1003 #undef OPJ_MACRO_TCD_ALLOCATE
1006 * Allocates memory for an encoding code block.
1008 OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1010 if (! p_code_block->data) {
1012 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
1013 if(! p_code_block->data) {
1017 p_code_block->data[0] = 0;
1018 p_code_block->data+=1;
1020 /* no memset since data */
1021 p_code_block->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
1022 if (! p_code_block->layers) {
1026 p_code_block->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
1027 if (! p_code_block->passes) {
1036 * Allocates memory for a decoding code block.
1038 OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1040 if (! p_code_block->data) {
1042 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1043 if (! p_code_block->data) {
1046 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1047 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1049 p_code_block->segs = (opj_tcd_seg_t *) opj_calloc(OPJ_J2K_DEFAULT_NB_SEGS,sizeof(opj_tcd_seg_t));
1050 if (! p_code_block->segs) {
1053 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1055 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1056 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1059 /*p_code_block->numsegs = 0; */
1064 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1067 OPJ_UINT32 l_data_size = 0;
1068 opj_image_comp_t * l_img_comp = 00;
1069 opj_tcd_tilecomp_t * l_tile_comp = 00;
1070 opj_tcd_resolution_t * l_res = 00;
1071 OPJ_UINT32 l_size_comp, l_remaining;
1073 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1074 l_img_comp = p_tcd->image->comps;
1076 for (i=0;i<p_tcd->image->numcomps;++i) {
1077 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1078 l_remaining = l_img_comp->prec & 7; /* (%8) */
1084 if (l_size_comp == 3) {
1088 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1089 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1097 OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1098 OPJ_UINT32 p_tile_no,
1100 OPJ_UINT32 * p_data_written,
1101 OPJ_UINT32 p_max_length,
1102 opj_codestream_info_t *p_cstr_info)
1105 if (p_tcd->cur_tp_num == 0) {
1107 p_tcd->tcd_tileno = p_tile_no;
1108 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1110 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1112 OPJ_UINT32 l_num_packs = 0;
1114 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1115 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1117 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1118 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1120 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1121 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1123 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1124 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1125 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1127 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));
1128 if (!p_cstr_info->tile[p_tile_no].packet) {
1129 /* FIXME event manager error callback */
1135 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1136 /*---------------TILE-------------------*/
1137 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1140 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1142 /* FIXME _ProfStart(PGROUP_MCT); */
1143 if (! opj_tcd_mct_encode(p_tcd)) {
1146 /* FIXME _ProfStop(PGROUP_MCT); */
1148 /* FIXME _ProfStart(PGROUP_DWT); */
1149 if (! opj_tcd_dwt_encode(p_tcd)) {
1152 /* FIXME _ProfStop(PGROUP_DWT); */
1154 /* FIXME _ProfStart(PGROUP_T1); */
1155 if (! opj_tcd_t1_encode(p_tcd)) {
1158 /* FIXME _ProfStop(PGROUP_T1); */
1160 /* FIXME _ProfStart(PGROUP_RATE); */
1161 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1164 /* FIXME _ProfStop(PGROUP_RATE); */
1167 /*--------------TIER2------------------*/
1171 p_cstr_info->index_write = 1;
1173 /* FIXME _ProfStart(PGROUP_T2); */
1175 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1178 /* FIXME _ProfStop(PGROUP_T2); */
1180 /*---------------CLEAN-------------------*/
1185 OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1187 OPJ_UINT32 p_max_length,
1188 OPJ_UINT32 p_tile_no,
1189 opj_codestream_index_t *p_cstr_index
1192 OPJ_UINT32 l_data_read;
1193 p_tcd->tcd_tileno = p_tile_no;
1194 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1196 #ifdef TODO_MSD /* FIXME */
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];
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;
1219 /*--------------TIER2------------------*/
1220 /* FIXME _ProfStart(PGROUP_T2); */
1222 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1226 /* FIXME _ProfStop(PGROUP_T2); */
1228 /*------------------TIER1-----------------*/
1230 /* FIXME _ProfStart(PGROUP_T1); */
1232 (! opj_tcd_t1_decode(p_tcd))
1236 /* FIXME _ProfStop(PGROUP_T1); */
1238 /*----------------DWT---------------------*/
1240 /* FIXME _ProfStart(PGROUP_DWT); */
1242 (! opj_tcd_dwt_decode(p_tcd))
1246 /* FIXME _ProfStop(PGROUP_DWT); */
1248 /*----------------MCT-------------------*/
1249 /* FIXME _ProfStart(PGROUP_MCT); */
1251 (! opj_tcd_mct_decode(p_tcd))
1255 /* FIXME _ProfStop(PGROUP_MCT); */
1257 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1259 (! opj_tcd_dc_level_shift_decode(p_tcd))
1263 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1266 /*---------------TILE-------------------*/
1270 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1272 OPJ_UINT32 p_dest_length
1275 OPJ_UINT32 i,j,k,l_data_size = 0;
1276 opj_image_comp_t * l_img_comp = 00;
1277 opj_tcd_tilecomp_t * l_tilec = 00;
1278 opj_tcd_resolution_t * l_res;
1279 OPJ_UINT32 l_size_comp, l_remaining;
1280 OPJ_UINT32 l_stride, l_width,l_height;
1282 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1283 if (l_data_size > p_dest_length) {
1287 l_tilec = p_tcd->tcd_image->tiles->comps;
1288 l_img_comp = p_tcd->image->comps;
1290 for (i=0;i<p_tcd->image->numcomps;++i) {
1291 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1292 l_remaining = l_img_comp->prec & 7; /* (%8) */
1293 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1294 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1295 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1296 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1302 if (l_size_comp == 3) {
1306 switch (l_size_comp)
1310 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1311 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1313 if (l_img_comp->sgnd) {
1314 for (j=0;j<l_height;++j) {
1315 for (k=0;k<l_width;++k) {
1316 *(l_dest_ptr++) = (OPJ_CHAR) (*(l_src_ptr++));
1318 l_src_ptr += l_stride;
1322 for (j=0;j<l_height;++j) {
1323 for (k=0;k<l_width;++k) {
1324 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1326 l_src_ptr += l_stride;
1330 p_dest = (OPJ_BYTE *)l_dest_ptr;
1335 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1336 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1338 if (l_img_comp->sgnd) {
1339 for (j=0;j<l_height;++j) {
1340 for (k=0;k<l_width;++k) {
1341 *(l_dest_ptr++) = (OPJ_INT16) (*(l_src_ptr++));
1343 l_src_ptr += l_stride;
1347 for (j=0;j<l_height;++j) {
1348 for (k=0;k<l_width;++k) {
1349 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1351 l_src_ptr += l_stride;
1355 p_dest = (OPJ_BYTE*) l_dest_ptr;
1360 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1361 OPJ_INT32 * l_src_ptr = l_tilec->data;
1363 for (j=0;j<l_height;++j) {
1364 for (k=0;k<l_width;++k) {
1365 *(l_dest_ptr++) = (*(l_src_ptr++));
1367 l_src_ptr += l_stride;
1370 p_dest = (OPJ_BYTE*) l_dest_ptr;
1385 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1387 OPJ_UINT32 compno, resno, bandno, precno;
1388 opj_tcd_tile_t *l_tile = 00;
1389 opj_tcd_tilecomp_t *l_tile_comp = 00;
1390 opj_tcd_resolution_t *l_res = 00;
1391 opj_tcd_band_t *l_band = 00;
1392 opj_tcd_precinct_t *l_precinct = 00;
1393 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1394 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1400 if (! p_tcd->tcd_image) {
1404 if (p_tcd->m_is_decoder) {
1405 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1408 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1411 l_tile = p_tcd->tcd_image->tiles;
1416 l_tile_comp = l_tile->comps;
1418 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1419 l_res = l_tile_comp->resolutions;
1422 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1423 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1424 l_band = l_res->bands;
1425 for (bandno = 0; bandno < 3; ++bandno) {
1426 l_precinct = l_band->precincts;
1429 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1430 for (precno = 0; precno < l_nb_precincts; ++precno) {
1431 opj_tgt_destroy(l_precinct->incltree);
1432 l_precinct->incltree = 00;
1433 opj_tgt_destroy(l_precinct->imsbtree);
1434 l_precinct->imsbtree = 00;
1435 (*l_tcd_code_block_deallocate) (l_precinct);
1439 opj_free(l_band->precincts);
1440 l_band->precincts = 00;
1447 opj_free(l_tile_comp->resolutions);
1448 l_tile_comp->resolutions = 00;
1451 if (l_tile_comp->data) {
1452 opj_free(l_tile_comp->data);
1453 l_tile_comp->data = 00;
1458 opj_free(l_tile->comps);
1460 opj_free(p_tcd->tcd_image->tiles);
1461 p_tcd->tcd_image->tiles = 00;
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
1474 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1479 if (! opj_t2_decode_packets(
1482 p_tcd->tcd_image->tiles,
1487 opj_t2_destroy(l_t2);
1491 opj_t2_destroy(l_t2);
1493 /*---------------CLEAN-------------------*/
1497 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1501 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1502 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1503 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1506 l_t1 = opj_t1_create();
1511 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1512 /* The +3 is headroom required by the vectorized DWT */
1513 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1514 opj_t1_destroy(l_t1);
1521 opj_t1_destroy(l_t1);
1527 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1530 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1531 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1532 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1533 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1535 for (compno = 0; compno < l_tile->numcomps; compno++) {
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)
1545 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1546 if(numres2decode > 0){
1549 if (l_tccp->qmfbid == 1) {
1550 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1555 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1567 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1569 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1570 opj_tcp_t * l_tcp = p_tcd->tcp;
1571 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1572 OPJ_UINT32 l_samples,i;
1578 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1580 if (l_tile->numcomps >= 3 ){
1581 /* testcase 1336.pdf.asan.47.376 */
1582 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 (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 (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 fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1588 else if (l_tcp->mct == 2) {
1591 if (! l_tcp->m_mct_decoding_matrix) {
1595 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1600 for (i=0;i<l_tile->numcomps;++i) {
1601 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1605 if (! opj_mct_decode_custom(/* MCT data */
1606 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1607 /* size of components */
1611 /* nb of components (i.e. size of pData) */
1613 /* tells if the data is signed */
1614 p_tcd->image->comps->sgnd)) {
1622 if (l_tcp->tccps->qmfbid == 1) {
1623 opj_mct_decode( l_tile->comps[0].data,
1624 l_tile->comps[1].data,
1625 l_tile->comps[2].data,
1629 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1630 (OPJ_FLOAT32*)l_tile->comps[1].data,
1631 (OPJ_FLOAT32*)l_tile->comps[2].data,
1637 /* FIXME need to use opj_event_msg function */
1638 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1645 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1648 opj_tcd_tilecomp_t * l_tile_comp = 00;
1649 opj_tccp_t * l_tccp = 00;
1650 opj_image_comp_t * l_img_comp = 00;
1651 opj_tcd_resolution_t* l_res = 00;
1652 opj_tcd_tile_t * l_tile;
1653 OPJ_UINT32 l_width,l_height,i,j;
1654 OPJ_INT32 * l_current_ptr;
1655 OPJ_INT32 l_min, l_max;
1656 OPJ_UINT32 l_stride;
1658 l_tile = p_tcd->tcd_image->tiles;
1659 l_tile_comp = l_tile->comps;
1660 l_tccp = p_tcd->tcp->tccps;
1661 l_img_comp = p_tcd->image->comps;
1663 for (compno = 0; compno < l_tile->numcomps; compno++) {
1664 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1665 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1666 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1667 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1669 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1671 if (l_img_comp->sgnd) {
1672 l_min = -(1 << (l_img_comp->prec - 1));
1673 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1677 l_max = (1 << l_img_comp->prec) - 1;
1680 l_current_ptr = l_tile_comp->data;
1682 if (l_tccp->qmfbid == 1) {
1683 for (j=0;j<l_height;++j) {
1684 for (i = 0; i < l_width; ++i) {
1685 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1688 l_current_ptr += l_stride;
1692 for (j=0;j<l_height;++j) {
1693 for (i = 0; i < l_width; ++i) {
1694 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1695 *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1698 l_current_ptr += l_stride;
1713 * Deallocates the encoding data of the given precinct.
1715 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1717 OPJ_UINT32 cblkno , l_nb_code_blocks;
1719 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
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 );*/
1727 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);*/
1730 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1732 if (l_code_block->data) {
1733 opj_free(l_code_block->data);
1734 l_code_block->data = 00;
1737 if (l_code_block->segs) {
1738 opj_free(l_code_block->segs );
1739 l_code_block->segs = 00;
1745 opj_free(p_precinct->cblks.dec);
1746 p_precinct->cblks.dec = 00;
1751 * Deallocates the encoding data of the given precinct.
1753 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1755 OPJ_UINT32 cblkno , l_nb_code_blocks;
1757 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1759 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1761 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1762 if (l_code_block->data) {
1763 opj_free(l_code_block->data - 1);
1764 l_code_block->data = 00;
1767 if (l_code_block->layers) {
1768 opj_free(l_code_block->layers );
1769 l_code_block->layers = 00;
1772 if (l_code_block->passes) {
1773 opj_free(l_code_block->passes );
1774 l_code_block->passes = 00;
1779 opj_free(p_precinct->cblks.enc);
1781 p_precinct->cblks.enc = 00;
1785 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1787 OPJ_UINT32 i,l_data_size = 0;
1788 opj_image_comp_t * l_img_comp = 00;
1789 opj_tcd_tilecomp_t * l_tilec = 00;
1790 OPJ_UINT32 l_size_comp, l_remaining;
1792 l_tilec = p_tcd->tcd_image->tiles->comps;
1793 l_img_comp = p_tcd->image->comps;
1794 for (i=0;i<p_tcd->image->numcomps;++i) {
1795 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1796 l_remaining = l_img_comp->prec & 7; /* (%8) */
1802 if (l_size_comp == 3) {
1806 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1814 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1817 opj_tcd_tilecomp_t * l_tile_comp = 00;
1818 opj_tccp_t * l_tccp = 00;
1819 opj_image_comp_t * l_img_comp = 00;
1820 opj_tcd_tile_t * l_tile;
1821 OPJ_UINT32 l_nb_elem,i;
1822 OPJ_INT32 * l_current_ptr;
1824 l_tile = p_tcd->tcd_image->tiles;
1825 l_tile_comp = l_tile->comps;
1826 l_tccp = p_tcd->tcp->tccps;
1827 l_img_comp = p_tcd->image->comps;
1829 for (compno = 0; compno < l_tile->numcomps; compno++) {
1830 l_current_ptr = l_tile_comp->data;
1831 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1833 if (l_tccp->qmfbid == 1) {
1834 for (i = 0; i < l_nb_elem; ++i) {
1835 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1840 for (i = 0; i < l_nb_elem; ++i) {
1841 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1854 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1856 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1857 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1858 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1860 OPJ_BYTE ** l_data = 00;
1861 opj_tcp_t * l_tcp = p_tcd->tcp;
1863 if(!p_tcd->tcp->mct) {
1867 if (p_tcd->tcp->mct == 2) {
1868 if (! p_tcd->tcp->m_mct_coding_matrix) {
1872 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1877 for (i=0;i<l_tile->numcomps;++i) {
1878 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1882 if (! opj_mct_encode_custom(/* MCT data */
1883 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1884 /* size of components */
1888 /* nb of components (i.e. size of pData) */
1890 /* tells if the data is signed */
1891 p_tcd->image->comps->sgnd) )
1899 else if (l_tcp->tccps->qmfbid == 0) {
1900 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1903 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1909 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1911 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1912 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1913 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1916 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1917 if (l_tccp->qmfbid == 1) {
1918 if (! opj_dwt_encode(l_tile_comp)) {
1922 else if (l_tccp->qmfbid == 0) {
1923 if (! opj_dwt_encode_real(l_tile_comp)) {
1935 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1938 const OPJ_FLOAT64 * l_mct_norms;
1939 opj_tcp_t * l_tcp = p_tcd->tcp;
1941 l_t1 = opj_t1_create();
1946 if (l_tcp->mct == 1) {
1947 /* irreversible encoding */
1948 if (l_tcp->tccps->qmfbid == 0) {
1949 l_mct_norms = opj_mct_get_mct_norms_real();
1952 l_mct_norms = opj_mct_get_mct_norms();
1956 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1959 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1960 opj_t1_destroy(l_t1);
1964 opj_t1_destroy(l_t1);
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 )
1977 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1982 if (! opj_t2_encode_packets(
1985 p_tcd->tcd_image->tiles,
1986 p_tcd->tcp->numlayers,
1996 opj_t2_destroy(l_t2);
2000 opj_t2_destroy(l_t2);
2002 /*---------------CLEAN-------------------*/
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 )
2012 opj_cp_t * l_cp = p_tcd->cp;
2013 OPJ_UINT32 l_nb_written = 0;
2016 p_cstr_info->index_write = 0;
2019 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2021 /* Normal Rate/distortion allocation */
2022 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2027 /* Fixed layer allocation */
2028 opj_tcd_rateallocate_fixed(p_tcd);
2035 OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2037 OPJ_UINT32 p_src_length )
2039 OPJ_UINT32 i,j,l_data_size = 0;
2040 opj_image_comp_t * l_img_comp = 00;
2041 opj_tcd_tilecomp_t * l_tilec = 00;
2042 OPJ_UINT32 l_size_comp, l_remaining;
2043 OPJ_UINT32 l_nb_elem;
2045 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2046 if (l_data_size != p_src_length) {
2050 l_tilec = p_tcd->tcd_image->tiles->comps;
2051 l_img_comp = p_tcd->image->comps;
2052 for (i=0;i<p_tcd->image->numcomps;++i) {
2053 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2054 l_remaining = l_img_comp->prec & 7; /* (%8) */
2055 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2061 if (l_size_comp == 3) {
2065 switch (l_size_comp) {
2068 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2069 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2071 if (l_img_comp->sgnd) {
2072 for (j=0;j<l_nb_elem;++j) {
2073 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2077 for (j=0;j<l_nb_elem;++j) {
2078 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2082 p_src = (OPJ_BYTE*) l_src_ptr;
2087 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2088 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2090 if (l_img_comp->sgnd) {
2091 for (j=0;j<l_nb_elem;++j) {
2092 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2096 for (j=0;j<l_nb_elem;++j) {
2097 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2101 p_src = (OPJ_BYTE*) l_src_ptr;
2106 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2107 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2109 for (j=0;j<l_nb_elem;++j) {
2110 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2113 p_src = (OPJ_BYTE*) l_src_ptr;