2 * Copyrigth (c) 2006, M�nica D�ez, LPI-UVA, Spain
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
27 #include "opj_includes.h"
29 /** @defgroup T1_3D T1_3D - Implementation of the tier-1 coding */
32 /** @name Local static functions */
35 static int t1_3d_getctxno_zc(unsigned int f, int orient);
36 static int t1_3d_getctxno_sc(unsigned int f);
37 static int t1_3d_getctxno_mag(unsigned int f, int fsvr);
38 static int t1_3d_getspb(unsigned int f);
39 static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos);
40 static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos);
41 static void t1_3d_updateflags(unsigned int *fp, int s);
43 Encode significant pass
45 static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
46 int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
48 Decode significant pass
50 static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
51 int *dp, int orient, int oneplushalf, char type, int vsc);
53 Encode significant pass
55 static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
56 int orient, int *nmsedec, char type, int cblksty);
58 Decode significant pass
60 static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
61 int orient, char type, int cblksty);
63 Encode refinement pass
65 static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
66 int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
68 Decode refinement pass
70 static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
71 int *dp, int poshalf, int neghalf, char type, int vsc);
73 Encode refinement pass
75 static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
76 int *nmsedec, char type, int cblksty);
78 Decode refinement pass
80 static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
81 char type, int cblksty);
85 static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
86 int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
90 static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
91 int *dp, int orient, int oneplushalf, int partial, int vsc);
95 static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
96 int orient, int *nmsedec, int cblksty);
100 static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
101 int orient, int cblksty);
105 @param cblk Code-block coding parameters
107 @param compno Component number
111 @param cblksty Code-block style
115 static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk,
116 int orient, int compno, int level[3], int dwtid[3], double stepsize,
117 int cblksty, int numcomps, opj_tcd_tile_t * tile);
121 @param cblk Code-block coding parameters
123 @param roishift Region of interest shifting value
124 @param cblksty Code-block style
126 static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk,
127 int orient, int roishift, int cblksty);
128 static int t1_3d_init_ctxno_zc(unsigned int f, int orient);
129 static int t1_3d_init_ctxno_sc(unsigned int f);
130 static int t1_3d_init_ctxno_mag(unsigned int f, int f2);
131 static int t1_3d_init_spb(unsigned int f);
133 Initialize the look-up tables of the Tier-1 coder/decoder
136 static void t1_3d_init_luts(opj_t1_3d_t *t1);
142 /* ----------------------------------------------------------------------- */
144 static int t1_3d_getctxno_zc(unsigned int f, int orient)
146 return t1_3d_init_ctxno_zc((f & T1_3D_SIG_OTH), orient);
149 static int t1_3d_getctxno_sc(unsigned int f)
151 return t1_3d_init_ctxno_sc((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
152 /*return t1->lut_ctxno_sc[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
155 static int t1_3d_getctxno_mag(unsigned int f, int fsvr)
157 return t1_3d_init_ctxno_mag((f & T1_3D_SIG_OTH), fsvr);
160 static int t1_3d_getspb(unsigned int f)
162 return t1_3d_init_spb((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
163 /*return t1->lut_spb[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
166 static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos)
168 if (bitpos > T1_NMSEDEC_FRACBITS) {
169 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((
170 1 << T1_NMSEDEC_BITS) - 1)];
173 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
176 static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos)
178 if (bitpos > T1_NMSEDEC_FRACBITS) {
179 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((
180 1 << T1_NMSEDEC_BITS) - 1)];
183 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
186 static void t1_3d_updateflags(unsigned int *fp, int s)
188 unsigned int *np = fp - (T1_MAXCBLKW + 2);
189 unsigned int *sp = fp + (T1_MAXCBLKW + 2);
191 unsigned int *bwp = fp + ((T1_MAXCBLKW + 2) * (T1_MAXCBLKH + 2));
192 unsigned int *bnp = bwp - (T1_MAXCBLKW + 2);
193 unsigned int *bsp = bwp + (T1_MAXCBLKW + 2);
195 unsigned int *fwp = fp - ((T1_MAXCBLKW + 2) * (T1_MAXCBLKH + 2));
196 unsigned int *fnp = fwp - (T1_MAXCBLKW + 2);
197 unsigned int *fsp = fwp + (T1_MAXCBLKW + 2);
199 np[-1] |= T1_3D_SIG_SE;
200 np[1] |= T1_3D_SIG_SW;
201 sp[-1] |= T1_3D_SIG_NE;
202 sp[1] |= T1_3D_SIG_NW;
205 fp[-1] |= T1_3D_SIG_E;
206 fp[1] |= T1_3D_SIG_W;
208 *fwp |= T1_3D_SIG_FC;
209 *bwp |= T1_3D_SIG_BC;
211 fnp[-1] |= T1_3D_SIG_FSE;
212 fnp[1] |= T1_3D_SIG_FSW;
213 fsp[-1] |= T1_3D_SIG_FNE;
214 fsp[1] |= T1_3D_SIG_FNW;
215 *fnp |= T1_3D_SIG_FS;
216 *fsp |= T1_3D_SIG_FN;
217 fwp[-1] |= T1_3D_SIG_FE;
218 fwp[1] |= T1_3D_SIG_FW;
220 bnp[-1] |= T1_3D_SIG_BSE;
221 bnp[1] |= T1_3D_SIG_BSW;
222 bsp[-1] |= T1_3D_SIG_BNE;
223 bsp[1] |= T1_3D_SIG_BNW;
224 *bnp |= T1_3D_SIG_BS;
225 *bsp |= T1_3D_SIG_BN;
226 bwp[-1] |= T1_3D_SIG_BE;
227 bwp[1] |= T1_3D_SIG_BW;
230 *np |= (T1_3D_SGN_S << 16);
231 *sp |= (T1_3D_SGN_N << 16);
232 fp[-1] |= (T1_3D_SGN_E << 16);
233 fp[1] |= (T1_3D_SGN_W << 16);
234 *fwp |= (T1_3D_SGN_F << 16);
235 *bwp |= (T1_3D_SGN_B << 16);
239 static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
240 int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc)
245 opj_mqc_t *mqc = t1->mqc; /* MQC component */
247 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW |
248 (T1_3D_SGN_S << 16)))) : (*fp);
250 if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
251 v = int_abs(*dp) & one ? 1 : 0;
252 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
253 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); /* ESSAI */
254 mqc_bypass_enc(mqc, v);
256 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
261 *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
262 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
263 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); /* ESSAI */
264 mqc_bypass_enc(mqc, v);
266 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
267 mqc_encode(mqc, v ^ t1_3d_getspb(flag));
269 t1_3d_updateflags(fp, v);
272 *fsvr |= T1_3D_VISIT;
276 static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
277 int *dp, int orient, int oneplushalf, char type, int vsc)
282 opj_raw_t *raw = t1->raw; /* RAW component */
283 opj_mqc_t *mqc = t1->mqc; /* MQC component */
285 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW |
286 (T1_3D_SGN_S << 16)))) : (*fp);
288 if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
289 if (type == T1_TYPE_RAW) {
290 if (raw_decode(raw)) {
291 v = raw_decode(raw); /* ESSAI */
292 *dp = v ? -oneplushalf : oneplushalf;
293 t1_3d_updateflags(fp, v);
297 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
298 if (mqc_decode(mqc)) {
299 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
300 v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
301 *dp = v ? -oneplushalf : oneplushalf;
302 t1_3d_updateflags(fp, v);
306 *fsvr |= T1_3D_VISIT;
308 } /* VSC and BYPASS by Antonin */
310 static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
311 int orient, int *nmsedec, char type, int cblksty)
313 int i, j, k, m, one, vsc;
315 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
316 for (m = 0; m < l; m++) {
317 for (k = 0; k < h; k += 4) {
318 for (i = 0; i < w; i++) {
319 for (j = k; j < k + 4 && j < h; j++) {
320 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
321 t1_3d_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i],
322 &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one,
330 static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
331 int orient, char type, int cblksty)
333 int i, j, k, m, one, half, oneplushalf, vsc;
336 oneplushalf = one | half;
337 for (m = 0; m < l; m++) {
338 for (k = 0; k < h; k += 4) {
339 for (i = 0; i < w; i++) {
340 for (j = k; j < k + 4 && j < h; j++) {
341 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
342 t1_3d_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i],
343 &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf,
349 } /* VSC and BYPASS by Antonin */
351 static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
352 int *dp, int bpno, int one, int *nmsedec, char type, int vsc)
357 opj_mqc_t *mqc = t1->mqc; /* MQC component */
359 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW |
360 (T1_3D_SGN_S << 16)))) : (*fp);
362 if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
363 *nmsedec += t1_3d_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
364 v = int_abs(*dp) & one ? 1 : 0;
365 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
366 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); /* ESSAI */
367 mqc_bypass_enc(mqc, v);
369 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
372 *fsvr |= T1_3D_REFINE;
376 static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
377 int *dp, int poshalf, int neghalf, char type, int vsc)
382 opj_mqc_t *mqc = t1->mqc; /* MQC component */
383 opj_raw_t *raw = t1->raw; /* RAW component */
385 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW |
386 (T1_3D_SGN_S << 16)))) : (*fp);
388 if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
389 if (type == T1_TYPE_RAW) {
390 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); /* ESSAI */
393 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
396 t = v ? poshalf : neghalf;
397 *dp += *dp < 0 ? -t : t;
398 *fsvr |= T1_3D_REFINE;
400 } /* VSC and BYPASS by Antonin */
402 static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
403 int *nmsedec, char type, int cblksty)
405 int i, j, k, m, one, vsc;
407 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
408 for (m = 0; m < l; m++) {
409 for (k = 0; k < h; k += 4) {
410 for (i = 0; i < w; i++) {
411 for (j = k; j < k + 4 && j < h; j++) {
412 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
413 t1_3d_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i],
414 &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type,
422 static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
423 char type, int cblksty)
425 int i, j, k, m, one, poshalf, neghalf;
429 neghalf = bpno > 0 ? -poshalf : -1;
430 for (m = 0; m < l; m++) {
431 for (k = 0; k < h; k += 4) {
432 for (i = 0; i < w; i++) {
433 for (j = k; j < k + 4 && j < h; j++) {
434 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
435 t1_3d_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i],
436 &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type,
442 } /* VSC and BYPASS by Antonin */
444 static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
445 int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc)
450 opj_mqc_t *mqc = t1->mqc; /* MQC component */
452 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW |
453 (T1_3D_SGN_S << 16)))) : (*fp);
458 if (!(*fsvr & (T1_3D_SIG | T1_3D_VISIT))) {
459 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
460 v = int_abs(*dp) & one ? 1 : 0;
464 *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
465 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
467 mqc_encode(mqc, v ^ t1_3d_getspb(flag));
468 t1_3d_updateflags(fp, v);
472 *fsvr &= ~T1_3D_VISIT;
475 static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr,
476 int *dp, int orient, int oneplushalf, int partial, int vsc)
481 opj_mqc_t *mqc = t1->mqc; /* MQC component */
483 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW |
484 (T1_3D_SGN_S << 16)))) : (*fp);
489 if (!(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
490 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
491 if (mqc_decode(mqc)) {
493 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
494 v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
495 *dp = v ? -oneplushalf : oneplushalf;
496 t1_3d_updateflags(fp, v);
500 *fsvr &= ~T1_3D_VISIT;
501 } /* VSC and BYPASS by Antonin */
503 static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
504 int orient, int *nmsedec, int cblksty)
506 int i, j, k, m, one, agg, runlen, vsc;
508 opj_mqc_t *mqc = t1->mqc; /* MQC component */
511 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
512 for (m = 0; m < l; m++) {
513 for (k = 0; k < h; k += 4) {
514 for (i = 0; i < w; i++) {
516 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
517 agg = !(((t1->flagSVR[1 + m][1 + k][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) &
518 (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
519 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) &
520 (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
521 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) &
522 (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
523 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & ((
524 t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE |
525 T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
529 ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
530 (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
531 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
532 (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
533 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
534 (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
535 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
536 (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
543 for (runlen = 0; runlen < 4; runlen++) {
544 if (int_abs(t1->data[m][k + runlen][i]) & one) {
548 mqc_setcurctx(mqc, T1_CTXNO_AGG);
549 mqc_encode(mqc, runlen != 4);
553 mqc_setcurctx(mqc, T1_CTXNO_UNI);
554 mqc_encode(mqc, runlen >> 1);
555 mqc_encode(mqc, runlen & 1);
559 for (j = k + runlen; j < k + 4 && j < h; j++) {
560 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
561 t1_3d_enc_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i],
562 &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one,
563 nmsedec, agg && (j == k + runlen), vsc);
570 static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno,
571 int orient, int cblksty)
573 int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
574 int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
576 opj_mqc_t *mqc = t1->mqc; /* MQC component */
580 oneplushalf = one | half;
581 for (m = 0; m < l; m++) {
582 for (k = 0; k < h; k += 4) {
583 for (i = 0; i < w; i++) {
585 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
587 ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
588 (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
589 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
590 (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
591 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
592 (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
593 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | ((
594 t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE |
595 T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
599 ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
600 (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
601 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
602 (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
603 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
604 (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
605 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) |
606 (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
613 mqc_setcurctx(mqc, T1_CTXNO_AGG);
614 if (!mqc_decode(mqc)) {
617 mqc_setcurctx(mqc, T1_CTXNO_UNI);
618 runlen = mqc_decode(mqc);
619 runlen = (runlen << 1) | mqc_decode(mqc);
623 for (j = k + runlen; j < k + 4 && j < h; j++) {
624 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
625 t1_3d_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i],
626 &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf,
627 agg && (j == k + runlen), vsc);
634 mqc_setcurctx(mqc, T1_CTXNO_UNI);
636 v = (v << 1) | mqc_decode(mqc);
637 v = (v << 1) | mqc_decode(mqc);
638 v = (v << 1) | mqc_decode(mqc);
641 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
645 } /* VSC and BYPASS by Antonin */
648 static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk,
649 int orient, int compno, int level[3], int dwtid[3], double stepsize,
650 int cblksty, int numcomps, opj_tcd_tile_t * tile)
658 double cumwmsedec = 0;
659 char type = T1_TYPE_MQ;
661 opj_mqc_t *mqc = t1->mqc; /* MQC component */
663 w = cblk->x1 - cblk->x0;
664 h = cblk->y1 - cblk->y0;
665 l = cblk->z1 - cblk->z0;
668 for (k = 0; k < l; k++) {
669 for (j = 0; j < h; j++) {
670 for (i = 0; i < w; i++) {
671 max = int_max(max, int_abs(t1->data[k][j][i]));
675 for (k = 0; k <= l; k++) {
676 for (j = 0; j <= h; j++) {
677 for (i = 0; i <= w; i++) {
678 t1->flags[k][j][i] = 0;
679 t1->flagSVR[k][j][i] = 0;
684 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
686 bpno = cblk->numbps - 1;
690 mqc_init_enc(mqc, cblk->data);
692 for (passno = 0; bpno >= 0; passno++) {
693 opj_tcd_pass_t *pass = &cblk->passes[passno];
696 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) &&
697 (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
701 t1_3d_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
704 t1_3d_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
707 t1_3d_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
708 /* code switch SEGMARK (i.e. SEGSYM) */
709 if (cblksty & J3D_CCP_CBLKSTY_SEGSYM) {
710 mqc_segmark_enc(mqc);
716 tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize,
718 cumwmsedec += tmpwmsedec;
719 tile->distotile += tmpwmsedec;
721 /* Code switch "RESTART" (i.e. TERMALL) */
722 if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) &&
724 if (type == T1_TYPE_RAW) {
727 /* correction = mqc_bypass_flush_enc(); */
728 } else { /* correction = mqc_restart_enc(); */
734 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
735 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) &&
736 (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
737 if (type == T1_TYPE_RAW) {
750 if (++passtype == 3) {
755 if (pass->term && bpno > 0) {
756 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) &&
757 (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
758 if (type == T1_TYPE_RAW) {
759 mqc_bypass_init_enc(mqc);
761 mqc_restart_init_enc(mqc);
765 pass->distortiondec = cumwmsedec;
766 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
767 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
769 /* Code-switch "RESET" */
770 if (cblksty & J3D_CCP_CBLKSTY_RESET) {
775 /* Code switch "ERTERM" (i.e. PTERM) */
776 if (cblksty & J3D_CCP_CBLKSTY_PTERM) {
778 } else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY)) {
782 cblk->totalpasses = passno;
785 static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk,
786 int orient, int roishift, int cblksty)
792 char type = T1_TYPE_MQ; /* BYPASS mode */
793 opj_raw_t *raw = t1->raw; /* RAW component */
794 opj_mqc_t *mqc = t1->mqc; /* MQC component */
796 w = cblk->x1 - cblk->x0;
797 h = cblk->y1 - cblk->y0;
798 l = cblk->z1 - cblk->z0;
800 for (k = 0; k < l; k++) {
801 for (j = 0; j < h; j++) {
802 for (i = 0; i < w; i++) {
803 t1->data[k][j][i] = 0;
808 for (k = 0; k <= l; k++) {
809 for (j = 0; j <= h; j++) {
810 for (i = 0; i <= w; i++) {
811 t1->flags[k][j][i] = 0;
812 t1->flagSVR[k][j][i] = 0;
818 bpno = roishift + cblk->numbps - 1;
823 for (segno = 0; segno < cblk->numsegs; segno++) {
824 opj_tcd_seg_t *seg = &cblk->segs[segno];
827 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) &&
828 (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
829 if (type == T1_TYPE_RAW) {
830 raw_init_dec(raw, seg->data, seg->len);
832 mqc_init_dec(mqc, seg->data, seg->len);
835 for (passno = 0; passno < seg->numpasses; passno++) {
838 t1_3d_dec_sigpass(t1, w, h, l, bpno + 1, orient, type, cblksty);
841 t1_3d_dec_refpass(t1, w, h, l, bpno + 1, type, cblksty);
844 t1_3d_dec_clnpass(t1, w, h, l, bpno + 1, orient, cblksty);
848 if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
851 if (++passtype == 3) {
860 static int t1_3d_init_ctxno_zc(unsigned int f, int orient)
862 unsigned int h, v, c;
863 unsigned int d2xy, d2xz, d2yz, d3;
865 unsigned int hvc, hc, d2, d2xy2yz, d2xy2xz;
867 h = ((f & T1_3D_SIG_W) != 0) + ((f & T1_3D_SIG_E) != 0);
868 v = ((f & T1_3D_SIG_N) != 0) + ((f & T1_3D_SIG_S) != 0);
869 c = ((f & T1_3D_SIG_FC) != 0) + ((f & T1_3D_SIG_BC) != 0);
870 d2xy = ((f & T1_3D_SIG_NW) != 0) + ((f & T1_3D_SIG_NE) != 0) + ((
871 f & T1_3D_SIG_SE) != 0) + ((f & T1_3D_SIG_SW) != 0);
872 d2xz = ((f & T1_3D_SIG_FW) != 0) + ((f & T1_3D_SIG_BW) != 0) + ((
873 f & T1_3D_SIG_FE) != 0) + ((f & T1_3D_SIG_BE) != 0);
874 d2yz = ((f & T1_3D_SIG_FN) != 0) + ((f & T1_3D_SIG_FS) != 0) + ((
875 f & T1_3D_SIG_BN) != 0) + ((f & T1_3D_SIG_BS) != 0);
876 d3 = ((f & T1_3D_SIG_FNW) != 0) + ((f & T1_3D_SIG_FNE) != 0) + ((
877 f & T1_3D_SIG_FSE) != 0) + ((f & T1_3D_SIG_FSW) != 0)
878 + ((f & T1_3D_SIG_BNW) != 0) + ((f & T1_3D_SIG_BNE) != 0) + ((
879 f & T1_3D_SIG_BSE) != 0) + ((f & T1_3D_SIG_BSW) != 0);
885 d2 = d2xy + d2xz + d2yz;
889 } else if (d2 == 1) {
894 } else if (hvc == 1) {
897 } else if (d2 == 1) {
902 } else if (hvc == 2) {
908 } else if (hvc == 3) {
919 d2xy2yz = d2xy + d2yz;
923 n = (!d2xy2yz) ? ((!d3) ? 0 : 1) : ((!d3) ? 2 : 3);
924 } else if (d2xy == 1) {
925 n = (!d2xy2yz) ? ((!d3) ? 4 : 5) : 6;
930 n = (v == 1) ? 8 : 9; /* =1 or =2*/
932 } else if (hc == 1) {
933 n = (!v) ? ((!d2xy) ? ((!d2xy2yz) ? ((!d3) ? 10 : 11) : (12)) : (13)) : (14);
934 } else { /*if (hc >= 2)*/
943 d2xy2xz = d2xy + d2xz;
946 if (!hc && !d2xy2xz) {
948 } else if (hc == 1) {
949 n = (!d2xy2xz) ? 2 : 3;
953 } else if (d2xz >= 1 && !hc) {
955 } else if (hc >= 1) {
956 n = (d2xz == 1) ? 6 : 7;
961 } else if (d2xz == 1) {
963 } else if (d2xz == 2) {
965 } else { /* if (d2xz >= 3) {*/
974 return (T1_3D_CTXNO_ZC + n);
977 static int t1_3d_init_ctxno_sc(unsigned int f)
982 hc = int_min(((f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E)
983 + ((f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W), 1)
984 - int_min(((f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E))
985 + ((f & (T1_3D_SIG_W | T1_3D_SGN_W)) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
987 vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N)
988 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1)
989 - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N))
990 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
992 cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC)
993 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1)
994 - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F))
995 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
1005 } else if (vc == -1) {
1006 n = (!cc) ? 1 : ((cc > 0) ? 2 : 4);
1007 } else if (vc == 1) {
1008 n = (!cc) ? 1 : ((cc < 0) ? 2 : 4);
1010 } else if (hc == 1) {
1012 n = (!cc) ? 1 : ((cc < 0) ? 2 : 4);
1013 } else if (vc == 1) {
1014 n = (!cc) ? 4 : ((cc > 0) ? 5 : 3);
1015 } else if (vc == -1) {
1018 } else if (hc == -1) {
1020 n = (!cc) ? 1 : ((cc > 0) ? 2 : 4);
1021 } else if (vc == 1) {
1023 } else if (vc == -1) {
1024 n = (!cc) ? 4 : ((cc < 0) ? 5 : 3);
1028 return (T1_3D_CTXNO_SC + n);
1031 static int t1_3d_init_ctxno_mag(unsigned int f, int f2)
1035 T1_3D_REFINE)) { /*First refinement for this coefficient (no previous refinement)*/
1036 n = (f & (T1_3D_SIG_PRIM)) ? 1 : 0;
1041 return (T1_3D_CTXNO_MAG + n);
1044 static int t1_3d_init_spb(unsigned int f)
1049 hc = int_min(((f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E)
1050 + ((f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W), 1)
1051 - int_min(((f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E))
1052 + ((f & (T1_3D_SIG_W | T1_3D_SGN_W)) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
1054 vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N)
1055 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1)
1056 - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N))
1057 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
1059 cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC)
1060 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1)
1061 - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F))
1062 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
1064 n = ((hc + vc + cc) < 0);
1069 static void t1_3d_init_luts(opj_t1_3d_t *t1)
1073 /*for (j = 0; j < 4; j++) {
1074 for (i = 0; i < 256; ++i) {
1075 t1->lut_ctxno_zc[(j << 8) | i] = t1_3d_init_ctxno_zc(i, j);
1078 for (i = 0; i < 4096; i++) {
1079 t1->lut_ctxno_sc[i] = t1_3d_init_ctxno_sc(i << 4);
1081 for (j = 0; j < 2; j++) {
1082 for (i = 0; i < 2048; ++i) {
1083 t1->lut_ctxno_mag[(j << 11) + i] = t1_3d_init_ctxno_mag((j ? T1_3D_REFINE : 0) | i);
1086 for (i = 0; i < 4096; ++i) {
1087 t1->lut_spb[i] = t1_3d_init_spb(i << 4);
1089 /* FIXME FIXME FIXME */
1090 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
1091 t = i / pow(2, T1_NMSEDEC_FRACBITS);
1094 t1->lut_nmsedec_sig[i] =
1096 (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
1097 T1_NMSEDEC_FRACBITS) * 8192.0));
1098 t1->lut_nmsedec_sig0[i] =
1100 (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
1101 T1_NMSEDEC_FRACBITS) * 8192.0));
1103 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
1108 t1->lut_nmsedec_ref[i] =
1110 (int)(floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
1111 T1_NMSEDEC_FRACBITS) * 8192.0));
1112 t1->lut_nmsedec_ref0[i] =
1114 (int)(floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2,
1115 T1_NMSEDEC_FRACBITS) * 8192.0));
1119 /* ----------------------------------------------------------------------- */
1121 opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo)
1123 opj_t1_3d_t *t1 = (opj_t1_3d_t*)opj_malloc(sizeof(opj_t1_3d_t));
1126 /* create MQC and RAW handles */
1127 t1->mqc = mqc_create();
1128 t1->raw = raw_create();
1129 /* initialize the look-up tables of the Tier-1 coder/decoder */
1130 t1_3d_init_luts(t1);
1135 void t1_3d_destroy(opj_t1_3d_t *t1)
1138 /* destroy MQC and RAW handles */
1139 mqc_destroy(t1->mqc);
1140 raw_destroy(t1->raw);
1145 void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp)
1147 int compno, resno, bandno, precno, cblkno;
1148 int x, y, z, i, j, k, orient;
1150 tile->distotile = 0; /* fixed_quality */
1152 for (compno = 0; compno < tile->numcomps; compno++) {
1153 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1155 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1156 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1158 for (bandno = 0; bandno < res->numbands; bandno++) {
1159 opj_tcd_band_t *band = &res->bands[bandno];
1161 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2];
1163 opj_tcd_precinct_t *prc = &band->precincts[precno];
1165 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
1167 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1169 if (band->bandno == 0) {
1170 x = cblk->x0 - band->x0;
1171 y = cblk->y0 - band->y0;
1172 z = cblk->z0 - band->z0;
1173 } else if (band->bandno == 1) {
1174 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1175 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1176 y = cblk->y0 - band->y0;
1177 z = cblk->z0 - band->z0;
1178 } else if (band->bandno == 2) {
1179 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1180 x = cblk->x0 - band->x0;
1181 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1182 z = cblk->z0 - band->z0;
1183 } else if (band->bandno == 3) {
1184 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1185 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1186 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1187 z = cblk->z0 - band->z0;
1188 } else if (band->bandno == 4) {
1189 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1190 x = cblk->x0 - band->x0;
1191 y = cblk->y0 - band->y0;
1192 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1193 } else if (band->bandno == 5) {
1194 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1195 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1196 y = cblk->y0 - band->y0;
1197 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1198 } else if (band->bandno == 6) {
1199 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1200 x = cblk->x0 - band->x0;
1201 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1202 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1203 } else if (band->bandno == 7) {
1204 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1205 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1206 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1207 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1210 if (tcp->tccps[compno].reversible == 1) {
1211 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1212 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1213 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1215 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1216 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
1220 } else if (tcp->tccps[compno].reversible == 0) {
1221 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1222 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1223 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1224 t1->data[k][j][i] = fix_mul(
1225 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1226 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
1227 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 -
1228 T1_NMSEDEC_FRACBITS);
1233 orient = band->bandno; /* FIXME */
1234 for (i = 0; i < 3; i++) {
1235 level[i] = tilec->numresolution[i] - 1 - resno;
1238 t1_3d_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid,
1239 band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1248 void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp)
1250 int compno, resno, bandno, precno, cblkno;
1252 for (compno = 0; compno < tile->numcomps; compno++) {
1253 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1255 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1256 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1258 for (bandno = 0; bandno < res->numbands; bandno++) {
1259 opj_tcd_band_t *band = &res->bands[bandno];
1261 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2];
1263 opj_tcd_precinct_t *prc = &band->precincts[precno];
1265 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2];
1267 int x, y, z, i, j, k, orient;
1268 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1270 orient = band->bandno; /* FIXME */
1272 /*fprintf(stdout,"[INFO] t1_3d_decode_cblk(t1, cblk, orient(%d), tcp->tccps[compno].roishift (%d), tcp->tccps[compno].cblksty (%d));\n",orient,tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);*/
1273 t1_3d_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift,
1274 tcp->tccps[compno].cblksty);
1276 if (band->bandno == 0) {
1277 x = cblk->x0 - band->x0;
1278 y = cblk->y0 - band->y0;
1279 z = cblk->z0 - band->z0;
1280 } else if (band->bandno == 1) {
1281 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1282 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1283 y = cblk->y0 - band->y0;
1284 z = cblk->z0 - band->z0;
1285 } else if (band->bandno == 2) {
1286 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1287 x = cblk->x0 - band->x0;
1288 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1289 z = cblk->z0 - band->z0;
1290 } else if (band->bandno == 3) {
1291 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1292 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1293 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1294 z = cblk->z0 - band->z0;
1295 } else if (band->bandno == 4) {
1296 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1297 x = cblk->x0 - band->x0;
1298 y = cblk->y0 - band->y0;
1299 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1300 } else if (band->bandno == 5) {
1301 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1302 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1303 y = cblk->y0 - band->y0;
1304 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1305 } else if (band->bandno == 6) {
1306 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1307 x = cblk->x0 - band->x0;
1308 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1309 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1310 } else if (band->bandno == 7) {
1311 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1312 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1313 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1314 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1317 if (tcp->tccps[compno].roishift) {
1318 int thresh, val, mag;
1319 thresh = 1 << tcp->tccps[compno].roishift;
1320 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1321 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1322 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1323 val = t1->data[k][j][i];
1325 if (mag >= thresh) {
1326 mag >>= tcp->tccps[compno].roishift;
1327 t1->data[k][j][i] = val < 0 ? -mag : mag;
1334 if (tcp->tccps[compno].reversible == 1) {
1335 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1336 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1337 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1338 int tmp = t1->data[k][j][i];
1339 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1340 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp / 2;
1344 } else { /* if (tcp->tccps[compno].reversible == 0) */
1345 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1346 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1347 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1348 double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
1349 if (t1->data[k][j][i] >> 1 == 0) {
1350 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1351 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
1353 int tmp2 = ((int)(floor(fabs(tmp)))) + ((int) floor(fabs(tmp * 2)) % 2);
1354 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) *
1355 (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp < 0) ? -tmp2 : tmp2);