changed one of the 9-7 coefficients (value closer to the one in the std)
[openjpeg.git] / libopenjpeg / tcd.c
1 /*
2  * Copyright (c) 2001-2002, David Janssens
3  * Copyright (c) 2002-2004, Yannick Verschueren
4  * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "tcd.h"
30 #include "int.h"
31 #include "t1.h"
32 #include "t2.h"
33 #include "dwt.h"
34 #include "mct.h"
35 #include <setjmp.h>
36 #include <float.h>
37 #include <stdio.h>
38 #include <time.h>
39 #include <math.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 static tcd_image_t tcd_image;
44
45 static j2k_image_t *tcd_img;
46 static j2k_cp_t *tcd_cp;
47
48 static tcd_tile_t *tcd_tile;
49 static j2k_tcp_t *tcd_tcp;
50 static int tcd_tileno;
51
52 static tcd_tile_t *tile;
53 static tcd_tilecomp_t *tilec;
54 static tcd_resolution_t *res;
55 static tcd_band_t *band;
56 static tcd_precinct_t *prc;
57 static tcd_cblk_t *cblk;
58
59 extern jmp_buf j2k_error;
60
61 void tcd_dump(tcd_image_t * img, int curtileno)
62 {
63   int tileno, compno, resno, bandno, precno, cblkno;
64   fprintf(stdout, "image {\n");
65   fprintf(stdout, "  tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n", img->tw,
66           img->th, tcd_img->x0, tcd_img->x1, tcd_img->y0, tcd_img->y1);
67   for (tileno = 0; tileno < img->th * img->tw; tileno++) {
68     tcd_tile_t *tile = &tcd_image.tiles[tileno];
69     fprintf(stdout, "  tile {\n");
70     fprintf(stdout, "    x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
71             tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
72     for (compno = 0; compno < tile->numcomps; compno++) {
73       tcd_tilecomp_t *tilec = &tile->comps[compno];
74       fprintf(stdout, "    tilec {\n");
75       fprintf(stdout,
76               "      x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
77               tilec->x0, tilec->y0, tilec->x1, tilec->y1,
78               tilec->numresolutions);
79       for (resno = 0; resno < tilec->numresolutions; resno++) {
80         tcd_resolution_t *res = &tilec->resolutions[resno];
81         fprintf(stdout, "\n   res {\n");
82         fprintf(stdout,
83                 "          x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
84                 res->x0, res->y0, res->x1, res->y1, res->pw, res->ph,
85                 res->numbands);
86         for (bandno = 0; bandno < res->numbands; bandno++) {
87           tcd_band_t *band = &res->bands[bandno];
88           fprintf(stdout, "        band {\n");
89           fprintf(stdout,
90                   "          x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
91                   band->x0, band->y0, band->x1, band->y1,
92                   band->stepsize, band->numbps);
93           for (precno = 0; precno < res->pw * res->ph; precno++) {
94             tcd_precinct_t *prec = &band->precincts[precno];
95             fprintf(stdout, "          prec {\n");
96             fprintf(stdout,
97                     "            x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
98                     prec->x0, prec->y0, prec->x1, prec->y1,
99                     prec->cw, prec->ch);
100             for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
101               tcd_cblk_t *cblk = &prec->cblks[cblkno];
102               fprintf(stdout, "            cblk {\n");
103               fprintf(stdout,
104                       "              x0=%d, y0=%d, x1=%d, y1=%d\n",
105                       cblk->x0, cblk->y0, cblk->x1, cblk->y1);
106               fprintf(stdout, "            }\n");
107             }
108             fprintf(stdout, "          }\n");
109           }
110           fprintf(stdout, "        }\n");
111         }
112         fprintf(stdout, "      }\n");
113       }
114       fprintf(stdout, "    }\n");
115     }
116     fprintf(stdout, "  }\n");
117   }
118   fprintf(stdout, "}\n");
119 }
120
121 void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
122 {
123   int tileno, compno, resno, bandno, precno, cblkno;
124   tcd_img = img;
125   tcd_cp = cp;
126   tcd_image.tw = cp->tw;
127   tcd_image.th = cp->th;
128   tcd_image.tiles = (tcd_tile_t *) malloc(sizeof(tcd_tile_t));
129
130   for (tileno = 0; tileno < 1; tileno++) {
131     j2k_tcp_t *tcp = &cp->tcps[curtileno];
132     int j;
133     /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
134     int p = curtileno % cp->tw; /* si numerotation matricielle .. */
135     int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
136     /* tcd_tile_t *tile=&tcd_image.tiles[tileno]; */
137     tile = tcd_image.tiles;
138     /* 4 borders of the tile rescale on the image if necessary */
139     tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
140     tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
141     tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
142     tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
143     tile->numcomps = img->numcomps;
144     /* tile->PPT=img->PPT;  */
145     /* Modification of the RATE >> */
146     for (j = 0; j < tcp->numlayers; j++) {
147       tcp->rates[j] = tcp->rates[j] ? int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0;  //Mod antonin losslessbug
148       if (tcp->rates[j]) {
149         if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
150           tcp->rates[j] = tcp->rates[j - 1] + 20;
151         } else {
152           if (!j && tcp->rates[j] < 30)
153             tcp->rates[j] = 30;
154         }
155       }
156     }
157     /* << Modification of the RATE */
158
159     tile->comps =
160       (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
161     for (compno = 0; compno < tile->numcomps; compno++) {
162       j2k_tccp_t *tccp = &tcp->tccps[compno];
163       /* tcd_tilecomp_t *tilec=&tile->comps[compno]; */
164       tilec = &tile->comps[compno];
165       /* border of each tile component (global) */
166       tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
167
168       tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
169       tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
170       tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
171
172       tilec->data =
173         (int *) malloc((tilec->x1 - tilec->x0) *
174                        (tilec->y1 - tilec->y0) * sizeof(int));
175       tilec->numresolutions = tccp->numresolutions;
176
177       tilec->resolutions =
178         (tcd_resolution_t *) malloc(tilec->numresolutions *
179                                     sizeof(tcd_resolution_t));
180
181       for (resno = 0; resno < tilec->numresolutions; resno++) {
182         int pdx, pdy;
183         int levelno = tilec->numresolutions - 1 - resno;
184         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
185         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
186         int cbgwidthexpn, cbgheightexpn;
187         int cblkwidthexpn, cblkheightexpn;
188         /* tcd_resolution_t *res=&tilec->resolutions[resno]; */
189
190         res = &tilec->resolutions[resno];
191
192         /* border for each resolution level (global) */
193         res->x0 = int_ceildivpow2(tilec->x0, levelno);
194         res->y0 = int_ceildivpow2(tilec->y0, levelno);
195         res->x1 = int_ceildivpow2(tilec->x1, levelno);
196         res->y1 = int_ceildivpow2(tilec->y1, levelno);
197
198         res->numbands = resno == 0 ? 1 : 3;
199         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
200         if (tccp->csty & J2K_CCP_CSTY_PRT) {
201           pdx = tccp->prcw[resno];
202           pdy = tccp->prch[resno];
203         } else {
204           pdx = 15;
205           pdy = 15;
206         }
207         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
208         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
209         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
210         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
211         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
212
213         res->pw = (brprcxend - tlprcxstart) >> pdx;
214         res->ph = (brprcyend - tlprcystart) >> pdy;
215
216         if (resno == 0) {
217           tlcbgxstart = tlprcxstart;
218           tlcbgystart = tlprcystart;
219           brcbgxend = brprcxend;
220           brcbgyend = brprcyend;
221           cbgwidthexpn = pdx;
222           cbgheightexpn = pdy;
223         } else {
224           tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
225           tlcbgystart = int_ceildivpow2(tlprcystart, 1);
226           brcbgxend = int_ceildivpow2(brprcxend, 1);
227           brcbgyend = int_ceildivpow2(brprcyend, 1);
228           cbgwidthexpn = pdx - 1;
229           cbgheightexpn = pdy - 1;
230         }
231
232         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
233         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
234
235         for (bandno = 0; bandno < res->numbands; bandno++) {
236           int x0b, y0b, i;
237           int gain, numbps;
238           j2k_stepsize_t *ss;
239           band = &res->bands[bandno];
240           band->bandno = resno == 0 ? 0 : bandno + 1;
241           x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
242           y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
243
244           if (band->bandno == 0) {
245             /* band border (global) */
246             band->x0 = int_ceildivpow2(tilec->x0, levelno);
247             band->y0 = int_ceildivpow2(tilec->y0, levelno);
248             band->x1 = int_ceildivpow2(tilec->x1, levelno);
249             band->y1 = int_ceildivpow2(tilec->y1, levelno);
250           } else {
251             /* band border (global) */
252             band->x0 =
253               int_ceildivpow2(tilec->x0 -
254                               (1 << levelno) * x0b, levelno + 1);
255             band->y0 =
256               int_ceildivpow2(tilec->y0 -
257                               (1 << levelno) * y0b, levelno + 1);
258             band->x1 =
259               int_ceildivpow2(tilec->x1 -
260                               (1 << levelno) * x0b, levelno + 1);
261             band->y1 =
262               int_ceildivpow2(tilec->y1 -
263                               (1 << levelno) * y0b, levelno + 1);
264
265           }
266
267           ss = &tccp->stepsizes[resno ==
268                                 0 ? 0 : 3 * (resno - 1) + bandno + 1];
269           gain =
270             tccp->qmfbid ==
271             0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
272           numbps = img->comps[compno].prec + gain;
273           band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
274           band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
275
276           band->precincts =
277             (tcd_precinct_t *) malloc(3 * res->pw * res->ph *
278                                       sizeof(tcd_precinct_t));
279
280           for (i = 0; i < res->pw * res->ph * 3; i++) {
281             band->precincts[i].imsbtree = NULL;
282             band->precincts[i].incltree = NULL;
283           }
284
285           for (precno = 0; precno < res->pw * res->ph; precno++) {
286             int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
287             int cbgxstart =
288               tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
289             int cbgystart =
290               tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
291             int cbgxend = cbgxstart + (1 << cbgwidthexpn);
292             int cbgyend = cbgystart + (1 << cbgheightexpn);
293             /* tcd_precinct_t *prc=&band->precincts[precno]; */
294             prc = &band->precincts[precno];
295             /* precinct size (global) */
296             prc->x0 = int_max(cbgxstart, band->x0);
297             prc->y0 = int_max(cbgystart, band->y0);
298             prc->x1 = int_min(cbgxend, band->x1);
299             prc->y1 = int_min(cbgyend, band->y1);
300
301             tlcblkxstart =
302               int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
303             tlcblkystart =
304               int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
305             brcblkxend =
306               int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
307             brcblkyend =
308               int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
309             prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
310             prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
311
312             prc->cblks =
313               (tcd_cblk_t *) malloc((prc->cw * prc->ch) *
314                                     sizeof(tcd_cblk_t));
315             prc->incltree = tgt_create(prc->cw, prc->ch);
316             prc->imsbtree = tgt_create(prc->cw, prc->ch);
317
318             for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
319               int cblkxstart =
320                 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
321               int cblkystart =
322                 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
323               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
324               int cblkyend = cblkystart + (1 << cblkheightexpn);
325
326               cblk = &prc->cblks[cblkno];
327               /* code-block size (global) */
328               cblk->x0 = int_max(cblkxstart, prc->x0);
329               cblk->y0 = int_max(cblkystart, prc->y0);
330               cblk->x1 = int_min(cblkxend, prc->x1);
331               cblk->y1 = int_min(cblkyend, prc->y1);
332             }
333           }
334         }
335       }
336     }
337   }
338   /* tcd_dump(&tcd_image,curtileno); */
339 }
340
341 void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
342 {
343   int tileno, compno, resno, bandno, precno;
344   tcd_img = img;
345   tcd_cp = cp;
346   tcd_image.tw = cp->tw;
347   tcd_image.th = cp->th;
348   for (tileno = 0; tileno < 1; tileno++) {
349     /* j2k_tcp_t *tcp=&cp->tcps[curtileno]; */
350     tile = tcd_image.tiles;
351     for (compno = 0; compno < tile->numcomps; compno++) {
352       tilec = &tile->comps[compno];
353       for (resno = 0; resno < tilec->numresolutions; resno++) {
354         res = &tilec->resolutions[resno];
355         for (bandno = 0; bandno < res->numbands; bandno++) {
356           band = &res->bands[bandno];
357           for (precno = 0; precno < res->pw * res->ph; precno++) {
358             prc = &band->precincts[precno];
359
360             if (prc->incltree != NULL)
361               tgt_destroy(prc->incltree);
362             if (prc->imsbtree != NULL)
363               tgt_destroy(prc->imsbtree);
364             free(prc->cblks);
365           }                     /* for (precno */
366           free(band->precincts);
367         }                       /* for (bandno */
368       }                         /* for (resno */
369       free(tilec->resolutions);
370     }                           /* for (compno */
371     free(tile->comps);
372   }                             /* for (tileno */
373   free(tcd_image.tiles);
374 }
375
376 void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno)
377 {
378   int tileno, compno, resno, bandno, precno, cblkno;
379
380   for (tileno = 0; tileno < 1; tileno++) {
381     j2k_tcp_t *tcp = &cp->tcps[curtileno];
382     int j;
383     //              int previous_x0, previous_x1, previous_y0, previous_y1;
384     /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
385     int p = curtileno % cp->tw;
386     int q = curtileno / cp->tw;
387     tile = tcd_image.tiles;
388
389     /* 4 borders of the tile rescale on the image if necessary */
390     tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
391     tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
392     tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
393     tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
394
395     tile->numcomps = img->numcomps;
396     /* tile->PPT=img->PPT; */
397     /* Modification of the RATE >> */
398     for (j = 0; j < tcp->numlayers; j++) {
399       tcp->rates[j] = tcp->rates[j] ? int_ceildiv(tile->numcomps * (tile->x1 - tile->x0) * (tile->y1 - tile->y0) * img->comps[0].prec, (tcp->rates[j] * 8 * img->comps[0].dx * img->comps[0].dy)) : 0;  //Mod antonin losslessbug
400       if (tcp->rates[j]) {
401         if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
402           tcp->rates[j] = tcp->rates[j - 1] + 20;
403         } else {
404           if (!j && tcp->rates[j] < 30)
405             tcp->rates[j] = 30;
406         }
407       }
408     }
409     /* << Modification of the RATE */
410     /* tile->comps=(tcd_tilecomp_t*)realloc(tile->comps,img->numcomps*sizeof(tcd_tilecomp_t)); */
411     for (compno = 0; compno < tile->numcomps; compno++) {
412       j2k_tccp_t *tccp = &tcp->tccps[compno];
413       /* int realloc_op; */
414
415       tilec = &tile->comps[compno];
416       /* border of each tile component (global) */
417       tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
418       tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
419       tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
420       tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
421
422       tilec->data =
423         (int *) malloc((tilec->x1 - tilec->x0) *
424                        (tilec->y1 - tilec->y0) * sizeof(int));
425       tilec->numresolutions = tccp->numresolutions;
426       /* tilec->resolutions=(tcd_resolution_t*)realloc(tilec->resolutions,tilec->numresolutions*sizeof(tcd_resolution_t)); */
427       for (resno = 0; resno < tilec->numresolutions; resno++) {
428         int pdx, pdy;
429         int levelno = tilec->numresolutions - 1 - resno;
430         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
431         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
432         int cbgwidthexpn, cbgheightexpn;
433         int cblkwidthexpn, cblkheightexpn;
434
435         res = &tilec->resolutions[resno];
436         /* border for each resolution level (global) */
437         res->x0 = int_ceildivpow2(tilec->x0, levelno);
438         res->y0 = int_ceildivpow2(tilec->y0, levelno);
439         res->x1 = int_ceildivpow2(tilec->x1, levelno);
440         res->y1 = int_ceildivpow2(tilec->y1, levelno);
441
442         res->numbands = resno == 0 ? 1 : 3;
443         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
444         if (tccp->csty & J2K_CCP_CSTY_PRT) {
445           pdx = tccp->prcw[resno];
446           pdy = tccp->prch[resno];
447         } else {
448           pdx = 15;
449           pdy = 15;
450         }
451         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
452         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
453         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
454         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
455         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
456
457         res->pw = (brprcxend - tlprcxstart) >> pdx;
458         res->ph = (brprcyend - tlprcystart) >> pdy;
459
460         if (resno == 0) {
461           tlcbgxstart = tlprcxstart;
462           tlcbgystart = tlprcystart;
463           brcbgxend = brprcxend;
464           brcbgyend = brprcyend;
465           cbgwidthexpn = pdx;
466           cbgheightexpn = pdy;
467         } else {
468           tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
469           tlcbgystart = int_ceildivpow2(tlprcystart, 1);
470           brcbgxend = int_ceildivpow2(brprcxend, 1);
471           brcbgyend = int_ceildivpow2(brprcyend, 1);
472           cbgwidthexpn = pdx - 1;
473           cbgheightexpn = pdy - 1;
474         }
475
476         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
477         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
478
479         for (bandno = 0; bandno < res->numbands; bandno++) {
480           int x0b, y0b;
481           int gain, numbps;
482           j2k_stepsize_t *ss;
483           band = &res->bands[bandno];
484           band->bandno = resno == 0 ? 0 : bandno + 1;
485           x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
486           y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
487
488           if (band->bandno == 0) {
489             /* band border */
490             band->x0 = int_ceildivpow2(tilec->x0, levelno);
491             band->y0 = int_ceildivpow2(tilec->y0, levelno);
492             band->x1 = int_ceildivpow2(tilec->x1, levelno);
493             band->y1 = int_ceildivpow2(tilec->y1, levelno);
494           } else {
495             band->x0 =
496               int_ceildivpow2(tilec->x0 -
497                               (1 << levelno) * x0b, levelno + 1);
498             band->y0 =
499               int_ceildivpow2(tilec->y0 -
500                               (1 << levelno) * y0b, levelno + 1);
501             band->x1 =
502               int_ceildivpow2(tilec->x1 -
503                               (1 << levelno) * x0b, levelno + 1);
504             band->y1 =
505               int_ceildivpow2(tilec->y1 -
506                               (1 << levelno) * y0b, levelno + 1);
507           }
508
509           ss = &tccp->stepsizes[resno ==
510                                 0 ? 0 : 3 * (resno - 1) + bandno + 1];
511           gain =
512             tccp->qmfbid ==
513             0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
514           numbps = img->comps[compno].prec + gain;
515           band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
516           band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
517
518           for (precno = 0; precno < res->pw * res->ph; precno++) {
519             int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
520             int cbgxstart =
521               tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
522             int cbgystart =
523               tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
524             int cbgxend = cbgxstart + (1 << cbgwidthexpn);
525             int cbgyend = cbgystart + (1 << cbgheightexpn);
526
527             prc = &band->precincts[precno];
528             /* precinct size (global) */
529             prc->x0 = int_max(cbgxstart, band->x0);
530             prc->y0 = int_max(cbgystart, band->y0);
531             prc->x1 = int_min(cbgxend, band->x1);
532             prc->y1 = int_min(cbgyend, band->y1);
533
534             tlcblkxstart =
535               int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
536             tlcblkystart =
537               int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
538             brcblkxend =
539               int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
540             brcblkyend =
541               int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
542             prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
543             prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
544
545             free(prc->cblks);
546             prc->cblks =
547               (tcd_cblk_t *) malloc(prc->cw * prc->ch *
548                                     sizeof(tcd_cblk_t));
549
550             if (prc->incltree != NULL)
551               tgt_destroy(prc->incltree);
552             if (prc->imsbtree != NULL)
553               tgt_destroy(prc->imsbtree);
554
555             prc->incltree = tgt_create(prc->cw, prc->ch);
556             prc->imsbtree = tgt_create(prc->cw, prc->ch);
557
558             for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
559               int cblkxstart =
560                 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
561               int cblkystart =
562                 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
563               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
564               int cblkyend = cblkystart + (1 << cblkheightexpn);
565               cblk = &prc->cblks[cblkno];
566
567               /* code-block size (global) */
568               cblk->x0 = int_max(cblkxstart, prc->x0);
569               cblk->y0 = int_max(cblkystart, prc->y0);
570               cblk->x1 = int_min(cblkxend, prc->x1);
571               cblk->y1 = int_min(cblkyend, prc->y1);
572
573             }
574           }
575         }
576       }
577     }
578   }
579   /* tcd_dump(&tcd_image,0); */
580 }
581
582 void tcd_init(j2k_image_t * img, j2k_cp_t * cp)
583 {
584   int tileno, compno, resno, bandno, precno, cblkno, i, j, p, q;
585   unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
586   tcd_img = img;
587   tcd_cp = cp;
588   tcd_image.tw = cp->tw;
589   tcd_image.th = cp->th;
590   tcd_image.tiles =
591     (tcd_tile_t *) malloc(cp->tw * cp->th * sizeof(tcd_tile_t));
592
593   /*for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
594      j2k_tcp_t *tcp = &cp->tcps[tileno];
595      tcd_tile_t *tile = &tcd_image.tiles[tileno]; */
596
597   for (i = 0; i < cp->tileno_size; i++) {
598     j2k_tcp_t *tcp = &cp->tcps[cp->tileno[i]];
599     tcd_tile_t *tile = &tcd_image.tiles[cp->tileno[i]];
600     tileno = cp->tileno[i];
601
602
603     //              int previous_x0, previous_x1, previous_y0, previous_y1;
604     /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
605     p = tileno % cp->tw;        /* si numerotation matricielle .. */
606     q = tileno / cp->tw;        /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
607
608     /* 4 borders of the tile rescale on the image if necessary */
609     tile->x0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
610     tile->y0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
611     tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
612     tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, img->y1);
613
614     tile->numcomps = img->numcomps;
615     tile->comps =
616       (tcd_tilecomp_t *) malloc(img->numcomps * sizeof(tcd_tilecomp_t));
617     for (compno = 0; compno < tile->numcomps; compno++) {
618       j2k_tccp_t *tccp = &tcp->tccps[compno];
619       tcd_tilecomp_t *tilec = &tile->comps[compno];
620       /* border of each tile component (global) */
621       tilec->x0 = int_ceildiv(tile->x0, img->comps[compno].dx);
622       tilec->y0 = int_ceildiv(tile->y0, img->comps[compno].dy);
623       tilec->x1 = int_ceildiv(tile->x1, img->comps[compno].dx);
624       tilec->y1 = int_ceildiv(tile->y1, img->comps[compno].dy);
625
626       tilec->data =
627         (int *) malloc((tilec->x1 - tilec->x0) *
628                        (tilec->y1 - tilec->y0) * sizeof(int));
629       tilec->numresolutions = tccp->numresolutions;
630       tilec->resolutions =
631         (tcd_resolution_t *) malloc(tilec->numresolutions *
632                                     sizeof(tcd_resolution_t));
633       for (resno = 0; resno < tilec->numresolutions; resno++) {
634         int pdx, pdy;
635         int levelno = tilec->numresolutions - 1 - resno;
636         int tlprcxstart, tlprcystart, brprcxend, brprcyend;
637         int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
638         int cbgwidthexpn, cbgheightexpn;
639         int cblkwidthexpn, cblkheightexpn;
640         tcd_resolution_t *res = &tilec->resolutions[resno];
641
642         /* border for each resolution level (global) */
643         res->x0 = int_ceildivpow2(tilec->x0, levelno);
644         res->y0 = int_ceildivpow2(tilec->y0, levelno);
645         res->x1 = int_ceildivpow2(tilec->x1, levelno);
646         res->y1 = int_ceildivpow2(tilec->y1, levelno);
647
648         res->numbands = resno == 0 ? 1 : 3;
649         /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
650         if (tccp->csty & J2K_CCP_CSTY_PRT) {
651           pdx = tccp->prcw[resno];
652           pdy = tccp->prch[resno];
653         } else {
654           pdx = 15;
655           pdy = 15;
656         }
657         /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000)  */
658         tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
659         tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
660         brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
661         brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
662         res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);        // Mod Antonin : sizebug1
663         res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);        // Mod Antonin : sizebug1
664
665         if (resno == 0) {
666           tlcbgxstart = tlprcxstart;
667           tlcbgystart = tlprcystart;
668           brcbgxend = brprcxend;
669           brcbgyend = brprcyend;
670           cbgwidthexpn = pdx;
671           cbgheightexpn = pdy;
672         } else {
673           tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
674           tlcbgystart = int_ceildivpow2(tlprcystart, 1);
675           brcbgxend = int_ceildivpow2(brprcxend, 1);
676           brcbgyend = int_ceildivpow2(brprcyend, 1);
677           cbgwidthexpn = pdx - 1;
678           cbgheightexpn = pdy - 1;
679         }
680
681         cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
682         cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
683
684         for (bandno = 0; bandno < res->numbands; bandno++) {
685           int x0b, y0b;
686           int gain, numbps;
687           j2k_stepsize_t *ss;
688           tcd_band_t *band = &res->bands[bandno];
689           band->bandno = resno == 0 ? 0 : bandno + 1;
690           x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
691           y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
692
693           if (band->bandno == 0) {
694             /* band border (global) */
695             band->x0 = int_ceildivpow2(tilec->x0, levelno);
696             band->y0 = int_ceildivpow2(tilec->y0, levelno);
697             band->x1 = int_ceildivpow2(tilec->x1, levelno);
698             band->y1 = int_ceildivpow2(tilec->y1, levelno);
699           } else {
700             /* band border (global) */
701             band->x0 =
702               int_ceildivpow2(tilec->x0 -
703                               (1 << levelno) * x0b, levelno + 1);
704             band->y0 =
705               int_ceildivpow2(tilec->y0 -
706                               (1 << levelno) * y0b, levelno + 1);
707             band->x1 =
708               int_ceildivpow2(tilec->x1 -
709                               (1 << levelno) * x0b, levelno + 1);
710             band->y1 =
711               int_ceildivpow2(tilec->y1 -
712                               (1 << levelno) * y0b, levelno + 1);
713           }
714
715           ss = &tccp->stepsizes[resno ==
716                                 0 ? 0 : 3 * (resno - 1) + bandno + 1];
717           gain =
718             tccp->qmfbid ==
719             0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
720           numbps = img->comps[compno].prec + gain;
721           band->stepsize = (1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn);
722           band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
723
724           band->precincts =
725             (tcd_precinct_t *) malloc(res->pw * res->ph *
726                                       sizeof(tcd_precinct_t));
727
728           for (precno = 0; precno < res->pw * res->ph; precno++) {
729             int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
730             int cbgxstart =
731               tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
732             int cbgystart =
733               tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
734             int cbgxend = cbgxstart + (1 << cbgwidthexpn);
735             int cbgyend = cbgystart + (1 << cbgheightexpn);
736             tcd_precinct_t *prc = &band->precincts[precno];
737             /* precinct size (global) */
738             prc->x0 = int_max(cbgxstart, band->x0);
739             prc->y0 = int_max(cbgystart, band->y0);
740             prc->x1 = int_min(cbgxend, band->x1);
741             prc->y1 = int_min(cbgyend, band->y1);
742
743             tlcblkxstart =
744               int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
745             tlcblkystart =
746               int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
747             brcblkxend =
748               int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
749             brcblkyend =
750               int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
751             prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
752             prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
753
754             prc->cblks =
755               (tcd_cblk_t *) malloc(prc->cw * prc->ch *
756                                     sizeof(tcd_cblk_t));
757
758             prc->incltree = tgt_create(prc->cw, prc->ch);
759             prc->imsbtree = tgt_create(prc->cw, prc->ch);
760
761             for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
762               int cblkxstart =
763                 tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
764               int cblkystart =
765                 tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
766               int cblkxend = cblkxstart + (1 << cblkwidthexpn);
767               int cblkyend = cblkystart + (1 << cblkheightexpn);
768               tcd_cblk_t *cblk = &prc->cblks[cblkno];
769               /* code-block size (global) */
770               cblk->x0 = int_max(cblkxstart, prc->x0);
771               cblk->y0 = int_max(cblkystart, prc->y0);
772               cblk->x1 = int_min(cblkxend, prc->x1);
773               cblk->y1 = int_min(cblkyend, prc->y1);
774             }
775           }
776         }
777       }
778     }
779   }
780   //tcd_dump(&tcd_image,0);
781
782
783   /* Allocate place to store the data decoded = final image */
784   /* Place limited by the tile really present in the codestream */
785
786
787   for (i = 0; i < img->numcomps; i++) {
788     for (j = 0; j < cp->tileno_size; j++) {
789       tileno = cp->tileno[j];
790       x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
791                                                                   (unsigned int) 
792                                                                   tcd_image.
793                                                                   tiles
794                                                                   [tileno].
795                                                                   comps
796                                                                   [i].x0);
797       y0 =
798         j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
799                                                                (unsigned int) 
800                                                                tcd_image.
801                                                                tiles
802                                                                [tileno].
803                                                                comps[i].
804                                                                y0);
805       x1 =
806         j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
807                                                                (unsigned int) 
808                                                                tcd_image.
809                                                                tiles
810                                                                [tileno].
811                                                                comps[i].
812                                                                x1);
813       y1 =
814         j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
815                                                                (unsigned int) 
816                                                                tcd_image.
817                                                                tiles
818                                                                [tileno].
819                                                                comps[i].
820                                                                y1);
821     }
822
823     w = x1 - x0;
824
825     h = y1 - y0;
826     img->comps[i].data = (int *) calloc(w * h, sizeof(int));
827     img->comps[i].w = w;
828     img->comps[i].h = h;
829     img->comps[i].x0 = x0;
830     img->comps[i].y0 = y0;
831   }
832 }
833
834 void tcd_makelayer_fixed(int layno, int final)
835 {
836   int compno, resno, bandno, precno, cblkno;
837   int value;                    //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
838   int matrice[10][10][3];
839   int i, j, k;
840
841   /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
842
843   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
844     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
845     for (i = 0; i < tcd_tcp->numlayers; i++) {
846       for (j = 0; j < tilec->numresolutions; j++) {
847         for (k = 0; k < 3; k++) {
848           matrice[i][j][k] =
849             (int) (tcd_cp->
850                    matrice[i * tilec->numresolutions * 3 +
851                            j * 3 +
852                            k] *
853                    (float) (tcd_img->comps[compno].prec / 16.0));
854     }}}
855
856     for (resno = 0; resno < tilec->numresolutions; resno++) {
857       tcd_resolution_t *res = &tilec->resolutions[resno];
858       for (bandno = 0; bandno < res->numbands; bandno++) {
859         tcd_band_t *band = &res->bands[bandno];
860         for (precno = 0; precno < res->pw * res->ph; precno++) {
861           tcd_precinct_t *prc = &band->precincts[precno];
862           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
863             tcd_cblk_t *cblk = &prc->cblks[cblkno];
864             tcd_layer_t *layer = &cblk->layers[layno];
865             int n;
866             int imsb = tcd_img->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */
867             /* Correction of the matrix of coefficient to include the IMSB information */
868
869             if (layno == 0) {
870               value = matrice[layno][resno][bandno];
871               if (imsb >= value)
872                 value = 0;
873               else
874                 value -= imsb;
875             } else {
876               value =
877                 matrice[layno][resno][bandno] -
878                 matrice[layno - 1][resno][bandno];
879               if (imsb >= matrice[layno - 1][resno][bandno]) {
880                 value -= (imsb - matrice[layno - 1][resno][bandno]);
881                 if (value < 0)
882                   value = 0;
883               }
884             }
885
886             if (layno == 0)
887               cblk->numpassesinlayers = 0;
888
889             n = cblk->numpassesinlayers;
890             if (cblk->numpassesinlayers == 0) {
891               if (value != 0)
892                 n = 3 * value - 2 + cblk->numpassesinlayers;
893               else
894                 n = cblk->numpassesinlayers;
895             } else
896               n = 3 * value + cblk->numpassesinlayers;
897
898             layer->numpasses = n - cblk->numpassesinlayers;
899
900             if (!layer->numpasses)
901               continue;
902
903             if (cblk->numpassesinlayers == 0) {
904               layer->len = cblk->passes[n - 1].rate;
905               layer->data = cblk->data;
906             } else {
907               layer->len =
908                 cblk->passes[n - 1].rate -
909                 cblk->passes[cblk->numpassesinlayers - 1].rate;
910               layer->data =
911                 cblk->data +
912                 cblk->passes[cblk->numpassesinlayers - 1].rate;
913             }
914             if (final)
915               cblk->numpassesinlayers = n;
916           }
917         }
918       }
919     }
920   }
921 }
922
923 void tcd_rateallocate_fixed()
924 {
925   int layno;
926
927   for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
928     tcd_makelayer_fixed(layno, 1);
929   }
930 }
931
932 void tcd_makelayer(int layno, double thresh, int final)
933 {
934   int compno, resno, bandno, precno, cblkno, passno;
935
936   tcd_tile->distolayer[layno] = 0;      //add fixed_quality
937
938   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
939     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
940     for (resno = 0; resno < tilec->numresolutions; resno++) {
941       tcd_resolution_t *res = &tilec->resolutions[resno];
942       for (bandno = 0; bandno < res->numbands; bandno++) {
943         tcd_band_t *band = &res->bands[bandno];
944         for (precno = 0; precno < res->pw * res->ph; precno++) {
945           tcd_precinct_t *prc = &band->precincts[precno];
946           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
947             tcd_cblk_t *cblk = &prc->cblks[cblkno];
948             tcd_layer_t *layer = &cblk->layers[layno];
949             int n;
950
951             if (layno == 0) {
952               cblk->numpassesinlayers = 0;
953             }
954             n = cblk->numpassesinlayers;
955             for (passno = cblk->numpassesinlayers;
956                  passno < cblk->totalpasses; passno++) {
957               int dr;
958               double dd;
959               tcd_pass_t *pass = &cblk->passes[passno];
960               if (n == 0) {
961                 dr = pass->rate;
962                 dd = pass->distortiondec;
963               } else {
964                 dr = pass->rate - cblk->passes[n - 1].rate;
965                 dd = pass->distortiondec - cblk->passes[n -
966                                                         1].distortiondec;
967               }
968               if (!dr) {
969                 if (dd)
970                   n = passno + 1;
971                 continue;
972               }
973
974               if (dd / dr >= thresh)
975                 n = passno + 1;
976             }
977             layer->numpasses = n - cblk->numpassesinlayers;
978
979             if (!layer->numpasses) {
980               layer->disto = 0;
981               continue;
982             }
983
984             if (cblk->numpassesinlayers == 0) {
985               layer->len = cblk->passes[n - 1].rate;
986               layer->data = cblk->data;
987               layer->disto = cblk->passes[n - 1].distortiondec;
988             } else {
989               layer->len = cblk->passes[n - 1].rate -
990                 cblk->passes[cblk->numpassesinlayers - 1].rate;
991               layer->data =
992                 cblk->data +
993                 cblk->passes[cblk->numpassesinlayers - 1].rate;
994               layer->disto =
995                 cblk->passes[n - 1].distortiondec -
996                 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
997             }
998
999             tcd_tile->distolayer[layno] += layer->disto;        //add fixed_quality
1000
1001             if (final)
1002               cblk->numpassesinlayers = n;
1003           }
1004         }
1005       }
1006     }
1007   }
1008 }
1009
1010 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
1011 {
1012   int compno, resno, bandno, precno, cblkno, passno, layno;
1013   double min, max;
1014   double cumdisto[100];         //add fixed_quality
1015   const double K = 1;           // 1.1; //add fixed_quality
1016
1017   double maxSE = 0;
1018   min = DBL_MAX;
1019   max = 0;
1020
1021   tcd_tile->nbpix = 0;          //add fixed_quality
1022
1023   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1024     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1025
1026     tilec->nbpix = 0;
1027     for (resno = 0; resno < tilec->numresolutions; resno++) {
1028       tcd_resolution_t *res = &tilec->resolutions[resno];
1029       for (bandno = 0; bandno < res->numbands; bandno++) {
1030         tcd_band_t *band = &res->bands[bandno];
1031         for (precno = 0; precno < res->pw * res->ph; precno++) {
1032           tcd_precinct_t *prc = &band->precincts[precno];
1033           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1034             tcd_cblk_t *cblk = &prc->cblks[cblkno];
1035             for (passno = 0; passno < cblk->totalpasses; passno++) {
1036               tcd_pass_t *pass = &cblk->passes[passno];
1037               int dr;
1038               double dd, rdslope;
1039               if (passno == 0) {
1040                 dr = pass->rate;
1041                 dd = pass->distortiondec;
1042               } else {
1043                 dr = pass->rate - cblk->passes[passno - 1].rate;
1044                 dd = pass->distortiondec -
1045                   cblk->passes[passno - 1].distortiondec;
1046               }
1047               if (dr == 0) {
1048                 continue;
1049               }
1050
1051               rdslope = dd / dr;
1052
1053               if (rdslope < min) {
1054                 min = rdslope;
1055               }
1056               if (rdslope > max) {
1057                 max = rdslope;
1058               }
1059             }                   /* passno */
1060
1061             tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1062
1063             tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));    //add fixed_quality
1064
1065           }                     /* cbklno */
1066         }                       /* precno */
1067       }                         /* bandno */
1068     }                           /* resno */
1069     maxSE += (((double)(1 << tcd_img->comps[compno].prec) - 1.0) * ((double)(1 << tcd_img->comps[compno].prec) -1.0)) * ((double)(tilec->nbpix));
1070   }                             /* compno */
1071
1072   /* add antonin index */
1073   if (info_IM->index_on) {
1074     info_tile *info_TL = &info_IM->tile[tcd_tileno];
1075     info_TL->nbpix = tcd_tile->nbpix;
1076     info_TL->distotile = tcd_tile->distotile;
1077     info_TL->thresh =
1078       (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1079   }
1080   /* dda */
1081
1082   for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1083     volatile double lo = min;
1084     volatile double hi = max;
1085     volatile int success = 0;
1086     volatile int maxlen = tcd_tcp->rates[layno] ? int_min(tcd_tcp->rates[layno], len) : len;    //Mod antonin losslessbug
1087     volatile double goodthresh;
1088     volatile int i;
1089     double distotarget;         //add fixed_quality
1090
1091     distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10));       // add fixed_quality
1092     
1093     if ((tcd_tcp->rates[layno]) || (tcd_cp->disto_alloc==0)) {
1094       for (i = 0; i < 32; i++) {
1095         volatile double thresh = (lo + hi) / 2;
1096         int l = 0;
1097         double distoachieved = 0;       // add fixed_quality
1098
1099         tcd_makelayer(layno, thresh, 0);
1100
1101         if (tcd_cp->fixed_quality) {    // add fixed_quality
1102           distoachieved =
1103             layno ==
1104             0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1105             tcd_tile->distolayer[layno];
1106           if (distoachieved < distotarget) {
1107             hi = thresh;
1108             continue;
1109           }
1110           lo = thresh;
1111         } else {
1112           l =
1113             t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1114                               layno + 1, dest, maxlen, info_IM);
1115           /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1116           if (l == -999) {
1117             lo = thresh;
1118             continue;
1119           }
1120           hi = thresh;
1121         }
1122
1123         success = 1;
1124         goodthresh = thresh;
1125       }
1126     } else {
1127       success = 1;
1128       goodthresh = min;
1129     }
1130
1131     if (!success) {
1132       longjmp(j2k_error, 1);
1133     }
1134
1135     if (info_IM->index_on) {    /* Threshold for Marcela Index */
1136       info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1137     }
1138     tcd_makelayer(layno, goodthresh, 1);
1139
1140     cumdisto[layno] = layno == 0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno]; // add fixed_quality
1141   }
1142 }
1143
1144 int
1145 tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1146                     info_image * info_IM)
1147 {
1148   int compno;
1149   int l, i, npck=0;
1150   clock_t time7;
1151   tcd_tile_t *tile;
1152   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1153   j2k_tccp_t *tccp = &tcp->tccps[0];
1154   
1155   tcd_tileno = tileno;
1156   tcd_tile = tcd_image.tiles;
1157   tcd_tcp = &tcd_cp->tcps[tileno];
1158   tile = tcd_tile;
1159   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1160   if (info_IM->index_on) {
1161     tcd_tilecomp_t *tilec_idx = &tile->comps[0];        //Based on Component 0
1162     
1163     for (i = 0; i < tilec_idx->numresolutions; i++) {
1164       
1165       tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1166       
1167       info_IM->tile[tileno].pw[i] = res_idx->pw;
1168       info_IM->tile[tileno].ph[i] = res_idx->ph;
1169       
1170       npck+=res_idx->pw * res_idx->ph;
1171       
1172       info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1173       info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1174       
1175     }
1176     info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1177   }
1178   /* << INDEX */
1179
1180 /*---------------TILE-------------------*/
1181
1182   time7 = clock();
1183
1184   for (compno = 0; compno < tile->numcomps; compno++) {
1185     FILE *src;
1186     char tmp[256];
1187     int k;
1188     unsigned char elmt;
1189     int i, j;
1190     int tw, w;
1191     tcd_tilecomp_t *tilec = &tile->comps[compno];
1192     int adjust =
1193       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1194                                               prec - 1);
1195     int offset_x, offset_y;
1196
1197     offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1198     offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1199     tw = tilec->x1 - tilec->x0;
1200     w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1201     sprintf(tmp, "Compo%d", compno);    /* component file */
1202     src = fopen(tmp, "rb");
1203     if (!src) {
1204       fprintf(stderr, "failed to open %s for reading\n", tmp);
1205       return 1;
1206     }
1207
1208     /* read the Compo file to extract data of the tile */
1209     k = 0;
1210     fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1211           SEEK_SET);
1212     k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1213     for (j = tilec->y0; j < tilec->y1; j++) {
1214       for (i = tilec->x0; i < tilec->x1; i++) {
1215         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1216           elmt = fgetc(src);
1217           tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1218             elmt - adjust;
1219           k++;
1220         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1221           elmt = fgetc(src);
1222           tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1223             (elmt - adjust) << 13;
1224           k++;
1225         }
1226       }
1227       fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1228             SEEK_CUR);
1229       k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1230
1231     }
1232     fclose(src);
1233   }
1234
1235 /*----------------MCT-------------------*/
1236
1237   if (tcd_tcp->mct) {
1238     if (tcd_tcp->tccps[0].qmfbid == 0) {
1239       mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1240                       tile->comps[2].data,
1241                       (tile->comps[0].x1 -
1242                        tile->comps[0].x0) * (tile->comps[0].y1 -
1243                                              tile->comps[0].y0));
1244     } else {
1245       mct_encode(tile->comps[0].data, tile->comps[1].data,
1246                  tile->comps[2].data,
1247                  (tile->comps[0].x1 -
1248                   tile->comps[0].x0) * (tile->comps[0].y1 -
1249                                         tile->comps[0].y0));
1250     }
1251   }
1252 /*----------------DWT---------------------*/
1253
1254 // mod Ive
1255 for (compno = 0; compno < tile->numcomps; compno++) {
1256   tcd_tilecomp_t *tilec = &tile->comps[compno];
1257   if (tcd_tcp->tccps[compno].qmfbid == 1) {
1258     dwt_encode(tilec);
1259   } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1260     dwt_encode_real(tilec);
1261   }
1262 }
1263 // /mod Ive
1264 /*------------------TIER1-----------------*/
1265
1266   t1_init_luts();
1267   t1_encode_cblks(tile, tcd_tcp);
1268
1269 /*-----------RATE-ALLOCATE------------------*/
1270   info_IM->index_write = 0;     /* INDEX     */
1271
1272   if (tcd_cp->disto_alloc || tcd_cp->fixed_quality)     // mod fixed_quality
1273     /* Normal Rate/distortion allocation */
1274     tcd_rateallocate(dest, len, info_IM);
1275   else
1276     /* Fixed layer allocation */
1277     tcd_rateallocate_fixed();
1278
1279 /*--------------TIER2------------------*/
1280   info_IM->index_write = 1;     /* INDEX     */
1281   l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1282                         tcd_tcp->numlayers, dest, len, info_IM);
1283 /*---------------CLEAN-------------------*/
1284
1285   time7 = clock() - time7;
1286   fprintf(stdout,"total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1287          (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1288
1289   /* cleaning memory */
1290   for (compno = 0; compno < tile->numcomps; compno++) {
1291     tilec = &tile->comps[compno];
1292     free(tilec->data);
1293   }
1294
1295   return l;
1296 }
1297
1298 int
1299 tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1300                     info_image * info_IM)
1301 {
1302   int compno;
1303   int l, i, npck=0;
1304   clock_t time;
1305   tcd_tile_t *tile;
1306   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1307   j2k_tccp_t *tccp = &tcp->tccps[0];
1308   
1309   tcd_tileno = tileno;
1310   tcd_tile = tcd_image.tiles;
1311   tcd_tcp = &tcd_cp->tcps[tileno];
1312   tile = tcd_tile;
1313   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1314   if (info_IM->index_on) {
1315     tcd_tilecomp_t *tilec_idx = &tile->comps[0];        //Based on Component 0
1316     
1317     for (i = 0; i < tilec_idx->numresolutions; i++) {
1318       
1319       tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1320       
1321       info_IM->tile[tileno].pw[i] = res_idx->pw;
1322       info_IM->tile[tileno].ph[i] = res_idx->ph;
1323       
1324       npck+=res_idx->pw * res_idx->ph;
1325       
1326       info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1327       info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1328       
1329     }
1330     info_IM->tile[tileno].packet = (info_packet *) calloc(info_IM->Comp * info_IM->Layer * npck, sizeof(info_packet));
1331   }
1332   /* << INDEX */
1333 /*---------------TILE-------------------*/
1334   time = clock();
1335
1336   for (compno = 0; compno < tile->numcomps; compno++) {
1337     FILE *src;
1338     char tmp[256];
1339     int k;
1340     int elmt;
1341     int i, j;
1342     int tw, w;
1343     tcd_tilecomp_t *tilec = &tile->comps[compno];
1344     int adjust =
1345       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1346                                               prec - 1);
1347     int offset_x, offset_y;
1348
1349     offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1350     offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1351     tw = tilec->x1 - tilec->x0;
1352     w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1353     sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1);        /* bandtile file opening */
1354     src = fopen(tmp, "rb");
1355     if (!src) {
1356       fprintf(stderr, "failed to open %s for reading\n", tmp);
1357       return 1;
1358     }
1359     /* Extract data from bandtile file limited to the current tile */
1360     k = 0;
1361     while (k < tilec->x0 - offset_x) {
1362       k++;
1363       fscanf(src, "%d", &elmt);
1364     }
1365
1366     for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1367       for (i = tilec->x0; i < tilec->x1; i++) {
1368         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1369           fscanf(src, "%d", &elmt);
1370           tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1371           k++;
1372         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1373           fscanf(src, "%d", &elmt);
1374           tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1375           k++;
1376         }
1377       }
1378       while (k < tilec->x0 - offset_x + (j + 1) * w) {
1379         k++;
1380         fscanf(src, "%d", &elmt);
1381       }
1382     }
1383     fclose(src);
1384   }
1385
1386 /*----------------MCT-------------------*/
1387
1388   if (tcd_tcp->mct) {
1389     if (tcd_tcp->tccps[0].qmfbid == 0) {
1390       mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1391                       tile->comps[2].data,
1392                       (tile->comps[0].x1 -
1393                        tile->comps[0].x0) * (tile->comps[0].y1 -
1394                                              tile->comps[0].y0));
1395     } else {
1396       mct_encode(tile->comps[0].data, tile->comps[1].data,
1397                  tile->comps[2].data,
1398                  (tile->comps[0].x1 -
1399                   tile->comps[0].x0) * (tile->comps[0].y1 -
1400                                         tile->comps[0].y0));
1401     }
1402   }
1403
1404 /*----------------DWT---------------------*/
1405
1406 // mod Ive
1407 for (compno = 0; compno < tile->numcomps; compno++) {
1408   tcd_tilecomp_t *tilec = &tile->comps[compno];
1409   if (tcd_tcp->tccps[compno].qmfbid == 1) {
1410     dwt_encode(tilec);
1411   } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1412     dwt_encode_real(tilec);
1413   }
1414 }
1415 // /mod Ive
1416
1417 /*------------------TIER1-----------------*/
1418
1419   t1_init_luts();
1420   t1_encode_cblks(tile, tcd_tcp);
1421
1422 /*-----------RATE-ALLOCATE------------------*/
1423
1424   info_IM->index_write = 0;     /* INDEX */
1425
1426   if (tcd_cp->disto_alloc || tcd_cp->fixed_quality)     // mod fixed_quality
1427
1428     /* Normal Rate/distortion allocation */
1429
1430     tcd_rateallocate(dest, len, info_IM);
1431
1432   else
1433     /* Fixed layer allocation */
1434
1435     tcd_rateallocate_fixed();
1436
1437 /*--------------TIER2------------------*/
1438   info_IM->index_write = 1;     /* INDEX */
1439
1440   l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1441                         tcd_tcp->numlayers, dest, len, info_IM);
1442
1443  /*---------------CLEAN-------------------*/
1444   time = clock() - time;
1445   fprintf(stdout,"total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1446          (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1447
1448   for (compno = 0; compno < tile->numcomps; compno++) {
1449     tilec = &tile->comps[compno];
1450     free(tilec->data);
1451   }
1452
1453   return l;
1454 }
1455
1456
1457 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1458 {
1459   int l;
1460   int compno;
1461   int eof = 0;
1462   clock_t time;
1463   tcd_tile_t *tile;
1464
1465   tcd_tileno = tileno;
1466   tcd_tile = &tcd_image.tiles[tileno];
1467   tcd_tcp = &tcd_cp->tcps[tileno];
1468   tile = tcd_tile;
1469
1470   time = clock();
1471
1472   fprintf(stdout, "Tile %d of %d decoded in ", tileno + 1,
1473           tcd_cp->tw * tcd_cp->th);
1474
1475         /*--------------TIER2------------------*/
1476
1477   l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1478
1479   if (l == -999) {
1480     eof = 1;
1481     fprintf(stderr, "tcd_decode: incomplete bistream\n");
1482   }
1483
1484         /*------------------TIER1-----------------*/
1485   t1_init_luts();
1486   t1_decode_cblks(tile, tcd_tcp);
1487
1488         /*----------------DWT---------------------*/
1489
1490   for (compno = 0; compno < tile->numcomps; compno++) {
1491     tcd_tilecomp_t *tilec = &tile->comps[compno];
1492     if (tcd_cp->reduce != 0) {
1493       tcd_img->comps[compno].resno_decoded =
1494         tile->comps[compno].numresolutions - tcd_cp->reduce - 1;
1495     }
1496
1497
1498     // mod Ive  
1499     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1500       dwt_decode(tilec, 
1501                  tilec->numresolutions - 1 - 
1502                  tcd_img->comps[compno].resno_decoded);
1503     } else {
1504       dwt_decode_real(tilec,
1505                       tilec->numresolutions - 1 -
1506                       tcd_img->comps[compno].resno_decoded);
1507     }
1508     // /mod Ive
1509     
1510     if (tile->comps[compno].numresolutions > 0)
1511       tcd_img->comps[compno].factor =
1512         tile->comps[compno].numresolutions -
1513         (tcd_img->comps[compno].resno_decoded + 1);
1514   }
1515
1516         /*----------------MCT-------------------*/
1517
1518   if (tcd_tcp->mct) {
1519     if (tcd_tcp->tccps[0].qmfbid == 1) {
1520       mct_decode(tile->comps[0].data, tile->comps[1].data,
1521                  tile->comps[2].data,
1522                  (tile->comps[0].x1 -
1523                   tile->comps[0].x0) * (tile->comps[0].y1 -
1524                                         tile->comps[0].y0));
1525     } else {
1526       mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1527                       tile->comps[2].data,
1528                       (tile->comps[0].x1 -
1529                        tile->comps[0].x0) * (tile->comps[0].y1 -
1530                                              tile->comps[0].y0));
1531     }
1532   }
1533
1534         /*---------------TILE-------------------*/
1535
1536   for (compno = 0; compno < tile->numcomps; compno++) {
1537     tcd_tilecomp_t *tilec = &tile->comps[compno];
1538     tcd_resolution_t *res =
1539       &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1540     int adjust =
1541       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1542                                               prec - 1);
1543     int min =
1544       tcd_img->comps[compno].
1545       sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1546     int max =
1547       tcd_img->comps[compno].
1548       sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1549       1 : (1 << tcd_img->comps[compno].prec) - 1;
1550
1551     int tw = tilec->x1 - tilec->x0;
1552     int w = tcd_img->comps[compno].w;
1553
1554     int i, j;
1555     int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1556                                    tcd_img->comps[compno].factor);
1557     int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1558                                    tcd_img->comps[compno].factor);
1559
1560     for (j = res->y0; j < res->y1; j++) {
1561       for (i = res->x0; i < res->x1; i++) {
1562
1563         int v;
1564         float tmp = (tilec->data[i - res->x0 + (j - res->y0) * tw])/8192.0;
1565         int tmp2;
1566         
1567         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1568           v = tilec->data[i - res->x0 + (j - res->y0) * tw];
1569         } else {
1570           tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1571           v = ((tmp<0)?-tmp2:tmp2);
1572         }
1573         
1574         v += adjust;
1575
1576         tcd_img->comps[compno].data[(i - offset_x) +
1577                                     (j - offset_y) * w] =
1578           int_clamp(v, min, max);
1579       }
1580     }
1581   }
1582
1583   time = clock() - time;
1584   fprintf(stdout, "%ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1585           (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1586
1587
1588
1589   for (compno = 0; compno < tile->numcomps; compno++) {
1590     free(tcd_image.tiles[tileno].comps[compno].data);
1591   }
1592
1593   if (eof) {
1594     longjmp(j2k_error, 1);
1595   }
1596
1597   return l;
1598 }
1599
1600
1601
1602 void tcd_dec_release()
1603
1604 {
1605
1606   int tileno,compno,resno,bandno,precno;
1607
1608   for (tileno=0;tileno<tcd_image.tw*tcd_image.th;tileno++) {
1609
1610     tcd_tile_t tile=tcd_image.tiles[tileno];
1611
1612     for (compno=0;compno<tile.numcomps;compno++) {
1613
1614       tcd_tilecomp_t tilec=tile.comps[compno];
1615
1616       for (resno=0;resno<tilec.numresolutions;resno++) {
1617
1618         tcd_resolution_t res=tilec.resolutions[resno];
1619
1620         for (bandno=0;bandno<res.numbands;bandno++) {
1621
1622           tcd_band_t band=res.bands[bandno];
1623
1624           for (precno=0;precno<res.ph*res.pw;precno++) {
1625
1626             tcd_precinct_t prec=band.precincts[precno];
1627
1628             if (prec.cblks!=NULL) free(prec.cblks);
1629
1630             if (prec.imsbtree!=NULL) free(prec.imsbtree);
1631
1632             if (prec.incltree!=NULL) free(prec.incltree);
1633
1634           }
1635
1636           if (band.precincts!=NULL) free(band.precincts);
1637
1638         }
1639
1640       }
1641
1642       if (tilec.resolutions!=NULL) free(tilec.resolutions);
1643
1644     }
1645
1646     if (tile.comps!=NULL) free(tile.comps);
1647
1648   }
1649
1650   if (tcd_image.tiles!=NULL) free(tcd_image.tiles);
1651
1652 }