1 From 195345e454d3df7ff9c07c3d36881ec52e789af2 Mon Sep 17 00:00:00 2001
2 From: Mukund Sivaraman <muks@banu.com>
3 Date: Sun, 3 Oct 2010 12:23:41 +0530
4 Subject: [PATCH] Add support for decoding arithmetic coded content
6 This is based on a patch by Guido Vollbeding <guivol@esc.de>, which
7 included code to both encode and decode arithmetic coded content.
9 This patch only adds the decoding portion. No new arithmetic coded
10 content can be produced, but existing arithmetic coded content can
13 libjpeg-turbo with this patch has been tested with some arithmetic
14 coded images (digital photographs).
17 README.arithmetic | 215 +++++++++++++++
18 jaricom.c | 149 +++++++++++
19 jdarith.c | 762 +++++++++++++++++++++++++++++++++++++++++++++++++++++
25 9 files changed, 1142 insertions(+), 11 deletions(-)
26 create mode 100644 README.arithmetic
27 create mode 100644 jaricom.c
28 create mode 100644 jdarith.c
30 diff --git a/Makefile.am b/Makefile.am
31 index f8552ea..1eee598 100644
34 @@ -7,14 +7,15 @@ nodist_include_HEADERS = jconfig.h
35 HDRS = jchuff.h jdct.h jdhuff.h jerror.h jinclude.h jmemsys.h jmorecfg.h \
36 jpegint.h jpeglib.h jversion.h jsimd.h jsimddct.h
38 -libjpeg_la_SOURCES = $(HDRS) jcapimin.c jcapistd.c jccoefct.c jccolor.c \
39 - jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c \
40 - jcomapi.c jcparam.c jcphuff.c jcprepct.c jcsample.c jctrans.c \
41 - jdapimin.c jdapistd.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c \
42 - jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c \
43 - jdmerge.c jdphuff.c jdpostct.c jdsample.c jdtrans.c jerror.c \
44 - jfdctflt.c jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c \
45 - jidctred.c jquant1.c jquant2.c jutils.c jmemmgr.c jmemnobs.c
46 +libjpeg_la_SOURCES = $(HDRS) jaricom.c jcapimin.c jcapistd.c \
47 + jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcinit.c jcmainct.c \
48 + jcmarker.c jcmaster.c jcomapi.c jcparam.c jcphuff.c jcprepct.c \
49 + jcsample.c jctrans.c jdapimin.c jdapistd.c jdarith.c \
50 + jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jddctmgr.c jdhuff.c \
51 + jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \
52 + jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \
53 + jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c \
54 + jquant1.c jquant2.c jutils.c jmemmgr.c jmemnobs.c
56 libturbojpeg_la_SOURCES = $(libjpeg_la_SOURCES) turbojpegl.c turbojpeg.h \
58 diff --git a/README.arithmetic b/README.arithmetic
60 index 0000000..0dd3b9a
62 +++ b/README.arithmetic
64 +JPEG arithmetic encoding and decoding portable software implementation
65 +======================================================================
67 +Release of 28-Mar-98 by Guido Vollbeding <guido@jpegclub.org>
68 +=============================================================
72 + http://sylvana.net/jpeg-ari/
73 + (directory containing the actual archive files:)
75 + http://sylvana.net/jpeg-ari/jpeg-ari-28mar98.tar.gz
77 + http://sylvana.net/jpeg-ari/jpeg-ari.zip
83 +This package is distributed in the hope that it will be useful,
84 +but WITHOUT ANY WARRANTY; without even the implied warranty of
85 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
87 +It is possible that certain products which can be built using this
88 +software modules might form inventions protected by patent rights in
89 +some countries (e.g. by patents about arithmetic coding algorithms
90 +owned by IBM and AT&T in the USA). Provision of this software by the
91 +author does NOT include any licenses for any patents.
92 +In those countries where a patent license is required for certain
93 +applications of this software modules, you will have to obtain such
96 +See Annex L in the JPEG spec for further information
97 +and a list of relevant patents.
103 +This is my implementation of the arithmetic encoding and decoding
104 +back-end for JPEG as specified in the
106 + ISO/IEC International Standard 10918-1 and CCITT Recommendation
107 + ITU-T T.81, "Information Technology - Digital Compression and
108 + Coding of Continuous-tone Still Images, Part 1: Requirements
111 +Arithmetic coding is a state-of-the-art lossless entropy data
112 +compression method which offers better compression performance
113 +than the well-established Huffman entropy coding process.
115 +The JPEG standard specifies a particular arithmetic coding scheme
116 +to be used optionally as alternative to Huffman coding.
122 +This package might be of interest for people who are looking for
123 +enhanced state-of-the-art image compression technologies.
125 +It is intended to provide a reasonable tool for experimental,
126 +comparison and evaluation purposes.
128 +See the Disclaimer above for restricted conditions of usage.
134 +This distribution is organized as add-on to the widespread
135 +Independent JPEG Group's JPEG software.
137 +Thus, once you managed to install the IJG software distribution
138 +successfully, there should be no additional problems (portability
139 +issues etc.) to incorporate this package into the library,
140 +and usage is straightforward.
142 +Transcode given JPEG files simply with a command like
144 + jpegtran -arithmetic [-progressive] < orig.jpg > arit.jpg
146 +into an arithmetic coded version LOSSLESSLY! Since there are
147 +practically no applications in existence which can handle such
148 +files, you can only transform it back with the same tool
150 + jpegtran [-optimize] [-progressive] < arit.jpg > orig2.jpg
152 +to verify correct operation.
154 +Thus, you can easily verify the enhanced compression performance
155 +of the arithmetic coding version compared to the Huffman (with
156 +fixed or custom tables) version.
158 +The claim to evaluate was that arithmetic coding gives an average
159 +5-10% compression improvement against Huffman.
160 +Early tests with this implementation support this claim, and you
161 +can perform tests with own material.
163 +Here are some actual results:
165 +% ./jpegtran -optimize < testorig.jpg > testopt.jpg
166 +% ./jpegtran -arithmetic < testorig.jpg > testarit.jpg
167 +% ./jpegtran < testarit.jpg > testorig2.jpg
168 +% ./jpegtran -arithmetic -progressive < testorig.jpg > testaritp.jpg
169 +% ./jpegtran < testaritp.jpg > testorig3.jpg
170 +% ./jpegtran -optimize < ../butterfly.jpg > ../buttopt.jpg
171 +% ./jpegtran -progressive < ../butterfly.jpg > ../buttprog.jpg
172 +% ./jpegtran -arithmetic < ../butterfly.jpg > ../buttarit.jpg
173 +% ./jpegtran < ../buttarit.jpg > ../butterfly2.jpg
174 +% ./jpegtran -arithmetic -progressive < ../butterfly.jpg > ../buttaritp.jpg
175 +% ./jpegtran < ../buttaritp.jpg > ../butterfly3.jpg
177 +-rw-r--r-- 1 guivol 5153 Apr 13 18:51 testarit.jpg
178 +-rw-r--r-- 1 guivol 5186 Apr 13 18:51 testaritp.jpg
179 +-rw-r--r-- 1 guivol 5756 Apr 2 15:10 testimg.jpg
180 +-rw-r--r-- 1 guivol 5645 Apr 2 15:10 testimgp.jpg
181 +-rw-r--r-- 1 guivol 5463 Apr 13 18:51 testopt.jpg
182 +-rw-r--r-- 1 guivol 5770 Apr 2 15:10 testorig.jpg
183 +-rw-r--r-- 1 guivol 5770 Apr 13 18:51 testorig2.jpg
184 +-rw-r--r-- 1 guivol 5770 Apr 13 18:51 testorig3.jpg
185 +-rw-r--r-- 1 guivol 5655 Apr 2 15:10 testprog.jpg
186 +% ls -l ../butt*.jpg
187 +-rw-r--r-- 1 guivol 460091 Apr 13 18:52 ../buttarit.jpg
188 +-rw-r--r-- 1 guivol 453703 Apr 13 18:52 ../buttaritp.jpg
189 +-rw-r--r-- 1 guivol 527823 Nov 19 18:41 ../butterfly.jpg
190 +-rw-r--r-- 1 guivol 527823 Apr 13 18:52 ../butterfly2.jpg
191 +-rw-r--r-- 1 guivol 527823 Apr 13 18:52 ../butterfly3.jpg
192 +-rw-r--r-- 1 guivol 511834 Apr 13 18:52 ../buttopt.jpg
193 +-rw-r--r-- 1 guivol 492237 Apr 13 18:52 ../buttprog.jpg
196 +Note that arithmetic coding requires only a single processing
197 +pass due to its fully-adaptive nature, and compared to one-pass
198 +(fixed tables) Huffman the arithmetic coded version consistently
199 +achieves 10% compression improvement.
200 +Compared with two-pass (custom tables) Huffman the improvement
203 +Note that I wasn't able yet to cross-check interoperability of
204 +the produced files with other implementations.
205 +Thus, I can't be sure that the files are compliant to the spec,
206 +but I hope so and the tests support it.
207 +The encoding and decoding processes should be correct anyway,
208 +however, in the sense that they are complementary to each other
209 +and thus retain data integrity.
211 +I would appreciate any indications for compliance or interoperability
212 +with other implementations from somebody.
213 +Please let me know if you are able to cross-check something.
219 +The installation is a 2-stage procedure:
221 +1. Preparing the IJG package for potential incorporation
222 + of the arithmetic coding feature.
224 +2. Incorporation of the actual arithmetic coding modules
225 + and enabling the feature for usage.
227 +The reason for this 2-stage process is the hope to make
228 +step 1 obsolete in future IJG releases.
229 +The actual implementation should remain separate IMHO due
230 +to the different usage conditions.
234 +1.1. Copy all files from the subdirectory 'patchv6b' into
235 + the IJG software's v6b source directory.
236 + This includes minor patches to some files and 3 extra
237 + files which hold place for the actual implementation.
239 +1.2. Update your Makefile/Projectfile for the inclusion of
240 + the 3 extra files. This will be done automatically
241 + if you use a configure-generated makefile and type
242 + './configure' (reconfigure).
244 +1.3. Recompile ('make').
246 +See the file 'PATCHES' in 'patchv6b' for details.
250 +2.1. Replace the 3 placeholder files by the actual implementation
253 +2.2. Enable application support of the new features by #defining
254 + C_ARITH_CODING_SUPPORTED and D_ARITH_CODING_SUPPORTED
257 +2.3. Recompile ('make').
259 +Note that I suggest to add 3 placeholder files to the IJG
260 +distribution. This would remove the need for system-dependent
261 +changes (Makefiles) and thus considerably simplify the actual
262 +installation for systems without a configure-generated makefile.
268 +- The Independent JPEG Group's software
270 +- JBIG-KIT lossless image compression library by Markus Kuhn
272 +- William B. Pennebaker, Joan L. Mitchell:
273 + "JPEG Still Image Data Compression Standard",
274 + Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
276 +- jpeg-faq (http://www.faqs.org/faqs/jpeg-faq/)
278 +- compression-faq (http://www.faqs.org/faqs/compression-faq/)
279 diff --git a/jaricom.c b/jaricom.c
281 index 0000000..e4292b0
288 + * Copyright (C) 1997, Guido Vollbeding <guivol@esc.de>.
289 + * This file is NOT part of the Independent JPEG Group's software
290 + * for legal reasons.
291 + * See the accompanying README file for conditions of distribution and use.
293 + * This file contains probability estimation tables for common use in
294 + * arithmetic entropy encoding and decoding routines.
296 + * This data represents Table D.2 in the JPEG spec (ISO/IEC IS 10918-1
297 + * and CCITT Recommendation ITU-T T.81) and Table 24 in the JBIG spec
298 + * (ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82).
301 +#define JPEG_INTERNALS
302 +#include "jinclude.h"
303 +#include "jpeglib.h"
305 +/* The following #define specifies the packing of the four components
306 + * into the compact INT32 representation.
307 + * Note that this formula must match the actual arithmetic encoder
308 + * and decoder implementation. The implementation has to be changed
309 + * if this formula is changed.
310 + * The current organisation is leaned on Markus Kuhn's JBIG
311 + * implementation (jbig_tab.c).
314 +#define V(a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b)
316 +const INT32 jaritab[113] = {
318 + * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS
320 +/* 0 */ V( 0x5a1d, 1, 1, 1 ),
321 +/* 1 */ V( 0x2586, 14, 2, 0 ),
322 +/* 2 */ V( 0x1114, 16, 3, 0 ),
323 +/* 3 */ V( 0x080b, 18, 4, 0 ),
324 +/* 4 */ V( 0x03d8, 20, 5, 0 ),
325 +/* 5 */ V( 0x01da, 23, 6, 0 ),
326 +/* 6 */ V( 0x00e5, 25, 7, 0 ),
327 +/* 7 */ V( 0x006f, 28, 8, 0 ),
328 +/* 8 */ V( 0x0036, 30, 9, 0 ),
329 +/* 9 */ V( 0x001a, 33, 10, 0 ),
330 +/* 10 */ V( 0x000d, 35, 11, 0 ),
331 +/* 11 */ V( 0x0006, 9, 12, 0 ),
332 +/* 12 */ V( 0x0003, 10, 13, 0 ),
333 +/* 13 */ V( 0x0001, 12, 13, 0 ),
334 +/* 14 */ V( 0x5a7f, 15, 15, 1 ),
335 +/* 15 */ V( 0x3f25, 36, 16, 0 ),
336 +/* 16 */ V( 0x2cf2, 38, 17, 0 ),
337 +/* 17 */ V( 0x207c, 39, 18, 0 ),
338 +/* 18 */ V( 0x17b9, 40, 19, 0 ),
339 +/* 19 */ V( 0x1182, 42, 20, 0 ),
340 +/* 20 */ V( 0x0cef, 43, 21, 0 ),
341 +/* 21 */ V( 0x09a1, 45, 22, 0 ),
342 +/* 22 */ V( 0x072f, 46, 23, 0 ),
343 +/* 23 */ V( 0x055c, 48, 24, 0 ),
344 +/* 24 */ V( 0x0406, 49, 25, 0 ),
345 +/* 25 */ V( 0x0303, 51, 26, 0 ),
346 +/* 26 */ V( 0x0240, 52, 27, 0 ),
347 +/* 27 */ V( 0x01b1, 54, 28, 0 ),
348 +/* 28 */ V( 0x0144, 56, 29, 0 ),
349 +/* 29 */ V( 0x00f5, 57, 30, 0 ),
350 +/* 30 */ V( 0x00b7, 59, 31, 0 ),
351 +/* 31 */ V( 0x008a, 60, 32, 0 ),
352 +/* 32 */ V( 0x0068, 62, 33, 0 ),
353 +/* 33 */ V( 0x004e, 63, 34, 0 ),
354 +/* 34 */ V( 0x003b, 32, 35, 0 ),
355 +/* 35 */ V( 0x002c, 33, 9, 0 ),
356 +/* 36 */ V( 0x5ae1, 37, 37, 1 ),
357 +/* 37 */ V( 0x484c, 64, 38, 0 ),
358 +/* 38 */ V( 0x3a0d, 65, 39, 0 ),
359 +/* 39 */ V( 0x2ef1, 67, 40, 0 ),
360 +/* 40 */ V( 0x261f, 68, 41, 0 ),
361 +/* 41 */ V( 0x1f33, 69, 42, 0 ),
362 +/* 42 */ V( 0x19a8, 70, 43, 0 ),
363 +/* 43 */ V( 0x1518, 72, 44, 0 ),
364 +/* 44 */ V( 0x1177, 73, 45, 0 ),
365 +/* 45 */ V( 0x0e74, 74, 46, 0 ),
366 +/* 46 */ V( 0x0bfb, 75, 47, 0 ),
367 +/* 47 */ V( 0x09f8, 77, 48, 0 ),
368 +/* 48 */ V( 0x0861, 78, 49, 0 ),
369 +/* 49 */ V( 0x0706, 79, 50, 0 ),
370 +/* 50 */ V( 0x05cd, 48, 51, 0 ),
371 +/* 51 */ V( 0x04de, 50, 52, 0 ),
372 +/* 52 */ V( 0x040f, 50, 53, 0 ),
373 +/* 53 */ V( 0x0363, 51, 54, 0 ),
374 +/* 54 */ V( 0x02d4, 52, 55, 0 ),
375 +/* 55 */ V( 0x025c, 53, 56, 0 ),
376 +/* 56 */ V( 0x01f8, 54, 57, 0 ),
377 +/* 57 */ V( 0x01a4, 55, 58, 0 ),
378 +/* 58 */ V( 0x0160, 56, 59, 0 ),
379 +/* 59 */ V( 0x0125, 57, 60, 0 ),
380 +/* 60 */ V( 0x00f6, 58, 61, 0 ),
381 +/* 61 */ V( 0x00cb, 59, 62, 0 ),
382 +/* 62 */ V( 0x00ab, 61, 63, 0 ),
383 +/* 63 */ V( 0x008f, 61, 32, 0 ),
384 +/* 64 */ V( 0x5b12, 65, 65, 1 ),
385 +/* 65 */ V( 0x4d04, 80, 66, 0 ),
386 +/* 66 */ V( 0x412c, 81, 67, 0 ),
387 +/* 67 */ V( 0x37d8, 82, 68, 0 ),
388 +/* 68 */ V( 0x2fe8, 83, 69, 0 ),
389 +/* 69 */ V( 0x293c, 84, 70, 0 ),
390 +/* 70 */ V( 0x2379, 86, 71, 0 ),
391 +/* 71 */ V( 0x1edf, 87, 72, 0 ),
392 +/* 72 */ V( 0x1aa9, 87, 73, 0 ),
393 +/* 73 */ V( 0x174e, 72, 74, 0 ),
394 +/* 74 */ V( 0x1424, 72, 75, 0 ),
395 +/* 75 */ V( 0x119c, 74, 76, 0 ),
396 +/* 76 */ V( 0x0f6b, 74, 77, 0 ),
397 +/* 77 */ V( 0x0d51, 75, 78, 0 ),
398 +/* 78 */ V( 0x0bb6, 77, 79, 0 ),
399 +/* 79 */ V( 0x0a40, 77, 48, 0 ),
400 +/* 80 */ V( 0x5832, 80, 81, 1 ),
401 +/* 81 */ V( 0x4d1c, 88, 82, 0 ),
402 +/* 82 */ V( 0x438e, 89, 83, 0 ),
403 +/* 83 */ V( 0x3bdd, 90, 84, 0 ),
404 +/* 84 */ V( 0x34ee, 91, 85, 0 ),
405 +/* 85 */ V( 0x2eae, 92, 86, 0 ),
406 +/* 86 */ V( 0x299a, 93, 87, 0 ),
407 +/* 87 */ V( 0x2516, 86, 71, 0 ),
408 +/* 88 */ V( 0x5570, 88, 89, 1 ),
409 +/* 89 */ V( 0x4ca9, 95, 90, 0 ),
410 +/* 90 */ V( 0x44d9, 96, 91, 0 ),
411 +/* 91 */ V( 0x3e22, 97, 92, 0 ),
412 +/* 92 */ V( 0x3824, 99, 93, 0 ),
413 +/* 93 */ V( 0x32b4, 99, 94, 0 ),
414 +/* 94 */ V( 0x2e17, 93, 86, 0 ),
415 +/* 95 */ V( 0x56a8, 95, 96, 1 ),
416 +/* 96 */ V( 0x4f46, 101, 97, 0 ),
417 +/* 97 */ V( 0x47e5, 102, 98, 0 ),
418 +/* 98 */ V( 0x41cf, 103, 99, 0 ),
419 +/* 99 */ V( 0x3c3d, 104, 100, 0 ),
420 +/* 100 */ V( 0x375e, 99, 93, 0 ),
421 +/* 101 */ V( 0x5231, 105, 102, 0 ),
422 +/* 102 */ V( 0x4c0f, 106, 103, 0 ),
423 +/* 103 */ V( 0x4639, 107, 104, 0 ),
424 +/* 104 */ V( 0x415e, 103, 99, 0 ),
425 +/* 105 */ V( 0x5627, 105, 106, 1 ),
426 +/* 106 */ V( 0x50e7, 108, 107, 0 ),
427 +/* 107 */ V( 0x4b85, 109, 103, 0 ),
428 +/* 108 */ V( 0x5597, 110, 109, 0 ),
429 +/* 109 */ V( 0x504f, 111, 107, 0 ),
430 +/* 110 */ V( 0x5a10, 110, 111, 1 ),
431 +/* 111 */ V( 0x5522, 112, 109, 0 ),
432 +/* 112 */ V( 0x59eb, 112, 111, 1 )
434 diff --git a/jdarith.c b/jdarith.c
436 index 0000000..1ef513e
443 + * Copyright (C) 1997, Guido Vollbeding <guivol@esc.de>.
444 + * This file is NOT part of the Independent JPEG Group's software
445 + * for legal reasons.
446 + * See the accompanying README file for conditions of distribution and use.
448 + * This file contains portable arithmetic entropy decoding routines for JPEG
449 + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
451 + * Both sequential and progressive modes are supported in this single module.
453 + * Suspension is not currently supported in this module.
456 +#define JPEG_INTERNALS
457 +#include "jinclude.h"
458 +#include "jpeglib.h"
461 +/* Expanded entropy decoder object for arithmetic decoding. */
464 + struct jpeg_entropy_decoder pub; /* public fields */
466 + INT32 c; /* C register, base of coding interval + input bit buffer */
467 + INT32 a; /* A register, normalized size of coding interval */
468 + int ct; /* bit shift counter, # of bits left in bit buffer part of C */
469 + /* init: ct = -16 */
470 + /* run: ct = 0..7 */
471 + /* error: ct = -1 */
472 + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
473 + int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
475 + unsigned int restarts_to_go; /* MCUs left in this restart interval */
477 + /* Pointers to statistics areas (these workspaces have image lifespan) */
478 + unsigned char * dc_stats[NUM_ARITH_TBLS];
479 + unsigned char * ac_stats[NUM_ARITH_TBLS];
480 +} arith_entropy_decoder;
482 +typedef arith_entropy_decoder * arith_entropy_ptr;
484 +/* The following two definitions specify the allocation chunk size
485 + * for the statistics area.
486 + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
487 + * 49 statistics bins for DC, and 245 statistics bins for AC coding.
488 + * Note that we use one additional AC bin for codings with fixed
489 + * probability (0.5), thus the minimum number for AC is 246.
491 + * We use a compact representation with 1 byte per statistics bin,
492 + * thus the numbers directly represent byte sizes.
493 + * This 1 byte per statistics bin contains the meaning of the MPS
494 + * (more probable symbol) in the highest bit (mask 0x80), and the
495 + * index into the probability estimation state machine table
496 + * in the lower bits (mask 0x7F).
499 +#define DC_STAT_BINS 64
500 +#define AC_STAT_BINS 256
504 +get_byte (j_decompress_ptr cinfo)
505 +/* Read next input byte; we do not support suspension in this module. */
507 + struct jpeg_source_mgr * src = cinfo->src;
509 + if (src->bytes_in_buffer == 0)
510 + if (! (*src->fill_input_buffer) (cinfo))
511 + ERREXIT(cinfo, JERR_CANT_SUSPEND);
512 + src->bytes_in_buffer--;
513 + return GETJOCTET(*src->next_input_byte++);
518 + * The core arithmetic decoding routine (common in JPEG and JBIG).
519 + * This needs to go as fast as possible.
520 + * Machine-dependent optimization facilities
521 + * are not utilized in this portable implementation.
522 + * However, this code should be fairly efficient and
523 + * may be a good base for further optimizations anyway.
525 + * Return value is 0 or 1 (binary decision).
527 + * Note: I've changed the handling of the code base & bit
528 + * buffer register C compared to other implementations
529 + * based on the standards layout & procedures.
530 + * While it also contains both the actual base of the
531 + * coding interval (16 bits) and the next-bits buffer,
532 + * the cut-point between these two parts is floating
533 + * (instead of fixed) with the bit shift counter CT.
534 + * Thus, we also need only one (variable instead of
535 + * fixed size) shift for the LPS/MPS decision, and
536 + * we can get away with any renormalization update
537 + * of C (except for new data insertion, of course).
539 + * I've also introduced a new scheme for accessing
540 + * the probability estimation state machine table,
541 + * derived from Markus Kuhn's JBIG implementation.
545 +arith_decode (j_decompress_ptr cinfo, unsigned char *st)
547 + extern const INT32 jaritab[];
548 + register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
549 + register unsigned char nl, nm;
550 + register INT32 qe, temp;
551 + register int sv, data;
553 + /* Renormalization & data input per section D.2.6 */
554 + while (e->a < 0x8000L) {
556 + /* Need to fetch next data byte */
557 + if (cinfo->unread_marker)
558 + data = 0; /* stuff zero data */
560 + data = get_byte(cinfo); /* read next input byte */
561 + if (data == 0xFF) { /* zero stuff or marker code */
562 + do data = get_byte(cinfo);
563 + while (data == 0xFF); /* swallow extra 0xFF bytes */
565 + data = 0xFF; /* discard stuffed zero byte */
567 + /* Note: Different from the Huffman decoder, hitting
568 + * a marker while processing the compressed data
569 + * segment is legal in arithmetic coding.
570 + * The convention is to supply zero data
571 + * then until decoding is complete.
573 + cinfo->unread_marker = data;
578 + e->c = (e->c << 8) | data; /* insert data into C register */
579 + if ((e->ct += 8) < 0) /* update bit shift counter */
580 + /* Need more initial bytes */
582 + /* Got 2 initial bytes -> re-init A and exit loop */
583 + e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
588 + /* Fetch values from our compact representation of Table D.2:
589 + * Qe values and probability estimation state machine
592 + qe = jaritab[sv & 0x7F]; /* => Qe_Value */
593 + nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
594 + nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
596 + /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
600 + if (e->c >= temp) {
602 + /* Conditional LPS (less probable symbol) exchange */
605 + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
608 + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
609 + sv ^= 0x80; /* Exchange LPS/MPS */
611 + } else if (e->a < 0x8000L) {
612 + /* Conditional MPS (more probable symbol) exchange */
614 + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
615 + sv ^= 0x80; /* Exchange LPS/MPS */
617 + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
626 + * Check for a restart marker & resynchronize decoder.
630 +process_restart (j_decompress_ptr cinfo)
632 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
634 + jpeg_component_info * compptr;
636 + /* Advance past the RSTn marker */
637 + if (! (*cinfo->marker->read_restart_marker) (cinfo))
638 + ERREXIT(cinfo, JERR_CANT_SUSPEND);
640 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
641 + compptr = cinfo->cur_comp_info[ci];
642 + /* Re-initialize statistics areas */
643 + if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
644 + MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
645 + /* Reset DC predictions to 0 */
646 + entropy->last_dc_val[ci] = 0;
647 + entropy->dc_context[ci] = 0;
649 + if (cinfo->progressive_mode == 0 || cinfo->Ss) {
650 + MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
654 + /* Reset arithmetic decoding variables */
657 + entropy->ct = -16; /* force reading 2 initial bytes to fill C */
659 + /* Reset restart counter */
660 + entropy->restarts_to_go = cinfo->restart_interval;
665 + * Arithmetic MCU decoding.
666 + * Each of these routines decodes and returns one MCU's worth of
667 + * arithmetic-compressed coefficients.
668 + * The coefficients are reordered from zigzag order into natural array order,
669 + * but are not dequantized.
671 + * The i'th block of the MCU is stored into the block pointed to by
672 + * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
676 + * MCU decoding for DC initial scan (either spectral selection,
677 + * or first pass of successive approximation).
681 +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
683 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
686 + int blkn, ci, tbl, sign;
689 + /* Process restart marker if needed */
690 + if (cinfo->restart_interval) {
691 + if (entropy->restarts_to_go == 0)
692 + process_restart(cinfo);
693 + entropy->restarts_to_go--;
696 + if (entropy->ct == -1) return TRUE; /* if error do nothing */
698 + /* Outer loop handles each block in the MCU */
700 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
701 + block = MCU_data[blkn];
702 + ci = cinfo->MCU_membership[blkn];
703 + tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
705 + /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
707 + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
708 + st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
710 + /* Figure F.19: Decode_DC_DIFF */
711 + if (arith_decode(cinfo, st) == 0)
712 + entropy->dc_context[ci] = 0;
714 + /* Figure F.21: Decoding nonzero value v */
715 + /* Figure F.22: Decoding the sign of v */
716 + sign = arith_decode(cinfo, st + 1);
717 + st += 2; st += sign;
718 + /* Figure F.23: Decoding the magnitude category of v */
719 + if ((m = arith_decode(cinfo, st)) != 0) {
720 + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
721 + while (arith_decode(cinfo, st)) {
722 + if ((m <<= 1) == 0x8000) {
723 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
724 + entropy->ct = -1; /* magnitude overflow */
730 + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
731 + if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
732 + entropy->dc_context[ci] = 0; /* zero diff category */
733 + else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
734 + entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
736 + entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
738 + /* Figure F.24: Decoding the magnitude bit pattern of v */
741 + if (arith_decode(cinfo, st)) v |= m;
742 + v += 1; if (sign) v = -v;
743 + entropy->last_dc_val[ci] += v;
746 + /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
747 + (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
755 + * MCU decoding for AC initial scan (either spectral selection,
756 + * or first pass of successive approximation).
760 +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
762 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
768 + /* Process restart marker if needed */
769 + if (cinfo->restart_interval) {
770 + if (entropy->restarts_to_go == 0)
771 + process_restart(cinfo);
772 + entropy->restarts_to_go--;
775 + if (entropy->ct == -1) return TRUE; /* if error do nothing */
777 + /* There is always only one block per MCU */
778 + block = MCU_data[0];
779 + tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
781 + /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
783 + /* Figure F.20: Decode_AC_coefficients */
784 + for (k = cinfo->Ss; k <= cinfo->Se; k++) {
785 + st = entropy->ac_stats[tbl] + 3 * (k - 1);
786 + if (arith_decode(cinfo, st)) break; /* EOB flag */
787 + while (arith_decode(cinfo, st + 1) == 0) {
789 + if (k > cinfo->Se) {
790 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
791 + entropy->ct = -1; /* spectral overflow */
795 + /* Figure F.21: Decoding nonzero value v */
796 + /* Figure F.22: Decoding the sign of v */
797 + entropy->ac_stats[tbl][245] = 0;
798 + sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245);
800 + /* Figure F.23: Decoding the magnitude category of v */
801 + if ((m = arith_decode(cinfo, st)) != 0) {
802 + if (arith_decode(cinfo, st)) {
804 + st = entropy->ac_stats[tbl] +
805 + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
806 + while (arith_decode(cinfo, st)) {
807 + if ((m <<= 1) == 0x8000) {
808 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
809 + entropy->ct = -1; /* magnitude overflow */
817 + /* Figure F.24: Decoding the magnitude bit pattern of v */
820 + if (arith_decode(cinfo, st)) v |= m;
821 + v += 1; if (sign) v = -v;
822 + /* Scale and output coefficient in natural (dezigzagged) order */
823 + (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
831 + * MCU decoding for DC successive approximation refinement scan.
835 +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
837 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
838 + unsigned char st[4];
841 + /* Process restart marker if needed */
842 + if (cinfo->restart_interval) {
843 + if (entropy->restarts_to_go == 0)
844 + process_restart(cinfo);
845 + entropy->restarts_to_go--;
848 + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
850 + /* Outer loop handles each block in the MCU */
852 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
853 + st[0] = 0; /* use fixed probability estimation */
854 + /* Encoded data is simply the next bit of the two's-complement DC value */
855 + if (arith_decode(cinfo, st))
856 + MCU_data[blkn][0][0] |= p1;
864 + * MCU decoding for AC successive approximation refinement scan.
868 +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
870 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
877 + /* Process restart marker if needed */
878 + if (cinfo->restart_interval) {
879 + if (entropy->restarts_to_go == 0)
880 + process_restart(cinfo);
881 + entropy->restarts_to_go--;
884 + if (entropy->ct == -1) return TRUE; /* if error do nothing */
886 + /* There is always only one block per MCU */
887 + block = MCU_data[0];
888 + tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
890 + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
891 + m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
893 + /* Establish EOBx (previous stage end-of-block) index */
894 + for (kex = cinfo->Se + 1; kex > 1; kex--)
895 + if ((*block)[jpeg_natural_order[kex - 1]]) break;
897 + for (k = cinfo->Ss; k <= cinfo->Se; k++) {
898 + st = entropy->ac_stats[tbl] + 3 * (k - 1);
900 + if (arith_decode(cinfo, st)) break; /* EOB flag */
902 + thiscoef = *block + jpeg_natural_order[k];
903 + if (*thiscoef) { /* previously nonzero coef */
904 + if (arith_decode(cinfo, st + 2))
911 + if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
912 + entropy->ac_stats[tbl][245] = 0;
913 + if (arith_decode(cinfo, entropy->ac_stats[tbl] + 245))
920 + if (k > cinfo->Se) {
921 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
922 + entropy->ct = -1; /* spectral overflow */
933 + * Decode one MCU's worth of arithmetic-compressed coefficients.
937 +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
939 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
940 + jpeg_component_info * compptr;
943 + int blkn, ci, tbl, sign, k;
946 + /* Process restart marker if needed */
947 + if (cinfo->restart_interval) {
948 + if (entropy->restarts_to_go == 0)
949 + process_restart(cinfo);
950 + entropy->restarts_to_go--;
953 + if (entropy->ct == -1) return TRUE; /* if error do nothing */
955 + /* Outer loop handles each block in the MCU */
957 + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
958 + block = MCU_data[blkn];
959 + ci = cinfo->MCU_membership[blkn];
960 + compptr = cinfo->cur_comp_info[ci];
962 + /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
964 + tbl = compptr->dc_tbl_no;
966 + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
967 + st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
969 + /* Figure F.19: Decode_DC_DIFF */
970 + if (arith_decode(cinfo, st) == 0)
971 + entropy->dc_context[ci] = 0;
973 + /* Figure F.21: Decoding nonzero value v */
974 + /* Figure F.22: Decoding the sign of v */
975 + sign = arith_decode(cinfo, st + 1);
976 + st += 2; st += sign;
977 + /* Figure F.23: Decoding the magnitude category of v */
978 + if ((m = arith_decode(cinfo, st)) != 0) {
979 + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
980 + while (arith_decode(cinfo, st)) {
981 + if ((m <<= 1) == 0x8000) {
982 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
983 + entropy->ct = -1; /* magnitude overflow */
989 + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
990 + if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
991 + entropy->dc_context[ci] = 0; /* zero diff category */
992 + else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
993 + entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
995 + entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
997 + /* Figure F.24: Decoding the magnitude bit pattern of v */
1000 + if (arith_decode(cinfo, st)) v |= m;
1001 + v += 1; if (sign) v = -v;
1002 + entropy->last_dc_val[ci] += v;
1005 + (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
1007 + /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
1009 + tbl = compptr->ac_tbl_no;
1011 + /* Figure F.20: Decode_AC_coefficients */
1012 + for (k = 1; k < DCTSIZE2; k++) {
1013 + st = entropy->ac_stats[tbl] + 3 * (k - 1);
1014 + if (arith_decode(cinfo, st)) break; /* EOB flag */
1015 + while (arith_decode(cinfo, st + 1) == 0) {
1017 + if (k >= DCTSIZE2) {
1018 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
1019 + entropy->ct = -1; /* spectral overflow */
1023 + /* Figure F.21: Decoding nonzero value v */
1024 + /* Figure F.22: Decoding the sign of v */
1025 + entropy->ac_stats[tbl][245] = 0;
1026 + sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245);
1028 + /* Figure F.23: Decoding the magnitude category of v */
1029 + if ((m = arith_decode(cinfo, st)) != 0) {
1030 + if (arith_decode(cinfo, st)) {
1032 + st = entropy->ac_stats[tbl] +
1033 + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
1034 + while (arith_decode(cinfo, st)) {
1035 + if ((m <<= 1) == 0x8000) {
1036 + WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
1037 + entropy->ct = -1; /* magnitude overflow */
1045 + /* Figure F.24: Decoding the magnitude bit pattern of v */
1048 + if (arith_decode(cinfo, st)) v |= m;
1049 + v += 1; if (sign) v = -v;
1050 + (*block)[jpeg_natural_order[k]] = (JCOEF) v;
1059 + * Initialize for an arithmetic-compressed scan.
1063 +start_pass (j_decompress_ptr cinfo)
1065 + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
1067 + jpeg_component_info * compptr;
1069 + if (cinfo->progressive_mode) {
1070 + /* Validate progressive scan parameters */
1071 + if (cinfo->Ss == 0) {
1072 + if (cinfo->Se != 0)
1075 + /* need not check Ss/Se < 0 since they came from unsigned bytes */
1076 + if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2)
1078 + /* AC scans may have only one component */
1079 + if (cinfo->comps_in_scan != 1)
1082 + if (cinfo->Ah != 0) {
1083 + /* Successive approximation refinement scan: must have Al = Ah-1. */
1084 + if (cinfo->Ah-1 != cinfo->Al)
1087 + if (cinfo->Al > 13) { /* need not check for < 0 */
1089 + ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
1090 + cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
1092 + /* Update progression status, and verify that scan order is legal.
1093 + * Note that inter-scan inconsistencies are treated as warnings
1094 + * not fatal errors ... not clear if this is right way to behave.
1096 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1097 + int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
1098 + int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
1099 + if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
1100 + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
1101 + for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
1102 + int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
1103 + if (cinfo->Ah != expected)
1104 + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
1105 + coef_bit_ptr[coefi] = cinfo->Al;
1108 + /* Select MCU decoding routine */
1109 + if (cinfo->Ah == 0) {
1110 + if (cinfo->Ss == 0)
1111 + entropy->pub.decode_mcu = decode_mcu_DC_first;
1113 + entropy->pub.decode_mcu = decode_mcu_AC_first;
1115 + if (cinfo->Ss == 0)
1116 + entropy->pub.decode_mcu = decode_mcu_DC_refine;
1118 + entropy->pub.decode_mcu = decode_mcu_AC_refine;
1121 + /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
1122 + * This ought to be an error condition, but we make it a warning because
1123 + * there are some baseline files out there with all zeroes in these bytes.
1125 + if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
1126 + cinfo->Ah != 0 || cinfo->Al != 0)
1127 + WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
1128 + /* Select MCU decoding routine */
1129 + entropy->pub.decode_mcu = decode_mcu;
1132 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1133 + compptr = cinfo->cur_comp_info[ci];
1134 + /* Allocate & initialize requested statistics areas */
1135 + if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
1136 + tbl = compptr->dc_tbl_no;
1137 + if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
1138 + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
1139 + if (entropy->dc_stats[tbl] == NULL)
1140 + entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
1141 + ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
1142 + MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
1143 + /* Initialize DC predictions to 0 */
1144 + entropy->last_dc_val[ci] = 0;
1145 + entropy->dc_context[ci] = 0;
1147 + if (cinfo->progressive_mode == 0 || cinfo->Ss) {
1148 + tbl = compptr->ac_tbl_no;
1149 + if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
1150 + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
1151 + if (entropy->ac_stats[tbl] == NULL)
1152 + entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
1153 + ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
1154 + MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
1158 + /* Initialize arithmetic decoding variables */
1161 + entropy->ct = -16; /* force reading 2 initial bytes to fill C */
1163 + /* Initialize restart counter */
1164 + entropy->restarts_to_go = cinfo->restart_interval;
1169 + * Module initialization routine for arithmetic entropy decoding.
1173 +jinit_arith_decoder (j_decompress_ptr cinfo)
1175 + arith_entropy_ptr entropy;
1178 + entropy = (arith_entropy_ptr)
1179 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1180 + SIZEOF(arith_entropy_decoder));
1181 + cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
1182 + entropy->pub.start_pass = start_pass;
1184 + /* Mark tables unallocated */
1185 + for (i = 0; i < NUM_ARITH_TBLS; i++) {
1186 + entropy->dc_stats[i] = NULL;
1187 + entropy->ac_stats[i] = NULL;
1190 + if (cinfo->progressive_mode) {
1191 + /* Create progression status table */
1192 + int *coef_bit_ptr, ci;
1193 + cinfo->coef_bits = (int (*)[DCTSIZE2])
1194 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1195 + cinfo->num_components*DCTSIZE2*SIZEOF(int));
1196 + coef_bit_ptr = & cinfo->coef_bits[0][0];
1197 + for (ci = 0; ci < cinfo->num_components; ci++)
1198 + for (i = 0; i < DCTSIZE2; i++)
1199 + *coef_bit_ptr++ = -1;
1202 diff --git a/jdmaster.c b/jdmaster.c
1203 index 8314b67..537abc7 100644
1206 @@ -384,7 +384,7 @@ master_selection (j_decompress_ptr cinfo)
1207 jinit_inverse_dct(cinfo);
1208 /* Entropy decoding: either Huffman or arithmetic coding. */
1209 if (cinfo->arith_code) {
1210 - ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
1211 + jinit_arith_decoder(cinfo);
1213 if (cinfo->progressive_mode) {
1214 #ifdef D_PROGRESSIVE_SUPPORTED
1215 diff --git a/jdtrans.c b/jdtrans.c
1216 index 6c0ab71..fcc9ae9 100644
1219 @@ -101,7 +101,7 @@ transdecode_master_selection (j_decompress_ptr cinfo)
1221 /* Entropy decoding: either Huffman or arithmetic coding. */
1222 if (cinfo->arith_code) {
1223 - ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
1224 + jinit_arith_decoder(cinfo);
1226 if (cinfo->progressive_mode) {
1227 #ifdef D_PROGRESSIVE_SUPPORTED
1228 diff --git a/jerror.h b/jerror.h
1229 index fc2fffe..109e3d3 100644
1232 @@ -93,6 +93,7 @@ JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
1233 JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
1234 JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
1235 JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
1236 +JMESSAGE(JERR_NO_ARITH_TABLE, "Arithmetic table 0x%02x was not defined")
1237 JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
1238 JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
1239 JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
1240 @@ -170,6 +171,7 @@ JMESSAGE(JTRC_UNKNOWN_IDS,
1241 JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
1242 JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
1243 JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
1244 +JMESSAGE(JWRN_ARITH_BAD_CODE, "Corrupt JPEG data: bad arithmetic code")
1245 JMESSAGE(JWRN_BOGUS_PROGRESSION,
1246 "Inconsistent progression sequence for component %d coefficient %d")
1247 JMESSAGE(JWRN_EXTRANEOUS_DATA,
1248 diff --git a/jmorecfg.h b/jmorecfg.h
1249 index 0e7fb72..4d66335 100644
1252 @@ -283,7 +283,7 @@ typedef int boolean;
1254 /* Decoder capability options: */
1256 -#undef D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
1257 +#define D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */
1258 #define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */
1259 #define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/
1260 #define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */
1261 diff --git a/jpegint.h b/jpegint.h
1262 index 7a31f51..78bb1cf 100644
1265 @@ -313,6 +313,7 @@ struct jpeg_color_quantizer {
1266 #define jinit_marker_reader jIMReader
1267 #define jinit_huff_decoder jIHDecoder
1268 #define jinit_phuff_decoder jIPHDecoder
1269 +#define jinit_arith_decoder jIADecoder
1270 #define jinit_inverse_dct jIIDCT
1271 #define jinit_upsampler jIUpsampler
1272 #define jinit_color_deconverter jIDColor
1273 @@ -358,6 +359,7 @@ EXTERN(void) jinit_input_controller JPP((j_decompress_ptr cinfo));
1274 EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo));
1275 EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo));
1276 EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo));
1277 +EXTERN(void) jinit_arith_decoder JPP((j_decompress_ptr cinfo));
1278 EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo));
1279 EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo));
1280 EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo));