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_malloc(sizeof(opj_tcd_t));
182 memset(l_tcd,0,sizeof(opj_tcd_t));
184 l_tcd->m_is_decoder = p_is_decoder ? 1 : 0;
186 l_tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
187 if (!l_tcd->tcd_image) {
191 memset(l_tcd->tcd_image,0,sizeof(opj_tcd_image_t));
197 /* ----------------------------------------------------------------------- */
199 void opj_tcd_rateallocate_fixed(opj_tcd_t *tcd) {
202 for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
203 opj_tcd_makelayer_fixed(tcd, layno, 1);
208 void opj_tcd_makelayer( opj_tcd_t *tcd,
213 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
216 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
218 tcd_tile->distolayer[layno] = 0; /* fixed_quality */
220 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
221 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
223 for (resno = 0; resno < tilec->numresolutions; resno++) {
224 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
226 for (bandno = 0; bandno < res->numbands; bandno++) {
227 opj_tcd_band_t *band = &res->bands[bandno];
229 for (precno = 0; precno < res->pw * res->ph; precno++) {
230 opj_tcd_precinct_t *prc = &band->precincts[precno];
232 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
233 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
234 opj_tcd_layer_t *layer = &cblk->layers[layno];
238 cblk->numpassesinlayers = 0;
241 n = cblk->numpassesinlayers;
243 for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
246 opj_tcd_pass_t *pass = &cblk->passes[passno];
250 dd = pass->distortiondec;
252 dr = pass->rate - cblk->passes[n - 1].rate;
253 dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
261 if (dd / dr >= thresh)
265 layer->numpasses = n - cblk->numpassesinlayers;
267 if (!layer->numpasses) {
272 if (cblk->numpassesinlayers == 0) {
273 layer->len = cblk->passes[n - 1].rate;
274 layer->data = cblk->data;
275 layer->disto = cblk->passes[n - 1].distortiondec;
277 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
278 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
279 layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
282 tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
285 cblk->numpassesinlayers = n;
293 void opj_tcd_makelayer_fixed(opj_tcd_t *tcd, OPJ_UINT32 layno, OPJ_UINT32 final) {
294 OPJ_UINT32 compno, resno, bandno, precno, cblkno;
295 OPJ_INT32 value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
296 OPJ_INT32 matrice[10][10][3];
299 opj_cp_t *cp = tcd->cp;
300 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
301 opj_tcp_t *tcd_tcp = tcd->tcp;
303 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
304 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
306 for (i = 0; i < tcd_tcp->numlayers; i++) {
307 for (j = 0; j < tilec->numresolutions; j++) {
308 for (k = 0; k < 3; k++) {
310 (OPJ_INT32) ((OPJ_FLOAT32)cp->m_specific_param.m_enc.m_matrice[i * tilec->numresolutions * 3 + j * 3 + k]
311 * (OPJ_FLOAT32) (tcd->image->comps[compno].prec / 16.0));
316 for (resno = 0; resno < tilec->numresolutions; resno++) {
317 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
319 for (bandno = 0; bandno < res->numbands; bandno++) {
320 opj_tcd_band_t *band = &res->bands[bandno];
322 for (precno = 0; precno < res->pw * res->ph; precno++) {
323 opj_tcd_precinct_t *prc = &band->precincts[precno];
325 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
326 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
327 opj_tcd_layer_t *layer = &cblk->layers[layno];
329 OPJ_INT32 imsb = (OPJ_INT32)(tcd->image->comps[compno].prec - cblk->numbps); /* number of bit-plan equal to zero */
331 /* Correction of the matrix of coefficient to include the IMSB information */
333 value = matrice[layno][resno][bandno];
340 value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
341 if (imsb >= matrice[layno - 1][resno][bandno]) {
342 value -= (imsb - matrice[layno - 1][resno][bandno]);
350 cblk->numpassesinlayers = 0;
353 n = cblk->numpassesinlayers;
354 if (cblk->numpassesinlayers == 0) {
356 n = 3 * (OPJ_UINT32)value - 2 + cblk->numpassesinlayers;
358 n = cblk->numpassesinlayers;
361 n = 3 * (OPJ_UINT32)value + cblk->numpassesinlayers;
364 layer->numpasses = n - cblk->numpassesinlayers;
366 if (!layer->numpasses)
369 if (cblk->numpassesinlayers == 0) {
370 layer->len = cblk->passes[n - 1].rate;
371 layer->data = cblk->data;
373 layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
374 layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
378 cblk->numpassesinlayers = n;
386 OPJ_BOOL opj_tcd_rateallocate( opj_tcd_t *tcd,
388 OPJ_UINT32 * p_data_written,
390 opj_codestream_info_t *cstr_info)
392 OPJ_UINT32 compno, resno, bandno, precno, cblkno, layno;
394 OPJ_FLOAT64 min, max;
395 OPJ_FLOAT64 cumdisto[100]; /* fixed_quality */
396 const OPJ_FLOAT64 K = 1; /* 1.1; fixed_quality */
397 OPJ_FLOAT64 maxSE = 0;
399 opj_cp_t *cp = tcd->cp;
400 opj_tcd_tile_t *tcd_tile = tcd->tcd_image->tiles;
401 opj_tcp_t *tcd_tcp = tcd->tcp;
406 tcd_tile->numpix = 0; /* fixed_quality */
408 for (compno = 0; compno < tcd_tile->numcomps; compno++) {
409 opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
412 for (resno = 0; resno < tilec->numresolutions; resno++) {
413 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
415 for (bandno = 0; bandno < res->numbands; bandno++) {
416 opj_tcd_band_t *band = &res->bands[bandno];
418 for (precno = 0; precno < res->pw * res->ph; precno++) {
419 opj_tcd_precinct_t *prc = &band->precincts[precno];
421 for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
422 opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
424 for (passno = 0; passno < cblk->totalpasses; passno++) {
425 opj_tcd_pass_t *pass = &cblk->passes[passno];
427 OPJ_FLOAT64 dd, rdslope;
430 dr = (OPJ_INT32)pass->rate;
431 dd = pass->distortiondec;
433 dr = (OPJ_INT32)(pass->rate - cblk->passes[passno - 1].rate);
434 dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
452 tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
453 tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
459 maxSE += (((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) - 1.0)
460 * ((OPJ_FLOAT64)(1 << tcd->image->comps[compno].prec) -1.0))
461 * ((OPJ_FLOAT64)(tilec->numpix));
466 opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
467 tile_info->numpix = tcd_tile->numpix;
468 tile_info->distotile = tcd_tile->distotile;
469 tile_info->thresh = (OPJ_FLOAT64 *) opj_malloc(tcd_tcp->numlayers * sizeof(OPJ_FLOAT64));
472 for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
473 OPJ_FLOAT64 lo = min;
474 OPJ_FLOAT64 hi = max;
475 OPJ_BOOL success = OPJ_FALSE;
476 OPJ_UINT32 maxlen = tcd_tcp->rates[layno] ? opj_uint_min(((OPJ_UINT32) ceil(tcd_tcp->rates[layno])), len) : len;
477 OPJ_FLOAT64 goodthresh = 0;
478 OPJ_FLOAT64 stable_thresh = 0;
480 OPJ_FLOAT64 distotarget; /* fixed_quality */
483 distotarget = tcd_tile->distotile - ((K * maxSE) / pow((OPJ_FLOAT32)10, tcd_tcp->distoratio[layno] / 10));
485 /* Don't try to find an optimal threshold but rather take everything not included yet, if
486 -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
487 -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
488 ==> possible to have some lossy layers and the last layer for sure lossless */
489 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))) {
490 opj_t2_t*t2 = opj_t2_create(tcd->image, cp);
491 OPJ_FLOAT64 thresh = 0;
497 for (i = 0; i < 128; ++i) {
498 OPJ_FLOAT64 distoachieved = 0; /* fixed_quality */
500 thresh = (lo + hi) / 2;
502 opj_tcd_makelayer(tcd, layno, thresh, 0);
504 if (cp->m_specific_param.m_enc.m_fixed_quality) { /* fixed_quality */
505 if(cp->m_specific_param.m_enc.m_cinema){
506 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)) {
512 distoachieved = layno == 0 ?
513 tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
515 if (distoachieved < distotarget) {
517 stable_thresh = thresh;
524 distoachieved = (layno == 0) ?
525 tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
527 if (distoachieved < distotarget) {
529 stable_thresh = thresh;
535 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))
537 /* TODO: what to do with l ??? seek / tell ??? */
538 /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
544 stable_thresh = thresh;
549 goodthresh = stable_thresh == 0? thresh : stable_thresh;
561 if(cstr_info) { /* Threshold for Marcela Index */
562 cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
565 opj_tcd_makelayer(tcd, layno, goodthresh, 1);
568 cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
574 OPJ_BOOL opj_tcd_init( opj_tcd_t *p_tcd,
575 opj_image_t * p_image,
578 OPJ_UINT32 l_tile_comp_size;
580 p_tcd->image = p_image;
583 p_tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
584 if (! p_tcd->tcd_image->tiles) {
587 memset(p_tcd->tcd_image->tiles,0, sizeof(opj_tcd_tile_t));
589 l_tile_comp_size = p_image->numcomps * (OPJ_UINT32)sizeof(opj_tcd_tilecomp_t);
590 p_tcd->tcd_image->tiles->comps = (opj_tcd_tilecomp_t *) opj_malloc(l_tile_comp_size);
591 if (! p_tcd->tcd_image->tiles->comps ) {
594 memset( p_tcd->tcd_image->tiles->comps , 0 , l_tile_comp_size);
596 p_tcd->tcd_image->tiles->numcomps = p_image->numcomps;
597 p_tcd->tp_pos = p_cp->m_specific_param.m_enc.m_tp_pos;
603 Destroy a previously created TCD handle
605 void opj_tcd_destroy(opj_tcd_t *tcd) {
607 opj_tcd_free_tile(tcd);
609 if (tcd->tcd_image) {
610 opj_free(tcd->tcd_image);
617 /* ----------------------------------------------------------------------- */
618 #define OPJ_MACRO_TCD_ALLOCATE(FUNCTION,TYPE,FRACTION,ELEMENT,FUNCTION_ELEMENT) \
619 OPJ_BOOL FUNCTION ( opj_tcd_t *p_tcd, \
620 OPJ_UINT32 p_tile_no \
623 OPJ_UINT32 (*l_gain_ptr)(OPJ_UINT32) = 00; \
624 OPJ_UINT32 compno, resno, bandno, precno, cblkno; \
625 opj_tcp_t * l_tcp = 00; \
626 opj_cp_t * l_cp = 00; \
627 opj_tcd_tile_t * l_tile = 00; \
628 opj_tccp_t *l_tccp = 00; \
629 opj_tcd_tilecomp_t *l_tilec = 00; \
630 opj_image_comp_t * l_image_comp = 00; \
631 opj_tcd_resolution_t *l_res = 00; \
632 opj_tcd_band_t *l_band = 00; \
633 opj_stepsize_t * l_step_size = 00; \
634 opj_tcd_precinct_t *l_current_precinct = 00; \
635 TYPE* l_code_block = 00; \
636 opj_image_t *l_image = 00; \
638 OPJ_UINT32 l_level_no; \
639 OPJ_UINT32 l_pdx, l_pdy; \
641 OPJ_INT32 l_x0b, l_y0b; \
642 /* extent of precincts , top left, bottom right**/ \
643 OPJ_INT32 l_tl_prc_x_start, l_tl_prc_y_start, l_br_prc_x_end, l_br_prc_y_end; \
644 /* number of precinct for a resolution */ \
645 OPJ_UINT32 l_nb_precincts; \
646 /* room needed to store l_nb_precinct precinct for a resolution */ \
647 OPJ_UINT32 l_nb_precinct_size; \
648 /* number of code blocks for a precinct*/ \
649 OPJ_UINT32 l_nb_code_blocks; \
650 /* room needed to store l_nb_code_blocks code blocks for a precinct*/ \
651 OPJ_UINT32 l_nb_code_blocks_size; \
652 /* size of data for a tile */ \
653 OPJ_UINT32 l_data_size; \
656 l_tcp = &(l_cp->tcps[p_tile_no]); \
657 l_tile = p_tcd->tcd_image->tiles; \
658 l_tccp = l_tcp->tccps; \
659 l_tilec = l_tile->comps; \
660 l_image = p_tcd->image; \
661 l_image_comp = p_tcd->image->comps; \
663 p = p_tile_no % l_cp->tw; /* tile coordinates */ \
664 q = p_tile_no / l_cp->tw; \
665 /*fprintf(stderr, "Tile coordinate = %d,%d\n", p, q);*/ \
667 /* 4 borders of the tile rescale on the image if necessary */ \
668 l_tile->x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + p * l_cp->tdx), (OPJ_INT32)l_image->x0); \
669 l_tile->y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + q * l_cp->tdy), (OPJ_INT32)l_image->y0); \
670 l_tile->x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (p + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1); \
671 l_tile->y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (q + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1); \
672 /* testcase 1888.pdf.asan.35.988 */ \
673 if (l_tccp->numresolutions == 0) { \
674 fprintf(stderr, "tiles require at least one resolution\n"); \
677 /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ \
679 /*tile->numcomps = image->numcomps; */ \
680 for(compno = 0; compno < l_tile->numcomps; ++compno) { \
681 /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ \
683 /* border of each l_tile component (global) */ \
684 l_tilec->x0 = opj_int_ceildiv(l_tile->x0, (OPJ_INT32)l_image_comp->dx); \
685 l_tilec->y0 = opj_int_ceildiv(l_tile->y0, (OPJ_INT32)l_image_comp->dy); \
686 l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx); \
687 l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy); \
688 /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ \
689 if (l_tilec->x1 < 0 || l_tilec->x0 < 0 || l_tilec->y1 < 0 || l_tilec->y0 < 0) return OPJ_FALSE;\
690 l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) \
691 * (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0) * (OPJ_UINT32)sizeof(OPJ_UINT32 );\
692 l_tilec->numresolutions = l_tccp->numresolutions; \
693 if (l_tccp->numresolutions < l_cp->m_specific_param.m_dec.m_reduce) { \
694 l_tilec->minimum_num_resolutions = 1; \
697 l_tilec->minimum_num_resolutions = l_tccp->numresolutions \
698 - l_cp->m_specific_param.m_dec.m_reduce; \
701 if (l_tilec->data == 00) { \
702 l_tilec->data = (OPJ_INT32 *) opj_malloc(l_data_size); \
703 if (! l_tilec->data ) { \
706 /*fprintf(stderr, "\tAllocate data of tilec (int): %d x OPJ_UINT32\n",l_data_size);*/ \
708 l_tilec->data_size = l_data_size; \
710 else if (l_data_size > l_tilec->data_size) { \
711 OPJ_INT32 * new_data = (OPJ_INT32 *) opj_realloc(l_tilec->data, l_data_size); \
712 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile data\n"); */ \
713 fprintf(stderr, "Not enough memory to handle tile data\n"); \
715 opj_free(l_tilec->data); \
716 l_tilec->data = NULL; \
717 l_tilec->data_size = 0; \
720 l_tilec->data = new_data; \
721 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->data_size, l_data_size);*/ \
722 l_tilec->data_size = l_data_size; \
725 l_data_size = l_tilec->numresolutions * (OPJ_UINT32)sizeof(opj_tcd_resolution_t); \
727 if (l_tilec->resolutions == 00) { \
728 l_tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(l_data_size); \
729 if (! l_tilec->resolutions ) { \
732 /*fprintf(stderr, "\tAllocate resolutions of tilec (opj_tcd_resolution_t): %d\n",l_data_size);*/ \
733 l_tilec->resolutions_size = l_data_size; \
734 memset(l_tilec->resolutions,0,l_data_size); \
736 else if (l_data_size > l_tilec->resolutions_size) { \
737 opj_tcd_resolution_t* new_resolutions = (opj_tcd_resolution_t *) opj_realloc(l_tilec->resolutions, l_data_size); \
738 if (! new_resolutions) { \
739 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to tile resolutions\n"); */ \
740 fprintf(stderr, "Not enough memory to tile resolutions\n"); \
741 opj_free(l_tilec->resolutions); \
742 l_tilec->resolutions = NULL; \
743 l_tilec->resolutions_size = 0; \
746 l_tilec->resolutions = new_resolutions; \
747 /*fprintf(stderr, "\tReallocate data of tilec (int): from %d to %d x OPJ_UINT32\n", l_tilec->resolutions_size, l_data_size);*/ \
748 memset(((OPJ_BYTE*) l_tilec->resolutions)+l_tilec->resolutions_size,0,l_data_size - l_tilec->resolutions_size); \
749 l_tilec->resolutions_size = l_data_size; \
752 l_level_no = l_tilec->numresolutions - 1; \
753 l_res = l_tilec->resolutions; \
754 l_step_size = l_tccp->stepsizes; \
755 if (l_tccp->qmfbid == 0) { \
756 l_gain_ptr = &opj_dwt_getgain_real; \
759 l_gain_ptr = &opj_dwt_getgain; \
761 /*fprintf(stderr, "\tlevel_no=%d\n",l_level_no);*/ \
763 for(resno = 0; resno < l_tilec->numresolutions; ++resno) { \
764 /*fprintf(stderr, "\t\tresno = %d/%d\n", resno, l_tilec->numresolutions);*/ \
765 OPJ_INT32 tlcbgxstart, tlcbgystart /*, brcbgxend, brcbgyend*/; \
766 OPJ_UINT32 cbgwidthexpn, cbgheightexpn; \
767 OPJ_UINT32 cblkwidthexpn, cblkheightexpn; \
769 /* border for each resolution level (global) */ \
770 l_res->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
771 l_res->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
772 l_res->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
773 l_res->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
774 /*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);*/ \
775 /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */ \
776 l_pdx = l_tccp->prcw[resno]; \
777 l_pdy = l_tccp->prch[resno]; \
778 if (l_pdx == 0 || l_pdy == 0) \
780 /*fprintf(stderr, "\t\t\tpdx=%d, pdy=%d\n", l_pdx, l_pdy);*/ \
781 /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */ \
782 l_tl_prc_x_start = opj_int_floordivpow2(l_res->x0, (OPJ_INT32)l_pdx) << l_pdx; \
783 l_tl_prc_y_start = opj_int_floordivpow2(l_res->y0, (OPJ_INT32)l_pdy) << l_pdy; \
784 l_br_prc_x_end = opj_int_ceildivpow2(l_res->x1, (OPJ_INT32)l_pdx) << l_pdx; \
785 l_br_prc_y_end = opj_int_ceildivpow2(l_res->y1, (OPJ_INT32)l_pdy) << l_pdy; \
786 /*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 );*/ \
788 l_res->pw = (l_res->x0 == l_res->x1) ? 0 : (OPJ_UINT32)((l_br_prc_x_end - l_tl_prc_x_start) >> l_pdx); \
789 l_res->ph = (l_res->y0 == l_res->y1) ? 0 : (OPJ_UINT32)((l_br_prc_y_end - l_tl_prc_y_start) >> l_pdy); \
790 /*fprintf(stderr, "\t\t\tres_pw=%d, res_ph=%d\n", l_res->pw, l_res->ph );*/ \
792 l_nb_precincts = l_res->pw * l_res->ph; \
793 l_nb_precinct_size = l_nb_precincts * (OPJ_UINT32)sizeof(opj_tcd_precinct_t); \
795 tlcbgxstart = l_tl_prc_x_start; \
796 tlcbgystart = l_tl_prc_y_start; \
797 /*brcbgxend = l_br_prc_x_end;*/ \
798 /* brcbgyend = l_br_prc_y_end;*/ \
799 cbgwidthexpn = l_pdx; \
800 cbgheightexpn = l_pdy; \
801 l_res->numbands = 1; \
804 tlcbgxstart = opj_int_ceildivpow2(l_tl_prc_x_start, 1); \
805 tlcbgystart = opj_int_ceildivpow2(l_tl_prc_y_start, 1); \
806 /*brcbgxend = opj_int_ceildivpow2(l_br_prc_x_end, 1);*/ \
807 /*brcbgyend = opj_int_ceildivpow2(l_br_prc_y_end, 1);*/ \
808 cbgwidthexpn = l_pdx - 1; \
809 cbgheightexpn = l_pdy - 1; \
810 l_res->numbands = 3; \
813 cblkwidthexpn = opj_uint_min(l_tccp->cblkw, cbgwidthexpn); \
814 cblkheightexpn = opj_uint_min(l_tccp->cblkh, cbgheightexpn); \
815 l_band = l_res->bands; \
817 for (bandno = 0; bandno < l_res->numbands; ++bandno) { \
819 /*fprintf(stderr, "\t\t\tband_no=%d/%d\n", bandno, l_res->numbands );*/ \
822 l_band->bandno = 0 ; \
823 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0, (OPJ_INT32)l_level_no); \
824 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0, (OPJ_INT32)l_level_no); \
825 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1, (OPJ_INT32)l_level_no); \
826 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1, (OPJ_INT32)l_level_no); \
829 l_band->bandno = bandno + 1; \
830 /* x0b = 1 if bandno = 1 or 3 */ \
831 l_x0b = l_band->bandno&1; \
832 /* y0b = 1 if bandno = 2 or 3 */ \
833 l_y0b = (OPJ_INT32)((l_band->bandno)>>1); \
834 /* l_band border (global) */ \
835 l_band->x0 = opj_int_ceildivpow2(l_tilec->x0 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
836 l_band->y0 = opj_int_ceildivpow2(l_tilec->y0 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
837 l_band->x1 = opj_int_ceildivpow2(l_tilec->x1 - (1 << l_level_no) * l_x0b, (OPJ_INT32)(l_level_no + 1)); \
838 l_band->y1 = opj_int_ceildivpow2(l_tilec->y1 - (1 << l_level_no) * l_y0b, (OPJ_INT32)(l_level_no + 1)); \
841 /** avoid an if with storing function pointer */ \
842 l_gain = (*l_gain_ptr) (l_band->bandno); \
843 numbps = (OPJ_INT32)(l_image_comp->prec + l_gain); \
844 l_band->stepsize = (OPJ_FLOAT32)(((1.0 + l_step_size->mant / 2048.0) * pow(2.0, (OPJ_INT32) (numbps - l_step_size->expn)))) * FRACTION; \
845 l_band->numbps = l_step_size->expn + (OPJ_INT32)l_tccp->numgbits - 1; /* WHY -1 ? */ \
847 if (! l_band->precincts) { \
848 l_band->precincts = (opj_tcd_precinct_t *) opj_malloc( /*3 * */ l_nb_precinct_size); \
849 if (! l_band->precincts) { \
852 /*fprintf(stderr, "\t\t\t\tAllocate precincts of a band (opj_tcd_precinct_t): %d\n",l_nb_precinct_size); */ \
853 memset(l_band->precincts,0,l_nb_precinct_size); \
854 l_band->precincts_data_size = l_nb_precinct_size; \
856 else if (l_band->precincts_data_size < l_nb_precinct_size) { \
858 opj_tcd_precinct_t * new_precincts = (opj_tcd_precinct_t *) opj_realloc(l_band->precincts,/*3 * */ l_nb_precinct_size); \
859 if (! new_precincts) { \
860 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle band precints\n"); */ \
861 fprintf(stderr, "Not enough memory to handle band precints\n"); \
862 opj_free(l_band->precincts); \
863 l_band->precincts = NULL; \
864 l_band->precincts_data_size = 0; \
867 l_band->precincts = new_precincts; \
868 /*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);*/ \
869 memset(((OPJ_BYTE *) l_band->precincts) + l_band->precincts_data_size,0,l_nb_precinct_size - l_band->precincts_data_size); \
870 l_band->precincts_data_size = l_nb_precinct_size; \
873 l_current_precinct = l_band->precincts; \
874 for (precno = 0; precno < l_nb_precincts; ++precno) { \
875 OPJ_INT32 tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend; \
876 OPJ_INT32 cbgxstart = tlcbgxstart + (OPJ_INT32)(precno % l_res->pw) * (1 << cbgwidthexpn); \
877 OPJ_INT32 cbgystart = tlcbgystart + (OPJ_INT32)(precno / l_res->pw) * (1 << cbgheightexpn); \
878 OPJ_INT32 cbgxend = cbgxstart + (1 << cbgwidthexpn); \
879 OPJ_INT32 cbgyend = cbgystart + (1 << cbgheightexpn); \
880 /*fprintf(stderr, "\t precno=%d; bandno=%d, resno=%d; compno=%d\n", precno, bandno , resno, compno);*/ \
881 /*fprintf(stderr, "\t tlcbgxstart(=%d) + (precno(=%d) percent res->pw(=%d)) * (1 << cbgwidthexpn(=%d)) \n",tlcbgxstart,precno,l_res->pw,cbgwidthexpn);*/ \
883 /* precinct size (global) */ \
884 /*fprintf(stderr, "\t cbgxstart=%d, l_band->x0 = %d \n",cbgxstart, l_band->x0);*/ \
886 l_current_precinct->x0 = opj_int_max(cbgxstart, l_band->x0); \
887 l_current_precinct->y0 = opj_int_max(cbgystart, l_band->y0); \
888 l_current_precinct->x1 = opj_int_min(cbgxend, l_band->x1); \
889 l_current_precinct->y1 = opj_int_min(cbgyend, l_band->y1); \
890 /*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);*/ \
892 tlcblkxstart = opj_int_floordivpow2(l_current_precinct->x0, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
893 /*fprintf(stderr, "\t tlcblkxstart =%d\n",tlcblkxstart );*/ \
894 tlcblkystart = opj_int_floordivpow2(l_current_precinct->y0, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
895 /*fprintf(stderr, "\t tlcblkystart =%d\n",tlcblkystart );*/ \
896 brcblkxend = opj_int_ceildivpow2(l_current_precinct->x1, (OPJ_INT32)cblkwidthexpn) << cblkwidthexpn; \
897 /*fprintf(stderr, "\t brcblkxend =%d\n",brcblkxend );*/ \
898 brcblkyend = opj_int_ceildivpow2(l_current_precinct->y1, (OPJ_INT32)cblkheightexpn) << cblkheightexpn; \
899 /*fprintf(stderr, "\t brcblkyend =%d\n",brcblkyend );*/ \
900 l_current_precinct->cw = (OPJ_UINT32)((brcblkxend - tlcblkxstart) >> cblkwidthexpn); \
901 l_current_precinct->ch = (OPJ_UINT32)((brcblkyend - tlcblkystart) >> cblkheightexpn); \
903 l_nb_code_blocks = l_current_precinct->cw * l_current_precinct->ch; \
904 /*fprintf(stderr, "\t\t\t\t precinct_cw = %d x recinct_ch = %d\n",l_current_precinct->cw, l_current_precinct->ch); */ \
905 l_nb_code_blocks_size = l_nb_code_blocks * (OPJ_UINT32)sizeof(TYPE); \
907 if (! l_current_precinct->cblks.ELEMENT) { \
908 l_current_precinct->cblks.ELEMENT = (TYPE*) opj_malloc(l_nb_code_blocks_size); \
909 if (! l_current_precinct->cblks.ELEMENT ) { \
912 /*fprintf(stderr, "\t\t\t\tAllocate cblks of a precinct (opj_tcd_cblk_dec_t): %d\n",l_nb_code_blocks_size);*/ \
914 memset(l_current_precinct->cblks.ELEMENT,0,l_nb_code_blocks_size); \
916 l_current_precinct->block_size = l_nb_code_blocks_size; \
918 else if (l_nb_code_blocks_size > l_current_precinct->block_size) { \
919 TYPE *new_ELEMENT = (TYPE*) opj_realloc(l_current_precinct->cblks.ELEMENT, l_nb_code_blocks_size); \
920 if (! new_ELEMENT) { \
921 opj_free(l_current_precinct->cblks.ELEMENT); \
922 l_current_precinct->cblks.ELEMENT = NULL; \
923 l_current_precinct->block_size = 0; \
924 /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for current precinct codeblock element\n"); */ \
925 fprintf(stderr, "Not enough memory for current precinct codeblock element\n"); \
928 l_current_precinct->cblks.ELEMENT = new_ELEMENT; \
929 /*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); */\
931 memset(((OPJ_BYTE *) l_current_precinct->cblks.ELEMENT) + l_current_precinct->block_size \
933 ,l_nb_code_blocks_size - l_current_precinct->block_size); \
935 l_current_precinct->block_size = l_nb_code_blocks_size; \
938 if (! l_current_precinct->incltree) { \
939 l_current_precinct->incltree = opj_tgt_create(l_current_precinct->cw, \
940 l_current_precinct->ch); \
943 l_current_precinct->incltree = opj_tgt_init(l_current_precinct->incltree, \
944 l_current_precinct->cw, \
945 l_current_precinct->ch); \
948 if (! l_current_precinct->incltree) { \
949 fprintf(stderr, "WARNING: No incltree created.\n"); \
950 /*return OPJ_FALSE;*/ \
953 if (! l_current_precinct->imsbtree) { \
954 l_current_precinct->imsbtree = opj_tgt_create( \
955 l_current_precinct->cw, \
956 l_current_precinct->ch); \
959 l_current_precinct->imsbtree = opj_tgt_init( \
960 l_current_precinct->imsbtree, \
961 l_current_precinct->cw, \
962 l_current_precinct->ch); \
965 if (! l_current_precinct->imsbtree) { \
966 fprintf(stderr, "WARNING: No imsbtree created.\n"); \
967 /*return OPJ_FALSE;*/ \
970 l_code_block = l_current_precinct->cblks.ELEMENT; \
972 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { \
973 OPJ_INT32 cblkxstart = tlcblkxstart + (OPJ_INT32)(cblkno % l_current_precinct->cw) * (1 << cblkwidthexpn); \
974 OPJ_INT32 cblkystart = tlcblkystart + (OPJ_INT32)(cblkno / l_current_precinct->cw) * (1 << cblkheightexpn); \
975 OPJ_INT32 cblkxend = cblkxstart + (1 << cblkwidthexpn); \
976 OPJ_INT32 cblkyend = cblkystart + (1 << cblkheightexpn); \
978 /* code-block size (global) */ \
979 l_code_block->x0 = opj_int_max(cblkxstart, l_current_precinct->x0); \
980 l_code_block->y0 = opj_int_max(cblkystart, l_current_precinct->y0); \
981 l_code_block->x1 = opj_int_min(cblkxend, l_current_precinct->x1); \
982 l_code_block->y1 = opj_int_min(cblkyend, l_current_precinct->y1); \
984 if (! FUNCTION_ELEMENT(l_code_block)) { \
989 ++l_current_precinct; \
1005 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_encode_tile, opj_tcd_cblk_enc_t, 1.f, enc, opj_tcd_code_block_enc_allocate)
1006 OPJ_MACRO_TCD_ALLOCATE(opj_tcd_init_decode_tile, opj_tcd_cblk_dec_t, 0.5f, dec, opj_tcd_code_block_dec_allocate)
1008 #undef OPJ_MACRO_TCD_ALLOCATE
1011 * Allocates memory for an encoding code block.
1013 OPJ_BOOL opj_tcd_code_block_enc_allocate (opj_tcd_cblk_enc_t * p_code_block)
1015 if (! p_code_block->data) {
1017 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE*2); /*why +1 ?*/
1018 if(! p_code_block->data) {
1022 p_code_block->data[0] = 0;
1023 p_code_block->data+=1;
1025 /* no memset since data */
1026 p_code_block->layers = (opj_tcd_layer_t*) opj_malloc(100 * sizeof(opj_tcd_layer_t));
1027 if (! p_code_block->layers) {
1031 p_code_block->passes = (opj_tcd_pass_t*) opj_malloc(100 * sizeof(opj_tcd_pass_t));
1032 if (! p_code_block->passes) {
1037 memset(p_code_block->layers,0,100 * sizeof(opj_tcd_layer_t));
1038 memset(p_code_block->passes,0,100 * sizeof(opj_tcd_pass_t));
1044 * Allocates memory for a decoding code block.
1046 OPJ_BOOL opj_tcd_code_block_dec_allocate (opj_tcd_cblk_dec_t * p_code_block)
1048 OPJ_UINT32 l_seg_size;
1050 if (! p_code_block->data) {
1052 p_code_block->data = (OPJ_BYTE*) opj_malloc(OPJ_J2K_DEFAULT_CBLK_DATA_SIZE);
1053 if (! p_code_block->data) {
1056 p_code_block->data_max_size = OPJ_J2K_DEFAULT_CBLK_DATA_SIZE;
1057 /*fprintf(stderr, "Allocate 8192 elements of code_block->data\n");*/
1059 l_seg_size = OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t);
1060 p_code_block->segs = (opj_tcd_seg_t *) opj_malloc(l_seg_size);
1061 if (! p_code_block->segs) {
1064 memset(p_code_block->segs,0,l_seg_size);
1065 /*fprintf(stderr, "Allocate %d elements of code_block->data\n", OPJ_J2K_DEFAULT_NB_SEGS * sizeof(opj_tcd_seg_t));*/
1067 p_code_block->m_current_max_segs = OPJ_J2K_DEFAULT_NB_SEGS;
1068 /*fprintf(stderr, "m_current_max_segs of code_block->data = %d\n", p_code_block->m_current_max_segs);*/
1071 /*p_code_block->numsegs = 0; */
1076 OPJ_UINT32 opj_tcd_get_decoded_tile_size ( opj_tcd_t *p_tcd )
1079 OPJ_UINT32 l_data_size = 0;
1080 opj_image_comp_t * l_img_comp = 00;
1081 opj_tcd_tilecomp_t * l_tile_comp = 00;
1082 opj_tcd_resolution_t * l_res = 00;
1083 OPJ_UINT32 l_size_comp, l_remaining;
1085 l_tile_comp = p_tcd->tcd_image->tiles->comps;
1086 l_img_comp = p_tcd->image->comps;
1088 for (i=0;i<p_tcd->image->numcomps;++i) {
1089 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1090 l_remaining = l_img_comp->prec & 7; /* (%8) */
1096 if (l_size_comp == 3) {
1100 l_res = l_tile_comp->resolutions + l_tile_comp->minimum_num_resolutions - 1;
1101 l_data_size += l_size_comp * (OPJ_UINT32)((l_res->x1 - l_res->x0) * (l_res->y1 - l_res->y0));
1109 OPJ_BOOL opj_tcd_encode_tile( opj_tcd_t *p_tcd,
1110 OPJ_UINT32 p_tile_no,
1112 OPJ_UINT32 * p_data_written,
1113 OPJ_UINT32 p_max_length,
1114 opj_codestream_info_t *p_cstr_info)
1117 if (p_tcd->cur_tp_num == 0) {
1119 p_tcd->tcd_tileno = p_tile_no;
1120 p_tcd->tcp = &p_tcd->cp->tcps[p_tile_no];
1122 /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1124 OPJ_UINT32 l_num_packs = 0;
1126 opj_tcd_tilecomp_t *l_tilec_idx = &p_tcd->tcd_image->tiles->comps[0]; /* based on component 0 */
1127 opj_tccp_t *l_tccp = p_tcd->tcp->tccps; /* based on component 0 */
1129 for (i = 0; i < l_tilec_idx->numresolutions; i++) {
1130 opj_tcd_resolution_t *l_res_idx = &l_tilec_idx->resolutions[i];
1132 p_cstr_info->tile[p_tile_no].pw[i] = (int)l_res_idx->pw;
1133 p_cstr_info->tile[p_tile_no].ph[i] = (int)l_res_idx->ph;
1135 l_num_packs += l_res_idx->pw * l_res_idx->ph;
1136 p_cstr_info->tile[p_tile_no].pdx[i] = (int)l_tccp->prcw[i];
1137 p_cstr_info->tile[p_tile_no].pdy[i] = (int)l_tccp->prch[i];
1139 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));
1143 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1144 /*---------------TILE-------------------*/
1145 if (! opj_tcd_dc_level_shift_encode(p_tcd)) {
1148 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1150 /* FIXME _ProfStart(PGROUP_MCT); */
1151 if (! opj_tcd_mct_encode(p_tcd)) {
1154 /* FIXME _ProfStop(PGROUP_MCT); */
1156 /* FIXME _ProfStart(PGROUP_DWT); */
1157 if (! opj_tcd_dwt_encode(p_tcd)) {
1160 /* FIXME _ProfStop(PGROUP_DWT); */
1162 /* FIXME _ProfStart(PGROUP_T1); */
1163 if (! opj_tcd_t1_encode(p_tcd)) {
1166 /* FIXME _ProfStop(PGROUP_T1); */
1168 /* FIXME _ProfStart(PGROUP_RATE); */
1169 if (! opj_tcd_rate_allocate_encode(p_tcd,p_dest,p_max_length,p_cstr_info)) {
1172 /* FIXME _ProfStop(PGROUP_RATE); */
1175 /*--------------TIER2------------------*/
1179 p_cstr_info->index_write = 1;
1181 /* FIXME _ProfStart(PGROUP_T2); */
1183 if (! opj_tcd_t2_encode(p_tcd,p_dest,p_data_written,p_max_length,p_cstr_info)) {
1186 /* FIXME _ProfStop(PGROUP_T2); */
1188 /*---------------CLEAN-------------------*/
1193 OPJ_BOOL opj_tcd_decode_tile( opj_tcd_t *p_tcd,
1195 OPJ_UINT32 p_max_length,
1196 OPJ_UINT32 p_tile_no,
1197 opj_codestream_index_t *p_cstr_index
1200 OPJ_UINT32 l_data_read;
1201 p_tcd->tcd_tileno = p_tile_no;
1202 p_tcd->tcp = &(p_tcd->cp->tcps[p_tile_no]);
1204 #ifdef TODO_MSD /* FIXME */
1207 OPJ_UINT32 resno, compno, numprec = 0;
1208 for (compno = 0; compno < (OPJ_UINT32) p_cstr_info->numcomps; compno++) {
1209 opj_tcp_t *tcp = &p_tcd->cp->tcps[0];
1210 opj_tccp_t *tccp = &tcp->tccps[compno];
1211 opj_tcd_tilecomp_t *tilec_idx = &p_tcd->tcd_image->tiles->comps[compno];
1212 for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
1213 opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
1214 p_cstr_info->tile[p_tile_no].pw[resno] = res_idx->pw;
1215 p_cstr_info->tile[p_tile_no].ph[resno] = res_idx->ph;
1216 numprec += res_idx->pw * res_idx->ph;
1217 p_cstr_info->tile[p_tile_no].pdx[resno] = tccp->prcw[resno];
1218 p_cstr_info->tile[p_tile_no].pdy[resno] = tccp->prch[resno];
1221 p_cstr_info->tile[p_tile_no].packet = (opj_packet_info_t *) opj_malloc(p_cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
1222 p_cstr_info->packno = 0;
1227 /*--------------TIER2------------------*/
1228 /* FIXME _ProfStart(PGROUP_T2); */
1230 if (! opj_tcd_t2_decode(p_tcd, p_src, &l_data_read, p_max_length, p_cstr_index))
1234 /* FIXME _ProfStop(PGROUP_T2); */
1236 /*------------------TIER1-----------------*/
1238 /* FIXME _ProfStart(PGROUP_T1); */
1240 (! opj_tcd_t1_decode(p_tcd))
1244 /* FIXME _ProfStop(PGROUP_T1); */
1246 /*----------------DWT---------------------*/
1248 /* FIXME _ProfStart(PGROUP_DWT); */
1250 (! opj_tcd_dwt_decode(p_tcd))
1254 /* FIXME _ProfStop(PGROUP_DWT); */
1256 /*----------------MCT-------------------*/
1257 /* FIXME _ProfStart(PGROUP_MCT); */
1259 (! opj_tcd_mct_decode(p_tcd))
1263 /* FIXME _ProfStop(PGROUP_MCT); */
1265 /* FIXME _ProfStart(PGROUP_DC_SHIFT); */
1267 (! opj_tcd_dc_level_shift_decode(p_tcd))
1271 /* FIXME _ProfStop(PGROUP_DC_SHIFT); */
1274 /*---------------TILE-------------------*/
1278 OPJ_BOOL opj_tcd_update_tile_data ( opj_tcd_t *p_tcd,
1280 OPJ_UINT32 p_dest_length
1283 OPJ_UINT32 i,j,k,l_data_size = 0;
1284 opj_image_comp_t * l_img_comp = 00;
1285 opj_tcd_tilecomp_t * l_tilec = 00;
1286 opj_tcd_resolution_t * l_res;
1287 OPJ_UINT32 l_size_comp, l_remaining;
1288 OPJ_UINT32 l_stride, l_width,l_height;
1290 l_data_size = opj_tcd_get_decoded_tile_size(p_tcd);
1291 if (l_data_size > p_dest_length) {
1295 l_tilec = p_tcd->tcd_image->tiles->comps;
1296 l_img_comp = p_tcd->image->comps;
1298 for (i=0;i<p_tcd->image->numcomps;++i) {
1299 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1300 l_remaining = l_img_comp->prec & 7; /* (%8) */
1301 l_res = l_tilec->resolutions + l_img_comp->resno_decoded;
1302 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1303 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1304 l_stride = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0) - l_width;
1310 if (l_size_comp == 3) {
1314 switch (l_size_comp)
1318 OPJ_CHAR * l_dest_ptr = (OPJ_CHAR *) p_dest;
1319 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1321 if (l_img_comp->sgnd) {
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++));
1326 l_src_ptr += l_stride;
1330 for (j=0;j<l_height;++j) {
1331 for (k=0;k<l_width;++k) {
1332 *(l_dest_ptr++) = (OPJ_CHAR) ((*(l_src_ptr++))&0xff);
1334 l_src_ptr += l_stride;
1338 p_dest = (OPJ_BYTE *)l_dest_ptr;
1343 const OPJ_INT32 * l_src_ptr = l_tilec->data;
1344 OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_dest;
1346 if (l_img_comp->sgnd) {
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++));
1351 l_src_ptr += l_stride;
1355 for (j=0;j<l_height;++j) {
1356 for (k=0;k<l_width;++k) {
1357 *(l_dest_ptr++) = (OPJ_INT16) ((*(l_src_ptr++))&0xffff);
1359 l_src_ptr += l_stride;
1363 p_dest = (OPJ_BYTE*) l_dest_ptr;
1368 OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_dest;
1369 OPJ_INT32 * l_src_ptr = l_tilec->data;
1371 for (j=0;j<l_height;++j) {
1372 for (k=0;k<l_width;++k) {
1373 *(l_dest_ptr++) = (*(l_src_ptr++));
1375 l_src_ptr += l_stride;
1378 p_dest = (OPJ_BYTE*) l_dest_ptr;
1393 void opj_tcd_free_tile(opj_tcd_t *p_tcd)
1395 OPJ_UINT32 compno, resno, bandno, precno;
1396 opj_tcd_tile_t *l_tile = 00;
1397 opj_tcd_tilecomp_t *l_tile_comp = 00;
1398 opj_tcd_resolution_t *l_res = 00;
1399 opj_tcd_band_t *l_band = 00;
1400 opj_tcd_precinct_t *l_precinct = 00;
1401 OPJ_UINT32 l_nb_resolutions, l_nb_precincts;
1402 void (* l_tcd_code_block_deallocate) (opj_tcd_precinct_t *) = 00;
1408 if (! p_tcd->tcd_image) {
1412 if (p_tcd->m_is_decoder) {
1413 l_tcd_code_block_deallocate = opj_tcd_code_block_dec_deallocate;
1416 l_tcd_code_block_deallocate = opj_tcd_code_block_enc_deallocate;
1419 l_tile = p_tcd->tcd_image->tiles;
1424 l_tile_comp = l_tile->comps;
1426 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1427 l_res = l_tile_comp->resolutions;
1430 l_nb_resolutions = l_tile_comp->resolutions_size / sizeof(opj_tcd_resolution_t);
1431 for (resno = 0; resno < l_nb_resolutions; ++resno) {
1432 l_band = l_res->bands;
1433 for (bandno = 0; bandno < 3; ++bandno) {
1434 l_precinct = l_band->precincts;
1437 l_nb_precincts = l_band->precincts_data_size / sizeof(opj_tcd_precinct_t);
1438 for (precno = 0; precno < l_nb_precincts; ++precno) {
1439 opj_tgt_destroy(l_precinct->incltree);
1440 l_precinct->incltree = 00;
1441 opj_tgt_destroy(l_precinct->imsbtree);
1442 l_precinct->imsbtree = 00;
1443 (*l_tcd_code_block_deallocate) (l_precinct);
1447 opj_free(l_band->precincts);
1448 l_band->precincts = 00;
1455 opj_free(l_tile_comp->resolutions);
1456 l_tile_comp->resolutions = 00;
1459 if (l_tile_comp->data) {
1460 opj_free(l_tile_comp->data);
1461 l_tile_comp->data = 00;
1466 opj_free(l_tile->comps);
1468 opj_free(p_tcd->tcd_image->tiles);
1469 p_tcd->tcd_image->tiles = 00;
1473 OPJ_BOOL opj_tcd_t2_decode (opj_tcd_t *p_tcd,
1474 OPJ_BYTE * p_src_data,
1475 OPJ_UINT32 * p_data_read,
1476 OPJ_UINT32 p_max_src_size,
1477 opj_codestream_index_t *p_cstr_index
1482 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1487 if (! opj_t2_decode_packets(
1490 p_tcd->tcd_image->tiles,
1495 opj_t2_destroy(l_t2);
1499 opj_t2_destroy(l_t2);
1501 /*---------------CLEAN-------------------*/
1505 OPJ_BOOL opj_tcd_t1_decode ( opj_tcd_t *p_tcd )
1509 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1510 opj_tcd_tilecomp_t* l_tile_comp = l_tile->comps;
1511 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1514 l_t1 = opj_t1_create();
1519 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1520 /* The +3 is headroom required by the vectorized DWT */
1521 if (OPJ_FALSE == opj_t1_decode_cblks(l_t1, l_tile_comp, l_tccp)) {
1522 opj_t1_destroy(l_t1);
1529 opj_t1_destroy(l_t1);
1535 OPJ_BOOL opj_tcd_dwt_decode ( opj_tcd_t *p_tcd )
1538 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1539 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1540 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1541 opj_image_comp_t * l_img_comp = p_tcd->image->comps;
1543 for (compno = 0; compno < l_tile->numcomps; compno++) {
1545 if (tcd->cp->reduce != 0) {
1546 tcd->image->comps[compno].resno_decoded =
1547 tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
1548 if (tcd->image->comps[compno].resno_decoded < 0)
1553 numres2decode = tcd->image->comps[compno].resno_decoded + 1;
1554 if(numres2decode > 0){
1557 if (l_tccp->qmfbid == 1) {
1558 if (! opj_dwt_decode(l_tile_comp, l_img_comp->resno_decoded+1)) {
1563 if (! opj_dwt_decode_real(l_tile_comp, l_img_comp->resno_decoded+1)) {
1575 OPJ_BOOL opj_tcd_mct_decode ( opj_tcd_t *p_tcd )
1577 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1578 opj_tcp_t * l_tcp = p_tcd->tcp;
1579 opj_tcd_tilecomp_t * l_tile_comp = l_tile->comps;
1580 OPJ_UINT32 l_samples,i;
1586 l_samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1588 if (l_tile->numcomps >= 3 ){
1589 /* testcase 1336.pdf.asan.47.376 */
1590 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 ||
1591 (l_tile->comps[1].x1 - l_tile->comps[1].x0) * (l_tile->comps[1].y1 - l_tile->comps[1].y0) < (OPJ_INT32)l_samples ||
1592 (l_tile->comps[2].x1 - l_tile->comps[2].x0) * (l_tile->comps[2].y1 - l_tile->comps[2].y0) < (OPJ_INT32)l_samples) {
1593 fprintf(stderr, "Tiles don't all have the same dimension. Skip the MCT step.\n");
1596 else if (l_tcp->mct == 2) {
1599 if (! l_tcp->m_mct_decoding_matrix) {
1603 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1608 for (i=0;i<l_tile->numcomps;++i) {
1609 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1613 if (! opj_mct_decode_custom(/* MCT data */
1614 (OPJ_BYTE*) l_tcp->m_mct_decoding_matrix,
1615 /* size of components */
1619 /* nb of components (i.e. size of pData) */
1621 /* tells if the data is signed */
1622 p_tcd->image->comps->sgnd)) {
1630 if (l_tcp->tccps->qmfbid == 1) {
1631 opj_mct_decode( l_tile->comps[0].data,
1632 l_tile->comps[1].data,
1633 l_tile->comps[2].data,
1637 opj_mct_decode_real((OPJ_FLOAT32*)l_tile->comps[0].data,
1638 (OPJ_FLOAT32*)l_tile->comps[1].data,
1639 (OPJ_FLOAT32*)l_tile->comps[2].data,
1645 /* FIXME need to use opj_event_msg function */
1646 fprintf(stderr,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",l_tile->numcomps);
1653 OPJ_BOOL opj_tcd_dc_level_shift_decode ( opj_tcd_t *p_tcd )
1656 opj_tcd_tilecomp_t * l_tile_comp = 00;
1657 opj_tccp_t * l_tccp = 00;
1658 opj_image_comp_t * l_img_comp = 00;
1659 opj_tcd_resolution_t* l_res = 00;
1660 opj_tcd_tile_t * l_tile;
1661 OPJ_UINT32 l_width,l_height,i,j;
1662 OPJ_INT32 * l_current_ptr;
1663 OPJ_INT32 l_min, l_max;
1664 OPJ_UINT32 l_stride;
1666 l_tile = p_tcd->tcd_image->tiles;
1667 l_tile_comp = l_tile->comps;
1668 l_tccp = p_tcd->tcp->tccps;
1669 l_img_comp = p_tcd->image->comps;
1671 for (compno = 0; compno < l_tile->numcomps; compno++) {
1672 l_res = l_tile_comp->resolutions + l_img_comp->resno_decoded;
1673 l_width = (OPJ_UINT32)(l_res->x1 - l_res->x0);
1674 l_height = (OPJ_UINT32)(l_res->y1 - l_res->y0);
1675 l_stride = (OPJ_UINT32)(l_tile_comp->x1 - l_tile_comp->x0) - l_width;
1677 assert(l_height == 0 || l_width + l_stride <= l_tile_comp->data_size / l_height); /*MUPDF*/
1679 if (l_img_comp->sgnd) {
1680 l_min = -(1 << (l_img_comp->prec - 1));
1681 l_max = (1 << (l_img_comp->prec - 1)) - 1;
1685 l_max = (1 << l_img_comp->prec) - 1;
1688 l_current_ptr = l_tile_comp->data;
1690 if (l_tccp->qmfbid == 1) {
1691 for (j=0;j<l_height;++j) {
1692 for (i = 0; i < l_width; ++i) {
1693 *l_current_ptr = opj_int_clamp(*l_current_ptr + l_tccp->m_dc_level_shift, l_min, l_max);
1696 l_current_ptr += l_stride;
1700 for (j=0;j<l_height;++j) {
1701 for (i = 0; i < l_width; ++i) {
1702 OPJ_FLOAT32 l_value = *((OPJ_FLOAT32 *) l_current_ptr);
1703 *l_current_ptr = opj_int_clamp((OPJ_INT32)lrintf(l_value) + l_tccp->m_dc_level_shift, l_min, l_max); ;
1706 l_current_ptr += l_stride;
1721 * Deallocates the encoding data of the given precinct.
1723 void opj_tcd_code_block_dec_deallocate (opj_tcd_precinct_t * p_precinct)
1725 OPJ_UINT32 cblkno , l_nb_code_blocks;
1727 opj_tcd_cblk_dec_t * l_code_block = p_precinct->cblks.dec;
1729 /*fprintf(stderr,"deallocate codeblock:{\n");*/
1730 /*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);*/
1731 /*fprintf(stderr,"\t numbps=%d, numlenbits=%d, len=%d, numnewpasses=%d, real_num_segs=%d, m_current_max_segs=%d\n ",
1732 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 );*/
1735 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_dec_t);
1736 /*fprintf(stderr,"nb_code_blocks =%d\t}\n", l_nb_code_blocks);*/
1738 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1740 if (l_code_block->data) {
1741 opj_free(l_code_block->data);
1742 l_code_block->data = 00;
1745 if (l_code_block->segs) {
1746 opj_free(l_code_block->segs );
1747 l_code_block->segs = 00;
1753 opj_free(p_precinct->cblks.dec);
1754 p_precinct->cblks.dec = 00;
1759 * Deallocates the encoding data of the given precinct.
1761 void opj_tcd_code_block_enc_deallocate (opj_tcd_precinct_t * p_precinct)
1763 OPJ_UINT32 cblkno , l_nb_code_blocks;
1765 opj_tcd_cblk_enc_t * l_code_block = p_precinct->cblks.enc;
1767 l_nb_code_blocks = p_precinct->block_size / sizeof(opj_tcd_cblk_enc_t);
1769 for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) {
1770 if (l_code_block->data) {
1771 opj_free(l_code_block->data - 1);
1772 l_code_block->data = 00;
1775 if (l_code_block->layers) {
1776 opj_free(l_code_block->layers );
1777 l_code_block->layers = 00;
1780 if (l_code_block->passes) {
1781 opj_free(l_code_block->passes );
1782 l_code_block->passes = 00;
1787 opj_free(p_precinct->cblks.enc);
1789 p_precinct->cblks.enc = 00;
1793 OPJ_UINT32 opj_tcd_get_encoded_tile_size ( opj_tcd_t *p_tcd )
1795 OPJ_UINT32 i,l_data_size = 0;
1796 opj_image_comp_t * l_img_comp = 00;
1797 opj_tcd_tilecomp_t * l_tilec = 00;
1798 OPJ_UINT32 l_size_comp, l_remaining;
1800 l_tilec = p_tcd->tcd_image->tiles->comps;
1801 l_img_comp = p_tcd->image->comps;
1802 for (i=0;i<p_tcd->image->numcomps;++i) {
1803 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
1804 l_remaining = l_img_comp->prec & 7; /* (%8) */
1810 if (l_size_comp == 3) {
1814 l_data_size += l_size_comp * (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
1822 OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
1825 opj_tcd_tilecomp_t * l_tile_comp = 00;
1826 opj_tccp_t * l_tccp = 00;
1827 opj_image_comp_t * l_img_comp = 00;
1828 opj_tcd_tile_t * l_tile;
1829 OPJ_UINT32 l_nb_elem,i;
1830 OPJ_INT32 * l_current_ptr;
1832 l_tile = p_tcd->tcd_image->tiles;
1833 l_tile_comp = l_tile->comps;
1834 l_tccp = p_tcd->tcp->tccps;
1835 l_img_comp = p_tcd->image->comps;
1837 for (compno = 0; compno < l_tile->numcomps; compno++) {
1838 l_current_ptr = l_tile_comp->data;
1839 l_nb_elem = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1841 if (l_tccp->qmfbid == 1) {
1842 for (i = 0; i < l_nb_elem; ++i) {
1843 *l_current_ptr -= l_tccp->m_dc_level_shift ;
1848 for (i = 0; i < l_nb_elem; ++i) {
1849 *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
1862 OPJ_BOOL opj_tcd_mct_encode ( opj_tcd_t *p_tcd )
1864 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1865 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1866 OPJ_UINT32 samples = (OPJ_UINT32)((l_tile_comp->x1 - l_tile_comp->x0) * (l_tile_comp->y1 - l_tile_comp->y0));
1868 OPJ_BYTE ** l_data = 00;
1869 opj_tcp_t * l_tcp = p_tcd->tcp;
1871 if(!p_tcd->tcp->mct) {
1875 if (p_tcd->tcp->mct == 2) {
1876 if (! p_tcd->tcp->m_mct_coding_matrix) {
1880 l_data = (OPJ_BYTE **) opj_malloc(l_tile->numcomps*sizeof(OPJ_BYTE*));
1885 for (i=0;i<l_tile->numcomps;++i) {
1886 l_data[i] = (OPJ_BYTE*) l_tile_comp->data;
1890 if (! opj_mct_encode_custom(/* MCT data */
1891 (OPJ_BYTE*) p_tcd->tcp->m_mct_coding_matrix,
1892 /* size of components */
1896 /* nb of components (i.e. size of pData) */
1898 /* tells if the data is signed */
1899 p_tcd->image->comps->sgnd) )
1907 else if (l_tcp->tccps->qmfbid == 0) {
1908 opj_mct_encode_real(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1911 opj_mct_encode(l_tile->comps[0].data, l_tile->comps[1].data, l_tile->comps[2].data, samples);
1917 OPJ_BOOL opj_tcd_dwt_encode ( opj_tcd_t *p_tcd )
1919 opj_tcd_tile_t * l_tile = p_tcd->tcd_image->tiles;
1920 opj_tcd_tilecomp_t * l_tile_comp = p_tcd->tcd_image->tiles->comps;
1921 opj_tccp_t * l_tccp = p_tcd->tcp->tccps;
1924 for (compno = 0; compno < l_tile->numcomps; ++compno) {
1925 if (l_tccp->qmfbid == 1) {
1926 if (! opj_dwt_encode(l_tile_comp)) {
1930 else if (l_tccp->qmfbid == 0) {
1931 if (! opj_dwt_encode_real(l_tile_comp)) {
1943 OPJ_BOOL opj_tcd_t1_encode ( opj_tcd_t *p_tcd )
1946 const OPJ_FLOAT64 * l_mct_norms;
1947 opj_tcp_t * l_tcp = p_tcd->tcp;
1949 l_t1 = opj_t1_create();
1954 if (l_tcp->mct == 1) {
1955 /* irreversible encoding */
1956 if (l_tcp->tccps->qmfbid == 0) {
1957 l_mct_norms = opj_mct_get_mct_norms_real();
1960 l_mct_norms = opj_mct_get_mct_norms();
1964 l_mct_norms = (const OPJ_FLOAT64 *) (l_tcp->mct_norms);
1967 if (! opj_t1_encode_cblks(l_t1, p_tcd->tcd_image->tiles , l_tcp, l_mct_norms)) {
1968 opj_t1_destroy(l_t1);
1972 opj_t1_destroy(l_t1);
1977 OPJ_BOOL opj_tcd_t2_encode (opj_tcd_t *p_tcd,
1978 OPJ_BYTE * p_dest_data,
1979 OPJ_UINT32 * p_data_written,
1980 OPJ_UINT32 p_max_dest_size,
1981 opj_codestream_info_t *p_cstr_info )
1985 l_t2 = opj_t2_create(p_tcd->image, p_tcd->cp);
1990 if (! opj_t2_encode_packets(
1993 p_tcd->tcd_image->tiles,
1994 p_tcd->tcp->numlayers,
2004 opj_t2_destroy(l_t2);
2008 opj_t2_destroy(l_t2);
2010 /*---------------CLEAN-------------------*/
2015 OPJ_BOOL opj_tcd_rate_allocate_encode( opj_tcd_t *p_tcd,
2016 OPJ_BYTE * p_dest_data,
2017 OPJ_UINT32 p_max_dest_size,
2018 opj_codestream_info_t *p_cstr_info )
2020 opj_cp_t * l_cp = p_tcd->cp;
2021 OPJ_UINT32 l_nb_written = 0;
2024 p_cstr_info->index_write = 0;
2027 if (l_cp->m_specific_param.m_enc.m_disto_alloc|| l_cp->m_specific_param.m_enc.m_fixed_quality) {
2029 /* Normal Rate/distortion allocation */
2030 if (! opj_tcd_rateallocate(p_tcd, p_dest_data,&l_nb_written, p_max_dest_size, p_cstr_info)) {
2035 /* Fixed layer allocation */
2036 opj_tcd_rateallocate_fixed(p_tcd);
2043 OPJ_BOOL opj_tcd_copy_tile_data ( opj_tcd_t *p_tcd,
2045 OPJ_UINT32 p_src_length )
2047 OPJ_UINT32 i,j,l_data_size = 0;
2048 opj_image_comp_t * l_img_comp = 00;
2049 opj_tcd_tilecomp_t * l_tilec = 00;
2050 OPJ_UINT32 l_size_comp, l_remaining;
2051 OPJ_UINT32 l_nb_elem;
2053 l_data_size = opj_tcd_get_encoded_tile_size(p_tcd);
2054 if (l_data_size != p_src_length) {
2058 l_tilec = p_tcd->tcd_image->tiles->comps;
2059 l_img_comp = p_tcd->image->comps;
2060 for (i=0;i<p_tcd->image->numcomps;++i) {
2061 l_size_comp = l_img_comp->prec >> 3; /*(/ 8)*/
2062 l_remaining = l_img_comp->prec & 7; /* (%8) */
2063 l_nb_elem = (OPJ_UINT32)((l_tilec->x1 - l_tilec->x0) * (l_tilec->y1 - l_tilec->y0));
2069 if (l_size_comp == 3) {
2073 switch (l_size_comp) {
2076 OPJ_CHAR * l_src_ptr = (OPJ_CHAR *) p_src;
2077 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2079 if (l_img_comp->sgnd) {
2080 for (j=0;j<l_nb_elem;++j) {
2081 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2085 for (j=0;j<l_nb_elem;++j) {
2086 *(l_dest_ptr++) = (*(l_src_ptr++))&0xff;
2090 p_src = (OPJ_BYTE*) l_src_ptr;
2095 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2096 OPJ_INT16 * l_src_ptr = (OPJ_INT16 *) p_src;
2098 if (l_img_comp->sgnd) {
2099 for (j=0;j<l_nb_elem;++j) {
2100 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2104 for (j=0;j<l_nb_elem;++j) {
2105 *(l_dest_ptr++) = (*(l_src_ptr++))&0xffff;
2109 p_src = (OPJ_BYTE*) l_src_ptr;
2114 OPJ_INT32 * l_src_ptr = (OPJ_INT32 *) p_src;
2115 OPJ_INT32 * l_dest_ptr = l_tilec->data;
2117 for (j=0;j<l_nb_elem;++j) {
2118 *(l_dest_ptr++) = (OPJ_INT32) (*(l_src_ptr++));
2121 p_src = (OPJ_BYTE*) l_src_ptr;