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