]>
Commit | Line | Data |
---|---|---|
00432c94 JB |
1 | diff -Nur jpeg-6b.orig/jaricom.c jpeg-6b/jaricom.c |
2 | --- jpeg-6b.orig/jaricom.c 1970-01-01 01:00:00.000000000 +0100 | |
3 | +++ jpeg-6b/jaricom.c 1997-08-10 18:40:45.000000000 +0200 | |
4 | @@ -0,0 +1,149 @@ | |
5 | +/* | |
6 | + * jaricom.c | |
7 | + * | |
8 | + * Copyright (C) 1997, Guido Vollbeding <guivol@esc.de>. | |
9 | + * This file is NOT part of the Independent JPEG Group's software | |
10 | + * for legal reasons. | |
11 | + * See the accompanying README file for conditions of distribution and use. | |
12 | + * | |
13 | + * This file contains probability estimation tables for common use in | |
14 | + * arithmetic entropy encoding and decoding routines. | |
15 | + * | |
16 | + * This data represents Table D.2 in the JPEG spec (ISO/IEC IS 10918-1 | |
17 | + * and CCITT Recommendation ITU-T T.81) and Table 24 in the JBIG spec | |
18 | + * (ISO/IEC IS 11544 and CCITT Recommendation ITU-T T.82). | |
19 | + */ | |
20 | + | |
21 | +#define JPEG_INTERNALS | |
22 | +#include "jinclude.h" | |
23 | +#include "jpeglib.h" | |
24 | + | |
25 | +/* The following #define specifies the packing of the four components | |
26 | + * into the compact INT32 representation. | |
27 | + * Note that this formula must match the actual arithmetic encoder | |
28 | + * and decoder implementation. The implementation has to be changed | |
29 | + * if this formula is changed. | |
30 | + * The current organisation is leaned on Markus Kuhn's JBIG | |
31 | + * implementation (jbig_tab.c). | |
32 | + */ | |
33 | + | |
34 | +#define V(a,b,c,d) (((INT32)a << 16) | ((INT32)c << 8) | ((INT32)d << 7) | b) | |
35 | + | |
36 | +const INT32 jaritab[113] = { | |
37 | +/* | |
38 | + * Index, Qe_Value, Next_Index_LPS, Next_Index_MPS, Switch_MPS | |
39 | + */ | |
40 | +/* 0 */ V( 0x5a1d, 1, 1, 1 ), | |
41 | +/* 1 */ V( 0x2586, 14, 2, 0 ), | |
42 | +/* 2 */ V( 0x1114, 16, 3, 0 ), | |
43 | +/* 3 */ V( 0x080b, 18, 4, 0 ), | |
44 | +/* 4 */ V( 0x03d8, 20, 5, 0 ), | |
45 | +/* 5 */ V( 0x01da, 23, 6, 0 ), | |
46 | +/* 6 */ V( 0x00e5, 25, 7, 0 ), | |
47 | +/* 7 */ V( 0x006f, 28, 8, 0 ), | |
48 | +/* 8 */ V( 0x0036, 30, 9, 0 ), | |
49 | +/* 9 */ V( 0x001a, 33, 10, 0 ), | |
50 | +/* 10 */ V( 0x000d, 35, 11, 0 ), | |
51 | +/* 11 */ V( 0x0006, 9, 12, 0 ), | |
52 | +/* 12 */ V( 0x0003, 10, 13, 0 ), | |
53 | +/* 13 */ V( 0x0001, 12, 13, 0 ), | |
54 | +/* 14 */ V( 0x5a7f, 15, 15, 1 ), | |
55 | +/* 15 */ V( 0x3f25, 36, 16, 0 ), | |
56 | +/* 16 */ V( 0x2cf2, 38, 17, 0 ), | |
57 | +/* 17 */ V( 0x207c, 39, 18, 0 ), | |
58 | +/* 18 */ V( 0x17b9, 40, 19, 0 ), | |
59 | +/* 19 */ V( 0x1182, 42, 20, 0 ), | |
60 | +/* 20 */ V( 0x0cef, 43, 21, 0 ), | |
61 | +/* 21 */ V( 0x09a1, 45, 22, 0 ), | |
62 | +/* 22 */ V( 0x072f, 46, 23, 0 ), | |
63 | +/* 23 */ V( 0x055c, 48, 24, 0 ), | |
64 | +/* 24 */ V( 0x0406, 49, 25, 0 ), | |
65 | +/* 25 */ V( 0x0303, 51, 26, 0 ), | |
66 | +/* 26 */ V( 0x0240, 52, 27, 0 ), | |
67 | +/* 27 */ V( 0x01b1, 54, 28, 0 ), | |
68 | +/* 28 */ V( 0x0144, 56, 29, 0 ), | |
69 | +/* 29 */ V( 0x00f5, 57, 30, 0 ), | |
70 | +/* 30 */ V( 0x00b7, 59, 31, 0 ), | |
71 | +/* 31 */ V( 0x008a, 60, 32, 0 ), | |
72 | +/* 32 */ V( 0x0068, 62, 33, 0 ), | |
73 | +/* 33 */ V( 0x004e, 63, 34, 0 ), | |
74 | +/* 34 */ V( 0x003b, 32, 35, 0 ), | |
75 | +/* 35 */ V( 0x002c, 33, 9, 0 ), | |
76 | +/* 36 */ V( 0x5ae1, 37, 37, 1 ), | |
77 | +/* 37 */ V( 0x484c, 64, 38, 0 ), | |
78 | +/* 38 */ V( 0x3a0d, 65, 39, 0 ), | |
79 | +/* 39 */ V( 0x2ef1, 67, 40, 0 ), | |
80 | +/* 40 */ V( 0x261f, 68, 41, 0 ), | |
81 | +/* 41 */ V( 0x1f33, 69, 42, 0 ), | |
82 | +/* 42 */ V( 0x19a8, 70, 43, 0 ), | |
83 | +/* 43 */ V( 0x1518, 72, 44, 0 ), | |
84 | +/* 44 */ V( 0x1177, 73, 45, 0 ), | |
85 | +/* 45 */ V( 0x0e74, 74, 46, 0 ), | |
86 | +/* 46 */ V( 0x0bfb, 75, 47, 0 ), | |
87 | +/* 47 */ V( 0x09f8, 77, 48, 0 ), | |
88 | +/* 48 */ V( 0x0861, 78, 49, 0 ), | |
89 | +/* 49 */ V( 0x0706, 79, 50, 0 ), | |
90 | +/* 50 */ V( 0x05cd, 48, 51, 0 ), | |
91 | +/* 51 */ V( 0x04de, 50, 52, 0 ), | |
92 | +/* 52 */ V( 0x040f, 50, 53, 0 ), | |
93 | +/* 53 */ V( 0x0363, 51, 54, 0 ), | |
94 | +/* 54 */ V( 0x02d4, 52, 55, 0 ), | |
95 | +/* 55 */ V( 0x025c, 53, 56, 0 ), | |
96 | +/* 56 */ V( 0x01f8, 54, 57, 0 ), | |
97 | +/* 57 */ V( 0x01a4, 55, 58, 0 ), | |
98 | +/* 58 */ V( 0x0160, 56, 59, 0 ), | |
99 | +/* 59 */ V( 0x0125, 57, 60, 0 ), | |
100 | +/* 60 */ V( 0x00f6, 58, 61, 0 ), | |
101 | +/* 61 */ V( 0x00cb, 59, 62, 0 ), | |
102 | +/* 62 */ V( 0x00ab, 61, 63, 0 ), | |
103 | +/* 63 */ V( 0x008f, 61, 32, 0 ), | |
104 | +/* 64 */ V( 0x5b12, 65, 65, 1 ), | |
105 | +/* 65 */ V( 0x4d04, 80, 66, 0 ), | |
106 | +/* 66 */ V( 0x412c, 81, 67, 0 ), | |
107 | +/* 67 */ V( 0x37d8, 82, 68, 0 ), | |
108 | +/* 68 */ V( 0x2fe8, 83, 69, 0 ), | |
109 | +/* 69 */ V( 0x293c, 84, 70, 0 ), | |
110 | +/* 70 */ V( 0x2379, 86, 71, 0 ), | |
111 | +/* 71 */ V( 0x1edf, 87, 72, 0 ), | |
112 | +/* 72 */ V( 0x1aa9, 87, 73, 0 ), | |
113 | +/* 73 */ V( 0x174e, 72, 74, 0 ), | |
114 | +/* 74 */ V( 0x1424, 72, 75, 0 ), | |
115 | +/* 75 */ V( 0x119c, 74, 76, 0 ), | |
116 | +/* 76 */ V( 0x0f6b, 74, 77, 0 ), | |
117 | +/* 77 */ V( 0x0d51, 75, 78, 0 ), | |
118 | +/* 78 */ V( 0x0bb6, 77, 79, 0 ), | |
119 | +/* 79 */ V( 0x0a40, 77, 48, 0 ), | |
120 | +/* 80 */ V( 0x5832, 80, 81, 1 ), | |
121 | +/* 81 */ V( 0x4d1c, 88, 82, 0 ), | |
122 | +/* 82 */ V( 0x438e, 89, 83, 0 ), | |
123 | +/* 83 */ V( 0x3bdd, 90, 84, 0 ), | |
124 | +/* 84 */ V( 0x34ee, 91, 85, 0 ), | |
125 | +/* 85 */ V( 0x2eae, 92, 86, 0 ), | |
126 | +/* 86 */ V( 0x299a, 93, 87, 0 ), | |
127 | +/* 87 */ V( 0x2516, 86, 71, 0 ), | |
128 | +/* 88 */ V( 0x5570, 88, 89, 1 ), | |
129 | +/* 89 */ V( 0x4ca9, 95, 90, 0 ), | |
130 | +/* 90 */ V( 0x44d9, 96, 91, 0 ), | |
131 | +/* 91 */ V( 0x3e22, 97, 92, 0 ), | |
132 | +/* 92 */ V( 0x3824, 99, 93, 0 ), | |
133 | +/* 93 */ V( 0x32b4, 99, 94, 0 ), | |
134 | +/* 94 */ V( 0x2e17, 93, 86, 0 ), | |
135 | +/* 95 */ V( 0x56a8, 95, 96, 1 ), | |
136 | +/* 96 */ V( 0x4f46, 101, 97, 0 ), | |
137 | +/* 97 */ V( 0x47e5, 102, 98, 0 ), | |
138 | +/* 98 */ V( 0x41cf, 103, 99, 0 ), | |
139 | +/* 99 */ V( 0x3c3d, 104, 100, 0 ), | |
140 | +/* 100 */ V( 0x375e, 99, 93, 0 ), | |
141 | +/* 101 */ V( 0x5231, 105, 102, 0 ), | |
142 | +/* 102 */ V( 0x4c0f, 106, 103, 0 ), | |
143 | +/* 103 */ V( 0x4639, 107, 104, 0 ), | |
144 | +/* 104 */ V( 0x415e, 103, 99, 0 ), | |
145 | +/* 105 */ V( 0x5627, 105, 106, 1 ), | |
146 | +/* 106 */ V( 0x50e7, 108, 107, 0 ), | |
147 | +/* 107 */ V( 0x4b85, 109, 103, 0 ), | |
148 | +/* 108 */ V( 0x5597, 110, 109, 0 ), | |
149 | +/* 109 */ V( 0x504f, 111, 107, 0 ), | |
150 | +/* 110 */ V( 0x5a10, 110, 111, 1 ), | |
151 | +/* 111 */ V( 0x5522, 112, 109, 0 ), | |
152 | +/* 112 */ V( 0x59eb, 112, 111, 1 ) | |
153 | +}; | |
154 | diff -Nur jpeg-6b.orig/jcarith.c jpeg-6b/jcarith.c | |
155 | --- jpeg-6b.orig/jcarith.c 1970-01-01 01:00:00.000000000 +0100 | |
156 | +++ jpeg-6b/jcarith.c 1997-08-10 18:40:45.000000000 +0200 | |
157 | @@ -0,0 +1,922 @@ | |
158 | +/* | |
159 | + * jcarith.c | |
160 | + * | |
161 | + * Copyright (C) 1997, Guido Vollbeding <guivol@esc.de>. | |
162 | + * This file is NOT part of the Independent JPEG Group's software | |
163 | + * for legal reasons. | |
164 | + * See the accompanying README file for conditions of distribution and use. | |
165 | + * | |
166 | + * This file contains portable arithmetic entropy encoding routines for JPEG | |
167 | + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). | |
168 | + * | |
169 | + * Both sequential and progressive modes are supported in this single module. | |
170 | + * | |
171 | + * Suspension is not currently supported in this module. | |
172 | + */ | |
173 | + | |
174 | +#define JPEG_INTERNALS | |
175 | +#include "jinclude.h" | |
176 | +#include "jpeglib.h" | |
177 | + | |
178 | + | |
179 | +/* Expanded entropy encoder object for arithmetic encoding. */ | |
180 | + | |
181 | +typedef struct { | |
182 | + struct jpeg_entropy_encoder pub; /* public fields */ | |
183 | + | |
184 | + INT32 c; /* C register, base of coding interval, layout as in sec. D.1.3 */ | |
185 | + INT32 a; /* A register, normalized size of coding interval */ | |
186 | + INT32 sc; /* counter for stacked 0xFF values which might overflow */ | |
187 | + INT32 zc; /* counter for pending 0x00 output values which might * | |
188 | + * be discarded at the end ("Pacman" termination) */ | |
189 | + int ct; /* bit shift counter, determines when next byte will be written */ | |
190 | + int buffer; /* buffer for most recent output byte != 0xFF */ | |
191 | + | |
192 | + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | |
193 | + int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ | |
194 | + | |
195 | + unsigned int restarts_to_go; /* MCUs left in this restart interval */ | |
196 | + int next_restart_num; /* next restart number to write (0-7) */ | |
197 | + | |
198 | + /* Pointers to statistics areas (these workspaces have image lifespan) */ | |
199 | + unsigned char * dc_stats[NUM_ARITH_TBLS]; | |
200 | + unsigned char * ac_stats[NUM_ARITH_TBLS]; | |
201 | +} arith_entropy_encoder; | |
202 | + | |
203 | +typedef arith_entropy_encoder * arith_entropy_ptr; | |
204 | + | |
205 | +/* The following two definitions specify the allocation chunk size | |
206 | + * for the statistics area. | |
207 | + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least | |
208 | + * 49 statistics bins for DC, and 245 statistics bins for AC coding. | |
209 | + * Note that we use one additional AC bin for codings with fixed | |
210 | + * probability (0.5), thus the minimum number for AC is 246. | |
211 | + * | |
212 | + * We use a compact representation with 1 byte per statistics bin, | |
213 | + * thus the numbers directly represent byte sizes. | |
214 | + * This 1 byte per statistics bin contains the meaning of the MPS | |
215 | + * (more probable symbol) in the highest bit (mask 0x80), and the | |
216 | + * index into the probability estimation state machine table | |
217 | + * in the lower bits (mask 0x7F). | |
218 | + */ | |
219 | + | |
220 | +#define DC_STAT_BINS 64 | |
221 | +#define AC_STAT_BINS 256 | |
222 | + | |
223 | +/* NOTE: Uncomment the following #define if you want to use the | |
224 | + * given formula for calculating the AC conditioning parameter Kx | |
225 | + * for spectral selection progressive coding in section G.1.3.2 | |
226 | + * of the spec (Kx = Kmin + SRL (8 + Se - Kmin) 4). | |
227 | + * Although the spec and P&M authors claim that this "has proven | |
228 | + * to give good results for 8 bit precision samples", I'm not | |
229 | + * convinced yet that this is really beneficial. | |
230 | + * Early tests gave only very marginal compression enhancements | |
231 | + * (a few - around 5 or so - bytes even for very large files), | |
232 | + * which would turn out rather negative if we'd suppress the | |
233 | + * DAC (Define Arithmetic Conditioning) marker segments for | |
234 | + * the default parameters in the future. | |
235 | + * Note that currently the marker writing module emits 12-byte | |
236 | + * DAC segments for a full-component scan in a color image. | |
237 | + * This is not worth worrying about IMHO. However, since the | |
238 | + * spec defines the default values to be used if the tables | |
239 | + * are omitted (unlike Huffman tables, which are required | |
240 | + * anyway), one might optimize this behaviour in the future, | |
241 | + * and then it would be disadvantageous to use custom tables if | |
242 | + * they don't provide sufficient gain to exceed the DAC size. | |
243 | + * | |
244 | + * On the other hand, I'd consider it as a reasonable result | |
245 | + * that the conditioning has no significant influence on the | |
246 | + * compression performance. This means that the basic | |
247 | + * statistical model is already rather stable. | |
248 | + * | |
249 | + * Thus, at the moment, we use the default conditioning values | |
250 | + * anyway, and do not use the custom formula. | |
251 | + * | |
252 | +#define CALCULATE_SPECTRAL_CONDITIONING | |
253 | + */ | |
254 | + | |
255 | +/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. | |
256 | + * We assume that int right shift is unsigned if INT32 right shift is, | |
257 | + * which should be safe. | |
258 | + */ | |
259 | + | |
260 | +#ifdef RIGHT_SHIFT_IS_UNSIGNED | |
261 | +#define ISHIFT_TEMPS int ishift_temp; | |
262 | +#define IRIGHT_SHIFT(x,shft) \ | |
263 | + ((ishift_temp = (x)) < 0 ? \ | |
264 | + (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ | |
265 | + (ishift_temp >> (shft))) | |
266 | +#else | |
267 | +#define ISHIFT_TEMPS | |
268 | +#define IRIGHT_SHIFT(x,shft) ((x) >> (shft)) | |
269 | +#endif | |
270 | + | |
271 | + | |
272 | +LOCAL(void) | |
273 | +emit_byte (int val, j_compress_ptr cinfo) | |
274 | +/* Write next output byte; we do not support suspension in this module. */ | |
275 | +{ | |
276 | + struct jpeg_destination_mgr * dest = cinfo->dest; | |
277 | + | |
278 | + *dest->next_output_byte++ = (JOCTET) val; | |
279 | + if (--dest->free_in_buffer == 0) | |
280 | + if (! (*dest->empty_output_buffer) (cinfo)) | |
281 | + ERREXIT(cinfo, JERR_CANT_SUSPEND); | |
282 | +} | |
283 | + | |
284 | + | |
285 | +/* | |
286 | + * Finish up at the end of an arithmetic-compressed scan. | |
287 | + */ | |
288 | + | |
289 | +METHODDEF(void) | |
290 | +finish_pass (j_compress_ptr cinfo) | |
291 | +{ | |
292 | + arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; | |
293 | + INT32 temp; | |
294 | + | |
295 | + /* Section D.1.8: Termination of encoding */ | |
296 | + | |
297 | + /* Find the e->c in the coding interval with the largest | |
298 | + * number of trailing zero bits */ | |
299 | + if ((temp = (e->a - 1 + e->c) & 0xFFFF0000L) < e->c) | |
300 | + e->c = temp + 0x8000L; | |
301 | + else | |
302 | + e->c = temp; | |
303 | + /* Send remaining bytes to output */ | |
304 | + e->c <<= e->ct; | |
305 | + if (e->c & 0xF8000000L) { | |
306 | + /* One final overflow has to be handled */ | |
307 | + if (e->buffer >= 0) { | |
308 | + if (e->zc) | |
309 | + do emit_byte(0x00, cinfo); | |
310 | + while (--e->zc); | |
311 | + emit_byte(e->buffer + 1, cinfo); | |
312 | + if (e->buffer + 1 == 0xFF) | |
313 | + emit_byte(0x00, cinfo); | |
314 | + } | |
315 | + e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ | |
316 | + e->sc = 0; | |
317 | + } else { | |
318 | + if (e->buffer == 0) | |
319 | + ++e->zc; | |
320 | + else if (e->buffer >= 0) { | |
321 | + if (e->zc) | |
322 | + do emit_byte(0x00, cinfo); | |
323 | + while (--e->zc); | |
324 | + emit_byte(e->buffer, cinfo); | |
325 | + } | |
326 | + if (e->sc) { | |
327 | + if (e->zc) | |
328 | + do emit_byte(0x00, cinfo); | |
329 | + while (--e->zc); | |
330 | + do { | |
331 | + emit_byte(0xFF, cinfo); | |
332 | + emit_byte(0x00, cinfo); | |
333 | + } while (--e->sc); | |
334 | + } | |
335 | + } | |
336 | + /* Output final bytes only if they are not 0x00 */ | |
337 | + if (e->c & 0x7FFF800L) { | |
338 | + if (e->zc) /* output final pending zero bytes */ | |
339 | + do emit_byte(0x00, cinfo); | |
340 | + while (--e->zc); | |
341 | + emit_byte((e->c >> 19) & 0xFF, cinfo); | |
342 | + if (((e->c >> 19) & 0xFF) == 0xFF) | |
343 | + emit_byte(0x00, cinfo); | |
344 | + if (e->c & 0x7F800L) { | |
345 | + emit_byte((e->c >> 11) & 0xFF, cinfo); | |
346 | + if (((e->c >> 11) & 0xFF) == 0xFF) | |
347 | + emit_byte(0x00, cinfo); | |
348 | + } | |
349 | + } | |
350 | +} | |
351 | + | |
352 | + | |
353 | +/* | |
354 | + * The core arithmetic encoding routine (common in JPEG and JBIG). | |
355 | + * This needs to go as fast as possible. | |
356 | + * Machine-dependent optimization facilities | |
357 | + * are not utilized in this portable implementation. | |
358 | + * However, this code should be fairly efficient and | |
359 | + * may be a good base for further optimizations anyway. | |
360 | + * | |
361 | + * Parameter 'val' to be encoded may be 0 or 1 (binary decision). | |
362 | + * | |
363 | + * Note: I've added full "Pacman" termination support to the | |
364 | + * byte output routines, which is equivalent to the optional | |
365 | + * Discard_final_zeros procedure (Figure D.15) in the spec. | |
366 | + * Thus, we always produce the shortest possible output | |
367 | + * stream compliant to the spec (no trailing zero bytes, | |
368 | + * except for FF stuffing). | |
369 | + * | |
370 | + * I've also introduced a new scheme for accessing | |
371 | + * the probability estimation state machine table, | |
372 | + * derived from Markus Kuhn's JBIG implementation. | |
373 | + */ | |
374 | + | |
375 | +LOCAL(void) | |
376 | +arith_encode (j_compress_ptr cinfo, unsigned char *st, int val) | |
377 | +{ | |
378 | + extern const INT32 jaritab[]; | |
379 | + register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; | |
380 | + register unsigned char nl, nm; | |
381 | + register INT32 qe, temp; | |
382 | + register int sv; | |
383 | + | |
384 | + /* Fetch values from our compact representation of Table D.2: | |
385 | + * Qe values and probability estimation state machine | |
386 | + */ | |
387 | + sv = *st; | |
388 | + qe = jaritab[sv & 0x7F]; /* => Qe_Value */ | |
389 | + nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ | |
390 | + nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ | |
391 | + | |
392 | + /* Encode & estimation procedures per sections D.1.4 & D.1.5 */ | |
393 | + e->a -= qe; | |
394 | + if (val != (sv >> 7)) { | |
395 | + /* Encode the less probable symbol */ | |
396 | + if (e->a >= qe) { | |
397 | + /* If the interval size (qe) for the less probable symbol (LPS) | |
398 | + * is larger than the interval size for the MPS, then exchange | |
399 | + * the two symbols for coding efficiency, otherwise code the LPS | |
400 | + * as usual: */ | |
401 | + e->c += e->a; | |
402 | + e->a = qe; | |
403 | + } | |
404 | + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ | |
405 | + } else { | |
406 | + /* Encode the more probable symbol */ | |
407 | + if (e->a >= 0x8000L) | |
408 | + return; /* A >= 0x8000 -> ready, no renormalization required */ | |
409 | + if (e->a < qe) { | |
410 | + /* If the interval size (qe) for the less probable symbol (LPS) | |
411 | + * is larger than the interval size for the MPS, then exchange | |
412 | + * the two symbols for coding efficiency: */ | |
413 | + e->c += e->a; | |
414 | + e->a = qe; | |
415 | + } | |
416 | + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ | |
417 | + } | |
418 | + | |
419 | + /* Renormalization & data output per section D.1.6 */ | |
420 | + do { | |
421 | + e->a <<= 1; | |
422 | + e->c <<= 1; | |
423 | + if (--e->ct == 0) { | |
424 | + /* Another byte is ready for output */ | |
425 | + temp = e->c >> 19; | |
426 | + if (temp > 0xFF) { | |
427 | + /* Handle overflow over all stacked 0xFF bytes */ | |
428 | + if (e->buffer >= 0) { | |
429 | + if (e->zc) | |
430 | + do emit_byte(0x00, cinfo); | |
431 | + while (--e->zc); | |
432 | + emit_byte(e->buffer + 1, cinfo); | |
433 | + if (e->buffer + 1 == 0xFF) | |
434 | + emit_byte(0x00, cinfo); | |
435 | + } | |
436 | + e->zc += e->sc; /* carry-over converts stacked 0xFF bytes to 0x00 */ | |
437 | + e->sc = 0; | |
438 | + /* Note: The 3 spacer bits in the C register guarantee | |
439 | + * that the new buffer byte can't be 0xFF here | |
440 | + * (see page 160 in the P&M JPEG book). */ | |
441 | + e->buffer = temp & 0xFF; /* new output byte, might overflow later */ | |
442 | + } else if (temp == 0xFF) { | |
443 | + ++e->sc; /* stack 0xFF byte (which might overflow later) */ | |
444 | + } else { | |
445 | + /* Output all stacked 0xFF bytes, they will not overflow any more */ | |
446 | + if (e->buffer == 0) | |
447 | + ++e->zc; | |
448 | + else if (e->buffer >= 0) { | |
449 | + if (e->zc) | |
450 | + do emit_byte(0x00, cinfo); | |
451 | + while (--e->zc); | |
452 | + emit_byte(e->buffer, cinfo); | |
453 | + } | |
454 | + if (e->sc) { | |
455 | + if (e->zc) | |
456 | + do emit_byte(0x00, cinfo); | |
457 | + while (--e->zc); | |
458 | + do { | |
459 | + emit_byte(0xFF, cinfo); | |
460 | + emit_byte(0x00, cinfo); | |
461 | + } while (--e->sc); | |
462 | + } | |
463 | + e->buffer = temp & 0xFF; /* new output byte (can still overflow) */ | |
464 | + } | |
465 | + e->c &= 0x7FFFFL; | |
466 | + e->ct += 8; | |
467 | + } | |
468 | + } while (e->a < 0x8000L); | |
469 | +} | |
470 | + | |
471 | + | |
472 | +/* | |
473 | + * Emit a restart marker & resynchronize predictions. | |
474 | + */ | |
475 | + | |
476 | +LOCAL(void) | |
477 | +emit_restart (j_compress_ptr cinfo, int restart_num) | |
478 | +{ | |
479 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
480 | + int ci; | |
481 | + jpeg_component_info * compptr; | |
482 | + | |
483 | + finish_pass(cinfo); | |
484 | + | |
485 | + emit_byte(0xFF, cinfo); | |
486 | + emit_byte(JPEG_RST0 + restart_num, cinfo); | |
487 | + | |
488 | + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | |
489 | + compptr = cinfo->cur_comp_info[ci]; | |
490 | + /* Re-initialize statistics areas */ | |
491 | + if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { | |
492 | + MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); | |
493 | + /* Reset DC predictions to 0 */ | |
494 | + entropy->last_dc_val[ci] = 0; | |
495 | + entropy->dc_context[ci] = 0; | |
496 | + } | |
497 | + if (cinfo->progressive_mode == 0 || cinfo->Ss) { | |
498 | + MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); | |
499 | + } | |
500 | + } | |
501 | + | |
502 | + /* Reset arithmetic encoding variables */ | |
503 | + entropy->c = 0; | |
504 | + entropy->a = 0x10000L; | |
505 | + entropy->sc = 0; | |
506 | + entropy->zc = 0; | |
507 | + entropy->ct = 11; | |
508 | + entropy->buffer = -1; /* empty */ | |
509 | +} | |
510 | + | |
511 | + | |
512 | +/* | |
513 | + * MCU encoding for DC initial scan (either spectral selection, | |
514 | + * or first pass of successive approximation). | |
515 | + */ | |
516 | + | |
517 | +METHODDEF(boolean) | |
518 | +encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | |
519 | +{ | |
520 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
521 | + JBLOCKROW block; | |
522 | + unsigned char *st; | |
523 | + int blkn, ci, tbl; | |
524 | + int v, v2, m; | |
525 | + ISHIFT_TEMPS | |
526 | + | |
527 | + /* Emit restart marker if needed */ | |
528 | + if (cinfo->restart_interval) { | |
529 | + if (entropy->restarts_to_go == 0) { | |
530 | + emit_restart(cinfo, entropy->next_restart_num); | |
531 | + entropy->restarts_to_go = cinfo->restart_interval; | |
532 | + entropy->next_restart_num++; | |
533 | + entropy->next_restart_num &= 7; | |
534 | + } | |
535 | + entropy->restarts_to_go--; | |
536 | + } | |
537 | + | |
538 | + /* Encode the MCU data blocks */ | |
539 | + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | |
540 | + block = MCU_data[blkn]; | |
541 | + ci = cinfo->MCU_membership[blkn]; | |
542 | + tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; | |
543 | + | |
544 | + /* Compute the DC value after the required point transform by Al. | |
545 | + * This is simply an arithmetic right shift. | |
546 | + */ | |
547 | + m = IRIGHT_SHIFT((int) ((*block)[0]), cinfo->Al); | |
548 | + | |
549 | + /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ | |
550 | + | |
551 | + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ | |
552 | + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; | |
553 | + | |
554 | + /* Figure F.4: Encode_DC_DIFF */ | |
555 | + if ((v = m - entropy->last_dc_val[ci]) == 0) { | |
556 | + arith_encode(cinfo, st, 0); | |
557 | + entropy->dc_context[ci] = 0; /* zero diff category */ | |
558 | + } else { | |
559 | + entropy->last_dc_val[ci] = m; | |
560 | + arith_encode(cinfo, st, 1); | |
561 | + /* Figure F.6: Encoding nonzero value v */ | |
562 | + /* Figure F.7: Encoding the sign of v */ | |
563 | + if (v > 0) { | |
564 | + arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ | |
565 | + st += 2; /* Table F.4: SP = S0 + 2 */ | |
566 | + entropy->dc_context[ci] = 4; /* small positive diff category */ | |
567 | + } else { | |
568 | + v = -v; | |
569 | + arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ | |
570 | + st += 3; /* Table F.4: SN = S0 + 3 */ | |
571 | + entropy->dc_context[ci] = 8; /* small negative diff category */ | |
572 | + } | |
573 | + /* Figure F.8: Encoding the magnitude category of v */ | |
574 | + m = 0; | |
575 | + if (v -= 1) { | |
576 | + arith_encode(cinfo, st, 1); | |
577 | + m = 1; | |
578 | + v2 = v; | |
579 | + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ | |
580 | + while (v2 >>= 1) { | |
581 | + arith_encode(cinfo, st, 1); | |
582 | + m <<= 1; | |
583 | + st += 1; | |
584 | + } | |
585 | + } | |
586 | + arith_encode(cinfo, st, 0); | |
587 | + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ | |
588 | + if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) | |
589 | + entropy->dc_context[ci] = 0; /* zero diff category */ | |
590 | + else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) | |
591 | + entropy->dc_context[ci] += 8; /* large diff category */ | |
592 | + /* Figure F.9: Encoding the magnitude bit pattern of v */ | |
593 | + st += 14; | |
594 | + while (m >>= 1) | |
595 | + arith_encode(cinfo, st, (m & v) ? 1 : 0); | |
596 | + } | |
597 | + } | |
598 | + | |
599 | + return TRUE; | |
600 | +} | |
601 | + | |
602 | + | |
603 | +/* | |
604 | + * MCU encoding for AC initial scan (either spectral selection, | |
605 | + * or first pass of successive approximation). | |
606 | + */ | |
607 | + | |
608 | +METHODDEF(boolean) | |
609 | +encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | |
610 | +{ | |
611 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
612 | + JBLOCKROW block; | |
613 | + unsigned char *st; | |
614 | + int tbl, k, ke; | |
615 | + int v, v2, m; | |
616 | + | |
617 | + /* Emit restart marker if needed */ | |
618 | + if (cinfo->restart_interval) { | |
619 | + if (entropy->restarts_to_go == 0) { | |
620 | + emit_restart(cinfo, entropy->next_restart_num); | |
621 | + entropy->restarts_to_go = cinfo->restart_interval; | |
622 | + entropy->next_restart_num++; | |
623 | + entropy->next_restart_num &= 7; | |
624 | + } | |
625 | + entropy->restarts_to_go--; | |
626 | + } | |
627 | + | |
628 | + /* Encode the MCU data block */ | |
629 | + block = MCU_data[0]; | |
630 | + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; | |
631 | + | |
632 | + /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ | |
633 | + | |
634 | + /* Establish EOB (end-of-block) index */ | |
635 | + for (ke = cinfo->Se + 1; ke > 1; ke--) | |
636 | + /* We must apply the point transform by Al. For AC coefficients this | |
637 | + * is an integer division with rounding towards 0. To do this portably | |
638 | + * in C, we shift after obtaining the absolute value. | |
639 | + */ | |
640 | + if ((v = (*block)[jpeg_natural_order[ke - 1]]) >= 0) { | |
641 | + if (v >>= cinfo->Al) break; | |
642 | + } else { | |
643 | + v = -v; | |
644 | + if (v >>= cinfo->Al) break; | |
645 | + } | |
646 | + | |
647 | + /* Figure F.5: Encode_AC_Coefficients */ | |
648 | + for (k = cinfo->Ss; k < ke; k++) { | |
649 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
650 | + arith_encode(cinfo, st, 0); /* EOB decision */ | |
651 | + entropy->ac_stats[tbl][245] = 0; | |
652 | + for (;;) { | |
653 | + if ((v = (*block)[jpeg_natural_order[k]]) >= 0) { | |
654 | + if (v >>= cinfo->Al) { | |
655 | + arith_encode(cinfo, st + 1, 1); | |
656 | + arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0); | |
657 | + break; | |
658 | + } | |
659 | + } else { | |
660 | + v = -v; | |
661 | + if (v >>= cinfo->Al) { | |
662 | + arith_encode(cinfo, st + 1, 1); | |
663 | + arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1); | |
664 | + break; | |
665 | + } | |
666 | + } | |
667 | + arith_encode(cinfo, st + 1, 0); st += 3; k++; | |
668 | + } | |
669 | + st += 2; | |
670 | + /* Figure F.8: Encoding the magnitude category of v */ | |
671 | + m = 0; | |
672 | + if (v -= 1) { | |
673 | + arith_encode(cinfo, st, 1); | |
674 | + m = 1; | |
675 | + v2 = v; | |
676 | + if (v2 >>= 1) { | |
677 | + arith_encode(cinfo, st, 1); | |
678 | + m <<= 1; | |
679 | + st = entropy->ac_stats[tbl] + | |
680 | + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); | |
681 | + while (v2 >>= 1) { | |
682 | + arith_encode(cinfo, st, 1); | |
683 | + m <<= 1; | |
684 | + st += 1; | |
685 | + } | |
686 | + } | |
687 | + } | |
688 | + arith_encode(cinfo, st, 0); | |
689 | + /* Figure F.9: Encoding the magnitude bit pattern of v */ | |
690 | + st += 14; | |
691 | + while (m >>= 1) | |
692 | + arith_encode(cinfo, st, (m & v) ? 1 : 0); | |
693 | + } | |
694 | + /* Encode EOB decision only if k <= cinfo->Se */ | |
695 | + if (k <= cinfo->Se) { | |
696 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
697 | + arith_encode(cinfo, st, 1); | |
698 | + } | |
699 | + | |
700 | + return TRUE; | |
701 | +} | |
702 | + | |
703 | + | |
704 | +/* | |
705 | + * MCU encoding for DC successive approximation refinement scan. | |
706 | + */ | |
707 | + | |
708 | +METHODDEF(boolean) | |
709 | +encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | |
710 | +{ | |
711 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
712 | + unsigned char st[4]; | |
713 | + int Al, blkn; | |
714 | + | |
715 | + /* Emit restart marker if needed */ | |
716 | + if (cinfo->restart_interval) { | |
717 | + if (entropy->restarts_to_go == 0) { | |
718 | + emit_restart(cinfo, entropy->next_restart_num); | |
719 | + entropy->restarts_to_go = cinfo->restart_interval; | |
720 | + entropy->next_restart_num++; | |
721 | + entropy->next_restart_num &= 7; | |
722 | + } | |
723 | + entropy->restarts_to_go--; | |
724 | + } | |
725 | + | |
726 | + Al = cinfo->Al; | |
727 | + | |
728 | + /* Encode the MCU data blocks */ | |
729 | + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | |
730 | + st[0] = 0; /* use fixed probability estimation */ | |
731 | + /* We simply emit the Al'th bit of the DC coefficient value. */ | |
732 | + arith_encode(cinfo, st, (MCU_data[blkn][0][0] >> Al) & 1); | |
733 | + } | |
734 | + | |
735 | + return TRUE; | |
736 | +} | |
737 | + | |
738 | + | |
739 | +/* | |
740 | + * MCU encoding for AC successive approximation refinement scan. | |
741 | + */ | |
742 | + | |
743 | +METHODDEF(boolean) | |
744 | +encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | |
745 | +{ | |
746 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
747 | + JBLOCKROW block; | |
748 | + unsigned char *st; | |
749 | + int tbl, k, ke, kex; | |
750 | + int v; | |
751 | + | |
752 | + /* Emit restart marker if needed */ | |
753 | + if (cinfo->restart_interval) { | |
754 | + if (entropy->restarts_to_go == 0) { | |
755 | + emit_restart(cinfo, entropy->next_restart_num); | |
756 | + entropy->restarts_to_go = cinfo->restart_interval; | |
757 | + entropy->next_restart_num++; | |
758 | + entropy->next_restart_num &= 7; | |
759 | + } | |
760 | + entropy->restarts_to_go--; | |
761 | + } | |
762 | + | |
763 | + /* Encode the MCU data block */ | |
764 | + block = MCU_data[0]; | |
765 | + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; | |
766 | + | |
767 | + /* Section G.1.3.3: Encoding of AC coefficients */ | |
768 | + | |
769 | + /* Establish EOB (end-of-block) index */ | |
770 | + for (ke = cinfo->Se + 1; ke > 1; ke--) | |
771 | + /* We must apply the point transform by Al. For AC coefficients this | |
772 | + * is an integer division with rounding towards 0. To do this portably | |
773 | + * in C, we shift after obtaining the absolute value. | |
774 | + */ | |
775 | + if ((v = (*block)[jpeg_natural_order[ke - 1]]) >= 0) { | |
776 | + if (v >>= cinfo->Al) break; | |
777 | + } else { | |
778 | + v = -v; | |
779 | + if (v >>= cinfo->Al) break; | |
780 | + } | |
781 | + | |
782 | + /* Establish EOBx (previous stage end-of-block) index */ | |
783 | + for (kex = ke; kex > 1; kex--) | |
784 | + if ((v = (*block)[jpeg_natural_order[kex - 1]]) >= 0) { | |
785 | + if (v >>= cinfo->Ah) break; | |
786 | + } else { | |
787 | + v = -v; | |
788 | + if (v >>= cinfo->Ah) break; | |
789 | + } | |
790 | + | |
791 | + /* Figure G.10: Encode_AC_Coefficients_SA */ | |
792 | + for (k = cinfo->Ss; k < ke; k++) { | |
793 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
794 | + if (k >= kex) | |
795 | + arith_encode(cinfo, st, 0); /* EOB decision */ | |
796 | + entropy->ac_stats[tbl][245] = 0; | |
797 | + for (;;) { | |
798 | + if ((v = (*block)[jpeg_natural_order[k]]) >= 0) { | |
799 | + if (v >>= cinfo->Al) { | |
800 | + if (v >> 1) /* previously nonzero coef */ | |
801 | + arith_encode(cinfo, st + 2, (v & 1)); | |
802 | + else { /* newly nonzero coef */ | |
803 | + arith_encode(cinfo, st + 1, 1); | |
804 | + arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0); | |
805 | + } | |
806 | + break; | |
807 | + } | |
808 | + } else { | |
809 | + v = -v; | |
810 | + if (v >>= cinfo->Al) { | |
811 | + if (v >> 1) /* previously nonzero coef */ | |
812 | + arith_encode(cinfo, st + 2, (v & 1)); | |
813 | + else { /* newly nonzero coef */ | |
814 | + arith_encode(cinfo, st + 1, 1); | |
815 | + arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1); | |
816 | + } | |
817 | + break; | |
818 | + } | |
819 | + } | |
820 | + arith_encode(cinfo, st + 1, 0); st += 3; k++; | |
821 | + } | |
822 | + } | |
823 | + /* Encode EOB decision only if k <= cinfo->Se */ | |
824 | + if (k <= cinfo->Se) { | |
825 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
826 | + arith_encode(cinfo, st, 1); | |
827 | + } | |
828 | + | |
829 | + return TRUE; | |
830 | +} | |
831 | + | |
832 | + | |
833 | +/* | |
834 | + * Encode and output one MCU's worth of arithmetic-compressed coefficients. | |
835 | + */ | |
836 | + | |
837 | +METHODDEF(boolean) | |
838 | +encode_mcu (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | |
839 | +{ | |
840 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
841 | + jpeg_component_info * compptr; | |
842 | + JBLOCKROW block; | |
843 | + unsigned char *st; | |
844 | + int blkn, ci, tbl, k, ke; | |
845 | + int v, v2, m; | |
846 | + | |
847 | + /* Emit restart marker if needed */ | |
848 | + if (cinfo->restart_interval) { | |
849 | + if (entropy->restarts_to_go == 0) { | |
850 | + emit_restart(cinfo, entropy->next_restart_num); | |
851 | + entropy->restarts_to_go = cinfo->restart_interval; | |
852 | + entropy->next_restart_num++; | |
853 | + entropy->next_restart_num &= 7; | |
854 | + } | |
855 | + entropy->restarts_to_go--; | |
856 | + } | |
857 | + | |
858 | + /* Encode the MCU data blocks */ | |
859 | + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | |
860 | + block = MCU_data[blkn]; | |
861 | + ci = cinfo->MCU_membership[blkn]; | |
862 | + compptr = cinfo->cur_comp_info[ci]; | |
863 | + | |
864 | + /* Sections F.1.4.1 & F.1.4.4.1: Encoding of DC coefficients */ | |
865 | + | |
866 | + tbl = compptr->dc_tbl_no; | |
867 | + | |
868 | + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ | |
869 | + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; | |
870 | + | |
871 | + /* Figure F.4: Encode_DC_DIFF */ | |
872 | + if ((v = (*block)[0] - entropy->last_dc_val[ci]) == 0) { | |
873 | + arith_encode(cinfo, st, 0); | |
874 | + entropy->dc_context[ci] = 0; /* zero diff category */ | |
875 | + } else { | |
876 | + entropy->last_dc_val[ci] = (*block)[0]; | |
877 | + arith_encode(cinfo, st, 1); | |
878 | + /* Figure F.6: Encoding nonzero value v */ | |
879 | + /* Figure F.7: Encoding the sign of v */ | |
880 | + if (v > 0) { | |
881 | + arith_encode(cinfo, st + 1, 0); /* Table F.4: SS = S0 + 1 */ | |
882 | + st += 2; /* Table F.4: SP = S0 + 2 */ | |
883 | + entropy->dc_context[ci] = 4; /* small positive diff category */ | |
884 | + } else { | |
885 | + v = -v; | |
886 | + arith_encode(cinfo, st + 1, 1); /* Table F.4: SS = S0 + 1 */ | |
887 | + st += 3; /* Table F.4: SN = S0 + 3 */ | |
888 | + entropy->dc_context[ci] = 8; /* small negative diff category */ | |
889 | + } | |
890 | + /* Figure F.8: Encoding the magnitude category of v */ | |
891 | + m = 0; | |
892 | + if (v -= 1) { | |
893 | + arith_encode(cinfo, st, 1); | |
894 | + m = 1; | |
895 | + v2 = v; | |
896 | + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ | |
897 | + while (v2 >>= 1) { | |
898 | + arith_encode(cinfo, st, 1); | |
899 | + m <<= 1; | |
900 | + st += 1; | |
901 | + } | |
902 | + } | |
903 | + arith_encode(cinfo, st, 0); | |
904 | + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ | |
905 | + if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) | |
906 | + entropy->dc_context[ci] = 0; /* zero diff category */ | |
907 | + else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) | |
908 | + entropy->dc_context[ci] += 8; /* large diff category */ | |
909 | + /* Figure F.9: Encoding the magnitude bit pattern of v */ | |
910 | + st += 14; | |
911 | + while (m >>= 1) | |
912 | + arith_encode(cinfo, st, (m & v) ? 1 : 0); | |
913 | + } | |
914 | + | |
915 | + /* Sections F.1.4.2 & F.1.4.4.2: Encoding of AC coefficients */ | |
916 | + | |
917 | + tbl = compptr->ac_tbl_no; | |
918 | + | |
919 | + /* Establish EOB (end-of-block) index */ | |
920 | + for (ke = DCTSIZE2; ke > 1; ke--) | |
921 | + if ((*block)[jpeg_natural_order[ke - 1]]) break; | |
922 | + | |
923 | + /* Figure F.5: Encode_AC_Coefficients */ | |
924 | + for (k = 1; k < ke; k++) { | |
925 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
926 | + arith_encode(cinfo, st, 0); /* EOB decision */ | |
927 | + while ((v = (*block)[jpeg_natural_order[k]]) == 0) { | |
928 | + arith_encode(cinfo, st + 1, 0); st += 3; k++; | |
929 | + } | |
930 | + arith_encode(cinfo, st + 1, 1); | |
931 | + /* Figure F.6: Encoding nonzero value v */ | |
932 | + /* Figure F.7: Encoding the sign of v */ | |
933 | + entropy->ac_stats[tbl][245] = 0; | |
934 | + if (v > 0) { | |
935 | + arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 0); | |
936 | + } else { | |
937 | + v = -v; | |
938 | + arith_encode(cinfo, entropy->ac_stats[tbl] + 245, 1); | |
939 | + } | |
940 | + st += 2; | |
941 | + /* Figure F.8: Encoding the magnitude category of v */ | |
942 | + m = 0; | |
943 | + if (v -= 1) { | |
944 | + arith_encode(cinfo, st, 1); | |
945 | + m = 1; | |
946 | + v2 = v; | |
947 | + if (v2 >>= 1) { | |
948 | + arith_encode(cinfo, st, 1); | |
949 | + m <<= 1; | |
950 | + st = entropy->ac_stats[tbl] + | |
951 | + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); | |
952 | + while (v2 >>= 1) { | |
953 | + arith_encode(cinfo, st, 1); | |
954 | + m <<= 1; | |
955 | + st += 1; | |
956 | + } | |
957 | + } | |
958 | + } | |
959 | + arith_encode(cinfo, st, 0); | |
960 | + /* Figure F.9: Encoding the magnitude bit pattern of v */ | |
961 | + st += 14; | |
962 | + while (m >>= 1) | |
963 | + arith_encode(cinfo, st, (m & v) ? 1 : 0); | |
964 | + } | |
965 | + /* Encode EOB decision only if k < DCTSIZE2 */ | |
966 | + if (k < DCTSIZE2) { | |
967 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
968 | + arith_encode(cinfo, st, 1); | |
969 | + } | |
970 | + } | |
971 | + | |
972 | + return TRUE; | |
973 | +} | |
974 | + | |
975 | + | |
976 | +/* | |
977 | + * Initialize for an arithmetic-compressed scan. | |
978 | + */ | |
979 | + | |
980 | +METHODDEF(void) | |
981 | +start_pass (j_compress_ptr cinfo, boolean gather_statistics) | |
982 | +{ | |
983 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
984 | + int ci, tbl; | |
985 | + jpeg_component_info * compptr; | |
986 | + | |
987 | + if (gather_statistics) | |
988 | + /* Make sure to avoid that in the master control logic! | |
989 | + * We are fully adaptive here and need no extra | |
990 | + * statistics gathering pass! | |
991 | + */ | |
992 | + ERREXIT(cinfo, JERR_NOT_COMPILED); | |
993 | + | |
994 | + /* We assume jcmaster.c already validated the progressive scan parameters. */ | |
995 | + | |
996 | + /* Select execution routines */ | |
997 | + if (cinfo->progressive_mode) { | |
998 | + if (cinfo->Ah == 0) { | |
999 | + if (cinfo->Ss == 0) | |
1000 | + entropy->pub.encode_mcu = encode_mcu_DC_first; | |
1001 | + else | |
1002 | + entropy->pub.encode_mcu = encode_mcu_AC_first; | |
1003 | + } else { | |
1004 | + if (cinfo->Ss == 0) | |
1005 | + entropy->pub.encode_mcu = encode_mcu_DC_refine; | |
1006 | + else | |
1007 | + entropy->pub.encode_mcu = encode_mcu_AC_refine; | |
1008 | + } | |
1009 | + } else | |
1010 | + entropy->pub.encode_mcu = encode_mcu; | |
1011 | + | |
1012 | + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | |
1013 | + compptr = cinfo->cur_comp_info[ci]; | |
1014 | + /* Allocate & initialize requested statistics areas */ | |
1015 | + if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { | |
1016 | + tbl = compptr->dc_tbl_no; | |
1017 | + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) | |
1018 | + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); | |
1019 | + if (entropy->dc_stats[tbl] == NULL) | |
1020 | + entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) | |
1021 | + ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); | |
1022 | + MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); | |
1023 | + /* Initialize DC predictions to 0 */ | |
1024 | + entropy->last_dc_val[ci] = 0; | |
1025 | + entropy->dc_context[ci] = 0; | |
1026 | + } | |
1027 | + if (cinfo->progressive_mode == 0 || cinfo->Ss) { | |
1028 | + tbl = compptr->ac_tbl_no; | |
1029 | + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) | |
1030 | + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); | |
1031 | + if (entropy->ac_stats[tbl] == NULL) | |
1032 | + entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) | |
1033 | + ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); | |
1034 | + MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); | |
1035 | +#ifdef CALCULATE_SPECTRAL_CONDITIONING | |
1036 | + if (cinfo->progressive_mode) | |
1037 | + /* Section G.1.3.2: Set appropriate arithmetic conditioning value Kx */ | |
1038 | + cinfo->arith_ac_K[tbl] = cinfo->Ss + ((8 + cinfo->Se - cinfo->Ss) >> 4); | |
1039 | +#endif | |
1040 | + } | |
1041 | + } | |
1042 | + | |
1043 | + /* Initialize arithmetic encoding variables */ | |
1044 | + entropy->c = 0; | |
1045 | + entropy->a = 0x10000L; | |
1046 | + entropy->sc = 0; | |
1047 | + entropy->zc = 0; | |
1048 | + entropy->ct = 11; | |
1049 | + entropy->buffer = -1; /* empty */ | |
1050 | + | |
1051 | + /* Initialize restart stuff */ | |
1052 | + entropy->restarts_to_go = cinfo->restart_interval; | |
1053 | + entropy->next_restart_num = 0; | |
1054 | +} | |
1055 | + | |
1056 | + | |
1057 | +/* | |
1058 | + * Module initialization routine for arithmetic entropy encoding. | |
1059 | + */ | |
1060 | + | |
1061 | +GLOBAL(void) | |
1062 | +jinit_arith_encoder (j_compress_ptr cinfo) | |
1063 | +{ | |
1064 | + arith_entropy_ptr entropy; | |
1065 | + int i; | |
1066 | + | |
1067 | + entropy = (arith_entropy_ptr) | |
1068 | + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | |
1069 | + SIZEOF(arith_entropy_encoder)); | |
1070 | + cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; | |
1071 | + entropy->pub.start_pass = start_pass; | |
1072 | + entropy->pub.finish_pass = finish_pass; | |
1073 | + | |
1074 | + /* Mark tables unallocated */ | |
1075 | + for (i = 0; i < NUM_ARITH_TBLS; i++) { | |
1076 | + entropy->dc_stats[i] = NULL; | |
1077 | + entropy->ac_stats[i] = NULL; | |
1078 | + } | |
1079 | +} | |
1080 | diff -Nur jpeg-6b.orig/jcinit.c jpeg-6b/jcinit.c | |
1081 | --- jpeg-6b.orig/jcinit.c 1997-09-07 22:50:40.000000000 +0200 | |
1082 | +++ jpeg-6b/jcinit.c 1997-10-21 17:50:14.000000000 +0200 | |
1083 | @@ -41,9 +41,9 @@ | |
1084 | /* Forward DCT */ | |
1085 | jinit_forward_dct(cinfo); | |
1086 | /* Entropy encoding: either Huffman or arithmetic coding. */ | |
1087 | - if (cinfo->arith_code) { | |
1088 | - ERREXIT(cinfo, JERR_ARITH_NOTIMPL); | |
1089 | - } else { | |
1090 | + if (cinfo->arith_code) | |
1091 | + jinit_arith_encoder(cinfo); | |
1092 | + else { | |
1093 | if (cinfo->progressive_mode) { | |
1094 | #ifdef C_PROGRESSIVE_SUPPORTED | |
1095 | jinit_phuff_encoder(cinfo); | |
1096 | diff -Nur jpeg-6b.orig/jcmarker.c jpeg-6b/jcmarker.c | |
1097 | --- jpeg-6b.orig/jcmarker.c 1998-02-21 22:54:00.000000000 +0100 | |
1098 | +++ jpeg-6b/jcmarker.c 1998-02-23 16:15:08.000000000 +0100 | |
1099 | @@ -529,7 +529,10 @@ | |
1100 | ||
1101 | /* Emit the proper SOF marker */ | |
1102 | if (cinfo->arith_code) { | |
1103 | - emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */ | |
1104 | + if (cinfo->progressive_mode) | |
1105 | + emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */ | |
1106 | + else | |
1107 | + emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */ | |
1108 | } else { | |
1109 | if (cinfo->progressive_mode) | |
1110 | emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */ | |
1111 | diff -Nur jpeg-6b.orig/jcmaster.c jpeg-6b/jcmaster.c | |
1112 | --- jpeg-6b.orig/jcmaster.c 1997-08-11 01:40:57.000000000 +0200 | |
1113 | +++ jpeg-6b/jcmaster.c 1997-10-21 18:06:05.000000000 +0200 | |
1114 | @@ -433,7 +433,7 @@ | |
1115 | /* Do Huffman optimization for a scan after the first one. */ | |
1116 | select_scan_parameters(cinfo); | |
1117 | per_scan_setup(cinfo); | |
1118 | - if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { | |
1119 | + if (cinfo->Ss != 0 || cinfo->Ah == 0) { | |
1120 | (*cinfo->entropy->start_pass) (cinfo, TRUE); | |
1121 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); | |
1122 | master->pub.call_pass_startup = FALSE; | |
1123 | @@ -567,7 +567,7 @@ | |
1124 | cinfo->num_scans = 1; | |
1125 | } | |
1126 | ||
1127 | - if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */ | |
1128 | + if (cinfo->progressive_mode && cinfo->arith_code == 0) /* TEMPORARY HACK ??? */ | |
1129 | cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ | |
1130 | ||
1131 | /* Initialize my private state */ | |
1132 | diff -Nur jpeg-6b.orig/jctrans.c jpeg-6b/jctrans.c | |
1133 | --- jpeg-6b.orig/jctrans.c 1998-02-21 21:03:25.000000000 +0100 | |
1134 | +++ jpeg-6b/jctrans.c 1998-02-23 16:20:16.000000000 +0100 | |
1135 | @@ -167,7 +167,7 @@ | |
1136 | ||
1137 | /* Entropy encoding: either Huffman or arithmetic coding. */ | |
1138 | if (cinfo->arith_code) { | |
1139 | - ERREXIT(cinfo, JERR_ARITH_NOTIMPL); | |
1140 | + jinit_arith_encoder(cinfo); | |
1141 | } else { | |
1142 | if (cinfo->progressive_mode) { | |
1143 | #ifdef C_PROGRESSIVE_SUPPORTED | |
1144 | diff -Nur jpeg-6b.orig/jdarith.c jpeg-6b/jdarith.c | |
1145 | --- jpeg-6b.orig/jdarith.c 1970-01-01 01:00:00.000000000 +0100 | |
1146 | +++ jpeg-6b/jdarith.c 1997-08-10 18:40:45.000000000 +0200 | |
1147 | @@ -0,0 +1,762 @@ | |
1148 | +/* | |
1149 | + * jdarith.c | |
1150 | + * | |
1151 | + * Copyright (C) 1997, Guido Vollbeding <guivol@esc.de>. | |
1152 | + * This file is NOT part of the Independent JPEG Group's software | |
1153 | + * for legal reasons. | |
1154 | + * See the accompanying README file for conditions of distribution and use. | |
1155 | + * | |
1156 | + * This file contains portable arithmetic entropy decoding routines for JPEG | |
1157 | + * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81). | |
1158 | + * | |
1159 | + * Both sequential and progressive modes are supported in this single module. | |
1160 | + * | |
1161 | + * Suspension is not currently supported in this module. | |
1162 | + */ | |
1163 | + | |
1164 | +#define JPEG_INTERNALS | |
1165 | +#include "jinclude.h" | |
1166 | +#include "jpeglib.h" | |
1167 | + | |
1168 | + | |
1169 | +/* Expanded entropy decoder object for arithmetic decoding. */ | |
1170 | + | |
1171 | +typedef struct { | |
1172 | + struct jpeg_entropy_decoder pub; /* public fields */ | |
1173 | + | |
1174 | + INT32 c; /* C register, base of coding interval + input bit buffer */ | |
1175 | + INT32 a; /* A register, normalized size of coding interval */ | |
1176 | + int ct; /* bit shift counter, # of bits left in bit buffer part of C */ | |
1177 | + /* init: ct = -16 */ | |
1178 | + /* run: ct = 0..7 */ | |
1179 | + /* error: ct = -1 */ | |
1180 | + int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | |
1181 | + int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ | |
1182 | + | |
1183 | + unsigned int restarts_to_go; /* MCUs left in this restart interval */ | |
1184 | + | |
1185 | + /* Pointers to statistics areas (these workspaces have image lifespan) */ | |
1186 | + unsigned char * dc_stats[NUM_ARITH_TBLS]; | |
1187 | + unsigned char * ac_stats[NUM_ARITH_TBLS]; | |
1188 | +} arith_entropy_decoder; | |
1189 | + | |
1190 | +typedef arith_entropy_decoder * arith_entropy_ptr; | |
1191 | + | |
1192 | +/* The following two definitions specify the allocation chunk size | |
1193 | + * for the statistics area. | |
1194 | + * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least | |
1195 | + * 49 statistics bins for DC, and 245 statistics bins for AC coding. | |
1196 | + * Note that we use one additional AC bin for codings with fixed | |
1197 | + * probability (0.5), thus the minimum number for AC is 246. | |
1198 | + * | |
1199 | + * We use a compact representation with 1 byte per statistics bin, | |
1200 | + * thus the numbers directly represent byte sizes. | |
1201 | + * This 1 byte per statistics bin contains the meaning of the MPS | |
1202 | + * (more probable symbol) in the highest bit (mask 0x80), and the | |
1203 | + * index into the probability estimation state machine table | |
1204 | + * in the lower bits (mask 0x7F). | |
1205 | + */ | |
1206 | + | |
1207 | +#define DC_STAT_BINS 64 | |
1208 | +#define AC_STAT_BINS 256 | |
1209 | + | |
1210 | + | |
1211 | +LOCAL(int) | |
1212 | +get_byte (j_decompress_ptr cinfo) | |
1213 | +/* Read next input byte; we do not support suspension in this module. */ | |
1214 | +{ | |
1215 | + struct jpeg_source_mgr * src = cinfo->src; | |
1216 | + | |
1217 | + if (src->bytes_in_buffer == 0) | |
1218 | + if (! (*src->fill_input_buffer) (cinfo)) | |
1219 | + ERREXIT(cinfo, JERR_CANT_SUSPEND); | |
1220 | + src->bytes_in_buffer--; | |
1221 | + return GETJOCTET(*src->next_input_byte++); | |
1222 | +} | |
1223 | + | |
1224 | + | |
1225 | +/* | |
1226 | + * The core arithmetic decoding routine (common in JPEG and JBIG). | |
1227 | + * This needs to go as fast as possible. | |
1228 | + * Machine-dependent optimization facilities | |
1229 | + * are not utilized in this portable implementation. | |
1230 | + * However, this code should be fairly efficient and | |
1231 | + * may be a good base for further optimizations anyway. | |
1232 | + * | |
1233 | + * Return value is 0 or 1 (binary decision). | |
1234 | + * | |
1235 | + * Note: I've changed the handling of the code base & bit | |
1236 | + * buffer register C compared to other implementations | |
1237 | + * based on the standards layout & procedures. | |
1238 | + * While it also contains both the actual base of the | |
1239 | + * coding interval (16 bits) and the next-bits buffer, | |
1240 | + * the cut-point between these two parts is floating | |
1241 | + * (instead of fixed) with the bit shift counter CT. | |
1242 | + * Thus, we also need only one (variable instead of | |
1243 | + * fixed size) shift for the LPS/MPS decision, and | |
1244 | + * we can get away with any renormalization update | |
1245 | + * of C (except for new data insertion, of course). | |
1246 | + * | |
1247 | + * I've also introduced a new scheme for accessing | |
1248 | + * the probability estimation state machine table, | |
1249 | + * derived from Markus Kuhn's JBIG implementation. | |
1250 | + */ | |
1251 | + | |
1252 | +LOCAL(int) | |
1253 | +arith_decode (j_decompress_ptr cinfo, unsigned char *st) | |
1254 | +{ | |
1255 | + extern const INT32 jaritab[]; | |
1256 | + register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy; | |
1257 | + register unsigned char nl, nm; | |
1258 | + register INT32 qe, temp; | |
1259 | + register int sv, data; | |
1260 | + | |
1261 | + /* Renormalization & data input per section D.2.6 */ | |
1262 | + while (e->a < 0x8000L) { | |
1263 | + if (--e->ct < 0) { | |
1264 | + /* Need to fetch next data byte */ | |
1265 | + if (cinfo->unread_marker) | |
1266 | + data = 0; /* stuff zero data */ | |
1267 | + else { | |
1268 | + data = get_byte(cinfo); /* read next input byte */ | |
1269 | + if (data == 0xFF) { /* zero stuff or marker code */ | |
1270 | + do data = get_byte(cinfo); | |
1271 | + while (data == 0xFF); /* swallow extra 0xFF bytes */ | |
1272 | + if (data == 0) | |
1273 | + data = 0xFF; /* discard stuffed zero byte */ | |
1274 | + else { | |
1275 | + /* Note: Different from the Huffman decoder, hitting | |
1276 | + * a marker while processing the compressed data | |
1277 | + * segment is legal in arithmetic coding. | |
1278 | + * The convention is to supply zero data | |
1279 | + * then until decoding is complete. | |
1280 | + */ | |
1281 | + cinfo->unread_marker = data; | |
1282 | + data = 0; | |
1283 | + } | |
1284 | + } | |
1285 | + } | |
1286 | + e->c = (e->c << 8) | data; /* insert data into C register */ | |
1287 | + if ((e->ct += 8) < 0) /* update bit shift counter */ | |
1288 | + /* Need more initial bytes */ | |
1289 | + if (++e->ct == 0) | |
1290 | + /* Got 2 initial bytes -> re-init A and exit loop */ | |
1291 | + e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ | |
1292 | + } | |
1293 | + e->a <<= 1; | |
1294 | + } | |
1295 | + | |
1296 | + /* Fetch values from our compact representation of Table D.2: | |
1297 | + * Qe values and probability estimation state machine | |
1298 | + */ | |
1299 | + sv = *st; | |
1300 | + qe = jaritab[sv & 0x7F]; /* => Qe_Value */ | |
1301 | + nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ | |
1302 | + nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ | |
1303 | + | |
1304 | + /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ | |
1305 | + temp = e->a - qe; | |
1306 | + e->a = temp; | |
1307 | + temp <<= e->ct; | |
1308 | + if (e->c >= temp) { | |
1309 | + e->c -= temp; | |
1310 | + /* Conditional LPS (less probable symbol) exchange */ | |
1311 | + if (e->a < qe) { | |
1312 | + e->a = qe; | |
1313 | + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ | |
1314 | + } else { | |
1315 | + e->a = qe; | |
1316 | + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ | |
1317 | + sv ^= 0x80; /* Exchange LPS/MPS */ | |
1318 | + } | |
1319 | + } else if (e->a < 0x8000L) { | |
1320 | + /* Conditional MPS (more probable symbol) exchange */ | |
1321 | + if (e->a < qe) { | |
1322 | + *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ | |
1323 | + sv ^= 0x80; /* Exchange LPS/MPS */ | |
1324 | + } else { | |
1325 | + *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ | |
1326 | + } | |
1327 | + } | |
1328 | + | |
1329 | + return sv >> 7; | |
1330 | +} | |
1331 | + | |
1332 | + | |
1333 | +/* | |
1334 | + * Check for a restart marker & resynchronize decoder. | |
1335 | + */ | |
1336 | + | |
1337 | +LOCAL(void) | |
1338 | +process_restart (j_decompress_ptr cinfo) | |
1339 | +{ | |
1340 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1341 | + int ci; | |
1342 | + jpeg_component_info * compptr; | |
1343 | + | |
1344 | + /* Advance past the RSTn marker */ | |
1345 | + if (! (*cinfo->marker->read_restart_marker) (cinfo)) | |
1346 | + ERREXIT(cinfo, JERR_CANT_SUSPEND); | |
1347 | + | |
1348 | + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | |
1349 | + compptr = cinfo->cur_comp_info[ci]; | |
1350 | + /* Re-initialize statistics areas */ | |
1351 | + if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { | |
1352 | + MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS); | |
1353 | + /* Reset DC predictions to 0 */ | |
1354 | + entropy->last_dc_val[ci] = 0; | |
1355 | + entropy->dc_context[ci] = 0; | |
1356 | + } | |
1357 | + if (cinfo->progressive_mode == 0 || cinfo->Ss) { | |
1358 | + MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS); | |
1359 | + } | |
1360 | + } | |
1361 | + | |
1362 | + /* Reset arithmetic decoding variables */ | |
1363 | + entropy->c = 0; | |
1364 | + entropy->a = 0; | |
1365 | + entropy->ct = -16; /* force reading 2 initial bytes to fill C */ | |
1366 | + | |
1367 | + /* Reset restart counter */ | |
1368 | + entropy->restarts_to_go = cinfo->restart_interval; | |
1369 | +} | |
1370 | + | |
1371 | + | |
1372 | +/* | |
1373 | + * Arithmetic MCU decoding. | |
1374 | + * Each of these routines decodes and returns one MCU's worth of | |
1375 | + * arithmetic-compressed coefficients. | |
1376 | + * The coefficients are reordered from zigzag order into natural array order, | |
1377 | + * but are not dequantized. | |
1378 | + * | |
1379 | + * The i'th block of the MCU is stored into the block pointed to by | |
1380 | + * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. | |
1381 | + */ | |
1382 | + | |
1383 | +/* | |
1384 | + * MCU decoding for DC initial scan (either spectral selection, | |
1385 | + * or first pass of successive approximation). | |
1386 | + */ | |
1387 | + | |
1388 | +METHODDEF(boolean) | |
1389 | +decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | |
1390 | +{ | |
1391 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1392 | + JBLOCKROW block; | |
1393 | + unsigned char *st; | |
1394 | + int blkn, ci, tbl, sign; | |
1395 | + int v, m; | |
1396 | + | |
1397 | + /* Process restart marker if needed */ | |
1398 | + if (cinfo->restart_interval) { | |
1399 | + if (entropy->restarts_to_go == 0) | |
1400 | + process_restart(cinfo); | |
1401 | + entropy->restarts_to_go--; | |
1402 | + } | |
1403 | + | |
1404 | + if (entropy->ct == -1) return TRUE; /* if error do nothing */ | |
1405 | + | |
1406 | + /* Outer loop handles each block in the MCU */ | |
1407 | + | |
1408 | + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | |
1409 | + block = MCU_data[blkn]; | |
1410 | + ci = cinfo->MCU_membership[blkn]; | |
1411 | + tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; | |
1412 | + | |
1413 | + /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ | |
1414 | + | |
1415 | + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ | |
1416 | + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; | |
1417 | + | |
1418 | + /* Figure F.19: Decode_DC_DIFF */ | |
1419 | + if (arith_decode(cinfo, st) == 0) | |
1420 | + entropy->dc_context[ci] = 0; | |
1421 | + else { | |
1422 | + /* Figure F.21: Decoding nonzero value v */ | |
1423 | + /* Figure F.22: Decoding the sign of v */ | |
1424 | + sign = arith_decode(cinfo, st + 1); | |
1425 | + st += 2; st += sign; | |
1426 | + /* Figure F.23: Decoding the magnitude category of v */ | |
1427 | + if ((m = arith_decode(cinfo, st)) != 0) { | |
1428 | + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ | |
1429 | + while (arith_decode(cinfo, st)) { | |
1430 | + if ((m <<= 1) == 0x8000) { | |
1431 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1432 | + entropy->ct = -1; /* magnitude overflow */ | |
1433 | + return TRUE; | |
1434 | + } | |
1435 | + st += 1; | |
1436 | + } | |
1437 | + } | |
1438 | + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ | |
1439 | + if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) | |
1440 | + entropy->dc_context[ci] = 0; /* zero diff category */ | |
1441 | + else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) | |
1442 | + entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ | |
1443 | + else | |
1444 | + entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ | |
1445 | + v = m; | |
1446 | + /* Figure F.24: Decoding the magnitude bit pattern of v */ | |
1447 | + st += 14; | |
1448 | + while (m >>= 1) | |
1449 | + if (arith_decode(cinfo, st)) v |= m; | |
1450 | + v += 1; if (sign) v = -v; | |
1451 | + entropy->last_dc_val[ci] += v; | |
1452 | + } | |
1453 | + | |
1454 | + /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ | |
1455 | + (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al); | |
1456 | + } | |
1457 | + | |
1458 | + return TRUE; | |
1459 | +} | |
1460 | + | |
1461 | + | |
1462 | +/* | |
1463 | + * MCU decoding for AC initial scan (either spectral selection, | |
1464 | + * or first pass of successive approximation). | |
1465 | + */ | |
1466 | + | |
1467 | +METHODDEF(boolean) | |
1468 | +decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | |
1469 | +{ | |
1470 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1471 | + JBLOCKROW block; | |
1472 | + unsigned char *st; | |
1473 | + int tbl, sign, k; | |
1474 | + int v, m; | |
1475 | + | |
1476 | + /* Process restart marker if needed */ | |
1477 | + if (cinfo->restart_interval) { | |
1478 | + if (entropy->restarts_to_go == 0) | |
1479 | + process_restart(cinfo); | |
1480 | + entropy->restarts_to_go--; | |
1481 | + } | |
1482 | + | |
1483 | + if (entropy->ct == -1) return TRUE; /* if error do nothing */ | |
1484 | + | |
1485 | + /* There is always only one block per MCU */ | |
1486 | + block = MCU_data[0]; | |
1487 | + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; | |
1488 | + | |
1489 | + /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ | |
1490 | + | |
1491 | + /* Figure F.20: Decode_AC_coefficients */ | |
1492 | + for (k = cinfo->Ss; k <= cinfo->Se; k++) { | |
1493 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
1494 | + if (arith_decode(cinfo, st)) break; /* EOB flag */ | |
1495 | + while (arith_decode(cinfo, st + 1) == 0) { | |
1496 | + st += 3; k++; | |
1497 | + if (k > cinfo->Se) { | |
1498 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1499 | + entropy->ct = -1; /* spectral overflow */ | |
1500 | + return TRUE; | |
1501 | + } | |
1502 | + } | |
1503 | + /* Figure F.21: Decoding nonzero value v */ | |
1504 | + /* Figure F.22: Decoding the sign of v */ | |
1505 | + entropy->ac_stats[tbl][245] = 0; | |
1506 | + sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245); | |
1507 | + st += 2; | |
1508 | + /* Figure F.23: Decoding the magnitude category of v */ | |
1509 | + if ((m = arith_decode(cinfo, st)) != 0) { | |
1510 | + if (arith_decode(cinfo, st)) { | |
1511 | + m <<= 1; | |
1512 | + st = entropy->ac_stats[tbl] + | |
1513 | + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); | |
1514 | + while (arith_decode(cinfo, st)) { | |
1515 | + if ((m <<= 1) == 0x8000) { | |
1516 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1517 | + entropy->ct = -1; /* magnitude overflow */ | |
1518 | + return TRUE; | |
1519 | + } | |
1520 | + st += 1; | |
1521 | + } | |
1522 | + } | |
1523 | + } | |
1524 | + v = m; | |
1525 | + /* Figure F.24: Decoding the magnitude bit pattern of v */ | |
1526 | + st += 14; | |
1527 | + while (m >>= 1) | |
1528 | + if (arith_decode(cinfo, st)) v |= m; | |
1529 | + v += 1; if (sign) v = -v; | |
1530 | + /* Scale and output coefficient in natural (dezigzagged) order */ | |
1531 | + (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al); | |
1532 | + } | |
1533 | + | |
1534 | + return TRUE; | |
1535 | +} | |
1536 | + | |
1537 | + | |
1538 | +/* | |
1539 | + * MCU decoding for DC successive approximation refinement scan. | |
1540 | + */ | |
1541 | + | |
1542 | +METHODDEF(boolean) | |
1543 | +decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | |
1544 | +{ | |
1545 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1546 | + unsigned char st[4]; | |
1547 | + int p1, blkn; | |
1548 | + | |
1549 | + /* Process restart marker if needed */ | |
1550 | + if (cinfo->restart_interval) { | |
1551 | + if (entropy->restarts_to_go == 0) | |
1552 | + process_restart(cinfo); | |
1553 | + entropy->restarts_to_go--; | |
1554 | + } | |
1555 | + | |
1556 | + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ | |
1557 | + | |
1558 | + /* Outer loop handles each block in the MCU */ | |
1559 | + | |
1560 | + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | |
1561 | + st[0] = 0; /* use fixed probability estimation */ | |
1562 | + /* Encoded data is simply the next bit of the two's-complement DC value */ | |
1563 | + if (arith_decode(cinfo, st)) | |
1564 | + MCU_data[blkn][0][0] |= p1; | |
1565 | + } | |
1566 | + | |
1567 | + return TRUE; | |
1568 | +} | |
1569 | + | |
1570 | + | |
1571 | +/* | |
1572 | + * MCU decoding for AC successive approximation refinement scan. | |
1573 | + */ | |
1574 | + | |
1575 | +METHODDEF(boolean) | |
1576 | +decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | |
1577 | +{ | |
1578 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1579 | + JBLOCKROW block; | |
1580 | + JCOEFPTR thiscoef; | |
1581 | + unsigned char *st; | |
1582 | + int tbl, k, kex; | |
1583 | + int p1, m1; | |
1584 | + | |
1585 | + /* Process restart marker if needed */ | |
1586 | + if (cinfo->restart_interval) { | |
1587 | + if (entropy->restarts_to_go == 0) | |
1588 | + process_restart(cinfo); | |
1589 | + entropy->restarts_to_go--; | |
1590 | + } | |
1591 | + | |
1592 | + if (entropy->ct == -1) return TRUE; /* if error do nothing */ | |
1593 | + | |
1594 | + /* There is always only one block per MCU */ | |
1595 | + block = MCU_data[0]; | |
1596 | + tbl = cinfo->cur_comp_info[0]->ac_tbl_no; | |
1597 | + | |
1598 | + p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ | |
1599 | + m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ | |
1600 | + | |
1601 | + /* Establish EOBx (previous stage end-of-block) index */ | |
1602 | + for (kex = cinfo->Se + 1; kex > 1; kex--) | |
1603 | + if ((*block)[jpeg_natural_order[kex - 1]]) break; | |
1604 | + | |
1605 | + for (k = cinfo->Ss; k <= cinfo->Se; k++) { | |
1606 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
1607 | + if (k >= kex) | |
1608 | + if (arith_decode(cinfo, st)) break; /* EOB flag */ | |
1609 | + for (;;) { | |
1610 | + thiscoef = *block + jpeg_natural_order[k]; | |
1611 | + if (*thiscoef) { /* previously nonzero coef */ | |
1612 | + if (arith_decode(cinfo, st + 2)) | |
1613 | + if (*thiscoef < 0) | |
1614 | + *thiscoef += m1; | |
1615 | + else | |
1616 | + *thiscoef += p1; | |
1617 | + break; | |
1618 | + } | |
1619 | + if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ | |
1620 | + entropy->ac_stats[tbl][245] = 0; | |
1621 | + if (arith_decode(cinfo, entropy->ac_stats[tbl] + 245)) | |
1622 | + *thiscoef = m1; | |
1623 | + else | |
1624 | + *thiscoef = p1; | |
1625 | + break; | |
1626 | + } | |
1627 | + st += 3; k++; | |
1628 | + if (k > cinfo->Se) { | |
1629 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1630 | + entropy->ct = -1; /* spectral overflow */ | |
1631 | + return TRUE; | |
1632 | + } | |
1633 | + } | |
1634 | + } | |
1635 | + | |
1636 | + return TRUE; | |
1637 | +} | |
1638 | + | |
1639 | + | |
1640 | +/* | |
1641 | + * Decode one MCU's worth of arithmetic-compressed coefficients. | |
1642 | + */ | |
1643 | + | |
1644 | +METHODDEF(boolean) | |
1645 | +decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) | |
1646 | +{ | |
1647 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1648 | + jpeg_component_info * compptr; | |
1649 | + JBLOCKROW block; | |
1650 | + unsigned char *st; | |
1651 | + int blkn, ci, tbl, sign, k; | |
1652 | + int v, m; | |
1653 | + | |
1654 | + /* Process restart marker if needed */ | |
1655 | + if (cinfo->restart_interval) { | |
1656 | + if (entropy->restarts_to_go == 0) | |
1657 | + process_restart(cinfo); | |
1658 | + entropy->restarts_to_go--; | |
1659 | + } | |
1660 | + | |
1661 | + if (entropy->ct == -1) return TRUE; /* if error do nothing */ | |
1662 | + | |
1663 | + /* Outer loop handles each block in the MCU */ | |
1664 | + | |
1665 | + for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | |
1666 | + block = MCU_data[blkn]; | |
1667 | + ci = cinfo->MCU_membership[blkn]; | |
1668 | + compptr = cinfo->cur_comp_info[ci]; | |
1669 | + | |
1670 | + /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ | |
1671 | + | |
1672 | + tbl = compptr->dc_tbl_no; | |
1673 | + | |
1674 | + /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ | |
1675 | + st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; | |
1676 | + | |
1677 | + /* Figure F.19: Decode_DC_DIFF */ | |
1678 | + if (arith_decode(cinfo, st) == 0) | |
1679 | + entropy->dc_context[ci] = 0; | |
1680 | + else { | |
1681 | + /* Figure F.21: Decoding nonzero value v */ | |
1682 | + /* Figure F.22: Decoding the sign of v */ | |
1683 | + sign = arith_decode(cinfo, st + 1); | |
1684 | + st += 2; st += sign; | |
1685 | + /* Figure F.23: Decoding the magnitude category of v */ | |
1686 | + if ((m = arith_decode(cinfo, st)) != 0) { | |
1687 | + st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ | |
1688 | + while (arith_decode(cinfo, st)) { | |
1689 | + if ((m <<= 1) == 0x8000) { | |
1690 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1691 | + entropy->ct = -1; /* magnitude overflow */ | |
1692 | + return TRUE; | |
1693 | + } | |
1694 | + st += 1; | |
1695 | + } | |
1696 | + } | |
1697 | + /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ | |
1698 | + if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1)) | |
1699 | + entropy->dc_context[ci] = 0; /* zero diff category */ | |
1700 | + else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1)) | |
1701 | + entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ | |
1702 | + else | |
1703 | + entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ | |
1704 | + v = m; | |
1705 | + /* Figure F.24: Decoding the magnitude bit pattern of v */ | |
1706 | + st += 14; | |
1707 | + while (m >>= 1) | |
1708 | + if (arith_decode(cinfo, st)) v |= m; | |
1709 | + v += 1; if (sign) v = -v; | |
1710 | + entropy->last_dc_val[ci] += v; | |
1711 | + } | |
1712 | + | |
1713 | + (*block)[0] = (JCOEF) entropy->last_dc_val[ci]; | |
1714 | + | |
1715 | + /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ | |
1716 | + | |
1717 | + tbl = compptr->ac_tbl_no; | |
1718 | + | |
1719 | + /* Figure F.20: Decode_AC_coefficients */ | |
1720 | + for (k = 1; k < DCTSIZE2; k++) { | |
1721 | + st = entropy->ac_stats[tbl] + 3 * (k - 1); | |
1722 | + if (arith_decode(cinfo, st)) break; /* EOB flag */ | |
1723 | + while (arith_decode(cinfo, st + 1) == 0) { | |
1724 | + st += 3; k++; | |
1725 | + if (k >= DCTSIZE2) { | |
1726 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1727 | + entropy->ct = -1; /* spectral overflow */ | |
1728 | + return TRUE; | |
1729 | + } | |
1730 | + } | |
1731 | + /* Figure F.21: Decoding nonzero value v */ | |
1732 | + /* Figure F.22: Decoding the sign of v */ | |
1733 | + entropy->ac_stats[tbl][245] = 0; | |
1734 | + sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245); | |
1735 | + st += 2; | |
1736 | + /* Figure F.23: Decoding the magnitude category of v */ | |
1737 | + if ((m = arith_decode(cinfo, st)) != 0) { | |
1738 | + if (arith_decode(cinfo, st)) { | |
1739 | + m <<= 1; | |
1740 | + st = entropy->ac_stats[tbl] + | |
1741 | + (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); | |
1742 | + while (arith_decode(cinfo, st)) { | |
1743 | + if ((m <<= 1) == 0x8000) { | |
1744 | + WARNMS(cinfo, JWRN_ARITH_BAD_CODE); | |
1745 | + entropy->ct = -1; /* magnitude overflow */ | |
1746 | + return TRUE; | |
1747 | + } | |
1748 | + st += 1; | |
1749 | + } | |
1750 | + } | |
1751 | + } | |
1752 | + v = m; | |
1753 | + /* Figure F.24: Decoding the magnitude bit pattern of v */ | |
1754 | + st += 14; | |
1755 | + while (m >>= 1) | |
1756 | + if (arith_decode(cinfo, st)) v |= m; | |
1757 | + v += 1; if (sign) v = -v; | |
1758 | + (*block)[jpeg_natural_order[k]] = (JCOEF) v; | |
1759 | + } | |
1760 | + } | |
1761 | + | |
1762 | + return TRUE; | |
1763 | +} | |
1764 | + | |
1765 | + | |
1766 | +/* | |
1767 | + * Initialize for an arithmetic-compressed scan. | |
1768 | + */ | |
1769 | + | |
1770 | +METHODDEF(void) | |
1771 | +start_pass (j_decompress_ptr cinfo) | |
1772 | +{ | |
1773 | + arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; | |
1774 | + int ci, tbl; | |
1775 | + jpeg_component_info * compptr; | |
1776 | + | |
1777 | + if (cinfo->progressive_mode) { | |
1778 | + /* Validate progressive scan parameters */ | |
1779 | + if (cinfo->Ss == 0) { | |
1780 | + if (cinfo->Se != 0) | |
1781 | + goto bad; | |
1782 | + } else { | |
1783 | + /* need not check Ss/Se < 0 since they came from unsigned bytes */ | |
1784 | + if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2) | |
1785 | + goto bad; | |
1786 | + /* AC scans may have only one component */ | |
1787 | + if (cinfo->comps_in_scan != 1) | |
1788 | + goto bad; | |
1789 | + } | |
1790 | + if (cinfo->Ah != 0) { | |
1791 | + /* Successive approximation refinement scan: must have Al = Ah-1. */ | |
1792 | + if (cinfo->Ah-1 != cinfo->Al) | |
1793 | + goto bad; | |
1794 | + } | |
1795 | + if (cinfo->Al > 13) { /* need not check for < 0 */ | |
1796 | + bad: | |
1797 | + ERREXIT4(cinfo, JERR_BAD_PROGRESSION, | |
1798 | + cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); | |
1799 | + } | |
1800 | + /* Update progression status, and verify that scan order is legal. | |
1801 | + * Note that inter-scan inconsistencies are treated as warnings | |
1802 | + * not fatal errors ... not clear if this is right way to behave. | |
1803 | + */ | |
1804 | + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | |
1805 | + int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; | |
1806 | + int *coef_bit_ptr = & cinfo->coef_bits[cindex][0]; | |
1807 | + if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ | |
1808 | + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); | |
1809 | + for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { | |
1810 | + int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; | |
1811 | + if (cinfo->Ah != expected) | |
1812 | + WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); | |
1813 | + coef_bit_ptr[coefi] = cinfo->Al; | |
1814 | + } | |
1815 | + } | |
1816 | + /* Select MCU decoding routine */ | |
1817 | + if (cinfo->Ah == 0) { | |
1818 | + if (cinfo->Ss == 0) | |
1819 | + entropy->pub.decode_mcu = decode_mcu_DC_first; | |
1820 | + else | |
1821 | + entropy->pub.decode_mcu = decode_mcu_AC_first; | |
1822 | + } else { | |
1823 | + if (cinfo->Ss == 0) | |
1824 | + entropy->pub.decode_mcu = decode_mcu_DC_refine; | |
1825 | + else | |
1826 | + entropy->pub.decode_mcu = decode_mcu_AC_refine; | |
1827 | + } | |
1828 | + } else { | |
1829 | + /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. | |
1830 | + * This ought to be an error condition, but we make it a warning because | |
1831 | + * there are some baseline files out there with all zeroes in these bytes. | |
1832 | + */ | |
1833 | + if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || | |
1834 | + cinfo->Ah != 0 || cinfo->Al != 0) | |
1835 | + WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); | |
1836 | + /* Select MCU decoding routine */ | |
1837 | + entropy->pub.decode_mcu = decode_mcu; | |
1838 | + } | |
1839 | + | |
1840 | + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | |
1841 | + compptr = cinfo->cur_comp_info[ci]; | |
1842 | + /* Allocate & initialize requested statistics areas */ | |
1843 | + if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) { | |
1844 | + tbl = compptr->dc_tbl_no; | |
1845 | + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) | |
1846 | + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); | |
1847 | + if (entropy->dc_stats[tbl] == NULL) | |
1848 | + entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) | |
1849 | + ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS); | |
1850 | + MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS); | |
1851 | + /* Initialize DC predictions to 0 */ | |
1852 | + entropy->last_dc_val[ci] = 0; | |
1853 | + entropy->dc_context[ci] = 0; | |
1854 | + } | |
1855 | + if (cinfo->progressive_mode == 0 || cinfo->Ss) { | |
1856 | + tbl = compptr->ac_tbl_no; | |
1857 | + if (tbl < 0 || tbl >= NUM_ARITH_TBLS) | |
1858 | + ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); | |
1859 | + if (entropy->ac_stats[tbl] == NULL) | |
1860 | + entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small) | |
1861 | + ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS); | |
1862 | + MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS); | |
1863 | + } | |
1864 | + } | |
1865 | + | |
1866 | + /* Initialize arithmetic decoding variables */ | |
1867 | + entropy->c = 0; | |
1868 | + entropy->a = 0; | |
1869 | + entropy->ct = -16; /* force reading 2 initial bytes to fill C */ | |
1870 | + | |
1871 | + /* Initialize restart counter */ | |
1872 | + entropy->restarts_to_go = cinfo->restart_interval; | |
1873 | +} | |
1874 | + | |
1875 | + | |
1876 | +/* | |
1877 | + * Module initialization routine for arithmetic entropy decoding. | |
1878 | + */ | |
1879 | + | |
1880 | +GLOBAL(void) | |
1881 | +jinit_arith_decoder (j_decompress_ptr cinfo) | |
1882 | +{ | |
1883 | + arith_entropy_ptr entropy; | |
1884 | + int i; | |
1885 | + | |
1886 | + entropy = (arith_entropy_ptr) | |
1887 | + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | |
1888 | + SIZEOF(arith_entropy_decoder)); | |
1889 | + cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; | |
1890 | + entropy->pub.start_pass = start_pass; | |
1891 | + | |
1892 | + /* Mark tables unallocated */ | |
1893 | + for (i = 0; i < NUM_ARITH_TBLS; i++) { | |
1894 | + entropy->dc_stats[i] = NULL; | |
1895 | + entropy->ac_stats[i] = NULL; | |
1896 | + } | |
1897 | + | |
1898 | + if (cinfo->progressive_mode) { | |
1899 | + /* Create progression status table */ | |
1900 | + int *coef_bit_ptr, ci; | |
1901 | + cinfo->coef_bits = (int (*)[DCTSIZE2]) | |
1902 | + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | |
1903 | + cinfo->num_components*DCTSIZE2*SIZEOF(int)); | |
1904 | + coef_bit_ptr = & cinfo->coef_bits[0][0]; | |
1905 | + for (ci = 0; ci < cinfo->num_components; ci++) | |
1906 | + for (i = 0; i < DCTSIZE2; i++) | |
1907 | + *coef_bit_ptr++ = -1; | |
1908 | + } | |
1909 | +} | |
1910 | diff -Nur jpeg-6b.orig/jdmaster.c jpeg-6b/jdmaster.c | |
1911 | --- jpeg-6b.orig/jdmaster.c 1997-11-07 17:25:45.000000000 +0100 | |
1912 | +++ jpeg-6b/jdmaster.c 1998-02-23 16:23:29.000000000 +0100 | |
1913 | @@ -373,7 +373,7 @@ | |
1914 | jinit_inverse_dct(cinfo); | |
1915 | /* Entropy decoding: either Huffman or arithmetic coding. */ | |
1916 | if (cinfo->arith_code) { | |
1917 | - ERREXIT(cinfo, JERR_ARITH_NOTIMPL); | |
1918 | + jinit_arith_decoder(cinfo); | |
1919 | } else { | |
1920 | if (cinfo->progressive_mode) { | |
1921 | #ifdef D_PROGRESSIVE_SUPPORTED | |
1922 | diff -Nur jpeg-6b.orig/jdtrans.c jpeg-6b/jdtrans.c | |
1923 | --- jpeg-6b.orig/jdtrans.c 1997-08-03 23:47:58.000000000 +0200 | |
1924 | +++ jpeg-6b/jdtrans.c 1997-10-21 17:57:05.000000000 +0200 | |
1925 | @@ -100,9 +100,9 @@ | |
1926 | cinfo->buffered_image = TRUE; | |
1927 | ||
1928 | /* Entropy decoding: either Huffman or arithmetic coding. */ | |
1929 | - if (cinfo->arith_code) { | |
1930 | - ERREXIT(cinfo, JERR_ARITH_NOTIMPL); | |
1931 | - } else { | |
1932 | + if (cinfo->arith_code) | |
1933 | + jinit_arith_decoder(cinfo); | |
1934 | + else { | |
1935 | if (cinfo->progressive_mode) { | |
1936 | #ifdef D_PROGRESSIVE_SUPPORTED | |
1937 | jinit_phuff_decoder(cinfo); | |
1938 | diff -Nur jpeg-6b.orig/jerror.h jpeg-6b/jerror.h | |
1939 | --- jpeg-6b.orig/jerror.h 1997-10-18 20:59:10.000000000 +0200 | |
1940 | +++ jpeg-6b/jerror.h 1997-10-21 18:10:09.000000000 +0200 | |
1941 | @@ -93,6 +93,7 @@ | |
1942 | JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change") | |
1943 | JMESSAGE(JERR_NOTIMPL, "Not implemented yet") | |
1944 | JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time") | |
1945 | +JMESSAGE(JERR_NO_ARITH_TABLE, "Arithmetic table 0x%02x was not defined") | |
1946 | JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported") | |
1947 | JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined") | |
1948 | JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image") | |
1949 | @@ -170,6 +171,7 @@ | |
1950 | JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u") | |
1951 | JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u") | |
1952 | JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d") | |
1953 | +JMESSAGE(JWRN_ARITH_BAD_CODE, "Corrupt JPEG data: bad arithmetic code") | |
1954 | JMESSAGE(JWRN_BOGUS_PROGRESSION, | |
1955 | "Inconsistent progression sequence for component %d coefficient %d") | |
1956 | JMESSAGE(JWRN_EXTRANEOUS_DATA, | |
1957 | diff -Nur jpeg-6b.orig/jpegint.h jpeg-6b/jpegint.h | |
1958 | --- jpeg-6b.orig/jpegint.h 1997-04-20 01:44:35.000000000 +0200 | |
1959 | +++ jpeg-6b/jpegint.h 1997-10-21 18:14:02.000000000 +0200 | |
1960 | @@ -345,6 +345,7 @@ | |
1961 | EXTERN(void) jinit_forward_dct JPP((j_compress_ptr cinfo)); | |
1962 | EXTERN(void) jinit_huff_encoder JPP((j_compress_ptr cinfo)); | |
1963 | EXTERN(void) jinit_phuff_encoder JPP((j_compress_ptr cinfo)); | |
1964 | +EXTERN(void) jinit_arith_encoder JPP((j_compress_ptr cinfo)); | |
1965 | EXTERN(void) jinit_marker_writer JPP((j_compress_ptr cinfo)); | |
1966 | /* Decompression module initialization routines */ | |
1967 | EXTERN(void) jinit_master_decompress JPP((j_decompress_ptr cinfo)); | |
1968 | @@ -358,6 +359,7 @@ | |
1969 | EXTERN(void) jinit_marker_reader JPP((j_decompress_ptr cinfo)); | |
1970 | EXTERN(void) jinit_huff_decoder JPP((j_decompress_ptr cinfo)); | |
1971 | EXTERN(void) jinit_phuff_decoder JPP((j_decompress_ptr cinfo)); | |
1972 | +EXTERN(void) jinit_arith_decoder JPP((j_decompress_ptr cinfo)); | |
1973 | EXTERN(void) jinit_inverse_dct JPP((j_decompress_ptr cinfo)); | |
1974 | EXTERN(void) jinit_upsampler JPP((j_decompress_ptr cinfo)); | |
1975 | EXTERN(void) jinit_color_deconverter JPP((j_decompress_ptr cinfo)); | |
1976 | diff -Nur jpeg-6b.orig/makefile.cfg jpeg-6b/makefile.cfg | |
1977 | --- jpeg-6b.orig/makefile.cfg 1998-03-21 20:08:57.000000000 +0100 | |
1978 | +++ jpeg-6b/makefile.cfg 1998-03-28 22:46:03.000000000 +0100 | |
1979 | @@ -80,7 +80,7 @@ | |
1980 | jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdphuff.c \ | |
1981 | jdpostct.c jdsample.c jdtrans.c jerror.c jfdctflt.c jfdctfst.c \ | |
1982 | jfdctint.c jidctflt.c jidctfst.c jidctint.c jidctred.c jquant1.c \ | |
1983 | - jquant2.c jutils.c jmemmgr.c | |
1984 | + jquant2.c jutils.c jmemmgr.c jaricom.c jcarith.c jdarith.c | |
1985 | # memmgr back ends: compile only one of these into a working library | |
1986 | SYSDEPSOURCES= jmemansi.c jmemname.c jmemnobs.c jmemdos.c jmemmac.c | |
1987 | # source files: cjpeg/djpeg/jpegtran applications, also rdjpgcom/wrjpgcom | |
1988 | @@ -110,19 +110,19 @@ | |
1989 | DISTFILES= $(DOCS) $(MKFILES) $(CONFIGFILES) $(SOURCES) $(INCLUDES) \ | |
1990 | $(CONFIGUREFILES) $(OTHERFILES) $(TESTFILES) | |
1991 | # library object files common to compression and decompression | |
1992 | -COMOBJECTS= jcomapi.$(O) jutils.$(O) jerror.$(O) jmemmgr.$(O) $(SYSDEPMEM) | |
1993 | +COMOBJECTS= jcomapi.$(O) jutils.$(O) jerror.$(O) jmemmgr.$(O) jaricom.$(O) $(SYSDEPMEM) | |
1994 | # compression library object files | |
1995 | CLIBOBJECTS= jcapimin.$(O) jcapistd.$(O) jctrans.$(O) jcparam.$(O) \ | |
1996 | jdatadst.$(O) jcinit.$(O) jcmaster.$(O) jcmarker.$(O) jcmainct.$(O) \ | |
1997 | jcprepct.$(O) jccoefct.$(O) jccolor.$(O) jcsample.$(O) jchuff.$(O) \ | |
1998 | jcphuff.$(O) jcdctmgr.$(O) jfdctfst.$(O) jfdctflt.$(O) \ | |
1999 | - jfdctint.$(O) | |
2000 | + jfdctint.$(O) jcarith.$(O) | |
2001 | # decompression library object files | |
2002 | DLIBOBJECTS= jdapimin.$(O) jdapistd.$(O) jdtrans.$(O) jdatasrc.$(O) \ | |
2003 | jdmaster.$(O) jdinput.$(O) jdmarker.$(O) jdhuff.$(O) jdphuff.$(O) \ | |
2004 | jdmainct.$(O) jdcoefct.$(O) jdpostct.$(O) jddctmgr.$(O) \ | |
2005 | jidctfst.$(O) jidctflt.$(O) jidctint.$(O) jidctred.$(O) \ | |
2006 | - jdsample.$(O) jdcolor.$(O) jquant1.$(O) jquant2.$(O) jdmerge.$(O) | |
2007 | + jdsample.$(O) jdcolor.$(O) jquant1.$(O) jquant2.$(O) jdmerge.$(O) jdarith.$(O) | |
2008 | # These objectfiles are included in libjpeg.a | |
2009 | LIBOBJECTS= $(CLIBOBJECTS) $(DLIBOBJECTS) $(COMOBJECTS) | |
2010 | # object files for sample applications (excluding library files) | |
2011 | @@ -317,3 +317,6 @@ | |
2012 | wrbmp.$(O): wrbmp.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h | |
2013 | rdrle.$(O): rdrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h | |
2014 | wrrle.$(O): wrrle.c cdjpeg.h jinclude.h jconfig.h jpeglib.h jmorecfg.h jerror.h cderror.h | |
2015 | +jcarith.$(O): jcarith.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h | |
2016 | +jdarith.$(O): jdarith.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h | |
2017 | +jaricom.$(O): jaricom.c jinclude.h jconfig.h jpeglib.h jmorecfg.h jpegint.h jerror.h | |
2018 | --- jpeg-6b/jmorecfg.h.orig 1997-08-10 01:58:56.000000000 +0200 | |
2019 | +++ jpeg-6b/jmorecfg.h 2008-01-27 20:59:01.245915635 +0100 | |
2020 | @@ -266,7 +266,7 @@ | |
2021 | ||
2022 | /* Encoder capability options: */ | |
2023 | ||
2024 | -#undef C_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */ | |
2025 | +#define C_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */ | |
2026 | #define C_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */ | |
2027 | #define C_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/ | |
2028 | #define ENTROPY_OPT_SUPPORTED /* Optimization of entropy coding parms? */ | |
2029 | @@ -282,7 +282,7 @@ | |
2030 | ||
2031 | /* Decoder capability options: */ | |
2032 | ||
2033 | -#undef D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */ | |
2034 | +#define D_ARITH_CODING_SUPPORTED /* Arithmetic coding back end? */ | |
2035 | #define D_MULTISCAN_FILES_SUPPORTED /* Multiple-scan JPEG files? */ | |
2036 | #define D_PROGRESSIVE_SUPPORTED /* Progressive JPEG? (Requires MULTISCAN)*/ | |
2037 | #define SAVE_MARKERS_SUPPORTED /* jpeg_save_markers() needed? */ | |
2038 | --- jpeg-6b/README.arithmetic.orig 1970-01-01 01:00:00.000000000 +0100 | |
2039 | +++ jpeg-6b/README.arithmetic 2001-04-24 22:15:35.000000000 +0200 | |
2040 | @@ -0,0 +1,215 @@ | |
2041 | +JPEG arithmetic encoding and decoding portable software implementation | |
2042 | +====================================================================== | |
2043 | + | |
2044 | +Release of 28-Mar-98 by Guido Vollbeding <guido@jpegclub.org> | |
2045 | +============================================================= | |
2046 | + | |
2047 | +Primary URLs: | |
2048 | + | |
2049 | + http://sylvana.net/jpeg-ari/ | |
2050 | + (directory containing the actual archive files:) | |
2051 | + | |
2052 | + http://sylvana.net/jpeg-ari/jpeg-ari-28mar98.tar.gz | |
2053 | + | |
2054 | + http://sylvana.net/jpeg-ari/jpeg-ari.zip | |
2055 | + | |
2056 | + | |
2057 | +DISCLAIMER | |
2058 | +========== | |
2059 | + | |
2060 | +This package is distributed in the hope that it will be useful, | |
2061 | +but WITHOUT ANY WARRANTY; without even the implied warranty of | |
2062 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
2063 | + | |
2064 | +It is possible that certain products which can be built using this | |
2065 | +software modules might form inventions protected by patent rights in | |
2066 | +some countries (e.g. by patents about arithmetic coding algorithms | |
2067 | +owned by IBM and AT&T in the USA). Provision of this software by the | |
2068 | +author does NOT include any licenses for any patents. | |
2069 | +In those countries where a patent license is required for certain | |
2070 | +applications of this software modules, you will have to obtain such | |
2071 | +a license yourself. | |
2072 | + | |
2073 | +See Annex L in the JPEG spec for further information | |
2074 | +and a list of relevant patents. | |
2075 | + | |
2076 | + | |
2077 | +What is it? | |
2078 | +=========== | |
2079 | + | |
2080 | +This is my implementation of the arithmetic encoding and decoding | |
2081 | +back-end for JPEG as specified in the | |
2082 | + | |
2083 | + ISO/IEC International Standard 10918-1 and CCITT Recommendation | |
2084 | + ITU-T T.81, "Information Technology - Digital Compression and | |
2085 | + Coding of Continuous-tone Still Images, Part 1: Requirements | |
2086 | + and Guidelines". | |
2087 | + | |
2088 | +Arithmetic coding is a state-of-the-art lossless entropy data | |
2089 | +compression method which offers better compression performance | |
2090 | +than the well-established Huffman entropy coding process. | |
2091 | + | |
2092 | +The JPEG standard specifies a particular arithmetic coding scheme | |
2093 | +to be used optionally as alternative to Huffman coding. | |
2094 | + | |
2095 | + | |
2096 | +Who needs it? | |
2097 | +============= | |
2098 | + | |
2099 | +This package might be of interest for people who are looking for | |
2100 | +enhanced state-of-the-art image compression technologies. | |
2101 | + | |
2102 | +It is intended to provide a reasonable tool for experimental, | |
2103 | +comparison and evaluation purposes. | |
2104 | + | |
2105 | +See the Disclaimer above for restricted conditions of usage. | |
2106 | + | |
2107 | + | |
2108 | +How does it work? | |
2109 | +================= | |
2110 | + | |
2111 | +This distribution is organized as add-on to the widespread | |
2112 | +Independent JPEG Group's JPEG software. | |
2113 | + | |
2114 | +Thus, once you managed to install the IJG software distribution | |
2115 | +successfully, there should be no additional problems (portability | |
2116 | +issues etc.) to incorporate this package into the library, | |
2117 | +and usage is straightforward. | |
2118 | + | |
2119 | +Transcode given JPEG files simply with a command like | |
2120 | + | |
2121 | + jpegtran -arithmetic [-progressive] < orig.jpg > arit.jpg | |
2122 | + | |
2123 | +into an arithmetic coded version LOSSLESSLY! Since there are | |
2124 | +practically no applications in existence which can handle such | |
2125 | +files, you can only transform it back with the same tool | |
2126 | + | |
2127 | + jpegtran [-optimize] [-progressive] < arit.jpg > orig2.jpg | |
2128 | + | |
2129 | +to verify correct operation. | |
2130 | + | |
2131 | +Thus, you can easily verify the enhanced compression performance | |
2132 | +of the arithmetic coding version compared to the Huffman (with | |
2133 | +fixed or custom tables) version. | |
2134 | + | |
2135 | +The claim to evaluate was that arithmetic coding gives an average | |
2136 | +5-10% compression improvement against Huffman. | |
2137 | +Early tests with this implementation support this claim, and you | |
2138 | +can perform tests with own material. | |
2139 | + | |
2140 | +Here are some actual results: | |
2141 | + | |
2142 | +% ./jpegtran -optimize < testorig.jpg > testopt.jpg | |
2143 | +% ./jpegtran -arithmetic < testorig.jpg > testarit.jpg | |
2144 | +% ./jpegtran < testarit.jpg > testorig2.jpg | |
2145 | +% ./jpegtran -arithmetic -progressive < testorig.jpg > testaritp.jpg | |
2146 | +% ./jpegtran < testaritp.jpg > testorig3.jpg | |
2147 | +% ./jpegtran -optimize < ../butterfly.jpg > ../buttopt.jpg | |
2148 | +% ./jpegtran -progressive < ../butterfly.jpg > ../buttprog.jpg | |
2149 | +% ./jpegtran -arithmetic < ../butterfly.jpg > ../buttarit.jpg | |
2150 | +% ./jpegtran < ../buttarit.jpg > ../butterfly2.jpg | |
2151 | +% ./jpegtran -arithmetic -progressive < ../butterfly.jpg > ../buttaritp.jpg | |
2152 | +% ./jpegtran < ../buttaritp.jpg > ../butterfly3.jpg | |
2153 | +% ls -l test*.jpg | |
2154 | +-rw-r--r-- 1 guivol 5153 Apr 13 18:51 testarit.jpg | |
2155 | +-rw-r--r-- 1 guivol 5186 Apr 13 18:51 testaritp.jpg | |
2156 | +-rw-r--r-- 1 guivol 5756 Apr 2 15:10 testimg.jpg | |
2157 | +-rw-r--r-- 1 guivol 5645 Apr 2 15:10 testimgp.jpg | |
2158 | +-rw-r--r-- 1 guivol 5463 Apr 13 18:51 testopt.jpg | |
2159 | +-rw-r--r-- 1 guivol 5770 Apr 2 15:10 testorig.jpg | |
2160 | +-rw-r--r-- 1 guivol 5770 Apr 13 18:51 testorig2.jpg | |
2161 | +-rw-r--r-- 1 guivol 5770 Apr 13 18:51 testorig3.jpg | |
2162 | +-rw-r--r-- 1 guivol 5655 Apr 2 15:10 testprog.jpg | |
2163 | +% ls -l ../butt*.jpg | |
2164 | +-rw-r--r-- 1 guivol 460091 Apr 13 18:52 ../buttarit.jpg | |
2165 | +-rw-r--r-- 1 guivol 453703 Apr 13 18:52 ../buttaritp.jpg | |
2166 | +-rw-r--r-- 1 guivol 527823 Nov 19 18:41 ../butterfly.jpg | |
2167 | +-rw-r--r-- 1 guivol 527823 Apr 13 18:52 ../butterfly2.jpg | |
2168 | +-rw-r--r-- 1 guivol 527823 Apr 13 18:52 ../butterfly3.jpg | |
2169 | +-rw-r--r-- 1 guivol 511834 Apr 13 18:52 ../buttopt.jpg | |
2170 | +-rw-r--r-- 1 guivol 492237 Apr 13 18:52 ../buttprog.jpg | |
2171 | +% | |
2172 | + | |
2173 | +Note that arithmetic coding requires only a single processing | |
2174 | +pass due to its fully-adaptive nature, and compared to one-pass | |
2175 | +(fixed tables) Huffman the arithmetic coded version consistently | |
2176 | +achieves 10% compression improvement. | |
2177 | +Compared with two-pass (custom tables) Huffman the improvement | |
2178 | +is 5-10%. | |
2179 | + | |
2180 | +Note that I wasn't able yet to cross-check interoperability of | |
2181 | +the produced files with other implementations. | |
2182 | +Thus, I can't be sure that the files are compliant to the spec, | |
2183 | +but I hope so and the tests support it. | |
2184 | +The encoding and decoding processes should be correct anyway, | |
2185 | +however, in the sense that they are complementary to each other | |
2186 | +and thus retain data integrity. | |
2187 | + | |
2188 | +I would appreciate any indications for compliance or interoperability | |
2189 | +with other implementations from somebody. | |
2190 | +Please let me know if you are able to cross-check something. | |
2191 | + | |
2192 | + | |
2193 | +Installation | |
2194 | +============ | |
2195 | + | |
2196 | +The installation is a 2-stage procedure: | |
2197 | + | |
2198 | +1. Preparing the IJG package for potential incorporation | |
2199 | + of the arithmetic coding feature. | |
2200 | + | |
2201 | +2. Incorporation of the actual arithmetic coding modules | |
2202 | + and enabling the feature for usage. | |
2203 | + | |
2204 | +The reason for this 2-stage process is the hope to make | |
2205 | +step 1 obsolete in future IJG releases. | |
2206 | +The actual implementation should remain separate IMHO due | |
2207 | +to the different usage conditions. | |
2208 | + | |
2209 | +Step 1: | |
2210 | + | |
2211 | +1.1. Copy all files from the subdirectory 'patchv6b' into | |
2212 | + the IJG software's v6b source directory. | |
2213 | + This includes minor patches to some files and 3 extra | |
2214 | + files which hold place for the actual implementation. | |
2215 | + | |
2216 | +1.2. Update your Makefile/Projectfile for the inclusion of | |
2217 | + the 3 extra files. This will be done automatically | |
2218 | + if you use a configure-generated makefile and type | |
2219 | + './configure' (reconfigure). | |
2220 | + | |
2221 | +1.3. Recompile ('make'). | |
2222 | + | |
2223 | +See the file 'PATCHES' in 'patchv6b' for details. | |
2224 | + | |
2225 | +Step 2: | |
2226 | + | |
2227 | +2.1. Replace the 3 placeholder files by the actual implementation | |
2228 | + modules. | |
2229 | + | |
2230 | +2.2. Enable application support of the new features by #defining | |
2231 | + C_ARITH_CODING_SUPPORTED and D_ARITH_CODING_SUPPORTED | |
2232 | + in 'jmorecfg.h'. | |
2233 | + | |
2234 | +2.3. Recompile ('make'). | |
2235 | + | |
2236 | +Note that I suggest to add 3 placeholder files to the IJG | |
2237 | +distribution. This would remove the need for system-dependent | |
2238 | +changes (Makefiles) and thus considerably simplify the actual | |
2239 | +installation for systems without a configure-generated makefile. | |
2240 | + | |
2241 | + | |
2242 | +References | |
2243 | +========== | |
2244 | + | |
2245 | +- The Independent JPEG Group's software | |
2246 | + | |
2247 | +- JBIG-KIT lossless image compression library by Markus Kuhn | |
2248 | + | |
2249 | +- William B. Pennebaker, Joan L. Mitchell: | |
2250 | + "JPEG Still Image Data Compression Standard", | |
2251 | + Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1. | |
2252 | + | |
2253 | +- jpeg-faq (http://www.faqs.org/faqs/jpeg-faq/) | |
2254 | + | |
2255 | +- compression-faq (http://www.faqs.org/faqs/compression-faq/) |