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, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc);
47 Decode significant pass
49 static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc);
51 Encode significant pass
53 static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty);
55 Decode significant pass
57 static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty);
59 Encode refinement pass
61 static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc);
63 Decode refinement pass
65 static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc);
67 Encode refinement pass
69 static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty);
71 Decode refinement pass
73 static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty);
77 static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc);
81 static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc);
85 static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty);
89 static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty);
93 @param cblk Code-block coding parameters
95 @param compno Component number
99 @param cblksty Code-block style
103 static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile);
107 @param cblk Code-block coding parameters
109 @param roishift Region of interest shifting value
110 @param cblksty Code-block style
112 static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty);
113 static int t1_3d_init_ctxno_zc(unsigned int f, int orient);
114 static int t1_3d_init_ctxno_sc(unsigned int f);
115 static int t1_3d_init_ctxno_mag(unsigned int f, int f2);
116 static int t1_3d_init_spb(unsigned int f);
118 Initialize the look-up tables of the Tier-1 coder/decoder
121 static void t1_3d_init_luts(opj_t1_3d_t *t1);
127 /* ----------------------------------------------------------------------- */
129 static int t1_3d_getctxno_zc(unsigned int f, int orient) {
130 return t1_3d_init_ctxno_zc((f & T1_3D_SIG_OTH), orient);
133 static int t1_3d_getctxno_sc(unsigned int f) {
134 return t1_3d_init_ctxno_sc((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
135 /*return t1->lut_ctxno_sc[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
138 static int t1_3d_getctxno_mag(unsigned int f, int fsvr) {
139 return t1_3d_init_ctxno_mag((f & T1_3D_SIG_OTH), fsvr);
142 static int t1_3d_getspb(unsigned int f) {
143 return t1_3d_init_spb((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN));
144 /*return t1->lut_spb[((f & T1_3D_SIG_PRIM) | ((f >> 16) & T1_3D_SGN)) >> 4];*/
147 static int t1_3d_getnmsedec_sig(opj_t1_3d_t *t1, int x, int bitpos) {
148 if (bitpos > T1_NMSEDEC_FRACBITS) {
149 return t1->lut_nmsedec_sig[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
152 return t1->lut_nmsedec_sig0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
155 static int t1_3d_getnmsedec_ref(opj_t1_3d_t *t1, int x, int bitpos) {
156 if (bitpos > T1_NMSEDEC_FRACBITS) {
157 return t1->lut_nmsedec_ref[(x >> (bitpos - T1_NMSEDEC_FRACBITS)) & ((1 << T1_NMSEDEC_BITS) - 1)];
160 return t1->lut_nmsedec_ref0[x & ((1 << T1_NMSEDEC_BITS) - 1)];
163 static void t1_3d_updateflags(unsigned int *fp, int s) {
164 unsigned int *np = fp - (T1_MAXCBLKW + 2);
165 unsigned int *sp = fp + (T1_MAXCBLKW + 2);
167 unsigned int *bwp = fp + ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));
168 unsigned int *bnp = bwp - (T1_MAXCBLKW + 2);
169 unsigned int *bsp = bwp + (T1_MAXCBLKW + 2);
171 unsigned int *fwp = fp - ((T1_MAXCBLKW + 2)*(T1_MAXCBLKH +2));
172 unsigned int *fnp = fwp - (T1_MAXCBLKW + 2);
173 unsigned int *fsp = fwp + (T1_MAXCBLKW + 2);
175 np[-1] |= T1_3D_SIG_SE;
176 np[1] |= T1_3D_SIG_SW;
177 sp[-1] |= T1_3D_SIG_NE;
178 sp[1] |= T1_3D_SIG_NW;
181 fp[-1] |= T1_3D_SIG_E;
182 fp[1] |= T1_3D_SIG_W;
184 *fwp |= T1_3D_SIG_FC;
185 *bwp |= T1_3D_SIG_BC;
187 fnp[-1] |= T1_3D_SIG_FSE;
188 fnp[1] |= T1_3D_SIG_FSW;
189 fsp[-1] |= T1_3D_SIG_FNE;
190 fsp[1] |= T1_3D_SIG_FNW;
191 *fnp |= T1_3D_SIG_FS;
192 *fsp |= T1_3D_SIG_FN;
193 fwp[-1] |= T1_3D_SIG_FE;
194 fwp[1] |= T1_3D_SIG_FW;
196 bnp[-1] |= T1_3D_SIG_BSE;
197 bnp[1] |= T1_3D_SIG_BSW;
198 bsp[-1] |= T1_3D_SIG_BNE;
199 bsp[1] |= T1_3D_SIG_BNW;
200 *bnp |= T1_3D_SIG_BS;
201 *bsp |= T1_3D_SIG_BN;
202 bwp[-1] |= T1_3D_SIG_BE;
203 bwp[1] |= T1_3D_SIG_BW;
206 *np |= (T1_3D_SGN_S << 16);
207 *sp |= (T1_3D_SGN_N << 16);
208 fp[-1] |= (T1_3D_SGN_E << 16);
209 fp[1] |= (T1_3D_SGN_W << 16);
210 *fwp |= (T1_3D_SGN_F << 16);
211 *bwp |= (T1_3D_SGN_B << 16);
215 static void t1_3d_enc_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, char type, int vsc) {
219 opj_mqc_t *mqc = t1->mqc; /* MQC component */
221 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
223 if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
224 v = int_abs(*dp) & one ? 1 : 0;
225 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
226 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient)); /* ESSAI */
227 mqc_bypass_enc(mqc, v);
229 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
234 *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
235 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
236 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag)); /* ESSAI */
237 mqc_bypass_enc(mqc, v);
239 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
240 mqc_encode(mqc, v ^ t1_3d_getspb(flag));
242 t1_3d_updateflags(fp, v);
245 *fsvr |= T1_3D_VISIT;
249 static void t1_3d_dec_sigpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, char type, int vsc) {
253 opj_raw_t *raw = t1->raw; /* RAW component */
254 opj_mqc_t *mqc = t1->mqc; /* MQC component */
256 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
258 if ((flag & T1_3D_SIG_OTH) && !(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
259 if (type == T1_TYPE_RAW) {
260 if (raw_decode(raw)) {
261 v = raw_decode(raw); /* ESSAI */
262 *dp = v ? -oneplushalf : oneplushalf;
263 t1_3d_updateflags(fp, v);
267 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
268 if (mqc_decode(mqc)) {
269 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
270 v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
271 *dp = v ? -oneplushalf : oneplushalf;
272 t1_3d_updateflags(fp, v);
276 *fsvr |= T1_3D_VISIT;
278 } /* VSC and BYPASS by Antonin */
280 static void t1_3d_enc_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, char type, int cblksty) {
281 int i, j, k, m, one, vsc;
283 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
284 for (m = 0; m < l; m++) {
285 for (k = 0; k < h; k += 4) {
286 for (i = 0; i < w; i++) {
287 for (j = k; j < k + 4 && j < h; j++) {
288 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
289 t1_3d_enc_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, type, vsc);
296 static void t1_3d_dec_sigpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, char type, int cblksty) {
297 int i, j, k, m, one, half, oneplushalf, vsc;
300 oneplushalf = one | half;
301 for (m = 0; m < l; m++) {
302 for (k = 0; k < h; k += 4) {
303 for (i = 0; i < w; i++) {
304 for (j = k; j < k + 4 && j < h; j++) {
305 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
306 t1_3d_dec_sigpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, type, vsc);
311 } /* VSC and BYPASS by Antonin */
313 static void t1_3d_enc_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int bpno, int one, int *nmsedec, char type, int vsc) {
317 opj_mqc_t *mqc = t1->mqc; /* MQC component */
319 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
321 if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
322 *nmsedec += t1_3d_getnmsedec_ref(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
323 v = int_abs(*dp) & one ? 1 : 0;
324 if (type == T1_TYPE_RAW) { /* BYPASS/LAZY MODE */
325 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); /* ESSAI */
326 mqc_bypass_enc(mqc, v);
328 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
331 *fsvr |= T1_3D_REFINE;
335 static void t1_3d_dec_refpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int poshalf, int neghalf, char type, int vsc) {
339 opj_mqc_t *mqc = t1->mqc; /* MQC component */
340 opj_raw_t *raw = t1->raw; /* RAW component */
342 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
344 if ((flagsvr & (T1_3D_SIG | T1_3D_VISIT)) == T1_3D_SIG) {
345 if (type == T1_TYPE_RAW) {
346 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr)); /* ESSAI */
349 mqc_setcurctx(mqc, t1_3d_getctxno_mag(flag, flagsvr));
352 t = v ? poshalf : neghalf;
353 *dp += *dp < 0 ? -t : t;
354 *fsvr |= T1_3D_REFINE;
356 } /* VSC and BYPASS by Antonin */
358 static void t1_3d_enc_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int *nmsedec, char type, int cblksty) {
359 int i, j, k, m, one, vsc;
361 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
362 for (m = 0; m < l; m++){
363 for (k = 0; k < h; k += 4) {
364 for (i = 0; i < w; i++) {
365 for (j = k; j < k + 4 && j < h; j++) {
366 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
367 t1_3d_enc_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], bpno, one, nmsedec, type, vsc);
374 static void t1_3d_dec_refpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, char type, int cblksty) {
375 int i, j, k, m, one, poshalf, neghalf;
379 neghalf = bpno > 0 ? -poshalf : -1;
380 for (m = 0; m < l; m++) {
381 for (k = 0; k < h; k += 4) {
382 for (i = 0; i < w; i++) {
383 for (j = k; j < k + 4 && j < h; j++) {
384 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
385 t1_3d_dec_refpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], poshalf, neghalf, type, vsc);
390 } /* VSC and BYPASS by Antonin */
392 static void t1_3d_enc_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int bpno, int one, int *nmsedec, int partial, int vsc) {
396 opj_mqc_t *mqc = t1->mqc; /* MQC component */
398 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
403 if (!(*fsvr & (T1_3D_SIG | T1_3D_VISIT))) {
404 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
405 v = int_abs(*dp) & one ? 1 : 0;
409 *nmsedec += t1_3d_getnmsedec_sig(t1, int_abs(*dp), bpno + T1_NMSEDEC_FRACBITS);
410 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
412 mqc_encode(mqc, v ^ t1_3d_getspb(flag));
413 t1_3d_updateflags(fp, v);
417 *fsvr &= ~T1_3D_VISIT;
420 static void t1_3d_dec_clnpass_step(opj_t1_3d_t *t1, unsigned int *fp, int *fsvr, int *dp, int orient, int oneplushalf, int partial, int vsc) {
424 opj_mqc_t *mqc = t1->mqc; /* MQC component */
426 flag = vsc ? ((*fp) & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) : (*fp);
431 if (!(flagsvr & (T1_3D_SIG | T1_3D_VISIT))) {
432 mqc_setcurctx(mqc, t1_3d_getctxno_zc(flag, orient));
433 if (mqc_decode(mqc)) {
435 mqc_setcurctx(mqc, t1_3d_getctxno_sc(flag));
436 v = mqc_decode(mqc) ^ t1_3d_getspb(flag);
437 *dp = v ? -oneplushalf : oneplushalf;
438 t1_3d_updateflags(fp, v);
442 *fsvr &= ~T1_3D_VISIT;
443 } /* VSC and BYPASS by Antonin */
445 static void t1_3d_enc_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int *nmsedec, int cblksty) {
446 int i, j, k, m, one, agg, runlen, vsc;
448 opj_mqc_t *mqc = t1->mqc; /* MQC component */
451 one = 1 << (bpno + T1_NMSEDEC_FRACBITS);
452 for (m = 0; m < l; m++) {
453 for (k = 0; k < h; k += 4) {
454 for (i = 0; i < w; i++) {
456 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
457 agg = !( ((t1->flagSVR[1 + m][1 + k][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
458 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
459 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
460 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] | (T1_3D_SIG | T1_3D_VISIT)) & ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
464 ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
465 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
466 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
467 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
474 for (runlen = 0; runlen < 4; runlen++) {
475 if (int_abs(t1->data[m][k + runlen][i]) & one)
478 mqc_setcurctx(mqc, T1_CTXNO_AGG);
479 mqc_encode(mqc, runlen != 4);
483 mqc_setcurctx(mqc, T1_CTXNO_UNI);
484 mqc_encode(mqc, runlen >> 1);
485 mqc_encode(mqc, runlen & 1);
489 for (j = k + runlen; j < k + 4 && j < h; j++) {
490 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
491 t1_3d_enc_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, bpno, one, nmsedec, agg && (j == k + runlen), vsc);
498 static void t1_3d_dec_clnpass(opj_t1_3d_t *t1, int w, int h, int l, int bpno, int orient, int cblksty) {
499 int i, j, k, m, one, half, oneplushalf, agg, runlen, vsc;
500 int segsym = cblksty & J3D_CCP_CBLKSTY_SEGSYM;
502 opj_mqc_t *mqc = t1->mqc; /* MQC component */
506 oneplushalf = one | half;
507 for (m = 0; m < l; m++) {
508 for (k = 0; k < h; k += 4) {
509 for (i = 0; i < w; i++) {
511 if (cblksty & J3D_CCP_CBLKSTY_VSC) {
513 ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
514 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
515 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 2][1 + i] & T1_3D_SIG_OTH))
516 || ((t1->flagSVR[1 + m][1 + k + 3][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | ((t1->flags[1 + m][1 + k + 3][1 + i] & (~(T1_3D_SIG_S | T1_3D_SIG_SE | T1_3D_SIG_SW | (T1_3D_SGN_S << 16)))) & (T1_3D_SIG_OTH)))
520 ((t1->flagSVR[1 + m][1 + k][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k][1 + i] & T1_3D_SIG_OTH))
521 || ((t1->flagSVR[1 + m][1 + k + 1][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (t1->flags[1 + m][1 + k + 1][1 + i] & T1_3D_SIG_OTH))
522 || ((t1->flagSVR[1 + m][1 + k + 2][1 + i] & (T1_3D_SIG | T1_3D_VISIT)) | (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)) | (t1->flags[1 + m][1 + k + 3][1 + i] & T1_3D_SIG_OTH))
530 mqc_setcurctx(mqc, T1_CTXNO_AGG);
531 if (!mqc_decode(mqc)) {
534 mqc_setcurctx(mqc, T1_CTXNO_UNI);
535 runlen = mqc_decode(mqc);
536 runlen = (runlen << 1) | mqc_decode(mqc);
540 for (j = k + runlen; j < k + 4 && j < h; j++) {
541 vsc = ((cblksty & J3D_CCP_CBLKSTY_VSC) && (j == k + 3 || j == h - 1)) ? 1 : 0;
542 t1_3d_dec_clnpass_step(t1, &t1->flags[1 + m][1 + j][1 + i], &t1->flagSVR[1 + m][1 + j][1 + i], &t1->data[m][j][i], orient, oneplushalf, agg && (j == k + runlen), vsc);
549 mqc_setcurctx(mqc, T1_CTXNO_UNI);
551 v = (v << 1) | mqc_decode(mqc);
552 v = (v << 1) | mqc_decode(mqc);
553 v = (v << 1) | mqc_decode(mqc);
556 opj_event_msg(t1->cinfo, EVT_WARNING, "Bad segmentation symbol %x\n", v);
560 } /* VSC and BYPASS by Antonin */
563 static void t1_3d_encode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int compno, int level[3], int dwtid[3], double stepsize, int cblksty, int numcomps, opj_tcd_tile_t * tile) {
570 double cumwmsedec = 0;
571 char type = T1_TYPE_MQ;
573 opj_mqc_t *mqc = t1->mqc; /* MQC component */
575 w = cblk->x1 - cblk->x0;
576 h = cblk->y1 - cblk->y0;
577 l = cblk->z1 - cblk->z0;
580 for (k = 0; k < l; k++) {
581 for (j = 0; j < h; j++) {
582 for (i = 0; i < w; i++) {
583 max = int_max(max, int_abs(t1->data[k][j][i]));
587 for (k = 0; k <= l; k++) {
588 for (j = 0; j <= h; j++) {
589 for (i = 0; i <= w; i++) {
590 t1->flags[k][j][i] = 0;
591 t1->flagSVR[k][j][i] = 0;
596 cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
598 bpno = cblk->numbps - 1;
602 mqc_init_enc(mqc, cblk->data);
604 for (passno = 0; bpno >= 0; passno++) {
605 opj_tcd_pass_t *pass = &cblk->passes[passno];
608 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
612 t1_3d_enc_sigpass(t1, w, h, l, bpno, orient, &nmsedec, type, cblksty);
615 t1_3d_enc_refpass(t1, w, h, l, bpno, &nmsedec, type, cblksty);
618 t1_3d_enc_clnpass(t1, w, h, l, bpno, orient, &nmsedec, cblksty);
619 /* code switch SEGMARK (i.e. SEGSYM) */
620 if (cblksty & J3D_CCP_CBLKSTY_SEGSYM)
621 mqc_segmark_enc(mqc);
626 tmpwmsedec = t1_getwmsedec(nmsedec, compno, level, orient, bpno, stepsize, numcomps, dwtid);
627 cumwmsedec += tmpwmsedec;
628 tile->distotile += tmpwmsedec;
630 /* Code switch "RESTART" (i.e. TERMALL) */
631 if ((cblksty & J3D_CCP_CBLKSTY_TERMALL) && !((passtype == 2) && (bpno - 1 < 0))) {
632 if (type == T1_TYPE_RAW) {
635 /* correction = mqc_bypass_flush_enc(); */
636 } else { /* correction = mqc_restart_enc(); */
642 if (((bpno < (cblk->numbps - 4) && (passtype > 0))
643 || ((bpno == (cblk->numbps - 4)) && (passtype == 2))) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) {
644 if (type == T1_TYPE_RAW) {
657 if (++passtype == 3) {
662 if (pass->term && bpno > 0) {
663 type = ((bpno < (cblk->numbps - 4)) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
664 if (type == T1_TYPE_RAW)
665 mqc_bypass_init_enc(mqc);
667 mqc_restart_init_enc(mqc);
670 pass->distortiondec = cumwmsedec;
671 pass->rate = mqc_numbytes(mqc) + correction; /* FIXME */
672 pass->len = pass->rate - (passno == 0 ? 0 : cblk->passes[passno - 1].rate);
674 /* Code-switch "RESET" */
675 if (cblksty & J3D_CCP_CBLKSTY_RESET)
679 /* Code switch "ERTERM" (i.e. PTERM) */
680 if (cblksty & J3D_CCP_CBLKSTY_PTERM)
682 else /* Default coding */ if (!(cblksty & J3D_CCP_CBLKSTY_LAZY))
685 cblk->totalpasses = passno;
688 static void t1_3d_decode_cblk(opj_t1_3d_t *t1, opj_tcd_cblk_t * cblk, int orient, int roishift, int cblksty) {
693 char type = T1_TYPE_MQ; /* BYPASS mode */
694 opj_raw_t *raw = t1->raw; /* RAW component */
695 opj_mqc_t *mqc = t1->mqc; /* MQC component */
697 w = cblk->x1 - cblk->x0;
698 h = cblk->y1 - cblk->y0;
699 l = cblk->z1 - cblk->z0;
701 for (k = 0; k < l; k++) {
702 for (j = 0; j < h; j++) {
703 for (i = 0; i < w; i++) {
704 t1->data[k][j][i] = 0;
709 for (k = 0; k <= l; k++) {
710 for (j = 0; j <= h; j++) {
711 for (i = 0; i <= w; i++) {
712 t1->flags[k][j][i] = 0;
713 t1->flagSVR[k][j][i] = 0;
719 bpno = roishift + cblk->numbps - 1;
724 for (segno = 0; segno < cblk->numsegs; segno++) {
725 opj_tcd_seg_t *seg = &cblk->segs[segno];
728 type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2) && (cblksty & J3D_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW : T1_TYPE_MQ;
729 if (type == T1_TYPE_RAW) {
730 raw_init_dec(raw, seg->data, seg->len);
732 mqc_init_dec(mqc, seg->data, seg->len);
735 for (passno = 0; passno < seg->numpasses; passno++) {
738 t1_3d_dec_sigpass(t1, w, h, l, bpno+1, orient, type, cblksty);
741 t1_3d_dec_refpass(t1, w, h, l, bpno+1, type, cblksty);
744 t1_3d_dec_clnpass(t1, w, h, l, bpno+1, orient, cblksty);
748 if ((cblksty & J3D_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
751 if (++passtype == 3) {
760 static int t1_3d_init_ctxno_zc(unsigned int f, int orient) {
761 unsigned int h, v, c;
762 unsigned int d2xy, d2xz, d2yz, d3;
764 unsigned int hvc, hc, d2, d2xy2yz, d2xy2xz;
766 h = ((f & T1_3D_SIG_W) != 0) + ((f & T1_3D_SIG_E) != 0);
767 v = ((f & T1_3D_SIG_N) != 0) + ((f & T1_3D_SIG_S) != 0);
768 c = ((f & T1_3D_SIG_FC) != 0) + ((f & T1_3D_SIG_BC) != 0);
769 d2xy = ((f & T1_3D_SIG_NW) != 0) + ((f & T1_3D_SIG_NE) != 0) + ((f & T1_3D_SIG_SE) != 0) + ((f & T1_3D_SIG_SW) != 0);
770 d2xz = ((f & T1_3D_SIG_FW) != 0) + ((f & T1_3D_SIG_BW) != 0) + ((f & T1_3D_SIG_FE) != 0) + ((f & T1_3D_SIG_BE) != 0);
771 d2yz = ((f & T1_3D_SIG_FN) != 0) + ((f & T1_3D_SIG_FS) != 0) + ((f & T1_3D_SIG_BN) != 0) + ((f & T1_3D_SIG_BS) != 0);
772 d3 = ((f & T1_3D_SIG_FNW) != 0) + ((f & T1_3D_SIG_FNE) != 0) + ((f & T1_3D_SIG_FSE) != 0) + ((f & T1_3D_SIG_FSW) != 0)
773 + ((f & T1_3D_SIG_BNW) != 0) + ((f & T1_3D_SIG_BNE) != 0) + ((f & T1_3D_SIG_BSE) != 0) + ((f & T1_3D_SIG_BSW) != 0);
779 d2 = d2xy + d2xz + d2yz;
783 } else if (d2 == 1) {
788 } else if (hvc == 1) {
791 } else if (d2 == 1) {
796 } else if (hvc == 2) {
802 } else if (hvc == 3) {
813 d2xy2yz = d2xy + d2yz;
817 n = (!d2xy2yz) ? ((!d3) ? 0 : 1) : ((!d3) ? 2 : 3);
818 } else if (d2xy == 1) {
819 n = (!d2xy2yz) ? ((!d3) ? 4 : 5) : 6;
824 n = (v == 1) ? 8 : 9; /* =1 or =2*/
826 } else if (hc == 1) {
827 n = (!v) ? ( (!d2xy) ? ( (!d2xy2yz) ? ( (!d3) ? 10 : 11) : (12) ) : (13) ) : (14);
828 } else { /*if (hc >= 2)*/
837 d2xy2xz = d2xy + d2xz;
840 if (!hc && !d2xy2xz) {
842 } else if (hc == 1) {
843 n = (!d2xy2xz) ? 2 : 3;
847 } else if ( d2xz>=1 && !hc ) {
849 } else if ( hc>=1 ) {
850 n = (d2xz==1) ? 6 : 7;
855 } else if (d2xz == 1) {
857 } else if (d2xz == 2) {
859 } else { /* if (d2xz >= 3) {*/
868 return (T1_3D_CTXNO_ZC + n);
871 static int t1_3d_init_ctxno_sc(unsigned int f) {
875 hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E )
876 + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1)
877 - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) )
878 + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
880 vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N)
881 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1)
882 - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N))
883 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
885 cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC)
886 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1)
887 - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F))
888 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
899 n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);
901 n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);
902 } else if (hc == 1) {
904 n = (!cc) ? 1 : ( (cc<0) ? 2 : 4);
906 n = (!cc) ? 4 : ( (cc>0) ? 5 : 3);
909 } else if (hc == -1) {
911 n = (!cc) ? 1 : ( (cc>0) ? 2 : 4);
915 n = (!cc) ? 4 : ( (cc<0) ? 5 : 3);
918 return (T1_3D_CTXNO_SC + n);
921 static int t1_3d_init_ctxno_mag(unsigned int f, int f2) {
923 if (!(f2 & T1_3D_REFINE)) /*First refinement for this coefficient (no previous refinement)*/
924 n = (f & (T1_3D_SIG_PRIM)) ? 1 : 0;
928 return (T1_3D_CTXNO_MAG + n);
931 static int t1_3d_init_spb(unsigned int f) {
935 hc = int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == T1_3D_SIG_E )
936 + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W)) == T1_3D_SIG_W ) , 1)
937 - int_min( ( (f & (T1_3D_SIG_E | T1_3D_SGN_E)) == (T1_3D_SIG_E | T1_3D_SGN_E) )
938 + ( (f & (T1_3D_SIG_W | T1_3D_SGN_W) ) == (T1_3D_SIG_W | T1_3D_SGN_W)), 1);
940 vc = int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == T1_3D_SIG_N)
941 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == T1_3D_SIG_S), 1)
942 - int_min(((f & (T1_3D_SIG_N | T1_3D_SGN_N)) == (T1_3D_SIG_N | T1_3D_SGN_N))
943 + ((f & (T1_3D_SIG_S | T1_3D_SGN_S)) == (T1_3D_SIG_S | T1_3D_SGN_S)), 1);
945 cc = int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == T1_3D_SIG_FC)
946 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == T1_3D_SIG_BC), 1)
947 - int_min(((f & (T1_3D_SIG_FC | T1_3D_SGN_F)) == (T1_3D_SIG_FC | T1_3D_SGN_F))
948 + ((f & (T1_3D_SIG_BC | T1_3D_SGN_B)) == (T1_3D_SIG_BC | T1_3D_SGN_B)), 1);
950 n = ((hc + vc + cc) < 0);
955 static void t1_3d_init_luts(opj_t1_3d_t *t1) {
958 /*for (j = 0; j < 4; j++) {
959 for (i = 0; i < 256; ++i) {
960 t1->lut_ctxno_zc[(j << 8) | i] = t1_3d_init_ctxno_zc(i, j);
963 for (i = 0; i < 4096; i++) {
964 t1->lut_ctxno_sc[i] = t1_3d_init_ctxno_sc(i << 4);
966 for (j = 0; j < 2; j++) {
967 for (i = 0; i < 2048; ++i) {
968 t1->lut_ctxno_mag[(j << 11) + i] = t1_3d_init_ctxno_mag((j ? T1_3D_REFINE : 0) | i);
971 for (i = 0; i < 4096; ++i) {
972 t1->lut_spb[i] = t1_3d_init_spb(i << 4);
974 /* FIXME FIXME FIXME */
975 for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
976 t = i / pow(2, T1_NMSEDEC_FRACBITS);
979 t1->lut_nmsedec_sig[i] =
981 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
982 t1->lut_nmsedec_sig0[i] =
984 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
986 if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
991 t1->lut_nmsedec_ref[i] =
993 (int) (floor((u * u - v * v) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
994 t1->lut_nmsedec_ref0[i] =
996 (int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
1000 /* ----------------------------------------------------------------------- */
1002 opj_t1_3d_t* t1_3d_create(opj_common_ptr cinfo) {
1003 opj_t1_3d_t *t1 = (opj_t1_3d_t*)opj_malloc(sizeof(opj_t1_3d_t));
1006 /* create MQC and RAW handles */
1007 t1->mqc = mqc_create();
1008 t1->raw = raw_create();
1009 /* initialize the look-up tables of the Tier-1 coder/decoder */
1010 t1_3d_init_luts(t1);
1015 void t1_3d_destroy(opj_t1_3d_t *t1) {
1017 /* destroy MQC and RAW handles */
1018 mqc_destroy(t1->mqc);
1019 raw_destroy(t1->raw);
1024 void t1_3d_encode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1025 int compno, resno, bandno, precno, cblkno;
1026 int x, y, z, i, j, k, orient;
1028 tile->distotile = 0; /* fixed_quality */
1030 for (compno = 0; compno < tile->numcomps; compno++) {
1031 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1033 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1034 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1036 for (bandno = 0; bandno < res->numbands; bandno++) {
1037 opj_tcd_band_t *band = &res->bands[bandno];
1039 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1040 opj_tcd_precinct_t *prc = &band->precincts[precno];
1042 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1043 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1045 if (band->bandno == 0) {
1046 x = cblk->x0 - band->x0;
1047 y = cblk->y0 - band->y0;
1048 z = cblk->z0 - band->z0;
1049 } else if (band->bandno == 1) {
1050 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1051 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1052 y = cblk->y0 - band->y0;
1053 z = cblk->z0 - band->z0;
1054 } else if (band->bandno == 2) {
1055 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1056 x = cblk->x0 - band->x0;
1057 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1058 z = cblk->z0 - band->z0;
1059 } else if (band->bandno == 3) {
1060 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1061 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1062 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1063 z = cblk->z0 - band->z0;
1064 } else if (band->bandno == 4) {
1065 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1066 x = cblk->x0 - band->x0;
1067 y = cblk->y0 - band->y0;
1068 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1069 } else if (band->bandno == 5) {
1070 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1071 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1072 y = cblk->y0 - band->y0;
1073 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1074 } else if (band->bandno == 6) {
1075 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1076 x = cblk->x0 - band->x0;
1077 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1078 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1079 } else if (band->bandno == 7) {
1080 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1081 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1082 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1083 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1086 if (tcp->tccps[compno].reversible == 1) {
1087 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1088 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1089 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1091 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] << T1_NMSEDEC_FRACBITS;
1095 } else if (tcp->tccps[compno].reversible == 0) {
1096 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1097 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1098 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1099 t1->data[k][j][i] = fix_mul(
1100 tilec->data[(x + i) + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)],
1101 8192 * 8192 / ((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
1106 orient = band->bandno; /* FIXME */
1107 for (i = 0; i < 3; i++)
1108 level[i] = tilec->numresolution[i] - 1 - resno;
1110 t1_3d_encode_cblk(t1, cblk, orient, compno, level, tcp->tccps[compno].dwtid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile);
1119 void t1_3d_decode_cblks(opj_t1_3d_t *t1, opj_tcd_tile_t *tile, opj_tcp_t *tcp) {
1120 int compno, resno, bandno, precno, cblkno;
1122 for (compno = 0; compno < tile->numcomps; compno++) {
1123 opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
1125 for (resno = 0; resno < tilec->numresolution[0]; resno++) {
1126 opj_tcd_resolution_t *res = &tilec->resolutions[resno];
1128 for (bandno = 0; bandno < res->numbands; bandno++) {
1129 opj_tcd_band_t *band = &res->bands[bandno];
1131 for (precno = 0; precno < res->prctno[0] * res->prctno[1] * res->prctno[2]; precno++) {
1132 opj_tcd_precinct_t *prc = &band->precincts[precno];
1134 for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) {
1135 int x, y, z, i, j, k, orient;
1136 opj_tcd_cblk_t *cblk = &prc->cblks[cblkno];
1138 orient = band->bandno; /* FIXME */
1140 /*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);*/
1141 t1_3d_decode_cblk(t1, cblk, orient, tcp->tccps[compno].roishift, tcp->tccps[compno].cblksty);
1143 if (band->bandno == 0) {
1144 x = cblk->x0 - band->x0;
1145 y = cblk->y0 - band->y0;
1146 z = cblk->z0 - band->z0;
1147 } else if (band->bandno == 1) {
1148 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1149 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1150 y = cblk->y0 - band->y0;
1151 z = cblk->z0 - band->z0;
1152 } else if (band->bandno == 2) {
1153 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1154 x = cblk->x0 - band->x0;
1155 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1156 z = cblk->z0 - band->z0;
1157 } else if (band->bandno == 3) {
1158 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1159 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1160 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1161 z = cblk->z0 - band->z0;
1162 } else if (band->bandno == 4) {
1163 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1164 x = cblk->x0 - band->x0;
1165 y = cblk->y0 - band->y0;
1166 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1167 } else if (band->bandno == 5) {
1168 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1169 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1170 y = cblk->y0 - band->y0;
1171 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1172 } else if (band->bandno == 6) {
1173 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1174 x = cblk->x0 - band->x0;
1175 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1176 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1177 } else if (band->bandno == 7) {
1178 opj_tcd_resolution_t *pres = &tilec->resolutions[resno - 1];
1179 x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
1180 y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
1181 z = pres->z1 - pres->z0 + cblk->z0 - band->z0;
1184 if (tcp->tccps[compno].roishift) {
1185 int thresh, val, mag;
1186 thresh = 1 << tcp->tccps[compno].roishift;
1187 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1188 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1189 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1190 val = t1->data[k][j][i];
1192 if (mag >= thresh) {
1193 mag >>= tcp->tccps[compno].roishift;
1194 t1->data[k][j][i] = val < 0 ? -mag : mag;
1201 if (tcp->tccps[compno].reversible == 1) {
1202 for (k = 0; k < cblk->z1 - cblk->z0; k++) {
1203 for (j = 0; j < cblk->y1 - cblk->y0; j++) {
1204 for (i = 0; i < cblk->x1 - cblk->x0; i++) {
1205 int tmp = t1->data[k][j][i];
1206 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = tmp/2;
1210 } else { /* if (tcp->tccps[compno].reversible == 0) */
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++) {
1214 double tmp = (double)(t1->data[k][j][i] * band->stepsize * 4096.0);
1215 if (t1->data[k][j][i] >> 1 == 0) {
1216 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = 0;
1218 int tmp2 = ((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
1219 tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0) + (z + k) * (tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0)] = ((tmp<0)?-tmp2:tmp2);