| 1 |
/* |
| 2 |
* Arithmetic encoder and decoder of the portable JBIG |
| 3 |
* compression library |
| 4 |
* |
| 5 |
* Markus Kuhn -- http://www.cl.cam.ac.uk/~mgk25/jbigkit/ |
| 6 |
* |
| 7 |
* This module implements a portable standard C arithmetic encoder |
| 8 |
* and decoder used by the JBIG lossless bi-level image compression |
| 9 |
* algorithm as specified in International Standard ISO 11544:1993 |
| 10 |
* and ITU-T Recommendation T.82. |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License as published by |
| 14 |
* the Free Software Foundation; either version 2 of the License, or |
| 15 |
* (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 |
* |
| 26 |
* If you want to use this program under different license conditions, |
| 27 |
* then contact the author for an arrangement. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
#include "jbig_ar.h" |
| 32 |
|
| 33 |
/* |
| 34 |
* Probability estimation tables for the arithmetic encoder/decoder |
| 35 |
* given by ITU T.82 Table 24. |
| 36 |
*/ |
| 37 |
|
| 38 |
static short int lsztab[113] = { |
| 39 |
0x5a1d, 0x2586, 0x1114, 0x080b, 0x03d8, 0x01da, 0x00e5, 0x006f, |
| 40 |
0x0036, 0x001a, 0x000d, 0x0006, 0x0003, 0x0001, 0x5a7f, 0x3f25, |
| 41 |
0x2cf2, 0x207c, 0x17b9, 0x1182, 0x0cef, 0x09a1, 0x072f, 0x055c, |
| 42 |
0x0406, 0x0303, 0x0240, 0x01b1, 0x0144, 0x00f5, 0x00b7, 0x008a, |
| 43 |
0x0068, 0x004e, 0x003b, 0x002c, 0x5ae1, 0x484c, 0x3a0d, 0x2ef1, |
| 44 |
0x261f, 0x1f33, 0x19a8, 0x1518, 0x1177, 0x0e74, 0x0bfb, 0x09f8, |
| 45 |
0x0861, 0x0706, 0x05cd, 0x04de, 0x040f, 0x0363, 0x02d4, 0x025c, |
| 46 |
0x01f8, 0x01a4, 0x0160, 0x0125, 0x00f6, 0x00cb, 0x00ab, 0x008f, |
| 47 |
0x5b12, 0x4d04, 0x412c, 0x37d8, 0x2fe8, 0x293c, 0x2379, 0x1edf, |
| 48 |
0x1aa9, 0x174e, 0x1424, 0x119c, 0x0f6b, 0x0d51, 0x0bb6, 0x0a40, |
| 49 |
0x5832, 0x4d1c, 0x438e, 0x3bdd, 0x34ee, 0x2eae, 0x299a, 0x2516, |
| 50 |
0x5570, 0x4ca9, 0x44d9, 0x3e22, 0x3824, 0x32b4, 0x2e17, 0x56a8, |
| 51 |
0x4f46, 0x47e5, 0x41cf, 0x3c3d, 0x375e, 0x5231, 0x4c0f, 0x4639, |
| 52 |
0x415e, 0x5627, 0x50e7, 0x4b85, 0x5597, 0x504f, 0x5a10, 0x5522, |
| 53 |
0x59eb |
| 54 |
}; |
| 55 |
|
| 56 |
static unsigned char nmpstab[113] = { |
| 57 |
1, 2, 3, 4, 5, 6, 7, 8, |
| 58 |
9, 10, 11, 12, 13, 13, 15, 16, |
| 59 |
17, 18, 19, 20, 21, 22, 23, 24, |
| 60 |
25, 26, 27, 28, 29, 30, 31, 32, |
| 61 |
33, 34, 35, 9, 37, 38, 39, 40, |
| 62 |
41, 42, 43, 44, 45, 46, 47, 48, |
| 63 |
49, 50, 51, 52, 53, 54, 55, 56, |
| 64 |
57, 58, 59, 60, 61, 62, 63, 32, |
| 65 |
65, 66, 67, 68, 69, 70, 71, 72, |
| 66 |
73, 74, 75, 76, 77, 78, 79, 48, |
| 67 |
81, 82, 83, 84, 85, 86, 87, 71, |
| 68 |
89, 90, 91, 92, 93, 94, 86, 96, |
| 69 |
97, 98, 99, 100, 93, 102, 103, 104, |
| 70 |
99, 106, 107, 103, 109, 107, 111, 109, |
| 71 |
111 |
| 72 |
}; |
| 73 |
|
| 74 |
/* |
| 75 |
* least significant 7 bits (mask 0x7f) of nlpstab[] contain NLPS value, |
| 76 |
* most significant bit (mask 0x80) contains SWTCH bit |
| 77 |
*/ |
| 78 |
static unsigned char nlpstab[113] = { |
| 79 |
129, 14, 16, 18, 20, 23, 25, 28, |
| 80 |
30, 33, 35, 9, 10, 12, 143, 36, |
| 81 |
38, 39, 40, 42, 43, 45, 46, 48, |
| 82 |
49, 51, 52, 54, 56, 57, 59, 60, |
| 83 |
62, 63, 32, 33, 165, 64, 65, 67, |
| 84 |
68, 69, 70, 72, 73, 74, 75, 77, |
| 85 |
78, 79, 48, 50, 50, 51, 52, 53, |
| 86 |
54, 55, 56, 57, 58, 59, 61, 61, |
| 87 |
193, 80, 81, 82, 83, 84, 86, 87, |
| 88 |
87, 72, 72, 74, 74, 75, 77, 77, |
| 89 |
208, 88, 89, 90, 91, 92, 93, 86, |
| 90 |
216, 95, 96, 97, 99, 99, 93, 223, |
| 91 |
101, 102, 103, 104, 99, 105, 106, 107, |
| 92 |
103, 233, 108, 109, 110, 111, 238, 112, |
| 93 |
240 |
| 94 |
}; |
| 95 |
|
| 96 |
/* |
| 97 |
* The next functions implement the arithmedic encoder and decoder |
| 98 |
* required for JBIG. The same algorithm is also used in the arithmetic |
| 99 |
* variant of JPEG. |
| 100 |
*/ |
| 101 |
|
| 102 |
/* marker codes */ |
| 103 |
#define MARKER_STUFF 0x00 |
| 104 |
#define MARKER_ESC 0xff |
| 105 |
|
| 106 |
void |
| 107 |
arith_encode_init(struct jbg_arenc_state *s, |
| 108 |
int reuse_st) { |
| 109 |
|
| 110 |
int i; |
| 111 |
|
| 112 |
if (!reuse_st) |
| 113 |
for (i = 0; i < 4096; s->st[i++] = 0) ; |
| 114 |
s->c = 0; |
| 115 |
s->a = 0x10000L; |
| 116 |
s->sc = 0; |
| 117 |
s->ct = 11; |
| 118 |
s->buffer = -1; /* empty */ |
| 119 |
|
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
void |
| 126 |
arith_encode_flush(struct jbg_arenc_state *s) { |
| 127 |
|
| 128 |
unsigned long int temp; |
| 129 |
|
| 130 |
/* find the s->c in the coding interval with the largest |
| 131 |
* number of trailing zero bits */ |
| 132 |
if ((temp = (s->a - 1 + s->c) & 0xffff0000L) < s->c) |
| 133 |
s->c = temp + 0x8000; |
| 134 |
else |
| 135 |
s->c = temp; |
| 136 |
/* send remaining bytes to output */ |
| 137 |
s->c <<= s->ct; |
| 138 |
if (s->c & 0xf8000000L) { |
| 139 |
/* one final overflow has to be handled */ |
| 140 |
if (s->buffer >= 0) { |
| 141 |
s->byte_out(s->buffer + 1, s->file); |
| 142 |
if (s->buffer + 1 == MARKER_ESC) |
| 143 |
s->byte_out(MARKER_STUFF, s->file); |
| 144 |
} |
| 145 |
/* output 0x00 bytes only when more non-0x00 will follow */ |
| 146 |
if (s->c & 0x7fff800L) |
| 147 |
for (; s->sc; --s->sc) |
| 148 |
s->byte_out(0x00, s->file); |
| 149 |
} else { |
| 150 |
if (s->buffer >= 0) |
| 151 |
s->byte_out(s->buffer, s->file); |
| 152 |
/* T.82 figure 30 says buffer+1 for the above line! Typo? */ |
| 153 |
for (; s->sc; --s->sc) { |
| 154 |
s->byte_out(0xff, s->file); |
| 155 |
s->byte_out(MARKER_STUFF, s->file); |
| 156 |
} |
| 157 |
} |
| 158 |
/* output final bytes only if they are not 0x00 */ |
| 159 |
if (s->c & 0x7fff800L) { |
| 160 |
s->byte_out((s->c >> 19) & 0xff, s->file); |
| 161 |
if (((s->c >> 19) & 0xff) == MARKER_ESC) |
| 162 |
s->byte_out(MARKER_STUFF, s->file); |
| 163 |
if (s->c & 0x7f800L) { |
| 164 |
s->byte_out((s->c >> 11) & 0xff, s->file); |
| 165 |
if (((s->c >> 11) & 0xff) == MARKER_ESC) |
| 166 |
s->byte_out(MARKER_STUFF, s->file); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
void |
| 176 |
arith_encode(struct jbg_arenc_state *s, |
| 177 |
int cx, |
| 178 |
int pix) { |
| 179 |
|
| 180 |
unsigned int lsz, ss; |
| 181 |
unsigned char *st; |
| 182 |
long int temp; |
| 183 |
|
| 184 |
assert(cx >= 0 && cx < 4096); |
| 185 |
st = s->st + cx; |
| 186 |
ss = *st & 0x7f; |
| 187 |
assert(ss < 113); |
| 188 |
lsz = lsztab[ss]; |
| 189 |
|
| 190 |
#if 0 |
| 191 |
fprintf(stderr, "pix = %d, cx = %d, mps = %d, st = %3d, lsz = 0x%04x, " |
| 192 |
"a = 0x%05lx, c = 0x%08lx, ct = %2d, buf = 0x%02x\n", |
| 193 |
pix, cx, !!(s->st[cx] & 0x80), ss, lsz, s->a, s->c, s->ct, |
| 194 |
s->buffer); |
| 195 |
#endif |
| 196 |
|
| 197 |
if (((pix << 7) ^ s->st[cx]) & 0x80) { |
| 198 |
/* encode the less probable symbol */ |
| 199 |
if ((s->a -= lsz) >= lsz) { |
| 200 |
/* If the interval size (lsz) for the less probable symbol (LPS) |
| 201 |
* is larger than the interval size for the MPS, then exchange |
| 202 |
* the two symbols for coding efficiency, otherwise code the LPS |
| 203 |
* as usual: */ |
| 204 |
s->c += s->a; |
| 205 |
s->a = lsz; |
| 206 |
} |
| 207 |
/* Check whether MPS/LPS exchange is necessary |
| 208 |
* and chose next probability estimator status */ |
| 209 |
*st &= 0x80; |
| 210 |
*st ^= nlpstab[ss]; |
| 211 |
} else { |
| 212 |
/* encode the more probable symbol */ |
| 213 |
if ((s->a -= lsz) & 0xffff8000L) |
| 214 |
return; /* A >= 0x8000 -> ready, no renormalization required */ |
| 215 |
if (s->a < lsz) { |
| 216 |
/* If the interval size (lsz) for the less probable symbol (LPS) |
| 217 |
* is larger than the interval size for the MPS, then exchange |
| 218 |
* the two symbols for coding efficiency: */ |
| 219 |
s->c += s->a; |
| 220 |
s->a = lsz; |
| 221 |
} |
| 222 |
/* chose next probability estimator status */ |
| 223 |
*st &= 0x80; |
| 224 |
*st |= nmpstab[ss]; |
| 225 |
} |
| 226 |
|
| 227 |
/* renormalization of coding interval */ |
| 228 |
do { |
| 229 |
s->a <<= 1; |
| 230 |
s->c <<= 1; |
| 231 |
--s->ct; |
| 232 |
if (s->ct == 0) { |
| 233 |
/* another byte is ready for output */ |
| 234 |
temp = s->c >> 19; |
| 235 |
if (temp & 0xffffff00L) { |
| 236 |
/* handle overflow over all buffered 0xff bytes */ |
| 237 |
if (s->buffer >= 0) { |
| 238 |
++s->buffer; |
| 239 |
s->byte_out(s->buffer, s->file); |
| 240 |
if (s->buffer == MARKER_ESC) |
| 241 |
s->byte_out(MARKER_STUFF, s->file); |
| 242 |
} |
| 243 |
for (; s->sc; --s->sc) |
| 244 |
s->byte_out(0x00, s->file); |
| 245 |
s->buffer = temp & 0xff; /* new output byte, might overflow later */ |
| 246 |
assert(s->buffer != 0xff); |
| 247 |
/* can s->buffer really never become 0xff here? */ |
| 248 |
} else if (temp == 0xff) { |
| 249 |
/* buffer 0xff byte (which might overflow later) */ |
| 250 |
++s->sc; |
| 251 |
} else { |
| 252 |
/* output all buffered 0xff bytes, they will not overflow any more */ |
| 253 |
if (s->buffer >= 0) |
| 254 |
s->byte_out(s->buffer, s->file); |
| 255 |
for (; s->sc; --s->sc) { |
| 256 |
s->byte_out(0xff, s->file); |
| 257 |
s->byte_out(MARKER_STUFF, s->file); |
| 258 |
} |
| 259 |
s->buffer = temp; /* buffer new output byte (can still overflow) */ |
| 260 |
} |
| 261 |
s->c &= 0x7ffffL; |
| 262 |
s->ct = 8; |
| 263 |
} |
| 264 |
} while (s->a < 0x8000); |
| 265 |
|
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
void |
| 272 |
arith_decode_init(struct jbg_ardec_state *s, |
| 273 |
int reuse_st) { |
| 274 |
|
| 275 |
int i; |
| 276 |
|
| 277 |
if (!reuse_st) |
| 278 |
for (i = 0; i < 4096; s->st[i++] = 0) ; |
| 279 |
s->c = 0; |
| 280 |
s->a = 1; |
| 281 |
s->ct = 0; |
| 282 |
s->startup = 1; |
| 283 |
s->nopadding = 0; |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
/* |
| 290 |
* Decode and return one symbol from the provided PSCD byte stream |
| 291 |
* that starts in s->pscd_ptr and ends in the byte before s->pscd_end. |
| 292 |
* The context cx is a 12-bit integer in the range 0..4095. This |
| 293 |
* function will advance s->pscd_ptr each time it has consumed all |
| 294 |
* information from that PSCD byte. |
| 295 |
* |
| 296 |
* If a symbol has been decoded successfully, the return value will be |
| 297 |
* 0 or 1 (depending on the symbol). |
| 298 |
* |
| 299 |
* If the decoder was not able to decode a symbol from the provided |
| 300 |
* PSCD, then the return value will be -1, and two cases can be |
| 301 |
* distinguished: |
| 302 |
* |
| 303 |
* s->pscd_ptr == s->pscd_end: |
| 304 |
* |
| 305 |
* The decoder has used up all information in the provided PSCD |
| 306 |
* bytes. Further PSCD bytes have to be provided (via new values of |
| 307 |
* s->pscd_ptr and/or s->pscd_end) before another symbol can be |
| 308 |
* decoded. |
| 309 |
* |
| 310 |
* s->pscd_ptr == s->pscd_end - 1: |
| 311 |
* |
| 312 |
* The decoder has used up all provided PSCD bytes except for the |
| 313 |
* very last byte, because that has the value 0xff. The decoder can |
| 314 |
* at this point not yet tell whether this 0xff belongs to a |
| 315 |
* MARKER_STUFF sequence or marks the end of the PSCD. Further PSCD |
| 316 |
* bytes have to be provided (via new values of s->pscd_ptr and/or |
| 317 |
* s->pscd_end), including the not yet processed 0xff byte, before |
| 318 |
* another symbol can be decoded successfully. |
| 319 |
* |
| 320 |
* If s->nopadding != 0, the decoder will return -2 when it reaches |
| 321 |
* the first two bytes of the marker segment that follows (and |
| 322 |
* terminates) the PSCD, but before decoding the first symbol that |
| 323 |
* depends on a bit in the input data that could have been the result |
| 324 |
* of zero padding, and might, therefore, never have been encoded. |
| 325 |
* This gives the caller the opportunity to lookahead early enough |
| 326 |
* beyond a terminating SDNORM/SDRST for a trailing NEWLEN (as |
| 327 |
* required by T.85) before decoding remaining symbols. Call the |
| 328 |
* decoder again afterwards as often as necessary (leaving s->pscd_ptr |
| 329 |
* pointing to the start of the marker segment) to retrieve any |
| 330 |
* required remaining symbols that might depend on padding. |
| 331 |
* |
| 332 |
* [Note that each PSCD can be decoded into an infinitely long |
| 333 |
* sequence of symbols, because the encoder might have truncated away |
| 334 |
* an arbitrarily long sequence of trailing 0x00 bytes, which the |
| 335 |
* decoder will append automatically as needed when it reaches the end |
| 336 |
* of the PSCD. Therefore, the decoder cannot report any end of the |
| 337 |
* symbol sequence and other means (external to the PSCD and |
| 338 |
* arithmetic decoding process) are needed to determine that.] |
| 339 |
*/ |
| 340 |
|
| 341 |
int |
| 342 |
arith_decode(struct jbg_ardec_state *s, |
| 343 |
int cx) { |
| 344 |
|
| 345 |
unsigned lsz, ss; |
| 346 |
unsigned char *st; |
| 347 |
int pix; |
| 348 |
|
| 349 |
/* renormalization */ |
| 350 |
while (s->a < 0x8000 || s->startup) { |
| 351 |
while (s->ct <= 8 && s->ct >= 0) { |
| 352 |
/* first we can move a new byte into s->c */ |
| 353 |
if (s->pscd_ptr >= s->pscd_end) { |
| 354 |
return -1; /* more bytes needed */ |
| 355 |
} |
| 356 |
if (*s->pscd_ptr == 0xff) |
| 357 |
if (s->pscd_ptr + 1 >= s->pscd_end) { |
| 358 |
return -1; /* final 0xff byte not processed */ |
| 359 |
} else { |
| 360 |
if (*(s->pscd_ptr + 1) == MARKER_STUFF) { |
| 361 |
s->c |= 0xffL << (8 - s->ct); |
| 362 |
s->ct += 8; |
| 363 |
s->pscd_ptr += 2; |
| 364 |
} else { |
| 365 |
s->ct = -1; /* start padding with zero bytes */ |
| 366 |
if (s->nopadding) { |
| 367 |
s->nopadding = 0; |
| 368 |
return -2; /* subsequent symbols might depend on zero padding */ |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
else { |
| 373 |
s->c |= (long int)*(s->pscd_ptr++) << (8 - s->ct); |
| 374 |
s->ct += 8; |
| 375 |
} |
| 376 |
} |
| 377 |
s->c <<= 1; |
| 378 |
s->a <<= 1; |
| 379 |
if (s->ct >= 0) s->ct--; |
| 380 |
if (s->a == 0x10000L) |
| 381 |
s->startup = 0; |
| 382 |
} |
| 383 |
|
| 384 |
st = s->st + cx; |
| 385 |
ss = *st & 0x7f; |
| 386 |
assert(ss < 113); |
| 387 |
lsz = lsztab[ss]; |
| 388 |
|
| 389 |
#if 0 |
| 390 |
fprintf(stderr, "cx = %d, mps = %d, st = %3d, lsz = 0x%04x, a = 0x%05lx, " |
| 391 |
"c = 0x%08lx, ct = %2d\n", |
| 392 |
cx, !!(s->st[cx] & 0x80), ss, lsz, s->a, s->c, s->ct); |
| 393 |
#endif |
| 394 |
|
| 395 |
if ((s->c >> 16) < (s->a -= lsz)) |
| 396 |
if (s->a & 0xffff8000L) |
| 397 |
return *st >> 7; |
| 398 |
else { |
| 399 |
/* MPS_EXCHANGE */ |
| 400 |
if (s->a < lsz) { |
| 401 |
pix = 1 - (*st >> 7); |
| 402 |
/* Check whether MPS/LPS exchange is necessary |
| 403 |
* and chose next probability estimator status */ |
| 404 |
*st &= 0x80; |
| 405 |
*st ^= nlpstab[ss]; |
| 406 |
} else { |
| 407 |
pix = *st >> 7; |
| 408 |
*st &= 0x80; |
| 409 |
*st |= nmpstab[ss]; |
| 410 |
} |
| 411 |
} |
| 412 |
else { |
| 413 |
/* LPS_EXCHANGE */ |
| 414 |
if (s->a < lsz) { |
| 415 |
s->c -= s->a << 16; |
| 416 |
s->a = lsz; |
| 417 |
pix = *st >> 7; |
| 418 |
*st &= 0x80; |
| 419 |
*st |= nmpstab[ss]; |
| 420 |
} else { |
| 421 |
s->c -= s->a << 16; |
| 422 |
s->a = lsz; |
| 423 |
pix = 1 - (*st >> 7); |
| 424 |
/* Check whether MPS/LPS exchange is necessary |
| 425 |
* and chose next probability estimator status */ |
| 426 |
*st &= 0x80; |
| 427 |
*st ^= nlpstab[ss]; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
return pix; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|