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