88d44e6b01154e6df4f26754481efb9b6fdad16e
[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(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 < 1; tileno++) {
68     tcd_tile_t *tile = &tcd_image.tiles[curtileno];
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               cblk->lastbp = 0; // Add Antonin : quantizbug1
779             }
780           }
781         }
782       }
783     }
784   }
785   //tcd_dump(&tcd_image,0);
786
787
788   /* Allocate place to store the data decoded = final image */
789   /* Place limited by the tile really present in the codestream */
790   
791   for (i = 0; i < img->numcomps; i++) {
792     for (j = 0; j < cp->tileno_size; j++) {
793       tileno = cp->tileno[j];
794       x0 = j == 0 ? tcd_image.tiles[tileno].comps[i].x0 : int_min(x0,
795                                                          tcd_image.
796                                                          tiles[tileno].comps[i].x0);
797       y0 = j == 0 ? tcd_image.tiles[tileno].comps[i].y0 : int_min(y0,
798                                                          tcd_image.
799                                                          tiles[tileno].comps[i].y0);
800       x1 = j == 0 ? tcd_image.tiles[tileno].comps[i].x1 : int_max(x1,
801                                                          tcd_image.
802                                                          tiles[tileno].comps[i].x1);
803       y1 = j == 0 ? tcd_image.tiles[tileno].comps[i].y1 : int_max(y1,
804                                                          tcd_image.
805                                                          tiles[tileno].comps[i].y1);
806     }
807     //w = int_ceildiv(x1 - x0, img->comps[i].dx);
808     //h = int_ceildiv(y1 - y0, img->comps[i].dy);
809     w = x1 - x0;
810     h = y1 - y0;
811     img->comps[i].data = (int *) calloc(w * h, sizeof(int));
812     img->comps[i].w = w;
813     img->comps[i].h = h;
814     img->comps[i].x0 = x0;
815     img->comps[i].y0 = y0;
816   }
817 }
818
819 void tcd_makelayer_fixed(int layno, int final)
820 {
821   int compno, resno, bandno, precno, cblkno;
822   int value;                    //, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3];
823   int matrice[10][10][3];
824   int i, j, k;
825
826   /*matrice=(int*)malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
827
828   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
829     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
830     for (i = 0; i < tcd_tcp->numlayers; i++) {
831       for (j = 0; j < tilec->numresolutions; j++) {
832         for (k = 0; k < 3; k++) {
833           matrice[i][j][k] =
834             (int) (tcd_cp->
835                    matrice[i * tilec->numresolutions * 3 +
836                            j * 3 +
837                            k] *
838                    (float) (tcd_img->comps[compno].prec / 16.0));
839     }}}
840
841     for (resno = 0; resno < tilec->numresolutions; resno++) {
842       tcd_resolution_t *res = &tilec->resolutions[resno];
843       for (bandno = 0; bandno < res->numbands; bandno++) {
844         tcd_band_t *band = &res->bands[bandno];
845         for (precno = 0; precno < res->pw * res->ph; precno++) {
846           tcd_precinct_t *prc = &band->precincts[precno];
847           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
848             tcd_cblk_t *cblk = &prc->cblks[cblkno];
849             tcd_layer_t *layer = &cblk->layers[layno];
850             int n;
851             int imsb = tcd_img->comps[compno].prec - cblk->numbps;      /* number of bit-plan equal to zero */
852             /* Correction of the matrix of coefficient to include the IMSB information */
853
854             if (layno == 0) {
855               value = matrice[layno][resno][bandno];
856               if (imsb >= value)
857                 value = 0;
858               else
859                 value -= imsb;
860             } else {
861               value =
862                 matrice[layno][resno][bandno] -
863                 matrice[layno - 1][resno][bandno];
864               if (imsb >= matrice[layno - 1][resno][bandno]) {
865                 value -= (imsb - matrice[layno - 1][resno][bandno]);
866                 if (value < 0)
867                   value = 0;
868               }
869             }
870
871             if (layno == 0)
872               cblk->numpassesinlayers = 0;
873
874             n = cblk->numpassesinlayers;
875             if (cblk->numpassesinlayers == 0) {
876               if (value != 0)
877                 n = 3 * value - 2 + cblk->numpassesinlayers;
878               else
879                 n = cblk->numpassesinlayers;
880             } else
881               n = 3 * value + cblk->numpassesinlayers;
882
883             layer->numpasses = n - cblk->numpassesinlayers;
884
885             if (!layer->numpasses)
886               continue;
887
888             if (cblk->numpassesinlayers == 0) {
889               layer->len = cblk->passes[n - 1].rate;
890               layer->data = cblk->data;
891             } else {
892               layer->len =
893                 cblk->passes[n - 1].rate -
894                 cblk->passes[cblk->numpassesinlayers - 1].rate;
895               layer->data =
896                 cblk->data +
897                 cblk->passes[cblk->numpassesinlayers - 1].rate;
898             }
899             if (final)
900               cblk->numpassesinlayers = n;
901           }
902         }
903       }
904     }
905   }
906 }
907
908 void tcd_rateallocate_fixed()
909 {
910   int layno;
911
912   for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
913     tcd_makelayer_fixed(layno, 1);
914   }
915 }
916
917 void tcd_makelayer(int layno, double thresh, int final)
918 {
919   int compno, resno, bandno, precno, cblkno, passno;
920
921   tcd_tile->distolayer[layno] = 0;      //add fixed_quality
922
923   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
924     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
925     for (resno = 0; resno < tilec->numresolutions; resno++) {
926       tcd_resolution_t *res = &tilec->resolutions[resno];
927       for (bandno = 0; bandno < res->numbands; bandno++) {
928         tcd_band_t *band = &res->bands[bandno];
929         for (precno = 0; precno < res->pw * res->ph; precno++) {
930           tcd_precinct_t *prc = &band->precincts[precno];
931           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
932             tcd_cblk_t *cblk = &prc->cblks[cblkno];
933             tcd_layer_t *layer = &cblk->layers[layno];
934             int n;
935
936             if (layno == 0) {
937               cblk->numpassesinlayers = 0;
938             }
939             n = cblk->numpassesinlayers;
940             for (passno = cblk->numpassesinlayers;
941                  passno < cblk->totalpasses; passno++) {
942               int dr;
943               double dd;
944               tcd_pass_t *pass = &cblk->passes[passno];
945               if (n == 0) {
946                 dr = pass->rate;
947                 dd = pass->distortiondec;
948               } else {
949                 dr = pass->rate - cblk->passes[n - 1].rate;
950                 dd = pass->distortiondec - cblk->passes[n -
951                                                         1].distortiondec;
952               }
953               if (dr == 0) {
954                 if (dd != 0)
955                   n = passno + 1;
956                 continue;
957               }
958               if (dd / dr > thresh)
959                 n = passno + 1;
960             }
961             layer->numpasses = n - cblk->numpassesinlayers;
962
963             if (!layer->numpasses) {
964               layer->disto = 0;
965               continue;
966             }
967
968             if (cblk->numpassesinlayers == 0) {
969               layer->len = cblk->passes[n - 1].rate;
970               layer->data = cblk->data;
971               layer->disto = cblk->passes[n - 1].distortiondec;
972             } else {
973               layer->len = cblk->passes[n - 1].rate -
974                 cblk->passes[cblk->numpassesinlayers - 1].rate;
975               layer->data =
976                 cblk->data +
977                 cblk->passes[cblk->numpassesinlayers - 1].rate;
978               layer->disto =
979                 cblk->passes[n - 1].distortiondec -
980                 cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
981             }
982
983             tcd_tile->distolayer[layno] += layer->disto;        //add fixed_quality
984
985             if (final)
986               cblk->numpassesinlayers = n;
987           }
988         }
989       }
990     }
991   }
992 }
993
994 void tcd_rateallocate(unsigned char *dest, int len, info_image * info_IM)
995 {
996   int compno, resno, bandno, precno, cblkno, passno, layno;
997   double min, max;
998   double cumdisto[100];         //add fixed_quality
999   const double K = 1;           // 1.1; //add fixed_quality
1000   double maxSE = 0;
1001   min = DBL_MAX;
1002   max = 0;
1003
1004   tcd_tile->nbpix = 0;          //add fixed_quality
1005
1006   for (compno = 0; compno < tcd_tile->numcomps; compno++) {
1007     tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
1008     tilec->nbpix = 0;
1009     for (resno = 0; resno < tilec->numresolutions; resno++) {
1010       tcd_resolution_t *res = &tilec->resolutions[resno];
1011       for (bandno = 0; bandno < res->numbands; bandno++) {
1012         tcd_band_t *band = &res->bands[bandno];
1013         for (precno = 0; precno < res->pw * res->ph; precno++) {
1014           tcd_precinct_t *prc = &band->precincts[precno];
1015           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1016             tcd_cblk_t *cblk = &prc->cblks[cblkno];
1017             for (passno = 0; passno < cblk->totalpasses; passno++) {
1018               tcd_pass_t *pass = &cblk->passes[passno];
1019               int dr;
1020               double dd, rdslope;
1021               if (passno == 0) {
1022                 dr = pass->rate;
1023                 dd = pass->distortiondec;
1024               } else {
1025                 dr = pass->rate - cblk->passes[passno - 1].rate;
1026                 dd = pass->distortiondec -
1027                   cblk->passes[passno - 1].distortiondec;
1028               }
1029               if (dr == 0) {
1030                 continue;
1031               }
1032               rdslope = dd / dr;
1033               if (rdslope < min) {
1034                 min = rdslope;
1035               }
1036               if (rdslope > max) {
1037                 max = rdslope;
1038               }
1039             }                   /* passno */
1040
1041             tcd_tile->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0)); //add fixed_quality
1042             tilec->nbpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));    //add fixed_quality
1043
1044           }                     /* cbklno */
1045         }                       /* precno */
1046       }                         /* bandno */
1047     }                           /* resno */
1048     maxSE+=(double)(((1<<tcd_img->comps[compno].prec)-1)*((1<<tcd_img->comps[compno].prec)-1))*(tilec->nbpix);
1049   }                             /* compno */
1050
1051   /* add antonin index */
1052   if (info_IM->index_on) {
1053     info_tile *info_TL = &info_IM->tile[tcd_tileno];
1054     info_TL->nbpix = tcd_tile->nbpix;
1055     info_TL->distotile = tcd_tile->distotile;
1056     info_TL->thresh =
1057       (double *) malloc(tcd_tcp->numlayers * sizeof(double));
1058   }
1059   /* dda */
1060
1061   for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
1062     volatile double lo = min;
1063     volatile double hi = max;
1064     volatile int success = 0;
1065     volatile int maxlen = int_min(tcd_tcp->rates[layno], len);
1066     volatile double goodthresh;
1067     volatile int goodlen;
1068     volatile int i;
1069     double distotarget;         //add fixed_quality
1070
1071     distotarget = tcd_tile->distotile - ((K * maxSE) / pow(10, tcd_tcp->distoratio[layno] / 10));       // add fixed_quality
1072
1073     for (i = 0; i < 32; i++) {
1074       volatile double thresh = (lo + hi) / 2;
1075       int l=0;
1076       double distoachieved = 0; // add fixed_quality
1077
1078       tcd_makelayer(layno, thresh, 0);
1079
1080       if (tcd_cp->fixed_quality) {      // add fixed_quality
1081         distoachieved =
1082           layno ==
1083           0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1084           tcd_tile->distolayer[layno];
1085         if (distoachieved < distotarget) {
1086           hi = thresh;
1087           continue;
1088         }
1089         lo = thresh;
1090       } else {
1091         l =
1092           t2_encode_packets(tcd_img, tcd_cp, tcd_tileno, tcd_tile,
1093                             layno + 1, dest, maxlen, info_IM);
1094         /* fprintf(stderr, "rate alloc: len=%d, max=%d\n", l, maxlen); */
1095         if (l == -999) {
1096           lo = thresh;
1097           continue;
1098         }
1099         hi = thresh;
1100       }
1101
1102       success = 1;
1103       goodthresh = thresh;
1104       goodlen = l;
1105
1106     }
1107
1108     if (!success) {
1109       longjmp(j2k_error, 1);
1110     }
1111
1112     if (info_IM->index_on) {    /* Threshold for Marcela Index */
1113       info_IM->tile[tcd_tileno].thresh[layno] = goodthresh;
1114     }
1115     tcd_makelayer(layno, goodthresh, 1);
1116     cumdisto[layno] =
1117       layno ==
1118       0 ? tcd_tile->distolayer[0] : cumdisto[layno - 1] +
1119       tcd_tile->distolayer[layno]; // add fixed_quality
1120   }
1121 }
1122
1123 int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
1124                         info_image * info_IM)
1125 {
1126   int compno;
1127   int l,i;
1128   clock_t time7;
1129   tcd_tile_t *tile;
1130   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1131   j2k_tccp_t *tccp = &tcp->tccps[0];
1132
1133   tcd_tileno = tileno;
1134   tcd_tile = tcd_image.tiles;
1135   tcd_tcp = &tcd_cp->tcps[tileno];
1136   tile = tcd_tile;
1137   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1138   if (info_IM->index_on) {
1139     tcd_tilecomp_t *tilec_idx = &tile->comps[0];  //Based on Component 0
1140     for (i=0;i<tilec_idx->numresolutions;i++) {
1141       tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1142
1143       info_IM->tile[tileno].pw[i] = res_idx->pw;
1144       info_IM->tile[tileno].ph[i] = res_idx->ph;
1145
1146       info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1147       info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1148     }
1149   }
1150   /* << INDEX */
1151
1152 /*---------------TILE-------------------*/
1153
1154   time7 = clock();
1155
1156   for (compno = 0; compno < tile->numcomps; compno++) {
1157     FILE *src;
1158     char tmp[256];
1159     int k;
1160     unsigned char elmt;
1161     int i, j;
1162     int tw, w;
1163     tcd_tilecomp_t *tilec = &tile->comps[compno];
1164     int adjust =
1165       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1166                                               prec - 1);
1167     int offset_x, offset_y;
1168
1169     offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1170     offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1171     tw = tilec->x1 - tilec->x0;
1172     w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1173     sprintf(tmp, "Compo%d", compno);    /* component file */
1174     src = fopen(tmp, "rb");
1175     if (!src) {
1176       fprintf(stderr, "failed to open %s for reading\n", tmp);
1177       return 1;
1178     }
1179
1180     /* read the Compo file to extract data of the tile */
1181     k = 0;
1182     fseek(src, (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w,
1183           SEEK_SET);
1184     k = (tilec->x0 - offset_x) + (tilec->y0 - offset_y) * w;
1185     for (j = tilec->y0; j < tilec->y1; j++) {
1186       for (i = tilec->x0; i < tilec->x1; i++) {
1187         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1188           elmt = fgetc(src);
1189           tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1190             elmt - adjust;
1191           k++;
1192         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1193           elmt = fgetc(src);
1194           tilec->data[i - tilec->x0 + (j - tilec->y0) * tw] =
1195             (elmt - adjust) << 13;
1196           k++;
1197         }
1198       }
1199       fseek(src, (tilec->x0 - offset_x) + (j + 1 - offset_y) * w - k,
1200             SEEK_CUR);
1201       k = tilec->x0 - offset_x + (j + 1 - offset_y) * w;
1202
1203     }
1204     fclose(src);
1205   }
1206
1207 /*----------------MCT-------------------*/
1208
1209   if (tcd_tcp->mct) {
1210     if (tcd_tcp->tccps[0].qmfbid == 0) {
1211       mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1212                       tile->comps[2].data,
1213                       (tile->comps[0].x1 -
1214                        tile->comps[0].x0) * (tile->comps[0].y1 -
1215                                              tile->comps[0].y0));
1216     } else {
1217       mct_encode(tile->comps[0].data, tile->comps[1].data,
1218                  tile->comps[2].data,
1219                  (tile->comps[0].x1 -
1220                   tile->comps[0].x0) * (tile->comps[0].y1 -
1221                                         tile->comps[0].y0));
1222     }
1223   }
1224 /*----------------DWT---------------------*/
1225
1226   /* time3=clock(); */
1227   for (compno = 0; compno < tile->numcomps; compno++) {
1228     tcd_tilecomp_t *tilec = &tile->comps[compno];
1229     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1230       dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1231                  tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1232     } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1233       dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1234                       tilec->y1 - tilec->y0, tilec,
1235                       tilec->numresolutions - 1);
1236     }
1237   }
1238 /*------------------TIER1-----------------*/
1239
1240   t1_init_luts();
1241   t1_encode_cblks(tile, tcd_tcp);
1242
1243 /*-----------RATE-ALLOCATE------------------*/
1244   info_IM->index_write = 0;     /* INDEX     */
1245
1246   if (tcd_cp->disto_alloc || tcd_cp->fixed_quality)     // mod fixed_quality
1247     /* Normal Rate/distortion allocation */
1248     tcd_rateallocate(dest, len, info_IM);
1249   else
1250     /* Fixed layer allocation */
1251     tcd_rateallocate_fixed();
1252
1253 /*--------------TIER2------------------*/
1254   info_IM->index_write = 1;     /* INDEX     */
1255   l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1256                         tcd_tcp->numlayers, dest, len, info_IM);
1257 /*---------------CLEAN-------------------*/
1258
1259   time7 = clock() - time7;
1260   printf("total:     %ld.%.3ld s\n", time7 / CLOCKS_PER_SEC,
1261          (time7 % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1262
1263   /* cleaning memory */
1264   for (compno = 0; compno < tile->numcomps; compno++) {
1265     tilec = &tile->comps[compno];
1266     free(tilec->data);
1267   }
1268
1269   return l;
1270 }
1271
1272 int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
1273                         info_image * info_IM)
1274 {
1275   int compno;
1276   int l,i;
1277   clock_t time;
1278   tcd_tile_t *tile;
1279   j2k_tcp_t *tcp = &tcd_cp->tcps[0];
1280   j2k_tccp_t *tccp = &tcp->tccps[0];
1281
1282   tcd_tileno = tileno;
1283   tcd_tile = tcd_image.tiles;
1284   tcd_tcp = &tcd_cp->tcps[tileno];
1285   tile = tcd_tile;
1286   /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
1287   if (info_IM->index_on) {
1288     tcd_tilecomp_t *tilec_idx = &tile->comps[0];  //Based on Component 0
1289     for (i=0;i<tilec_idx->numresolutions;i++) {
1290       tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
1291
1292       info_IM->tile[tileno].pw[i] = res_idx->pw;
1293       info_IM->tile[tileno].ph[i] = res_idx->ph;
1294
1295       info_IM->tile[tileno].pdx[i] = tccp->prcw[i];
1296       info_IM->tile[tileno].pdy[i] = tccp->prch[i];
1297     }
1298   }
1299   /* << INDEX */
1300 /*---------------TILE-------------------*/
1301   time = clock();
1302
1303   for (compno = 0; compno < tile->numcomps; compno++) {
1304     FILE *src;
1305     char tmp[256];
1306     int k;
1307     int elmt;
1308     int i, j;
1309     int tw, w;
1310     tcd_tilecomp_t *tilec = &tile->comps[compno];
1311     int adjust =
1312       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1313                                               prec - 1);
1314     int offset_x, offset_y;
1315
1316     offset_x = int_ceildiv(tcd_img->x0, tcd_img->comps[compno].dx);
1317     offset_y = int_ceildiv(tcd_img->y0, tcd_img->comps[compno].dy);
1318     tw = tilec->x1 - tilec->x0;
1319     w = int_ceildiv(tcd_img->x1 - tcd_img->x0, tcd_img->comps[compno].dx);
1320     sprintf(tmp, "bandtile%d", tileno / tcd_cp->tw + 1);        /* bandtile file opening */
1321     src = fopen(tmp, "rb");
1322     if (!src) {
1323       fprintf(stderr, "failed to open %s for reading\n", tmp);
1324       return 1;
1325     }
1326     /* Extract data from bandtile file limited to the current tile */
1327     k = 0;
1328     while (k < tilec->x0 - offset_x) {
1329       k++;
1330       fscanf(src, "%d", &elmt);
1331     }
1332
1333     for (j = 0; j < tilec->y1 - tilec->y0; j++) {
1334       for (i = tilec->x0; i < tilec->x1; i++) {
1335         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1336           fscanf(src, "%d", &elmt);
1337           tilec->data[i - tilec->x0 + (j) * tw] = elmt - adjust;
1338           k++;
1339         } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1340           fscanf(src, "%d", &elmt);
1341           tilec->data[i - tilec->x0 + (j) * tw] = (elmt - adjust) << 13;
1342           k++;
1343         }
1344       }
1345       while (k < tilec->x0 - offset_x + (j + 1) * w) {
1346         k++;
1347         fscanf(src, "%d", &elmt);
1348       }
1349     }
1350     fclose(src);
1351   }
1352
1353 /*----------------MCT-------------------*/
1354
1355   if (tcd_tcp->mct) {
1356     if (tcd_tcp->tccps[0].qmfbid == 0) {
1357       mct_encode_real(tile->comps[0].data, tile->comps[1].data,
1358                       tile->comps[2].data,
1359                       (tile->comps[0].x1 -
1360                        tile->comps[0].x0) * (tile->comps[0].y1 -
1361                                              tile->comps[0].y0));
1362     } else {
1363       mct_encode(tile->comps[0].data, tile->comps[1].data,
1364                  tile->comps[2].data,
1365                  (tile->comps[0].x1 -
1366                   tile->comps[0].x0) * (tile->comps[0].y1 -
1367                                         tile->comps[0].y0));
1368     }
1369   }
1370
1371 /*----------------DWT---------------------*/
1372
1373   for (compno = 0; compno < tile->numcomps; compno++) {
1374     tcd_tilecomp_t *tilec = &tile->comps[compno];
1375     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1376       dwt_encode(tilec->data, tilec->x1 - tilec->x0,
1377                  tilec->y1 - tilec->y0, tilec, tilec->numresolutions - 1);
1378     } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
1379       dwt_encode_real(tilec->data, tilec->x1 - tilec->x0,
1380                       tilec->y1 - tilec->y0, tilec,
1381                       tilec->numresolutions - 1);
1382     }
1383   }
1384
1385 /*------------------TIER1-----------------*/
1386
1387   t1_init_luts();
1388   t1_encode_cblks(tile, tcd_tcp);
1389
1390 /*-----------RATE-ALLOCATE------------------*/
1391
1392   info_IM->index_write = 0;     /* INDEX */
1393
1394   if (tcd_cp->disto_alloc || tcd_cp->fixed_quality)     // mod fixed_quality
1395     /* Normal Rate/distortion allocation */
1396     tcd_rateallocate(dest, len, info_IM);
1397   else
1398     /* Fixed layer allocation */
1399     tcd_rateallocate_fixed();
1400
1401 /*--------------TIER2------------------*/
1402   info_IM->index_write = 1;     /* INDEX */
1403
1404   l = t2_encode_packets(tcd_img, tcd_cp, tileno, tile,
1405                         tcd_tcp->numlayers, dest, len, info_IM);
1406
1407  /*---------------CLEAN-------------------*/
1408   time = clock() - time;
1409   printf("total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1410          (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1411
1412   for (compno = 0; compno < tile->numcomps; compno++) {
1413     tilec = &tile->comps[compno];
1414     free(tilec->data);
1415   }
1416
1417   return l;
1418 }
1419
1420
1421 int tcd_decode_tile(unsigned char *src, int len, int tileno)
1422 {
1423   int l;
1424   int compno;
1425   int eof = 0;
1426   clock_t time;
1427   tcd_tile_t *tile;
1428
1429   tcd_tileno = tileno;
1430   tcd_tile = &tcd_image.tiles[tileno];
1431   tcd_tcp = &tcd_cp->tcps[tileno];
1432   tile = tcd_tile;
1433
1434   time = clock();
1435
1436   fprintf(stderr, "tile decoding time %d/%d: ", tileno + 1,
1437           tcd_cp->tw * tcd_cp->th);
1438
1439         /*--------------TIER2------------------*/
1440
1441   l = t2_decode_packets(src, len, tcd_img, tcd_cp, tileno, tile);
1442
1443   if (l == -999) {
1444     eof = 1;
1445     fprintf(stderr, "tcd_decode: incomplete bistream\n");
1446   }
1447
1448         /*------------------TIER1-----------------*/
1449   t1_init_luts();
1450   t1_decode_cblks(tile, tcd_tcp);
1451
1452         /*----------------DWT---------------------*/
1453
1454   for (compno = 0; compno < tile->numcomps; compno++) {
1455     tcd_tilecomp_t *tilec = &tile->comps[compno];
1456     if (tcd_cp->reduce_on == 1) {
1457       tcd_img->comps[compno].resno_decoded =
1458         tile->comps[compno].numresolutions - tcd_cp->reduce_value - 1;
1459     }
1460
1461
1462     if (tcd_tcp->tccps[compno].qmfbid == 1) {
1463       dwt_decode(tilec->data, tilec->x1 - tilec->x0,
1464                  tilec->y1 - tilec->y0, tilec,
1465                  tilec->numresolutions - 1,
1466                  tilec->numresolutions - 1 -
1467                  tcd_img->comps[compno].resno_decoded);
1468     } else {
1469       dwt_decode_real(tilec->data, tilec->x1 - tilec->x0,
1470                       tilec->y1 - tilec->y0, tilec,
1471                       tilec->numresolutions - 1,
1472                       tilec->numresolutions - 1 -
1473                       tcd_img->comps[compno].resno_decoded);
1474     }
1475
1476     if (tile->comps[compno].numresolutions > 0)
1477       tcd_img->comps[compno].factor =
1478         tile->comps[compno].numresolutions -
1479         (tcd_img->comps[compno].resno_decoded + 1);
1480   }
1481
1482         /*----------------MCT-------------------*/
1483
1484   if (tcd_tcp->mct) {
1485     if (tcd_tcp->tccps[0].qmfbid == 1) {
1486       mct_decode(tile->comps[0].data, tile->comps[1].data,
1487                  tile->comps[2].data,
1488                  (tile->comps[0].x1 -
1489                   tile->comps[0].x0) * (tile->comps[0].y1 -
1490                                         tile->comps[0].y0));
1491     } else {
1492       mct_decode_real(tile->comps[0].data, tile->comps[1].data,
1493                       tile->comps[2].data,
1494                       (tile->comps[0].x1 -
1495                        tile->comps[0].x0) * (tile->comps[0].y1 -
1496                                              tile->comps[0].y0));
1497     }
1498   }
1499
1500         /*---------------TILE-------------------*/
1501
1502   for (compno = 0; compno < tile->numcomps; compno++) {
1503     tcd_tilecomp_t *tilec = &tile->comps[compno];
1504     tcd_resolution_t *res =
1505       &tilec->resolutions[tcd_img->comps[compno].resno_decoded];
1506     int adjust =
1507       tcd_img->comps[compno].sgnd ? 0 : 1 << (tcd_img->comps[compno].
1508                                               prec - 1);
1509     int min =
1510       tcd_img->comps[compno].
1511       sgnd ? -(1 << (tcd_img->comps[compno].prec - 1)) : 0;
1512     int max =
1513       tcd_img->comps[compno].
1514       sgnd ? (1 << (tcd_img->comps[compno].prec - 1)) -
1515       1 : (1 << tcd_img->comps[compno].prec) - 1;
1516
1517     int tw = tilec->x1 - tilec->x0;
1518     int w = tcd_img->comps[compno].w;
1519
1520     int i, j;
1521     int offset_x = int_ceildivpow2(tcd_img->comps[compno].x0,
1522                                    tcd_img->comps[compno].factor);
1523     int offset_y = int_ceildivpow2(tcd_img->comps[compno].y0,
1524                                    tcd_img->comps[compno].factor);
1525
1526     for (j = res->y0; j < res->y1; j++) {
1527       for (i = res->x0; i < res->x1; i++) {
1528
1529         int v;
1530         double tmp= (double) tilec->data[i - res->x0 + (j - res->y0) * tw];
1531         if (tcd_tcp->tccps[compno].qmfbid == 1) {
1532           v = (int) tmp;
1533         } else {
1534           //v = (int) tmp >> 13;
1535           //Mod antonin : multbug1
1536           v = (int) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));
1537           v = (tmp<0)?-v:v;
1538           //doM
1539         }
1540         v += adjust;
1541
1542         tcd_img->comps[compno].data[(i - offset_x) +
1543                                     (j - offset_y) * w] =
1544           int_clamp(v, min, max);
1545       }
1546     }
1547   }
1548
1549   time = clock() - time;
1550   fprintf(stderr, "total:     %ld.%.3ld s\n", time / CLOCKS_PER_SEC,
1551           (time % CLOCKS_PER_SEC) * 1000 / CLOCKS_PER_SEC);
1552
1553   if (eof) {
1554     longjmp(j2k_error, 1);
1555   }
1556
1557   return l;
1558 }