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