]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/libopenjpeg/t1.c
avoid Doxygen Warnings
[gdcm.git] / src / gdcmopenjpeg / libopenjpeg / t1.c
1 /*
2  * Copyright (c) 2001-2003, David Janssens
3  * Copyright (c) 2002-2003, Yannick Verschueren
4  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5  * Copyright (c) 2005, HervĂ© Drolon, FreeImage Team
6  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "opj_includes.h"
32
33 /** @defgroup T1 T1 - Implementation of the tier-1 coding */
34 /*@{*/
35
36 /** @name Local static functions */
37 /*@{*/
38
39 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient);
40 static int t1_getctxno_sc(opj_t1_t *t1, int f);
41 static int t1_getctxno_mag(opj_t1_t *t1, int f);
42 static int t1_getspb(opj_t1_t *t1, int f);
43 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos);
44 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos);
45 static void t1_updateflags(int *fp, int s);
46 /**
47 Encode significant pass
48 */
49 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
50 /**
51 Decode significant pass
52 */
53 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc);
54 /**
55 Encode significant pass
56 */
57 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty);
58 /**
59 Decode significant pass
60 */
61 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty);
62 /**
63 Encode refinement pass
64 */
65 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
66 /**
67 Decode refinement pass
68 */
69 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc);
70 /**
71 Encode refinement pass
72 */
73 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty);
74 /**
75 Decode refinement pass
76 */
77 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty);
78 /**
79 Encode clean-up pass
80 */
81 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
82 /**
83 Decode clean-up pass
84 */
85 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc);
86 /**
87 Encode clean-up pass
88 */
89 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty);
90 /**
91 Decode clean-up pass
92 */
93 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty);
94 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps);
95 /**
96 Encode 1 code-block
97 @param t1 T1 handle
98 @param cblk Code-block coding parameters
99 @param orient
100 @param compno Component number
101 @param level
102 @param qmfbid
103 @param stepsize
104 @param cblksty Code-block style
105 @param numcomps
106 @param tile
107 */
108 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
109 /**
110 Decode 1 code-block
111 @param t1 T1 handle
112 @param cblk Code-block coding parameters
113 @param orient
114 @param roishift Region of interest shifting value
115 @param cblksty Code-block style
116 */
117 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
118 static int t1_init_ctxno_zc(int f, int orient);
119 static int t1_init_ctxno_sc(int f);
120 static int t1_init_ctxno_mag(int f);
121 static int t1_init_spb(int f);
122 /**
123 Initialize the look-up tables of the Tier-1 coder/decoder
124 @param t1 T1 handle
125 */
126 static void t1_init_luts(opj_t1_t *t1);
127
128 /*@}*/
129
130 /*@}*/
131
132 /* ----------------------------------------------------------------------- */
133
134 static int t1_getctxno_zc(opj_t1_t *t1, int f, int orient) {
135   return t1->lut_ctxno_zc[(orient << 8) | (f & T1_SIG_OTH)];
136 }
137
138 static int t1_getctxno_sc(opj_t1_t *t1, int f) {
139   return t1->lut_ctxno_sc[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
140 }
141
142 static int t1_getctxno_mag(opj_t1_t *t1, int f) {
143   return t1->lut_ctxno_mag[(f & T1_SIG_OTH) | (((f & T1_REFINE) != 0) << 11)];
144 }
145
146 static int t1_getspb(opj_t1_t *t1, int f) {
147   return t1->lut_spb[(f & (T1_SIG_PRIM | T1_SGN)) >> 4];
148 }
149
150 static int t1_getnmsedec_sig(opj_t1_t *t1, int x, int bitpos) {
151   if (bitpos > T1_NMSEDEC_FRACBITS) {
152     return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
153   }
154   
155   return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
156 }
157
158 static int t1_getnmsedec_ref(opj_t1_t *t1, int x, int bitpos) {
159   if (bitpos > T1_NMSEDEC_FRACBITS) {
160     return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
161   }
162
163     return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
164 }
165
166 static void t1_updateflags(int *fp, int s) {
167   int *np = fp - (T1_MAXCBLKW + 2);
168   int *sp = fp + (T1_MAXCBLKW + 2);
169   np[-1] |= T1_SIG_SE;
170   np[1] |= T1_SIG_SW;
171   sp[-1] |= T1_SIG_NE;
172   sp[1] |= T1_SIG_NW;
173   *np |= T1_SIG_S;
174   *sp |= T1_SIG_N;
175   fp[-1] |= T1_SIG_E;
176   fp[1] |= T1_SIG_W;
177   if (s) {
178     *np |= T1_SGN_S;
179     *sp |= T1_SGN_N;
180     fp[-1] |= T1_SGN_E;
181     fp[1] |= T1_SGN_W;
182   }
183 }
184
185 static void t1_enc_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
186   int v, flag;
187   
188   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
189   
190   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
191   if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
192     v = int_abs(*dp) & one ? 1 : 0;
193     if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
194       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));  /* ESSAI */
195       mqc_bypass_enc(mqc, v);
196     } else {
197       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
198       mqc_encode(mqc, v);
199     }
200     if (v) {
201       v = *dp < 0 ? 1 : 0;
202       *nmsedec +=  t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
203       if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
204         mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));  /* ESSAI */
205         mqc_bypass_enc(mqc, v);
206       } else {
207         mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
208         mqc_encode(mqc, v ^ t1_getspb(t1, flag));
209       }
210       t1_updateflags(fp, v);
211       *fp |= T1_SIG;
212     }
213     *fp |= T1_VISIT;
214   }
215 }
216
217 static void t1_dec_sigpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, char type, int vsc) {
218   int v, flag;
219   
220   opj_raw_t *raw = t1->raw;  /* RAW component */
221   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
222   
223   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
224   if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
225     if (type == T1_TYPE_RAW) {
226       if (raw_decode(raw)) {
227         v = raw_decode(raw);  /* ESSAI */
228         *dp = v ? -oneplushalf : oneplushalf;
229         t1_updateflags(fp, v);
230         *fp |= T1_SIG;
231       }
232     } else {
233       mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
234       if (mqc_decode(mqc)) {
235         mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
236         v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
237         *dp = v ? -oneplushalf : oneplushalf;
238         t1_updateflags(fp, v);
239         *fp |= T1_SIG;
240       }
241     }
242     *fp |= T1_VISIT;
243   }
244 }        /* VSC and  BYPASS by Antonin */
245
246 static void t1_enc_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, char type, int cblksty) {
247   int i, j, k, one, vsc;
248   *nmsedec = 0;
249   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
250   for (k = 0; k < h; k += 4) {
251     for (i = 0; i < w; i++) {
252       for (j = k; j < k + 4 && j < h; j++) {
253         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
254         t1_enc_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, type, vsc);
255       }
256     }
257   }
258 }
259
260 static void t1_dec_sigpass(opj_t1_t *t1, int w, int h, int bpno, int orient, char type, int cblksty) {
261   int i, j, k, one, half, oneplushalf, vsc;
262   one = 1 << bpno;
263   half = one >> 1;
264   oneplushalf = one | half;
265   for (k = 0; k < h; k += 4) {
266     for (i = 0; i < w; i++) {
267       for (j = k; j < k + 4 && j < h; j++) {
268         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
269         t1_dec_sigpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, type, vsc);
270       }
271     }
272   }
273 }        /* VSC and  BYPASS by Antonin */
274
275 static void t1_enc_refpass_step(opj_t1_t *t1, int *fp, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
276   int v, flag;
277   
278   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
279   
280   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
281   if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
282     *nmsedec += t1_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
283     v = int_abs(*dp) & one ? 1 : 0;
284     if (type == T1_TYPE_RAW) {  /* BYPASS/LAZY MODE */
285       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));  /* ESSAI */
286       mqc_bypass_enc(mqc, v);
287     } else {
288       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
289       mqc_encode(mqc, v);
290     }
291     *fp |= T1_REFINE;
292   }
293 }
294
295 static void t1_dec_refpass_step(opj_t1_t *t1, int *fp, int *dp, int poshalf, int neghalf, char type, int vsc) {
296   int v, t, flag;
297   
298   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
299   opj_raw_t *raw = t1->raw;  /* RAW component */
300   
301   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
302   if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
303     if (type == T1_TYPE_RAW) {
304       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));  /* ESSAI */
305       v = raw_decode(raw);
306     } else {
307       mqc_setcurctx(mqc, t1_getctxno_mag(t1, flag));
308       v = mqc_decode(mqc);
309     }
310     t = v ? poshalf : neghalf;
311     *dp += *dp < 0 ? -t : t;
312     *fp |= T1_REFINE;
313   }
314 }        /* VSC and  BYPASS by Antonin  */
315
316 static void t1_enc_refpass(opj_t1_t *t1, int w, int h, int bpno, int *nmsedec, char type, int cblksty) {
317   int i, j, k, one, vsc;
318   *nmsedec = 0;
319   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
320   for (k = 0; k < h; k += 4) {
321     for (i = 0; i < w; i++) {
322       for (j = k; j < k + 4 && j < h; j++) {
323         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
324         t1_enc_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], bpno, one, nmsedec, type, vsc);
325       }
326     }
327   }
328 }
329
330 static void t1_dec_refpass(opj_t1_t *t1, int w, int h, int bpno, char type, int cblksty) {
331   int i, j, k, one, poshalf, neghalf;
332   int vsc;
333   one = 1 << bpno;
334   poshalf = one >> 1;
335   neghalf = bpno > 0 ? -poshalf : -1;
336   for (k = 0; k < h; k += 4) {
337     for (i = 0; i < w; i++) {
338       for (j = k; j < k + 4 && j < h; j++) {
339         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
340         t1_dec_refpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], poshalf, neghalf, type, vsc);
341       }
342     }
343   }
344 }        /* VSC and  BYPASS by Antonin */
345
346 static void t1_enc_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
347   int v, flag;
348   
349   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
350   
351   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
352   if (partial) {
353     goto LABEL_PARTIAL;
354   }
355   if (!(*fp & (T1_SIG | T1_VISIT))) {
356     mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
357     v = int_abs(*dp) & one ? 1 : 0;
358     mqc_encode(mqc, v);
359     if (v) {
360 LABEL_PARTIAL:
361       *nmsedec += t1_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
362       mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
363       v = *dp < 0 ? 1 : 0;
364       mqc_encode(mqc, v ^ t1_getspb(t1, flag));
365       t1_updateflags(fp, v);
366       *fp |= T1_SIG;
367     }
368   }
369   *fp &= ~T1_VISIT;
370 }
371
372 static void t1_dec_clnpass_step(opj_t1_t *t1, int *fp, int *dp, int orient, int oneplushalf, int partial, int vsc) {
373   int v, flag;
374   
375   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
376   
377   flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S))) : (*fp);
378   if (partial) {
379     goto LABEL_PARTIAL;
380   }
381   if (!(flag & (T1_SIG | T1_VISIT))) {
382     mqc_setcurctx(mqc, t1_getctxno_zc(t1, flag, orient));
383     if (mqc_decode(mqc)) {
384 LABEL_PARTIAL:
385       mqc_setcurctx(mqc, t1_getctxno_sc(t1, flag));
386       v = mqc_decode(mqc) ^ t1_getspb(t1, flag);
387       *dp = v ? -oneplushalf : oneplushalf;
388       t1_updateflags(fp, v);
389       *fp |= T1_SIG;
390     }
391   }
392   *fp &= ~T1_VISIT;
393 }        /* VSC and  BYPASS by Antonin */
394
395 static void t1_enc_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int *nmsedec, int cblksty) {
396   int i, j, k, one, agg, runlen, vsc;
397   
398   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
399   
400   *nmsedec = 0;
401   one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
402   for (k = 0; k < h; k += 4) {
403     for (i = 0; i < w; i++) {
404       if (k + 3 < h) {
405         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
406           agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
407             || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
408             || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
409             || (t1->flags[1 + k + 3][1 + i] 
410             & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |  T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
411         } else {
412           agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
413             || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
414             || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
415             || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
416         }
417       } else {
418         agg = 0;
419       }
420       if (agg) {
421         for (runlen = 0; runlen < 4; runlen++) {
422           if (int_abs(t1->data[k + runlen][i]) & one)
423             break;
424         }
425         mqc_setcurctx(mqc, T1_CTXNO_AGG);
426         mqc_encode(mqc, runlen != 4);
427         if (runlen == 4) {
428           continue;
429         }
430         mqc_setcurctx(mqc, T1_CTXNO_UNI);
431         mqc_encode(mqc, runlen >> 1);
432         mqc_encode(mqc, runlen & 1);
433       } else {
434         runlen = 0;
435       }
436       for (j = k + runlen; j < k + 4 && j < h; j++) {
437         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
438         t1_enc_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
439       }
440     }
441   }
442 }
443
444 static void t1_dec_clnpass(opj_t1_t *t1, int w, int h, int bpno, int orient, int cblksty) {
445   int i, j, k, one, half, oneplushalf, agg, runlen, vsc;
446   int segsym = cblksty & J2K_CCP_CBLKSTY_SEGSYM;
447   
448   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
449   
450   one = 1 << bpno;
451   half = one >> 1;
452   oneplushalf = one | half;
453   for (k = 0; k < h; k += 4) {
454     for (i = 0; i < w; i++) {
455       if (k + 3 < h) {
456         if (cblksty & J2K_CCP_CBLKSTY_VSC) {
457           agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
458             || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
459             || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
460             || (t1->flags[1 + k + 3][1 + i] 
461             & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |  T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
462         } else {
463           agg = !(t1->flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
464             || t1->flags[1 + k + 1][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
465             || t1->flags[1 + k + 2][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
466             || t1->flags[1 + k + 3][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH));
467         }
468       } else {
469         agg = 0;
470       }
471       if (agg) {
472         mqc_setcurctx(mqc, T1_CTXNO_AGG);
473         if (!mqc_decode(mqc)) {
474           continue;
475         }
476         mqc_setcurctx(mqc, T1_CTXNO_UNI);
477         runlen = mqc_decode(mqc);
478         runlen = (runlen << 1) | mqc_decode(mqc);
479       } else {
480         runlen = 0;
481       }
482       for (j = k + runlen; j < k + 4 && j < h; j++) {
483         vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
484         t1_dec_clnpass_step(t1, &t1->flags[1 + j][1 + i], &t1->data[j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
485       }
486     }
487   }
488   if (segsym) {
489     int v = 0;
490     mqc_setcurctx(mqc, T1_CTXNO_UNI);
491     v = mqc_decode(mqc);
492     v = (v << 1) | mqc_decode(mqc);
493     v = (v << 1) | mqc_decode(mqc);
494     v = (v << 1) | mqc_decode(mqc);
495     /*
496     if (v!=0xa) {
497       opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
498     } 
499     */
500   }
501 }        /* VSC and  BYPASS by Antonin */
502
503
504 /** mod fixed_quality */
505 static double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, double stepsize, int numcomps)  {
506   double w1, w2, wmsedec;
507   if (qmfbid == 1) {
508     w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
509     w2 = dwt_getnorm(level, orient);
510   } else {      /* if (qmfbid == 0) */
511     w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
512     w2 = dwt_getnorm_real(level, orient);
513   }
514   wmsedec = w1 * w2 * stepsize * (1 << bpno);
515   wmsedec *= wmsedec * nmsedec / 8192.0;
516   
517   return wmsedec;
518 }
519
520 /** mod fixed_quality */
521 static void t1_encode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
522   int i, j;
523   int w, h;
524   int passno;
525   int bpno, passtype;
526   int max;
527   int nmsedec = 0;
528   double cumwmsedec = 0;
529   char type = T1_TYPE_MQ;
530   
531   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
532   
533   w = cblk->x1 - cblk->x0;
534   h = cblk->y1 - cblk->y0;
535   
536   max = 0;
537   for (j = 0; j < h; j++) {
538     for (i = 0; i < w; i++) {
539       max = int_max(max, int_abs(t1->data[j][i]));
540     }
541   }
542   
543   cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
544   
545   /* Changed by Dmitry Kolyadin */
546   for (i = 0; i <= w; i++) {
547     for (j = 0; j <= h; j++) {
548       t1->flags[j][i] = 0;
549     }
550   }
551   
552   bpno = cblk->numbps - 1;
553   passtype = 2;
554   
555   mqc_resetstates(mqc);
556   mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
557   mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
558   mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
559   mqc_init_enc(mqc, cblk->data);
560   
561   for (passno = 0; bpno >= 0; passno++) {
562     opj_tcd_pass_t *pass = &cblk->passes[passno];
563     int correction = 3;
564     type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
565     
566     switch (passtype) {
567       case 0:
568         t1_enc_sigpass(t1, w, h, bpno, orient, &nmsedec, type, cblksty);
569         break;
570       case 1:
571         t1_enc_refpass(t1, w, h, bpno, &nmsedec, type, cblksty);
572         break;
573       case 2:
574         t1_enc_clnpass(t1, w, h, bpno, orient, &nmsedec, cblksty);
575         /* code switch SEGMARK (i.e. SEGSYM) */
576         if (cblksty & J2K_CCP_CBLKSTY_SEGSYM)
577           mqc_segmark_enc(mqc);
578         break;
579     }
580     
581     /* fixed_quality */
582     cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
583     tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps);
584     
585     /* Code switch "RESTART" (i.e. TERMALL) */
586     if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)  && !((passtype == 2) && (bpno - 1 < 0))) {
587       if (type == T1_TYPE_RAW) {
588         mqc_flush(mqc);
589         correction = 1;
590         /* correction = mqc_bypass_flush_enc(); */
591       } else {      /* correction = mqc_restart_enc(); */
592         mqc_flush(mqc);
593         correction = 1;
594       }
595       pass->term = 1;
596     } else {
597       if (((bpno < (cblk->numbps - 4) && (passtype > 0)) 
598         || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
599         if (type == T1_TYPE_RAW) {
600           mqc_flush(mqc);
601           correction = 1;
602           /* correction = mqc_bypass_flush_enc(); */
603         } else {    /* correction = mqc_restart_enc(); */
604           mqc_flush(mqc);
605           correction = 1;
606         }
607         pass->term = 1;
608       } else {
609         pass->term = 0;
610       }
611     }
612     
613     if (++passtype == 3) {
614       passtype = 0;
615       bpno--;
616     }
617     
618     if (pass->term && bpno > 0) {
619       type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
620       if (type == T1_TYPE_RAW)
621         mqc_bypass_init_enc(mqc);
622       else
623         mqc_restart_init_enc(mqc);
624     }
625     
626     pass->distortiondec = cumwmsedec;
627     pass->rate = mqc_numbytes(mqc) + correction;  /* FIXME */
628     pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
629     
630     /* Code-switch "RESET" */
631     if (cblksty & J2K_CCP_CBLKSTY_RESET)
632       mqc_reset_enc(mqc);
633   }
634   
635   /* Code switch "ERTERM" (i.e. PTERM) */
636   if (cblksty & J2K_CCP_CBLKSTY_PTERM)
637     mqc_erterm_enc(mqc);
638   else /* Default coding */ if (!(cblksty & J2K_CCP_CBLKSTY_LAZY))
639     mqc_flush(mqc);
640   
641   cblk->totalpasses = passno;
642 }
643
644 static void t1_decode_cblk(opj_t1_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
645   int i, j, w, h;
646   int bpno, passtype;
647   int segno, passno;
648   char type = T1_TYPE_MQ; /* BYPASS mode */
649   
650   opj_raw_t *raw = t1->raw;  /* RAW component */
651   opj_mqc_t *mqc = t1->mqc;  /* MQC component */
652   
653   w = cblk->x1 - cblk->x0;
654   h = cblk->y1 - cblk->y0;
655   
656   /* Changed by Dmitry Kolyadin */
657   for (j = 0; j <= h; j++) {
658     for (i = 0; i <= w; i++) {
659       t1->flags[j][i] = 0;
660     }
661   }
662   
663   /* Changed by Dmitry Kolyadin */
664   for (i = 0; i < w; i++) {
665     for (j = 0; j < h; j++){
666       t1->data[j][i] = 0;
667     }
668   }
669   
670   bpno = roishift + cblk->numbps - 1;
671   passtype = 2;
672   
673   mqc_resetstates(mqc);
674   mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);
675   mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
676   mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
677   
678   for (segno = 0; segno < cblk->numsegs; segno++) {
679     opj_tcd_seg_t *seg = &cblk->segs[segno];
680     
681     /* BYPASS mode */
682     type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
683     if (type == T1_TYPE_RAW) {
684       raw_init_dec(raw, seg->data, seg->len);
685     } else {
686       mqc_init_dec(mqc, seg->data, seg->len);
687     }
688     
689     for (passno = 0; passno < seg->numpasses; passno++) {
690       switch (passtype) {
691         case 0:
692           t1_dec_sigpass(t1, w, h, bpno+1, orient, type, cblksty);
693           break;
694         case 1:
695           t1_dec_refpass(t1, w, h, bpno+1, type, cblksty);
696           break;
697         case 2:
698           t1_dec_clnpass(t1, w, h, bpno+1, orient, cblksty);
699           break;
700       }
701       
702       if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
703         mqc_resetstates(mqc);
704         mqc_setstate(mqc, T1_CTXNO_UNI, 0, 46);        
705         mqc_setstate(mqc, T1_CTXNO_AGG, 0, 3);
706         mqc_setstate(mqc, T1_CTXNO_ZC, 0, 4);
707       }
708       if (++passtype == 3) {
709         passtype = 0;
710         bpno--;
711       }
712     }
713   }
714 }
715
716 static int t1_init_ctxno_zc(int f, int orient) {
717   int h, v, d, n, t, hv;
718   n = 0;
719   h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
720   v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
721   d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) != 0) + ((f & T1_SIG_SW) != 0);
722   
723   switch (orient) {
724     case 2:
725       t = h;
726       h = v;
727       v = t;
728     case 0:
729     case 1:
730       if (!h) {
731         if (!v) {
732           if (!d)
733             n = 0;
734           else if (d == 1)
735             n = 1;
736           else
737             n = 2;
738         } else if (v == 1) {
739           n = 3;
740         } else {
741           n = 4;
742         }
743       } else if (h == 1) {
744         if (!v) {
745           if (!d)
746             n = 5;
747           else
748             n = 6;
749         } else {
750           n = 7;
751         }
752       } else
753         n = 8;
754       break;
755     case 3:
756       hv = h + v;
757       if (!d) {
758         if (!hv) {
759           n = 0;
760         } else if (hv == 1) {
761           n = 1;
762         } else {
763           n = 2;
764         }
765       } else if (d == 1) {
766         if (!hv) {
767           n = 3;
768         } else if (hv == 1) {
769           n = 4;
770         } else {
771           n = 5;
772         }
773       } else if (d == 2) {
774         if (!hv) {
775           n = 6;
776         } else {
777           n = 7;
778         }
779       } else {
780         n = 8;
781       }
782       break;
783   }
784   
785   return (T1_CTXNO_ZC + n);
786 }
787
788 static int t1_init_ctxno_sc(int f) {
789   int hc, vc, n;
790   n = 0;
791
792   hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
793     T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
794          1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
795        (T1_SIG_E | T1_SGN_E)) +
796        ((f & (T1_SIG_W | T1_SGN_W)) ==
797        (T1_SIG_W | T1_SGN_W)), 1);
798   
799   vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
800     T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
801          1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
802        (T1_SIG_N | T1_SGN_N)) +
803        ((f & (T1_SIG_S | T1_SGN_S)) ==
804        (T1_SIG_S | T1_SGN_S)), 1);
805   
806   if (hc < 0) {
807     hc = -hc;
808     vc = -vc;
809   }
810   if (!hc) {
811     if (vc == -1)
812       n = 1;
813     else if (!vc)
814       n = 0;
815     else
816       n = 1;
817   } else if (hc == 1) {
818     if (vc == -1)
819       n = 2;
820     else if (!vc)
821       n = 3;
822     else
823       n = 4;
824   }
825   
826   return (T1_CTXNO_SC + n);
827 }
828
829 static int t1_init_ctxno_mag(int f) {
830   int n;
831   if (!(f & T1_REFINE))
832     n = (f & (T1_SIG_OTH)) ? 1 : 0;
833   else
834     n = 2;
835   
836   return (T1_CTXNO_MAG + n);
837 }
838
839 static int t1_init_spb(int f) {
840   int hc, vc, n;
841   
842   hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
843     T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
844          1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
845        (T1_SIG_E | T1_SGN_E)) +
846        ((f & (T1_SIG_W | T1_SGN_W)) ==
847        (T1_SIG_W | T1_SGN_W)), 1);
848   
849   vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
850     T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
851          1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
852        (T1_SIG_N | T1_SGN_N)) +
853        ((f & (T1_SIG_S | T1_SGN_S)) ==
854        (T1_SIG_S | T1_SGN_S)), 1);
855   
856   if (!hc && !vc)
857     n = 0;
858   else
859     n = (!(hc > 0 || (!hc && vc > 0)));
860   
861   return n;
862 }
863
864 static void t1_init_luts(opj_t1_t *t1) {
865   int i, j;
866   double u, v, t;
867   for (j = 0; j < 4; j++) {
868     for (i = 0; i < 256; ++i) {
869       t1->lut_ctxno_zc[(j << 8) | i] = t1_init_ctxno_zc(i, j);
870     }
871   }
872   for (i = 0; i < 256; i++) {
873     t1->lut_ctxno_sc[i] = t1_init_ctxno_sc(i << 4);
874   }
875   for (j = 0; j < 2; j++) {
876     for (i = 0; i < 2048; ++i) {
877       t1->lut_ctxno_mag[(j << 11) + i] = t1_init_ctxno_mag((j ? T1_REFINE : 0) | i);
878     }
879   }
880   for (i = 0; i < 256; ++i) {
881     t1->lut_spb[i] = t1_init_spb(i << 4);
882   }
883   /* FIXME FIXME FIXME */
884   /* fprintf(stdout,"nmsedec luts:\n"); */
885   for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
886     t = i / pow(2, T1_NMSEDEC_FRACBITS);
887     u = t;
888     v = t - 1.5;
889     t1->lut_nmsedec_sig[i] = 
890       int_max(0, 
891       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
892     t1->lut_nmsedec_sig0[i] =
893       int_max(0,
894       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
895     u = t - 1.0;
896     if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
897       v = t - 1.5;
898     } else {
899       v = t - 0.5;
900     }
901     t1->lut_nmsedec_ref[i] =
902       int_max(0,
903       (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
904     t1->lut_nmsedec_ref0[i] =
905       int_max(0,
906       (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
907   }
908 }
909
910 /* ----------------------------------------------------------------------- */
911
912 opj_t1_t* t1_create(opj_common_ptr cinfo) {
913   opj_t1_t *t1 = (opj_t1_t*)opj_malloc(sizeof(opj_t1_t));
914   if(t1) {
915     t1->cinfo = cinfo;
916     /* create MQC and RAW handles */
917     t1->mqc = mqc_create();
918     t1->raw = raw_create();
919     /* initialize the look-up tables of the Tier-1 coder/decoder */
920     t1_init_luts(t1);
921   }
922   return t1;
923 }
924
925 void t1_destroy(opj_t1_t *t1) {
926   if(t1) {
927     /* destroy MQC and RAW handles */
928     mqc_destroy(t1->mqc);
929     raw_destroy(t1->raw);
930     opj_free(t1);
931   }
932 }
933
934 void t1_encode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
935   int compno, resno, bandno, precno, cblkno;
936   int x, y, i, j, orient;
937   
938   tile->distotile = 0;    /* fixed_quality */
939
940   for (compno = 0; compno < tile->numcomps; compno++) {
941     opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
942
943     for (resno = 0; resno < tilec->numresolutions; resno++) {
944       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
945
946       for (bandno = 0; bandno < res->numbands; bandno++) {
947         opj_tcd_band_t *band = &res->bands[bandno];
948
949         for (precno = 0; precno < res->pw * res->ph; precno++) {
950           opj_tcd_precinct_t *prc = &band->precincts[precno];
951
952           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
953             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
954
955             if (band->bandno == 0) {
956               x = cblk->x0 - band->x0;
957               y = cblk->y0 - band->y0;
958             } else if (band->bandno == 1) {
959               opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
960               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
961               y = cblk->y0 - band->y0;
962             } else if (band->bandno == 2) {
963               opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
964               x = cblk->x0 - band->x0;
965               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
966             } else {    /* if (band->bandno == 3) */
967               opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
968               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
969               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
970             }
971             
972             if (tcp->tccps[compno].qmfbid == 1) {
973               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
974                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
975                   t1->data[j][i] = 
976                     tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0)] << T1_NMSEDEC_FRACBITS;
977                 }
978               }
979             } else if (tcp->tccps[compno].qmfbid == 0) {
980               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
981                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
982                   t1->data[j][i] = 
983                     fix_mul(
984                     tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)], 
985                     8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
986                 }
987               }
988             }
989             orient = band->bandno;  /* FIXME */
990             if (orient == 2) {
991               orient = 1;
992             } else if (orient == 1) {
993               orient = 2;
994             }
995
996             t1_encode_cblk(t1, cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
997
998           } /* cblkno */
999         } /* precno */
1000       } /* bandno */
1001     } /* resno  */
1002   } /* compno  */
1003 }
1004
1005 void t1_decode_cblks(opj_t1_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1006   int compno, resno, bandno, precno, cblkno;
1007   
1008   for (compno = 0; compno < tile->numcomps; compno++) {
1009     opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1010
1011     for (resno = 0; resno < tilec->numresolutions; resno++) {
1012       opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1013
1014       for (bandno = 0; bandno < res->numbands; bandno++) {
1015         opj_tcd_band_t *band = &res->bands[bandno];
1016
1017         for (precno = 0; precno < res->pw * res->ph; precno++) {
1018           opj_tcd_precinct_t *prc = &band->precincts[precno];
1019
1020           for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
1021             int x, y, i, j, orient;
1022             opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1023
1024             orient = band->bandno;  /* FIXME */
1025             if (orient == 2) {
1026               orient = 1;
1027             } else if (orient == 1) {
1028               orient = 2;
1029             }
1030             
1031             t1_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1032
1033             if (band->bandno == 0) {
1034               x = cblk->x0 - band->x0;
1035               y = cblk->y0 - band->y0;
1036             } else if (band->bandno == 1) {
1037               opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1038               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1039               y = cblk->y0 - band->y0;
1040             } else if (band->bandno == 2) {
1041               opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1042               x = cblk->x0 - band->x0;
1043               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1044             } else {    /* if (band->bandno == 3) */
1045               opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1046               x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1047               y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1048             }
1049             
1050             if (tcp->tccps[compno].roishift) {
1051               int thresh, val, mag;
1052               thresh = 1 << tcp->tccps[compno].roishift;
1053               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1054                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1055                   val = t1->data[j][i];
1056                   mag = int_abs(val);
1057                   if (mag >= thresh) {
1058                     mag >>= tcp->tccps[compno].roishift;
1059                     t1->data[j][i] = val < 0 ? -mag : mag;
1060                   }
1061                 }
1062               }
1063             }
1064             
1065             if (tcp->tccps[compno].qmfbid == 1) {
1066               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1067                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1068                   int tmp = t1->data[j][i];
1069                   if(tmp >> 1 == 0) {
1070                     tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
1071                   } else {
1072                     tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);
1073                   }
1074                 }
1075               }
1076             } else {    /* if (tcp->tccps[compno].qmfbid == 0) */
1077               for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1078                 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1079                   double tmp = (double)(t1->data[j][i] * band->stepsize * 4096.0);
1080                   if (t1->data[j][i] >> 1 == 0) {
1081                     tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
1082                   } else {
1083                     int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1084                     tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2);
1085                   }
1086                 }
1087               }
1088             }
1089           } /* cblkno */
1090         } /* precno */
1091       } /* bandno */
1092     } /* resno */
1093   } /* compno */
1094 }
1095