diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2023-09-24 16:51:04 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2023-09-24 16:51:04 +0000 |
commit | e90b1605a083938d582dd459f8a14141274e7492 (patch) | |
tree | d01b692f4c437b6fe831d0308a330e3e5246fcd7 /converter/other/jbig/libjbig/jbig.c | |
parent | 0f567f941233d027720170114917327b842d4188 (diff) | |
download | netpbm-mirror-e90b1605a083938d582dd459f8a14141274e7492.tar.gz netpbm-mirror-e90b1605a083938d582dd459f8a14141274e7492.tar.xz netpbm-mirror-e90b1605a083938d582dd459f8a14141274e7492.zip |
whitespace
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4685 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/other/jbig/libjbig/jbig.c')
-rw-r--r-- | converter/other/jbig/libjbig/jbig.c | 1770 |
1 files changed, 885 insertions, 885 deletions
diff --git a/converter/other/jbig/libjbig/jbig.c b/converter/other/jbig/libjbig/jbig.c index cf24a93b..42932ca7 100644 --- a/converter/other/jbig/libjbig/jbig.c +++ b/converter/other/jbig/libjbig/jbig.c @@ -143,7 +143,7 @@ static void *checked_malloc(size_t nmemb, size_t size) #if 0 fprintf(stderr, "%p = malloc(%lu * %lu)\n", p, - (unsigned long) nmemb, (unsigned long) size); + (unsigned long) nmemb, (unsigned long) size); #endif return p; @@ -169,7 +169,7 @@ static void *checked_realloc(void *ptr, size_t nmemb, size_t size) #if 0 fprintf(stderr, "%p = realloc(%p, %lu * %lu)\n", p, ptr, - (unsigned long) nmemb, (unsigned long) size); + (unsigned long) nmemb, (unsigned long) size); #endif return p; @@ -334,9 +334,9 @@ static void jbg_buf_prefix(struct jbg_buf *new_prefix, struct jbg_buf **start) * After the call, *head == NULL. */ static void jbg_buf_output(struct jbg_buf **head, - void (*data_out)(unsigned char *start, - size_t len, void *file), - void *file) + void (*data_out)(unsigned char *start, + size_t len, void *file), + void *file) { struct jbg_buf *tmp; @@ -388,7 +388,7 @@ static void jbg_set_default_l0(struct jbg_enc_state *s) * Calculate the number of stripes, as defined in clause 6.2.3 of T.82. */ static unsigned long jbg_stripes(unsigned long l0, unsigned long yd, - unsigned long d) + unsigned long d) { unsigned long y0 = jbg_ceil_half(yd, d); @@ -767,8 +767,8 @@ static char jbg_dptable[256 + 512 + 2048 + 4096] = { void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y, int planes, unsigned char **p, void (*data_out)(unsigned char *start, size_t len, - void *file), - void *file) + void *file), + void *file) { unsigned long l, lx; int i; @@ -831,7 +831,7 @@ void jbg_enc_init(struct jbg_enc_state *s, unsigned long x, unsigned long y, * is returned. */ int jbg_enc_lrlmax(struct jbg_enc_state *s, unsigned long x, - unsigned long y) + unsigned long y) { for (s->d = 0; s->d < 6; s->d++) if (jbg_ceil_half(s->xd, s->d) <= x && jbg_ceil_half(s->yd, s->d) <= y) @@ -882,7 +882,7 @@ int jbg_enc_lrange(struct jbg_enc_state *s, int dl, int dh) * number of layer 0 lines per stripes. */ void jbg_enc_options(struct jbg_enc_state *s, int order, int options, - unsigned long l0, int mx, int my) + unsigned long l0, int mx, int my) { if (order >= 0 && order <= 0x0f) s->order = order; if (options >= 0) s->options = options; @@ -900,7 +900,7 @@ void jbg_enc_options(struct jbg_enc_state *s, int order, int options, * for later output in the correct order. */ static void encode_sde(struct jbg_enc_state *s, - long stripe, int layer, int plane) + long stripe, int layer, int plane) { unsigned char *hp, *lp1, *lp2, *p0, *p1, *q1, *q2; unsigned long hl, ll, hx, hy, lx, ly, hbpl, lbpl; @@ -933,7 +933,7 @@ static void encode_sde(struct jbg_enc_state *s, if (stripe == 0) tp_lines = tp_exceptions = tp_pixels = dp_pixels = encoded_pixels = 0; fprintf(stderr, "encode_sde: s/d/p = %2ld/%2d/%2d\n", - stripe, layer, plane); + stripe, layer, plane); #endif /* number of lines per stripe in highres image */ @@ -1001,59 +1001,59 @@ static void encode_sde(struct jbg_enc_state *s, /* check whether it is worth to perform an ATMOVE */ if (!at_determined && c_all > 2048) { - cmin = clmin = 0xffffffffL; - cmax = clmax = 0; - tmax = 0; - for (t = (s->options & JBG_LRLTWO) ? 5 : 3; t <= s->mx; t++) { - if (c[t] > cmax) cmax = c[t]; - if (c[t] < cmin) cmin = c[t]; - if (c[t] > c[tmax]) tmax = t; - } - clmin = (c[0] < cmin) ? c[0] : cmin; - clmax = (c[0] > cmax) ? c[0] : cmax; - if (c_all - cmax < (c_all >> 3) && - cmax - c[s->tx[plane]] > c_all - cmax && - cmax - c[s->tx[plane]] > (c_all >> 4) && - /* ^ T.82 said < here, fixed in Cor.1/25 */ - cmax - (c_all - c[s->tx[plane]]) > c_all - cmax && - cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) && - cmax - cmin > (c_all >> 2) && - (s->tx[plane] || clmax - clmin > (c_all >> 3))) { - /* we have decided to perform an ATMOVE */ - new_tx = tmax; - if (!(s->options & JBG_DELAY_AT)) { - new_tx_line = i; - s->tx[plane] = new_tx; - } + cmin = clmin = 0xffffffffL; + cmax = clmax = 0; + tmax = 0; + for (t = (s->options & JBG_LRLTWO) ? 5 : 3; t <= s->mx; t++) { + if (c[t] > cmax) cmax = c[t]; + if (c[t] < cmin) cmin = c[t]; + if (c[t] > c[tmax]) tmax = t; + } + clmin = (c[0] < cmin) ? c[0] : cmin; + clmax = (c[0] > cmax) ? c[0] : cmax; + if (c_all - cmax < (c_all >> 3) && + cmax - c[s->tx[plane]] > c_all - cmax && + cmax - c[s->tx[plane]] > (c_all >> 4) && + /* ^ T.82 said < here, fixed in Cor.1/25 */ + cmax - (c_all - c[s->tx[plane]]) > c_all - cmax && + cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) && + cmax - cmin > (c_all >> 2) && + (s->tx[plane] || clmax - clmin > (c_all >> 3))) { + /* we have decided to perform an ATMOVE */ + new_tx = tmax; + if (!(s->options & JBG_DELAY_AT)) { + new_tx_line = i; + s->tx[plane] = new_tx; + } #ifdef DEBUG - fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n", - i, new_tx, c_all); + fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n", + i, new_tx, c_all); #endif - } - at_determined = 1; + } + at_determined = 1; } assert(s->tx[plane] >= 0); /* i.e., tx can safely be cast to unsigned */ /* typical prediction */ if (s->options & JBG_TPBON) { - ltp = 1; - p1 = hp; - if (i > 0 || !reset) { - q1 = hp - hbpl; - while (q1 < hp && (ltp = (*p1++ == *q1++)) != 0) ; - } else - while (p1 < hp + hbpl && (ltp = (*p1++ == 0)) != 0) ; - arith_encode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX, - ltp == ltp_old); + ltp = 1; + p1 = hp; + if (i > 0 || !reset) { + q1 = hp - hbpl; + while (q1 < hp && (ltp = (*p1++ == *q1++)) != 0) ; + } else + while (p1 < hp + hbpl && (ltp = (*p1++ == 0)) != 0) ; + arith_encode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX, + ltp == ltp_old); #ifdef DEBUG - tp_lines += ltp; + tp_lines += ltp; #endif - ltp_old = ltp; - if (ltp) { - /* skip next line */ - hp += hbpl; - continue; - } + ltp_old = ltp; + if (ltp) { + /* skip next line */ + hp += hbpl; + continue; + } } /* @@ -1071,102 +1071,102 @@ static void encode_sde(struct jbg_enc_state *s, /* encode line */ for (j = 0; j < hx; hp++) { - line_h1 |= *hp; - if (j < hbpl * 8 - 8 && (i > 0 || !reset)) { - line_h2 |= *(hp - hbpl + 1); - if (i > 1 || !reset) - line_h3 |= *(hp - hbpl - hbpl + 1); - } - if (s->options & JBG_LRLTWO) { - /* two line template */ - do { - line_h1 <<= 1; line_h2 <<= 1; line_h3 <<= 1; - if (s->tx[plane]) { - if ((unsigned) s->tx[plane] > j) - a = 0; - else { - o = (j - s->tx[plane]) - (j & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - a <<= 4; - } - assert(s->tx[plane] > 23 || - a == ((line_h1 >> (4 + s->tx[plane])) & 0x010)); - arith_encode(se, (((line_h2 >> 10) & 0x3e0) | a | - ((line_h1 >> 9) & 0x00f)), - (line_h1 >> 8) & 1); - } - else - arith_encode(se, (((line_h2 >> 10) & 0x3f0) | - ((line_h1 >> 9) & 0x00f)), - (line_h1 >> 8) & 1); + line_h1 |= *hp; + if (j < hbpl * 8 - 8 && (i > 0 || !reset)) { + line_h2 |= *(hp - hbpl + 1); + if (i > 1 || !reset) + line_h3 |= *(hp - hbpl - hbpl + 1); + } + if (s->options & JBG_LRLTWO) { + /* two line template */ + do { + line_h1 <<= 1; line_h2 <<= 1; line_h3 <<= 1; + if (s->tx[plane]) { + if ((unsigned) s->tx[plane] > j) + a = 0; + else { + o = (j - s->tx[plane]) - (j & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + a <<= 4; + } + assert(s->tx[plane] > 23 || + a == ((line_h1 >> (4 + s->tx[plane])) & 0x010)); + arith_encode(se, (((line_h2 >> 10) & 0x3e0) | a | + ((line_h1 >> 9) & 0x00f)), + (line_h1 >> 8) & 1); + } + else + arith_encode(se, (((line_h2 >> 10) & 0x3f0) | + ((line_h1 >> 9) & 0x00f)), + (line_h1 >> 8) & 1); #ifdef DEBUG - encoded_pixels++; + encoded_pixels++; #endif - /* statistics for adaptive template changes */ - if (!at_determined && j >= s->mx && j < hx-2) { - p = (line_h1 & 0x100) != 0; /* current pixel value */ - c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */ - assert((!(((line_h2 >> 6) ^ line_h1) & 0x100)) == - (((line_h2 & 0x4000) != 0) == p)); - for (t = 5; t <= s->mx && t <= j; t++) { - o = (j - t) - (j & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - assert(t > 23 || - (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100)); - c[t] += a == p; - } - for (; t <= s->mx; t++) { - c[t] += 0 == p; - } - ++c_all; - } - } while (++j & 7 && j < hx); - } else { - /* three line template */ - do { - line_h1 <<= 1; line_h2 <<= 1; line_h3 <<= 1; - if (s->tx[plane]) { - if ((unsigned) s->tx[plane] > j) - a = 0; - else { - o = (j - s->tx[plane]) - (j & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - a <<= 2; - } - assert(s->tx[plane] > 23 || - a == ((line_h1 >> (6 + s->tx[plane])) & 0x004)); - arith_encode(se, (((line_h3 >> 8) & 0x380) | - ((line_h2 >> 12) & 0x078) | a | - ((line_h1 >> 9) & 0x003)), - (line_h1 >> 8) & 1); - } else - arith_encode(se, (((line_h3 >> 8) & 0x380) | - ((line_h2 >> 12) & 0x07c) | - ((line_h1 >> 9) & 0x003)), - (line_h1 >> 8) & 1); + /* statistics for adaptive template changes */ + if (!at_determined && j >= s->mx && j < hx-2) { + p = (line_h1 & 0x100) != 0; /* current pixel value */ + c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */ + assert((!(((line_h2 >> 6) ^ line_h1) & 0x100)) == + (((line_h2 & 0x4000) != 0) == p)); + for (t = 5; t <= s->mx && t <= j; t++) { + o = (j - t) - (j & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + assert(t > 23 || + (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100)); + c[t] += a == p; + } + for (; t <= s->mx; t++) { + c[t] += 0 == p; + } + ++c_all; + } + } while (++j & 7 && j < hx); + } else { + /* three line template */ + do { + line_h1 <<= 1; line_h2 <<= 1; line_h3 <<= 1; + if (s->tx[plane]) { + if ((unsigned) s->tx[plane] > j) + a = 0; + else { + o = (j - s->tx[plane]) - (j & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + a <<= 2; + } + assert(s->tx[plane] > 23 || + a == ((line_h1 >> (6 + s->tx[plane])) & 0x004)); + arith_encode(se, (((line_h3 >> 8) & 0x380) | + ((line_h2 >> 12) & 0x078) | a | + ((line_h1 >> 9) & 0x003)), + (line_h1 >> 8) & 1); + } else + arith_encode(se, (((line_h3 >> 8) & 0x380) | + ((line_h2 >> 12) & 0x07c) | + ((line_h1 >> 9) & 0x003)), + (line_h1 >> 8) & 1); #ifdef DEBUG - encoded_pixels++; + encoded_pixels++; #endif - /* statistics for adaptive template changes */ - if (!at_determined && j >= s->mx && j < hx-2) { - p = (line_h1 & 0x100) != 0; /* current pixel value */ - c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */ - assert((!(((line_h2 >> 6) ^ line_h1) & 0x100)) == - (((line_h2 & 0x4000) != 0) == p)); - for (t = 3; t <= s->mx && t <= j; t++) { - o = (j - t) - (j & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - assert(t > 23 || - (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100)); - c[t] += a == p; - } - for (; t <= s->mx; t++) { - c[t] += 0 == p; - } - ++c_all; - } - } while (++j & 7 && j < hx); - } /* if (s->options & JBG_LRLTWO) */ + /* statistics for adaptive template changes */ + if (!at_determined && j >= s->mx && j < hx-2) { + p = (line_h1 & 0x100) != 0; /* current pixel value */ + c[0] += ((line_h2 & 0x4000) != 0) == p; /* default position */ + assert((!(((line_h2 >> 6) ^ line_h1) & 0x100)) == + (((line_h2 & 0x4000) != 0) == p)); + for (t = 3; t <= s->mx && t <= j; t++) { + o = (j - t) - (j & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + assert(t > 23 || + (a == p) == !(((line_h1 >> t) ^ line_h1) & 0x100)); + c[t] += a == p; + } + for (; t <= s->mx; t++) { + c[t] += 0 == p; + } + ++c_all; + } + } while (++j & 7 && j < hx); + } /* if (s->options & JBG_LRLTWO) */ } /* for (j = ...) */ } /* for (i = ...) */ @@ -1180,101 +1180,101 @@ static void encode_sde(struct jbg_enc_state *s, /* check whether it is worth to perform an ATMOVE */ if (!at_determined && c_all > 2048) { - cmin = clmin = 0xffffffffL; - cmax = clmax = 0; - tmax = 0; - for (t = 3; t <= s->mx; t++) { - if (c[t] > cmax) cmax = c[t]; - if (c[t] < cmin) cmin = c[t]; - if (c[t] > c[tmax]) tmax = t; - } - clmin = (c[0] < cmin) ? c[0] : cmin; - clmax = (c[0] > cmax) ? c[0] : cmax; - if (c_all - cmax < (c_all >> 3) && - cmax - c[s->tx[plane]] > c_all - cmax && - cmax - c[s->tx[plane]] > (c_all >> 4) && - /* ^ T.82 said < here, fixed in Cor.1/25 */ - cmax - (c_all - c[s->tx[plane]]) > c_all - cmax && - cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) && - cmax - cmin > (c_all >> 2) && - (s->tx[plane] || clmax - clmin > (c_all >> 3))) { - /* we have decided to perform an ATMOVE */ - new_tx = tmax; - if (!(s->options & JBG_DELAY_AT)) { - new_tx_line = i; - s->tx[plane] = new_tx; - } + cmin = clmin = 0xffffffffL; + cmax = clmax = 0; + tmax = 0; + for (t = 3; t <= s->mx; t++) { + if (c[t] > cmax) cmax = c[t]; + if (c[t] < cmin) cmin = c[t]; + if (c[t] > c[tmax]) tmax = t; + } + clmin = (c[0] < cmin) ? c[0] : cmin; + clmax = (c[0] > cmax) ? c[0] : cmax; + if (c_all - cmax < (c_all >> 3) && + cmax - c[s->tx[plane]] > c_all - cmax && + cmax - c[s->tx[plane]] > (c_all >> 4) && + /* ^ T.82 said < here, fixed in Cor.1/25 */ + cmax - (c_all - c[s->tx[plane]]) > c_all - cmax && + cmax - (c_all - c[s->tx[plane]]) > (c_all >> 4) && + cmax - cmin > (c_all >> 2) && + (s->tx[plane] || clmax - clmin > (c_all >> 3))) { + /* we have decided to perform an ATMOVE */ + new_tx = tmax; + if (!(s->options & JBG_DELAY_AT)) { + new_tx_line = i; + s->tx[plane] = new_tx; + } #ifdef DEBUG - fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n", - i, new_tx, c_all); + fprintf(stderr, "ATMOVE: line=%ld, tx=%d, c_all=%ld\n", + i, new_tx, c_all); #endif - } - at_determined = 1; + } + at_determined = 1; } if ((i >> 1) >= ll - 1 || (y >> 1) >= ly - 1) - lp1 = lp2; + lp1 = lp2; /* typical prediction */ if (s->options & JBG_TPDON && (i & 1) == 0) { - q1 = lp1; q2 = lp2; - p0 = p1 = hp; - if (i < hl - 1 && y < hy - 1) - p0 = hp + hbpl; - if (i > 1 || !reset) - line_l3 = (long)*(q2 - lbpl) << 8; - else - line_l3 = 0; - line_l2 = (long)*q2 << 8; - line_l1 = (long)*q1 << 8; - ltp = 1; - for (j = 0; j < lx && ltp; q1++, q2++) { - if (j < lbpl * 8 - 8) { - if (i > 1 || !reset) - line_l3 |= *(q2 - lbpl + 1); - line_l2 |= *(q2 + 1); - line_l1 |= *(q1 + 1); - } - do { - if ((j >> 2) < hbpl) { - line_h1 = *(p1++); - line_h0 = *(p0++); - } - do { - line_l3 <<= 1; - line_l2 <<= 1; - line_l1 <<= 1; - line_h1 <<= 2; - line_h0 <<= 2; - cx = (((line_l3 >> 15) & 0x007) | - ((line_l2 >> 12) & 0x038) | - ((line_l1 >> 9) & 0x1c0)); - if (cx == 0x000) - if ((line_h1 & 0x300) == 0 && (line_h0 & 0x300) == 0) - s->tp[j] = 0; - else { - ltp = 0; + q1 = lp1; q2 = lp2; + p0 = p1 = hp; + if (i < hl - 1 && y < hy - 1) + p0 = hp + hbpl; + if (i > 1 || !reset) + line_l3 = (long)*(q2 - lbpl) << 8; + else + line_l3 = 0; + line_l2 = (long)*q2 << 8; + line_l1 = (long)*q1 << 8; + ltp = 1; + for (j = 0; j < lx && ltp; q1++, q2++) { + if (j < lbpl * 8 - 8) { + if (i > 1 || !reset) + line_l3 |= *(q2 - lbpl + 1); + line_l2 |= *(q2 + 1); + line_l1 |= *(q1 + 1); + } + do { + if ((j >> 2) < hbpl) { + line_h1 = *(p1++); + line_h0 = *(p0++); + } + do { + line_l3 <<= 1; + line_l2 <<= 1; + line_l1 <<= 1; + line_h1 <<= 2; + line_h0 <<= 2; + cx = (((line_l3 >> 15) & 0x007) | + ((line_l2 >> 12) & 0x038) | + ((line_l1 >> 9) & 0x1c0)); + if (cx == 0x000) + if ((line_h1 & 0x300) == 0 && (line_h0 & 0x300) == 0) + s->tp[j] = 0; + else { + ltp = 0; #ifdef DEBUG - tp_exceptions++; + tp_exceptions++; #endif - } - else if (cx == 0x1ff) - if ((line_h1 & 0x300) == 0x300 && (line_h0 & 0x300) == 0x300) - s->tp[j] = 1; - else { - ltp = 0; + } + else if (cx == 0x1ff) + if ((line_h1 & 0x300) == 0x300 && (line_h0 & 0x300) == 0x300) + s->tp[j] = 1; + else { + ltp = 0; #ifdef DEBUG - tp_exceptions++; + tp_exceptions++; #endif - } - else - s->tp[j] = 2; - } while (++j & 3 && j < lx); - } while (j & 7 && j < lx); - } /* for (j = ...) */ - arith_encode(se, TPDCX, !ltp); + } + else + s->tp[j] = 2; + } while (++j & 3 && j < lx); + } while (j & 7 && j < lx); + } /* for (j = ...) */ + arith_encode(se, TPDCX, !ltp); #ifdef DEBUG - tp_lines += ltp; + tp_lines += ltp; #endif } @@ -1302,158 +1302,158 @@ static void encode_sde(struct jbg_enc_state *s, line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0; if (i > 0 || !reset) line_h2 = (long)*(hp - hbpl) << 8; if (i > 1 || !reset) { - line_h3 = (long)*(hp - hbpl - hbpl) << 8; - line_l3 = (long)*(lp2 - lbpl) << 8; + line_h3 = (long)*(hp - hbpl - hbpl) << 8; + line_l3 = (long)*(lp2 - lbpl) << 8; } line_l2 = (long)*lp2 << 8; line_l1 = (long)*lp1 << 8; /* encode line */ for (j = 0; j < hx; lp1++, lp2++) { - if ((j >> 1) < lbpl * 8 - 8) { - if (i > 1 || !reset) - line_l3 |= *(lp2 - lbpl + 1); - line_l2 |= *(lp2 + 1); - line_l1 |= *(lp1 + 1); - } - do { /* ... while (j & 15 && j < hx) */ - - assert(hp - (s->lhp[s->highres[plane]][plane] + - (stripe * hl + i) * hbpl) - == (ptrdiff_t) j >> 3); - - assert(lp2 - (s->lhp[1-s->highres[plane]][plane] + - (stripe * ll + (i>>1)) * lbpl) - == (ptrdiff_t) j >> 4); - - line_h1 |= *hp; - if (j < hbpl * 8 - 8) { - if (i > 0 || !reset) { - line_h2 |= *(hp - hbpl + 1); - if (i > 1 || !reset) - line_h3 |= *(hp - hbpl - hbpl + 1); - } - } - do { /* ... while (j & 7 && j < hx) */ - line_l1 <<= 1; line_l2 <<= 1; line_l3 <<= 1; - if (ltp && s->tp[j >> 1] < 2) { - /* pixel are typical and have not to be encoded */ - line_h1 <<= 2; line_h2 <<= 2; line_h3 <<= 2; + if ((j >> 1) < lbpl * 8 - 8) { + if (i > 1 || !reset) + line_l3 |= *(lp2 - lbpl + 1); + line_l2 |= *(lp2 + 1); + line_l1 |= *(lp1 + 1); + } + do { /* ... while (j & 15 && j < hx) */ + + assert(hp - (s->lhp[s->highres[plane]][plane] + + (stripe * hl + i) * hbpl) + == (ptrdiff_t) j >> 3); + + assert(lp2 - (s->lhp[1-s->highres[plane]][plane] + + (stripe * ll + (i>>1)) * lbpl) + == (ptrdiff_t) j >> 4); + + line_h1 |= *hp; + if (j < hbpl * 8 - 8) { + if (i > 0 || !reset) { + line_h2 |= *(hp - hbpl + 1); + if (i > 1 || !reset) + line_h3 |= *(hp - hbpl - hbpl + 1); + } + } + do { /* ... while (j & 7 && j < hx) */ + line_l1 <<= 1; line_l2 <<= 1; line_l3 <<= 1; + if (ltp && s->tp[j >> 1] < 2) { + /* pixel are typical and have not to be encoded */ + line_h1 <<= 2; line_h2 <<= 2; line_h3 <<= 2; #ifdef DEBUG - do { - ++tp_pixels; - } while (++j & 1 && j < hx); + do { + ++tp_pixels; + } while (++j & 1 && j < hx); #else - j += 2; + j += 2; #endif - } else - do { /* ... while (++j & 1 && j < hx) */ - line_h1 <<= 1; line_h2 <<= 1; line_h3 <<= 1; - - /* deterministic prediction */ - if (s->options & JBG_DPON) { - if ((y & 1) == 0) { - if ((j & 1) == 0) { - /* phase 0 */ - if (s->dppriv[((line_l3 >> 16) & 0x003) | - ((line_l2 >> 14) & 0x00c) | - ((line_h1 >> 5) & 0x010) | - ((line_h2 >> 10) & 0x0e0)] < 2) { + } else + do { /* ... while (++j & 1 && j < hx) */ + line_h1 <<= 1; line_h2 <<= 1; line_h3 <<= 1; + + /* deterministic prediction */ + if (s->options & JBG_DPON) { + if ((y & 1) == 0) { + if ((j & 1) == 0) { + /* phase 0 */ + if (s->dppriv[((line_l3 >> 16) & 0x003) | + ((line_l2 >> 14) & 0x00c) | + ((line_h1 >> 5) & 0x010) | + ((line_h2 >> 10) & 0x0e0)] < 2) { #ifdef DEBUG - ++dp_pixels; + ++dp_pixels; #endif - continue; - } - } else { - /* phase 1 */ - if (s->dppriv[(((line_l3 >> 16) & 0x003) | - ((line_l2 >> 14) & 0x00c) | - ((line_h1 >> 5) & 0x030) | - ((line_h2 >> 10) & 0x1c0)) + 256] < 2) { + continue; + } + } else { + /* phase 1 */ + if (s->dppriv[(((line_l3 >> 16) & 0x003) | + ((line_l2 >> 14) & 0x00c) | + ((line_h1 >> 5) & 0x030) | + ((line_h2 >> 10) & 0x1c0)) + 256] < 2) { #ifdef DEBUG - ++dp_pixels; + ++dp_pixels; #endif - continue; - } - } - } else { - if ((j & 1) == 0) { - /* phase 2 */ - if (s->dppriv[(((line_l3 >> 16) & 0x003) | - ((line_l2 >> 14) & 0x00c) | - ((line_h1 >> 5) & 0x010) | - ((line_h2 >> 10) & 0x0e0) | - ((line_h3 >> 7) & 0x700)) + 768] < 2) { + continue; + } + } + } else { + if ((j & 1) == 0) { + /* phase 2 */ + if (s->dppriv[(((line_l3 >> 16) & 0x003) | + ((line_l2 >> 14) & 0x00c) | + ((line_h1 >> 5) & 0x010) | + ((line_h2 >> 10) & 0x0e0) | + ((line_h3 >> 7) & 0x700)) + 768] < 2) { #ifdef DEBUG - ++dp_pixels; + ++dp_pixels; #endif - continue; - } - } else { - /* phase 3 */ - if (s->dppriv[(((line_l3 >> 16) & 0x003) | - ((line_l2 >> 14) & 0x00c) | - ((line_h1 >> 5) & 0x030) | - ((line_h2 >> 10) & 0x1c0) | - ((line_h3 >> 7) & 0xe00)) + 2816] < 2) { + continue; + } + } else { + /* phase 3 */ + if (s->dppriv[(((line_l3 >> 16) & 0x003) | + ((line_l2 >> 14) & 0x00c) | + ((line_h1 >> 5) & 0x030) | + ((line_h2 >> 10) & 0x1c0) | + ((line_h3 >> 7) & 0xe00)) + 2816] < 2) { #ifdef DEBUG - ++dp_pixels; + ++dp_pixels; #endif - continue; - } - } - } - } - - /* determine context */ - if (s->tx[plane]) { - if ((unsigned) s->tx[plane] > j) - a = 0; - else { - o = (j - s->tx[plane]) - (j & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - a <<= 4; - } - assert(s->tx[plane] > 23 || - a == ((line_h1 >> (4 + s->tx[plane])) & 0x010)); - cx = (((line_h1 >> 9) & 0x003) | a | - ((line_h2 >> 13) & 0x00c) | - ((line_h3 >> 11) & 0x020)); - } else - cx = (((line_h1 >> 9) & 0x003) | - ((line_h2 >> 13) & 0x01c) | - ((line_h3 >> 11) & 0x020)); - if (j & 1) - cx |= (((line_l2 >> 9) & 0x0c0) | - ((line_l1 >> 7) & 0x300)) | (1UL << 10); - else - cx |= (((line_l2 >> 10) & 0x0c0) | - ((line_l1 >> 8) & 0x300)); - cx |= (y & 1) << 11; - - arith_encode(se, cx, (line_h1 >> 8) & 1); + continue; + } + } + } + } + + /* determine context */ + if (s->tx[plane]) { + if ((unsigned) s->tx[plane] > j) + a = 0; + else { + o = (j - s->tx[plane]) - (j & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + a <<= 4; + } + assert(s->tx[plane] > 23 || + a == ((line_h1 >> (4 + s->tx[plane])) & 0x010)); + cx = (((line_h1 >> 9) & 0x003) | a | + ((line_h2 >> 13) & 0x00c) | + ((line_h3 >> 11) & 0x020)); + } else + cx = (((line_h1 >> 9) & 0x003) | + ((line_h2 >> 13) & 0x01c) | + ((line_h3 >> 11) & 0x020)); + if (j & 1) + cx |= (((line_l2 >> 9) & 0x0c0) | + ((line_l1 >> 7) & 0x300)) | (1UL << 10); + else + cx |= (((line_l2 >> 10) & 0x0c0) | + ((line_l1 >> 8) & 0x300)); + cx |= (y & 1) << 11; + + arith_encode(se, cx, (line_h1 >> 8) & 1); #ifdef DEBUG - encoded_pixels++; + encoded_pixels++; #endif - /* statistics for adaptive template changes */ - if (!at_determined && j >= s->mx) { - c[0] += !(((line_h2 >> 6) ^ line_h1) & 0x100); - for (t = 3; t <= s->mx; t++) - c[t] += !(((line_h1 >> t) ^ line_h1) & 0x100); - ++c_all; - } - - } while (++j & 1 && j < hx); - } while (j & 7 && j < hx); - hp++; - } while (j & 15 && j < hx); + /* statistics for adaptive template changes */ + if (!at_determined && j >= s->mx) { + c[0] += !(((line_h2 >> 6) ^ line_h1) & 0x100); + for (t = 3; t <= s->mx; t++) + c[t] += !(((line_h1 >> t) ^ line_h1) & 0x100); + ++c_all; + } + + } while (++j & 1 && j < hx); + } while (j & 7 && j < hx); + hp++; + } while (j & 15 && j < hx); } /* for (j = ...) */ /* low resolution pixels are used twice */ if ((i & 1) == 0) { - lp1 -= lbpl; - lp2 -= lbpl; + lp1 -= lbpl; + lp2 -= lbpl; } } /* for (i = ...) */ @@ -1463,7 +1463,7 @@ static void encode_sde(struct jbg_enc_state *s, jbg_buf_remove_zeros(s->sde[stripe][layer][plane]); jbg_buf_write(MARKER_ESC, s->sde[stripe][layer][plane]); jbg_buf_write((s->options & JBG_SDRST) ? MARKER_SDRST : MARKER_SDNORM, - s->sde[stripe][layer][plane]); + s->sde[stripe][layer][plane]); if (s->options & JBG_SDRST) s->tx[plane] = 0; @@ -1499,8 +1499,8 @@ static void encode_sde(struct jbg_enc_state *s, #if 0 if (stripe == s->stripes - 1) fprintf(stderr, "tp_lines = %ld, tp_exceptions = %ld, tp_pixels = %ld, " - "dp_pixels = %ld, encoded_pixels = %ld\n", - tp_lines, tp_exceptions, tp_pixels, dp_pixels, encoded_pixels); + "dp_pixels = %ld, encoded_pixels = %ld\n", + tp_lines, tp_exceptions, tp_pixels, dp_pixels, encoded_pixels); #endif return; @@ -1511,7 +1511,7 @@ static void encode_sde(struct jbg_enc_state *s, * Create the next lower resolution version of an image */ static void resolution_reduction(struct jbg_enc_state *s, int plane, - int higher_layer) + int higher_layer) { unsigned long hl, ll, hx, hy, lx, ly, hbpl, lbpl; unsigned char *hp1, *hp2, *hp3, *lp; @@ -1542,7 +1542,7 @@ static void resolution_reduction(struct jbg_enc_state *s, int plane, #ifdef DEBUG fprintf(stderr, "resolution_reduction: plane = %d, higher_layer = %d\n", - plane, higher_layer); + plane, higher_layer); #endif /* @@ -1566,34 +1566,34 @@ static void resolution_reduction(struct jbg_enc_state *s, int plane, for (y = 0; y < ly;) { for (i = 0; i < ll && y < ly; i++, y++) { if (2*y + 1 >= hy) - hp1 = hp2; + hp1 = hp2; pix = 0; line_h1 = line_h2 = line_h3 = line_l2 = 0; for (j = 0; j < lbpl * 8; j += 8) { - *lp = 0; - if (i > 0 || (y > 0 && !(s->options & JBG_SDRST))) - line_l2 |= *(lp-lbpl); - for (k = 0; k < 8 && j + k < lx; k += 4) { - if (((j + k) >> 2) < hbpl) { - if (i > 0 || (y > 0 && !(s->options & JBG_SDRST))) - line_h3 |= *hp3; - ++hp3; - line_h2 |= *(hp2++); - line_h1 |= *(hp1++); - } - for (l = 0; l < 4 && j + k + l < lx; l++) { - line_h3 <<= 2; - line_h2 <<= 2; - line_h1 <<= 2; - line_l2 <<= 1; - pix = s->res_tab[((line_h1 >> 8) & 0x007) | - ((line_h2 >> 5) & 0x038) | - ((line_h3 >> 2) & 0x1c0) | - (pix << 9) | ((line_l2 << 2) & 0xc00)]; - *lp = (*lp << 1) | pix; - } - } - ++lp; + *lp = 0; + if (i > 0 || (y > 0 && !(s->options & JBG_SDRST))) + line_l2 |= *(lp-lbpl); + for (k = 0; k < 8 && j + k < lx; k += 4) { + if (((j + k) >> 2) < hbpl) { + if (i > 0 || (y > 0 && !(s->options & JBG_SDRST))) + line_h3 |= *hp3; + ++hp3; + line_h2 |= *(hp2++); + line_h1 |= *(hp1++); + } + for (l = 0; l < 4 && j + k + l < lx; l++) { + line_h3 <<= 2; + line_h2 <<= 2; + line_h1 <<= 2; + line_l2 <<= 1; + pix = s->res_tab[((line_h1 >> 8) & 0x007) | + ((line_h2 >> 5) & 0x038) | + ((line_h3 >> 2) & 0x1c0) | + (pix << 9) | ((line_l2 << 2) & 0xc00)]; + *lp = (*lp << 1) | pix; + } + } + ++lp; } *(lp - 1) <<= lbpl * 8 - lx; hp1 += hbpl; @@ -1635,7 +1635,7 @@ static void resolution_reduction(struct jbg_enc_state *s, int plane, * of doing it, but it minimizes the amount of temporary memory used. */ static void output_sde(struct jbg_enc_state *s, - unsigned long stripe, int layer, int plane) + unsigned long stripe, int layer, int plane) { int lfcl; /* lowest fully coded layer */ long i; @@ -1646,7 +1646,7 @@ static void output_sde(struct jbg_enc_state *s, if (s->sde[stripe][layer][plane] != SDE_TODO) { #ifdef DEBUG fprintf(stderr, "writing SDE: s/d/p = %2lu/%2d/%2d\n", - stripe, layer, plane); + stripe, layer, plane); #endif jbg_buf_output(&s->sde[stripe][layer][plane], s->data_out, s->file); s->sde[stripe][layer][plane] = SDE_DONE; @@ -1822,7 +1822,7 @@ void jbg_enc_out(struct jbg_enc_state *s) bpl = jbg_ceil_half(s->xd, 3); /* bytes per line */ for (plane = 0; plane < s->planes; plane++) for (y = 0; y < s->yd; y++) - s->lhp[0][plane][y * bpl + bpl - 1] &= ~((1 << (8 - (s->xd & 7))) - 1); + s->lhp[0][plane][y * bpl + bpl - 1] &= ~((1 << (8 - (s->xd & 7))) - 1); } /* prepare BIH */ @@ -1864,12 +1864,12 @@ void jbg_enc_out(struct jbg_enc_state *s) checked_malloc(s->stripes, sizeof(struct jbg_buf ***)); for (stripe = 0; stripe < s->stripes; stripe++) { s->sde[stripe] = (struct jbg_buf ***) - checked_malloc(s->d + 1, sizeof(struct jbg_buf **)); + checked_malloc(s->d + 1, sizeof(struct jbg_buf **)); for (layer = 0; layer < s->d + 1; layer++) { - s->sde[stripe][layer] = (struct jbg_buf **) - checked_malloc(s->planes, sizeof(struct jbg_buf *)); - for (plane = 0; plane < s->planes; plane++) - s->sde[stripe][layer][plane] = SDE_TODO; + s->sde[stripe][layer] = (struct jbg_buf **) + checked_malloc(s->planes, sizeof(struct jbg_buf *)); + for (plane = 0; plane < s->planes; plane++) + s->sde[stripe][layer][plane] = SDE_TODO; } } } @@ -1892,9 +1892,9 @@ void jbg_enc_out(struct jbg_enc_state *s) for (layer = s->dh; layer >= s->dl; layer--) { for (plane = 0; plane < s->planes; plane++) { if (layer > 0) - resolution_reduction(s, plane, layer); + resolution_reduction(s, plane, layer); for (stripe = 0; stripe < s->stripes; stripe++) - encode_sde(s, stripe, layer, plane); + encode_sde(s, stripe, layer, plane); s->highres[plane] ^= 1; } } @@ -1917,55 +1917,55 @@ void jbg_enc_out(struct jbg_enc_state *s) for (ii[1] = is[1]; ii[1] <= ie[1]; ii[1]++) for (ii[2] = is[2]; ii[2] <= ie[2]; ii[2]++) { - stripe = ii[iindex[order][STRIPE]]; - if (s->order & JBG_HITOLO) - layer = s->dh - (ii[iindex[order][LAYER]] - s->dl); - else - layer = ii[iindex[order][LAYER]]; - plane = ii[iindex[order][PLANE]]; - - /* output comment marker segment if there is any pending */ - if (s->comment) { - buf[0] = MARKER_ESC; - buf[1] = MARKER_COMMENT; - buf[2] = s->comment_len >> 24; - buf[3] = (s->comment_len >> 16) & 0xff; - buf[4] = (s->comment_len >> 8) & 0xff; - buf[5] = s->comment_len & 0xff; - s->data_out(buf, 6, s->file); - s->data_out(s->comment, s->comment_len, s->file); - s->comment = NULL; - } - - output_sde(s, stripe, layer, plane); - - /* - * When we generate a NEWLEN test case (s->yd1 > s->yd), output - * NEWLEN after last stripe if we have only a single - * resolution layer or plane (see ITU-T T.85 profile), otherwise - * output NEWLEN before last stripe. - */ - if (s->yd1 > s->yd && - (stripe == s->stripes - 1 || - (stripe == s->stripes - 2 && - (s->dl != s->dh || s->planes > 1)))) { - s->yd1 = s->yd; - yd = jbg_ceil_half(s->yd, s->d - s->dh); - buf[0] = MARKER_ESC; - buf[1] = MARKER_NEWLEN; - buf[2] = yd >> 24; - buf[3] = (yd >> 16) & 0xff; - buf[4] = (yd >> 8) & 0xff; - buf[5] = yd & 0xff; - s->data_out(buf, 6, s->file); + stripe = ii[iindex[order][STRIPE]]; + if (s->order & JBG_HITOLO) + layer = s->dh - (ii[iindex[order][LAYER]] - s->dl); + else + layer = ii[iindex[order][LAYER]]; + plane = ii[iindex[order][PLANE]]; + + /* output comment marker segment if there is any pending */ + if (s->comment) { + buf[0] = MARKER_ESC; + buf[1] = MARKER_COMMENT; + buf[2] = s->comment_len >> 24; + buf[3] = (s->comment_len >> 16) & 0xff; + buf[4] = (s->comment_len >> 8) & 0xff; + buf[5] = s->comment_len & 0xff; + s->data_out(buf, 6, s->file); + s->data_out(s->comment, s->comment_len, s->file); + s->comment = NULL; + } + + output_sde(s, stripe, layer, plane); + + /* + * When we generate a NEWLEN test case (s->yd1 > s->yd), output + * NEWLEN after last stripe if we have only a single + * resolution layer or plane (see ITU-T T.85 profile), otherwise + * output NEWLEN before last stripe. + */ + if (s->yd1 > s->yd && + (stripe == s->stripes - 1 || + (stripe == s->stripes - 2 && + (s->dl != s->dh || s->planes > 1)))) { + s->yd1 = s->yd; + yd = jbg_ceil_half(s->yd, s->d - s->dh); + buf[0] = MARKER_ESC; + buf[1] = MARKER_NEWLEN; + buf[2] = yd >> 24; + buf[3] = (yd >> 16) & 0xff; + buf[4] = (yd >> 8) & 0xff; + buf[5] = yd & 0xff; + s->data_out(buf, 6, s->file); #ifdef DEBUG - fprintf(stderr, "NEWLEN: yd=%lu\n", yd); + fprintf(stderr, "NEWLEN: yd=%lu\n", yd); #endif - if (stripe == s->stripes - 1) { - buf[1] = MARKER_SDNORM; - s->data_out(buf, 2, s->file); - } - } + if (stripe == s->stripes - 1) { + buf[1] = MARKER_SDNORM; + s->data_out(buf, 2, s->file); + } + } } @@ -1986,11 +1986,11 @@ void jbg_enc_free(struct jbg_enc_state *s) if (s->sde) { for (stripe = 0; stripe < s->stripes; stripe++) { for (layer = 0; layer < s->d + 1; layer++) { - for (plane = 0; plane < s->planes; plane++) - if (s->sde[stripe][layer][plane] != SDE_DONE && - s->sde[stripe][layer][plane] != SDE_TODO) - jbg_buf_free(&s->sde[stripe][layer][plane]); - checked_free(s->sde[stripe][layer]); + for (plane = 0; plane < s->planes; plane++) + if (s->sde[stripe][layer][plane] != SDE_DONE && + s->sde[stripe][layer][plane] != SDE_TODO) + jbg_buf_free(&s->sde[stripe][layer][plane]); + checked_free(s->sde[stripe][layer]); } checked_free(s->sde[stripe]); } @@ -2063,7 +2063,7 @@ void jbg_dec_init(struct jbg_dec_state *s) * ymax. */ void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax, - unsigned long ymax) + unsigned long ymax) { if (xmax > 0) s->xmax = xmax; if (ymax > 0) s->ymax = ymax; @@ -2080,7 +2080,7 @@ void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax, * this code cannot determine whether we have a marker segment). */ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, - size_t len) + size_t len) { unsigned long stripe; unsigned int layer, plane; @@ -2139,7 +2139,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, #ifdef DEBUG if (s->x == 0 && s->i == 0 && s->pseudo) fprintf(stderr, "decode_pscd(%p, %p, %ld): s/d/p = %2lu/%2u/%2u\n", - (void *) s, (void *) data, (long) len, stripe, layer, plane); + (void *) s, (void *) data, (long) len, stripe, layer, plane); #endif if (s->x == 0 && s->i == 0 && @@ -2158,38 +2158,38 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, /* adaptive template changes */ if (x == 0 && s->pseudo) - for (n = 0; n < s->at_moves; n++) - if (s->at_line[n] == s->i) { - s->tx[plane][layer - s->dl] = s->at_tx[n]; - s->ty[plane][layer - s->dl] = s->at_ty[n]; + for (n = 0; n < s->at_moves; n++) + if (s->at_line[n] == s->i) { + s->tx[plane][layer - s->dl] = s->at_tx[n]; + s->ty[plane][layer - s->dl] = s->at_ty[n]; #ifdef DEBUG - fprintf(stderr, "ATMOVE: line=%lu, tx=%d, ty=%d.\n", s->i, - s->tx[plane][layer - s->dl], s->ty[plane][layer - s->dl]); + fprintf(stderr, "ATMOVE: line=%lu, tx=%d, ty=%d.\n", s->i, + s->tx[plane][layer - s->dl], s->ty[plane][layer - s->dl]); #endif - } + } tx = s->tx[plane][layer - s->dl]; assert(tx >= 0); /* i.e., tx can safely be cast to unsigned */ /* typical prediction */ if (s->options & JBG_TPBON && s->pseudo) { - slntp = arith_decode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX); - if (slntp < 0) - goto leave; - s->lntp[plane][layer - s->dl] = - !(slntp ^ s->lntp[plane][layer - s->dl]); - if (!s->lntp[plane][layer - s->dl]) { - /* this line is 'typical' (i.e. identical to the previous one) */ - p1 = hp; - if (s->i == 0 && (stripe == 0 || s->reset[plane][layer - s->dl])) - while (p1 < hp + hbpl) *p1++ = 0; - else { - q1 = hp - hbpl; - while (q1 < hp) *p1++ = *q1++; - } - hp += hbpl; - continue; - } - /* this line is 'not typical' and has to be coded completely */ + slntp = arith_decode(se, (s->options & JBG_LRLTWO) ? TPB2CX : TPB3CX); + if (slntp < 0) + goto leave; + s->lntp[plane][layer - s->dl] = + !(slntp ^ s->lntp[plane][layer - s->dl]); + if (!s->lntp[plane][layer - s->dl]) { + /* this line is 'typical' (i.e. identical to the previous one) */ + p1 = hp; + if (s->i == 0 && (stripe == 0 || s->reset[plane][layer - s->dl])) + while (p1 < hp + hbpl) *p1++ = 0; + else { + q1 = hp - hbpl; + while (q1 < hp) *p1++ = *q1++; + } + hp += hbpl; + continue; + } + /* this line is 'not typical' and has to be coded completely */ } s->pseudo = 0; @@ -2203,11 +2203,11 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, */ if (x == 0) { - line_h1 = line_h2 = line_h3 = 0; - if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) - line_h2 = (long)*(hp - hbpl) << 8; - if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) - line_h3 = (long)*(hp - hbpl - hbpl) << 8; + line_h1 = line_h2 = line_h3 = 0; + if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) + line_h2 = (long)*(hp - hbpl) << 8; + if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) + line_h3 = (long)*(hp - hbpl - hbpl) << 8; } /* @@ -2256,70 +2256,70 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, /* decode line */ while (x < hx) { - if ((x & 7) == 0) { - if (x < hbpl * 8 - 8 && - (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))) { - line_h2 |= *(hp - hbpl + 1); - if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) - line_h3 |= *(hp - hbpl - hbpl + 1); - } - } - if (s->options & JBG_LRLTWO) { - /* two line template */ - do { - if (tx) { - if ((unsigned) tx > x) - a = 0; - else if (tx < 8) - a = ((line_h1 >> (tx - 5)) & 0x010); - else { - o = (x - tx) - (x & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - a <<= 4; - } - assert(tx > 31 || - a == ((line_h1 >> (tx - 5)) & 0x010)); - pix = arith_decode(se, (((line_h2 >> 9) & 0x3e0) | a | - (line_h1 & 0x00f))); - } else - pix = arith_decode(se, (((line_h2 >> 9) & 0x3f0) | - (line_h1 & 0x00f))); - if (pix < 0) - goto leave; - line_h1 = (line_h1 << 1) | pix; - line_h2 <<= 1; - } while ((++x & 7) && x < hx); - } else { - /* three line template */ - do { - if (tx) { - if ((unsigned) tx > x) - a = 0; - else if (tx < 8) - a = ((line_h1 >> (tx - 3)) & 0x004); - else { - o = (x - tx) - (x & ~7L); - a = (hp[o >> 3] >> (7 - (o & 7))) & 1; - a <<= 2; - } - assert(tx > 31 || - a == ((line_h1 >> (tx - 3)) & 0x004)); - pix = arith_decode(se, (((line_h3 >> 7) & 0x380) | - ((line_h2 >> 11) & 0x078) | a | - (line_h1 & 0x003))); - } else - pix = arith_decode(se, (((line_h3 >> 7) & 0x380) | - ((line_h2 >> 11) & 0x07c) | - (line_h1 & 0x003))); - if (pix < 0) - goto leave; - - line_h1 = (line_h1 << 1) | pix; - line_h2 <<= 1; - line_h3 <<= 1; - } while ((++x & 7) && x < hx); - } /* if (s->options & JBG_LRLTWO) */ - *hp++ = line_h1; + if ((x & 7) == 0) { + if (x < hbpl * 8 - 8 && + (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl]))) { + line_h2 |= *(hp - hbpl + 1); + if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) + line_h3 |= *(hp - hbpl - hbpl + 1); + } + } + if (s->options & JBG_LRLTWO) { + /* two line template */ + do { + if (tx) { + if ((unsigned) tx > x) + a = 0; + else if (tx < 8) + a = ((line_h1 >> (tx - 5)) & 0x010); + else { + o = (x - tx) - (x & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + a <<= 4; + } + assert(tx > 31 || + a == ((line_h1 >> (tx - 5)) & 0x010)); + pix = arith_decode(se, (((line_h2 >> 9) & 0x3e0) | a | + (line_h1 & 0x00f))); + } else + pix = arith_decode(se, (((line_h2 >> 9) & 0x3f0) | + (line_h1 & 0x00f))); + if (pix < 0) + goto leave; + line_h1 = (line_h1 << 1) | pix; + line_h2 <<= 1; + } while ((++x & 7) && x < hx); + } else { + /* three line template */ + do { + if (tx) { + if ((unsigned) tx > x) + a = 0; + else if (tx < 8) + a = ((line_h1 >> (tx - 3)) & 0x004); + else { + o = (x - tx) - (x & ~7L); + a = (hp[o >> 3] >> (7 - (o & 7))) & 1; + a <<= 2; + } + assert(tx > 31 || + a == ((line_h1 >> (tx - 3)) & 0x004)); + pix = arith_decode(se, (((line_h3 >> 7) & 0x380) | + ((line_h2 >> 11) & 0x078) | a | + (line_h1 & 0x003))); + } else + pix = arith_decode(se, (((line_h3 >> 7) & 0x380) | + ((line_h2 >> 11) & 0x07c) | + (line_h1 & 0x003))); + if (pix < 0) + goto leave; + + line_h1 = (line_h1 << 1) | pix; + line_h2 <<= 1; + line_h3 <<= 1; + } while ((++x & 7) && x < hx); + } /* if (s->options & JBG_LRLTWO) */ + *hp++ = line_h1; } /* while */ *(hp - 1) <<= hbpl * 8 - hx; x = 0; @@ -2336,25 +2336,25 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, /* adaptive template changes */ if (x == 0) - for (n = 0; n < s->at_moves; n++) - if (s->at_line[n] == s->i) { - s->tx[plane][layer - s->dl] = s->at_tx[n]; - s->ty[plane][layer - s->dl] = s->at_ty[n]; + for (n = 0; n < s->at_moves; n++) + if (s->at_line[n] == s->i) { + s->tx[plane][layer - s->dl] = s->at_tx[n]; + s->ty[plane][layer - s->dl] = s->at_ty[n]; #ifdef DEBUG - fprintf(stderr, "ATMOVE: line=%lu, tx=%d, ty=%d.\n", s->i, - s->tx[plane][layer - s->dl], s->ty[plane][layer - s->dl]); + fprintf(stderr, "ATMOVE: line=%lu, tx=%d, ty=%d.\n", s->i, + s->tx[plane][layer - s->dl], s->ty[plane][layer - s->dl]); #endif - } + } tx = s->tx[plane][layer - s->dl]; /* handle lower border of low-resolution image */ if ((s->i >> 1) >= ll - 1 || (y >> 1) >= ly - 1) - lp1 = lp2; + lp1 = lp2; /* typical prediction */ if ((s->options & JBG_TPDON) && s->pseudo) { - if ((s->lntp[plane][layer - s->dl] = arith_decode(se, TPDCX)) < 0) - goto leave; + if ((s->lntp[plane][layer - s->dl] = arith_decode(se, TPDCX)) < 0) + goto leave; } s->pseudo = 0; @@ -2379,135 +2379,135 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, if (x == 0) { - line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0; - if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) { - line_h2 = (long)*(hp - hbpl) << 8; - if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) - line_h3 = (long)*(hp - hbpl - hbpl) << 8; - } - if (s->i > 1 || (y > 1 && !s->reset[plane][layer-s->dl])) - line_l3 = (long)*(lp2 - lbpl) << 8; - line_l2 = (long)*lp2 << 8; - line_l1 = (long)*lp1 << 8; + line_h1 = line_h2 = line_h3 = line_l1 = line_l2 = line_l3 = 0; + if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) { + line_h2 = (long)*(hp - hbpl) << 8; + if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) + line_h3 = (long)*(hp - hbpl - hbpl) << 8; + } + if (s->i > 1 || (y > 1 && !s->reset[plane][layer-s->dl])) + line_l3 = (long)*(lp2 - lbpl) << 8; + line_l2 = (long)*lp2 << 8; + line_l1 = (long)*lp1 << 8; } /* decode line */ while (x < hx) { - if ((x & 15) == 0) - if ((x >> 1) < lbpl * 8 - 8) { - line_l1 |= *(lp1 + 1); - line_l2 |= *(lp2 + 1); - if (s->i > 1 || - (y > 1 && !s->reset[plane][layer - s->dl])) - line_l3 |= *(lp2 - lbpl + 1); - } - do { - - assert(hp - (s->lhp[ layer &1][plane] + (stripe * hl + s->i) - * hbpl) == (ptrdiff_t) x >> 3); - assert(lp2 - (s->lhp[(layer-1) &1][plane] + (stripe * ll + (s->i>>1)) - * lbpl) == (ptrdiff_t) x >> 4); - - if ((x & 7) == 0) - if (x < hbpl * 8 - 8) { - if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) { - line_h2 |= *(hp + 1 - hbpl); - if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) - line_h3 |= *(hp + 1 - hbpl - hbpl); - } - } - do { - if (!s->lntp[plane][layer - s->dl]) + if ((x & 15) == 0) + if ((x >> 1) < lbpl * 8 - 8) { + line_l1 |= *(lp1 + 1); + line_l2 |= *(lp2 + 1); + if (s->i > 1 || + (y > 1 && !s->reset[plane][layer - s->dl])) + line_l3 |= *(lp2 - lbpl + 1); + } + do { + + assert(hp - (s->lhp[ layer &1][plane] + (stripe * hl + s->i) + * hbpl) == (ptrdiff_t) x >> 3); + assert(lp2 - (s->lhp[(layer-1) &1][plane] + (stripe * ll + (s->i>>1)) + * lbpl) == (ptrdiff_t) x >> 4); + + if ((x & 7) == 0) + if (x < hbpl * 8 - 8) { + if (s->i > 0 || (y > 0 && !s->reset[plane][layer - s->dl])) { + line_h2 |= *(hp + 1 - hbpl); + if (s->i > 1 || (y > 1 && !s->reset[plane][layer - s->dl])) + line_h3 |= *(hp + 1 - hbpl - hbpl); + } + } + do { + if (!s->lntp[plane][layer - s->dl]) cx = (((line_l3 >> 14) & 0x007) | ((line_l2 >> 11) & 0x038) | ((line_l1 >> 8) & 0x1c0)); - if (!s->lntp[plane][layer - s->dl] && - (cx == 0x000 || cx == 0x1ff)) { - /* pixels are typical and have not to be decoded */ - do { - line_h1 = (line_h1 << 1) | (cx & 1); - } while ((++x & 1) && x < hx); - line_h2 <<= 2; line_h3 <<= 2; - } else - do { - - /* deterministic prediction */ - if (s->options & JBG_DPON) - if ((y & 1) == 0) - if ((x & 1) == 0) - /* phase 0 */ - pix = s->dppriv[((line_l3 >> 15) & 0x003) | - ((line_l2 >> 13) & 0x00c) | - ((line_h1 << 4) & 0x010) | - ((line_h2 >> 9) & 0x0e0)]; - else - /* phase 1 */ - pix = s->dppriv[(((line_l3 >> 15) & 0x003) | - ((line_l2 >> 13) & 0x00c) | - ((line_h1 << 4) & 0x030) | - ((line_h2 >> 9) & 0x1c0)) + 256]; - else - if ((x & 1) == 0) - /* phase 2 */ - pix = s->dppriv[(((line_l3 >> 15) & 0x003) | - ((line_l2 >> 13) & 0x00c) | - ((line_h1 << 4) & 0x010) | - ((line_h2 >> 9) & 0x0e0) | - ((line_h3 >> 6) & 0x700)) + 768]; - else - /* phase 3 */ - pix = s->dppriv[(((line_l3 >> 15) & 0x003) | - ((line_l2 >> 13) & 0x00c) | - ((line_h1 << 4) & 0x030) | - ((line_h2 >> 9) & 0x1c0) | - ((line_h3 >> 6) & 0xe00)) + 2816]; - else - pix = 2; - - if (pix & 2) { - if (tx) - cx = ((line_h1 & 0x003) | - (((line_h1 << 2) >> (tx - 3)) & 0x010) | - ((line_h2 >> 12) & 0x00c) | - ((line_h3 >> 10) & 0x020)); - else - cx = ((line_h1 & 0x003) | - ((line_h2 >> 12) & 0x01c) | - ((line_h3 >> 10) & 0x020)); - if (x & 1) - cx |= (((line_l2 >> 8) & 0x0c0) | - ((line_l1 >> 6) & 0x300)) | (1UL << 10); - else - cx |= (((line_l2 >> 9) & 0x0c0) | - ((line_l1 >> 7) & 0x300)); - cx |= (y & 1) << 11; - - pix = arith_decode(se, cx); - if (pix < 0) - goto leave; - } - - line_h1 = (line_h1 << 1) | pix; - line_h2 <<= 1; - line_h3 <<= 1; - - } while ((++x & 1) && x < hx); - line_l1 <<= 1; line_l2 <<= 1; line_l3 <<= 1; - } while ((x & 7) && x < hx); - *hp++ = line_h1; - } while ((x & 15) && x < hx); - ++lp1; - ++lp2; + if (!s->lntp[plane][layer - s->dl] && + (cx == 0x000 || cx == 0x1ff)) { + /* pixels are typical and have not to be decoded */ + do { + line_h1 = (line_h1 << 1) | (cx & 1); + } while ((++x & 1) && x < hx); + line_h2 <<= 2; line_h3 <<= 2; + } else + do { + + /* deterministic prediction */ + if (s->options & JBG_DPON) + if ((y & 1) == 0) + if ((x & 1) == 0) + /* phase 0 */ + pix = s->dppriv[((line_l3 >> 15) & 0x003) | + ((line_l2 >> 13) & 0x00c) | + ((line_h1 << 4) & 0x010) | + ((line_h2 >> 9) & 0x0e0)]; + else + /* phase 1 */ + pix = s->dppriv[(((line_l3 >> 15) & 0x003) | + ((line_l2 >> 13) & 0x00c) | + ((line_h1 << 4) & 0x030) | + ((line_h2 >> 9) & 0x1c0)) + 256]; + else + if ((x & 1) == 0) + /* phase 2 */ + pix = s->dppriv[(((line_l3 >> 15) & 0x003) | + ((line_l2 >> 13) & 0x00c) | + ((line_h1 << 4) & 0x010) | + ((line_h2 >> 9) & 0x0e0) | + ((line_h3 >> 6) & 0x700)) + 768]; + else + /* phase 3 */ + pix = s->dppriv[(((line_l3 >> 15) & 0x003) | + ((line_l2 >> 13) & 0x00c) | + ((line_h1 << 4) & 0x030) | + ((line_h2 >> 9) & 0x1c0) | + ((line_h3 >> 6) & 0xe00)) + 2816]; + else + pix = 2; + + if (pix & 2) { + if (tx) + cx = ((line_h1 & 0x003) | + (((line_h1 << 2) >> (tx - 3)) & 0x010) | + ((line_h2 >> 12) & 0x00c) | + ((line_h3 >> 10) & 0x020)); + else + cx = ((line_h1 & 0x003) | + ((line_h2 >> 12) & 0x01c) | + ((line_h3 >> 10) & 0x020)); + if (x & 1) + cx |= (((line_l2 >> 8) & 0x0c0) | + ((line_l1 >> 6) & 0x300)) | (1UL << 10); + else + cx |= (((line_l2 >> 9) & 0x0c0) | + ((line_l1 >> 7) & 0x300)); + cx |= (y & 1) << 11; + + pix = arith_decode(se, cx); + if (pix < 0) + goto leave; + } + + line_h1 = (line_h1 << 1) | pix; + line_h2 <<= 1; + line_h3 <<= 1; + + } while ((++x & 1) && x < hx); + line_l1 <<= 1; line_l2 <<= 1; line_l3 <<= 1; + } while ((x & 7) && x < hx); + *hp++ = line_h1; + } while ((x & 15) && x < hx); + ++lp1; + ++lp2; } /* while */ x = 0; *(hp - 1) <<= hbpl * 8 - hx; if ((s->i & 1) == 0) { - /* low resolution pixels are used twice */ - lp1 -= lbpl; - lp2 -= lbpl; + /* low resolution pixels are used twice */ + lp1 -= lbpl; + lp2 -= lbpl; } else - s->pseudo = 1; + s->pseudo = 1; } /* for (i = ...) */ @@ -2563,7 +2563,7 @@ static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data, * information by identifying which test exactly has failed.) */ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, - size_t *cnt) + size_t *cnt) { int i, j, required_length; unsigned long x, y; @@ -2595,18 +2595,18 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, s->planes = s->buffer[2]; else if (s->planes != s->buffer[2]) - return JBG_ENOCONT | 2; + return JBG_ENOCONT | 2; x = (((long) s->buffer[ 4] << 24) | ((long) s->buffer[ 5] << 16) | - ((long) s->buffer[ 6] << 8) | (long) s->buffer[ 7]); + ((long) s->buffer[ 6] << 8) | (long) s->buffer[ 7]); y = (((long) s->buffer[ 8] << 24) | ((long) s->buffer[ 9] << 16) | - ((long) s->buffer[10] << 8) | (long) s->buffer[11]); + ((long) s->buffer[10] << 8) | (long) s->buffer[11]); if (s->dl != 0 && ((s->xd << (s->d - s->dl + 1)) != x && - (s->yd << (s->d - s->dl + 1)) != y)) + (s->yd << (s->d - s->dl + 1)) != y)) return JBG_ENOCONT | 3; s->xd = x; s->yd = y; s->l0 = (((long) s->buffer[12] << 24) | ((long) s->buffer[13] << 16) | - ((long) s->buffer[14] << 8) | (long) s->buffer[15]); + ((long) s->buffer[14] << 8) | (long) s->buffer[15]); /* ITU-T T.85 trick not directly implemented by decoder; for full * T.85 compatibility with respect to all NEWLEN marker scenarios, * preprocess BIE with jbg_newlen() before passing it to the decoder, @@ -2647,51 +2647,51 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, s->ii[iindex[s->order & 7][PLANE]] = 0; if (s->dl == 0) { s->s = (struct jbg_ardec_state **) - checked_malloc(s->planes, sizeof(struct jbg_ardec_state *)); + checked_malloc(s->planes, sizeof(struct jbg_ardec_state *)); s->tx = (int **) checked_malloc(s->planes, sizeof(int *)); s->ty = (int **) checked_malloc(s->planes, sizeof(int *)); s->reset = (int **) checked_malloc(s->planes, sizeof(int *)); s->lntp = (int **) checked_malloc(s->planes, sizeof(int *)); s->lhp[0] = (unsigned char **) - checked_malloc(s->planes, sizeof(unsigned char *)); + checked_malloc(s->planes, sizeof(unsigned char *)); s->lhp[1] = (unsigned char **) - checked_malloc(s->planes, sizeof(unsigned char *)); + checked_malloc(s->planes, sizeof(unsigned char *)); for (i = 0; i < s->planes; i++) { - s->s[i] = (struct jbg_ardec_state *) - checked_malloc(s->d - s->dl + 1, sizeof(struct jbg_ardec_state)); - s->tx[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); - s->ty[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); - s->reset[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); - s->lntp[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); - s->lhp[ s->d & 1][i] = (unsigned char *) - checked_malloc(s->yd, jbg_ceil_half(s->xd, 3)); - s->lhp[(s->d-1) & 1][i] = (unsigned char *) - checked_malloc(jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3)); + s->s[i] = (struct jbg_ardec_state *) + checked_malloc(s->d - s->dl + 1, sizeof(struct jbg_ardec_state)); + s->tx[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); + s->ty[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); + s->reset[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); + s->lntp[i] = (int *) checked_malloc(s->d - s->dl + 1, sizeof(int)); + s->lhp[ s->d & 1][i] = (unsigned char *) + checked_malloc(s->yd, jbg_ceil_half(s->xd, 3)); + s->lhp[(s->d-1) & 1][i] = (unsigned char *) + checked_malloc(jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3)); } } else { for (i = 0; i < s->planes; i++) { - s->s[i] = (struct jbg_ardec_state *) - checked_realloc(s->s[i], s->d - s->dl + 1, - sizeof(struct jbg_ardec_state)); - s->tx[i] = (int *) checked_realloc(s->tx[i], - s->d - s->dl + 1, sizeof(int)); - s->ty[i] = (int *) checked_realloc(s->ty[i], - s->d - s->dl + 1, sizeof(int)); - s->reset[i] = (int *) checked_realloc(s->reset[i], - s->d - s->dl + 1, sizeof(int)); - s->lntp[i] = (int *) checked_realloc(s->lntp[i], - s->d - s->dl + 1, sizeof(int)); - s->lhp[ s->d & 1][i] = (unsigned char *) - checked_realloc(s->lhp[ s->d & 1][i], - s->yd, jbg_ceil_half(s->xd, 3)); - s->lhp[(s->d-1) & 1][i] = (unsigned char *) - checked_realloc(s->lhp[(s->d-1) & 1][i], - jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3)); + s->s[i] = (struct jbg_ardec_state *) + checked_realloc(s->s[i], s->d - s->dl + 1, + sizeof(struct jbg_ardec_state)); + s->tx[i] = (int *) checked_realloc(s->tx[i], + s->d - s->dl + 1, sizeof(int)); + s->ty[i] = (int *) checked_realloc(s->ty[i], + s->d - s->dl + 1, sizeof(int)); + s->reset[i] = (int *) checked_realloc(s->reset[i], + s->d - s->dl + 1, sizeof(int)); + s->lntp[i] = (int *) checked_realloc(s->lntp[i], + s->d - s->dl + 1, sizeof(int)); + s->lhp[ s->d & 1][i] = (unsigned char *) + checked_realloc(s->lhp[ s->d & 1][i], + s->yd, jbg_ceil_half(s->xd, 3)); + s->lhp[(s->d-1) & 1][i] = (unsigned char *) + checked_realloc(s->lhp[(s->d-1) & 1][i], + jbg_ceil_half(s->yd, 1), jbg_ceil_half(s->xd, 1+3)); } } for (i = 0; i < s->planes; i++) for (j = 0; j <= s->d - s->dl; j++) - arith_decode_init(s->s[i] + j, 0); + arith_decode_init(s->s[i] + j, 0); if (s->dl == 0 || (s->options & JBG_DPON && !(s->options & JBG_DPPRIV))) s->dppriv = jbg_dptable; s->comment_skip = 0; @@ -2730,11 +2730,11 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, /* skip COMMENT contents */ if (s->comment_skip) { if (s->comment_skip <= len - *cnt) { - *cnt += s->comment_skip; - s->comment_skip = 0; + *cnt += s->comment_skip; + s->comment_skip = 0; } else { - s->comment_skip -= len - *cnt; - *cnt = len; + s->comment_skip -= len - *cnt; + *cnt = len; } continue; } @@ -2743,7 +2743,7 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, if (s->buf_len > 0) { assert(s->buffer[0] == MARKER_ESC); while (s->buf_len < 2 && *cnt < len) - s->buffer[s->buf_len++] = data[(*cnt)++]; + s->buffer[s->buf_len++] = data[(*cnt)++]; if (s->buf_len < 2) continue; switch (s->buffer[1]) { case MARKER_COMMENT: required_length = 6; break; @@ -2753,120 +2753,120 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, case MARKER_SDNORM: case MARKER_SDRST: required_length = 2; break; case MARKER_STUFF: - /* forward stuffed 0xff to arithmetic decoder */ - s->buf_len = 0; - decode_pscd(s, s->buffer, 2); - continue; + /* forward stuffed 0xff to arithmetic decoder */ + s->buf_len = 0; + decode_pscd(s, s->buffer, 2); + continue; default: - return JBG_EMARKER; + return JBG_EMARKER; } while (s->buf_len < required_length && *cnt < len) - s->buffer[s->buf_len++] = data[(*cnt)++]; + s->buffer[s->buf_len++] = data[(*cnt)++]; if (s->buf_len < required_length) continue; /* now the buffer is filled with exactly one marker segment */ switch (s->buffer[1]) { case MARKER_COMMENT: - s->comment_skip = - (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) | - ((long) s->buffer[4] << 8) | (long) s->buffer[5]); - break; + s->comment_skip = + (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) | + ((long) s->buffer[4] << 8) | (long) s->buffer[5]); + break; case MARKER_ATMOVE: - if (s->at_moves < JBG_ATMOVES_MAX) { - s->at_line[s->at_moves] = - (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) | - ((long) s->buffer[4] << 8) | (long) s->buffer[5]); - s->at_tx[s->at_moves] = (signed char) s->buffer[6]; - s->at_ty[s->at_moves] = s->buffer[7]; - if (s->at_tx[s->at_moves] < - (int) s->mx || - s->at_tx[s->at_moves] > (int) s->mx || - s->at_ty[s->at_moves] > (int) s->my || - (s->at_ty[s->at_moves] == 0 && s->at_tx[s->at_moves] < 0)) - return JBG_EINVAL | 11; - if (s->at_ty[s->at_moves] != 0) - return JBG_EIMPL | 6; - s->at_moves++; - } else - return JBG_EIMPL | 7; /* more than JBG_ATMOVES_MAX ATMOVES */ - break; + if (s->at_moves < JBG_ATMOVES_MAX) { + s->at_line[s->at_moves] = + (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) | + ((long) s->buffer[4] << 8) | (long) s->buffer[5]); + s->at_tx[s->at_moves] = (signed char) s->buffer[6]; + s->at_ty[s->at_moves] = s->buffer[7]; + if (s->at_tx[s->at_moves] < - (int) s->mx || + s->at_tx[s->at_moves] > (int) s->mx || + s->at_ty[s->at_moves] > (int) s->my || + (s->at_ty[s->at_moves] == 0 && s->at_tx[s->at_moves] < 0)) + return JBG_EINVAL | 11; + if (s->at_ty[s->at_moves] != 0) + return JBG_EIMPL | 6; + s->at_moves++; + } else + return JBG_EIMPL | 7; /* more than JBG_ATMOVES_MAX ATMOVES */ + break; case MARKER_NEWLEN: - y = (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) | - ((long) s->buffer[4] << 8) | (long) s->buffer[5]); - if (y > s->yd) return JBG_EINVAL | 12; - if (!(s->options & JBG_VLENGTH)) return JBG_EINVAL | 13; - s->yd = y; - /* calculate again number of stripes that will be required */ - s->stripes = jbg_stripes(s->l0, s->yd, s->d); - break; + y = (((long) s->buffer[2] << 24) | ((long) s->buffer[3] << 16) | + ((long) s->buffer[4] << 8) | (long) s->buffer[5]); + if (y > s->yd) return JBG_EINVAL | 12; + if (!(s->options & JBG_VLENGTH)) return JBG_EINVAL | 13; + s->yd = y; + /* calculate again number of stripes that will be required */ + s->stripes = jbg_stripes(s->l0, s->yd, s->d); + break; case MARKER_ABORT: - return JBG_EABORT; + return JBG_EABORT; case MARKER_SDNORM: case MARKER_SDRST: - /* decode final pixels based on trailing zero bytes */ - decode_pscd(s, s->buffer, 2); - - arith_decode_init(s->s[s->ii[iindex[s->order & 7][PLANE]]] + - s->ii[iindex[s->order & 7][LAYER]] - s->dl, - s->ii[iindex[s->order & 7][STRIPE]] != s->stripes - 1 - && s->buffer[1] != MARKER_SDRST); - - s->reset[s->ii[iindex[s->order & 7][PLANE]]] - [s->ii[iindex[s->order & 7][LAYER]] - s->dl] = - (s->buffer[1] == MARKER_SDRST); - - /* prepare for next SDE */ - s->x = 0; - s->i = 0; - s->pseudo = 1; - s->at_moves = 0; - - /* increment layer/stripe/plane loop variables */ - /* start and end value for each loop: */ - is[iindex[s->order & 7][STRIPE]] = 0; - ie[iindex[s->order & 7][STRIPE]] = s->stripes - 1; - is[iindex[s->order & 7][LAYER]] = s->dl; - ie[iindex[s->order & 7][LAYER]] = s->d; - is[iindex[s->order & 7][PLANE]] = 0; - ie[iindex[s->order & 7][PLANE]] = s->planes - 1; - i = 2; /* index to innermost loop */ - do { - j = 0; /* carry flag */ - if (++s->ii[i] > ie[i]) { - /* handling overflow of loop variable */ - j = 1; - if (i > 0) - s->ii[i] = is[i]; - } - } while (--i >= 0 && j); - - s->buf_len = 0; - - /* check whether this have been all SDEs */ - if (j) { + /* decode final pixels based on trailing zero bytes */ + decode_pscd(s, s->buffer, 2); + + arith_decode_init(s->s[s->ii[iindex[s->order & 7][PLANE]]] + + s->ii[iindex[s->order & 7][LAYER]] - s->dl, + s->ii[iindex[s->order & 7][STRIPE]] != s->stripes - 1 + && s->buffer[1] != MARKER_SDRST); + + s->reset[s->ii[iindex[s->order & 7][PLANE]]] + [s->ii[iindex[s->order & 7][LAYER]] - s->dl] = + (s->buffer[1] == MARKER_SDRST); + + /* prepare for next SDE */ + s->x = 0; + s->i = 0; + s->pseudo = 1; + s->at_moves = 0; + + /* increment layer/stripe/plane loop variables */ + /* start and end value for each loop: */ + is[iindex[s->order & 7][STRIPE]] = 0; + ie[iindex[s->order & 7][STRIPE]] = s->stripes - 1; + is[iindex[s->order & 7][LAYER]] = s->dl; + ie[iindex[s->order & 7][LAYER]] = s->d; + is[iindex[s->order & 7][PLANE]] = 0; + ie[iindex[s->order & 7][PLANE]] = s->planes - 1; + i = 2; /* index to innermost loop */ + do { + j = 0; /* carry flag */ + if (++s->ii[i] > ie[i]) { + /* handling overflow of loop variable */ + j = 1; + if (i > 0) + s->ii[i] = is[i]; + } + } while (--i >= 0 && j); + + s->buf_len = 0; + + /* check whether this have been all SDEs */ + if (j) { #ifdef DEBUG - fprintf(stderr, "This was the final SDE in this BIE, " - "%ld bytes left.\n", (long) (len - *cnt)); + fprintf(stderr, "This was the final SDE in this BIE, " + "%ld bytes left.\n", (long) (len - *cnt)); #endif - s->bie_len = 0; - return JBG_EOK; - } - - /* check whether we have to abort because of xmax/ymax */ - if (iindex[s->order & 7][LAYER] == 0 && i < 0) { - /* LAYER is the outermost loop and we have just gone to next layer */ - if (jbg_ceil_half(s->xd, s->d - s->ii[0]) > s->xmax || - jbg_ceil_half(s->yd, s->d - s->ii[0]) > s->ymax) { - s->xmax = 4294967295UL; - s->ymax = 4294967295UL; - return JBG_EOK_INTR; - } - if (s->ii[0] > (unsigned long) s->dmax) { - s->dmax = 256; - return JBG_EOK_INTR; - } - } - - break; + s->bie_len = 0; + return JBG_EOK; + } + + /* check whether we have to abort because of xmax/ymax */ + if (iindex[s->order & 7][LAYER] == 0 && i < 0) { + /* LAYER is the outermost loop and we have just gone to next layer */ + if (jbg_ceil_half(s->xd, s->d - s->ii[0]) > s->xmax || + jbg_ceil_half(s->yd, s->d - s->ii[0]) > s->ymax) { + s->xmax = 4294967295UL; + s->ymax = 4294967295UL; + return JBG_EOK_INTR; + } + if (s->ii[0] > (unsigned long) s->dmax) { + s->dmax = 256; + return JBG_EOK_INTR; + } + } + + break; } s->buf_len = 0; @@ -2879,11 +2879,11 @@ int jbg_dec_in(struct jbg_dec_state *s, unsigned char *data, size_t len, *cnt += decode_pscd(s, data + *cnt, len - *cnt); if (*cnt < len && data[*cnt] != 0xff) { #ifdef DEBUG - fprintf(stderr, "PSCD was longer than expected, unread bytes " - "%02x %02x %02x %02x ...\n", data[*cnt], data[*cnt+1], - data[*cnt+2], data[*cnt+3]); + fprintf(stderr, "PSCD was longer than expected, unread bytes " + "%02x %02x %02x %02x ...\n", data[*cnt], data[*cnt+1], + data[*cnt+2], data[*cnt+3]); #endif - return JBG_EINVAL | 14; + return JBG_EINVAL | 14; } } @@ -2967,8 +2967,8 @@ unsigned long jbg_dec_getsize(const struct jbg_dec_state *s) return 0; else return - jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1) + 3) * /* overflow risk? */ - jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1)); + jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1) + 3) * /* overflow risk? */ + jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1)); } return jbg_ceil_half(s->xd, 3) * s->yd; @@ -2989,9 +2989,9 @@ unsigned long jbg_dec_getsize_merged(const struct jbg_dec_state *s) return 0; else return - jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1)) * /* overflow risk? */ - jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1)) * - ((s->planes + 7) / 8); + jbg_ceil_half(s->xd, s->d - (s->ii[0] - 1)) * /* overflow risk? */ + jbg_ceil_half(s->yd, s->d - (s->ii[0] - 1)) * + ((s->planes + 7) / 8); } return s->xd * s->yd * ((s->planes + 7) / 8); @@ -3045,9 +3045,9 @@ void jbg_dec_free(struct jbg_dec_state *s) * actually transfer to dest. */ void jbg_split_planes(unsigned long x, unsigned long y, int has_planes, - int encode_planes, - const unsigned char *src, unsigned char **dest, - int use_graycode) + int encode_planes, + const unsigned char *src, unsigned char **dest, + int use_graycode) { unsigned long bpl = jbg_ceil_half(x, 3); /* bytes per line in dest plane */ unsigned long line, i; @@ -3068,29 +3068,29 @@ void jbg_split_planes(unsigned long x, unsigned long y, int has_planes, for (line = 0; line < y; line++) { /* lines loop */ for (i = 0; i * 8 < x; i++) { /* dest bytes loop */ for (k = 0; k < 8 && i * 8 + k < x; k++) { /* pixel loop */ - prev = 0; - for (p = 0; p < encode_planes; p++) { /* bit planes loop */ - /* calculate which bit in *src do we want */ - bitno = (msb - p) & 7; - /* put this bit with its left neighbor right adjusted into bits */ - bits = (prev | *src) >> bitno; - /* go to next *src byte, but keep old */ - if (bitno == 0) - prev = *src++ << 8; - /* make space for inserting new bit */ - dest[p][bpl * line + i] <<= 1; - /* insert bit, if requested apply Gray encoding */ - dest[p][bpl * line + i] |= (bits ^ (use_graycode & (bits>>1))) & 1; - /* - * Theorem: Let b(n),...,b(1),b(0) be the digits of a - * binary word and let g(n),...,g(1),g(0) be the digits of the - * corresponding Gray code word, then g(i) = b(i) xor b(i+1). - */ - } - /* skip unused *src bytes */ - for (;p < has_planes; p++) - if (((msb - p) & 7) == 0) - src++; + prev = 0; + for (p = 0; p < encode_planes; p++) { /* bit planes loop */ + /* calculate which bit in *src do we want */ + bitno = (msb - p) & 7; + /* put this bit with its left neighbor right adjusted into bits */ + bits = (prev | *src) >> bitno; + /* go to next *src byte, but keep old */ + if (bitno == 0) + prev = *src++ << 8; + /* make space for inserting new bit */ + dest[p][bpl * line + i] <<= 1; + /* insert bit, if requested apply Gray encoding */ + dest[p][bpl * line + i] |= (bits ^ (use_graycode & (bits>>1))) & 1; + /* + * Theorem: Let b(n),...,b(1),b(0) be the digits of a + * binary word and let g(n),...,g(1),g(0) be the digits of the + * corresponding Gray code word, then g(i) = b(i) xor b(i+1). + */ + } + /* skip unused *src bytes */ + for (;p < has_planes; p++) + if (((msb - p) & 7) == 0) + src++; } } for (p = 0; p < encode_planes; p++) /* right padding loop */ @@ -3106,8 +3106,8 @@ void jbg_split_planes(unsigned long x, unsigned long y, int has_planes, * jbg_split_planes(). */ void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode, - void (*data_out)(unsigned char *start, size_t len, - void *file), void *file) + void (*data_out)(unsigned char *start, size_t len, + void *file), void *file) { #define BUFLEN 4096 unsigned long bpl, line, i; @@ -3139,19 +3139,19 @@ void jbg_dec_merge_planes(const struct jbg_dec_state *s, int use_graycode, for (line = 0; line < y; line++) { /* lines loop */ for (i = 0; i * 8 < x; i++) { /* src bytes loop */ for (k = 0; k < 8 && i * 8 + k < x; k++) { /* pixel loop */ - v = 0; - for (p = 0; p < s->planes;) { /* dest bytes loop */ - do { - v = (v << 1) | - (((src[p][bpl * line + i] >> (7 - k)) & 1) ^ - (use_graycode & v)); - } while ((s->planes - ++p) & 7); - *bp++ = v; - if (bp - buf == BUFLEN) { - data_out(buf, BUFLEN, file); - bp = buf; - } - } + v = 0; + for (p = 0; p < s->planes;) { /* dest bytes loop */ + do { + v = (v << 1) | + (((src[p][bpl * line + i] >> (7 - k)) & 1) ^ + (use_graycode & v)); + } while ((s->planes - ++p) & 7); + *bp++ = v; + if (bp - buf == BUFLEN) { + data_out(buf, BUFLEN, file); + bp = buf; + } + } } } } @@ -3186,15 +3186,15 @@ static unsigned char *jbg_next_pscdms(unsigned char *p, size_t len) if (p[0] != MARKER_ESC || p[1] == MARKER_STUFF) { do { while (p[0] == MARKER_ESC && p[1] == MARKER_STUFF) { - p += 2; - len -= 2; - if (len < 2) - return NULL; /* not enough bytes left for complete marker segment */ + p += 2; + len -= 2; + if (len < 2) + return NULL; /* not enough bytes left for complete marker segment */ } assert(len >= 2); pp = (unsigned char *) memchr(p, MARKER_ESC, len - 1); if (!pp) - return NULL; /* no marker segment terminates the PSCD */ + return NULL; /* no marker segment terminates the PSCD */ l = pp - p; assert(l < len); p += l; @@ -3208,19 +3208,19 @@ static unsigned char *jbg_next_pscdms(unsigned char *p, size_t len) return p + 2; case MARKER_NEWLEN: if (len < 6) - return NULL; /* not enough bytes left for complete marker segment */ + return NULL; /* not enough bytes left for complete marker segment */ return p + 6; case MARKER_ATMOVE: if (len < 8) - return NULL; /* not enough bytes left for complete marker segment */ + return NULL; /* not enough bytes left for complete marker segment */ return p + 8; case MARKER_COMMENT: if (len < 6) - return NULL; /* not enough bytes left for complete marker segment */ + return NULL; /* not enough bytes left for complete marker segment */ l = (((long) p[2] << 24) | ((long) p[3] << 16) | - ((long) p[4] << 8) | (long) p[5]); + ((long) p[4] << 8) | (long) p[5]); if (len - 6 < l) - return NULL; /* not enough bytes left for complete marker segment */ + return NULL; /* not enough bytes left for complete marker segment */ return p + 6 + l; default: /* unknown marker sequence encountered */ @@ -3265,18 +3265,18 @@ int jbg_newlen(unsigned char *bie, size_t len) else if (p[0] == MARKER_ESC) switch (p[1]) { case MARKER_NEWLEN: - y = (((long) bie[ 8] << 24) | ((long) bie[ 9] << 16) | - ((long) bie[10] << 8) | (long) bie[11]); - yn = (((long) p[2] << 24) | ((long) p[3] << 16) | - ((long) p[4] << 8) | (long) p[5]); - if (yn > y) return JBG_EINVAL | 12; - /* overwrite YD in BIH with YD from NEWLEN */ - for (i = 0; i < 4; i++) { - bie[8+i] = p[2+i]; - } - return JBG_EOK; + y = (((long) bie[ 8] << 24) | ((long) bie[ 9] << 16) | + ((long) bie[10] << 8) | (long) bie[11]); + yn = (((long) p[2] << 24) | ((long) p[3] << 16) | + ((long) p[4] << 8) | (long) p[5]); + if (yn > y) return JBG_EINVAL | 12; + /* overwrite YD in BIH with YD from NEWLEN */ + for (i = 0; i < 4; i++) { + bie[8+i] = p[2+i]; + } + return JBG_EOK; case MARKER_ABORT: - return JBG_EABORT; + return JBG_EABORT; } } return JBG_EINVAL | 0; |