]> git.pld-linux.org Git - packages/libjpeg-turbo.git/blame - libjpeg-turbo10-rh639672.patch
- updated to 2.0.6
[packages/libjpeg-turbo.git] / libjpeg-turbo10-rh639672.patch
CommitLineData
8740afba
AM
1From 195345e454d3df7ff9c07c3d36881ec52e789af2 Mon Sep 17 00:00:00 2001
2From: Mukund Sivaraman <muks@banu.com>
3Date: Sun, 3 Oct 2010 12:23:41 +0530
4Subject: [PATCH] Add support for decoding arithmetic coded content
5
6This is based on a patch by Guido Vollbeding <guivol@esc.de>, which
7included code to both encode and decode arithmetic coded content.
8
9This patch only adds the decoding portion. No new arithmetic coded
10content can be produced, but existing arithmetic coded content can
11be decoded.
12
13libjpeg-turbo with this patch has been tested with some arithmetic
14coded images (digital photographs).
15---
16 Makefile.am | 17 +-
17 README.arithmetic | 215 +++++++++++++++
18 jaricom.c | 149 +++++++++++
19 jdarith.c | 762 +++++++++++++++++++++++++++++++++++++++++++++++++++++
20 jdmaster.c | 2 +-
21 jdtrans.c | 2 +-
22 jerror.h | 2 +
23 jmorecfg.h | 2 +-
24 jpegint.h | 2 +
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
29
30diff --git a/Makefile.am b/Makefile.am
31index f8552ea..1eee598 100644
32--- a/Makefile.am
33+++ b/Makefile.am
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
37
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
55
56 libturbojpeg_la_SOURCES = $(libjpeg_la_SOURCES) turbojpegl.c turbojpeg.h \
57 turbojpeg-mapfile
58diff --git a/README.arithmetic b/README.arithmetic
59new file mode 100644
60index 0000000..0dd3b9a
61--- /dev/null
62+++ b/README.arithmetic
63@@ -0,0 +1,215 @@
64+JPEG arithmetic encoding and decoding portable software implementation
65+======================================================================
66+
67+Release of 28-Mar-98 by Guido Vollbeding <guido@jpegclub.org>
68+=============================================================
69+
70+Primary URLs:
71+
72+ http://sylvana.net/jpeg-ari/
73+ (directory containing the actual archive files:)
74+
75+ http://sylvana.net/jpeg-ari/jpeg-ari-28mar98.tar.gz
76+
77+ http://sylvana.net/jpeg-ari/jpeg-ari.zip
78+
79+
80+DISCLAIMER
81+==========
82+
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.
86+
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
94+a license yourself.
95+
96+See Annex L in the JPEG spec for further information
97+and a list of relevant patents.
98+
99+
100+What is it?
101+===========
102+
103+This is my implementation of the arithmetic encoding and decoding
104+back-end for JPEG as specified in the
105+
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
109+ and Guidelines".
110+
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.
114+
115+The JPEG standard specifies a particular arithmetic coding scheme
116+to be used optionally as alternative to Huffman coding.
117+
118+
119+Who needs it?
120+=============
121+
122+This package might be of interest for people who are looking for
123+enhanced state-of-the-art image compression technologies.
124+
125+It is intended to provide a reasonable tool for experimental,
126+comparison and evaluation purposes.
127+
128+See the Disclaimer above for restricted conditions of usage.
129+
130+
131+How does it work?
132+=================
133+
134+This distribution is organized as add-on to the widespread
135+Independent JPEG Group's JPEG software.
136+
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.
141+
142+Transcode given JPEG files simply with a command like
143+
144+ jpegtran -arithmetic [-progressive] < orig.jpg > arit.jpg
145+
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
149+
150+ jpegtran [-optimize] [-progressive] < arit.jpg > orig2.jpg
151+
152+to verify correct operation.
153+
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.
157+
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.
162+
163+Here are some actual results:
164+
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
176+% ls -l test*.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
194+%
195+
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
201+is 5-10%.
202+
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.
210+
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.
214+
215+
216+Installation
217+============
218+
219+The installation is a 2-stage procedure:
220+
221+1. Preparing the IJG package for potential incorporation
222+ of the arithmetic coding feature.
223+
224+2. Incorporation of the actual arithmetic coding modules
225+ and enabling the feature for usage.
226+
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.
231+
232+Step 1:
233+
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.
238+
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).
243+
244+1.3. Recompile ('make').
245+
246+See the file 'PATCHES' in 'patchv6b' for details.
247+
248+Step 2:
249+
250+2.1. Replace the 3 placeholder files by the actual implementation
251+ modules.
252+
253+2.2. Enable application support of the new features by #defining
254+ C_ARITH_CODING_SUPPORTED and D_ARITH_CODING_SUPPORTED
255+ in 'jmorecfg.h'.
256+
257+2.3. Recompile ('make').
258+
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.
263+
264+
265+References
266+==========
267+
268+- The Independent JPEG Group's software
269+
270+- JBIG-KIT lossless image compression library by Markus Kuhn
271+
272+- William B. Pennebaker, Joan L. Mitchell:
273+ "JPEG Still Image Data Compression Standard",
274+ Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
275+
276+- jpeg-faq (http://www.faqs.org/faqs/jpeg-faq/)
277+
278+- compression-faq (http://www.faqs.org/faqs/compression-faq/)
279diff --git a/jaricom.c b/jaricom.c
280new file mode 100644
281index 0000000..e4292b0
282--- /dev/null
283+++ b/jaricom.c
284@@ -0,0 +1,149 @@
285+/*
286+ * jaricom.c
287+ *
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.
292+ *
293+ * This file contains probability estimation tables for common use in
294+ * arithmetic entropy encoding and decoding routines.
295+ *
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).
299+ */
300+
301+#define JPEG_INTERNALS
302+#include "jinclude.h"
303+#include "jpeglib.h"
304+
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).
312+ */
313+
314+#define V(a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b)
315+
316+const INT32 jaritab[113] = {
317+/*
318+ * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS
319+ */
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 )
433+};
434diff --git a/jdarith.c b/jdarith.c
435new file mode 100644
436index 0000000..1ef513e
437--- /dev/null
438+++ b/jdarith.c
439@@ -0,0 +1,762 @@
440+/*
441+ * jdarith.c
442+ *
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.
447+ *
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).
450+ *
451+ * Both sequential and progressive modes are supported in this single module.
452+ *
453+ * Suspension is not currently supported in this module.
454+ */
455+
456+#define JPEG_INTERNALS
457+#include "jinclude.h"
458+#include "jpeglib.h"
459+
460+
461+/* Expanded entropy decoder object for arithmetic decoding. */
462+
463+typedef struct {
464+ struct jpeg_entropy_decoder pub; /* public fields */
465+
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 */
474+
475+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
476+
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;
481+
482+typedef arith_entropy_decoder * arith_entropy_ptr;
483+
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.
490+ *
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).
497+ */
498+
499+#define DC_STAT_BINS 64
500+#define AC_STAT_BINS 256
501+
502+
503+LOCAL(int)
504+get_byte (j_decompress_ptr cinfo)
505+/* Read next input byte; we do not support suspension in this module. */
506+{
507+ struct jpeg_source_mgr * src = cinfo->src;
508+
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++);
514+}
515+
516+
517+/*
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.
524+ *
525+ * Return value is 0 or 1 (binary decision).
526+ *
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).
538+ *
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.
542+ */
543+
544+LOCAL(int)
545+arith_decode (j_decompress_ptr cinfo, unsigned char *st)
546+{
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;
552+
553+ /* Renormalization & data input per section D.2.6 */
554+ while (e->a < 0x8000L) {
555+ if (--e->ct < 0) {
556+ /* Need to fetch next data byte */
557+ if (cinfo->unread_marker)
558+ data = 0; /* stuff zero data */
559+ else {
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 */
564+ if (data == 0)
565+ data = 0xFF; /* discard stuffed zero byte */
566+ else {
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.
572+ */
573+ cinfo->unread_marker = data;
574+ data = 0;
575+ }
576+ }
577+ }
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 */
581+ if (++e->ct == 0)
582+ /* Got 2 initial bytes -> re-init A and exit loop */
583+ e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
584+ }
585+ e->a <<= 1;
586+ }
587+
588+ /* Fetch values from our compact representation of Table D.2:
589+ * Qe values and probability estimation state machine
590+ */
591+ sv = *st;
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 */
595+
596+ /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
597+ temp = e->a - qe;
598+ e->a = temp;
599+ temp <<= e->ct;
600+ if (e->c >= temp) {
601+ e->c -= temp;
602+ /* Conditional LPS (less probable symbol) exchange */
603+ if (e->a < qe) {
604+ e->a = qe;
605+ *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
606+ } else {
607+ e->a = qe;
608+ *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
609+ sv ^= 0x80; /* Exchange LPS/MPS */
610+ }
611+ } else if (e->a < 0x8000L) {
612+ /* Conditional MPS (more probable symbol) exchange */
613+ if (e->a < qe) {
614+ *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
615+ sv ^= 0x80; /* Exchange LPS/MPS */
616+ } else {
617+ *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
618+ }
619+ }
620+
621+ return sv >> 7;
622+}
623+
624+
625+/*
626+ * Check for a restart marker & resynchronize decoder.
627+ */
628+
629+LOCAL(void)
630+process_restart (j_decompress_ptr cinfo)
631+{
632+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
633+ int ci;
634+ jpeg_component_info * compptr;
635+
636+ /* Advance past the RSTn marker */
637+ if (! (*cinfo->marker->read_restart_marker) (cinfo))
638+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
639+
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;
648+ }
649+ if (cinfo->progressive_mode == 0 || cinfo->Ss) {
650+ MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
651+ }
652+ }
653+
654+ /* Reset arithmetic decoding variables */
655+ entropy->c = 0;
656+ entropy->a = 0;
657+ entropy->ct = -16; /* force reading 2 initial bytes to fill C */
658+
659+ /* Reset restart counter */
660+ entropy->restarts_to_go = cinfo->restart_interval;
661+}
662+
663+
664+/*
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.
670+ *
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.
673+ */
674+
675+/*
676+ * MCU decoding for DC initial scan (either spectral selection,
677+ * or first pass of successive approximation).
678+ */
679+
680+METHODDEF(boolean)
681+decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
682+{
683+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
684+ JBLOCKROW block;
685+ unsigned char *st;
686+ int blkn, ci, tbl, sign;
687+ int v, m;
688+
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--;
694+ }
695+
696+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
697+
698+ /* Outer loop handles each block in the MCU */
699+
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;
704+
705+ /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
706+
707+ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
708+ st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
709+
710+ /* Figure F.19: Decode_DC_DIFF */
711+ if (arith_decode(cinfo, st) == 0)
712+ entropy->dc_context[ci] = 0;
713+ else {
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 */
725+ return TRUE;
726+ }
727+ st += 1;
728+ }
729+ }
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 */
735+ else
736+ entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
737+ v = m;
738+ /* Figure F.24: Decoding the magnitude bit pattern of v */
739+ st += 14;
740+ while (m >>= 1)
741+ if (arith_decode(cinfo, st)) v |= m;
742+ v += 1; if (sign) v = -v;
743+ entropy->last_dc_val[ci] += v;
744+ }
745+
746+ /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
747+ (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
748+ }
749+
750+ return TRUE;
751+}
752+
753+
754+/*
755+ * MCU decoding for AC initial scan (either spectral selection,
756+ * or first pass of successive approximation).
757+ */
758+
759+METHODDEF(boolean)
760+decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
761+{
762+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
763+ JBLOCKROW block;
764+ unsigned char *st;
765+ int tbl, sign, k;
766+ int v, m;
767+
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--;
773+ }
774+
775+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
776+
777+ /* There is always only one block per MCU */
778+ block = MCU_data[0];
779+ tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
780+
781+ /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
782+
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) {
788+ st += 3; k++;
789+ if (k > cinfo->Se) {
790+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
791+ entropy->ct = -1; /* spectral overflow */
792+ return TRUE;
793+ }
794+ }
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);
799+ st += 2;
800+ /* Figure F.23: Decoding the magnitude category of v */
801+ if ((m = arith_decode(cinfo, st)) != 0) {
802+ if (arith_decode(cinfo, st)) {
803+ m <<= 1;
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 */
810+ return TRUE;
811+ }
812+ st += 1;
813+ }
814+ }
815+ }
816+ v = m;
817+ /* Figure F.24: Decoding the magnitude bit pattern of v */
818+ st += 14;
819+ while (m >>= 1)
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);
824+ }
825+
826+ return TRUE;
827+}
828+
829+
830+/*
831+ * MCU decoding for DC successive approximation refinement scan.
832+ */
833+
834+METHODDEF(boolean)
835+decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
836+{
837+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
838+ unsigned char st[4];
839+ int p1, blkn;
840+
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--;
846+ }
847+
848+ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
849+
850+ /* Outer loop handles each block in the MCU */
851+
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;
857+ }
858+
859+ return TRUE;
860+}
861+
862+
863+/*
864+ * MCU decoding for AC successive approximation refinement scan.
865+ */
866+
867+METHODDEF(boolean)
868+decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
869+{
870+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
871+ JBLOCKROW block;
872+ JCOEFPTR thiscoef;
873+ unsigned char *st;
874+ int tbl, k, kex;
875+ int p1, m1;
876+
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--;
882+ }
883+
884+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
885+
886+ /* There is always only one block per MCU */
887+ block = MCU_data[0];
888+ tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
889+
890+ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
891+ m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
892+
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;
896+
897+ for (k = cinfo->Ss; k <= cinfo->Se; k++) {
898+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
899+ if (k >= kex)
900+ if (arith_decode(cinfo, st)) break; /* EOB flag */
901+ for (;;) {
902+ thiscoef = *block + jpeg_natural_order[k];
903+ if (*thiscoef) { /* previously nonzero coef */
904+ if (arith_decode(cinfo, st + 2))
905+ if (*thiscoef < 0)
906+ *thiscoef += m1;
907+ else
908+ *thiscoef += p1;
909+ break;
910+ }
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))
914+ *thiscoef = m1;
915+ else
916+ *thiscoef = p1;
917+ break;
918+ }
919+ st += 3; k++;
920+ if (k > cinfo->Se) {
921+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
922+ entropy->ct = -1; /* spectral overflow */
923+ return TRUE;
924+ }
925+ }
926+ }
927+
928+ return TRUE;
929+}
930+
931+
932+/*
933+ * Decode one MCU's worth of arithmetic-compressed coefficients.
934+ */
935+
936+METHODDEF(boolean)
937+decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
938+{
939+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
940+ jpeg_component_info * compptr;
941+ JBLOCKROW block;
942+ unsigned char *st;
943+ int blkn, ci, tbl, sign, k;
944+ int v, m;
945+
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--;
951+ }
952+
953+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
954+
955+ /* Outer loop handles each block in the MCU */
956+
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];
961+
962+ /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
963+
964+ tbl = compptr->dc_tbl_no;
965+
966+ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
967+ st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
968+
969+ /* Figure F.19: Decode_DC_DIFF */
970+ if (arith_decode(cinfo, st) == 0)
971+ entropy->dc_context[ci] = 0;
972+ else {
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 */
984+ return TRUE;
985+ }
986+ st += 1;
987+ }
988+ }
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 */
994+ else
995+ entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
996+ v = m;
997+ /* Figure F.24: Decoding the magnitude bit pattern of v */
998+ st += 14;
999+ while (m >>= 1)
1000+ if (arith_decode(cinfo, st)) v |= m;
1001+ v += 1; if (sign) v = -v;
1002+ entropy->last_dc_val[ci] += v;
1003+ }
1004+
1005+ (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
1006+
1007+ /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
1008+
1009+ tbl = compptr->ac_tbl_no;
1010+
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) {
1016+ st += 3; k++;
1017+ if (k >= DCTSIZE2) {
1018+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
1019+ entropy->ct = -1; /* spectral overflow */
1020+ return TRUE;
1021+ }
1022+ }
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);
1027+ st += 2;
1028+ /* Figure F.23: Decoding the magnitude category of v */
1029+ if ((m = arith_decode(cinfo, st)) != 0) {
1030+ if (arith_decode(cinfo, st)) {
1031+ m <<= 1;
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 */
1038+ return TRUE;
1039+ }
1040+ st += 1;
1041+ }
1042+ }
1043+ }
1044+ v = m;
1045+ /* Figure F.24: Decoding the magnitude bit pattern of v */
1046+ st += 14;
1047+ while (m >>= 1)
1048+ if (arith_decode(cinfo, st)) v |= m;
1049+ v += 1; if (sign) v = -v;
1050+ (*block)[jpeg_natural_order[k]] = (JCOEF) v;
1051+ }
1052+ }
1053+
1054+ return TRUE;
1055+}
1056+
1057+
1058+/*
1059+ * Initialize for an arithmetic-compressed scan.
1060+ */
1061+
1062+METHODDEF(void)
1063+start_pass (j_decompress_ptr cinfo)
1064+{
1065+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
1066+ int ci, tbl;
1067+ jpeg_component_info * compptr;
1068+
1069+ if (cinfo->progressive_mode) {
1070+ /* Validate progressive scan parameters */
1071+ if (cinfo->Ss == 0) {
1072+ if (cinfo->Se != 0)
1073+ goto bad;
1074+ } else {
1075+ /* need not check Ss/Se < 0 since they came from unsigned bytes */
1076+ if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2)
1077+ goto bad;
1078+ /* AC scans may have only one component */
1079+ if (cinfo->comps_in_scan != 1)
1080+ goto bad;
1081+ }
1082+ if (cinfo->Ah != 0) {
1083+ /* Successive approximation refinement scan: must have Al = Ah-1. */
1084+ if (cinfo->Ah-1 != cinfo->Al)
1085+ goto bad;
1086+ }
1087+ if (cinfo->Al > 13) { /* need not check for < 0 */
1088+ bad:
1089+ ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
1090+ cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
1091+ }
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.
1095+ */
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;
1106+ }
1107+ }
1108+ /* Select MCU decoding routine */
1109+ if (cinfo->Ah == 0) {
1110+ if (cinfo->Ss == 0)
1111+ entropy->pub.decode_mcu = decode_mcu_DC_first;
1112+ else
1113+ entropy->pub.decode_mcu = decode_mcu_AC_first;
1114+ } else {
1115+ if (cinfo->Ss == 0)
1116+ entropy->pub.decode_mcu = decode_mcu_DC_refine;
1117+ else
1118+ entropy->pub.decode_mcu = decode_mcu_AC_refine;
1119+ }
1120+ } else {
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.
1124+ */
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;
1130+ }
1131+
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;
1146+ }
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);
1155+ }
1156+ }
1157+
1158+ /* Initialize arithmetic decoding variables */
1159+ entropy->c = 0;
1160+ entropy->a = 0;
1161+ entropy->ct = -16; /* force reading 2 initial bytes to fill C */
1162+
1163+ /* Initialize restart counter */
1164+ entropy->restarts_to_go = cinfo->restart_interval;
1165+}
1166+
1167+
1168+/*
1169+ * Module initialization routine for arithmetic entropy decoding.
1170+ */
1171+
1172+GLOBAL(void)
1173+jinit_arith_decoder (j_decompress_ptr cinfo)
1174+{
1175+ arith_entropy_ptr entropy;
1176+ int i;
1177+
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;
1183+
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;
1188+ }
1189+
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;
1200+ }
1201+}
1202diff --git a/jdmaster.c b/jdmaster.c
1203index 8314b67..537abc7 100644
1204--- a/jdmaster.c
1205+++ b/jdmaster.c
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);
1212 } else {
1213 if (cinfo->progressive_mode) {
1214 #ifdef D_PROGRESSIVE_SUPPORTED
1215diff --git a/jdtrans.c b/jdtrans.c
1216index 6c0ab71..fcc9ae9 100644
1217--- a/jdtrans.c
1218+++ b/jdtrans.c
1219@@ -101,7 +101,7 @@ transdecode_master_selection (j_decompress_ptr cinfo)
1220
1221 /* Entropy decoding: either Huffman or arithmetic coding. */
1222 if (cinfo->arith_code) {
1223- ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
1224+ jinit_arith_decoder(cinfo);
1225 } else {
1226 if (cinfo->progressive_mode) {
1227 #ifdef D_PROGRESSIVE_SUPPORTED
1228diff --git a/jerror.h b/jerror.h
1229index fc2fffe..109e3d3 100644
1230--- a/jerror.h
1231+++ b/jerror.h
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,
1248diff --git a/jmorecfg.h b/jmorecfg.h
1249index 0e7fb72..4d66335 100644
1250--- a/jmorecfg.h
1251+++ b/jmorecfg.h
1252@@ -283,7 +283,7 @@ typedef int boolean;
1253
1254 /* Decoder capability options: */
1255
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? */
1261diff --git a/jpegint.h b/jpegint.h
1262index 7a31f51..78bb1cf 100644
1263--- a/jpegint.h
1264+++ b/jpegint.h
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));
1281--
12821.7.2.3
1283
This page took 0.290879 seconds and 4 git commands to generate.