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