25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.image.JPEGDecoder;
|
|
14
|
|
15 import org.eclipse.swt.SWT;
|
|
16 import java.io.InputStream;
|
|
17 import org.eclipse.swt.internal.image.LEDataInputStream;
|
|
18 import org.eclipse.swt.graphics.ImageData;
|
|
19 import org.eclipse.swt.graphics.ImageLoader;
|
|
20 import org.eclipse.swt.graphics.ImageLoaderEvent;
|
|
21 import org.eclipse.swt.graphics.PaletteData;
|
|
22 import org.eclipse.swt.graphics.RGB;
|
|
23 import java.lang.all;
|
|
24
|
48
|
25 version(Tango){
|
25
|
26 import tango.util.Convert;
|
48
|
27 } else { // Phobos
|
|
28 }
|
25
|
29
|
|
30 public class JPEGDecoder {
|
|
31
|
|
32 static const int DCTSIZE = 8;
|
|
33 static const int DCTSIZE2 = 64;
|
|
34 static const int NUM_QUANT_TBLS = 4;
|
|
35 static const int NUM_HUFF_TBLS = 4;
|
|
36 static const int NUM_ARITH_TBLS = 16;
|
|
37 static const int MAX_COMPS_IN_SCAN = 4;
|
|
38 static const int MAX_COMPONENTS = 10;
|
|
39 static const int MAX_SAMP_FACTOR = 4;
|
|
40 static const int D_MAX_BLOCKS_IN_MCU = 10;
|
|
41 static const int HUFF_LOOKAHEAD = 8;
|
|
42 static const int MAX_Q_COMPS = 4;
|
|
43 static const int IFAST_SCALE_BITS = 2;
|
|
44 static const int MAXJSAMPLE = 255;
|
|
45 static const int CENTERJSAMPLE = 128;
|
|
46 static const int MIN_GET_BITS = 32-7;
|
|
47 static const int INPUT_BUFFER_SIZE = 4096;
|
|
48
|
|
49 static const int SCALEBITS = 16; /* speediest right-shift on some machines */
|
|
50 static const int ONE_HALF = 1 << (SCALEBITS-1);
|
|
51
|
|
52 static const int RGB_RED = 2; /* Offset of Red in an RGB scanline element */
|
|
53 static const int RGB_GREEN = 1; /* Offset of Green */
|
|
54 static const int RGB_BLUE = 0; /* Offset of Blue */
|
|
55 static const int RGB_PIXELSIZE = 3;
|
|
56
|
|
57 static const int JBUF_PASS_THRU = 0;
|
|
58 static const int JBUF_SAVE_SOURCE = 1; /* Run source subobject only, save output */
|
|
59 static const int JBUF_CRANK_DEST = 2; /* Run dest subobject only, using saved data */
|
|
60 static const int JBUF_SAVE_AND_PASS = 3;
|
|
61
|
|
62 static const int JPEG_MAX_DIMENSION = 65500;
|
|
63 static const int BITS_IN_JSAMPLE = 8;
|
|
64
|
|
65 static const int JDITHER_NONE = 0; /* no dithering */
|
|
66 static const int JDITHER_ORDERED = 1; /* simple ordered dither */
|
|
67 static const int JDITHER_FS = 2;
|
|
68
|
|
69 static const int JDCT_ISLOW = 0; /* slow but accurate integer algorithm */
|
|
70 static const int JDCT_IFAST = 1; /* faster, less accurate integer method */
|
|
71 static const int JDCT_FLOAT = 2; /* floating-point: accurate, fast on fast HW */
|
|
72 static const int JDCT_DEFAULT = JDCT_ISLOW;
|
|
73
|
|
74 static const int JCS_UNKNOWN = 0; /* error/unspecified */
|
|
75 static const int JCS_GRAYSCALE = 1; /* monochrome */
|
|
76 static const int JCS_RGB = 2; /* red/green/blue */
|
|
77 static const int JCS_YCbCr = 3; /* Y/Cb/Cr (also known as YUV) */
|
|
78 static const int JCS_CMYK = 4; /* C/M/Y/K */
|
|
79 static const int JCS_YCCK = 5; /* Y/Cb/Cr/K */
|
|
80
|
|
81 static const int SAVED_COEFS = 6;
|
|
82 static const int Q01_POS = 1;
|
|
83 static const int Q10_POS = 8;
|
|
84 static const int Q20_POS = 16;
|
|
85 static const int Q11_POS = 9;
|
|
86 static const int Q02_POS = 2;
|
|
87
|
|
88 static const int CTX_PREPARE_FOR_IMCU = 0; /* need to prepare for MCU row */
|
|
89 static const int CTX_PROCESS_IMCU = 1; /* feeding iMCU to postprocessor */
|
|
90 static const int CTX_POSTPONED_ROW = 2; /* feeding postponed row group */
|
|
91
|
|
92 static const int APP0_DATA_LEN = 14; /* Length of interesting data in APP0 */
|
|
93 static const int APP14_DATA_LEN = 12; /* Length of interesting data in APP14 */
|
|
94 static const int APPN_DATA_LEN = 14; /* Must be the largest of the above!! */
|
|
95
|
|
96 /* markers */
|
|
97 static const int M_SOF0 = 0xc0;
|
|
98 static const int M_SOF1 = 0xc1;
|
|
99 static const int M_SOF2 = 0xc2;
|
|
100 static const int M_SOF3 = 0xc3;
|
|
101 static const int M_SOF5 = 0xc5;
|
|
102 static const int M_SOF6 = 0xc6;
|
|
103 static const int M_SOF7 = 0xc7;
|
|
104 static const int M_JPG = 0xc8;
|
|
105 static const int M_SOF9 = 0xc9;
|
|
106 static const int M_SOF10 = 0xca;
|
|
107 static const int M_SOF11 = 0xcb;
|
|
108 static const int M_SOF13 = 0xcd;
|
|
109 static const int M_SOF14 = 0xce;
|
|
110 static const int M_SOF15 = 0xcf;
|
|
111 static const int M_DHT = 0xc4;
|
|
112 static const int M_DAC = 0xcc;
|
|
113 static const int M_RST0 = 0xd0;
|
|
114 static const int M_RST1 = 0xd1;
|
|
115 static const int M_RST2 = 0xd2;
|
|
116 static const int M_RST3 = 0xd3;
|
|
117 static const int M_RST4 = 0xd4;
|
|
118 static const int M_RST5 = 0xd5;
|
|
119 static const int M_RST6 = 0xd6;
|
|
120 static const int M_RST7 = 0xd7;
|
|
121 static const int M_SOI = 0xd8;
|
|
122 static const int M_EOI = 0xd9;
|
|
123 static const int M_SOS = 0xda;
|
|
124 static const int M_DQT = 0xdb;
|
|
125 static const int M_DNL = 0xdc;
|
|
126 static const int M_DRI = 0xdd;
|
|
127 static const int M_DHP = 0xde;
|
|
128 static const int M_EXP = 0xdf;
|
|
129 static const int M_APP0 = 0xe0;
|
|
130 static const int M_APP1 = 0xe1;
|
|
131 static const int M_APP2 = 0xe2;
|
|
132 static const int M_APP3 = 0xe3;
|
|
133 static const int M_APP4 = 0xe4;
|
|
134 static const int M_APP5 = 0xe5;
|
|
135 static const int M_APP6 = 0xe6;
|
|
136 static const int M_APP7 = 0xe7;
|
|
137 static const int M_APP8 = 0xe8;
|
|
138 static const int M_APP9 = 0xe9;
|
|
139 static const int M_APP10 = 0xea;
|
|
140 static const int M_APP11 = 0xeb;
|
|
141 static const int M_APP12 = 0xec;
|
|
142 static const int M_APP13 = 0xed;
|
|
143 static const int M_APP14 = 0xee;
|
|
144 static const int M_APP15 = 0xef;
|
|
145 static const int M_JPG0 = 0xf0;
|
|
146 static const int M_JPG13 = 0xfd;
|
|
147 static const int M_COM = 0xfe;
|
|
148 static const int M_TEM = 0x01;
|
|
149 static const int M_ERROR = 0x100;
|
|
150
|
|
151 /* Values of global_state field (jdapi.c has some dependencies on ordering!) */
|
|
152 static const int CSTATE_START = 100; /* after create_compress */
|
|
153 static const int CSTATE_SCANNING = 101; /* start_compress done, write_scanlines OK */
|
|
154 static const int CSTATE_RAW_OK = 102; /* start_compress done, write_raw_data OK */
|
|
155 static const int CSTATE_WRCOEFS = 103; /* jpeg_write_coefficients done */
|
|
156 static const int DSTATE_START = 200; /* after create_decompress */
|
|
157 static const int DSTATE_INHEADER = 201; /* reading header markers, no SOS yet */
|
|
158 static const int DSTATE_READY = 202; /* found SOS, ready for start_decompress */
|
|
159 static const int DSTATE_PRELOAD = 203; /* reading multiscan file in start_decompress*/
|
|
160 static const int DSTATE_PRESCAN = 204; /* performing dummy pass for 2-pass quant */
|
|
161 static const int DSTATE_SCANNING = 205; /* start_decompress done, read_scanlines OK */
|
|
162 static const int DSTATE_RAW_OK = 206; /* start_decompress done, read_raw_data OK */
|
|
163 static const int DSTATE_BUFIMAGE = 207; /* expecting jpeg_start_output */
|
|
164 static const int DSTATE_BUFPOST = 208; /* looking for SOS/EOI in jpeg_finish_output */
|
|
165 static const int DSTATE_RDCOEFS = 209; /* reading file in jpeg_read_coefficients */
|
|
166 static const int DSTATE_STOPPING = 210; /* looking for EOI in jpeg_finish_decompress */
|
|
167
|
|
168 static const int JPEG_REACHED_SOS = 1; /* Reached start of new scan */
|
|
169 static const int JPEG_REACHED_EOI = 2; /* Reached end of image */
|
|
170 static const int JPEG_ROW_COMPLETED = 3; /* Completed one iMCU row */
|
|
171 static const int JPEG_SCAN_COMPLETED = 4; /* Completed last iMCU row of a scan */
|
|
172
|
|
173 static const int JPEG_SUSPENDED = 0; /* Suspended due to lack of input data */
|
|
174 static const int JPEG_HEADER_OK = 1; /* Found valid image datastream */
|
|
175 static const int JPEG_HEADER_TABLES_ONLY = 2; /* Found valid table-specs-only datastream */
|
|
176
|
|
177 /* Function pointers */
|
|
178 static const int DECOMPRESS_DATA = 0;
|
|
179 static const int DECOMPRESS_SMOOTH_DATA = 1;
|
|
180 static const int DECOMPRESS_ONEPASS = 2;
|
|
181
|
|
182 static const int CONSUME_DATA = 0;
|
|
183 static const int DUMMY_CONSUME_DATA = 1;
|
|
184
|
|
185 static const int PROCESS_DATA_SIMPLE_MAIN = 0;
|
|
186 static const int PROCESS_DATA_CONTEXT_MAIN = 1;
|
|
187 static const int PROCESS_DATA_CRANK_POST = 2;
|
|
188
|
|
189 static const int POST_PROCESS_1PASS = 0;
|
|
190 static const int POST_PROCESS_DATA_UPSAMPLE = 1;
|
|
191
|
|
192 static const int NULL_CONVERT = 0;
|
|
193 static const int GRAYSCALE_CONVERT = 1;
|
|
194 static const int YCC_RGB_CONVERT = 2;
|
|
195 static const int GRAY_RGB_CONVERT = 3;
|
|
196 static const int YCCK_CMYK_CONVERT = 4;
|
|
197
|
|
198 static const int NOOP_UPSAMPLE = 0;
|
|
199 static const int FULLSIZE_UPSAMPLE = 1;
|
|
200 static const int H2V1_FANCY_UPSAMPLE = 2;
|
|
201 static const int H2V1_UPSAMPLE = 3;
|
|
202 static const int H2V2_FANCY_UPSAMPLE = 4;
|
|
203 static const int H2V2_UPSAMPLE = 5;
|
|
204 static const int INT_UPSAMPLE = 6;
|
|
205
|
|
206 static const int INPUT_CONSUME_INPUT = 0;
|
|
207 static const int COEF_CONSUME_INPUT = 1;
|
|
208
|
|
209 static int extend_test[] = /* entry n is 2**(n-1) */
|
|
210 [
|
|
211 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
|
|
212 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
|
|
213 ];
|
|
214
|
|
215 static int extend_offset[] = /* entry n is (-1 << n) + 1 */
|
|
216 [
|
|
217 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
|
|
218 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
|
|
219 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
|
|
220 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1
|
|
221 ];
|
|
222
|
|
223 static int jpeg_natural_order[] = [
|
|
224 0, 1, 8, 16, 9, 2, 3, 10,
|
|
225 17, 24, 32, 25, 18, 11, 4, 5,
|
|
226 12, 19, 26, 33, 40, 48, 41, 34,
|
|
227 27, 20, 13, 6, 7, 14, 21, 28,
|
|
228 35, 42, 49, 56, 57, 50, 43, 36,
|
|
229 29, 22, 15, 23, 30, 37, 44, 51,
|
|
230 58, 59, 52, 45, 38, 31, 39, 46,
|
|
231 53, 60, 61, 54, 47, 55, 62, 63,
|
|
232 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
|
|
233 63, 63, 63, 63, 63, 63, 63, 63
|
|
234 ];
|
|
235
|
|
236 static final class JQUANT_TBL {
|
|
237 /* This array gives the coefficient quantizers in natural array order
|
|
238 * (not the zigzag order in which they are stored in a JPEG DQT marker).
|
|
239 * CAUTION: IJG versions prior to v6a kept this array in zigzag order.
|
|
240 */
|
|
241 short[DCTSIZE2] quantval;// = new short[DCTSIZE2]; /* quantization step for each coefficient */
|
|
242 /* This field is used only during compression. It's initialized false when
|
|
243 * the table is created, and set true when it's been output to the file.
|
|
244 * You could suppress output of a table by setting this to true.
|
|
245 * (See jpeg_suppress_tables for an example.)
|
|
246 */
|
|
247 bool sent_table; /* true when table has been output */
|
|
248 }
|
|
249
|
|
250 static final class JHUFF_TBL {
|
|
251 /* These two fields directly represent the contents of a JPEG DHT marker */
|
|
252 byte[17] bits;// = new byte[17]; /* bits[k] = # of symbols with codes of */
|
|
253 /* length k bits; bits[0] is unused */
|
|
254 byte[256] huffval;// = new byte[256]; /* The symbols, in order of incr code length */
|
|
255 /* This field is used only during compression. It's initialized false when
|
|
256 * the table is created, and set true when it's been output to the file.
|
|
257 * You could suppress output of a table by setting this to true.
|
|
258 * (See jpeg_suppress_tables for an example.)
|
|
259 */
|
|
260 bool sent_table; /* true when table has been output */
|
|
261 }
|
|
262
|
|
263 static final class bitread_perm_state { /* Bitreading state saved across MCUs */
|
|
264 int get_buffer; /* current bit-extraction buffer */
|
|
265 int bits_left; /* # of unused bits in it */
|
|
266 }
|
|
267
|
|
268 static final class bitread_working_state { /* Bitreading working state within an MCU */
|
|
269 /* Current data source location */
|
|
270 /* We need a copy, rather than munging the original, in case of suspension */
|
|
271 byte[] buffer; /* => next byte to read from source */
|
|
272 int bytes_offset;
|
|
273 int bytes_in_buffer; /* # of bytes remaining in source buffer */
|
|
274 /* Bit input buffer --- note these values are kept in register variables,
|
|
275 * not in this struct, inside the inner loops.
|
|
276 */
|
|
277 int get_buffer; /* current bit-extraction buffer */
|
|
278 int bits_left; /* # of unused bits in it */
|
|
279 /* Pointer needed by jpeg_fill_bit_buffer. */
|
|
280 jpeg_decompress_struct cinfo; /* back link to decompress master record */
|
|
281 }
|
|
282
|
|
283 static final class savable_state {
|
|
284 int EOBRUN; //Note that this is only used in the progressive case
|
|
285 int[MAX_COMPS_IN_SCAN] last_dc_val;// = new int[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
|
286 }
|
|
287
|
|
288 static final class d_derived_tbl {
|
|
289 /* Basic tables: (element [0] of each array is unused) */
|
|
290 int[18] maxcode;// = new int[18]; /* largest code of length k (-1 if none) */
|
|
291 /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
|
|
292 int[17] valoffset;// = new int[17]; /* huffval[] offset for codes of length k */
|
|
293 /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
|
|
294 * the smallest code of length k; so given a code of length k, the
|
|
295 * corresponding symbol is huffval[code + valoffset[k]]
|
|
296 */
|
|
297
|
|
298 /* Link to public Huffman table (needed only in jpeg_huff_decode) */
|
|
299 JHUFF_TBL pub;
|
|
300
|
|
301 /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
|
|
302 * the input data stream. If the next Huffman code is no more
|
|
303 * than HUFF_LOOKAHEAD bits long, we can obtain its length and
|
|
304 * the corresponding symbol directly from these tables.
|
|
305 */
|
|
306 int[1<<HUFF_LOOKAHEAD] look_nbits;// = new int[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
|
|
307 byte[1<<HUFF_LOOKAHEAD] look_sym;// = new byte[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
|
|
308 }
|
|
309
|
|
310 static final class jpeg_d_coef_controller {
|
|
311 int consume_data;
|
|
312 int decompress_data;
|
|
313
|
|
314 /* Pointer to array of coefficient virtual arrays, or null if none */
|
|
315 short[][][] coef_arrays;
|
|
316
|
|
317 /* These variables keep track of the current location of the input side. */
|
|
318 /* cinfo.input_iMCU_row is also used for this. */
|
|
319 int MCU_ctr; /* counts MCUs processed in current row */
|
|
320 int MCU_vert_offset; /* counts MCU rows within iMCU row */
|
|
321 int MCU_rows_per_iMCU_row; /* number of such rows needed */
|
|
322
|
|
323 /* The output side's location is represented by cinfo.output_iMCU_row. */
|
|
324
|
|
325 /* In single-pass modes, it's sufficient to buffer just one MCU.
|
|
326 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
|
|
327 * and let the entropy decoder write into that workspace each time.
|
|
328 * (On 80x86, the workspace is FAR even though it's not really very big;
|
|
329 * this is to keep the module interfaces unchanged when a large coefficient
|
|
330 * buffer is necessary.)
|
|
331 * In multi-pass modes, this array points to the current MCU's blocks
|
|
332 * within the virtual arrays; it is used only by the input side.
|
|
333 */
|
|
334 short[][D_MAX_BLOCKS_IN_MCU] MCU_buffer;// = new short[D_MAX_BLOCKS_IN_MCU][];
|
|
335
|
|
336 /* In multi-pass modes, we need a virtual block array for each component. */
|
|
337 short[][][][MAX_COMPONENTS] whole_image;// = new short[MAX_COMPONENTS][][][];
|
|
338
|
|
339 /* When doing block smoothing, we latch coefficient Al values here */
|
|
340 int[] coef_bits_latch;
|
|
341
|
|
342 short[] workspace;
|
|
343
|
|
344 void start_input_pass (jpeg_decompress_struct cinfo) {
|
|
345 cinfo.input_iMCU_row = 0;
|
|
346 start_iMCU_row(cinfo);
|
|
347 }
|
|
348
|
|
349 /* Reset within-iMCU-row counters for a new row (input side) */
|
|
350 void start_iMCU_row (jpeg_decompress_struct cinfo) {
|
|
351 jpeg_d_coef_controller coef = cinfo.coef;
|
|
352
|
|
353 /* In an interleaved scan, an MCU row is the same as an iMCU row.
|
|
354 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
|
|
355 * But at the bottom of the image, process only what's left.
|
|
356 */
|
|
357 if (cinfo.comps_in_scan > 1) {
|
|
358 coef.MCU_rows_per_iMCU_row = 1;
|
|
359 } else {
|
|
360 if (cinfo.input_iMCU_row < (cinfo.total_iMCU_rows-1))
|
|
361 coef.MCU_rows_per_iMCU_row = cinfo.cur_comp_info[0].v_samp_factor;
|
|
362 else
|
|
363 coef.MCU_rows_per_iMCU_row = cinfo.cur_comp_info[0].last_row_height;
|
|
364 }
|
|
365
|
|
366 coef.MCU_ctr = 0;
|
|
367 coef.MCU_vert_offset = 0;
|
|
368 }
|
|
369
|
|
370 }
|
|
371
|
|
372 static abstract class jpeg_entropy_decoder {
|
|
373 abstract void start_pass (jpeg_decompress_struct cinfo);
|
|
374 abstract bool decode_mcu (jpeg_decompress_struct cinfo, short[][] MCU_data);
|
|
375
|
|
376 /* This is here to share code between baseline and progressive decoders; */
|
|
377 /* other modules probably should not use it */
|
|
378 bool insufficient_data; /* set true after emitting warning */
|
|
379
|
|
380 bitread_working_state br_state_local;
|
|
381 savable_state state_local;
|
|
382 public this(){
|
|
383 br_state_local = new bitread_working_state();
|
|
384 state_local = new savable_state();
|
|
385 }
|
|
386 }
|
|
387
|
|
388 static final class huff_entropy_decoder : jpeg_entropy_decoder {
|
|
389 bitread_perm_state bitstate;// = new bitread_perm_state(); /* Bit buffer at start of MCU */
|
|
390 savable_state saved;// = new savable_state(); /* Other state at start of MCU */
|
|
391
|
|
392 /* These fields are NOT loaded into local working state. */
|
|
393 int restarts_to_go; /* MCUs left in this restart interval */
|
|
394
|
|
395 /* Pointers to derived tables (these workspaces have image lifespan) */
|
|
396 d_derived_tbl[NUM_HUFF_TBLS] dc_derived_tbls;// = new d_derived_tbl[NUM_HUFF_TBLS];
|
|
397 d_derived_tbl[NUM_HUFF_TBLS] ac_derived_tbls;// = new d_derived_tbl[NUM_HUFF_TBLS];
|
|
398
|
|
399 /* Precalculated info set up by start_pass for use in decode_mcu: */
|
|
400
|
|
401 /* Pointers to derived tables to be used for each block within an MCU */
|
|
402 d_derived_tbl[D_MAX_BLOCKS_IN_MCU] dc_cur_tbls;// = new d_derived_tbl[D_MAX_BLOCKS_IN_MCU];
|
|
403 d_derived_tbl[D_MAX_BLOCKS_IN_MCU] ac_cur_tbls;// = new d_derived_tbl[D_MAX_BLOCKS_IN_MCU];
|
|
404 /* Whether we care about the DC and AC coefficient values for each block */
|
|
405 bool[D_MAX_BLOCKS_IN_MCU] dc_needed;// = new bool[D_MAX_BLOCKS_IN_MCU];
|
|
406 bool[D_MAX_BLOCKS_IN_MCU] ac_needed;// = new bool[D_MAX_BLOCKS_IN_MCU];
|
|
407
|
|
408 public this(){
|
|
409 bitstate = new bitread_perm_state(); /* Bit buffer at start of MCU */
|
|
410 saved = new savable_state(); /* Other state at start of MCU */
|
|
411 }
|
|
412
|
|
413 override void start_pass (jpeg_decompress_struct cinfo) {
|
|
414 start_pass_huff_decoder(cinfo);
|
|
415 }
|
|
416
|
|
417 override bool decode_mcu (jpeg_decompress_struct cinfo, short[][] MCU_data) {
|
|
418 huff_entropy_decoder entropy = this;
|
|
419 int blkn;
|
|
420 // BITREAD_STATE_VARS;
|
|
421 int get_buffer;
|
|
422 int bits_left;
|
|
423 // bitread_working_state br_state = new bitread_working_state();
|
|
424 // savable_state state = new savable_state();
|
|
425 bitread_working_state br_state = br_state_local;
|
|
426 savable_state state = state_local;
|
|
427
|
|
428 /* Process restart marker if needed; may have to suspend */
|
|
429 if (cinfo.restart_interval !is 0) {
|
|
430 if (entropy.restarts_to_go is 0)
|
|
431 if (! process_restart(cinfo))
|
|
432 return false;
|
|
433 }
|
|
434
|
|
435 /* If we've run out of data, just leave the MCU set to zeroes.
|
|
436 * This way, we return uniform gray for the remainder of the segment.
|
|
437 */
|
|
438 if (! entropy.insufficient_data) {
|
|
439
|
|
440 /* Load up working state */
|
|
441 // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
|
|
442 br_state.cinfo = cinfo;
|
|
443 br_state.buffer = cinfo.buffer;
|
|
444 br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
445 br_state.bytes_offset = cinfo.bytes_offset;
|
|
446 get_buffer = entropy.bitstate.get_buffer;
|
|
447 bits_left = entropy.bitstate.bits_left;
|
|
448
|
|
449 // ASSIGN_STATE(state, entropy.saved);
|
|
450 state.last_dc_val[0] = entropy.saved.last_dc_val[0];
|
|
451 state.last_dc_val[1] = entropy.saved.last_dc_val[1];
|
|
452 state.last_dc_val[2] = entropy.saved.last_dc_val[2];
|
|
453 state.last_dc_val[3] = entropy.saved.last_dc_val[3];
|
|
454
|
|
455 /* Outer loop handles each block in the MCU */
|
|
456
|
|
457 for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
|
|
458 short[] block = MCU_data[blkn];
|
|
459 d_derived_tbl dctbl = entropy.dc_cur_tbls[blkn];
|
|
460 d_derived_tbl actbl = entropy.ac_cur_tbls[blkn];
|
|
461 int s = 0, k, r;
|
|
462
|
|
463 /* Decode a single block's worth of coefficients */
|
|
464
|
|
465 /* Section F.2.2.1: decode the DC coefficient difference */
|
|
466 // HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
|
|
467 {
|
|
468 int nb = 0, look;
|
|
469 if (bits_left < HUFF_LOOKAHEAD) {
|
|
470 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) {
|
|
471 return false;
|
|
472 }
|
|
473 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
474 if (bits_left < HUFF_LOOKAHEAD) {
|
|
475 nb = 1;
|
|
476 // goto slowlabel;
|
|
477 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,dctbl,nb)) < 0) {
|
|
478 return false;
|
|
479 }
|
|
480 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
481 }
|
|
482 }
|
|
483 // look = PEEK_BITS(HUFF_LOOKAHEAD);
|
|
484 if (nb !is 1) {
|
|
485 look = (( (get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1<<(HUFF_LOOKAHEAD))-1));
|
|
486 if ((nb = dctbl.look_nbits[look]) !is 0) {
|
|
487 // DROP_BITS(nb);
|
|
488 bits_left -= nb;
|
|
489 s = dctbl.look_sym[look] & 0xFF;
|
|
490 } else {
|
|
491 nb = HUFF_LOOKAHEAD+1;
|
|
492 // slowlabel:
|
|
493 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,dctbl,nb)) < 0) {
|
|
494 return false;
|
|
495 }
|
|
496 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
497 }
|
|
498 }
|
|
499 }
|
|
500
|
|
501 if (s !is 0) {
|
|
502 // CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
503 {
|
|
504 if (bits_left < (s)) {
|
|
505 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) {
|
|
506 return false;
|
|
507 }
|
|
508 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
509 }
|
|
510 }
|
|
511 // r = GET_BITS(s);
|
|
512 r = (( (get_buffer >> (bits_left -= (s)))) & ((1<<(s))-1));
|
|
513 // s = HUFF_EXTEND(r, s);
|
|
514 s = ((r) < extend_test[s] ? (r) + extend_offset[s] : (r));
|
|
515 }
|
|
516
|
|
517 if (entropy.dc_needed[blkn]) {
|
|
518 /* Convert DC difference to actual value, update last_dc_val */
|
|
519 int ci = cinfo.MCU_membership[blkn];
|
|
520 s += state.last_dc_val[ci];
|
|
521 state.last_dc_val[ci] = s;
|
|
522 /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
|
|
523 block[0] = cast(short) s;
|
|
524 }
|
|
525
|
|
526 if (entropy.ac_needed[blkn]) {
|
|
527
|
|
528 /* Section F.2.2.2: decode the AC coefficients */
|
|
529 /* Since zeroes are skipped, output area must be cleared beforehand */
|
|
530 for (k = 1; k < DCTSIZE2; k++) {
|
|
531 // HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
|
|
532 {
|
|
533 int nb = 0, look;
|
|
534 if (bits_left < HUFF_LOOKAHEAD) {
|
|
535 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) {
|
|
536 return false;
|
|
537 }
|
|
538 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
539 if (bits_left < HUFF_LOOKAHEAD) {
|
|
540 nb = 1;
|
|
541 // goto slowlabel;
|
|
542 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,actbl,nb)) < 0) {
|
|
543 return false;
|
|
544 }
|
|
545 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
546 }
|
|
547 }
|
|
548 if (nb !is 1) {
|
|
549 // look = PEEK_BITS(HUFF_LOOKAHEAD);
|
|
550 look = (( (get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1<<(HUFF_LOOKAHEAD))-1));
|
|
551 if ((nb = actbl.look_nbits[look]) !is 0) {
|
|
552 // DROP_BITS(nb);
|
|
553 bits_left -= (nb);
|
|
554 s = actbl.look_sym[look] & 0xFF;
|
|
555 } else {
|
|
556 nb = HUFF_LOOKAHEAD+1;
|
|
557 // slowlabel:
|
|
558 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,actbl,nb)) < 0) {
|
|
559 return false;
|
|
560 }
|
|
561 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
562 }
|
|
563 }
|
|
564 }
|
|
565 r = s >> 4;
|
|
566 s &= 15;
|
|
567
|
|
568 if (s !is 0) {
|
|
569 k += r;
|
|
570 // CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
571 {
|
|
572 if (bits_left < (s)) {
|
|
573 if (!jpeg_fill_bit_buffer(br_state, get_buffer, bits_left, s)) {
|
|
574 return false;
|
|
575 }
|
|
576 get_buffer = br_state.get_buffer;
|
|
577 bits_left = br_state.bits_left;
|
|
578 }
|
|
579 }
|
|
580 // r = GET_BITS(s);
|
|
581 r = (((get_buffer >> (bits_left -= (s)))) & ((1 << (s)) - 1));
|
|
582 // s = HUFF_EXTEND(r, s);
|
|
583 s = ((r) < extend_test[s] ? (r) + extend_offset[s] : (r));
|
|
584 /*
|
|
585 * Output coefficient in natural (dezigzagged)
|
|
586 * order. Note: the extra entries in
|
|
587 * jpeg_natural_order[] will save us if k >=
|
|
588 * DCTSIZE2, which could happen if the data is
|
|
589 * corrupted.
|
|
590 */
|
|
591 block[jpeg_natural_order[k]] = cast(short) s;
|
|
592 } else {
|
|
593 if (r !is 15)
|
|
594 break;
|
|
595 k += 15;
|
|
596 }
|
|
597 }
|
|
598
|
|
599 } else {
|
|
600
|
|
601 /* Section F.2.2.2: decode the AC coefficients */
|
|
602 /* In this path we just discard the values */
|
|
603 for (k = 1; k < DCTSIZE2; k++) {
|
|
604 // HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
|
|
605 {
|
|
606 int nb = 0, look;
|
|
607 if (bits_left < HUFF_LOOKAHEAD) {
|
|
608 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) {
|
|
609 return false;
|
|
610 }
|
|
611 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
612 if (bits_left < HUFF_LOOKAHEAD) {
|
|
613 nb = 1;
|
|
614 // goto slowlabel;
|
|
615 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,actbl,nb)) < 0) {
|
|
616 return false;
|
|
617 }
|
|
618 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
619 }
|
|
620 }
|
|
621 if (nb !is 1) {
|
|
622 // look = PEEK_BITS(HUFF_LOOKAHEAD);
|
|
623 look = (( (get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1<<(HUFF_LOOKAHEAD))-1));
|
|
624 if ((nb = actbl.look_nbits[look]) !is 0) {
|
|
625 // DROP_BITS(nb);
|
|
626 bits_left -= (nb);
|
|
627 s = actbl.look_sym[look] & 0xFF;
|
|
628 } else {
|
|
629 nb = HUFF_LOOKAHEAD+1;
|
|
630 // slowlabel:
|
|
631 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,actbl,nb)) < 0) {
|
|
632 return false;
|
|
633 }
|
|
634 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
635 }
|
|
636 }
|
|
637 }
|
|
638 r = s >> 4;
|
|
639 s &= 15;
|
|
640
|
|
641 if (s !is 0) {
|
|
642 k += r;
|
|
643 // CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
644 {
|
|
645 if (bits_left < (s)) {
|
|
646 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) {
|
|
647 return false;
|
|
648 }
|
|
649 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
650 }
|
|
651 }
|
|
652 // DROP_BITS(s);
|
|
653 bits_left -= s;
|
|
654 } else {
|
|
655 if (r !is 15)
|
|
656 break;
|
|
657 k += 15;
|
|
658 }
|
|
659 }
|
|
660
|
|
661 }
|
|
662 }
|
|
663
|
|
664 /* Completed MCU, so update state */
|
|
665 // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
|
|
666 cinfo.buffer = br_state.buffer;
|
|
667 cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
|
|
668 cinfo.bytes_offset = br_state.bytes_offset;
|
|
669 entropy.bitstate.get_buffer = get_buffer;
|
|
670 entropy.bitstate.bits_left = bits_left;
|
|
671 // ASSIGN_STATE(entropy.saved, state);
|
|
672 entropy.saved.last_dc_val[0] = state.last_dc_val[0];
|
|
673 entropy.saved.last_dc_val[1] = state.last_dc_val[1];
|
|
674 entropy.saved.last_dc_val[2] = state.last_dc_val[2];
|
|
675 entropy.saved.last_dc_val[3] = state.last_dc_val[3];
|
|
676 }
|
|
677
|
|
678 /* Account for restart interval (no-op if not using restarts) */
|
|
679 entropy.restarts_to_go--;
|
|
680
|
|
681 return true;
|
|
682 }
|
|
683
|
|
684 void start_pass_huff_decoder (jpeg_decompress_struct cinfo) {
|
|
685 huff_entropy_decoder entropy = this;
|
|
686 int ci, blkn, dctbl, actbl;
|
|
687 jpeg_component_info compptr;
|
|
688
|
|
689 /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
|
|
690 * This ought to be an error condition, but we make it a warning because
|
|
691 * there are some baseline files out there with all zeroes in these bytes.
|
|
692 */
|
|
693 if (cinfo.Ss !is 0 || cinfo.Se !is DCTSIZE2-1 || cinfo.Ah !is 0 || cinfo.Al !is 0) {
|
|
694 // WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
|
|
695 }
|
|
696
|
|
697 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
698 compptr = cinfo.cur_comp_info[ci];
|
|
699 dctbl = compptr.dc_tbl_no;
|
|
700 actbl = compptr.ac_tbl_no;
|
|
701 /* Compute derived values for Huffman tables */
|
|
702 /* We may do this more than once for a table, but it's not expensive */
|
|
703 jpeg_make_d_derived_tbl(cinfo, true, dctbl, entropy.dc_derived_tbls[dctbl] = new d_derived_tbl());
|
|
704 jpeg_make_d_derived_tbl(cinfo, false, actbl, entropy.ac_derived_tbls[actbl] = new d_derived_tbl());
|
|
705 /* Initialize DC predictions to 0 */
|
|
706 entropy.saved.last_dc_val[ci] = 0;
|
|
707 }
|
|
708
|
|
709 /* Precalculate decoding info for each block in an MCU of this scan */
|
|
710 for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
|
|
711 ci = cinfo.MCU_membership[blkn];
|
|
712 compptr = cinfo.cur_comp_info[ci];
|
|
713 /* Precalculate which table to use for each block */
|
|
714 entropy.dc_cur_tbls[blkn] = entropy.dc_derived_tbls[compptr.dc_tbl_no];
|
|
715 entropy.ac_cur_tbls[blkn] = entropy.ac_derived_tbls[compptr.ac_tbl_no];
|
|
716 /* Decide whether we really care about the coefficient values */
|
|
717 if (compptr.component_needed) {
|
|
718 entropy.dc_needed[blkn] = true;
|
|
719 /* we don't need the ACs if producing a 1/8th-size image */
|
|
720 entropy.ac_needed[blkn] = (compptr.DCT_scaled_size > 1);
|
|
721 } else {
|
|
722 entropy.dc_needed[blkn] = entropy.ac_needed[blkn] = false;
|
|
723 }
|
|
724 }
|
|
725
|
|
726 /* Initialize bitread state variables */
|
|
727 entropy.bitstate.bits_left = 0;
|
|
728 entropy.bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
|
|
729 entropy.insufficient_data = false;
|
|
730
|
|
731 /* Initialize restart counter */
|
|
732 entropy.restarts_to_go = cinfo.restart_interval;
|
|
733 }
|
|
734
|
|
735 bool process_restart (jpeg_decompress_struct cinfo) {
|
|
736 huff_entropy_decoder entropy = this;
|
|
737 int ci;
|
|
738
|
|
739 /* Throw away any unused bits remaining in bit buffer; */
|
|
740 /* include any full bytes in next_marker's count of discarded bytes */
|
|
741 cinfo.marker.discarded_bytes += entropy.bitstate.bits_left / 8;
|
|
742 entropy.bitstate.bits_left = 0;
|
|
743
|
|
744 /* Advance past the RSTn marker */
|
|
745 if (! read_restart_marker (cinfo))
|
|
746 return false;
|
|
747
|
|
748 /* Re-initialize DC predictions to 0 */
|
|
749 for (ci = 0; ci < cinfo.comps_in_scan; ci++)
|
|
750 entropy.saved.last_dc_val[ci] = 0;
|
|
751
|
|
752 /* Reset restart counter */
|
|
753 entropy.restarts_to_go = cinfo.restart_interval;
|
|
754
|
|
755 /* Reset out-of-data flag, unless read_restart_marker left us smack up
|
|
756 * against a marker. In that case we will end up treating the next data
|
|
757 * segment as empty, and we can avoid producing bogus output pixels by
|
|
758 * leaving the flag set.
|
|
759 */
|
|
760 if (cinfo.unread_marker is 0)
|
|
761 entropy.insufficient_data = false;
|
|
762
|
|
763 return true;
|
|
764 }
|
|
765 }
|
|
766
|
|
767 static final class phuff_entropy_decoder : jpeg_entropy_decoder {
|
|
768
|
|
769 /* These fields are loaded into local variables at start of each MCU.
|
|
770 * In case of suspension, we exit WITHOUT updating them.
|
|
771 */
|
|
772 bitread_perm_state bitstate;// = new bitread_perm_state(); /* Bit buffer at start of MCU */
|
|
773 savable_state saved;// = new savable_state(); /* Other state at start of MCU */
|
|
774
|
|
775 /* These fields are NOT loaded into local working state. */
|
|
776 int restarts_to_go; /* MCUs left in this restart interval */
|
|
777
|
|
778 /* Pointers to derived tables (these workspaces have image lifespan) */
|
|
779 d_derived_tbl[NUM_HUFF_TBLS] derived_tbls;// = new d_derived_tbl[NUM_HUFF_TBLS];
|
|
780
|
|
781 d_derived_tbl ac_derived_tbl; /* active table during an AC scan */
|
|
782
|
|
783 int[DCTSIZE2] newnz_pos;// = new int[DCTSIZE2];
|
|
784
|
|
785 public this(){
|
|
786 bitstate = new bitread_perm_state(); /* Bit buffer at start of MCU */
|
|
787 saved = new savable_state(); /* Other state at start of MCU */
|
|
788 }
|
|
789
|
|
790 override void start_pass (jpeg_decompress_struct cinfo) {
|
|
791 start_pass_phuff_decoder(cinfo);
|
|
792 }
|
|
793
|
|
794 override bool decode_mcu (jpeg_decompress_struct cinfo, short[][] MCU_data) {
|
|
795 bool is_DC_band = (cinfo.Ss is 0);
|
|
796 if (cinfo.Ah is 0) {
|
|
797 if (is_DC_band)
|
|
798 return decode_mcu_DC_first(cinfo, MCU_data);
|
|
799 else
|
|
800 return decode_mcu_AC_first(cinfo, MCU_data);
|
|
801 } else {
|
|
802 if (is_DC_band)
|
|
803 return decode_mcu_DC_refine(cinfo, MCU_data);
|
|
804 else
|
|
805 return decode_mcu_AC_refine(cinfo, MCU_data);
|
|
806 }
|
|
807 }
|
|
808
|
|
809 bool decode_mcu_DC_refine (jpeg_decompress_struct cinfo, short[][] MCU_data) {
|
|
810 phuff_entropy_decoder entropy = this;
|
|
811 int p1 = 1 << cinfo.Al; /* 1 in the bit position being coded */
|
|
812 int blkn;
|
|
813 short[] block;
|
|
814 // BITREAD_STATE_VARS;
|
|
815 int get_buffer;
|
|
816 int bits_left;
|
|
817 // bitread_working_state br_state = new bitread_working_state();
|
|
818 bitread_working_state br_state = br_state_local;
|
|
819
|
|
820 /* Process restart marker if needed; may have to suspend */
|
|
821 if (cinfo.restart_interval !is 0) {
|
|
822 if (entropy.restarts_to_go is 0)
|
|
823 if (! process_restart(cinfo))
|
|
824 return false;
|
|
825 }
|
|
826
|
|
827 /* Not worth the cycles to check insufficient_data here,
|
|
828 * since we will not change the data anyway if we read zeroes.
|
|
829 */
|
|
830
|
|
831 /* Load up working state */
|
|
832 // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
|
|
833 br_state.cinfo = cinfo;
|
|
834 br_state.buffer = cinfo.buffer;
|
|
835 br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
836 br_state.bytes_offset = cinfo.bytes_offset;
|
|
837 get_buffer = entropy.bitstate.get_buffer;
|
|
838 bits_left = entropy.bitstate.bits_left;
|
|
839
|
|
840 /* Outer loop handles each block in the MCU */
|
|
841
|
|
842 for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
|
|
843 block = MCU_data[blkn];
|
|
844
|
|
845 /* Encoded data is simply the next bit of the two's-complement DC value */
|
|
846 // CHECK_BIT_BUFFER(br_state, 1, return FALSE);
|
|
847 {
|
|
848 if (bits_left < (1)) {
|
|
849 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) {
|
|
850 return false;
|
|
851 }
|
|
852 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
853 }
|
|
854 }
|
|
855 // if (GET_BITS(1))
|
|
856 if ((( (get_buffer >> (bits_left -= (1)))) & ((1<<(1))-1)) !is 0)
|
|
857 block[0] |= p1;
|
|
858 /* Note: since we use |=, repeating the assignment later is safe */
|
|
859 }
|
|
860
|
|
861 /* Completed MCU, so update state */
|
|
862 // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
|
|
863 cinfo.buffer = br_state.buffer;
|
|
864 cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
|
|
865 cinfo.bytes_offset = br_state.bytes_offset;
|
|
866 entropy.bitstate.get_buffer = get_buffer;
|
|
867 entropy.bitstate.bits_left = bits_left;
|
|
868
|
|
869 /* Account for restart interval (no-op if not using restarts) */
|
|
870 entropy.restarts_to_go--;
|
|
871
|
|
872 return true;
|
|
873
|
|
874 }
|
|
875
|
|
876 bool decode_mcu_AC_refine (jpeg_decompress_struct cinfo, short[][] MCU_data) {
|
|
877 phuff_entropy_decoder entropy = this;
|
|
878 int Se = cinfo.Se;
|
|
879 int p1 = 1 << cinfo.Al; /* 1 in the bit position being coded */
|
|
880 int m1 = (-1) << cinfo.Al; /* -1 in the bit position being coded */
|
|
881 int s = 0, k, r;
|
|
882 int EOBRUN;
|
|
883 short[] block;
|
|
884 short[] thiscoef;
|
|
885 // BITREAD_STATE_VARS;
|
|
886 int get_buffer;
|
|
887 int bits_left;
|
|
888 // bitread_working_state br_state = new bitread_working_state();
|
|
889 bitread_working_state br_state = br_state_local;
|
|
890
|
|
891 d_derived_tbl tbl;
|
|
892 int num_newnz;
|
|
893 int[] newnz_pos = entropy.newnz_pos;
|
|
894
|
|
895 /* Process restart marker if needed; may have to suspend */
|
|
896 if (cinfo.restart_interval !is 0) {
|
|
897 if (entropy.restarts_to_go is 0)
|
|
898 if (! process_restart(cinfo))
|
|
899 return false;
|
|
900 }
|
|
901
|
|
902 /* If we've run out of data, don't modify the MCU.
|
|
903 */
|
|
904 if (! entropy.insufficient_data) {
|
|
905
|
|
906 /* Load up working state */
|
|
907 // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
|
|
908 br_state.cinfo = cinfo;
|
|
909 br_state.buffer = cinfo.buffer;
|
|
910 br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
911 br_state.bytes_offset = cinfo.bytes_offset;
|
|
912 get_buffer = entropy.bitstate.get_buffer;
|
|
913 bits_left = entropy.bitstate.bits_left;
|
|
914
|
|
915 EOBRUN = entropy.saved.EOBRUN; /* only part of saved state we need */
|
|
916
|
|
917 /* There is always only one block per MCU */
|
|
918 block = MCU_data[0];
|
|
919 tbl = entropy.ac_derived_tbl;
|
|
920
|
|
921 /* If we are forced to suspend, we must undo the assignments to any newly
|
|
922 * nonzero coefficients in the block, because otherwise we'd get confused
|
|
923 * next time about which coefficients were already nonzero.
|
|
924 * But we need not undo addition of bits to already-nonzero coefficients;
|
|
925 * instead, we can test the current bit to see if we already did it.
|
|
926 */
|
|
927 num_newnz = 0;
|
|
928
|
|
929 /* initialize coefficient loop counter to start of band */
|
|
930 k = cinfo.Ss;
|
|
931
|
|
932 if (EOBRUN is 0) {
|
|
933 for (; k <= Se; k++) {
|
|
934 // HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
|
|
935 {
|
|
936 int nb = 0, look;
|
|
937 if (bits_left < HUFF_LOOKAHEAD) {
|
|
938 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) {
|
|
939 // failaction;
|
|
940 while (num_newnz > 0)
|
|
941 block[newnz_pos[--num_newnz]] = 0;
|
|
942
|
|
943 return false;
|
|
944 }
|
|
945 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
946 if (bits_left < HUFF_LOOKAHEAD) {
|
|
947 nb = 1;
|
|
948 // goto slowlabel;
|
|
949 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb)) < 0) {
|
|
950 // failaction;
|
|
951 while (num_newnz > 0)
|
|
952 block[newnz_pos[--num_newnz]] = 0;
|
|
953
|
|
954 return false;
|
|
955 }
|
|
956 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
957 }
|
|
958 }
|
|
959 if (nb !is 1) {
|
|
960 // look = PEEK_BITS(HUFF_LOOKAHEAD);
|
|
961 look = (( (get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1<<(HUFF_LOOKAHEAD))-1));
|
|
962 if ((nb = tbl.look_nbits[look]) !is 0) {
|
|
963 // DROP_BITS(nb);
|
|
964 bits_left -= nb;
|
|
965 s = tbl.look_sym[look] & 0xFF;
|
|
966 } else {
|
|
967 nb = HUFF_LOOKAHEAD+1;
|
|
968 // slowlabel:
|
|
969 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb)) < 0) {
|
|
970 // failaction;
|
|
971 while (num_newnz > 0)
|
|
972 block[newnz_pos[--num_newnz]] = 0;
|
|
973
|
|
974 return false;
|
|
975 }
|
|
976 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
977 }
|
|
978 }
|
|
979 }
|
|
980 r = s >> 4;
|
|
981 s &= 15;
|
|
982 if (s !is 0) {
|
|
983 if (s !is 1) { /* size of new coef should always be 1 */
|
|
984 // WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
|
|
985 }
|
|
986 // CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
|
987 {
|
|
988 if (bits_left < (1)) {
|
|
989 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) {
|
|
990 // failaction;
|
|
991 while (num_newnz > 0)
|
|
992 block[newnz_pos[--num_newnz]] = 0;
|
|
993
|
|
994 return false;
|
|
995 }
|
|
996 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
997 }
|
|
998 }
|
|
999 // if (GET_BITS(1))
|
|
1000 if ((( (get_buffer >> (bits_left -= (1)))) & ((1<<(1))-1)) !is 0)
|
|
1001 s = p1; /* newly nonzero coef is positive */
|
|
1002 else
|
|
1003 s = m1; /* newly nonzero coef is negative */
|
|
1004 } else {
|
|
1005 if (r !is 15) {
|
|
1006 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
|
|
1007 if (r !is 0) {
|
|
1008 // CHECK_BIT_BUFFER(br_state, r, goto undoit);
|
|
1009 {
|
|
1010 if (bits_left < (r)) {
|
|
1011 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) {
|
|
1012 // failaction;
|
|
1013 while (num_newnz > 0)
|
|
1014 block[newnz_pos[--num_newnz]] = 0;
|
|
1015
|
|
1016 return false;
|
|
1017 }
|
|
1018 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1019 }
|
|
1020 }
|
|
1021 // r = GET_BITS(r);
|
|
1022 r = (( (get_buffer >> (bits_left -= (r)))) & ((1<<(r))-1));
|
|
1023 EOBRUN += r;
|
|
1024 }
|
|
1025 break; /* rest of block is handled by EOB logic */
|
|
1026 }
|
|
1027 /* note s = 0 for processing ZRL */
|
|
1028 }
|
|
1029 /* Advance over already-nonzero coefs and r still-zero coefs,
|
|
1030 * appending correction bits to the nonzeroes. A correction bit is 1
|
|
1031 * if the absolute value of the coefficient must be increased.
|
|
1032 */
|
|
1033 do {
|
|
1034 thiscoef = block;
|
|
1035 int thiscoef_offset = jpeg_natural_order[k];
|
|
1036 if (thiscoef[thiscoef_offset] !is 0) {
|
|
1037 // CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
|
1038 {
|
|
1039 if (bits_left < (1)) {
|
|
1040 if (!jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) {
|
|
1041 // failaction;
|
|
1042 while (num_newnz > 0)
|
|
1043 block[newnz_pos[--num_newnz]] = 0;
|
|
1044
|
|
1045 return false;
|
|
1046 }
|
|
1047 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1048 }
|
|
1049 }
|
|
1050 // if (GET_BITS(1)) {
|
|
1051 if ((( (get_buffer >> (bits_left -= (1)))) & ((1<<(1))-1)) !is 0) {
|
|
1052 if ((thiscoef[thiscoef_offset] & p1) is 0) { /* do nothing if already set it */
|
|
1053 if (thiscoef[thiscoef_offset] >= 0)
|
|
1054 thiscoef[thiscoef_offset] += p1;
|
|
1055 else
|
|
1056 thiscoef[thiscoef_offset] += m1;
|
|
1057 }
|
|
1058 }
|
|
1059 } else {
|
|
1060 if (--r < 0)
|
|
1061 break; /* reached target zero coefficient */
|
|
1062 }
|
|
1063 k++;
|
|
1064 } while (k <= Se);
|
|
1065 if (s !is 0) {
|
|
1066 int pos = jpeg_natural_order[k];
|
|
1067 /* Output newly nonzero coefficient */
|
|
1068 block[pos] = cast(short) s;
|
|
1069 /* Remember its position in case we have to suspend */
|
|
1070 newnz_pos[num_newnz++] = pos;
|
|
1071 }
|
|
1072 }
|
|
1073 }
|
|
1074
|
|
1075 if (EOBRUN > 0) {
|
|
1076 /* Scan any remaining coefficient positions after the end-of-band
|
|
1077 * (the last newly nonzero coefficient, if any). Append a correction
|
|
1078 * bit to each already-nonzero coefficient. A correction bit is 1
|
|
1079 * if the absolute value of the coefficient must be increased.
|
|
1080 */
|
|
1081 for (; k <= Se; k++) {
|
|
1082 thiscoef = block;
|
|
1083 int thiscoef_offset = jpeg_natural_order[k];
|
|
1084 if (thiscoef[thiscoef_offset] !is 0) {
|
|
1085 // CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
|
1086 {
|
|
1087 if (bits_left < (1)) {
|
|
1088 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) {
|
|
1089 // failaction;
|
|
1090 while (num_newnz > 0)
|
|
1091 block[newnz_pos[--num_newnz]] = 0;
|
|
1092
|
|
1093 return false;
|
|
1094 }
|
|
1095 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1096 }
|
|
1097 }
|
|
1098 // if (GET_BITS(1)) {
|
|
1099 if ((( (get_buffer >> (bits_left -= (1)))) & ((1<<(1))-1)) !is 0) {
|
|
1100 if ((thiscoef[thiscoef_offset] & p1) is 0) { /* do nothing if already changed it */
|
|
1101 if (thiscoef[thiscoef_offset] >= 0)
|
|
1102 thiscoef[thiscoef_offset] += p1;
|
|
1103 else
|
|
1104 thiscoef[thiscoef_offset] += m1;
|
|
1105 }
|
|
1106 }
|
|
1107 }
|
|
1108 }
|
|
1109 /* Count one block completed in EOB run */
|
|
1110 EOBRUN--;
|
|
1111 }
|
|
1112
|
|
1113 /* Completed MCU, so update state */
|
|
1114 // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
|
|
1115 cinfo.buffer = br_state.buffer;
|
|
1116 cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
|
|
1117 cinfo.bytes_offset = br_state.bytes_offset;
|
|
1118 entropy.bitstate.get_buffer = get_buffer;
|
|
1119 entropy.bitstate.bits_left = bits_left;
|
|
1120
|
|
1121 entropy.saved.EOBRUN = EOBRUN; /* only part of saved state we need */
|
|
1122 }
|
|
1123
|
|
1124 /* Account for restart interval (no-op if not using restarts) */
|
|
1125 entropy.restarts_to_go--;
|
|
1126
|
|
1127 return true;
|
|
1128
|
|
1129 // undoit:
|
|
1130 // /* Re-zero any output coefficients that we made newly nonzero */
|
|
1131 // while (num_newnz > 0)
|
|
1132 // (*block)[newnz_pos[--num_newnz]] = 0;
|
|
1133 //
|
|
1134 // return false;
|
|
1135
|
|
1136 }
|
|
1137
|
|
1138 bool decode_mcu_AC_first (jpeg_decompress_struct cinfo, short[][] MCU_data) {
|
|
1139 phuff_entropy_decoder entropy = this;
|
|
1140 int Se = cinfo.Se;
|
|
1141 int Al = cinfo.Al;
|
|
1142 int s = 0, k, r;
|
|
1143 int EOBRUN;
|
|
1144 short[] block;
|
|
1145 // BITREAD_STATE_VARS;
|
|
1146 int get_buffer;
|
|
1147 int bits_left;
|
|
1148 // bitread_working_state br_state = new bitread_working_state();
|
|
1149 bitread_working_state br_state = br_state_local;
|
|
1150
|
|
1151 d_derived_tbl tbl;
|
|
1152
|
|
1153 /* Process restart marker if needed; may have to suspend */
|
|
1154 if (cinfo.restart_interval !is 0) {
|
|
1155 if (entropy.restarts_to_go is 0)
|
|
1156 if (! process_restart(cinfo))
|
|
1157 return false;
|
|
1158 }
|
|
1159
|
|
1160 /* If we've run out of data, just leave the MCU set to zeroes.
|
|
1161 * This way, we return uniform gray for the remainder of the segment.
|
|
1162 */
|
|
1163 if (! entropy.insufficient_data) {
|
|
1164
|
|
1165 /* Load up working state.
|
|
1166 * We can avoid loading/saving bitread state if in an EOB run.
|
|
1167 */
|
|
1168 EOBRUN = entropy.saved.EOBRUN; /* only part of saved state we need */
|
|
1169
|
|
1170 /* There is always only one block per MCU */
|
|
1171
|
|
1172 if (EOBRUN > 0) /* if it's a band of zeroes... */
|
|
1173 EOBRUN--; /* ...process it now (we do nothing) */
|
|
1174 else {
|
|
1175 // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
|
|
1176 br_state.cinfo = cinfo;
|
|
1177 br_state.buffer = cinfo.buffer;
|
|
1178 br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
1179 br_state.bytes_offset = cinfo.bytes_offset;
|
|
1180 get_buffer = entropy.bitstate.get_buffer;
|
|
1181 bits_left = entropy.bitstate.bits_left;
|
|
1182
|
|
1183 block = MCU_data[0];
|
|
1184 tbl = entropy.ac_derived_tbl;
|
|
1185
|
|
1186 for (k = cinfo.Ss; k <= Se; k++) {
|
|
1187 // HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
|
|
1188 {
|
|
1189 int nb = 0, look;
|
|
1190 if (bits_left < HUFF_LOOKAHEAD) {
|
|
1191 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) {
|
|
1192 return false;
|
|
1193 }
|
|
1194 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1195 if (bits_left < HUFF_LOOKAHEAD) {
|
|
1196 nb = 1;
|
|
1197 // goto slowlabel;
|
|
1198 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb)) < 0) {
|
|
1199 return false;
|
|
1200 }
|
|
1201 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1202 }
|
|
1203 }
|
|
1204 if (nb !is 1) {
|
|
1205 // look = PEEK_BITS(HUFF_LOOKAHEAD);
|
|
1206 look = (( (get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1<<(HUFF_LOOKAHEAD))-1));
|
|
1207
|
|
1208 if ((nb = tbl.look_nbits[look]) !is 0) {
|
|
1209 // DROP_BITS(nb);
|
|
1210 bits_left -= nb;
|
|
1211 s = tbl.look_sym[look] & 0xFF;
|
|
1212 } else {
|
|
1213 nb = HUFF_LOOKAHEAD+1;
|
|
1214 // slowlabel:
|
|
1215 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb)) < 0) {
|
|
1216 return false;
|
|
1217 }
|
|
1218 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1219 }
|
|
1220 }
|
|
1221 }
|
|
1222 r = s >> 4;
|
|
1223 s &= 15;
|
|
1224 if (s !is 0) {
|
|
1225 k += r;
|
|
1226 // CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
1227 {
|
|
1228 if (bits_left < (s)) {
|
|
1229 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) {
|
|
1230 return false;
|
|
1231 }
|
|
1232 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1233 }
|
|
1234 }
|
|
1235 // r = GET_BITS(s);
|
|
1236 r = (( (get_buffer >> (bits_left -= (s)))) & ((1<<(s))-1));
|
|
1237 // s = HUFF_EXTEND(r, s);
|
|
1238 s = ((r) < extend_test[s] ? (r) + extend_offset[s] : (r));
|
|
1239 /* Scale and output coefficient in natural (dezigzagged) order */
|
|
1240 block[jpeg_natural_order[k]] = cast(short) (s << Al);
|
|
1241 } else {
|
|
1242 if (r is 15) { /* ZRL */
|
|
1243 k += 15; /* skip 15 zeroes in band */
|
|
1244 } else { /* EOBr, run length is 2^r + appended bits */
|
|
1245 EOBRUN = 1 << r;
|
|
1246 if (r !is 0) { /* EOBr, r > 0 */
|
|
1247 // CHECK_BIT_BUFFER(br_state, r, return FALSE);
|
|
1248 {
|
|
1249 if (bits_left < (r)) {
|
|
1250 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) {
|
|
1251 return false;
|
|
1252 }
|
|
1253 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1254 }
|
|
1255 }
|
|
1256 // r = GET_BITS(r);
|
|
1257 r = (( (get_buffer >> (bits_left -= (r)))) & ((1<<(r))-1));
|
|
1258 EOBRUN += r;
|
|
1259 }
|
|
1260 EOBRUN--; /* this band is processed at this moment */
|
|
1261 break; /* force end-of-band */
|
|
1262 }
|
|
1263 }
|
|
1264 }
|
|
1265
|
|
1266 // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
|
|
1267 cinfo.buffer = br_state.buffer;
|
|
1268 cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
|
|
1269 cinfo.bytes_offset = br_state.bytes_offset;
|
|
1270 entropy.bitstate.get_buffer = get_buffer;
|
|
1271 entropy.bitstate.bits_left = bits_left;
|
|
1272 }
|
|
1273
|
|
1274 /* Completed MCU, so update state */
|
|
1275 entropy.saved.EOBRUN = EOBRUN; /* only part of saved state we need */
|
|
1276 }
|
|
1277
|
|
1278 /* Account for restart interval (no-op if not using restarts) */
|
|
1279 entropy.restarts_to_go--;
|
|
1280
|
|
1281 return true;
|
|
1282 }
|
|
1283
|
|
1284 bool decode_mcu_DC_first (jpeg_decompress_struct cinfo, short[][] MCU_data) {
|
|
1285 phuff_entropy_decoder entropy = this;
|
|
1286 int Al = cinfo.Al;
|
|
1287 int s = 0, r;
|
|
1288 int blkn, ci;
|
|
1289 short[] block;
|
|
1290 // BITREAD_STATE_VARS;
|
|
1291 int get_buffer;
|
|
1292 int bits_left;
|
|
1293 // bitread_working_state br_state = new bitread_working_state();
|
|
1294 bitread_working_state br_state = br_state_local;
|
|
1295
|
|
1296 // savable_state state = new savable_state();
|
|
1297 savable_state state = state_local;
|
|
1298 d_derived_tbl tbl;
|
|
1299 jpeg_component_info compptr;
|
|
1300
|
|
1301 /* Process restart marker if needed; may have to suspend */
|
|
1302 if (cinfo.restart_interval !is 0) {
|
|
1303 if (entropy.restarts_to_go is 0)
|
|
1304 if (! process_restart(cinfo))
|
|
1305 return false;
|
|
1306 }
|
|
1307
|
|
1308 /* If we've run out of data, just leave the MCU set to zeroes.
|
|
1309 * This way, we return uniform gray for the remainder of the segment.
|
|
1310 */
|
|
1311 if (! entropy.insufficient_data) {
|
|
1312
|
|
1313 /* Load up working state */
|
|
1314 // BITREAD_LOAD_STATE(cinfo,entropy.bitstate);
|
|
1315 br_state.cinfo = cinfo;
|
|
1316 br_state.buffer = cinfo.buffer;
|
|
1317 br_state.bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
1318 br_state.bytes_offset = cinfo.bytes_offset;
|
|
1319 get_buffer = entropy.bitstate.get_buffer;
|
|
1320 bits_left = entropy.bitstate.bits_left;
|
|
1321
|
|
1322 // ASSIGN_STATE(state, entropy.saved);
|
|
1323 state.EOBRUN = entropy.saved.EOBRUN;
|
|
1324 state.last_dc_val[0] = entropy.saved.last_dc_val[0];
|
|
1325 state.last_dc_val[1] = entropy.saved.last_dc_val[1];
|
|
1326 state.last_dc_val[2] = entropy.saved.last_dc_val[2];
|
|
1327 state.last_dc_val[3] = entropy.saved.last_dc_val[3];
|
|
1328
|
|
1329 /* Outer loop handles each block in the MCU */
|
|
1330
|
|
1331 for (blkn = 0; blkn < cinfo.blocks_in_MCU; blkn++) {
|
|
1332 block = MCU_data[blkn];
|
|
1333 ci = cinfo.MCU_membership[blkn];
|
|
1334 compptr = cinfo.cur_comp_info[ci];
|
|
1335 tbl = entropy.derived_tbls[compptr.dc_tbl_no];
|
|
1336
|
|
1337 /* Decode a single block's worth of coefficients */
|
|
1338
|
|
1339 /* Section F.2.2.1: decode the DC coefficient difference */
|
|
1340 // HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
|
|
1341 {
|
|
1342 int nb = 0, look;
|
|
1343 if (bits_left < HUFF_LOOKAHEAD) {
|
|
1344 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) {
|
|
1345 return false;
|
|
1346 }
|
|
1347 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1348 if (bits_left < HUFF_LOOKAHEAD) {
|
|
1349 nb = 1;
|
|
1350 // goto slowlabel;
|
|
1351 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb)) < 0) {
|
|
1352 return false;
|
|
1353 }
|
|
1354 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1355 }
|
|
1356 }
|
|
1357 if (nb !is 1) {
|
|
1358 // look = PEEK_BITS(HUFF_LOOKAHEAD);
|
|
1359 look = (( (get_buffer >> (bits_left - (HUFF_LOOKAHEAD)))) & ((1<<(HUFF_LOOKAHEAD))-1));
|
|
1360
|
|
1361 if ((nb = tbl.look_nbits[look]) !is 0) {
|
|
1362 // DROP_BITS(nb);
|
|
1363 bits_left -= nb;
|
|
1364 s = tbl.look_sym[look] & 0xFF;
|
|
1365 } else {
|
|
1366 nb = HUFF_LOOKAHEAD+1;
|
|
1367 // slowlabel:
|
|
1368 if ((s=jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb)) < 0) {
|
|
1369 return false;
|
|
1370 }
|
|
1371 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1372 }
|
|
1373 }
|
|
1374 }
|
|
1375 if (s !is 0) {
|
|
1376 // CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
|
1377 {
|
|
1378 if (bits_left < (s)) {
|
|
1379 if (! jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) {
|
|
1380 return false;
|
|
1381 }
|
|
1382 get_buffer = br_state.get_buffer; bits_left = br_state.bits_left;
|
|
1383 }
|
|
1384 }
|
|
1385 // r = GET_BITS(s);
|
|
1386 r = (( (get_buffer >> (bits_left -= (s)))) & ((1<<(s))-1));
|
|
1387 // s = HUFF_EXTEND(r, s);
|
|
1388 s = ((r) < extend_test[s] ? (r) + extend_offset[s] : (r));
|
|
1389 }
|
|
1390
|
|
1391 /* Convert DC difference to actual value, update last_dc_val */
|
|
1392 s += state.last_dc_val[ci];
|
|
1393 state.last_dc_val[ci] = s;
|
|
1394 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
|
|
1395 block[0] = cast(short) (s << Al);
|
|
1396 }
|
|
1397
|
|
1398 /* Completed MCU, so update state */
|
|
1399 // BITREAD_SAVE_STATE(cinfo,entropy.bitstate);
|
|
1400 cinfo.buffer = br_state.buffer;
|
|
1401 cinfo.bytes_in_buffer = br_state.bytes_in_buffer;
|
|
1402 cinfo.bytes_offset = br_state.bytes_offset;
|
|
1403 entropy.bitstate.get_buffer = get_buffer;
|
|
1404 entropy.bitstate.bits_left = bits_left;
|
|
1405 // ASSIGN_STATE(entropy.saved, state);
|
|
1406 entropy.saved.EOBRUN = state.EOBRUN;
|
|
1407 entropy.saved.last_dc_val[0] = state.last_dc_val[0];
|
|
1408 entropy.saved.last_dc_val[1] = state.last_dc_val[1];
|
|
1409 entropy.saved.last_dc_val[2] = state.last_dc_val[2];
|
|
1410 entropy.saved.last_dc_val[3] = state.last_dc_val[3];
|
|
1411 }
|
|
1412
|
|
1413 /* Account for restart interval (no-op if not using restarts) */
|
|
1414 entropy.restarts_to_go--;
|
|
1415
|
|
1416 return true;
|
|
1417 }
|
|
1418
|
|
1419 bool process_restart (jpeg_decompress_struct cinfo) {
|
|
1420 phuff_entropy_decoder entropy = this;
|
|
1421 int ci;
|
|
1422
|
|
1423 /* Throw away any unused bits remaining in bit buffer; */
|
|
1424 /* include any full bytes in next_marker's count of discarded bytes */
|
|
1425 cinfo.marker.discarded_bytes += entropy.bitstate.bits_left / 8;
|
|
1426 entropy.bitstate.bits_left = 0;
|
|
1427
|
|
1428 /* Advance past the RSTn marker */
|
|
1429 if (! read_restart_marker (cinfo))
|
|
1430 return false;
|
|
1431
|
|
1432 /* Re-initialize DC predictions to 0 */
|
|
1433 for (ci = 0; ci < cinfo.comps_in_scan; ci++)
|
|
1434 entropy.saved.last_dc_val[ci] = 0;
|
|
1435 /* Re-init EOB run count, too */
|
|
1436 entropy.saved.EOBRUN = 0;
|
|
1437
|
|
1438 /* Reset restart counter */
|
|
1439 entropy.restarts_to_go = cinfo.restart_interval;
|
|
1440
|
|
1441 /* Reset out-of-data flag, unless read_restart_marker left us smack up
|
|
1442 * against a marker. In that case we will end up treating the next data
|
|
1443 * segment as empty, and we can avoid producing bogus output pixels by
|
|
1444 * leaving the flag set.
|
|
1445 */
|
|
1446 if (cinfo.unread_marker is 0)
|
|
1447 entropy.insufficient_data = false;
|
|
1448
|
|
1449 return true;
|
|
1450 }
|
|
1451
|
|
1452 void start_pass_phuff_decoder (jpeg_decompress_struct cinfo) {
|
|
1453 phuff_entropy_decoder entropy = this;
|
|
1454 bool is_DC_band, bad;
|
|
1455 int ci, coefi, tbl;
|
|
1456 int[] coef_bit_ptr;
|
|
1457 jpeg_component_info compptr;
|
|
1458
|
|
1459 is_DC_band = (cinfo.Ss is 0);
|
|
1460
|
|
1461 /* Validate scan parameters */
|
|
1462 bad = false;
|
|
1463 if (is_DC_band) {
|
|
1464 if (cinfo.Se !is 0)
|
|
1465 bad = true;
|
|
1466 } else {
|
|
1467 /* need not check Ss/Se < 0 since they came from unsigned bytes */
|
|
1468 if (cinfo.Ss > cinfo.Se || cinfo.Se >= DCTSIZE2)
|
|
1469 bad = true;
|
|
1470 /* AC scans may have only one component */
|
|
1471 if (cinfo.comps_in_scan !is 1)
|
|
1472 bad = true;
|
|
1473 }
|
|
1474 if (cinfo.Ah !is 0) {
|
|
1475 /* Successive approximation refinement scan: must have Al = Ah-1. */
|
|
1476 if (cinfo.Al !is cinfo.Ah-1)
|
|
1477 bad = true;
|
|
1478 }
|
|
1479 if (cinfo.Al > 13) /* need not check for < 0 */
|
|
1480 bad = true;
|
|
1481 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
|
|
1482 * but the spec doesn't say so, and we try to be liberal about what we
|
|
1483 * accept. Note: large Al values could result in out-of-range DC
|
|
1484 * coefficients during early scans, leading to bizarre displays due to
|
|
1485 * overflows in the IDCT math. But we won't crash.
|
|
1486 */
|
|
1487 if (bad)
|
|
1488 error();
|
|
1489 // ERREXIT4(cinfo, JERR_BAD_PROGRESSION, cinfo.Ss, cinfo.Se, cinfo.Ah, cinfo.Al);
|
|
1490 /* Update progression status, and verify that scan order is legal.
|
|
1491 * Note that inter-scan inconsistencies are treated as warnings
|
|
1492 * not fatal errors ... not clear if this is right way to behave.
|
|
1493 */
|
|
1494 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
1495 int cindex = cinfo.cur_comp_info[ci].component_index;
|
|
1496 coef_bit_ptr = cinfo.coef_bits[cindex];
|
|
1497 if (!is_DC_band && coef_bit_ptr[0] < 0) {/* AC without prior DC scan */
|
|
1498 // WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
|
|
1499 }
|
|
1500 for (coefi = cinfo.Ss; coefi <= cinfo.Se; coefi++) {
|
|
1501 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
|
|
1502 if (cinfo.Ah !is expected) {
|
|
1503 // WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
|
|
1504 }
|
|
1505 coef_bit_ptr[coefi] = cinfo.Al;
|
|
1506 }
|
|
1507 }
|
|
1508
|
|
1509 /* Select MCU decoding routine */
|
|
1510 // if (cinfo.Ah is 0) {
|
|
1511 // if (is_DC_band)
|
|
1512 // entropy.pub.decode_mcu = decode_mcu_DC_first;
|
|
1513 // else
|
|
1514 // entropy.pub.decode_mcu = decode_mcu_AC_first;
|
|
1515 // } else {
|
|
1516 // if (is_DC_band)
|
|
1517 // entropy.pub.decode_mcu = decode_mcu_DC_refine;
|
|
1518 // else
|
|
1519 // entropy.pub.decode_mcu = decode_mcu_AC_refine;
|
|
1520 // }
|
|
1521
|
|
1522 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
1523 compptr = cinfo.cur_comp_info[ci];
|
|
1524 /* Make sure requested tables are present, and compute derived tables.
|
|
1525 * We may build same derived table more than once, but it's not expensive.
|
|
1526 */
|
|
1527 if (is_DC_band) {
|
|
1528 if (cinfo.Ah is 0) { /* DC refinement needs no table */
|
|
1529 tbl = compptr.dc_tbl_no;
|
|
1530 jpeg_make_d_derived_tbl(cinfo, true, tbl, entropy.derived_tbls[tbl] = new d_derived_tbl());
|
|
1531 }
|
|
1532 } else {
|
|
1533 tbl = compptr.ac_tbl_no;
|
|
1534 jpeg_make_d_derived_tbl(cinfo, false, tbl, entropy.derived_tbls[tbl] = new d_derived_tbl());
|
|
1535 /* remember the single active table */
|
|
1536 entropy.ac_derived_tbl = entropy.derived_tbls[tbl];
|
|
1537 }
|
|
1538 /* Initialize DC predictions to 0 */
|
|
1539 entropy.saved.last_dc_val[ci] = 0;
|
|
1540 }
|
|
1541
|
|
1542 /* Initialize bitread state variables */
|
|
1543 entropy.bitstate.bits_left = 0;
|
|
1544 entropy.bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
|
|
1545 entropy.insufficient_data = false;
|
|
1546
|
|
1547 /* Initialize private state variables */
|
|
1548 entropy.saved.EOBRUN = 0;
|
|
1549
|
|
1550 /* Initialize restart counter */
|
|
1551 entropy.restarts_to_go = cinfo.restart_interval;
|
|
1552 }
|
|
1553
|
|
1554 }
|
|
1555
|
|
1556 static final class jpeg_component_info {
|
|
1557 /* These values are fixed over the whole image. */
|
|
1558 /* For compression, they must be supplied by parameter setup; */
|
|
1559 /* for decompression, they are read from the SOF marker. */
|
|
1560 int component_id; /* identifier for this component (0..255) */
|
|
1561 int component_index; /* its index in SOF or cinfo.comp_info[] */
|
|
1562 int h_samp_factor; /* horizontal sampling factor (1..4) */
|
|
1563 int v_samp_factor; /* vertical sampling factor (1..4) */
|
|
1564 int quant_tbl_no; /* quantization table selector (0..3) */
|
|
1565 /* These values may vary between scans. */
|
|
1566 /* For compression, they must be supplied by parameter setup; */
|
|
1567 /* for decompression, they are read from the SOS marker. */
|
|
1568 /* The decompressor output side may not use these variables. */
|
|
1569 int dc_tbl_no; /* DC entropy table selector (0..3) */
|
|
1570 int ac_tbl_no; /* AC entropy table selector (0..3) */
|
|
1571
|
|
1572 /* Remaining fields should be treated as private by applications. */
|
|
1573
|
|
1574 /* These values are computed during compression or decompression startup: */
|
|
1575 /* Component's size in DCT blocks.
|
|
1576 * Any dummy blocks added to complete an MCU are not counted; therefore
|
|
1577 * these values do not depend on whether a scan is interleaved or not.
|
|
1578 */
|
|
1579 int width_in_blocks;
|
|
1580 int height_in_blocks;
|
|
1581 /* Size of a DCT block in samples. Always DCTSIZE for compression.
|
|
1582 * For decompression this is the size of the output from one DCT block,
|
|
1583 * reflecting any scaling we choose to apply during the IDCT step.
|
|
1584 * Values of 1,2,4,8 are likely to be supported. Note that different
|
|
1585 * components may receive different IDCT scalings.
|
|
1586 */
|
|
1587 int DCT_scaled_size;
|
|
1588 /* The downsampled dimensions are the component's actual, unpadded number
|
|
1589 * of samples at the main buffer (preprocessing/compression interface), thus
|
|
1590 * downsampled_width = ceil(image_width * Hi/Hmax)
|
|
1591 * and similarly for height. For decompression, IDCT scaling is included, so
|
|
1592 * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
|
|
1593 */
|
|
1594 int downsampled_width; /* actual width in samples */
|
|
1595 int downsampled_height; /* actual height in samples */
|
|
1596 /* This flag is used only for decompression. In cases where some of the
|
|
1597 * components will be ignored (eg grayscale output from YCbCr image),
|
|
1598 * we can skip most computations for the unused components.
|
|
1599 */
|
|
1600 bool component_needed; /* do we need the value of this component? */
|
|
1601
|
|
1602 /* These values are computed before starting a scan of the component. */
|
|
1603 /* The decompressor output side may not use these variables. */
|
|
1604 int MCU_width; /* number of blocks per MCU, horizontally */
|
|
1605 int MCU_height; /* number of blocks per MCU, vertically */
|
|
1606 int MCU_blocks; /* MCU_width * MCU_height */
|
|
1607 int MCU_sample_width; /* MCU width in samples, MCU_width*DCT_scaled_size */
|
|
1608 int last_col_width; /* # of non-dummy blocks across in last MCU */
|
|
1609 int last_row_height; /* # of non-dummy blocks down in last MCU */
|
|
1610
|
|
1611 /* Saved quantization table for component; null if none yet saved.
|
|
1612 * See jdinput.c comments about the need for this information.
|
|
1613 * This field is currently used only for decompression.
|
|
1614 */
|
|
1615 JQUANT_TBL quant_table;
|
|
1616
|
|
1617 /* Private per-component storage for DCT or IDCT subsystem. */
|
|
1618 int[] dct_table;
|
|
1619 }
|
|
1620
|
|
1621 static final class jpeg_color_quantizer {
|
|
1622 // JMETHOD(void, start_pass, (j_decompress_ptr cinfo, bool is_pre_scan));
|
|
1623 // JMETHOD(void, color_quantize, (j_decompress_ptr cinfo,
|
|
1624 // JSAMPARRAY input_buf, JSAMPARRAY output_buf,
|
|
1625 // int num_rows));
|
|
1626 // JMETHOD(void, finish_pass, (j_decompress_ptr cinfo));
|
|
1627 // JMETHOD(void, new_color_map, (j_decompress_ptr cinfo));
|
|
1628
|
|
1629 /* Initially allocated colormap is saved here */
|
|
1630 int[][] sv_colormap; /* The color map as a 2-D pixel array */
|
|
1631 int sv_actual; /* number of entries in use */
|
|
1632
|
|
1633 int[][] colorindex; /* Precomputed mapping for speed */
|
|
1634 /* colorindex[i][j] = index of color closest to pixel value j in component i,
|
|
1635 * premultiplied as described above. Since colormap indexes must fit into
|
|
1636 * JSAMPLEs, the entries of this array will too.
|
|
1637 */
|
|
1638 bool is_padded; /* is the colorindex padded for odither? */
|
|
1639
|
|
1640 int[MAX_Q_COMPS] Ncolors;// = new int [MAX_Q_COMPS]; /* # of values alloced to each component */
|
|
1641
|
|
1642 /* Variables for ordered dithering */
|
|
1643 int row_index; /* cur row's vertical index in dither matrix */
|
|
1644 // ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
|
|
1645
|
|
1646 /* Variables for Floyd-Steinberg dithering */
|
|
1647 // FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
|
|
1648 bool on_odd_row;
|
|
1649
|
|
1650 void start_pass (jpeg_decompress_struct cinfo, bool is_pre_scan) {
|
|
1651 error();
|
|
1652 }
|
|
1653 }
|
|
1654
|
|
1655 static final class jpeg_upsampler {
|
|
1656 // JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
|
|
1657 // JMETHOD(void, upsample, (j_decompress_ptr cinfo,
|
|
1658 // JSAMPIMAGE input_buf,
|
|
1659 // JDIMENSION *in_row_group_ctr,
|
|
1660 // JDIMENSION in_row_groups_avail,
|
|
1661 // JSAMPARRAY output_buf,
|
|
1662 // JDIMENSION *out_row_ctr,
|
|
1663 // JDIMENSION out_rows_avail));
|
|
1664
|
|
1665 bool need_context_rows; /* TRUE if need rows above & below */
|
|
1666
|
|
1667 /* Color conversion buffer. When using separate upsampling and color
|
|
1668 * conversion steps, this buffer holds one upsampled row group until it
|
|
1669 * has been color converted and output.
|
|
1670 * Note: we do not allocate any storage for component(s) which are full-size,
|
|
1671 * ie do not need rescaling. The corresponding entry of color_buf[] is
|
|
1672 * simply set to point to the input data array, thereby avoiding copying.
|
|
1673 */
|
|
1674 byte[][][MAX_COMPONENTS] color_buf;// = new byte[MAX_COMPONENTS][][];
|
|
1675 int[MAX_COMPONENTS] color_buf_offset;// = new int[MAX_COMPONENTS];
|
|
1676
|
|
1677 /* Per-component upsampling method pointers */
|
|
1678 int[MAX_COMPONENTS] methods;// = new int[MAX_COMPONENTS];
|
|
1679
|
|
1680 int next_row_out; /* counts rows emitted from color_buf */
|
|
1681 int rows_to_go; /* counts rows remaining in image */
|
|
1682
|
|
1683 /* Height of an input row group for each component. */
|
|
1684 int[MAX_COMPONENTS] rowgroup_height;// = new int[MAX_COMPONENTS];
|
|
1685
|
|
1686 /* These arrays save pixel expansion factors so that int_expand need not
|
|
1687 * recompute them each time. They are unused for other upsampling methods.
|
|
1688 */
|
|
1689 byte[MAX_COMPONENTS] h_expand;// = new byte[MAX_COMPONENTS];
|
|
1690 byte[MAX_COMPONENTS] v_expand;// = new byte[MAX_COMPONENTS];
|
|
1691
|
|
1692 void start_pass (jpeg_decompress_struct cinfo) {
|
|
1693 jpeg_upsampler upsample = cinfo.upsample;
|
|
1694
|
|
1695 /* Mark the conversion buffer empty */
|
|
1696 upsample.next_row_out = cinfo.max_v_samp_factor;
|
|
1697 /* Initialize total-height counter for detecting bottom of image */
|
|
1698 upsample.rows_to_go = cinfo.output_height;
|
|
1699 }
|
|
1700
|
|
1701 }
|
|
1702
|
|
1703 static final class jpeg_marker_reader {
|
|
1704 /* Read a restart marker --- exported for use by entropy decoder only */
|
|
1705 // jpeg_marker_parser_method read_restart_marker;
|
|
1706
|
|
1707 /* State of marker reader --- nominally internal, but applications
|
|
1708 * supplying COM or APPn handlers might like to know the state.
|
|
1709 */
|
|
1710 bool saw_SOI; /* found SOI? */
|
|
1711 bool saw_SOF; /* found SOF? */
|
|
1712 int next_restart_num; /* next restart number expected (0-7) */
|
|
1713 int discarded_bytes; /* # of bytes skipped looking for a marker */
|
|
1714
|
|
1715 /* Application-overridable marker processing methods */
|
|
1716 // jpeg_marker_parser_method process_COM;
|
|
1717 // jpeg_marker_parser_method process_APPn[16];
|
|
1718
|
|
1719 /* Limit on marker data length to save for each marker type */
|
|
1720 int length_limit_COM;
|
|
1721 int[16] length_limit_APPn;// = new int[16];
|
|
1722
|
|
1723 /* Status of COM/APPn marker saving */
|
|
1724 // jpeg_marker_reader cur_marker; /* null if not processing a marker */
|
|
1725 // int bytes_read; /* data bytes read so far in marker */
|
|
1726 /* Note: cur_marker is not linked into marker_list until it's all read. */
|
|
1727 }
|
|
1728
|
|
1729
|
|
1730 static final class jpeg_d_main_controller {
|
|
1731 // JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
|
|
1732 int process_data;
|
|
1733
|
|
1734 /* Pointer to allocated workspace (M or M+2 row groups). */
|
|
1735 byte[][][MAX_COMPONENTS] buffer;// = new byte[MAX_COMPONENTS][][];
|
|
1736 int[MAX_COMPONENTS] buffer_offset;// = new int[MAX_COMPONENTS];
|
|
1737
|
|
1738 bool buffer_full; /* Have we gotten an iMCU row from decoder? */
|
|
1739 int[1] rowgroup_ctr;// = new int[1]; /* counts row groups output to postprocessor */
|
|
1740
|
|
1741 /* Remaining fields are only used in the context case. */
|
|
1742
|
|
1743 /* These are the master pointers to the funny-order pointer lists. */
|
|
1744 byte[][][][2] xbuffer;// = new byte[2][][][]; /* pointers to weird pointer lists */
|
|
1745 int[][2] xbuffer_offset;// = new int[2][];
|
|
1746
|
|
1747 int whichptr; /* indicates which pointer set is now in use */
|
|
1748 int context_state; /* process_data state machine status */
|
|
1749 int rowgroups_avail; /* row groups available to postprocessor */
|
|
1750 int iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
|
|
1751
|
|
1752 void start_pass (jpeg_decompress_struct cinfo, int pass_mode) {
|
|
1753 jpeg_d_main_controller main = cinfo.main;
|
|
1754
|
|
1755 switch (pass_mode) {
|
|
1756 case JBUF_PASS_THRU:
|
|
1757 if (cinfo.upsample.need_context_rows) {
|
|
1758 main.process_data = PROCESS_DATA_CONTEXT_MAIN;
|
|
1759 make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
|
|
1760 main.whichptr = 0; /* Read first iMCU row into xbuffer[0] */
|
|
1761 main.context_state = CTX_PREPARE_FOR_IMCU;
|
|
1762 main.iMCU_row_ctr = 0;
|
|
1763 } else {
|
|
1764 /* Simple case with no context needed */
|
|
1765 main.process_data = PROCESS_DATA_SIMPLE_MAIN;
|
|
1766 }
|
|
1767 main.buffer_full = false; /* Mark buffer empty */
|
|
1768 main.rowgroup_ctr[0] = 0;
|
|
1769 break;
|
|
1770 // #ifdef QUANT_2PASS_SUPPORTED
|
|
1771 // case JBUF_CRANK_DEST:
|
|
1772 // /* For last pass of 2-pass quantization, just crank the postprocessor */
|
|
1773 // main.process_data = PROCESS_DATA_CRANK_POST;
|
|
1774 // break;
|
|
1775 // #endif
|
|
1776 default:
|
|
1777 error();
|
|
1778 // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
|
1779 break;
|
|
1780 }
|
|
1781 }
|
|
1782
|
|
1783 }
|
|
1784
|
|
1785 static final class jpeg_decomp_master {
|
|
1786 // JMETHOD(void, prepare_for_output_pass, (j_decompress_ptr cinfo));
|
|
1787 // JMETHOD(void, finish_output_pass, (j_decompress_ptr cinfo));
|
|
1788
|
|
1789 /* State variables made visible to other modules */
|
|
1790 bool is_dummy_pass;
|
|
1791
|
|
1792 int pass_number; /* # of passes completed */
|
|
1793
|
|
1794 bool using_merged_upsample; /* true if using merged upsample/cconvert */
|
|
1795
|
|
1796 /* Saved references to initialized quantizer modules,
|
|
1797 * in case we need to switch modes.
|
|
1798 */
|
|
1799 jpeg_color_quantizer quantizer_1pass;
|
|
1800 jpeg_color_quantizer quantizer_2pass;
|
|
1801 }
|
|
1802
|
|
1803 static final class jpeg_inverse_dct {
|
|
1804 // JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
|
|
1805 // /* It is useful to allow each component to have a separate IDCT method. */
|
|
1806 // inverse_DCT_method_ptr inverse_DCT[MAX_COMPONENTS];
|
|
1807 int[MAX_COMPONENTS] cur_method;// = new int[MAX_COMPONENTS];
|
|
1808
|
|
1809 void start_pass (jpeg_decompress_struct cinfo) {
|
|
1810 jpeg_inverse_dct idct = cinfo.idct;
|
|
1811 int ci, i;
|
|
1812 jpeg_component_info compptr;
|
|
1813 int method = 0;
|
|
1814 // inverse_DCT_method_ptr method_ptr = NULL;
|
|
1815 JQUANT_TBL qtbl;
|
|
1816
|
|
1817 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
1818 compptr = cinfo.comp_info[ci];
|
|
1819 /* Select the proper IDCT routine for this component's scaling */
|
|
1820 switch (compptr.DCT_scaled_size) {
|
|
1821 // #ifdef IDCT_SCALING_SUPPORTED
|
|
1822 // case 1:
|
|
1823 // method_ptr = jpeg_idct_1x1;
|
|
1824 // method = JDCT_ISLOW; /* jidctred uses islow-style table */
|
|
1825 // break;
|
|
1826 // case 2:
|
|
1827 // method_ptr = jpeg_idct_2x2;
|
|
1828 // method = JDCT_ISLOW; /* jidctred uses islow-style table */
|
|
1829 // break;
|
|
1830 // case 4:
|
|
1831 // method_ptr = jpeg_idct_4x4;
|
|
1832 // method = JDCT_ISLOW; /* jidctred uses islow-style table */
|
|
1833 // break;
|
|
1834 // #endif
|
|
1835 case DCTSIZE:
|
|
1836 switch (cinfo.dct_method) {
|
|
1837 // #ifdef DCT_ISLOW_SUPPORTED
|
|
1838 case JDCT_ISLOW:
|
|
1839 // method_ptr = jpeg_idct_islow;
|
|
1840 method = JDCT_ISLOW;
|
|
1841 break;
|
|
1842 // #endif
|
|
1843 // #ifdef DCT_IFAST_SUPPORTED
|
|
1844 // case JDCT_IFAST:
|
|
1845 // method_ptr = jpeg_idct_ifast;
|
|
1846 // method = JDCT_IFAST;
|
|
1847 // break;
|
|
1848 // #endif
|
|
1849 // #ifdef DCT_FLOAT_SUPPORTED
|
|
1850 // case JDCT_FLOAT:
|
|
1851 // method_ptr = jpeg_idct_float;
|
|
1852 // method = JDCT_FLOAT;
|
|
1853 // break;
|
|
1854 // #endif
|
|
1855 default:
|
|
1856 error();
|
|
1857 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
1858 break;
|
|
1859 }
|
|
1860 break;
|
|
1861 default:
|
|
1862 error();
|
|
1863 // ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr.DCT_scaled_size);
|
|
1864 break;
|
|
1865 }
|
|
1866 // idct.inverse_DCT[ci] = method_ptr;
|
|
1867 /* Create multiplier table from quant table.
|
|
1868 * However, we can skip this if the component is uninteresting
|
|
1869 * or if we already built the table. Also, if no quant table
|
|
1870 * has yet been saved for the component, we leave the
|
|
1871 * multiplier table all-zero; we'll be reading zeroes from the
|
|
1872 * coefficient controller's buffer anyway.
|
|
1873 */
|
|
1874 if (! compptr.component_needed || idct.cur_method[ci] is method)
|
|
1875 continue;
|
|
1876 qtbl = compptr.quant_table;
|
|
1877 if (qtbl is null) /* happens if no data yet for component */
|
|
1878 continue;
|
|
1879 idct.cur_method[ci] = method;
|
|
1880 switch (method) {
|
|
1881 // #ifdef PROVIDE_ISLOW_TABLES
|
|
1882 case JDCT_ISLOW:
|
|
1883 {
|
|
1884 /* For LL&M IDCT method, multipliers are equal to raw quantization
|
|
1885 * coefficients, but are stored as ints to ensure access efficiency.
|
|
1886 */
|
|
1887 int[] ismtbl = compptr.dct_table;
|
|
1888 for (i = 0; i < DCTSIZE2; i++) {
|
|
1889 ismtbl[i] = qtbl.quantval[i];
|
|
1890 }
|
|
1891 }
|
|
1892 break;
|
|
1893 // #endif
|
|
1894 // #ifdef DCT_IFAST_SUPPORTED
|
|
1895 // case JDCT_IFAST:
|
|
1896 // {
|
|
1897 // /* For AA&N IDCT method, multipliers are equal to quantization
|
|
1898 // * coefficients scaled by scalefactor[row]*scalefactor[col], where
|
|
1899 // * scalefactor[0] = 1
|
|
1900 // * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
|
|
1901 // * For integer operation, the multiplier table is to be scaled by
|
|
1902 // * IFAST_SCALE_BITS.
|
|
1903 // */
|
|
1904 // int[] ifmtbl = compptr.dct_table;
|
|
1905 // short aanscales[] = {
|
|
1906 // /* precomputed values scaled up by 14 bits */
|
|
1907 // 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
1908 // 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
|
|
1909 // 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
|
|
1910 // 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
|
|
1911 // 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
|
|
1912 // 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
|
|
1913 // 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
|
|
1914 // 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
|
|
1915 // };
|
|
1916 // SHIFT_TEMPS
|
|
1917 //
|
|
1918 // for (i = 0; i < DCTSIZE2; i++) {
|
|
1919 // ifmtbl[i] = DESCALE(MULTIPLY16V16( qtbl.quantval[i], aanscales[i]), CONST_BITS-IFAST_SCALE_BITS);
|
|
1920 // }
|
|
1921 // }
|
|
1922 // break;
|
|
1923 // #endif
|
|
1924 // #ifdef DCT_FLOAT_SUPPORTED
|
|
1925 // case JDCT_FLOAT:
|
|
1926 // {
|
|
1927 // /* For float AA&N IDCT method, multipliers are equal to quantization
|
|
1928 // * coefficients scaled by scalefactor[row]*scalefactor[col], where
|
|
1929 // * scalefactor[0] = 1
|
|
1930 // * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
|
|
1931 // */
|
|
1932 // FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr.dct_table;
|
|
1933 // int row, col;
|
|
1934 // static const double aanscalefactor[DCTSIZE] = {
|
|
1935 // 1.0, 1.387039845, 1.306562965, 1.175875602,
|
|
1936 // 1.0, 0.785694958, 0.541196100, 0.275899379
|
|
1937 // };
|
|
1938 //
|
|
1939 // i = 0;
|
|
1940 // for (row = 0; row < DCTSIZE; row++) {
|
|
1941 // for (col = 0; col < DCTSIZE; col++) {
|
|
1942 // fmtbl[i] = (FLOAT_MULT_TYPE)
|
|
1943 // ((double) qtbl.quantval[i] *
|
|
1944 // aanscalefactor[row] * aanscalefactor[col]);
|
|
1945 // i++;
|
|
1946 // }
|
|
1947 // }
|
|
1948 // }
|
|
1949 // break;
|
|
1950 // #endif
|
|
1951 default:
|
|
1952 error();
|
|
1953 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
1954 break;
|
|
1955 }
|
|
1956 }
|
|
1957 }
|
|
1958 }
|
|
1959
|
|
1960 static final class jpeg_input_controller {
|
|
1961 int consume_input;
|
|
1962 bool has_multiple_scans; /* True if file has multiple scans */
|
|
1963 bool eoi_reached;
|
|
1964
|
|
1965 bool inheaders; /* true until first SOS is reached */
|
|
1966 }
|
|
1967
|
|
1968 static final class jpeg_color_deconverter {
|
|
1969 // JMETHOD(void, start_pass, (j_decompress_ptr cinfo));
|
|
1970 int color_convert;
|
|
1971
|
|
1972 /* Private state for YCC.RGB conversion */
|
|
1973 int[] Cr_r_tab; /* => table for Cr to R conversion */
|
|
1974 int[] Cb_b_tab; /* => table for Cb to B conversion */
|
|
1975 int[] Cr_g_tab; /* => table for Cr to G conversion */
|
|
1976 int[] Cb_g_tab; /* => table for Cb to G conversion */
|
|
1977
|
|
1978 void start_pass (jpeg_decompress_struct cinfo) {
|
|
1979 /* no work needed */
|
|
1980 }
|
|
1981
|
|
1982 }
|
|
1983
|
|
1984 static final class jpeg_d_post_controller {
|
|
1985 // JMETHOD(void, start_pass, (j_decompress_ptr cinfo, J_BUF_MODE pass_mode));
|
|
1986 int post_process_data;
|
|
1987
|
|
1988 /* Color quantization source buffer: this holds output data from
|
|
1989 * the upsample/color conversion step to be passed to the quantizer.
|
|
1990 * For two-pass color quantization, we need a full-image buffer;
|
|
1991 * for one-pass operation, a strip buffer is sufficient.
|
|
1992 */
|
|
1993 int[] whole_image; /* virtual array, or NULL if one-pass */
|
|
1994 int[][] buffer; /* strip buffer, or current strip of virtual */
|
|
1995 int strip_height; /* buffer size in rows */
|
|
1996 /* for two-pass mode only: */
|
|
1997 int starting_row; /* row # of first row in current strip */
|
|
1998 int next_row; /* index of next row to fill/empty in strip */
|
|
1999
|
|
2000 void start_pass (jpeg_decompress_struct cinfo, int pass_mode) {
|
|
2001 jpeg_d_post_controller post = cinfo.post;
|
|
2002
|
|
2003 switch (pass_mode) {
|
|
2004 case JBUF_PASS_THRU:
|
|
2005 if (cinfo.quantize_colors) {
|
|
2006 error(SWT.ERROR_NOT_IMPLEMENTED);
|
|
2007 // /* Single-pass processing with color quantization. */
|
|
2008 // post.post_process_data = POST_PROCESS_1PASS;
|
|
2009 // /* We could be doing buffered-image output before starting a 2-pass
|
|
2010 // * color quantization; in that case, jinit_d_post_controller did not
|
|
2011 // * allocate a strip buffer. Use the virtual-array buffer as workspace.
|
|
2012 // */
|
|
2013 // if (post.buffer is null) {
|
|
2014 // post.buffer = (*cinfo.mem.access_virt_sarray)
|
|
2015 // ((j_common_ptr) cinfo, post.whole_image,
|
|
2016 // (JDIMENSION) 0, post.strip_height, TRUE);
|
|
2017 // }
|
|
2018 } else {
|
|
2019 /* For single-pass processing without color quantization,
|
|
2020 * I have no work to do; just call the upsampler directly.
|
|
2021 */
|
|
2022 post.post_process_data = POST_PROCESS_DATA_UPSAMPLE;
|
|
2023 }
|
|
2024 break;
|
|
2025 // #ifdef QUANT_2PASS_SUPPORTED
|
|
2026 // case JBUF_SAVE_AND_PASS:
|
|
2027 // /* First pass of 2-pass quantization */
|
|
2028 // if (post.whole_image is NULL)
|
|
2029 // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
|
2030 // post.pub.post_process_data = post_process_prepass;
|
|
2031 // break;
|
|
2032 // case JBUF_CRANK_DEST:
|
|
2033 // /* Second pass of 2-pass quantization */
|
|
2034 // if (post.whole_image is NULL)
|
|
2035 // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
|
2036 // post.pub.post_process_data = post_process_2pass;
|
|
2037 // break;
|
|
2038 // #endif /* QUANT_2PASS_SUPPORTED */
|
|
2039 default:
|
|
2040 error();
|
|
2041 // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
|
2042 break;
|
|
2043 }
|
|
2044 post.starting_row = post.next_row = 0;
|
|
2045 }
|
|
2046
|
|
2047 }
|
|
2048
|
|
2049 static final class jpeg_decompress_struct {
|
|
2050 // jpeg_error_mgr * err; /* Error handler module */\
|
|
2051 // struct jpeg_memory_mgr * mem; /* Memory manager module */\
|
|
2052 // struct jpeg_progress_mgr * progress; /* Progress monitor, or null if none */\
|
|
2053 // void * client_data; /* Available for use by application */\
|
|
2054 bool is_decompressor; /* So common code can tell which is which */
|
|
2055 int global_state; /* For checking call sequence validity */
|
|
2056
|
|
2057 // /* Source of compressed data */
|
|
2058 // struct jpeg_source_mgr * src;
|
|
2059 InputStream inputStream;
|
|
2060 byte[] buffer;
|
|
2061 int bytes_in_buffer;
|
|
2062 int bytes_offset;
|
|
2063 bool start_of_file;
|
|
2064
|
|
2065 /* Basic description of image --- filled in by jpeg_read_header(). */
|
|
2066 /* Application may inspect these values to decide how to process image. */
|
|
2067
|
|
2068 int image_width; /* nominal image width (from SOF marker) */
|
|
2069 int image_height; /* nominal image height */
|
|
2070 int num_components; /* # of color components in JPEG image */
|
|
2071 int jpeg_color_space; /* colorspace of JPEG image */
|
|
2072
|
|
2073 /* Decompression processing parameters --- these fields must be set before
|
|
2074 * calling jpeg_start_decompress(). Note that jpeg_read_header() initializes
|
|
2075 * them to default values.
|
|
2076 */
|
|
2077
|
|
2078 int out_color_space; /* colorspace for output */
|
|
2079
|
|
2080 int scale_num, scale_denom; /* fraction by which to scale image */
|
|
2081
|
|
2082 double output_gamma; /* image gamma wanted in output */
|
|
2083
|
|
2084 bool buffered_image; /* true=multiple output passes */
|
|
2085 bool raw_data_out; /* true=downsampled data wanted */
|
|
2086
|
|
2087 int dct_method; /* IDCT algorithm selector */
|
|
2088 bool do_fancy_upsampling; /* true=apply fancy upsampling */
|
|
2089 bool do_block_smoothing; /* true=apply interblock smoothing */
|
|
2090
|
|
2091 bool quantize_colors; /* true=colormapped output wanted */
|
|
2092 /* the following are ignored if not quantize_colors: */
|
|
2093 int dither_mode; /* type of color dithering to use */
|
|
2094 bool two_pass_quantize; /* true=use two-pass color quantization */
|
|
2095 int desired_number_of_colors; /* max # colors to use in created colormap */
|
|
2096 /* these are significant only in buffered-image mode: */
|
|
2097 bool enable_1pass_quant; /* enable future use of 1-pass quantizer */
|
|
2098 bool enable_external_quant;/* enable future use of external colormap */
|
|
2099 bool enable_2pass_quant; /* enable future use of 2-pass quantizer */
|
|
2100
|
|
2101 /* Description of actual output image that will be returned to application.
|
|
2102 * These fields are computed by jpeg_start_decompress().
|
|
2103 * You can also use jpeg_calc_output_dimensions() to determine these values
|
|
2104 * in advance of calling jpeg_start_decompress().
|
|
2105 */
|
|
2106
|
|
2107 int output_width; /* scaled image width */
|
|
2108 int output_height; /* scaled image height */
|
|
2109 int out_color_components; /* # of color components in out_color_space */
|
|
2110 int output_components; /* # of color components returned */
|
|
2111 /* output_components is 1 (a colormap index) when quantizing colors;
|
|
2112 * otherwise it equals out_color_components.
|
|
2113 */
|
|
2114 int rec_outbuf_height; /* min recommended height of scanline buffer */
|
|
2115 /* If the buffer passed to jpeg_read_scanlines() is less than this many rows
|
|
2116 * high, space and time will be wasted due to unnecessary data copying.
|
|
2117 * Usually rec_outbuf_height will be 1 or 2, at most 4.
|
|
2118 */
|
|
2119
|
|
2120 /* When quantizing colors, the output colormap is described by these fields.
|
|
2121 * The application can supply a colormap by setting colormap non-null before
|
|
2122 * calling jpeg_start_decompress; otherwise a colormap is created during
|
|
2123 * jpeg_start_decompress or jpeg_start_output.
|
|
2124 * The map has out_color_components rows and actual_number_of_colors columns.
|
|
2125 */
|
|
2126 int actual_number_of_colors; /* number of entries in use */
|
|
2127 int[] colormap; /* The color map as a 2-D pixel array */
|
|
2128
|
|
2129 /* State variables: these variables indicate the progress of decompression.
|
|
2130 * The application may examine these but must not modify them.
|
|
2131 */
|
|
2132
|
|
2133 /* Row index of next scanline to be read from jpeg_read_scanlines().
|
|
2134 * Application may use this to control its processing loop, e.g.,
|
|
2135 * "while (output_scanline < output_height)".
|
|
2136 */
|
|
2137 int output_scanline; /* 0 .. output_height-1 */
|
|
2138
|
|
2139 /* Current input scan number and number of iMCU rows completed in scan.
|
|
2140 * These indicate the progress of the decompressor input side.
|
|
2141 */
|
|
2142 int input_scan_number; /* Number of SOS markers seen so far */
|
|
2143 int input_iMCU_row; /* Number of iMCU rows completed */
|
|
2144
|
|
2145 /* The "output scan number" is the notional scan being displayed by the
|
|
2146 * output side. The decompressor will not allow output scan/row number
|
|
2147 * to get ahead of input scan/row, but it can fall arbitrarily far behind.
|
|
2148 */
|
|
2149 int output_scan_number; /* Nominal scan number being displayed */
|
|
2150 int output_iMCU_row; /* Number of iMCU rows read */
|
|
2151
|
|
2152 /* Current progression status. coef_bits[c][i] indicates the precision
|
|
2153 * with which component c's DCT coefficient i (in zigzag order) is known.
|
|
2154 * It is -1 when no data has yet been received, otherwise it is the point
|
|
2155 * transform (shift) value for the most recent scan of the coefficient
|
|
2156 * (thus, 0 at completion of the progression).
|
|
2157 * This pointer is null when reading a non-progressive file.
|
|
2158 */
|
|
2159 int[][] coef_bits; /* -1 or current Al value for each coef */
|
|
2160
|
|
2161 /* Internal JPEG parameters --- the application usually need not look at
|
|
2162 * these fields. Note that the decompressor output side may not use
|
|
2163 * any parameters that can change between scans.
|
|
2164 */
|
|
2165
|
|
2166 /* Quantization and Huffman tables are carried forward across input
|
|
2167 * datastreams when processing abbreviated JPEG datastreams.
|
|
2168 */
|
|
2169
|
|
2170 JQUANT_TBL[NUM_QUANT_TBLS] quant_tbl_ptrs;// = new JQUANT_TBL[NUM_QUANT_TBLS];
|
|
2171 /* ptrs to coefficient quantization tables, or null if not defined */
|
|
2172
|
|
2173 JHUFF_TBL[NUM_HUFF_TBLS] dc_huff_tbl_ptrs;// = new JHUFF_TBL[NUM_HUFF_TBLS];
|
|
2174 JHUFF_TBL[NUM_HUFF_TBLS] ac_huff_tbl_ptrs;// = new JHUFF_TBL[NUM_HUFF_TBLS];
|
|
2175 /* ptrs to Huffman coding tables, or null if not defined */
|
|
2176
|
|
2177 /* These parameters are never carried across datastreams, since they
|
|
2178 * are given in SOF/SOS markers or defined to be reset by SOI.
|
|
2179 */
|
|
2180
|
|
2181 int data_precision; /* bits of precision in image data */
|
|
2182
|
|
2183 jpeg_component_info[] comp_info;
|
|
2184 /* comp_info[i] describes component that appears i'th in SOF */
|
|
2185
|
|
2186 bool progressive_mode; /* true if SOFn specifies progressive mode */
|
|
2187 bool arith_code; /* true=arithmetic coding, false=Huffman */
|
|
2188
|
|
2189 byte[NUM_ARITH_TBLS] arith_dc_L;// = new byte[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
|
|
2190 byte[NUM_ARITH_TBLS] arith_dc_U;// = new byte[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
|
|
2191 byte[NUM_ARITH_TBLS] arith_ac_K;// = new byte[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
|
|
2192
|
|
2193 int restart_interval; /* MCUs per restart interval, or 0 for no restart */
|
|
2194
|
|
2195 /* These fields record data obtained from optional markers recognized by
|
|
2196 * the JPEG library.
|
|
2197 */
|
|
2198 bool saw_JFIF_marker; /* true iff a JFIF APP0 marker was found */
|
|
2199 /* Data copied from JFIF marker; only valid if saw_JFIF_marker is true: */
|
|
2200 byte JFIF_major_version; /* JFIF version number */
|
|
2201 byte JFIF_minor_version;
|
|
2202 byte density_unit; /* JFIF code for pixel size units */
|
|
2203 short X_density; /* Horizontal pixel density */
|
|
2204 short Y_density; /* Vertical pixel density */
|
|
2205 bool saw_Adobe_marker; /* true iff an Adobe APP14 marker was found */
|
|
2206 byte Adobe_transform; /* Color transform code from Adobe marker */
|
|
2207
|
|
2208 bool CCIR601_sampling; /* true=first samples are cosited */
|
|
2209
|
|
2210 /* Aside from the specific data retained from APPn markers known to the
|
|
2211 * library, the uninterpreted contents of any or all APPn and COM markers
|
|
2212 * can be saved in a list for examination by the application.
|
|
2213 */
|
|
2214 jpeg_marker_reader marker_list; /* Head of list of saved markers */
|
|
2215
|
|
2216 /* Remaining fields are known throughout decompressor, but generally
|
|
2217 * should not be touched by a surrounding application.
|
|
2218 */
|
|
2219
|
|
2220 /*
|
|
2221 * These fields are computed during decompression startup
|
|
2222 */
|
|
2223 int max_h_samp_factor; /* largest h_samp_factor */
|
|
2224 int max_v_samp_factor; /* largest v_samp_factor */
|
|
2225
|
|
2226 int min_DCT_scaled_size; /* smallest DCT_scaled_size of any component */
|
|
2227
|
|
2228 int total_iMCU_rows; /* # of iMCU rows in image */
|
|
2229 /* The coefficient controller's input and output progress is measured in
|
|
2230 * units of "iMCU" (interleaved MCU) rows. These are the same as MCU rows
|
|
2231 * in fully interleaved JPEG scans, but are used whether the scan is
|
|
2232 * interleaved or not. We define an iMCU row as v_samp_factor DCT block
|
|
2233 * rows of each component. Therefore, the IDCT output contains
|
|
2234 * v_samp_factor*DCT_scaled_size sample rows of a component per iMCU row.
|
|
2235 */
|
|
2236
|
|
2237 byte[] sample_range_limit; /* table for fast range-limiting */
|
|
2238 int sample_range_limit_offset;
|
|
2239
|
|
2240 /*
|
|
2241 * These fields are valid during any one scan.
|
|
2242 * They describe the components and MCUs actually appearing in the scan.
|
|
2243 * Note that the decompressor output side must not use these fields.
|
|
2244 */
|
|
2245 int comps_in_scan; /* # of JPEG components in this scan */
|
|
2246 jpeg_component_info[MAX_COMPS_IN_SCAN] cur_comp_info;// = new jpeg_component_info[MAX_COMPS_IN_SCAN];
|
|
2247 /* *cur_comp_info[i] describes component that appears i'th in SOS */
|
|
2248
|
|
2249 int MCUs_per_row; /* # of MCUs across the image */
|
|
2250 int MCU_rows_in_scan; /* # of MCU rows in the image */
|
|
2251
|
|
2252 int blocks_in_MCU; /* # of DCT blocks per MCU */
|
|
2253 int[D_MAX_BLOCKS_IN_MCU] MCU_membership;// = new int[D_MAX_BLOCKS_IN_MCU];
|
|
2254 /* MCU_membership[i] is index in cur_comp_info of component owning */
|
|
2255 /* i'th block in an MCU */
|
|
2256
|
|
2257 int Ss, Se, Ah, Al; /* progressive JPEG parameters for scan */
|
|
2258
|
|
2259 /* This field is shared between entropy decoder and marker parser.
|
|
2260 * It is either zero or the code of a JPEG marker that has been
|
|
2261 * read from the data source, but has not yet been processed.
|
|
2262 */
|
|
2263 int unread_marker;
|
|
2264
|
|
2265 int[DCTSIZE2] workspace;// = new int[DCTSIZE2];
|
|
2266 int[1] row_ctr;// = new int[1];
|
|
2267
|
|
2268 /*
|
|
2269 * Links to decompression subobjects (methods, private variables of modules)
|
|
2270 */
|
|
2271 jpeg_decomp_master master;
|
|
2272 jpeg_d_main_controller main;
|
|
2273 jpeg_d_coef_controller coef;
|
|
2274 jpeg_d_post_controller post;
|
|
2275 jpeg_input_controller inputctl;
|
|
2276 jpeg_marker_reader marker;
|
|
2277 jpeg_entropy_decoder entropy;
|
|
2278 jpeg_inverse_dct idct;
|
|
2279 jpeg_upsampler upsample;
|
|
2280 jpeg_color_deconverter cconvert;
|
|
2281 jpeg_color_quantizer cquantize;
|
|
2282 }
|
|
2283
|
|
2284 static void error() {
|
|
2285 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
2286 }
|
|
2287
|
|
2288 static void error(int code) {
|
|
2289 SWT.error(code);
|
|
2290 }
|
|
2291
|
|
2292 static void error(String msg) {
|
|
2293 SWT.error(SWT.ERROR_INVALID_IMAGE, null, msg);
|
|
2294 }
|
|
2295
|
|
2296 static void jinit_marker_reader (jpeg_decompress_struct cinfo) {
|
|
2297 jpeg_marker_reader marker = cinfo.marker = new jpeg_marker_reader();
|
|
2298 // int i;
|
|
2299
|
|
2300 /* Initialize COM/APPn processing.
|
|
2301 * By default, we examine and then discard APP0 and APP14,
|
|
2302 * but simply discard COM and all other APPn.
|
|
2303 */
|
|
2304 // marker.process_COM = skip_variable;
|
|
2305 marker.length_limit_COM = 0;
|
|
2306 // for (i = 0; i < 16; i++) {
|
|
2307 // marker.process_APPn[i] = skip_variable;
|
|
2308 // marker.length_limit_APPn[i] = 0;
|
|
2309 // }
|
|
2310 // marker.process_APPn[0] = get_interesting_appn;
|
|
2311 // marker.process_APPn[14] = get_interesting_appn;
|
|
2312 /* Reset marker processing state */
|
|
2313 reset_marker_reader(cinfo);
|
|
2314 }
|
|
2315
|
|
2316 static void jinit_d_coef_controller (jpeg_decompress_struct cinfo, bool need_full_buffer) {
|
|
2317 jpeg_d_coef_controller coef = new jpeg_d_coef_controller();
|
|
2318 cinfo.coef = coef;
|
|
2319 // coef.pub.start_input_pass = start_input_pass;
|
|
2320 // coef.pub.start_output_pass = start_output_pass;
|
|
2321 coef.coef_bits_latch = null;
|
|
2322
|
|
2323 /* Create the coefficient buffer. */
|
|
2324 if (need_full_buffer) {
|
|
2325 //#ifdef D_MULTISCAN_FILES_SUPPORTED
|
|
2326 /* Allocate a full-image virtual array for each component, */
|
|
2327 /* padded to a multiple of samp_factor DCT blocks in each direction. */
|
|
2328 /* Note we ask for a pre-zeroed array. */
|
|
2329 int ci, access_rows;
|
|
2330 jpeg_component_info compptr;
|
|
2331
|
|
2332 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2333 compptr = cinfo.comp_info[ci];
|
|
2334 access_rows = compptr.v_samp_factor;
|
|
2335 //#ifdef BLOCK_SMOOTHING_SUPPORTED
|
|
2336 /* If block smoothing could be used, need a bigger window */
|
|
2337 if (cinfo.progressive_mode)
|
|
2338 access_rows *= 3;
|
|
2339 //#endif
|
|
2340 coef.whole_image[ci] =
|
|
2341 new short[][][](
|
|
2342 cast(int)jround_up( compptr.height_in_blocks, compptr.v_samp_factor),
|
|
2343 cast(int)jround_up( compptr.width_in_blocks, compptr.h_samp_factor),
|
|
2344 DCTSIZE2
|
|
2345 );
|
|
2346 }
|
|
2347 // coef.consume_data = consume_data;
|
|
2348 coef.decompress_data = DECOMPRESS_DATA;
|
|
2349 coef.coef_arrays = coef.whole_image[0]; /* link to virtual arrays */
|
|
2350 // #else
|
|
2351 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
2352 // #endif
|
|
2353 } else {
|
|
2354 /* We only need a single-MCU buffer. */
|
|
2355 foreach( inout el; coef.MCU_buffer ){
|
|
2356 el = new short[](DCTSIZE2);
|
|
2357 }
|
|
2358 // coef.consume_data = dummy_consume_data;
|
|
2359 coef.decompress_data = DECOMPRESS_ONEPASS;
|
|
2360 coef.coef_arrays = null; /* flag for no virtual arrays */
|
|
2361 }
|
|
2362 }
|
|
2363
|
|
2364 static void start_output_pass (jpeg_decompress_struct cinfo) {
|
|
2365 //#ifdef BLOCK_SMOOTHING_SUPPORTED
|
|
2366 jpeg_d_coef_controller coef = cinfo.coef;
|
|
2367
|
|
2368 /* If multipass, check to see whether to use block smoothing on this pass */
|
|
2369 if (coef.coef_arrays !is null) {
|
|
2370 if (cinfo.do_block_smoothing && smoothing_ok(cinfo))
|
|
2371 coef.decompress_data = DECOMPRESS_SMOOTH_DATA;
|
|
2372 else
|
|
2373 coef.decompress_data = DECOMPRESS_DATA;
|
|
2374 }
|
|
2375 //#endif
|
|
2376 cinfo.output_iMCU_row = 0;
|
|
2377 }
|
|
2378
|
|
2379 static void jpeg_create_decompress(jpeg_decompress_struct cinfo) {
|
|
2380 cinfo.is_decompressor = true;
|
|
2381
|
|
2382
|
|
2383 /* Initialize marker processor so application can override methods
|
|
2384 * for COM, APPn markers before calling jpeg_read_header.
|
|
2385 */
|
|
2386 cinfo.marker_list = null;
|
|
2387 jinit_marker_reader(cinfo);
|
|
2388
|
|
2389 /* And initialize the overall input controller. */
|
|
2390 jinit_input_controller(cinfo);
|
|
2391
|
|
2392 /* OK, I'm ready */
|
|
2393 cinfo.global_state = DSTATE_START;
|
|
2394 }
|
|
2395
|
|
2396 static void jpeg_calc_output_dimensions (jpeg_decompress_struct cinfo)
|
|
2397 /* Do computations that are needed before master selection phase */
|
|
2398 {
|
|
2399 //#ifdef IDCT_SCALING_SUPPORTED
|
|
2400 // int ci;
|
|
2401 // jpeg_component_info compptr;
|
|
2402 //#endif
|
|
2403
|
|
2404 /* Prevent application from calling me at wrong times */
|
|
2405 if (cinfo.global_state !is DSTATE_READY)
|
|
2406 error();
|
|
2407 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
2408
|
|
2409 //#ifdef IDCT_SCALING_SUPPORTED
|
|
2410 //
|
|
2411 // /* Compute actual output image dimensions and DCT scaling choices. */
|
|
2412 // if (cinfo.scale_num * 8 <= cinfo.scale_denom) {
|
|
2413 // /* Provide 1/8 scaling */
|
|
2414 // cinfo.output_width = cast(int)
|
|
2415 // jdiv_round_up(cinfo.image_width, 8L);
|
|
2416 // cinfo.output_height = cast(int)
|
|
2417 // jdiv_round_up(cinfo.image_height, 8L);
|
|
2418 // cinfo.min_DCT_scaled_size = 1;
|
|
2419 // } else if (cinfo.scale_num * 4 <= cinfo.scale_denom) {
|
|
2420 // /* Provide 1/4 scaling */
|
|
2421 // cinfo.output_width = cast(int)
|
|
2422 // jdiv_round_up(cinfo.image_width, 4L);
|
|
2423 // cinfo.output_height = cast(int)
|
|
2424 // jdiv_round_up(cinfo.image_height, 4L);
|
|
2425 // cinfo.min_DCT_scaled_size = 2;
|
|
2426 // } else if (cinfo.scale_num * 2 <= cinfo.scale_denom) {
|
|
2427 // /* Provide 1/2 scaling */
|
|
2428 // cinfo.output_width = cast(int)
|
|
2429 // jdiv_round_up(cinfo.image_width, 2L);
|
|
2430 // cinfo.output_height = cast(int)
|
|
2431 // jdiv_round_up(cinfo.image_height, 2L);
|
|
2432 // cinfo.min_DCT_scaled_size = 4;
|
|
2433 // } else {
|
|
2434 // /* Provide 1/1 scaling */
|
|
2435 // cinfo.output_width = cinfo.image_width;
|
|
2436 // cinfo.output_height = cinfo.image_height;
|
|
2437 // cinfo.min_DCT_scaled_size = DCTSIZE;
|
|
2438 // }
|
|
2439 // /* In selecting the actual DCT scaling for each component, we try to
|
|
2440 // * scale up the chroma components via IDCT scaling rather than upsampling.
|
|
2441 // * This saves time if the upsampler gets to use 1:1 scaling.
|
|
2442 // * Note this code assumes that the supported DCT scalings are powers of 2.
|
|
2443 // */
|
|
2444 // for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2445 // compptr = cinfo.comp_info[ci];
|
|
2446 // int ssize = cinfo.min_DCT_scaled_size;
|
|
2447 // while (ssize < DCTSIZE &&
|
|
2448 // (compptr.h_samp_factor * ssize * 2 <= cinfo.max_h_samp_factor * cinfo.min_DCT_scaled_size) &&
|
|
2449 // (compptr.v_samp_factor * ssize * 2 <= cinfo.max_v_samp_factor * cinfo.min_DCT_scaled_size))
|
|
2450 // {
|
|
2451 // ssize = ssize * 2;
|
|
2452 // }
|
|
2453 // compptr.DCT_scaled_size = ssize;
|
|
2454 // }
|
|
2455 //
|
|
2456 // /* Recompute downsampled dimensions of components;
|
|
2457 // * application needs to know these if using raw downsampled data.
|
|
2458 // */
|
|
2459 // for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2460 // compptr = cinfo.comp_info[ci];
|
|
2461 // /* Size in samples, after IDCT scaling */
|
|
2462 // compptr.downsampled_width = cast(int)
|
|
2463 // jdiv_round_up(cast(long) cinfo.image_width * cast(long) (compptr.h_samp_factor * compptr.DCT_scaled_size),
|
|
2464 // (cinfo.max_h_samp_factor * DCTSIZE));
|
|
2465 // compptr.downsampled_height = cast(int)
|
|
2466 // jdiv_round_up(cast(long) cinfo.image_height * cast(long) (compptr.v_samp_factor * compptr.DCT_scaled_size),
|
|
2467 // (cinfo.max_v_samp_factor * DCTSIZE));
|
|
2468 // }
|
|
2469 //
|
|
2470 //#else /* !IDCT_SCALING_SUPPORTED */
|
|
2471
|
|
2472 /* Hardwire it to "no scaling" */
|
|
2473 cinfo.output_width = cinfo.image_width;
|
|
2474 cinfo.output_height = cinfo.image_height;
|
|
2475 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
|
|
2476 * and has computed unscaled downsampled_width and downsampled_height.
|
|
2477 */
|
|
2478
|
|
2479 //#endif /* IDCT_SCALING_SUPPORTED */
|
|
2480
|
|
2481 /* Report number of components in selected colorspace. */
|
|
2482 /* Probably this should be in the color conversion module... */
|
|
2483 switch (cinfo.out_color_space) {
|
|
2484 case JCS_GRAYSCALE:
|
|
2485 cinfo.out_color_components = 1;
|
|
2486 break;
|
|
2487 case JCS_RGB:
|
|
2488 if (RGB_PIXELSIZE !is 3) {
|
|
2489 cinfo.out_color_components = RGB_PIXELSIZE;
|
|
2490 break;
|
|
2491 }
|
|
2492 //FALLTHROUGH
|
|
2493 case JCS_YCbCr:
|
|
2494 cinfo.out_color_components = 3;
|
|
2495 break;
|
|
2496 case JCS_CMYK:
|
|
2497 case JCS_YCCK:
|
|
2498 cinfo.out_color_components = 4;
|
|
2499 break;
|
|
2500 default: /* else must be same colorspace as in file */
|
|
2501 cinfo.out_color_components = cinfo.num_components;
|
|
2502 break;
|
|
2503 }
|
|
2504 cinfo.output_components = (cinfo.quantize_colors ? 1 : cinfo.out_color_components);
|
|
2505
|
|
2506 /* See if upsampler will want to emit more than one row at a time */
|
|
2507 if (use_merged_upsample(cinfo))
|
|
2508 cinfo.rec_outbuf_height = cinfo.max_v_samp_factor;
|
|
2509 else
|
|
2510 cinfo.rec_outbuf_height = 1;
|
|
2511 }
|
|
2512
|
|
2513 static bool use_merged_upsample (jpeg_decompress_struct cinfo) {
|
|
2514 //#ifdef UPSAMPLE_MERGING_SUPPORTED
|
|
2515 /* Merging is the equivalent of plain box-filter upsampling */
|
|
2516 if (cinfo.do_fancy_upsampling || cinfo.CCIR601_sampling)
|
|
2517 return false;
|
|
2518 /* jdmerge.c only supports YCC=>RGB color conversion */
|
|
2519 if (cinfo.jpeg_color_space !is JCS_YCbCr || cinfo.num_components !is 3 ||
|
|
2520 cinfo.out_color_space !is JCS_RGB ||
|
|
2521 cinfo.out_color_components !is RGB_PIXELSIZE)
|
|
2522 return false;
|
|
2523 /* and it only handles 2h1v or 2h2v sampling ratios */
|
|
2524 if (cinfo.comp_info[0].h_samp_factor !is 2 ||
|
|
2525 cinfo.comp_info[1].h_samp_factor !is 1 ||
|
|
2526 cinfo.comp_info[2].h_samp_factor !is 1 ||
|
|
2527 cinfo.comp_info[0].v_samp_factor > 2 ||
|
|
2528 cinfo.comp_info[1].v_samp_factor !is 1 ||
|
|
2529 cinfo.comp_info[2].v_samp_factor !is 1)
|
|
2530 return false;
|
|
2531 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
|
|
2532 if (cinfo.comp_info[0].DCT_scaled_size !is cinfo.min_DCT_scaled_size ||
|
|
2533 cinfo.comp_info[1].DCT_scaled_size !is cinfo.min_DCT_scaled_size ||
|
|
2534 cinfo.comp_info[2].DCT_scaled_size !is cinfo.min_DCT_scaled_size)
|
|
2535 return false;
|
|
2536 /* ??? also need to test for upsample-time rescaling, when & if supported */
|
|
2537 return true; /* by golly, it'll work... */
|
|
2538 //#else
|
|
2539 // return false;
|
|
2540 //#endif
|
|
2541 }
|
|
2542
|
|
2543 static void prepare_range_limit_table (jpeg_decompress_struct cinfo)
|
|
2544 /* Allocate and fill in the sample_range_limit table */
|
|
2545 {
|
|
2546 byte[] table;
|
|
2547 int i;
|
|
2548
|
|
2549 table = new byte[5 * (MAXJSAMPLE+1) + CENTERJSAMPLE];
|
|
2550 int offset = (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
|
|
2551 cinfo.sample_range_limit_offset = offset;
|
|
2552 cinfo.sample_range_limit = table;
|
|
2553 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
|
|
2554 /* Main part of "simple" table: limit[x] = x */
|
|
2555 for (i = 0; i <= MAXJSAMPLE; i++)
|
|
2556 table[i + offset] = cast(byte)i;
|
|
2557 offset += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
|
|
2558 /* End of simple table, rest of first half of post-IDCT table */
|
|
2559 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
|
|
2560 table[i+offset] = cast(byte)MAXJSAMPLE;
|
|
2561 /* Second half of post-IDCT table */
|
|
2562 System.arraycopy(cinfo.sample_range_limit, cinfo.sample_range_limit_offset, table, offset + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE), CENTERJSAMPLE);
|
|
2563 }
|
|
2564
|
|
2565 static void build_ycc_rgb_table (jpeg_decompress_struct cinfo) {
|
|
2566 jpeg_color_deconverter cconvert = cinfo.cconvert;
|
|
2567 int i;
|
|
2568 int x;
|
|
2569 // SHIFT_TEMPS
|
|
2570
|
|
2571 cconvert.Cr_r_tab = new int[MAXJSAMPLE+1];
|
|
2572 cconvert.Cb_b_tab = new int[MAXJSAMPLE+1];
|
|
2573 cconvert.Cr_g_tab = new int[MAXJSAMPLE+1];
|
|
2574 cconvert.Cb_g_tab = new int[MAXJSAMPLE+1];
|
|
2575
|
|
2576 for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
|
|
2577 /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
|
|
2578 /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
|
|
2579 /* Cr=>R value is nearest int to 1.40200 * x */
|
|
2580 cconvert.Cr_r_tab[i] = (cast(int)(1.40200f * (1<<SCALEBITS) + 0.5f) * x + ONE_HALF) >> SCALEBITS;
|
|
2581 /* Cb=>B value is nearest int to 1.77200 * x */
|
|
2582 cconvert.Cb_b_tab[i] = (cast(int)(1.77200f * (1<<SCALEBITS) + 0.5f) * x + ONE_HALF) >> SCALEBITS;
|
|
2583 /* Cr=>G value is scaled-up -0.71414 * x */
|
|
2584 cconvert.Cr_g_tab[i] = (cast(int)(- (0.71414f * (1<<SCALEBITS) + 0.5f)) * x);
|
|
2585 /* Cb=>G value is scaled-up -0.34414 * x */
|
|
2586 /* We also add in ONE_HALF so that need not do it in inner loop */
|
|
2587 cconvert.Cb_g_tab[i] = (cast(int)(- (0.34414f* (1<<SCALEBITS) + 0.5f)) * x + ONE_HALF);
|
|
2588 }
|
|
2589 }
|
|
2590
|
|
2591 static void jinit_color_deconverter (jpeg_decompress_struct cinfo) {
|
|
2592 jpeg_color_deconverter cconvert = cinfo.cconvert = new jpeg_color_deconverter();
|
|
2593 // cconvert.start_pass = start_pass_dcolor;
|
|
2594
|
|
2595 /* Make sure num_components agrees with jpeg_color_space */
|
|
2596 switch (cinfo.jpeg_color_space) {
|
|
2597 case JCS_GRAYSCALE:
|
|
2598 if (cinfo.num_components !is 1)
|
|
2599 error();
|
|
2600 // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
|
2601 break;
|
|
2602
|
|
2603 case JCS_RGB:
|
|
2604 case JCS_YCbCr:
|
|
2605 if (cinfo.num_components !is 3)
|
|
2606 error();
|
|
2607 // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
|
2608 break;
|
|
2609
|
|
2610 case JCS_CMYK:
|
|
2611 case JCS_YCCK:
|
|
2612 if (cinfo.num_components !is 4)
|
|
2613 error();
|
|
2614 // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
|
2615 break;
|
|
2616
|
|
2617 default: /* JCS_UNKNOWN can be anything */
|
|
2618 if (cinfo.num_components < 1)
|
|
2619 error();
|
|
2620 // ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
|
|
2621 break;
|
|
2622 }
|
|
2623
|
|
2624 /* Set out_color_components and conversion method based on requested space.
|
|
2625 * Also clear the component_needed flags for any unused components,
|
|
2626 * so that earlier pipeline stages can avoid useless computation.
|
|
2627 */
|
|
2628
|
|
2629 int ci;
|
|
2630 switch (cinfo.out_color_space) {
|
|
2631 case JCS_GRAYSCALE:
|
|
2632 cinfo.out_color_components = 1;
|
|
2633 if (cinfo.jpeg_color_space is JCS_GRAYSCALE || cinfo.jpeg_color_space is JCS_YCbCr) {
|
|
2634 cconvert.color_convert = GRAYSCALE_CONVERT;
|
|
2635 /* For color.grayscale conversion, only the Y (0) component is needed */
|
|
2636 for (ci = 1; ci < cinfo.num_components; ci++)
|
|
2637 cinfo.comp_info[ci].component_needed = false;
|
|
2638 } else
|
|
2639 error();
|
|
2640 // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
2641 break;
|
|
2642
|
|
2643 case JCS_RGB:
|
|
2644 cinfo.out_color_components = RGB_PIXELSIZE;
|
|
2645 if (cinfo.jpeg_color_space is JCS_YCbCr) {
|
|
2646 cconvert.color_convert = YCC_RGB_CONVERT;
|
|
2647 build_ycc_rgb_table(cinfo);
|
|
2648 } else if (cinfo.jpeg_color_space is JCS_GRAYSCALE) {
|
|
2649 cconvert.color_convert = GRAY_RGB_CONVERT;
|
|
2650 } else if (cinfo.jpeg_color_space is JCS_RGB && RGB_PIXELSIZE is 3) {
|
|
2651 cconvert.color_convert = NULL_CONVERT;
|
|
2652 } else
|
|
2653 error();
|
|
2654 // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
2655 break;
|
|
2656
|
|
2657 case JCS_CMYK:
|
|
2658 cinfo.out_color_components = 4;
|
|
2659 if (cinfo.jpeg_color_space is JCS_YCCK) {
|
|
2660 cconvert.color_convert = YCCK_CMYK_CONVERT;
|
|
2661 build_ycc_rgb_table(cinfo);
|
|
2662 } else if (cinfo.jpeg_color_space is JCS_CMYK) {
|
|
2663 cconvert.color_convert = NULL_CONVERT;
|
|
2664 } else
|
|
2665 error();
|
|
2666 // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
2667 break;
|
|
2668
|
|
2669 default:
|
|
2670 /* Permit null conversion to same output space */
|
|
2671 if (cinfo.out_color_space is cinfo.jpeg_color_space) {
|
|
2672 cinfo.out_color_components = cinfo.num_components;
|
|
2673 cconvert.color_convert = NULL_CONVERT;
|
|
2674 } else /* unsupported non-null conversion */
|
|
2675 error();
|
|
2676 // ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
|
|
2677 break;
|
|
2678 }
|
|
2679
|
|
2680 if (cinfo.quantize_colors)
|
|
2681 cinfo.output_components = 1; /* single colormapped output component */
|
|
2682 else
|
|
2683 cinfo.output_components = cinfo.out_color_components;
|
|
2684 }
|
|
2685
|
|
2686 static void jinit_d_post_controller (jpeg_decompress_struct cinfo, bool need_full_buffer) {
|
|
2687 jpeg_d_post_controller post = cinfo.post = new jpeg_d_post_controller();
|
|
2688 // post.pub.start_pass = start_pass_dpost;
|
|
2689 post.whole_image = null; /* flag for no virtual arrays */
|
|
2690 post.buffer = null; /* flag for no strip buffer */
|
|
2691
|
|
2692 /* Create the quantization buffer, if needed */
|
|
2693 if (cinfo.quantize_colors) {
|
|
2694 error(SWT.ERROR_NOT_IMPLEMENTED);
|
|
2695 // /* The buffer strip height is max_v_samp_factor, which is typically
|
|
2696 // * an efficient number of rows for upsampling to return.
|
|
2697 // * (In the presence of output rescaling, we might want to be smarter?)
|
|
2698 // */
|
|
2699 // post.strip_height = cinfo.max_v_samp_factor;
|
|
2700 // if (need_full_buffer) {
|
|
2701 // /* Two-pass color quantization: need full-image storage. */
|
|
2702 // /* We round up the number of rows to a multiple of the strip height. */
|
|
2703 //#ifdef QUANT_2PASS_SUPPORTED
|
|
2704 // post.whole_image = (*cinfo.mem.request_virt_sarray)
|
|
2705 // ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
|
|
2706 // cinfo.output_width * cinfo.out_color_components,
|
|
2707 // (JDIMENSION) jround_up(cast(long) cinfo.output_height,
|
|
2708 // cast(long) post.strip_height),
|
|
2709 // post.strip_height);
|
|
2710 //#else
|
|
2711 // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
|
2712 //#endif /* QUANT_2PASS_SUPPORTED */
|
|
2713 // } else {
|
|
2714 // /* One-pass color quantization: just make a strip buffer. */
|
|
2715 // post.buffer = (*cinfo.mem.alloc_sarray)
|
|
2716 // ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
|
2717 // cinfo.output_width * cinfo.out_color_components,
|
|
2718 // post.strip_height);
|
|
2719 // }
|
|
2720 }
|
|
2721 }
|
|
2722
|
|
2723 static void make_funny_pointers (jpeg_decompress_struct cinfo)
|
|
2724 /* Create the funny pointer lists discussed in the comments above.
|
|
2725 * The actual workspace is already allocated (in main.buffer),
|
|
2726 * and the space for the pointer lists is allocated too.
|
|
2727 * This routine just fills in the curiously ordered lists.
|
|
2728 * This will be repeated at the beginning of each pass.
|
|
2729 */
|
|
2730 {
|
|
2731 jpeg_d_main_controller main = cinfo.main;
|
|
2732 int ci, i, rgroup;
|
|
2733 int M = cinfo.min_DCT_scaled_size;
|
|
2734 jpeg_component_info compptr;
|
|
2735 byte[][] buf, xbuf0, xbuf1;
|
|
2736
|
|
2737 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2738 compptr = cinfo.comp_info[ci];
|
|
2739 rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size) /
|
|
2740 cinfo.min_DCT_scaled_size; /* height of a row group of component */
|
|
2741 xbuf0 = main.xbuffer[0][ci];
|
|
2742 int xbuf0_offset = main.xbuffer_offset[0][ci];
|
|
2743 xbuf1 = main.xbuffer[1][ci];
|
|
2744 int xbuf1_offset = main.xbuffer_offset[1][ci];
|
|
2745 /* First copy the workspace pointers as-is */
|
|
2746 buf = main.buffer[ci];
|
|
2747 for (i = 0; i < rgroup * (M + 2); i++) {
|
|
2748 xbuf0[i + xbuf0_offset] = xbuf1[i + xbuf1_offset] = buf[i];
|
|
2749 }
|
|
2750 /* In the second list, put the last four row groups in swapped order */
|
|
2751 for (i = 0; i < rgroup * 2; i++) {
|
|
2752 xbuf1[rgroup*(M-2) + i + xbuf1_offset] = buf[rgroup*M + i];
|
|
2753 xbuf1[rgroup*M + i + xbuf1_offset] = buf[rgroup*(M-2) + i];
|
|
2754 }
|
|
2755 /* The wraparound pointers at top and bottom will be filled later
|
|
2756 * (see set_wraparound_pointers, below). Initially we want the "above"
|
|
2757 * pointers to duplicate the first actual data line. This only needs
|
|
2758 * to happen in xbuffer[0].
|
|
2759 */
|
|
2760 for (i = 0; i < rgroup; i++) {
|
|
2761 xbuf0[i - rgroup + xbuf0_offset] = xbuf0[0 + xbuf0_offset];
|
|
2762 }
|
|
2763 }
|
|
2764 }
|
|
2765
|
|
2766 static void alloc_funny_pointers (jpeg_decompress_struct cinfo)
|
|
2767 /* Allocate space for the funny pointer lists.
|
|
2768 * This is done only once, not once per pass.
|
|
2769 */
|
|
2770 {
|
|
2771 jpeg_d_main_controller main = cinfo.main;
|
|
2772 int ci, rgroup;
|
|
2773 int M = cinfo.min_DCT_scaled_size;
|
|
2774 jpeg_component_info compptr;
|
|
2775 byte[][] xbuf;
|
|
2776
|
|
2777 /* Get top-level space for component array pointers.
|
|
2778 * We alloc both arrays with one call to save a few cycles.
|
|
2779 */
|
|
2780 main.xbuffer[0] = new byte[][][](cinfo.num_components);
|
|
2781 main.xbuffer[1] = new byte[][][](cinfo.num_components);
|
|
2782 main.xbuffer_offset[0] = new int[](cinfo.num_components);
|
|
2783 main.xbuffer_offset[1] = new int[](cinfo.num_components);
|
|
2784
|
|
2785 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2786 compptr = cinfo.comp_info[ci];
|
|
2787 rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size) / cinfo.min_DCT_scaled_size; /* height of a row group of component */
|
|
2788 /* Get space for pointer lists --- M+4 row groups in each list.
|
|
2789 * We alloc both pointer lists with one call to save a few cycles.
|
|
2790 */
|
|
2791 xbuf = new byte[][](2 * (rgroup * (M + 4)));
|
|
2792 int offset = rgroup;
|
|
2793 main.xbuffer_offset[0][ci] = offset;
|
|
2794 main.xbuffer[0][ci] = xbuf;
|
|
2795 offset += rgroup * (M + 4);
|
|
2796 main.xbuffer_offset[1][ci] = offset;
|
|
2797 main.xbuffer[1][ci] = xbuf;
|
|
2798 }
|
|
2799 }
|
|
2800
|
|
2801
|
|
2802 static void jinit_d_main_controller (jpeg_decompress_struct cinfo, bool need_full_buffer) {
|
|
2803 int ci, rgroup, ngroups;
|
|
2804 jpeg_component_info compptr;
|
|
2805
|
|
2806 jpeg_d_main_controller main = cinfo.main = new jpeg_d_main_controller();
|
|
2807 // main.pub.start_pass = start_pass_main;
|
|
2808
|
|
2809 if (need_full_buffer) /* shouldn't happen */
|
|
2810 error();
|
|
2811 // ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
|
|
2812
|
|
2813 /* Allocate the workspace.
|
|
2814 * ngroups is the number of row groups we need.
|
|
2815 */
|
|
2816 if (cinfo.upsample.need_context_rows) {
|
|
2817 if (cinfo.min_DCT_scaled_size < 2) /* unsupported, see comments above */
|
|
2818 error();
|
|
2819 // ERREXIT(cinfo, JERR_NOTIMPL);
|
|
2820 alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
|
|
2821 ngroups = cinfo.min_DCT_scaled_size + 2;
|
|
2822 } else {
|
|
2823 ngroups = cinfo.min_DCT_scaled_size;
|
|
2824 }
|
|
2825
|
|
2826 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2827 compptr = cinfo.comp_info[ci];
|
|
2828 rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size) / cinfo.min_DCT_scaled_size; /* height of a row group of component */
|
|
2829 main.buffer[ci] = new byte[][]( rgroup * ngroups, compptr.width_in_blocks * compptr.DCT_scaled_size );
|
|
2830 }
|
|
2831 }
|
|
2832
|
|
2833 static long jround_up (long a, long b)
|
|
2834 /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
|
|
2835 /* Assumes a >= 0, b > 0 */
|
|
2836 {
|
|
2837 a += b - 1L;
|
|
2838 return a - (a % b);
|
|
2839 }
|
|
2840
|
|
2841 static void jinit_upsampler (jpeg_decompress_struct cinfo) {
|
|
2842 int ci;
|
|
2843 jpeg_component_info compptr;
|
|
2844 bool need_buffer, do_fancy;
|
|
2845 int h_in_group, v_in_group, h_out_group, v_out_group;
|
|
2846
|
|
2847 jpeg_upsampler upsample = new jpeg_upsampler();
|
|
2848 cinfo.upsample = upsample;
|
|
2849 // upsample.start_pass = start_pass_upsample;
|
|
2850 // upsample.upsample = sep_upsample;
|
|
2851 upsample.need_context_rows = false; /* until we find out differently */
|
|
2852
|
|
2853 if (cinfo.CCIR601_sampling) /* this isn't supported */
|
|
2854 error();
|
|
2855 // ERREXIT(cinfo, JERR_CCIR601_NOTIMPL);
|
|
2856
|
|
2857 /* jdmainct.c doesn't support context rows when min_DCT_scaled_size = 1,
|
|
2858 * so don't ask for it.
|
|
2859 */
|
|
2860 do_fancy = cinfo.do_fancy_upsampling && cinfo.min_DCT_scaled_size > 1;
|
|
2861
|
|
2862 /* Verify we can handle the sampling factors, select per-component methods,
|
|
2863 * and create storage as needed.
|
|
2864 */
|
|
2865 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2866 compptr = cinfo.comp_info[ci];
|
|
2867 /* Compute size of an "input group" after IDCT scaling. This many samples
|
|
2868 * are to be converted to max_h_samp_factor * max_v_samp_factor pixels.
|
|
2869 */
|
|
2870 h_in_group = (compptr.h_samp_factor * compptr.DCT_scaled_size) /
|
|
2871 cinfo.min_DCT_scaled_size;
|
|
2872 v_in_group = (compptr.v_samp_factor * compptr.DCT_scaled_size) /
|
|
2873 cinfo.min_DCT_scaled_size;
|
|
2874 h_out_group = cinfo.max_h_samp_factor;
|
|
2875 v_out_group = cinfo.max_v_samp_factor;
|
|
2876 upsample.rowgroup_height[ci] = v_in_group; /* save for use later */
|
|
2877 need_buffer = true;
|
|
2878 if (! compptr.component_needed) {
|
|
2879 /* Don't bother to upsample an uninteresting component. */
|
|
2880 upsample.methods[ci] = NOOP_UPSAMPLE;
|
|
2881 need_buffer = false;
|
|
2882 } else if (h_in_group is h_out_group && v_in_group is v_out_group) {
|
|
2883 /* Fullsize components can be processed without any work. */
|
|
2884 upsample.methods[ci] = FULLSIZE_UPSAMPLE;
|
|
2885 need_buffer = false;
|
|
2886 } else if (h_in_group * 2 is h_out_group && v_in_group is v_out_group) {
|
|
2887 /* Special cases for 2h1v upsampling */
|
|
2888 if (do_fancy && compptr.downsampled_width > 2)
|
|
2889 upsample.methods[ci] = H2V1_FANCY_UPSAMPLE;
|
|
2890 else
|
|
2891 upsample.methods[ci] = H2V1_UPSAMPLE;
|
|
2892 } else if (h_in_group * 2 is h_out_group && v_in_group * 2 is v_out_group) {
|
|
2893 /* Special cases for 2h2v upsampling */
|
|
2894 if (do_fancy && compptr.downsampled_width > 2) {
|
|
2895 upsample.methods[ci] = H2V2_FANCY_UPSAMPLE;
|
|
2896 upsample.need_context_rows = true;
|
|
2897 } else
|
|
2898 upsample.methods[ci] = H2V2_UPSAMPLE;
|
|
2899 } else if ((h_out_group % h_in_group) is 0 && (v_out_group % v_in_group) is 0) {
|
|
2900 /* Generic integral-factors upsampling method */
|
|
2901 upsample.methods[ci] = INT_UPSAMPLE;
|
|
2902 upsample.h_expand[ci] = cast(byte) (h_out_group / h_in_group);
|
|
2903 upsample.v_expand[ci] = cast(byte) (v_out_group / v_in_group);
|
|
2904 } else
|
|
2905 error();
|
|
2906 // ERREXIT(cinfo, JERR_FRACT_SAMPLE_NOTIMPL);
|
|
2907 if (need_buffer) {
|
|
2908 upsample.color_buf[ci] = new byte[][]( cinfo.max_v_samp_factor,
|
|
2909 cast(int) jround_up(cinfo.output_width, cinfo.max_h_samp_factor));
|
|
2910 }
|
|
2911 }
|
|
2912 }
|
|
2913
|
|
2914 static void jinit_phuff_decoder (jpeg_decompress_struct cinfo) {
|
|
2915 int[][] coef_bit_ptr;
|
|
2916 int ci, i;
|
|
2917
|
|
2918 cinfo.entropy = new phuff_entropy_decoder();
|
|
2919 // entropy.pub.start_pass = start_pass_phuff_decoder;
|
|
2920
|
|
2921 /* Create progression status table */
|
|
2922 cinfo.coef_bits = new int[][]( cinfo.num_components, DCTSIZE2 );
|
|
2923 coef_bit_ptr = cinfo.coef_bits;
|
|
2924 for (ci = 0; ci < cinfo.num_components; ci++)
|
|
2925 for (i = 0; i < DCTSIZE2; i++)
|
|
2926 coef_bit_ptr[ci][i] = -1;
|
|
2927 }
|
|
2928
|
|
2929
|
|
2930 static void jinit_huff_decoder (jpeg_decompress_struct cinfo) {
|
|
2931
|
|
2932 cinfo.entropy = new huff_entropy_decoder();
|
|
2933 // entropy.pub.start_pass = start_pass_huff_decoder;
|
|
2934 // entropy.pub.decode_mcu = decode_mcu;
|
|
2935
|
|
2936 }
|
|
2937
|
|
2938 static void jinit_inverse_dct (jpeg_decompress_struct cinfo) {
|
|
2939 int ci;
|
|
2940 jpeg_component_info compptr;
|
|
2941
|
|
2942 jpeg_inverse_dct idct = cinfo.idct = new jpeg_inverse_dct();
|
|
2943 // idct.pub.start_pass = start_pass;
|
|
2944
|
|
2945 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
2946 compptr = cinfo.comp_info[ci];
|
|
2947 /* Allocate and pre-zero a multiplier table for each component */
|
|
2948 compptr.dct_table = new int[DCTSIZE2];
|
|
2949 /* Mark multiplier table not yet set up for any method */
|
|
2950 idct.cur_method[ci] = -1;
|
|
2951 }
|
|
2952 }
|
|
2953
|
|
2954 static final int CONST_BITS = 13;
|
|
2955 static final int PASS1_BITS = 2;
|
|
2956 static final int RANGE_MASK =(MAXJSAMPLE * 4 + 3);
|
|
2957 static void jpeg_idct_islow (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
2958 short[] coef_block,
|
|
2959 byte[][] output_buf, int output_buf_offset, int output_col)
|
|
2960 {
|
|
2961 int tmp0, tmp1, tmp2, tmp3;
|
|
2962 int tmp10, tmp11, tmp12, tmp13;
|
|
2963 int z1, z2, z3, z4, z5;
|
|
2964 short[] inptr;
|
|
2965 int[] quantptr;
|
|
2966 int[] wsptr;
|
|
2967 byte[] outptr;
|
|
2968 byte[] range_limit = cinfo.sample_range_limit;
|
|
2969 int range_limit_offset = cinfo.sample_range_limit_offset + CENTERJSAMPLE;
|
|
2970 int ctr;
|
|
2971 int[] workspace = cinfo.workspace; /* buffers data between passes */
|
|
2972 // SHIFT_TEMPS
|
|
2973
|
|
2974 /* Pass 1: process columns from input, store into work array. */
|
|
2975 /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
|
|
2976 /* furthermore, we scale the results by 2**PASS1_BITS. */
|
|
2977
|
|
2978 inptr = coef_block;
|
|
2979 quantptr = compptr.dct_table;
|
|
2980 wsptr = workspace;
|
|
2981 int inptr_offset = 0, quantptr_offset = 0, wsptr_offset = 0;
|
|
2982 for (ctr = DCTSIZE; ctr > 0; ctr--) {
|
|
2983 /* Due to quantization, we will usually find that many of the input
|
|
2984 * coefficients are zero, especially the AC terms. We can exploit this
|
|
2985 * by short-circuiting the IDCT calculation for any column in which all
|
|
2986 * the AC terms are zero. In that case each output is equal to the
|
|
2987 * DC coefficient (with scale factor as needed).
|
|
2988 * With typical images and quantization tables, half or more of the
|
|
2989 * column DCT calculations can be simplified this way.
|
|
2990 */
|
|
2991
|
|
2992 if (inptr[DCTSIZE*1+inptr_offset] is 0 && inptr[DCTSIZE*2+inptr_offset] is 0 &&
|
|
2993 inptr[DCTSIZE*3+inptr_offset] is 0 && inptr[DCTSIZE*4+inptr_offset] is 0 &&
|
|
2994 inptr[DCTSIZE*5+inptr_offset] is 0 && inptr[DCTSIZE*6+inptr_offset] is 0 &&
|
|
2995 inptr[DCTSIZE*7+inptr_offset] is 0)
|
|
2996 {
|
|
2997 /* AC terms all zero */
|
|
2998 int dcval = ((inptr[DCTSIZE*0+inptr_offset]) * quantptr[DCTSIZE*0+quantptr_offset]) << PASS1_BITS;
|
|
2999
|
|
3000 wsptr[DCTSIZE*0+wsptr_offset] = dcval;
|
|
3001 wsptr[DCTSIZE*1+wsptr_offset] = dcval;
|
|
3002 wsptr[DCTSIZE*2+wsptr_offset] = dcval;
|
|
3003 wsptr[DCTSIZE*3+wsptr_offset] = dcval;
|
|
3004 wsptr[DCTSIZE*4+wsptr_offset] = dcval;
|
|
3005 wsptr[DCTSIZE*5+wsptr_offset] = dcval;
|
|
3006 wsptr[DCTSIZE*6+wsptr_offset] = dcval;
|
|
3007 wsptr[DCTSIZE*7+wsptr_offset] = dcval;
|
|
3008
|
|
3009 inptr_offset++; /* advance pointers to next column */
|
|
3010 quantptr_offset++;
|
|
3011 wsptr_offset++;
|
|
3012 continue;
|
|
3013 }
|
|
3014
|
|
3015 /* Even part: reverse the even part of the forward DCT. */
|
|
3016 /* The rotator is sqrt(2)*c(-6). */
|
|
3017
|
|
3018 z2 = ((inptr[DCTSIZE*2+inptr_offset]) * quantptr[DCTSIZE*2+quantptr_offset]);
|
|
3019 z3 = ((inptr[DCTSIZE*6+inptr_offset]) * quantptr[DCTSIZE*6+quantptr_offset]);
|
|
3020
|
|
3021 z1 = ((z2 + z3) * 4433/*FIX_0_541196100*/);
|
|
3022 tmp2 = z1 + (z3 * - 15137/*FIX_1_847759065*/);
|
|
3023 tmp3 = z1 + (z2 * 6270/*FIX_0_765366865*/);
|
|
3024
|
|
3025 z2 = ((inptr[DCTSIZE*0+inptr_offset]) * quantptr[DCTSIZE*0+quantptr_offset]);
|
|
3026 z3 = ((inptr[DCTSIZE*4+inptr_offset]) * quantptr[DCTSIZE*4+quantptr_offset]);
|
|
3027
|
|
3028 tmp0 = (z2 + z3) << CONST_BITS;
|
|
3029 tmp1 = (z2 - z3) << CONST_BITS;
|
|
3030
|
|
3031 tmp10 = tmp0 + tmp3;
|
|
3032 tmp13 = tmp0 - tmp3;
|
|
3033 tmp11 = tmp1 + tmp2;
|
|
3034 tmp12 = tmp1 - tmp2;
|
|
3035
|
|
3036 /* Odd part per figure 8; the matrix is unitary and hence its
|
|
3037 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
|
|
3038 */
|
|
3039
|
|
3040 tmp0 = ((inptr[DCTSIZE*7+inptr_offset]) * quantptr[DCTSIZE*7+quantptr_offset]);
|
|
3041 tmp1 = ((inptr[DCTSIZE*5+inptr_offset]) * quantptr[DCTSIZE*5+quantptr_offset]);
|
|
3042 tmp2 = ((inptr[DCTSIZE*3+inptr_offset]) * quantptr[DCTSIZE*3+quantptr_offset]);
|
|
3043 tmp3 = ((inptr[DCTSIZE*1+inptr_offset]) * quantptr[DCTSIZE*1+quantptr_offset]);
|
|
3044
|
|
3045 z1 = tmp0 + tmp3;
|
|
3046 z2 = tmp1 + tmp2;
|
|
3047 z3 = tmp0 + tmp2;
|
|
3048 z4 = tmp1 + tmp3;
|
|
3049 z5 = ((z3 + z4) * 9633/*FIX_1_175875602*/); /* sqrt(2) * c3 */
|
|
3050
|
|
3051 tmp0 = (tmp0 * 2446/*FIX_0_298631336*/); /* sqrt(2) * (-c1+c3+c5-c7) */
|
|
3052 tmp1 = (tmp1 * 16819/*FIX_2_053119869*/); /* sqrt(2) * ( c1+c3-c5+c7) */
|
|
3053 tmp2 = (tmp2 * 25172/*FIX_3_072711026*/); /* sqrt(2) * ( c1+c3+c5-c7) */
|
|
3054 tmp3 = (tmp3 * 12299/*FIX_1_501321110*/); /* sqrt(2) * ( c1+c3-c5-c7) */
|
|
3055 z1 = (z1 * - 7373/*FIX_0_899976223*/); /* sqrt(2) * (c7-c3) */
|
|
3056 z2 = (z2 * - 20995/*FIX_2_562915447*/); /* sqrt(2) * (-c1-c3) */
|
|
3057 z3 = (z3 * - 16069/*FIX_1_961570560*/); /* sqrt(2) * (-c3-c5) */
|
|
3058 z4 = (z4 * - 3196/*FIX_0_390180644*/); /* sqrt(2) * (c5-c3) */
|
|
3059
|
|
3060 z3 += z5;
|
|
3061 z4 += z5;
|
|
3062
|
|
3063 tmp0 += z1 + z3;
|
|
3064 tmp1 += z2 + z4;
|
|
3065 tmp2 += z2 + z3;
|
|
3066 tmp3 += z1 + z4;
|
|
3067
|
|
3068 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
|
|
3069
|
|
3070 // #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
|
|
3071 wsptr[DCTSIZE*0+wsptr_offset] = (((tmp10 + tmp3) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3072 wsptr[DCTSIZE*7+wsptr_offset] = (((tmp10 - tmp3) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3073 wsptr[DCTSIZE*1+wsptr_offset] = (((tmp11 + tmp2) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3074 wsptr[DCTSIZE*6+wsptr_offset] = (((tmp11 - tmp2) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3075 wsptr[DCTSIZE*2+wsptr_offset] = (((tmp12 + tmp1) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3076 wsptr[DCTSIZE*5+wsptr_offset] = (((tmp12 - tmp1) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3077 wsptr[DCTSIZE*3+wsptr_offset] = (((tmp13 + tmp0) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3078 wsptr[DCTSIZE*4+wsptr_offset] = (((tmp13 - tmp0) + (1 << ((CONST_BITS-PASS1_BITS)-1))) >> (CONST_BITS-PASS1_BITS));
|
|
3079
|
|
3080 inptr_offset++; /* advance pointers to next column */
|
|
3081 quantptr_offset++;
|
|
3082 wsptr_offset++;
|
|
3083 }
|
|
3084
|
|
3085
|
|
3086 /* Pass 2: process rows from work array, store into output array. */
|
|
3087 /* Note that we must descale the results by a factor of 8 is 2**3, */
|
|
3088 /* and also undo the PASS1_BITS scaling. */
|
|
3089
|
|
3090 int outptr_offset = 0;
|
|
3091 wsptr = workspace;
|
|
3092 wsptr_offset =0;
|
|
3093 for (ctr = 0; ctr < DCTSIZE; ctr++) {
|
|
3094 outptr = output_buf[ctr+output_buf_offset];
|
|
3095 outptr_offset = output_col;
|
|
3096 /* Rows of zeroes can be exploited in the same way as we did with columns.
|
|
3097 * However, the column calculation has created many nonzero AC terms, so
|
|
3098 * the simplification applies less often (typically 5% to 10% of the time).
|
|
3099 * On machines with very fast multiplication, it's possible that the
|
|
3100 * test takes more time than it's worth. In that case this section
|
|
3101 * may be commented out.
|
|
3102 */
|
|
3103
|
|
3104 //#ifndef NO_ZERO_ROW_TEST
|
|
3105 if (wsptr[1+wsptr_offset] is 0 && wsptr[2+wsptr_offset] is 0 && wsptr[3+wsptr_offset] is 0 && wsptr[4+wsptr_offset] is 0 &&
|
|
3106 wsptr[5+wsptr_offset] is 0 && wsptr[6+wsptr_offset] is 0 && wsptr[7+wsptr_offset] is 0)
|
|
3107 {
|
|
3108 /* AC terms all zero */
|
|
3109 // #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
|
|
3110 byte dcval = range_limit[range_limit_offset + ((((wsptr[0+wsptr_offset]) + (1 << ((PASS1_BITS+3)-1))) >> PASS1_BITS+3)
|
|
3111 & RANGE_MASK)];
|
|
3112
|
|
3113 outptr[0+outptr_offset] = dcval;
|
|
3114 outptr[1+outptr_offset] = dcval;
|
|
3115 outptr[2+outptr_offset] = dcval;
|
|
3116 outptr[3+outptr_offset] = dcval;
|
|
3117 outptr[4+outptr_offset] = dcval;
|
|
3118 outptr[5+outptr_offset] = dcval;
|
|
3119 outptr[6+outptr_offset] = dcval;
|
|
3120 outptr[7+outptr_offset] = dcval;
|
|
3121
|
|
3122 wsptr_offset += DCTSIZE; /* advance pointer to next row */
|
|
3123 continue;
|
|
3124 }
|
|
3125 //#endif
|
|
3126
|
|
3127 /* Even part: reverse the even part of the forward DCT. */
|
|
3128 /* The rotator is sqrt(2)*c(-6). */
|
|
3129
|
|
3130 z2 = wsptr[2+wsptr_offset];
|
|
3131 z3 = wsptr[6+wsptr_offset];
|
|
3132
|
|
3133 z1 = ((z2 + z3) * 4433/*FIX_0_541196100*/);
|
|
3134 tmp2 = z1 + (z3 * - 15137/*FIX_1_847759065*/);
|
|
3135 tmp3 = z1 + (z2 * 6270/*FIX_0_765366865*/);
|
|
3136
|
|
3137 tmp0 = (wsptr[0+wsptr_offset] + wsptr[4+wsptr_offset]) << CONST_BITS;
|
|
3138 tmp1 = (wsptr[0+wsptr_offset] - wsptr[4+wsptr_offset]) << CONST_BITS;
|
|
3139
|
|
3140 tmp10 = tmp0 + tmp3;
|
|
3141 tmp13 = tmp0 - tmp3;
|
|
3142 tmp11 = tmp1 + tmp2;
|
|
3143 tmp12 = tmp1 - tmp2;
|
|
3144
|
|
3145 /* Odd part per figure 8; the matrix is unitary and hence its
|
|
3146 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
|
|
3147 */
|
|
3148
|
|
3149 tmp0 = wsptr[7+wsptr_offset];
|
|
3150 tmp1 = wsptr[5+wsptr_offset];
|
|
3151 tmp2 = wsptr[3+wsptr_offset];
|
|
3152 tmp3 = wsptr[1+wsptr_offset];
|
|
3153
|
|
3154 z1 = tmp0 + tmp3;
|
|
3155 z2 = tmp1 + tmp2;
|
|
3156 z3 = tmp0 + tmp2;
|
|
3157 z4 = tmp1 + tmp3;
|
|
3158 z5 = ((z3 + z4) * 9633/*FIX_1_175875602*/); /* sqrt(2) * c3 */
|
|
3159
|
|
3160 tmp0 = (tmp0 * 2446/*FIX_0_298631336*/); /* sqrt(2) * (-c1+c3+c5-c7) */
|
|
3161 tmp1 = (tmp1 * 16819/*FIX_2_053119869*/); /* sqrt(2) * ( c1+c3-c5+c7) */
|
|
3162 tmp2 = (tmp2 * 25172/*FIX_3_072711026*/); /* sqrt(2) * ( c1+c3+c5-c7) */
|
|
3163 tmp3 = (tmp3 * 12299/*FIX_1_501321110*/); /* sqrt(2) * ( c1+c3-c5-c7) */
|
|
3164 z1 = (z1 * - 7373/*FIX_0_899976223*/); /* sqrt(2) * (c7-c3) */
|
|
3165 z2 = (z2 * - 20995/*FIX_2_562915447*/); /* sqrt(2) * (-c1-c3) */
|
|
3166 z3 = (z3 * - 16069/*FIX_1_961570560*/); /* sqrt(2) * (-c3-c5) */
|
|
3167 z4 = (z4 * - 3196/*FIX_0_390180644*/); /* sqrt(2) * (c5-c3) */
|
|
3168
|
|
3169 z3 += z5;
|
|
3170 z4 += z5;
|
|
3171
|
|
3172 tmp0 += z1 + z3;
|
|
3173 tmp1 += z2 + z4;
|
|
3174 tmp2 += z2 + z3;
|
|
3175 tmp3 += z1 + z4;
|
|
3176
|
|
3177 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
|
|
3178
|
|
3179
|
|
3180 // #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
|
|
3181 outptr[0+outptr_offset] = range_limit[range_limit_offset + ((((tmp10 + tmp3) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3182 CONST_BITS+PASS1_BITS+3)
|
|
3183 & RANGE_MASK)];
|
|
3184 outptr[7+outptr_offset] = range_limit[range_limit_offset + ((((tmp10 - tmp3) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3185 CONST_BITS+PASS1_BITS+3)
|
|
3186 & RANGE_MASK)];
|
|
3187 outptr[1+outptr_offset] = range_limit[range_limit_offset + ((((tmp11 + tmp2) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3188 CONST_BITS+PASS1_BITS+3)
|
|
3189 & RANGE_MASK)];
|
|
3190 outptr[6+outptr_offset] = range_limit[range_limit_offset + ((((tmp11 - tmp2) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3191 CONST_BITS+PASS1_BITS+3)
|
|
3192 & RANGE_MASK)];
|
|
3193 outptr[2+outptr_offset] = range_limit[range_limit_offset + ((((tmp12 + tmp1) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3194 CONST_BITS+PASS1_BITS+3)
|
|
3195 & RANGE_MASK)];
|
|
3196 outptr[5+outptr_offset] = range_limit[range_limit_offset + ((((tmp12 - tmp1) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3197 CONST_BITS+PASS1_BITS+3)
|
|
3198 & RANGE_MASK)];
|
|
3199 outptr[3+outptr_offset] = range_limit[range_limit_offset + ((((tmp13 + tmp0) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3200 CONST_BITS+PASS1_BITS+3)
|
|
3201 & RANGE_MASK)];
|
|
3202 outptr[4+outptr_offset] = range_limit[range_limit_offset + ((((tmp13 - tmp0) + (1 << ((CONST_BITS+PASS1_BITS+3)-1))) >>
|
|
3203 CONST_BITS+PASS1_BITS+3)
|
|
3204 & RANGE_MASK)];
|
|
3205
|
|
3206 wsptr_offset += DCTSIZE; /* advance pointer to next row */
|
|
3207 }
|
|
3208 }
|
|
3209
|
|
3210 static void upsample (jpeg_decompress_struct cinfo,
|
|
3211 byte[][][] input_buf, int[] input_buf_offset, int[] in_row_group_ctr,
|
|
3212 int in_row_groups_avail,
|
|
3213 byte[][] output_buf, int[] out_row_ctr,
|
|
3214 int out_rows_avail)
|
|
3215 {
|
|
3216 sep_upsample(cinfo, input_buf, input_buf_offset, in_row_group_ctr, in_row_groups_avail, output_buf, out_row_ctr, out_rows_avail);
|
|
3217 }
|
|
3218
|
|
3219 static bool smoothing_ok (jpeg_decompress_struct cinfo) {
|
|
3220 jpeg_d_coef_controller coef = cinfo.coef;
|
|
3221 bool smoothing_useful = false;
|
|
3222 int ci, coefi;
|
|
3223 jpeg_component_info compptr;
|
|
3224 JQUANT_TBL qtable;
|
|
3225 int[] coef_bits;
|
|
3226 int[] coef_bits_latch;
|
|
3227
|
|
3228 if (! cinfo.progressive_mode || cinfo.coef_bits is null)
|
|
3229 return false;
|
|
3230
|
|
3231 /* Allocate latch area if not already done */
|
|
3232 if (coef.coef_bits_latch is null)
|
|
3233 coef.coef_bits_latch = new int[cinfo.num_components * SAVED_COEFS];
|
|
3234 coef_bits_latch = coef.coef_bits_latch;
|
|
3235 int coef_bits_latch_offset = 0;
|
|
3236
|
|
3237 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
3238 compptr = cinfo.comp_info[ci];
|
|
3239 /* All components' quantization values must already be latched. */
|
|
3240 if ((qtable = compptr.quant_table) is null)
|
|
3241 return false;
|
|
3242 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
|
|
3243 if (qtable.quantval[0] is 0 ||
|
|
3244 qtable.quantval[Q01_POS] is 0 ||
|
|
3245 qtable.quantval[Q10_POS] is 0 ||
|
|
3246 qtable.quantval[Q20_POS] is 0 ||
|
|
3247 qtable.quantval[Q11_POS] is 0 ||
|
|
3248 qtable.quantval[Q02_POS] is 0)
|
|
3249 return false;
|
|
3250 /* DC values must be at least partly known for all components. */
|
|
3251 coef_bits = cinfo.coef_bits[ci];
|
|
3252 if (coef_bits[0] < 0)
|
|
3253 return false;
|
|
3254 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
|
|
3255 for (coefi = 1; coefi <= 5; coefi++) {
|
|
3256 coef_bits_latch[coefi+coef_bits_latch_offset] = coef_bits[coefi];
|
|
3257 if (coef_bits[coefi] !is 0)
|
|
3258 smoothing_useful = true;
|
|
3259 }
|
|
3260 coef_bits_latch_offset += SAVED_COEFS;
|
|
3261 }
|
|
3262
|
|
3263 return smoothing_useful;
|
|
3264 }
|
|
3265
|
|
3266 static void master_selection (jpeg_decompress_struct cinfo) {
|
|
3267 jpeg_decomp_master master = cinfo.master;
|
|
3268 bool use_c_buffer;
|
|
3269 long samplesperrow;
|
|
3270 int jd_samplesperrow;
|
|
3271
|
|
3272 /* Initialize dimensions and other stuff */
|
|
3273 jpeg_calc_output_dimensions(cinfo);
|
|
3274 prepare_range_limit_table(cinfo);
|
|
3275
|
|
3276 /* Width of an output scanline must be representable as JDIMENSION. */
|
|
3277 samplesperrow = cast(long) cinfo.output_width * cast(long) cinfo.out_color_components;
|
|
3278 jd_samplesperrow = cast(int) samplesperrow;
|
|
3279 if ( jd_samplesperrow !is samplesperrow)
|
|
3280 error();
|
|
3281 // ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
|
|
3282
|
|
3283 /* Initialize my private state */
|
|
3284 master.pass_number = 0;
|
|
3285 master.using_merged_upsample = use_merged_upsample(cinfo);
|
|
3286
|
|
3287 /* Color quantizer selection */
|
|
3288 master.quantizer_1pass = null;
|
|
3289 master.quantizer_2pass = null;
|
|
3290 /* No mode changes if not using buffered-image mode. */
|
|
3291 if (! cinfo.quantize_colors || ! cinfo.buffered_image) {
|
|
3292 cinfo.enable_1pass_quant = false;
|
|
3293 cinfo.enable_external_quant = false;
|
|
3294 cinfo.enable_2pass_quant = false;
|
|
3295 }
|
|
3296 if (cinfo.quantize_colors) {
|
|
3297 error(SWT.ERROR_NOT_IMPLEMENTED);
|
|
3298 // if (cinfo.raw_data_out)
|
|
3299 // ERREXIT(cinfo, JERR_NOTIMPL);
|
|
3300 // /* 2-pass quantizer only works in 3-component color space. */
|
|
3301 // if (cinfo.out_color_components !is 3) {
|
|
3302 // cinfo.enable_1pass_quant = true;
|
|
3303 // cinfo.enable_external_quant = false;
|
|
3304 // cinfo.enable_2pass_quant = false;
|
|
3305 // cinfo.colormap = null;
|
|
3306 // } else if (cinfo.colormap !is null) {
|
|
3307 // cinfo.enable_external_quant = true;
|
|
3308 // } else if (cinfo.two_pass_quantize) {
|
|
3309 // cinfo.enable_2pass_quant = true;
|
|
3310 // } else {
|
|
3311 // cinfo.enable_1pass_quant = true;
|
|
3312 // }
|
|
3313 //
|
|
3314 // if (cinfo.enable_1pass_quant) {
|
|
3315 //#ifdef QUANT_1PASS_SUPPORTED
|
|
3316 // jinit_1pass_quantizer(cinfo);
|
|
3317 // master.quantizer_1pass = cinfo.cquantize;
|
|
3318 //#else
|
|
3319 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
3320 //#endif
|
|
3321 // }
|
|
3322 //
|
|
3323 // /* We use the 2-pass code to map to external colormaps. */
|
|
3324 // if (cinfo.enable_2pass_quant || cinfo.enable_external_quant) {
|
|
3325 //#ifdef QUANT_2PASS_SUPPORTED
|
|
3326 // jinit_2pass_quantizer(cinfo);
|
|
3327 // master.quantizer_2pass = cinfo.cquantize;
|
|
3328 //#else
|
|
3329 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
3330 //#endif
|
|
3331 // }
|
|
3332 // /* If both quantizers are initialized, the 2-pass one is left active;
|
|
3333 // * this is necessary for starting with quantization to an external map.
|
|
3334 // */
|
|
3335 }
|
|
3336
|
|
3337 /* Post-processing: in particular, color conversion first */
|
|
3338 if (! cinfo.raw_data_out) {
|
|
3339 if (master.using_merged_upsample) {
|
|
3340 //#ifdef UPSAMPLE_MERGING_SUPPORTED
|
|
3341 // jinit_merged_upsampler(cinfo); /* does color conversion too */
|
|
3342 //#else
|
|
3343 error();
|
|
3344 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
3345 //#endif
|
|
3346 } else {
|
|
3347 jinit_color_deconverter(cinfo);
|
|
3348 jinit_upsampler(cinfo);
|
|
3349 }
|
|
3350 jinit_d_post_controller(cinfo, cinfo.enable_2pass_quant);
|
|
3351 }
|
|
3352 /* Inverse DCT */
|
|
3353 jinit_inverse_dct(cinfo);
|
|
3354 /* Entropy decoding: either Huffman or arithmetic coding. */
|
|
3355 if (cinfo.arith_code) {
|
|
3356 error();
|
|
3357 // ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
|
|
3358 } else {
|
|
3359 if (cinfo.progressive_mode) {
|
|
3360 //#ifdef D_PROGRESSIVE_SUPPORTED
|
|
3361 jinit_phuff_decoder(cinfo);
|
|
3362 //#else
|
|
3363 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
3364 //#endif
|
|
3365 } else
|
|
3366 jinit_huff_decoder(cinfo);
|
|
3367 }
|
|
3368
|
|
3369 /* Initialize principal buffer controllers. */
|
|
3370 use_c_buffer = cinfo.inputctl.has_multiple_scans || cinfo.buffered_image;
|
|
3371 jinit_d_coef_controller(cinfo, use_c_buffer);
|
|
3372
|
|
3373 if (! cinfo.raw_data_out)
|
|
3374 jinit_d_main_controller(cinfo, false /* never need full buffer here */);
|
|
3375
|
|
3376 /* Initialize input side of decompressor to consume first scan. */
|
|
3377 start_input_pass (cinfo);
|
|
3378
|
|
3379 //#ifdef D_MULTISCAN_FILES_SUPPORTED
|
|
3380 /* If jpeg_start_decompress will read the whole file, initialize
|
|
3381 * progress monitoring appropriately. The input step is counted
|
|
3382 * as one pass.
|
|
3383 */
|
|
3384 // if (cinfo.progress !is null && ! cinfo.buffered_image &&
|
|
3385 // cinfo.inputctl.has_multiple_scans) {
|
|
3386 // int nscans;
|
|
3387 // /* Estimate number of scans to set pass_limit. */
|
|
3388 // if (cinfo.progressive_mode) {
|
|
3389 // /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
|
|
3390 // nscans = 2 + 3 * cinfo.num_components;
|
|
3391 // } else {
|
|
3392 // /* For a nonprogressive multiscan file, estimate 1 scan per component. */
|
|
3393 // nscans = cinfo.num_components;
|
|
3394 // }
|
|
3395 // cinfo.progress.pass_counter = 0L;
|
|
3396 // cinfo.progress.pass_limit = cast(long) cinfo.total_iMCU_rows * nscans;
|
|
3397 // cinfo.progress.completed_passes = 0;
|
|
3398 // cinfo.progress.total_passes = (cinfo.enable_2pass_quant ? 3 : 2);
|
|
3399 // /* Count the input pass as done */
|
|
3400 // master.pass_number++;
|
|
3401 // }
|
|
3402 //#endif /* D_MULTISCAN_FILES_SUPPORTED */
|
|
3403 }
|
|
3404
|
|
3405 static void jinit_master_decompress (jpeg_decompress_struct cinfo) {
|
|
3406 jpeg_decomp_master master = new jpeg_decomp_master();
|
|
3407 cinfo.master = master;
|
|
3408 // master.prepare_for_output_pass = prepare_for_output_pass;
|
|
3409 // master.finish_output_pass = finish_output_pass;
|
|
3410
|
|
3411 master.is_dummy_pass = false;
|
|
3412
|
|
3413 master_selection(cinfo);
|
|
3414 }
|
|
3415
|
|
3416 static void
|
|
3417 jcopy_sample_rows (byte[][] input_array, int source_row,
|
|
3418 byte[][] output_array, int dest_row,
|
|
3419 int num_rows, int num_cols)
|
|
3420 /* Copy some rows of samples from one place to another.
|
|
3421 * num_rows rows are copied from input_array[source_row++]
|
|
3422 * to output_array[dest_row++]; these areas may overlap for duplication.
|
|
3423 * The source and destination arrays must be at least as wide as num_cols.
|
|
3424 */
|
|
3425 {
|
|
3426 byte[] inptr, outptr;
|
|
3427 int count = num_cols;
|
|
3428 int row;
|
|
3429
|
|
3430 int input_array_offset = source_row;
|
|
3431 int output_array_offset = dest_row;
|
|
3432
|
|
3433 for (row = num_rows; row > 0; row--) {
|
|
3434 inptr = input_array[input_array_offset++];
|
|
3435 outptr = output_array[output_array_offset++];
|
|
3436 System.arraycopy(inptr, 0, outptr, 0, count);
|
|
3437 }
|
|
3438 }
|
|
3439
|
|
3440 static bool jpeg_start_decompress (jpeg_decompress_struct cinfo) {
|
|
3441 if (cinfo.global_state is DSTATE_READY) {
|
|
3442 /* First call: initialize master control, select active modules */
|
|
3443 jinit_master_decompress(cinfo);
|
|
3444 if (cinfo.buffered_image) {
|
|
3445 /* No more work here; expecting jpeg_start_output next */
|
|
3446 cinfo.global_state = DSTATE_BUFIMAGE;
|
|
3447 return true;
|
|
3448 }
|
|
3449 cinfo.global_state = DSTATE_PRELOAD;
|
|
3450 }
|
|
3451 if (cinfo.global_state is DSTATE_PRELOAD) {
|
|
3452 /* If file has multiple scans, absorb them all into the coef buffer */
|
|
3453 if (cinfo.inputctl.has_multiple_scans) {
|
|
3454 //#ifdef D_MULTISCAN_FILES_SUPPORTED
|
|
3455 for (;;) {
|
|
3456 int retcode;
|
|
3457 /* Call progress monitor hook if present */
|
|
3458 // if (cinfo.progress !is null)
|
|
3459 // (*cinfo.progress.progress_monitor) ((j_common_ptr) cinfo);
|
|
3460 /* Absorb some more input */
|
|
3461 retcode = consume_input (cinfo);
|
|
3462 if (retcode is JPEG_SUSPENDED)
|
|
3463 return false;
|
|
3464 if (retcode is JPEG_REACHED_EOI)
|
|
3465 break;
|
|
3466 /* Advance progress counter if appropriate */
|
|
3467 // if (cinfo.progress !is null && (retcode is JPEG_ROW_COMPLETED || retcode is JPEG_REACHED_SOS)) {
|
|
3468 // if (++cinfo.progress.pass_counter >= cinfo.progress.pass_limit) {
|
|
3469 // /* jdmaster underestimated number of scans; ratchet up one scan */
|
|
3470 // cinfo.progress.pass_limit += cast(long) cinfo.total_iMCU_rows;
|
|
3471 // }
|
|
3472 // }
|
|
3473 }
|
|
3474 //#else
|
|
3475 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
3476 //#endif /* D_MULTISCAN_FILES_SUPPORTED */
|
|
3477 }
|
|
3478 cinfo.output_scan_number = cinfo.input_scan_number;
|
|
3479 } else if (cinfo.global_state !is DSTATE_PRESCAN)
|
|
3480 error();
|
|
3481 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
3482 /* Perform any dummy output passes, and set up for the final pass */
|
|
3483 return output_pass_setup(cinfo);
|
|
3484 }
|
|
3485
|
|
3486 static void prepare_for_output_pass (jpeg_decompress_struct cinfo) {
|
|
3487 jpeg_decomp_master master = cinfo.master;
|
|
3488
|
|
3489 if (master.is_dummy_pass) {
|
|
3490 //#ifdef QUANT_2PASS_SUPPORTED
|
|
3491 // /* Final pass of 2-pass quantization */
|
|
3492 // master.pub.is_dummy_pass = FALSE;
|
|
3493 // (*cinfo.cquantize.start_pass) (cinfo, FALSE);
|
|
3494 // (*cinfo.post.start_pass) (cinfo, JBUF_CRANK_DEST);
|
|
3495 // (*cinfo.main.start_pass) (cinfo, JBUF_CRANK_DEST);
|
|
3496 //#else
|
|
3497 error(SWT.ERROR_NOT_IMPLEMENTED);
|
|
3498 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
3499 //#endif /* QUANT_2PASS_SUPPORTED */
|
|
3500 } else {
|
|
3501 if (cinfo.quantize_colors && cinfo.colormap is null) {
|
|
3502 /* Select new quantization method */
|
|
3503 if (cinfo.two_pass_quantize && cinfo.enable_2pass_quant) {
|
|
3504 cinfo.cquantize = master.quantizer_2pass;
|
|
3505 master.is_dummy_pass = true;
|
|
3506 } else if (cinfo.enable_1pass_quant) {
|
|
3507 cinfo.cquantize = master.quantizer_1pass;
|
|
3508 } else {
|
|
3509 error();
|
|
3510 // ERREXIT(cinfo, JERR_MODE_CHANGE);
|
|
3511 }
|
|
3512 }
|
|
3513 cinfo.idct.start_pass (cinfo);
|
|
3514 start_output_pass (cinfo);
|
|
3515 if (! cinfo.raw_data_out) {
|
|
3516 if (! master.using_merged_upsample)
|
|
3517 cinfo.cconvert.start_pass (cinfo);
|
|
3518 cinfo.upsample.start_pass (cinfo);
|
|
3519 if (cinfo.quantize_colors)
|
|
3520 cinfo.cquantize.start_pass (cinfo, master.is_dummy_pass);
|
|
3521 cinfo.post.start_pass (cinfo, (master.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
|
|
3522 cinfo.main.start_pass (cinfo, JBUF_PASS_THRU);
|
|
3523 }
|
|
3524 }
|
|
3525
|
|
3526 // /* Set up progress monitor's pass info if present */
|
|
3527 // if (cinfo.progress !is NULL) {
|
|
3528 // cinfo.progress.completed_passes = master.pass_number;
|
|
3529 // cinfo.progress.total_passes = master.pass_number +
|
|
3530 // (master.pub.is_dummy_pass ? 2 : 1);
|
|
3531 // /* In buffered-image mode, we assume one more output pass if EOI not
|
|
3532 // * yet reached, but no more passes if EOI has been reached.
|
|
3533 // */
|
|
3534 // if (cinfo.buffered_image && ! cinfo.inputctl.eoi_reached) {
|
|
3535 // cinfo.progress.total_passes += (cinfo.enable_2pass_quant ? 2 : 1);
|
|
3536 // }
|
|
3537 // }
|
|
3538 }
|
|
3539
|
|
3540
|
|
3541 static bool jpeg_resync_to_restart (jpeg_decompress_struct cinfo, int desired) {
|
|
3542 int marker = cinfo.unread_marker;
|
|
3543 int action = 1;
|
|
3544
|
|
3545 /* Always put up a warning. */
|
|
3546 // WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
|
|
3547
|
|
3548 /* Outer loop handles repeated decision after scanning forward. */
|
|
3549 for (;;) {
|
|
3550 if (marker < M_SOF0)
|
|
3551 action = 2; /* invalid marker */
|
|
3552 else if (marker < M_RST0 || marker > M_RST7)
|
|
3553 action = 3; /* valid non-restart marker */
|
|
3554 else {
|
|
3555 if (marker is (M_RST0 + ((desired+1) & 7)) || marker is ( M_RST0 + ((desired+2) & 7)))
|
|
3556 action = 3; /* one of the next two expected restarts */
|
|
3557 else if (marker is (M_RST0 + ((desired-1) & 7)) || marker is ( M_RST0 + ((desired-2) & 7)))
|
|
3558 action = 2; /* a prior restart, so advance */
|
|
3559 else
|
|
3560 action = 1; /* desired restart or too far away */
|
|
3561 }
|
|
3562 // TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
|
|
3563 switch (action) {
|
|
3564 case 1:
|
|
3565 /* Discard marker and let entropy decoder resume processing. */
|
|
3566 cinfo.unread_marker = 0;
|
|
3567 return true;
|
|
3568 case 2:
|
|
3569 /* Scan to the next marker, and repeat the decision loop. */
|
|
3570 if (! next_marker(cinfo))
|
|
3571 return false;
|
|
3572 marker = cinfo.unread_marker;
|
|
3573 break;
|
|
3574 case 3:
|
|
3575 /* Return without advancing past this marker. */
|
|
3576 /* Entropy decoder will be forced to process an empty segment. */
|
|
3577 return true;
|
|
3578 default:
|
|
3579 }
|
|
3580 } /* end loop */
|
|
3581 }
|
|
3582
|
|
3583 static bool read_restart_marker (jpeg_decompress_struct cinfo) {
|
|
3584 /* Obtain a marker unless we already did. */
|
|
3585 /* Note that next_marker will complain if it skips any data. */
|
|
3586 if (cinfo.unread_marker is 0) {
|
|
3587 if (! next_marker(cinfo))
|
|
3588 return false;
|
|
3589 }
|
|
3590
|
|
3591 if (cinfo.unread_marker is (M_RST0 + cinfo.marker.next_restart_num)) {
|
|
3592 /* Normal case --- swallow the marker and let entropy decoder continue */
|
|
3593 // TRACEMS1(cinfo, 3, JTRC_RST, cinfo.marker.next_restart_num);
|
|
3594 cinfo.unread_marker = 0;
|
|
3595 } else {
|
|
3596 /* Uh-oh, the restart markers have been messed up. */
|
|
3597 /* Let the data source manager determine how to resync. */
|
|
3598 if (! jpeg_resync_to_restart (cinfo, cinfo.marker.next_restart_num))
|
|
3599 return false;
|
|
3600 }
|
|
3601
|
|
3602 /* Update next-restart state */
|
|
3603 cinfo.marker.next_restart_num = (cinfo.marker.next_restart_num + 1) & 7;
|
|
3604
|
|
3605 return true;
|
|
3606 }
|
|
3607
|
|
3608 static bool jpeg_fill_bit_buffer (bitread_working_state state, int get_buffer, int bits_left, int nbits)
|
|
3609 /* Load up the bit buffer to a depth of at least nbits */
|
|
3610 {
|
|
3611 /* Copy heavily used state fields into locals (hopefully registers) */
|
|
3612 byte[] buffer = state.buffer;
|
|
3613 int bytes_in_buffer = state.bytes_in_buffer;
|
|
3614 int bytes_offset = state.bytes_offset;
|
|
3615 jpeg_decompress_struct cinfo = state.cinfo;
|
|
3616
|
|
3617 /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
|
|
3618 /* (It is assumed that no request will be for more than that many bits.) */
|
|
3619 /* We fail to do so only if we hit a marker or are forced to suspend. */
|
|
3620
|
|
3621 if (cinfo.unread_marker is 0) { /* cannot advance past a marker */
|
|
3622 while (bits_left < MIN_GET_BITS) {
|
|
3623 int c;
|
|
3624
|
|
3625 /* Attempt to read a byte */
|
|
3626 if (bytes_offset is bytes_in_buffer) {
|
|
3627 if (! fill_input_buffer (cinfo))
|
|
3628 return false;
|
|
3629 buffer = cinfo.buffer;
|
|
3630 bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
3631 bytes_offset = cinfo.bytes_offset;
|
|
3632 }
|
|
3633 c = buffer[bytes_offset++] & 0xFF;
|
|
3634
|
|
3635 /* If it's 0xFF, check and discard stuffed zero byte */
|
|
3636 if (c is 0xFF) {
|
|
3637 /* Loop here to discard any padding FF's on terminating marker,
|
|
3638 * so that we can save a valid unread_marker value. NOTE: we will
|
|
3639 * accept multiple FF's followed by a 0 as meaning a single FF data
|
|
3640 * byte. This data pattern is not valid according to the standard.
|
|
3641 */
|
|
3642 do {
|
|
3643 if (bytes_offset is bytes_in_buffer) {
|
|
3644 if (! fill_input_buffer (cinfo))
|
|
3645 return false;
|
|
3646 buffer = cinfo.buffer;
|
|
3647 bytes_in_buffer = cinfo.bytes_in_buffer;
|
|
3648 bytes_offset = cinfo.bytes_offset;
|
|
3649 }
|
|
3650 c = buffer[bytes_offset++] & 0xFF;
|
|
3651 } while (c is 0xFF);
|
|
3652
|
|
3653 if (c is 0) {
|
|
3654 /* Found FF/00, which represents an FF data byte */
|
|
3655 c = 0xFF;
|
|
3656 } else {
|
|
3657 /* Oops, it's actually a marker indicating end of compressed data.
|
|
3658 * Save the marker code for later use.
|
|
3659 * Fine point: it might appear that we should save the marker into
|
|
3660 * bitread working state, not straight into permanent state. But
|
|
3661 * once we have hit a marker, we cannot need to suspend within the
|
|
3662 * current MCU, because we will read no more bytes from the data
|
|
3663 * source. So it is OK to update permanent state right away.
|
|
3664 */
|
|
3665 cinfo.unread_marker = c;
|
|
3666 /* See if we need to insert some fake zero bits. */
|
|
3667 // goto no_more_bytes;
|
|
3668 if (nbits > bits_left) {
|
|
3669 /* Uh-oh. Report corrupted data to user and stuff zeroes into
|
|
3670 * the data stream, so that we can produce some kind of image.
|
|
3671 * We use a nonvolatile flag to ensure that only one warning message
|
|
3672 * appears per data segment.
|
|
3673 */
|
|
3674 if (! cinfo.entropy.insufficient_data) {
|
|
3675 // WARNMS(cinfo, JWRN_HIT_MARKER);
|
|
3676 cinfo.entropy.insufficient_data = true;
|
|
3677 }
|
|
3678 /* Fill the buffer with zero bits */
|
|
3679 get_buffer <<= MIN_GET_BITS - bits_left;
|
|
3680 bits_left = MIN_GET_BITS;
|
|
3681 }
|
|
3682
|
|
3683 /* Unload the local registers */
|
|
3684 state.buffer = buffer;
|
|
3685 state.bytes_in_buffer = bytes_in_buffer;
|
|
3686 state.bytes_offset = bytes_offset;
|
|
3687 state.get_buffer = get_buffer;
|
|
3688 state.bits_left = bits_left;
|
|
3689
|
|
3690 return true;
|
|
3691
|
|
3692 }
|
|
3693 }
|
|
3694
|
|
3695 /* OK, load c into get_buffer */
|
|
3696 get_buffer = (get_buffer << 8) | c;
|
|
3697 bits_left += 8;
|
|
3698 } /* end while */
|
|
3699 } else {
|
|
3700 // no_more_bytes:
|
|
3701 /* We get here if we've read the marker that terminates the compressed
|
|
3702 * data segment. There should be enough bits in the buffer register
|
|
3703 * to satisfy the request; if so, no problem.
|
|
3704 */
|
|
3705 if (nbits > bits_left) {
|
|
3706 /* Uh-oh. Report corrupted data to user and stuff zeroes into
|
|
3707 * the data stream, so that we can produce some kind of image.
|
|
3708 * We use a nonvolatile flag to ensure that only one warning message
|
|
3709 * appears per data segment.
|
|
3710 */
|
|
3711 if (! cinfo.entropy.insufficient_data) {
|
|
3712 // WARNMS(cinfo, JWRN_HIT_MARKER);
|
|
3713 cinfo.entropy.insufficient_data = true;
|
|
3714 }
|
|
3715 /* Fill the buffer with zero bits */
|
|
3716 get_buffer <<= MIN_GET_BITS - bits_left;
|
|
3717 bits_left = MIN_GET_BITS;
|
|
3718 }
|
|
3719 }
|
|
3720
|
|
3721 /* Unload the local registers */
|
|
3722 state.buffer = buffer;
|
|
3723 state.bytes_in_buffer = bytes_in_buffer;
|
|
3724 state.bytes_offset = bytes_offset;
|
|
3725 state.get_buffer = get_buffer;
|
|
3726 state.bits_left = bits_left;
|
|
3727
|
|
3728 return true;
|
|
3729 }
|
|
3730
|
|
3731 static int jpeg_huff_decode (bitread_working_state state, int get_buffer, int bits_left, d_derived_tbl htbl, int min_bits) {
|
|
3732 int l = min_bits;
|
|
3733 int code;
|
|
3734
|
|
3735 /* HUFF_DECODE has determined that the code is at least min_bits */
|
|
3736 /* bits long, so fetch that many bits in one swoop. */
|
|
3737
|
|
3738 // CHECK_BIT_BUFFER(*state, l, return -1);
|
|
3739 {
|
|
3740 if (bits_left < (l)) {
|
|
3741 if (! jpeg_fill_bit_buffer(state,get_buffer,bits_left,l)) {
|
|
3742 return -1;
|
|
3743 }
|
|
3744 get_buffer = state.get_buffer; bits_left = state.bits_left;
|
|
3745 }
|
|
3746 }
|
|
3747 // code = GET_BITS(l);
|
|
3748 code = (( (get_buffer >> (bits_left -= (l)))) & ((1<<(l))-1));
|
|
3749
|
|
3750 /* Collect the rest of the Huffman code one bit at a time. */
|
|
3751 /* This is per Figure F.16 in the JPEG spec. */
|
|
3752
|
|
3753 while (code > htbl.maxcode[l]) {
|
|
3754 code <<= 1;
|
|
3755 // CHECK_BIT_BUFFER(*state, 1, return -1);
|
|
3756 {
|
|
3757 if (bits_left < (1)) {
|
|
3758 if (! jpeg_fill_bit_buffer(state,get_buffer,bits_left,1)) {
|
|
3759 return -1;
|
|
3760 }
|
|
3761 get_buffer = state.get_buffer; bits_left = state.bits_left;
|
|
3762 }
|
|
3763 }
|
|
3764 // code |= GET_BITS(1);
|
|
3765 code |= (( (get_buffer >> (bits_left -= (1)))) & ((1<<(1))-1));
|
|
3766 l++;
|
|
3767 }
|
|
3768
|
|
3769 /* Unload the local registers */
|
|
3770 state.get_buffer = get_buffer;
|
|
3771 state.bits_left = bits_left;
|
|
3772
|
|
3773 /* With garbage input we may reach the sentinel value l = 17. */
|
|
3774
|
|
3775 if (l > 16) {
|
|
3776 // WARNMS(state.cinfo, JWRN_HUFF_BAD_CODE);
|
|
3777 return 0; /* fake a zero as the safest result */
|
|
3778 }
|
|
3779
|
|
3780 return htbl.pub.huffval[ (code + htbl.valoffset[l]) ] & 0xFF;
|
|
3781 }
|
|
3782
|
|
3783 static int decompress_onepass (jpeg_decompress_struct cinfo, byte[][][] output_buf, int[] output_buf_offset) {
|
|
3784 jpeg_d_coef_controller coef = cinfo.coef;
|
|
3785 int MCU_col_num; /* index of current MCU within row */
|
|
3786 int last_MCU_col = cinfo.MCUs_per_row - 1;
|
|
3787 int last_iMCU_row = cinfo.total_iMCU_rows - 1;
|
|
3788 int blkn, ci, xindex, yindex, yoffset, useful_width;
|
|
3789 byte[][] output_ptr;
|
|
3790 int start_col, output_col;
|
|
3791 jpeg_component_info compptr;
|
|
3792 // inverse_DCT_method_ptr inverse_DCT;
|
|
3793
|
|
3794 /* Loop to process as much as one whole iMCU row */
|
|
3795 for (yoffset = coef.MCU_vert_offset; yoffset < coef.MCU_rows_per_iMCU_row; yoffset++) {
|
|
3796 for (MCU_col_num = coef.MCU_ctr; MCU_col_num <= last_MCU_col; MCU_col_num++) {
|
|
3797 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
|
|
3798 for (int i = 0; i < cinfo.blocks_in_MCU; i++) {
|
|
3799 short[] blk = coef.MCU_buffer[i];
|
|
3800 for (int j = 0; j < blk.length; j++) {
|
|
3801 blk[j] = 0;
|
|
3802 }
|
|
3803 }
|
|
3804 if (! cinfo.entropy.decode_mcu (cinfo, coef.MCU_buffer)) {
|
|
3805 /* Suspension forced; update state counters and exit */
|
|
3806 coef.MCU_vert_offset = yoffset;
|
|
3807 coef.MCU_ctr = MCU_col_num;
|
|
3808 return JPEG_SUSPENDED;
|
|
3809 }
|
|
3810 /* Determine where data should go in output_buf and do the IDCT thing.
|
|
3811 * We skip dummy blocks at the right and bottom edges (but blkn gets
|
|
3812 * incremented past them!). Note the inner loop relies on having
|
|
3813 * allocated the MCU_buffer[] blocks sequentially.
|
|
3814 */
|
|
3815 blkn = 0; /* index of current DCT block within MCU */
|
|
3816 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
3817 compptr = cinfo.cur_comp_info[ci];
|
|
3818 /* Don't bother to IDCT an uninteresting component. */
|
|
3819 if (! compptr.component_needed) {
|
|
3820 blkn += compptr.MCU_blocks;
|
|
3821 continue;
|
|
3822 }
|
|
3823 // inverse_DCT = cinfo.idct.inverse_DCT[compptr.component_index];
|
|
3824 useful_width = (MCU_col_num < last_MCU_col) ? compptr.MCU_width : compptr.last_col_width;
|
|
3825 output_ptr = output_buf[compptr.component_index];
|
|
3826 int output_ptr_offset = output_buf_offset[compptr.component_index] + yoffset * compptr.DCT_scaled_size;
|
|
3827 start_col = MCU_col_num * compptr.MCU_sample_width;
|
|
3828 for (yindex = 0; yindex < compptr.MCU_height; yindex++) {
|
|
3829 if (cinfo.input_iMCU_row < last_iMCU_row || yoffset+yindex < compptr.last_row_height) {
|
|
3830 output_col = start_col;
|
|
3831 for (xindex = 0; xindex < useful_width; xindex++) {
|
|
3832 jpeg_idct_islow(cinfo, compptr, coef.MCU_buffer[blkn+xindex], output_ptr, output_ptr_offset, output_col);
|
|
3833 output_col += compptr.DCT_scaled_size;
|
|
3834 }
|
|
3835 }
|
|
3836 blkn += compptr.MCU_width;
|
|
3837 output_ptr_offset += compptr.DCT_scaled_size;
|
|
3838 }
|
|
3839 }
|
|
3840 }
|
|
3841 /* Completed an MCU row, but perhaps not an iMCU row */
|
|
3842 coef.MCU_ctr = 0;
|
|
3843 }
|
|
3844 /* Completed the iMCU row, advance counters for next one */
|
|
3845 cinfo.output_iMCU_row++;
|
|
3846 if (++(cinfo.input_iMCU_row) < cinfo.total_iMCU_rows) {
|
|
3847 coef.start_iMCU_row(cinfo);
|
|
3848 return JPEG_ROW_COMPLETED;
|
|
3849 }
|
|
3850 /* Completed the scan */
|
|
3851 finish_input_pass (cinfo);
|
|
3852 return JPEG_SCAN_COMPLETED;
|
|
3853 }
|
|
3854
|
|
3855 static int decompress_smooth_data (jpeg_decompress_struct cinfo, byte[][][] output_buf, int[] output_buf_offset) {
|
|
3856 jpeg_d_coef_controller coef = cinfo.coef;
|
|
3857 int last_iMCU_row = cinfo.total_iMCU_rows - 1;
|
|
3858 int block_num, last_block_column;
|
|
3859 int ci, block_row, block_rows, access_rows;
|
|
3860 short[][][] buffer;
|
|
3861 short[][] buffer_ptr, prev_block_row, next_block_row;
|
|
3862 byte[][] output_ptr;
|
|
3863 int output_col;
|
|
3864 jpeg_component_info compptr;
|
|
3865 // inverse_DCT_method_ptr inverse_DCT;
|
|
3866 bool first_row, last_row;
|
|
3867 short[] workspace = coef.workspace;
|
|
3868 if (workspace is null) workspace = coef.workspace = new short[DCTSIZE2];
|
|
3869 int[] coef_bits;
|
|
3870 JQUANT_TBL quanttbl;
|
|
3871 int Q00,Q01,Q02,Q10,Q11,Q20, num;
|
|
3872 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
|
|
3873 int Al, pred;
|
|
3874
|
|
3875 /* Force some input to be done if we are getting ahead of the input. */
|
|
3876 while (cinfo.input_scan_number <= cinfo.output_scan_number && ! cinfo.inputctl.eoi_reached) {
|
|
3877 if (cinfo.input_scan_number is cinfo.output_scan_number) {
|
|
3878 /* If input is working on current scan, we ordinarily want it to
|
|
3879 * have completed the current row. But if input scan is DC,
|
|
3880 * we want it to keep one row ahead so that next block row's DC
|
|
3881 * values are up to date.
|
|
3882 */
|
|
3883 int delta = (cinfo.Ss is 0) ? 1 : 0;
|
|
3884 if (cinfo.input_iMCU_row > cinfo.output_iMCU_row+delta)
|
|
3885 break;
|
|
3886 }
|
|
3887 if (consume_input(cinfo) is JPEG_SUSPENDED)
|
|
3888 return JPEG_SUSPENDED;
|
|
3889 }
|
|
3890
|
|
3891 /* OK, output from the virtual arrays. */
|
|
3892 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
3893 compptr = cinfo.comp_info[ci];
|
|
3894 /* Don't bother to IDCT an uninteresting component. */
|
|
3895 if (! compptr.component_needed)
|
|
3896 continue;
|
|
3897 /* Count non-dummy DCT block rows in this iMCU row. */
|
|
3898 if (cinfo.output_iMCU_row < last_iMCU_row) {
|
|
3899 block_rows = compptr.v_samp_factor;
|
|
3900 access_rows = block_rows * 2; /* this and next iMCU row */
|
|
3901 last_row = false;
|
|
3902 } else {
|
|
3903 /* NB: can't use last_row_height here; it is input-side-dependent! */
|
|
3904 block_rows = (compptr.height_in_blocks % compptr.v_samp_factor);
|
|
3905 if (block_rows is 0) block_rows = compptr.v_samp_factor;
|
|
3906 access_rows = block_rows; /* this iMCU row only */
|
|
3907 last_row = true;
|
|
3908 }
|
|
3909 /* Align the virtual buffer for this component. */
|
|
3910 int buffer_offset;
|
|
3911 if (cinfo.output_iMCU_row > 0) {
|
|
3912 access_rows += compptr.v_samp_factor; /* prior iMCU row too */
|
|
3913 buffer = coef.whole_image[ci];
|
|
3914 buffer_offset = (cinfo.output_iMCU_row - 1) * compptr.v_samp_factor;
|
|
3915 buffer_offset += compptr.v_samp_factor; /* point to current iMCU row */
|
|
3916 first_row = false;
|
|
3917 } else {
|
|
3918 buffer = coef.whole_image[ci];
|
|
3919 buffer_offset = 0;
|
|
3920 first_row = true;
|
|
3921 }
|
|
3922 /* Fetch component-dependent info */
|
|
3923 coef_bits = coef.coef_bits_latch;
|
|
3924 int coef_offset = (ci * SAVED_COEFS);
|
|
3925 quanttbl = compptr.quant_table;
|
|
3926 Q00 = quanttbl.quantval[0];
|
|
3927 Q01 = quanttbl.quantval[Q01_POS];
|
|
3928 Q10 = quanttbl.quantval[Q10_POS];
|
|
3929 Q20 = quanttbl.quantval[Q20_POS];
|
|
3930 Q11 = quanttbl.quantval[Q11_POS];
|
|
3931 Q02 = quanttbl.quantval[Q02_POS];
|
|
3932 // inverse_DCT = cinfo.idct.inverse_DCT[ci];
|
|
3933 output_ptr = output_buf[ci];
|
|
3934 int output_ptr_offset = output_buf_offset[ci];
|
|
3935 /* Loop over all DCT blocks to be processed. */
|
|
3936 for (block_row = 0; block_row < block_rows; block_row++) {
|
|
3937 buffer_ptr = buffer[block_row+buffer_offset];
|
|
3938 int buffer_ptr_offset = 0, prev_block_row_offset = 0, next_block_row_offset = 0;
|
|
3939 if (first_row && block_row is 0) {
|
|
3940 prev_block_row = buffer_ptr;
|
|
3941 prev_block_row_offset = buffer_ptr_offset;
|
|
3942 } else {
|
|
3943 prev_block_row = buffer[block_row-1+buffer_offset];
|
|
3944 prev_block_row_offset = 0;
|
|
3945 }
|
|
3946 if (last_row && block_row is block_rows-1) {
|
|
3947 next_block_row = buffer_ptr;
|
|
3948 next_block_row_offset = buffer_ptr_offset;
|
|
3949 } else {
|
|
3950 next_block_row = buffer[block_row+1+buffer_offset];
|
|
3951 next_block_row_offset = 0;
|
|
3952 }
|
|
3953 /* We fetch the surrounding DC values using a sliding-register approach.
|
|
3954 * Initialize all nine here so as to do the right thing on narrow pics.
|
|
3955 */
|
|
3956 DC1 = DC2 = DC3 = prev_block_row[0+prev_block_row_offset][0];
|
|
3957 DC4 = DC5 = DC6 = buffer_ptr[0+buffer_ptr_offset][0];
|
|
3958 DC7 = DC8 = DC9 = next_block_row[0+next_block_row_offset][0];
|
|
3959 output_col = 0;
|
|
3960 last_block_column = compptr.width_in_blocks - 1;
|
|
3961 for (block_num = 0; block_num <= last_block_column; block_num++) {
|
|
3962 /* Fetch current DCT block into workspace so we can modify it. */
|
|
3963 // jcopy_block_row(buffer_ptr, workspace, 1);
|
|
3964 System.arraycopy(buffer_ptr[buffer_ptr_offset], 0, workspace, 0, workspace.length);
|
|
3965 /* Update DC values */
|
|
3966 if (block_num < last_block_column) {
|
|
3967 DC3 = prev_block_row[1+prev_block_row_offset][0];
|
|
3968 DC6 = buffer_ptr[1+buffer_ptr_offset][0];
|
|
3969 DC9 = next_block_row[1+next_block_row_offset][0];
|
|
3970 }
|
|
3971 /* Compute coefficient estimates per K.8.
|
|
3972 * An estimate is applied only if coefficient is still zero,
|
|
3973 * and is not known to be fully accurate.
|
|
3974 */
|
|
3975 /* AC01 */
|
|
3976 if ((Al=coef_bits[1+coef_offset]) !is 0 && workspace[1] is 0) {
|
|
3977 num = 36 * Q00 * (DC4 - DC6);
|
|
3978 if (num >= 0) {
|
|
3979 pred = (((Q01<<7) + num) / (Q01<<8));
|
|
3980 if (Al > 0 && pred >= (1<<Al))
|
|
3981 pred = (1<<Al)-1;
|
|
3982 } else {
|
|
3983 pred = (((Q01<<7) - num) / (Q01<<8));
|
|
3984 if (Al > 0 && pred >= (1<<Al))
|
|
3985 pred = (1<<Al)-1;
|
|
3986 pred = -pred;
|
|
3987 }
|
|
3988 workspace[1] = cast(short) pred;
|
|
3989 }
|
|
3990 /* AC10 */
|
|
3991 if ((Al=coef_bits[2+coef_offset]) !is 0 && workspace[8] is 0) {
|
|
3992 num = 36 * Q00 * (DC2 - DC8);
|
|
3993 if (num >= 0) {
|
|
3994 pred = (((Q10<<7) + num) / (Q10<<8));
|
|
3995 if (Al > 0 && pred >= (1<<Al))
|
|
3996 pred = (1<<Al)-1;
|
|
3997 } else {
|
|
3998 pred = (((Q10<<7) - num) / (Q10<<8));
|
|
3999 if (Al > 0 && pred >= (1<<Al))
|
|
4000 pred = (1<<Al)-1;
|
|
4001 pred = -pred;
|
|
4002 }
|
|
4003 workspace[8] = cast(short) pred;
|
|
4004 }
|
|
4005 /* AC20 */
|
|
4006 if ((Al=coef_bits[3+coef_offset]) !is 0 && workspace[16] is 0) {
|
|
4007 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
|
|
4008 if (num >= 0) {
|
|
4009 pred = (((Q20<<7) + num) / (Q20<<8));
|
|
4010 if (Al > 0 && pred >= (1<<Al))
|
|
4011 pred = (1<<Al)-1;
|
|
4012 } else {
|
|
4013 pred = (((Q20<<7) - num) / (Q20<<8));
|
|
4014 if (Al > 0 && pred >= (1<<Al))
|
|
4015 pred = (1<<Al)-1;
|
|
4016 pred = -pred;
|
|
4017 }
|
|
4018 workspace[16] = cast(short) pred;
|
|
4019 }
|
|
4020 /* AC11 */
|
|
4021 if ((Al=coef_bits[4+coef_offset]) !is 0 && workspace[9] is 0) {
|
|
4022 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
|
|
4023 if (num >= 0) {
|
|
4024 pred = (((Q11<<7) + num) / (Q11<<8));
|
|
4025 if (Al > 0 && pred >= (1<<Al))
|
|
4026 pred = (1<<Al)-1;
|
|
4027 } else {
|
|
4028 pred = (((Q11<<7) - num) / (Q11<<8));
|
|
4029 if (Al > 0 && pred >= (1<<Al))
|
|
4030 pred = (1<<Al)-1;
|
|
4031 pred = -pred;
|
|
4032 }
|
|
4033 workspace[9] = cast(short) pred;
|
|
4034 }
|
|
4035 /* AC02 */
|
|
4036 if ((Al=coef_bits[5+coef_offset]) !is 0 && workspace[2] is 0) {
|
|
4037 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
|
|
4038 if (num >= 0) {
|
|
4039 pred = (((Q02<<7) + num) / (Q02<<8));
|
|
4040 if (Al > 0 && pred >= (1<<Al))
|
|
4041 pred = (1<<Al)-1;
|
|
4042 } else {
|
|
4043 pred = (((Q02<<7) - num) / (Q02<<8));
|
|
4044 if (Al > 0 && pred >= (1<<Al))
|
|
4045 pred = (1<<Al)-1;
|
|
4046 pred = -pred;
|
|
4047 }
|
|
4048 workspace[2] = cast(short) pred;
|
|
4049 }
|
|
4050 /* OK, do the IDCT */
|
|
4051 jpeg_idct_islow(cinfo, compptr, workspace, output_ptr, output_ptr_offset, output_col);
|
|
4052 /* Advance for next column */
|
|
4053 DC1 = DC2; DC2 = DC3;
|
|
4054 DC4 = DC5; DC5 = DC6;
|
|
4055 DC7 = DC8; DC8 = DC9;
|
|
4056 buffer_ptr_offset++; prev_block_row_offset++; next_block_row_offset++;
|
|
4057 output_col += compptr.DCT_scaled_size;
|
|
4058 }
|
|
4059 output_ptr_offset += compptr.DCT_scaled_size;
|
|
4060 }
|
|
4061 }
|
|
4062
|
|
4063 if (++(cinfo.output_iMCU_row) < cinfo.total_iMCU_rows)
|
|
4064 return JPEG_ROW_COMPLETED;
|
|
4065 return JPEG_SCAN_COMPLETED;
|
|
4066 }
|
|
4067
|
|
4068 static int decompress_data (jpeg_decompress_struct cinfo, byte[][][] output_buf, int[] output_buf_offset) {
|
|
4069 jpeg_d_coef_controller coef = cinfo.coef;
|
|
4070 int last_iMCU_row = cinfo.total_iMCU_rows - 1;
|
|
4071 int block_num;
|
|
4072 int ci, block_row, block_rows;
|
|
4073 short[][][] buffer;
|
|
4074 short[][] buffer_ptr;
|
|
4075 byte[][] output_ptr;
|
|
4076 int output_col;
|
|
4077 jpeg_component_info compptr;
|
|
4078 // inverse_DCT_method_ptr inverse_DCT;
|
|
4079
|
|
4080 /* Force some input to be done if we are getting ahead of the input. */
|
|
4081 while (cinfo.input_scan_number < cinfo.output_scan_number ||
|
|
4082 (cinfo.input_scan_number is cinfo.output_scan_number &&
|
|
4083 cinfo.input_iMCU_row <= cinfo.output_iMCU_row))
|
|
4084 {
|
|
4085 if (consume_input(cinfo) is JPEG_SUSPENDED)
|
|
4086 return JPEG_SUSPENDED;
|
|
4087 }
|
|
4088
|
|
4089 /* OK, output from the virtual arrays. */
|
|
4090 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
4091 compptr = cinfo.comp_info[ci];
|
|
4092 /* Don't bother to IDCT an uninteresting component. */
|
|
4093 if (! compptr.component_needed)
|
|
4094 continue;
|
|
4095 /* Align the virtual buffer for this component. */
|
|
4096 buffer = coef.whole_image[ci];
|
|
4097 int buffer_offset = cinfo.output_iMCU_row * compptr.v_samp_factor;
|
|
4098 /* Count non-dummy DCT block rows in this iMCU row. */
|
|
4099 if (cinfo.output_iMCU_row < last_iMCU_row)
|
|
4100 block_rows = compptr.v_samp_factor;
|
|
4101 else {
|
|
4102 /* NB: can't use last_row_height here; it is input-side-dependent! */
|
|
4103 block_rows = (compptr.height_in_blocks % compptr.v_samp_factor);
|
|
4104 if (block_rows is 0) block_rows = compptr.v_samp_factor;
|
|
4105 }
|
|
4106 // inverse_DCT = cinfo.idct.inverse_DCT[ci];
|
|
4107 output_ptr = output_buf[ci];
|
|
4108 int output_ptr_offset = output_buf_offset[ci];
|
|
4109 /* Loop over all DCT blocks to be processed. */
|
|
4110 for (block_row = 0; block_row < block_rows; block_row++) {
|
|
4111 buffer_ptr = buffer[block_row+buffer_offset];
|
|
4112 int buffer_ptr_offset = 0;
|
|
4113 output_col = 0;
|
|
4114 for (block_num = 0; block_num < compptr.width_in_blocks; block_num++) {
|
|
4115 jpeg_idct_islow(cinfo, compptr, buffer_ptr[buffer_ptr_offset], output_ptr, output_ptr_offset, output_col);
|
|
4116
|
|
4117 buffer_ptr_offset++;
|
|
4118 output_col += compptr.DCT_scaled_size;
|
|
4119 }
|
|
4120 output_ptr_offset += compptr.DCT_scaled_size;
|
|
4121 }
|
|
4122 }
|
|
4123
|
|
4124 if (++(cinfo.output_iMCU_row) < cinfo.total_iMCU_rows)
|
|
4125 return JPEG_ROW_COMPLETED;
|
|
4126 return JPEG_SCAN_COMPLETED;
|
|
4127 }
|
|
4128
|
|
4129 static void post_process_data (jpeg_decompress_struct cinfo,
|
|
4130 byte[][][] input_buf, int[] input_buf_offset, int[] in_row_group_ctr,
|
|
4131 int in_row_groups_avail,
|
|
4132 byte[][] output_buf, int[] out_row_ctr,
|
|
4133 int out_rows_avail)
|
|
4134 {
|
|
4135 upsample(cinfo, input_buf, input_buf_offset, in_row_group_ctr, in_row_groups_avail, output_buf, out_row_ctr, out_rows_avail);
|
|
4136 }
|
|
4137
|
|
4138 static void set_bottom_pointers (jpeg_decompress_struct cinfo)
|
|
4139 /* Change the pointer lists to duplicate the last sample row at the bottom
|
|
4140 * of the image. whichptr indicates which xbuffer holds the final iMCU row.
|
|
4141 * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
|
|
4142 */
|
|
4143 {
|
|
4144 jpeg_d_main_controller main = cinfo.main;
|
|
4145 int ci, i, rgroup, iMCUheight, rows_left;
|
|
4146 jpeg_component_info compptr;
|
|
4147 byte[][] xbuf;
|
|
4148
|
|
4149 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
4150 compptr = cinfo.comp_info[ci];
|
|
4151 /* Count sample rows in one iMCU row and in one row group */
|
|
4152 iMCUheight = compptr.v_samp_factor * compptr.DCT_scaled_size;
|
|
4153 rgroup = iMCUheight / cinfo.min_DCT_scaled_size;
|
|
4154 /* Count nondummy sample rows remaining for this component */
|
|
4155 rows_left = (compptr.downsampled_height % iMCUheight);
|
|
4156 if (rows_left is 0) rows_left = iMCUheight;
|
|
4157 /* Count nondummy row groups. Should get same answer for each component,
|
|
4158 * so we need only do it once.
|
|
4159 */
|
|
4160 if (ci is 0) {
|
|
4161 main.rowgroups_avail = ((rows_left-1) / rgroup + 1);
|
|
4162 }
|
|
4163 /* Duplicate the last real sample row rgroup*2 times; this pads out the
|
|
4164 * last partial rowgroup and ensures at least one full rowgroup of context.
|
|
4165 */
|
|
4166 xbuf = main.xbuffer[main.whichptr][ci];
|
|
4167 int xbuf_offset = main.xbuffer_offset[main.whichptr][ci];
|
|
4168 for (i = 0; i < rgroup * 2; i++) {
|
|
4169 xbuf[rows_left + i + xbuf_offset] = xbuf[rows_left-1 + xbuf_offset];
|
|
4170 }
|
|
4171 }
|
|
4172 }
|
|
4173
|
|
4174 static void set_wraparound_pointers (jpeg_decompress_struct cinfo)
|
|
4175 /* Set up the "wraparound" pointers at top and bottom of the pointer lists.
|
|
4176 * This changes the pointer list state from top-of-image to the normal state.
|
|
4177 */
|
|
4178 {
|
|
4179 jpeg_d_main_controller main = cinfo.main;
|
|
4180 int ci, i, rgroup;
|
|
4181 int M = cinfo.min_DCT_scaled_size;
|
|
4182 jpeg_component_info compptr;
|
|
4183 byte[][] xbuf0, xbuf1;
|
|
4184
|
|
4185 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
4186 compptr = cinfo.comp_info[ci];
|
|
4187 rgroup = (compptr.v_samp_factor * compptr.DCT_scaled_size) / cinfo.min_DCT_scaled_size; /* height of a row group of component */
|
|
4188 xbuf0 = main.xbuffer[0][ci];
|
|
4189 int xbuf0_offset = main.xbuffer_offset[0][ci];
|
|
4190 xbuf1 = main.xbuffer[1][ci];
|
|
4191 int xbuf1_offset = main.xbuffer_offset[1][ci];
|
|
4192 for (i = 0; i < rgroup; i++) {
|
|
4193 xbuf0[i - rgroup + xbuf0_offset] = xbuf0[rgroup*(M+1) + i + xbuf0_offset];
|
|
4194 xbuf1[i - rgroup + xbuf1_offset] = xbuf1[rgroup*(M+1) + i + xbuf1_offset];
|
|
4195 xbuf0[rgroup*(M+2) + i + xbuf0_offset] = xbuf0[i + xbuf0_offset];
|
|
4196 xbuf1[rgroup*(M+2) + i + xbuf1_offset] = xbuf1[i + xbuf1_offset];
|
|
4197 }
|
|
4198 }
|
|
4199 }
|
|
4200
|
|
4201 static void process_data_crank_post (jpeg_decompress_struct cinfo,
|
|
4202 byte[][] output_buf, int[] out_row_ctr,
|
|
4203 int out_rows_avail)
|
|
4204 {
|
|
4205 error();
|
|
4206 }
|
|
4207
|
|
4208 static void process_data_context_main (jpeg_decompress_struct cinfo,
|
|
4209 byte[][] output_buf, int[] out_row_ctr,
|
|
4210 int out_rows_avail)
|
|
4211 {
|
|
4212 jpeg_d_main_controller main = cinfo.main;
|
|
4213
|
|
4214 /* Read input data if we haven't filled the main buffer yet */
|
|
4215 if (! main.buffer_full) {
|
|
4216 int result;
|
|
4217 switch (cinfo.coef.decompress_data) {
|
|
4218 case DECOMPRESS_DATA:
|
|
4219 result = decompress_data(cinfo, main.xbuffer[main.whichptr], main.xbuffer_offset[main.whichptr]);
|
|
4220 break;
|
|
4221 case DECOMPRESS_SMOOTH_DATA:
|
|
4222 result = decompress_smooth_data(cinfo, main.xbuffer[main.whichptr], main.xbuffer_offset[main.whichptr]);
|
|
4223 break;
|
|
4224 case DECOMPRESS_ONEPASS:
|
|
4225 result = decompress_onepass(cinfo, main.xbuffer[main.whichptr], main.xbuffer_offset[main.whichptr]);
|
|
4226 break;
|
|
4227 default: result = 0;
|
|
4228 }
|
|
4229 if (result is 0)
|
|
4230 return; /* suspension forced, can do nothing more */
|
|
4231 main.buffer_full = true; /* OK, we have an iMCU row to work with */
|
|
4232 main.iMCU_row_ctr++; /* count rows received */
|
|
4233 }
|
|
4234
|
|
4235 /* Postprocessor typically will not swallow all the input data it is handed
|
|
4236 * in one call (due to filling the output buffer first). Must be prepared
|
|
4237 * to exit and restart. This switch lets us keep track of how far we got.
|
|
4238 * Note that each case falls through to the next on successful completion.
|
|
4239 */
|
|
4240 switch (main.context_state) {
|
|
4241 case CTX_POSTPONED_ROW:
|
|
4242 /* Call postprocessor using previously set pointers for postponed row */
|
|
4243 post_process_data (cinfo, main.xbuffer[main.whichptr], main.xbuffer_offset[main.whichptr], main.rowgroup_ctr, main.rowgroups_avail, output_buf, out_row_ctr, out_rows_avail);
|
|
4244 if (main.rowgroup_ctr[0] < main.rowgroups_avail)
|
|
4245 return; /* Need to suspend */
|
|
4246 main.context_state = CTX_PREPARE_FOR_IMCU;
|
|
4247 if (out_row_ctr[0] >= out_rows_avail)
|
|
4248 return; /* Postprocessor exactly filled output buf */
|
|
4249 /*FALLTHROUGH*/
|
|
4250 case CTX_PREPARE_FOR_IMCU:
|
|
4251 /* Prepare to process first M-1 row groups of this iMCU row */
|
|
4252 main.rowgroup_ctr[0] = 0;
|
|
4253 main.rowgroups_avail = (cinfo.min_DCT_scaled_size - 1);
|
|
4254 /* Check for bottom of image: if so, tweak pointers to "duplicate"
|
|
4255 * the last sample row, and adjust rowgroups_avail to ignore padding rows.
|
|
4256 */
|
|
4257 if (main.iMCU_row_ctr is cinfo.total_iMCU_rows)
|
|
4258 set_bottom_pointers(cinfo);
|
|
4259 main.context_state = CTX_PROCESS_IMCU;
|
|
4260 /*FALLTHROUGH*/
|
|
4261 case CTX_PROCESS_IMCU:
|
|
4262 /* Call postprocessor using previously set pointers */
|
|
4263 post_process_data (cinfo, main.xbuffer[main.whichptr], main.xbuffer_offset[main.whichptr], main.rowgroup_ctr, main.rowgroups_avail, output_buf, out_row_ctr, out_rows_avail);
|
|
4264 if (main.rowgroup_ctr[0] < main.rowgroups_avail)
|
|
4265 return; /* Need to suspend */
|
|
4266 /* After the first iMCU, change wraparound pointers to normal state */
|
|
4267 if (main.iMCU_row_ctr is 1)
|
|
4268 set_wraparound_pointers(cinfo);
|
|
4269 /* Prepare to load new iMCU row using other xbuffer list */
|
|
4270 main.whichptr ^= 1; /* 0=>1 or 1=>0 */
|
|
4271 main.buffer_full = false;
|
|
4272 /* Still need to process last row group of this iMCU row, */
|
|
4273 /* which is saved at index M+1 of the other xbuffer */
|
|
4274 main.rowgroup_ctr[0] = (cinfo.min_DCT_scaled_size + 1);
|
|
4275 main.rowgroups_avail = (cinfo.min_DCT_scaled_size + 2);
|
|
4276 main.context_state = CTX_POSTPONED_ROW;
|
|
4277 default:
|
|
4278 }
|
|
4279 }
|
|
4280
|
|
4281 static void process_data_simple_main (jpeg_decompress_struct cinfo, byte[][] output_buf, int[] out_row_ctr, int out_rows_avail) {
|
|
4282 jpeg_d_main_controller main = cinfo.main;
|
|
4283 int rowgroups_avail;
|
|
4284
|
|
4285 /* Read input data if we haven't filled the main buffer yet */
|
|
4286 if (! main.buffer_full) {
|
|
4287 int result;
|
|
4288 switch (cinfo.coef.decompress_data) {
|
|
4289 case DECOMPRESS_DATA:
|
|
4290 result = decompress_data(cinfo, main.buffer, main.buffer_offset);
|
|
4291 break;
|
|
4292 case DECOMPRESS_SMOOTH_DATA:
|
|
4293 result = decompress_smooth_data(cinfo, main.buffer, main.buffer_offset);
|
|
4294 break;
|
|
4295 case DECOMPRESS_ONEPASS:
|
|
4296 result = decompress_onepass(cinfo, main.buffer, main.buffer_offset);
|
|
4297 break;
|
|
4298 default: result = 0;
|
|
4299 }
|
|
4300 if (result is 0)
|
|
4301 return; /* suspension forced, can do nothing more */
|
|
4302 main.buffer_full = true; /* OK, we have an iMCU row to work with */
|
|
4303 }
|
|
4304
|
|
4305 /* There are always min_DCT_scaled_size row groups in an iMCU row. */
|
|
4306 rowgroups_avail = cinfo.min_DCT_scaled_size;
|
|
4307 /* Note: at the bottom of the image, we may pass extra garbage row groups
|
|
4308 * to the postprocessor. The postprocessor has to check for bottom
|
|
4309 * of image anyway (at row resolution), so no point in us doing it too.
|
|
4310 */
|
|
4311
|
|
4312 /* Feed the postprocessor */
|
|
4313 post_process_data (cinfo, main.buffer, main.buffer_offset, main.rowgroup_ctr, rowgroups_avail, output_buf, out_row_ctr, out_rows_avail);
|
|
4314
|
|
4315 /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
|
|
4316 if (main.rowgroup_ctr[0] >= rowgroups_avail) {
|
|
4317 main.buffer_full = false;
|
|
4318 main.rowgroup_ctr[0] = 0;
|
|
4319 }
|
|
4320 }
|
|
4321
|
|
4322 static int jpeg_read_scanlines (jpeg_decompress_struct cinfo, byte[][] scanlines, int max_lines) {
|
|
4323
|
|
4324 if (cinfo.global_state !is DSTATE_SCANNING)
|
|
4325 error();
|
|
4326 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
4327 if (cinfo.output_scanline >= cinfo.output_height) {
|
|
4328 // WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
|
|
4329 return 0;
|
|
4330 }
|
|
4331
|
|
4332 /* Call progress monitor hook if present */
|
|
4333 // if (cinfo.progress !is NULL) {
|
|
4334 // cinfo.progress.pass_counter = cast(long) cinfo.output_scanline;
|
|
4335 // cinfo.progress.pass_limit = cast(long) cinfo.output_height;
|
|
4336 // (*cinfo.progress.progress_monitor) ((j_common_ptr) cinfo);
|
|
4337 // }
|
|
4338
|
|
4339 /* Process some data */
|
|
4340 cinfo.row_ctr[0] = 0;
|
|
4341 switch (cinfo.main.process_data) {
|
|
4342 case PROCESS_DATA_SIMPLE_MAIN:
|
|
4343 process_data_simple_main (cinfo, scanlines, cinfo.row_ctr, max_lines);
|
|
4344 break;
|
|
4345 case PROCESS_DATA_CONTEXT_MAIN:
|
|
4346 process_data_context_main (cinfo, scanlines, cinfo.row_ctr, max_lines);
|
|
4347 break;
|
|
4348 case PROCESS_DATA_CRANK_POST:
|
|
4349 process_data_crank_post (cinfo, scanlines, cinfo.row_ctr, max_lines);
|
|
4350 break;
|
|
4351 default: error();
|
|
4352 }
|
|
4353 cinfo.output_scanline += cinfo.row_ctr[0];
|
|
4354 return cinfo.row_ctr[0];
|
|
4355 }
|
|
4356
|
|
4357
|
|
4358 static bool output_pass_setup (jpeg_decompress_struct cinfo) {
|
|
4359 if (cinfo.global_state !is DSTATE_PRESCAN) {
|
|
4360 /* First call: do pass setup */
|
|
4361 prepare_for_output_pass (cinfo);
|
|
4362 cinfo.output_scanline = 0;
|
|
4363 cinfo.global_state = DSTATE_PRESCAN;
|
|
4364 }
|
|
4365 /* Loop over any required dummy passes */
|
|
4366 while (cinfo.master.is_dummy_pass) {
|
|
4367 error();
|
|
4368 //#ifdef QUANT_2PASS_SUPPORTED
|
|
4369 // /* Crank through the dummy pass */
|
|
4370 // while (cinfo.output_scanline < cinfo.output_height) {
|
|
4371 // JDIMENSION last_scanline;
|
|
4372 // /* Call progress monitor hook if present */
|
|
4373 // if (cinfo.progress !is NULL) {
|
|
4374 // cinfo.progress.pass_counter = cast(long) cinfo.output_scanline;
|
|
4375 // cinfo.progress.pass_limit = cast(long) cinfo.output_height;
|
|
4376 // (*cinfo.progress.progress_monitor) ((j_common_ptr) cinfo);
|
|
4377 // }
|
|
4378 // /* Process some data */
|
|
4379 // last_scanline = cinfo.output_scanline;
|
|
4380 // (*cinfo.main.process_data) (cinfo, (JSAMPARRAY) NULL,
|
|
4381 // &cinfo.output_scanline, (JDIMENSION) 0);
|
|
4382 // if (cinfo.output_scanline is last_scanline)
|
|
4383 // return FALSE; /* No progress made, must suspend */
|
|
4384 // }
|
|
4385 // /* Finish up dummy pass, and set up for another one */
|
|
4386 // (*cinfo.master.finish_output_pass) (cinfo);
|
|
4387 // (*cinfo.master.prepare_for_output_pass) (cinfo);
|
|
4388 // cinfo.output_scanline = 0;
|
|
4389 //#else
|
|
4390 // ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
4391 //#endif /* QUANT_2PASS_SUPPORTED */
|
|
4392 }
|
|
4393 /* Ready for application to drive output pass through
|
|
4394 * jpeg_read_scanlines or jpeg_read_raw_data.
|
|
4395 */
|
|
4396 cinfo.global_state = cinfo.raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
|
|
4397 return true;
|
|
4398 }
|
|
4399
|
|
4400 static bool get_dht (jpeg_decompress_struct cinfo)
|
|
4401 /* Process a DHT marker */
|
|
4402 {
|
|
4403 int length;
|
|
4404 byte[] bits = new byte[17];
|
|
4405 byte[] huffval = new byte[256];
|
|
4406 int i, index, count;
|
|
4407 JHUFF_TBL htblptr;
|
|
4408
|
|
4409 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4410 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4411 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4412 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4413 length -= 2;
|
|
4414
|
|
4415 while (length > 16) {
|
|
4416 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4417 index = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4418
|
|
4419 // TRACEMS1(cinfo, 1, JTRC_DHT, index);
|
|
4420
|
|
4421 bits[0] = 0;
|
|
4422 count = 0;
|
|
4423 for (i = 1; i <= 16; i++) {
|
|
4424 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4425 bits[i] = cinfo.buffer[cinfo.bytes_offset++];
|
|
4426 count += bits[i] & 0xFF;
|
|
4427 }
|
|
4428
|
|
4429 length -= 1 + 16;
|
|
4430
|
|
4431 // TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
|
|
4432 // bits[1], bits[2], bits[3], bits[4],
|
|
4433 // bits[5], bits[6], bits[7], bits[8]);
|
|
4434 // TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
|
|
4435 // bits[9], bits[10], bits[11], bits[12],
|
|
4436 // bits[13], bits[14], bits[15], bits[16]);
|
|
4437
|
|
4438 /* Here we just do minimal validation of the counts to avoid walking
|
|
4439 * off the end of our table space. jdhuff.c will check more carefully.
|
|
4440 */
|
|
4441 if (count > 256 || (count) > length)
|
|
4442 error();
|
|
4443 // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
4444
|
|
4445 for (i = 0; i < count; i++) {
|
|
4446 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4447 huffval[i] = cinfo.buffer[cinfo.bytes_offset++];
|
|
4448 }
|
|
4449
|
|
4450 length -= count;
|
|
4451
|
|
4452 if ((index & 0x10) !is 0) { /* AC table definition */
|
|
4453 index -= 0x10;
|
|
4454 htblptr = cinfo.ac_huff_tbl_ptrs[index] = new JHUFF_TBL();
|
|
4455 } else { /* DC table definition */
|
|
4456 htblptr = cinfo.dc_huff_tbl_ptrs[index] = new JHUFF_TBL();
|
|
4457 }
|
|
4458
|
|
4459 if (index < 0 || index >= NUM_HUFF_TBLS)
|
|
4460 error();
|
|
4461 // ERREXIT1(cinfo, JERR_DHT_INDEX, index);
|
|
4462
|
|
4463 System.arraycopy(bits, 0, htblptr.bits, 0, bits.length);
|
|
4464 System.arraycopy(huffval, 0, htblptr.huffval, 0, huffval.length);
|
|
4465 }
|
|
4466
|
|
4467 if (length !is 0)
|
|
4468 error();
|
|
4469 // ERREXIT(cinfo, JERR_BAD_LENGTH);
|
|
4470
|
|
4471 return true;
|
|
4472 }
|
|
4473
|
|
4474
|
|
4475 static bool get_dqt (jpeg_decompress_struct cinfo)
|
|
4476 /* Process a DQT marker */
|
|
4477 {
|
|
4478 int length;
|
|
4479 int n, i, prec;
|
|
4480 int tmp;
|
|
4481 JQUANT_TBL quant_ptr;
|
|
4482
|
|
4483 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4484 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4485 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4486 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4487 length -= 2;
|
|
4488
|
|
4489 while (length > 0) {
|
|
4490 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4491 n = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4492 prec = n >> 4;
|
|
4493 n &= 0x0F;
|
|
4494
|
|
4495 // TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
|
|
4496
|
|
4497 if (n >= NUM_QUANT_TBLS)
|
|
4498 error();
|
|
4499 // ERREXIT1(cinfo, JERR_DQT_INDEX, n);
|
|
4500
|
|
4501 if (cinfo.quant_tbl_ptrs[n] is null)
|
|
4502 cinfo.quant_tbl_ptrs[n] = new JQUANT_TBL();
|
|
4503 quant_ptr = cinfo.quant_tbl_ptrs[n];
|
|
4504
|
|
4505 for (i = 0; i < DCTSIZE2; i++) {
|
|
4506 if (prec !is 0) {
|
|
4507 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4508 tmp = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4509 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4510 tmp |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4511 } else {
|
|
4512 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4513 tmp = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4514 }
|
|
4515 /* We convert the zigzag-order table to natural array order. */
|
|
4516 quant_ptr.quantval[jpeg_natural_order[i]] = cast(short) tmp;
|
|
4517 }
|
|
4518
|
|
4519 // if (cinfo.err.trace_level >= 2) {
|
|
4520 // for (i = 0; i < DCTSIZE2; i += 8) {
|
|
4521 // TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
|
|
4522 // quant_ptr.quantval[i], quant_ptr.quantval[i+1],
|
|
4523 // quant_ptr.quantval[i+2], quant_ptr.quantval[i+3],
|
|
4524 // quant_ptr.quantval[i+4], quant_ptr.quantval[i+5],
|
|
4525 // quant_ptr.quantval[i+6], quant_ptr.quantval[i+7]);
|
|
4526 // }
|
|
4527 // }
|
|
4528
|
|
4529 length -= (DCTSIZE2+1);
|
|
4530 if (prec !is 0) length -= DCTSIZE2;
|
|
4531 }
|
|
4532
|
|
4533 if (length !is 0)
|
|
4534 error();
|
|
4535 // ERREXIT(cinfo, JERR_BAD_LENGTH);
|
|
4536
|
|
4537 return true;
|
|
4538 }
|
|
4539
|
|
4540 static bool get_dri (jpeg_decompress_struct cinfo)
|
|
4541 /* Process a DRI marker */
|
|
4542 {
|
|
4543 int length;
|
|
4544 int tmp;
|
|
4545
|
|
4546 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4547 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4548 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4549 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4550
|
|
4551 if (length !is 4)
|
|
4552 error();
|
|
4553 // ERREXIT(cinfo, JERR_BAD_LENGTH);
|
|
4554
|
|
4555 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4556 tmp = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4557 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4558 tmp |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4559
|
|
4560 // TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
|
|
4561
|
|
4562 cinfo.restart_interval = tmp;
|
|
4563
|
|
4564 return true;
|
|
4565 }
|
|
4566
|
|
4567 static bool get_dac (jpeg_decompress_struct cinfo)
|
|
4568 /* Process a DAC marker */
|
|
4569 {
|
|
4570 int length;
|
|
4571 int index, val;
|
|
4572 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4573 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4574 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4575 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4576 length -= 2;
|
|
4577
|
|
4578 while (length > 0) {
|
|
4579 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4580 index = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4581 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4582 val = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4583
|
|
4584 length -= 2;
|
|
4585
|
|
4586 // TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
|
|
4587
|
|
4588 if (index < 0 || index >= (2*NUM_ARITH_TBLS))
|
|
4589 error();
|
|
4590 // ERREXIT1(cinfo, JERR_DAC_INDEX, index);
|
|
4591
|
|
4592 if (index >= NUM_ARITH_TBLS) { /* define AC table */
|
|
4593 cinfo.arith_ac_K[index-NUM_ARITH_TBLS] = cast(byte) val;
|
|
4594 } else { /* define DC table */
|
|
4595 cinfo.arith_dc_L[index] = cast(byte) (val & 0x0F);
|
|
4596 cinfo.arith_dc_U[index] = cast(byte) (val >> 4);
|
|
4597 if (cinfo.arith_dc_L[index] > cinfo.arith_dc_U[index])
|
|
4598 error();
|
|
4599 // ERREXIT1(cinfo, JERR_DAC_VALUE, val);
|
|
4600 }
|
|
4601 }
|
|
4602
|
|
4603 if (length !is 0)
|
|
4604 error();
|
|
4605 // ERREXIT(cinfo, JERR_BAD_LENGTH);
|
|
4606
|
|
4607 return true;
|
|
4608 }
|
|
4609
|
|
4610
|
|
4611 static bool get_sos (jpeg_decompress_struct cinfo)
|
|
4612 /* Process a SOS marker */
|
|
4613 {
|
|
4614 int length;
|
|
4615 int i, ci, n, c, cc;
|
|
4616 jpeg_component_info compptr = null;
|
|
4617
|
|
4618 if (! cinfo.marker.saw_SOF)
|
|
4619 error();
|
|
4620 // ERREXIT(cinfo, JERR_SOS_NO_SOF);
|
|
4621
|
|
4622 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4623 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4624 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4625 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4626
|
|
4627 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4628 n = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4629
|
|
4630 // TRACEMS1(cinfo, 1, JTRC_SOS, n);
|
|
4631
|
|
4632 if (length !is (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
|
|
4633 error();
|
|
4634 // ERREXIT(cinfo, JERR_BAD_LENGTH);
|
|
4635
|
|
4636 cinfo.comps_in_scan = n;
|
|
4637
|
|
4638 /* Collect the component-spec parameters */
|
|
4639
|
|
4640 for (i = 0; i < n; i++) {
|
|
4641 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4642 cc = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4643 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4644 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4645
|
|
4646 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
4647 compptr = cinfo.comp_info[ci];
|
|
4648 if (cc is compptr.component_id)
|
|
4649 break;
|
|
4650 }
|
|
4651
|
|
4652 if (ci is cinfo.num_components)
|
|
4653 error();
|
|
4654 // ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
|
|
4655
|
|
4656 cinfo.cur_comp_info[i] = compptr;
|
|
4657 compptr.dc_tbl_no = (c >> 4) & 15;
|
|
4658 compptr.ac_tbl_no = (c ) & 15;
|
|
4659
|
|
4660 // TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc, compptr.dc_tbl_no, compptr.ac_tbl_no);
|
|
4661 }
|
|
4662
|
|
4663 /* Collect the additional scan parameters Ss, Se, Ah/Al. */
|
|
4664 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4665 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4666 cinfo.Ss = c;
|
|
4667 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4668 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4669 cinfo.Se = c;
|
|
4670 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4671 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4672 cinfo.Ah = (c >> 4) & 15;
|
|
4673 cinfo.Al = (c ) & 15;
|
|
4674
|
|
4675 // TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo.Ss, cinfo.Se, cinfo.Ah, cinfo.Al);
|
|
4676
|
|
4677 /* Prepare to scan data & restart markers */
|
|
4678 cinfo.marker.next_restart_num = 0;
|
|
4679
|
|
4680 /* Count another SOS marker */
|
|
4681 cinfo.input_scan_number++;
|
|
4682
|
|
4683 return true;
|
|
4684 }
|
|
4685
|
|
4686 static bool get_sof (jpeg_decompress_struct cinfo, bool is_prog, bool is_arith) {
|
|
4687 int length;
|
|
4688 int c, ci;
|
|
4689
|
|
4690 cinfo.progressive_mode = is_prog;
|
|
4691 cinfo.arith_code = is_arith;
|
|
4692
|
|
4693 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4694 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4695 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4696 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4697
|
|
4698 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4699 cinfo.data_precision = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4700
|
|
4701 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4702 cinfo.image_height = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4703 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4704 cinfo.image_height |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4705
|
|
4706 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4707 cinfo.image_width = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
4708 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4709 cinfo.image_width |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4710
|
|
4711 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4712 cinfo.num_components = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4713
|
|
4714 length -= 8;
|
|
4715
|
|
4716 // TRACEMS4(cinfo, 1, JTRC_SOF, cinfo.unread_marker,
|
|
4717 // cast(int) cinfo.image_width, cast(int) cinfo.image_height,
|
|
4718 // cinfo.num_components);
|
|
4719
|
|
4720 if (cinfo.marker.saw_SOF)
|
|
4721 error();
|
|
4722 // ERREXIT(cinfo, JERR_SOF_DUPLICATE);
|
|
4723
|
|
4724 /* We don't support files in which the image height is initially specified */
|
|
4725 /* as 0 and is later redefined by DNL. As long as we have to check that, */
|
|
4726 /* might as well have a general sanity check. */
|
|
4727 if (cinfo.image_height <= 0 || cinfo.image_width <= 0 || cinfo.num_components <= 0)
|
|
4728 error();
|
|
4729 // ERREXIT(cinfo, JERR_EMPTY_IMAGE);
|
|
4730
|
|
4731 if (length !is (cinfo.num_components * 3))
|
|
4732 error();
|
|
4733 // ERREXIT(cinfo, JERR_BAD_LENGTH);
|
|
4734
|
|
4735 if (cinfo.comp_info is null) /* do only once, even if suspend */
|
|
4736 cinfo.comp_info = new jpeg_component_info[cinfo.num_components];
|
|
4737
|
|
4738 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
4739 jpeg_component_info compptr = cinfo.comp_info[ci] = new jpeg_component_info();
|
|
4740 compptr.component_index = ci;
|
|
4741 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4742 compptr.component_id = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4743 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4744 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4745 compptr.h_samp_factor = (c >> 4) & 15;
|
|
4746 compptr.v_samp_factor = (c ) & 15;
|
|
4747 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
4748 compptr.quant_tbl_no = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
4749
|
|
4750 // TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
|
|
4751 // compptr.component_id, compptr.h_samp_factor,
|
|
4752 // compptr.v_samp_factor, compptr.quant_tbl_no);
|
|
4753 }
|
|
4754
|
|
4755 cinfo.marker.saw_SOF = true;
|
|
4756
|
|
4757 return true;
|
|
4758 }
|
|
4759
|
|
4760 static void sep_upsample (jpeg_decompress_struct cinfo, byte[][][] input_buf, int[] input_buf_offset,
|
|
4761 int[] in_row_group_ctr, int in_row_groups_avail,
|
|
4762 byte[][] output_buf, int[] out_row_ctr, int out_rows_avail)
|
|
4763 {
|
|
4764 jpeg_upsampler upsample = cinfo.upsample;
|
|
4765 int ci;
|
|
4766 jpeg_component_info compptr;
|
|
4767 int num_rows;
|
|
4768
|
|
4769 /* Fill the conversion buffer, if it's empty */
|
|
4770 if (upsample.next_row_out >= cinfo.max_v_samp_factor) {
|
|
4771 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
4772 compptr = cinfo.comp_info[ci];
|
|
4773 /* Invoke per-component upsample method. Notice we pass a POINTER
|
|
4774 * to color_buf[ci], so that fullsize_upsample can change it.
|
|
4775 */
|
|
4776 int offset = input_buf_offset[ci] + (in_row_group_ctr[0] * upsample.rowgroup_height[ci]);
|
|
4777 switch (upsample.methods[ci]) {
|
|
4778 case NOOP_UPSAMPLE: noop_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4779 case FULLSIZE_UPSAMPLE: fullsize_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4780 case H2V1_FANCY_UPSAMPLE: h2v1_fancy_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4781 case H2V1_UPSAMPLE: h2v1_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4782 case H2V2_FANCY_UPSAMPLE: h2v2_fancy_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4783 case H2V2_UPSAMPLE: h2v2_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4784 case INT_UPSAMPLE: int_upsample(cinfo, compptr, input_buf[ci], offset, upsample.color_buf, upsample.color_buf_offset, ci); break;
|
|
4785 default:
|
|
4786 }
|
|
4787 }
|
|
4788 upsample.next_row_out = 0;
|
|
4789 }
|
|
4790
|
|
4791 /* Color-convert and emit rows */
|
|
4792
|
|
4793 /* How many we have in the buffer: */
|
|
4794 num_rows = (cinfo.max_v_samp_factor - upsample.next_row_out);
|
|
4795 /* Not more than the distance to the end of the image. Need this test
|
|
4796 * in case the image height is not a multiple of max_v_samp_factor:
|
|
4797 */
|
|
4798 if (num_rows > upsample.rows_to_go)
|
|
4799 num_rows = upsample.rows_to_go;
|
|
4800 /* And not more than what the client can accept: */
|
|
4801 out_rows_avail -= out_row_ctr[0];
|
|
4802 if (num_rows > out_rows_avail)
|
|
4803 num_rows = out_rows_avail;
|
|
4804
|
|
4805 switch (cinfo.cconvert.color_convert) {
|
|
4806 case NULL_CONVERT: null_convert (cinfo, upsample.color_buf, upsample.color_buf_offset, upsample.next_row_out, output_buf, out_row_ctr[0], num_rows); break;
|
|
4807 case GRAYSCALE_CONVERT: grayscale_convert (cinfo, upsample.color_buf, upsample.color_buf_offset, upsample.next_row_out, output_buf, out_row_ctr[0], num_rows); break;
|
|
4808 case YCC_RGB_CONVERT: ycc_rgb_convert (cinfo, upsample.color_buf, upsample.color_buf_offset, upsample.next_row_out, output_buf, out_row_ctr[0], num_rows); break;
|
|
4809 case GRAY_RGB_CONVERT: gray_rgb_convert (cinfo, upsample.color_buf, upsample.color_buf_offset, upsample.next_row_out, output_buf, out_row_ctr[0], num_rows); break;
|
|
4810 case YCCK_CMYK_CONVERT: error(); break;
|
|
4811 default:
|
|
4812 }
|
|
4813
|
|
4814 /* Adjust counts */
|
|
4815 out_row_ctr[0] += num_rows;
|
|
4816 upsample.rows_to_go -= num_rows;
|
|
4817 upsample.next_row_out += num_rows;
|
|
4818 /* When the buffer is emptied, declare this input row group consumed */
|
|
4819 if (upsample.next_row_out >= cinfo.max_v_samp_factor) {
|
|
4820 in_row_group_ctr[0]++;
|
|
4821 }
|
|
4822 }
|
|
4823
|
|
4824 static void noop_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4825 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4826 {
|
|
4827 output_data_ptr[output_data_index] = null; /* safety check */
|
|
4828 }
|
|
4829
|
|
4830 static void fullsize_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4831 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4832 {
|
|
4833 output_data_ptr[output_data_index] = input_data;
|
|
4834 output_data_offset[output_data_index] = input_data_offset;
|
|
4835 }
|
|
4836
|
|
4837 static void h2v1_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4838 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4839 {
|
|
4840 byte[][] output_data = output_data_ptr[output_data_index];
|
|
4841 byte[] inptr, outptr;
|
|
4842 byte invalue;
|
|
4843 int outend;
|
|
4844 int inrow;
|
|
4845 output_data_offset[output_data_index] = 0;
|
|
4846
|
|
4847 for (inrow = 0; inrow < cinfo.max_v_samp_factor; inrow++) {
|
|
4848 inptr = input_data[inrow+input_data_offset];
|
|
4849 outptr = output_data[inrow];
|
|
4850 int inptr_offset = 0, outptr_offset = 0;
|
|
4851 outend = outptr_offset + cinfo.output_width;
|
|
4852 while (outptr_offset < outend) {
|
|
4853 invalue = inptr[inptr_offset++]; /* don't need GETJSAMPLE() here */
|
|
4854 outptr[outptr_offset++] = invalue;
|
|
4855 outptr[outptr_offset++] = invalue;
|
|
4856 }
|
|
4857 }
|
|
4858 }
|
|
4859
|
|
4860 static void h2v2_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4861 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4862 {
|
|
4863 byte[][] output_data = output_data_ptr[output_data_index];
|
|
4864 byte[] inptr, outptr;
|
|
4865 byte invalue;
|
|
4866 int outend;
|
|
4867 int inrow, outrow;
|
|
4868 output_data_offset[output_data_index] = 0;
|
|
4869
|
|
4870 inrow = outrow = 0;
|
|
4871 while (outrow < cinfo.max_v_samp_factor) {
|
|
4872 inptr = input_data[inrow+input_data_offset];
|
|
4873 outptr = output_data[outrow];
|
|
4874 int inptr_offset = 0, outptr_offset = 0;
|
|
4875 outend = outptr_offset + cinfo.output_width;
|
|
4876 while (outptr_offset < outend) {
|
|
4877 invalue = inptr[inptr_offset++]; /* don't need GETJSAMPLE() here */
|
|
4878 outptr[outptr_offset++] = invalue;
|
|
4879 outptr[outptr_offset++] = invalue;
|
|
4880 }
|
|
4881 jcopy_sample_rows(output_data, outrow, output_data, outrow+1, 1, cinfo.output_width);
|
|
4882 inrow++;
|
|
4883 outrow += 2;
|
|
4884 }
|
|
4885 }
|
|
4886
|
|
4887 static void h2v1_fancy_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4888 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4889 {
|
|
4890 byte[][] output_data = output_data_ptr[output_data_index];
|
|
4891 byte[] inptr, outptr;
|
|
4892 int invalue;
|
|
4893 int colctr;
|
|
4894 int inrow;
|
|
4895 output_data_offset[output_data_index] = 0;
|
|
4896
|
|
4897 for (inrow = 0; inrow < cinfo.max_v_samp_factor; inrow++) {
|
|
4898 inptr = input_data[inrow+input_data_offset];
|
|
4899 outptr = output_data[inrow];
|
|
4900 int inptr_offset = 0, outptr_offset = 0;
|
|
4901 /* Special case for first column */
|
|
4902 invalue = inptr[inptr_offset++] & 0xFF;
|
|
4903 outptr[outptr_offset++] = cast(byte) invalue;
|
|
4904 outptr[outptr_offset++] = cast(byte) ((invalue * 3 + (inptr[inptr_offset] & 0xFF) + 2) >> 2);
|
|
4905
|
|
4906 for (colctr = compptr.downsampled_width - 2; colctr > 0; colctr--) {
|
|
4907 /* General case: 3/4 * nearer pixel + 1/4 * further pixel */
|
|
4908 invalue = (inptr[inptr_offset++] & 0xFF) * 3;
|
|
4909 outptr[outptr_offset++] = cast(byte) ((invalue + (inptr[inptr_offset-2] & 0xFF) + 1) >> 2);
|
|
4910 outptr[outptr_offset++] = cast(byte) ((invalue + (inptr[inptr_offset] & 0xFF) + 2) >> 2);
|
|
4911 }
|
|
4912
|
|
4913 /* Special case for last column */
|
|
4914 invalue = (inptr[inptr_offset] & 0xFF);
|
|
4915 outptr[outptr_offset++] = cast(byte) ((invalue * 3 + (inptr[inptr_offset-1] & 0xFF) + 1) >> 2);
|
|
4916 outptr[outptr_offset++] = cast(byte) invalue;
|
|
4917 }
|
|
4918 }
|
|
4919
|
|
4920 static void h2v2_fancy_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4921 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4922 {
|
|
4923 byte[][] output_data = output_data_ptr[output_data_index];
|
|
4924 byte[] inptr0, inptr1, outptr;
|
|
4925 int thiscolsum, lastcolsum, nextcolsum;
|
|
4926 int colctr;
|
|
4927 int inrow, outrow, v;
|
|
4928 output_data_offset[output_data_index] = 0;
|
|
4929
|
|
4930 inrow = outrow = 0;
|
|
4931 while (outrow < cinfo.max_v_samp_factor) {
|
|
4932 for (v = 0; v < 2; v++) {
|
|
4933 /* inptr0 points to nearest input row, inptr1 points to next nearest */
|
|
4934 inptr0 = input_data[inrow+input_data_offset];
|
|
4935 if (v is 0) /* next nearest is row above */
|
|
4936 inptr1 = input_data[inrow-1+input_data_offset];
|
|
4937 else /* next nearest is row below */
|
|
4938 inptr1 = input_data[inrow+1+input_data_offset];
|
|
4939 outptr = output_data[outrow++];
|
|
4940
|
|
4941 int inptr0_offset = 0, inptr1_offset = 0, outptr_offset = 0;
|
|
4942
|
|
4943 /* Special case for first column */
|
|
4944 thiscolsum = (inptr0[inptr0_offset++] & 0xFF) * 3 + (inptr1[inptr1_offset++] & 0xFF);
|
|
4945 nextcolsum = (inptr0[inptr0_offset++] & 0xFF) * 3 + (inptr1[inptr1_offset++] & 0xFF);
|
|
4946 outptr[outptr_offset++] = cast(byte) ((thiscolsum * 4 + 8) >> 4);
|
|
4947 outptr[outptr_offset++] = cast(byte) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
|
|
4948 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
|
|
4949
|
|
4950 for (colctr = compptr.downsampled_width - 2; colctr > 0; colctr--) {
|
|
4951 /* General case: 3/4 * nearer pixel + 1/4 * further pixel in each */
|
|
4952 /* dimension, thus 9/16, 3/16, 3/16, 1/16 overall */
|
|
4953 nextcolsum = (inptr0[inptr0_offset++] & 0xFF) * 3 + (inptr1[inptr1_offset++] & 0xFF);
|
|
4954 outptr[outptr_offset++] = cast(byte) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
|
|
4955 outptr[outptr_offset++] = cast(byte) ((thiscolsum * 3 + nextcolsum + 7) >> 4);
|
|
4956 lastcolsum = thiscolsum; thiscolsum = nextcolsum;
|
|
4957 }
|
|
4958
|
|
4959 /* Special case for last column */
|
|
4960 outptr[outptr_offset++] = cast(byte) ((thiscolsum * 3 + lastcolsum + 8) >> 4);
|
|
4961 outptr[outptr_offset++] = cast(byte) ((thiscolsum * 4 + 7) >> 4);
|
|
4962 }
|
|
4963 inrow++;
|
|
4964 }
|
|
4965 }
|
|
4966
|
|
4967 static void int_upsample (jpeg_decompress_struct cinfo, jpeg_component_info compptr,
|
|
4968 byte[][] input_data, int input_data_offset, byte[][][] output_data_ptr, int[] output_data_offset, int output_data_index)
|
|
4969 {
|
|
4970 jpeg_upsampler upsample = cinfo.upsample;
|
|
4971 byte[][] output_data = output_data_ptr[output_data_index];
|
|
4972 byte[] inptr, outptr;
|
|
4973 byte invalue;
|
|
4974 int h;
|
|
4975 int outend;
|
|
4976 int h_expand, v_expand;
|
|
4977 int inrow, outrow;
|
|
4978 output_data_offset[output_data_index] = 0;
|
|
4979
|
|
4980 h_expand = upsample.h_expand[compptr.component_index];
|
|
4981 v_expand = upsample.v_expand[compptr.component_index];
|
|
4982
|
|
4983 inrow = outrow = 0;
|
|
4984 while (outrow < cinfo.max_v_samp_factor) {
|
|
4985 /* Generate one output row with proper horizontal expansion */
|
|
4986 inptr = input_data[inrow+input_data_offset];
|
|
4987 int inptr_offset = 0;
|
|
4988 outptr = output_data[outrow];
|
|
4989 int outptr_offset = 0;
|
|
4990 outend = outptr_offset + cinfo.output_width;
|
|
4991 while (outptr_offset < outend) {
|
|
4992 invalue = inptr[inptr_offset++]; /* don't need GETJSAMPLE() here */
|
|
4993 for (h = h_expand; h > 0; h--) {
|
|
4994 outptr[outptr_offset++] = invalue;
|
|
4995 }
|
|
4996 }
|
|
4997 /* Generate any additional output rows by duplicating the first one */
|
|
4998 if (v_expand > 1) {
|
|
4999 jcopy_sample_rows(output_data, outrow, output_data, outrow+1, v_expand-1, cinfo.output_width);
|
|
5000 }
|
|
5001 inrow++;
|
|
5002 outrow += v_expand;
|
|
5003 }
|
|
5004 }
|
|
5005
|
|
5006 static void null_convert (jpeg_decompress_struct cinfo,
|
|
5007 byte[][][] input_buf, int[] input_buf_offset, int input_row,
|
|
5008 byte[][] output_buf, int output_buf_offset, int num_rows)
|
|
5009 {
|
|
5010 byte[] inptr, outptr;
|
|
5011 int count;
|
|
5012 int num_components = cinfo.num_components;
|
|
5013 int num_cols = cinfo.output_width;
|
|
5014 int ci;
|
|
5015
|
|
5016 while (--num_rows >= 0) {
|
|
5017 for (ci = 0; ci < num_components; ci++) {
|
|
5018 inptr = input_buf[ci][input_row+input_buf_offset[0]];
|
|
5019 outptr = output_buf[output_buf_offset];
|
|
5020 /* BGR instead of RGB */
|
|
5021 int offset = 0;
|
|
5022 switch (ci) {
|
|
5023 case 2: offset = RGB_BLUE; break;
|
|
5024 case 1: offset = RGB_GREEN; break;
|
|
5025 case 0: offset = RGB_RED; break;
|
|
5026 default:
|
|
5027 }
|
|
5028 int outptr_offset = offset, inptr_offset = 0;
|
|
5029 for (count = num_cols; count > 0; count--) {
|
|
5030 outptr[outptr_offset] = inptr[inptr_offset++]; /* needn't bother with GETJSAMPLE() here */
|
|
5031 outptr_offset += num_components;
|
|
5032 }
|
|
5033 }
|
|
5034 input_row++;
|
|
5035 output_buf_offset++;
|
|
5036 }
|
|
5037 }
|
|
5038
|
|
5039 static void grayscale_convert (jpeg_decompress_struct cinfo,
|
|
5040 byte[][][] input_buf, int[] input_buf_offset, int input_row,
|
|
5041 byte[][] output_buf, int output_buf_offset, int num_rows)
|
|
5042 {
|
|
5043 jcopy_sample_rows(input_buf[0], input_row+input_buf_offset[0], output_buf, output_buf_offset,
|
|
5044 num_rows, cinfo.output_width);
|
|
5045 }
|
|
5046
|
|
5047 static void gray_rgb_convert (jpeg_decompress_struct cinfo,
|
|
5048 byte[][][] input_buf, int[] input_buf_offset, int input_row,
|
|
5049 byte[][] output_buf, int output_buf_offset, int num_rows)
|
|
5050 {
|
|
5051 byte[] inptr, outptr;
|
|
5052 int col;
|
|
5053 int num_cols = cinfo.output_width;
|
|
5054
|
|
5055 while (--num_rows >= 0) {
|
|
5056 inptr = input_buf[0][input_row+++input_buf_offset[0]];
|
|
5057 outptr = output_buf[output_buf_offset++];
|
|
5058 int outptr_offset = 0;
|
|
5059 for (col = 0; col < num_cols; col++) {
|
|
5060 /* We can dispense with GETJSAMPLE() here */
|
|
5061 outptr[RGB_RED+outptr_offset] = outptr[RGB_GREEN+outptr_offset] = outptr[RGB_BLUE+outptr_offset] = inptr[col];
|
|
5062 outptr_offset += RGB_PIXELSIZE;
|
|
5063 }
|
|
5064 }
|
|
5065 }
|
|
5066
|
|
5067 static void ycc_rgb_convert (jpeg_decompress_struct cinfo,
|
|
5068 byte[][][] input_buf, int[] input_buf_offset, int input_row,
|
|
5069 byte[][] output_buf, int output_buf_offset, int num_rows)
|
|
5070 {
|
|
5071 jpeg_color_deconverter cconvert = cinfo.cconvert;
|
|
5072 int y, cb, cr;
|
|
5073 byte[] outptr;
|
|
5074 byte[] inptr0, inptr1, inptr2;
|
|
5075 int col;
|
|
5076 int num_cols = cinfo.output_width;
|
|
5077 /* copy these pointers into registers if possible */
|
|
5078 byte[] range_limit = cinfo.sample_range_limit;
|
|
5079 int range_limit_offset = cinfo.sample_range_limit_offset;
|
|
5080 int[] Crrtab = cconvert.Cr_r_tab;
|
|
5081 int[] Cbbtab = cconvert.Cb_b_tab;
|
|
5082 int[] Crgtab = cconvert.Cr_g_tab;
|
|
5083 int[] Cbgtab = cconvert.Cb_g_tab;
|
|
5084 // SHIFT_TEMPS
|
|
5085
|
|
5086 while (--num_rows >= 0) {
|
|
5087 inptr0 = input_buf[0][input_row+input_buf_offset[0]];
|
|
5088 inptr1 = input_buf[1][input_row+input_buf_offset[1]];
|
|
5089 inptr2 = input_buf[2][input_row+input_buf_offset[2]];
|
|
5090 input_row++;
|
|
5091 outptr = output_buf[output_buf_offset++];
|
|
5092 int outptr_offset = 0;
|
|
5093 for (col = 0; col < num_cols; col++) {
|
|
5094 y = (inptr0[col] & 0xFF);
|
|
5095 cb = (inptr1[col] & 0xFF);
|
|
5096 cr = (inptr2[col] & 0xFF);
|
|
5097 /* Range-limiting is essential due to noise introduced by DCT losses. */
|
|
5098 outptr[outptr_offset + RGB_RED] = range_limit[y + Crrtab[cr] + range_limit_offset];
|
|
5099 outptr[outptr_offset + RGB_GREEN] = range_limit[y + ((Cbgtab[cb] + Crgtab[cr]>>SCALEBITS)) + range_limit_offset];
|
|
5100 outptr[outptr_offset + RGB_BLUE] = range_limit[y + Cbbtab[cb] + range_limit_offset];
|
|
5101 outptr_offset += RGB_PIXELSIZE;
|
|
5102 }
|
|
5103 }
|
|
5104 }
|
|
5105
|
|
5106 static bool process_APPn(int n, jpeg_decompress_struct cinfo) {
|
|
5107 if (n is 0 || n is 14) {
|
|
5108 return get_interesting_appn(cinfo);
|
|
5109 }
|
|
5110 return skip_variable(cinfo);
|
|
5111 }
|
|
5112
|
|
5113 static bool process_COM(jpeg_decompress_struct cinfo) {
|
|
5114 return skip_variable(cinfo);
|
|
5115 }
|
|
5116
|
|
5117 static void skip_input_data (jpeg_decompress_struct cinfo, int num_bytes) {
|
|
5118 if (num_bytes > 0) {
|
|
5119 while (num_bytes > cinfo.bytes_in_buffer - cinfo.bytes_offset) {
|
|
5120 num_bytes -= cinfo.bytes_in_buffer - cinfo.bytes_offset;
|
|
5121 if (!fill_input_buffer(cinfo)) error();
|
|
5122 /* note we assume that fill_input_buffer will never return FALSE,
|
|
5123 * so suspension need not be handled.
|
|
5124 */
|
|
5125 }
|
|
5126 cinfo.bytes_offset += num_bytes;
|
|
5127 }
|
|
5128 }
|
|
5129
|
|
5130 static bool skip_variable (jpeg_decompress_struct cinfo)
|
|
5131 /* Skip over an unknown or uninteresting variable-length marker */
|
|
5132 {
|
|
5133 int length;
|
|
5134
|
|
5135 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5136 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
5137 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5138 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5139
|
|
5140 length -= 2;
|
|
5141
|
|
5142 // TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo.unread_marker, cast(int) length);
|
|
5143
|
|
5144 if (length > 0) {
|
|
5145 skip_input_data (cinfo, length);
|
|
5146 }
|
|
5147
|
|
5148 return true;
|
|
5149 }
|
|
5150
|
|
5151 static bool get_interesting_appn (jpeg_decompress_struct cinfo)
|
|
5152 /* Process an APP0 or APP14 marker without saving it */
|
|
5153 {
|
|
5154 int length;
|
|
5155 byte[] b = new byte[APPN_DATA_LEN];
|
|
5156 int i, numtoread;
|
|
5157
|
|
5158 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5159 length = (cinfo.buffer[cinfo.bytes_offset++] & 0xFF) << 8;
|
|
5160 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5161 length |= cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5162 length -= 2;
|
|
5163
|
|
5164 /* get the interesting part of the marker data */
|
|
5165 if (length >= APPN_DATA_LEN)
|
|
5166 numtoread = APPN_DATA_LEN;
|
|
5167 else if (length > 0)
|
|
5168 numtoread = length;
|
|
5169 else
|
|
5170 numtoread = 0;
|
|
5171 for (i = 0; i < numtoread; i++) {
|
|
5172 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5173 b[i] = cinfo.buffer[cinfo.bytes_offset++];
|
|
5174 }
|
|
5175 length -= numtoread;
|
|
5176
|
|
5177 /* process it */
|
|
5178 switch (cinfo.unread_marker) {
|
|
5179 case M_APP0:
|
|
5180 examine_app0(cinfo, b, numtoread, length);
|
|
5181 break;
|
|
5182 case M_APP14:
|
|
5183 examine_app14(cinfo, b, numtoread, length);
|
|
5184 break;
|
|
5185 default:
|
|
5186 /* can't get here unless jpeg_save_markers chooses wrong processor */
|
|
5187 error();
|
|
5188 // ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo.unread_marker);
|
|
5189 break;
|
|
5190 }
|
|
5191
|
|
5192 /* skip any remaining data -- could be lots */
|
|
5193 if (length > 0)
|
|
5194 skip_input_data (cinfo, length);
|
|
5195
|
|
5196 return true;
|
|
5197 }
|
|
5198
|
|
5199 static void examine_app0 (jpeg_decompress_struct cinfo, byte[] data, int datalen, int remaining)
|
|
5200 /* Examine first few bytes from an APP0.
|
|
5201 * Take appropriate action if it is a JFIF marker.
|
|
5202 * datalen is # of bytes at data[], remaining is length of rest of marker data.
|
|
5203 */
|
|
5204 {
|
|
5205 int totallen = datalen + remaining;
|
|
5206
|
|
5207 if (datalen >= APP0_DATA_LEN &&
|
|
5208 (data[0] & 0xFF) is 0x4A &&
|
|
5209 (data[1] & 0xFF) is 0x46 &&
|
|
5210 (data[2] & 0xFF) is 0x49 &&
|
|
5211 (data[3] & 0xFF) is 0x46 &&
|
|
5212 (data[4] & 0xFF) is 0)
|
|
5213 {
|
|
5214 /* Found JFIF APP0 marker: save info */
|
|
5215 cinfo.saw_JFIF_marker = true;
|
|
5216 cinfo.JFIF_major_version = (data[5]);
|
|
5217 cinfo.JFIF_minor_version = cast(byte)(data[6] & 0xFF);
|
|
5218 cinfo.density_unit = cast(byte)(data[7] & 0xFF);
|
|
5219 cinfo.X_density = cast(short)(((data[8] & 0xFF) << 8) + (data[9] & 0xFF));
|
|
5220 cinfo.Y_density = cast(short)(((data[10] & 0xFF) << 8) + (data[11] & 0xFF));
|
|
5221 /* Check version.
|
|
5222 * Major version must be 1, anything else signals an incompatible change.
|
|
5223 * (We used to treat this as an error, but now it's a nonfatal warning,
|
|
5224 * because some bozo at Hijaak couldn't read the spec.)
|
|
5225 * Minor version should be 0..2, but process anyway if newer.
|
|
5226 */
|
|
5227 if (cinfo.JFIF_major_version !is 1) {
|
|
5228 // WARNMS2(cinfo, JWRN_JFIF_MAJOR,
|
|
5229 // cinfo.JFIF_major_version, cinfo.JFIF_minor_version);
|
|
5230 }
|
|
5231 /* Generate trace messages */
|
|
5232 // TRACEMS5(cinfo, 1, JTRC_JFIF,
|
|
5233 // cinfo.JFIF_major_version, cinfo.JFIF_minor_version,
|
|
5234 // cinfo.X_density, cinfo.Y_density, cinfo.density_unit);
|
|
5235 /* Validate thumbnail dimensions and issue appropriate messages */
|
|
5236 if (((data[12] & 0xFF) | (data[13]) & 0xFF) !is 0) {
|
|
5237 // TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
|
|
5238 // GETJOCTET(data[12]), GETJOCTET(data[13]));
|
|
5239 }
|
|
5240 totallen -= APP0_DATA_LEN;
|
|
5241 if (totallen !is ((data[12] & 0xFF) * (data[13] & 0xFF) * 3)) {
|
|
5242 // TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, cast(int) totallen);
|
|
5243 }
|
|
5244 } else if (datalen >= 6 &&
|
|
5245 (data[0] & 0xFF) is 0x4A &&
|
|
5246 (data[1] & 0xFF) is 0x46 &&
|
|
5247 (data[2] & 0xFF) is 0x58 &&
|
|
5248 (data[3] & 0xFF) is 0x58 &&
|
|
5249 (data[4] & 0xFF) is 0)
|
|
5250 {
|
|
5251 /* Found JFIF "JFXX" extension APP0 marker */
|
|
5252 /* The library doesn't actually do anything with these,
|
|
5253 * but we try to produce a helpful trace message.
|
|
5254 */
|
|
5255 switch ((data[5]) & 0xFF) {
|
|
5256 case 0x10:
|
|
5257 // TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, cast(int) totallen);
|
|
5258 break;
|
|
5259 case 0x11:
|
|
5260 // TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, cast(int) totallen);
|
|
5261 break;
|
|
5262 case 0x13:
|
|
5263 // TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, cast(int) totallen);
|
|
5264 break;
|
|
5265 default:
|
|
5266 // TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION, GETJOCTET(data[5]), cast(int) totallen);
|
|
5267 break;
|
|
5268 }
|
|
5269 } else {
|
|
5270 /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
|
|
5271 // TRACEMS1(cinfo, 1, JTRC_APP0, cast(int) totallen);
|
|
5272 }
|
|
5273 }
|
|
5274
|
|
5275 static void examine_app14 (jpeg_decompress_struct cinfo, byte[] data, int datalen, int remaining)
|
|
5276 /* Examine first few bytes from an APP14.
|
|
5277 * Take appropriate action if it is an Adobe marker.
|
|
5278 * datalen is # of bytes at data[], remaining is length of rest of marker data.
|
|
5279 */
|
|
5280 {
|
|
5281 int /*version, flags0, flags1, */transform;
|
|
5282
|
|
5283 if (datalen >= APP14_DATA_LEN &&
|
|
5284 (data[0] & 0xFF) is 0x41 &&
|
|
5285 (data[1] & 0xFF) is 0x64 &&
|
|
5286 (data[2] & 0xFF) is 0x6F &&
|
|
5287 (data[3] & 0xFF) is 0x62 &&
|
|
5288 (data[4] & 0xFF) is 0x65)
|
|
5289 {
|
|
5290 /* Found Adobe APP14 marker */
|
|
5291 // version = ((data[5] & 0xFF) << 8) + (data[6] & 0xFF);
|
|
5292 // flags0 = ((data[7] & 0xFF) << 8) + (data[8] & 0xFF);
|
|
5293 // flags1 = ((data[9] & 0xFF) << 8) + (data[10] & 0xFF);
|
|
5294 transform = (data[11] & 0xFF);
|
|
5295 // TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
|
|
5296 cinfo.saw_Adobe_marker = true;
|
|
5297 cinfo.Adobe_transform = cast(byte) transform;
|
|
5298 } else {
|
|
5299 /* Start of APP14 does not match "Adobe", or too short */
|
|
5300 // TRACEMS1(cinfo, 1, JTRC_APP14, cast(int) (datalen + remaining));
|
|
5301 }
|
|
5302 }
|
|
5303
|
|
5304 static bool get_soi (jpeg_decompress_struct cinfo) /* Process an SOI marker */ {
|
|
5305 int i;
|
|
5306
|
|
5307 // TRACEMS(cinfo, 1, JTRC_SOI);
|
|
5308
|
|
5309 if (cinfo.marker.saw_SOI)
|
|
5310 error();
|
|
5311 // ERREXIT(cinfo, JERR_SOI_DUPLICATE);
|
|
5312
|
|
5313 /* Reset all parameters that are defined to be reset by SOI */
|
|
5314
|
|
5315 for (i = 0; i < NUM_ARITH_TBLS; i++) {
|
|
5316 cinfo.arith_dc_L[i] = 0;
|
|
5317 cinfo.arith_dc_U[i] = 1;
|
|
5318 cinfo.arith_ac_K[i] = 5;
|
|
5319 }
|
|
5320 cinfo.restart_interval = 0;
|
|
5321
|
|
5322 /* Set initial assumptions for colorspace etc */
|
|
5323
|
|
5324 cinfo.jpeg_color_space = JCS_UNKNOWN;
|
|
5325 cinfo.CCIR601_sampling = false; /* Assume non-CCIR sampling??? */
|
|
5326
|
|
5327 cinfo.saw_JFIF_marker = false;
|
|
5328 cinfo.JFIF_major_version = 1; /* set default JFIF APP0 values */
|
|
5329 cinfo.JFIF_minor_version = 1;
|
|
5330 cinfo.density_unit = 0;
|
|
5331 cinfo.X_density = 1;
|
|
5332 cinfo.Y_density = 1;
|
|
5333 cinfo.saw_Adobe_marker = false;
|
|
5334 cinfo.Adobe_transform = 0;
|
|
5335
|
|
5336 cinfo.marker.saw_SOI = true;
|
|
5337
|
|
5338 return true;
|
|
5339 }
|
|
5340
|
|
5341 static void jinit_input_controller (jpeg_decompress_struct cinfo)
|
|
5342 {
|
|
5343 /* Initialize state: can't use reset_input_controller since we don't
|
|
5344 * want to try to reset other modules yet.
|
|
5345 */
|
|
5346 jpeg_input_controller inputctl = cinfo.inputctl = new jpeg_input_controller();
|
|
5347 inputctl.has_multiple_scans = false; /* "unknown" would be better */
|
|
5348 inputctl.eoi_reached = false;
|
|
5349 inputctl.inheaders = true;
|
|
5350 }
|
|
5351
|
|
5352 static void reset_marker_reader (jpeg_decompress_struct cinfo) {
|
|
5353 jpeg_marker_reader marker = cinfo.marker;
|
|
5354
|
|
5355 cinfo.comp_info = null; /* until allocated by get_sof */
|
|
5356 cinfo.input_scan_number = 0; /* no SOS seen yet */
|
|
5357 cinfo.unread_marker = 0; /* no pending marker */
|
|
5358 marker.saw_SOI = false; /* set internal state too */
|
|
5359 marker.saw_SOF = false;
|
|
5360 marker.discarded_bytes = 0;
|
|
5361 // marker.cur_marker = null;
|
|
5362 }
|
|
5363
|
|
5364 static void reset_input_controller (jpeg_decompress_struct cinfo) {
|
|
5365 jpeg_input_controller inputctl = cinfo.inputctl;
|
|
5366
|
|
5367 inputctl.has_multiple_scans = false; /* "unknown" would be better */
|
|
5368 inputctl.eoi_reached = false;
|
|
5369 inputctl.inheaders = true;
|
|
5370 /* Reset other modules */
|
|
5371 reset_marker_reader (cinfo);
|
|
5372 /* Reset progression state -- would be cleaner if entropy decoder did this */
|
|
5373 cinfo.coef_bits = null;
|
|
5374 }
|
|
5375
|
|
5376 static void finish_output_pass (jpeg_decompress_struct cinfo) {
|
|
5377 jpeg_decomp_master master = cinfo.master;
|
|
5378
|
|
5379 if (cinfo.quantize_colors) {
|
|
5380 error(SWT.ERROR_NOT_IMPLEMENTED);
|
|
5381 // (*cinfo.cquantize.finish_pass) (cinfo);
|
|
5382 }
|
|
5383 master.pass_number++;
|
|
5384 }
|
|
5385
|
|
5386 static void jpeg_destroy (jpeg_decompress_struct cinfo) {
|
|
5387 /* We need only tell the memory manager to release everything. */
|
|
5388 /* NB: mem pointer is NULL if memory mgr failed to initialize. */
|
|
5389 // if (cinfo.mem !is NULL)
|
|
5390 // (*cinfo.mem.self_destruct) (cinfo);
|
|
5391 // cinfo.mem = NULL; /* be safe if jpeg_destroy is called twice */
|
|
5392 cinfo.global_state = 0; /* mark it destroyed */
|
|
5393 }
|
|
5394
|
|
5395 static void jpeg_destroy_decompress (jpeg_decompress_struct cinfo) {
|
|
5396 jpeg_destroy(cinfo); /* use common routine */
|
|
5397 }
|
|
5398
|
|
5399 static bool jpeg_input_complete (jpeg_decompress_struct cinfo) {
|
|
5400 /* Check for valid jpeg object */
|
|
5401 if (cinfo.global_state < DSTATE_START || cinfo.global_state > DSTATE_STOPPING)
|
|
5402 error();
|
|
5403 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
5404 return cinfo.inputctl.eoi_reached;
|
|
5405 }
|
|
5406
|
|
5407 static bool jpeg_start_output (jpeg_decompress_struct cinfo, int scan_number) {
|
|
5408 if (cinfo.global_state !is DSTATE_BUFIMAGE && cinfo.global_state !is DSTATE_PRESCAN)
|
|
5409 error();
|
|
5410 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
5411 /* Limit scan number to valid range */
|
|
5412 if (scan_number <= 0)
|
|
5413 scan_number = 1;
|
|
5414 if (cinfo.inputctl.eoi_reached && scan_number > cinfo.input_scan_number)
|
|
5415 scan_number = cinfo.input_scan_number;
|
|
5416 cinfo.output_scan_number = scan_number;
|
|
5417 /* Perform any dummy output passes, and set up for the real pass */
|
|
5418 return output_pass_setup(cinfo);
|
|
5419 }
|
|
5420
|
|
5421 static bool jpeg_finish_output (jpeg_decompress_struct cinfo) {
|
|
5422 if ((cinfo.global_state is DSTATE_SCANNING || cinfo.global_state is DSTATE_RAW_OK) && cinfo.buffered_image) {
|
|
5423 /* Terminate this pass. */
|
|
5424 /* We do not require the whole pass to have been completed. */
|
|
5425 finish_output_pass (cinfo);
|
|
5426 cinfo.global_state = DSTATE_BUFPOST;
|
|
5427 } else if (cinfo.global_state !is DSTATE_BUFPOST) {
|
|
5428 /* BUFPOST = repeat call after a suspension, anything else is error */
|
|
5429 error();
|
|
5430 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
5431 }
|
|
5432 /* Read markers looking for SOS or EOI */
|
|
5433 while (cinfo.input_scan_number <= cinfo.output_scan_number && !cinfo.inputctl.eoi_reached) {
|
|
5434 if (consume_input (cinfo) is JPEG_SUSPENDED)
|
|
5435 return false; /* Suspend, come back later */
|
|
5436 }
|
|
5437 cinfo.global_state = DSTATE_BUFIMAGE;
|
|
5438 return true;
|
|
5439 }
|
|
5440
|
|
5441 static bool jpeg_finish_decompress (jpeg_decompress_struct cinfo) {
|
|
5442 if ((cinfo.global_state is DSTATE_SCANNING || cinfo.global_state is DSTATE_RAW_OK) && ! cinfo.buffered_image) {
|
|
5443 /* Terminate final pass of non-buffered mode */
|
|
5444 if (cinfo.output_scanline < cinfo.output_height)
|
|
5445 error();
|
|
5446 // ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
|
|
5447 finish_output_pass (cinfo);
|
|
5448 cinfo.global_state = DSTATE_STOPPING;
|
|
5449 } else if (cinfo.global_state is DSTATE_BUFIMAGE) {
|
|
5450 /* Finishing after a buffered-image operation */
|
|
5451 cinfo.global_state = DSTATE_STOPPING;
|
|
5452 } else if (cinfo.global_state !is DSTATE_STOPPING) {
|
|
5453 /* STOPPING = repeat call after a suspension, anything else is error */
|
|
5454 error();
|
|
5455 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
5456 }
|
|
5457 /* Read until EOI */
|
|
5458 while (! cinfo.inputctl.eoi_reached) {
|
|
5459 if (consume_input (cinfo) is JPEG_SUSPENDED)
|
|
5460 return false; /* Suspend, come back later */
|
|
5461 }
|
|
5462 /* Do final cleanup */
|
|
5463 // (*cinfo.src.term_source) (cinfo);
|
|
5464 /* We can use jpeg_abort to release memory and reset global_state */
|
|
5465 jpeg_abort(cinfo);
|
|
5466 return true;
|
|
5467 }
|
|
5468
|
|
5469
|
|
5470 static int jpeg_read_header (jpeg_decompress_struct cinfo, bool require_image) {
|
|
5471 int retcode;
|
|
5472
|
|
5473 if (cinfo.global_state !is DSTATE_START && cinfo.global_state !is DSTATE_INHEADER)
|
|
5474 error();
|
|
5475 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
5476
|
|
5477 retcode = jpeg_consume_input(cinfo);
|
|
5478
|
|
5479 switch (retcode) {
|
|
5480 case JPEG_REACHED_SOS:
|
|
5481 retcode = JPEG_HEADER_OK;
|
|
5482 break;
|
|
5483 case JPEG_REACHED_EOI:
|
|
5484 if (require_image) /* Complain if application wanted an image */
|
|
5485 error();
|
|
5486 // ERREXIT(cinfo, JERR_NO_IMAGE);
|
|
5487 /* Reset to start state; it would be safer to require the application to
|
|
5488 * call jpeg_abort, but we can't change it now for compatibility reasons.
|
|
5489 * A side effect is to free any temporary memory (there shouldn't be any).
|
|
5490 */
|
|
5491 jpeg_abort(cinfo); /* sets state = DSTATE_START */
|
|
5492 retcode = JPEG_HEADER_TABLES_ONLY;
|
|
5493 break;
|
|
5494 case JPEG_SUSPENDED:
|
|
5495 /* no work */
|
|
5496 break;
|
|
5497 default:
|
|
5498 }
|
|
5499
|
|
5500 return retcode;
|
|
5501 }
|
|
5502
|
|
5503 static int dummy_consume_data (jpeg_decompress_struct cinfo) {
|
|
5504 return JPEG_SUSPENDED; /* Always indicate nothing was done */
|
|
5505 }
|
|
5506
|
|
5507 static int consume_data (jpeg_decompress_struct cinfo) {
|
|
5508 jpeg_d_coef_controller coef = cinfo.coef;
|
|
5509 int MCU_col_num; /* index of current MCU within row */
|
|
5510 int blkn, ci, xindex, yindex, yoffset;
|
|
5511 int start_col;
|
|
5512 // short[][][][] buffer = new short[MAX_COMPS_IN_SCAN][][][];
|
|
5513 short[][] buffer_ptr;
|
|
5514 jpeg_component_info compptr;
|
|
5515
|
|
5516 // /* Align the virtual buffers for the components used in this scan. */
|
|
5517 // for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
5518 // compptr = cinfo.cur_comp_info[ci];
|
|
5519 // buffer[ci] = coef.whole_image[compptr.component_index];
|
|
5520 // /* Note: entropy decoder expects buffer to be zeroed,
|
|
5521 // * but this is handled automatically by the memory manager
|
|
5522 // * because we requested a pre-zeroed array.
|
|
5523 // */
|
|
5524 // }
|
|
5525
|
|
5526 /* Loop to process one whole iMCU row */
|
|
5527 for (yoffset = coef.MCU_vert_offset; yoffset < coef.MCU_rows_per_iMCU_row; yoffset++) {
|
|
5528 for (MCU_col_num = coef.MCU_ctr; MCU_col_num < cinfo.MCUs_per_row; MCU_col_num++) {
|
|
5529 /* Construct list of pointers to DCT blocks belonging to this MCU */
|
|
5530 blkn = 0; /* index of current DCT block within MCU */
|
|
5531 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
5532 compptr = cinfo.cur_comp_info[ci];
|
|
5533 start_col = MCU_col_num * compptr.MCU_width;
|
|
5534 for (yindex = 0; yindex < compptr.MCU_height; yindex++) {
|
|
5535 // buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
|
|
5536 buffer_ptr = coef.whole_image[compptr.component_index][yindex+yoffset+cinfo.input_iMCU_row*compptr.v_samp_factor];
|
|
5537 int buffer_ptr_offset = start_col;
|
|
5538 for (xindex = 0; xindex < compptr.MCU_width; xindex++) {
|
|
5539 coef.MCU_buffer[blkn++] = buffer_ptr[buffer_ptr_offset++];
|
|
5540 }
|
|
5541 }
|
|
5542 }
|
|
5543 /* Try to fetch the MCU. */
|
|
5544 if (! cinfo.entropy.decode_mcu (cinfo, coef.MCU_buffer)) {
|
|
5545 /* Suspension forced; update state counters and exit */
|
|
5546 coef.MCU_vert_offset = yoffset;
|
|
5547 coef.MCU_ctr = MCU_col_num;
|
|
5548 return JPEG_SUSPENDED;
|
|
5549 }
|
|
5550 }
|
|
5551 /* Completed an MCU row, but perhaps not an iMCU row */
|
|
5552 coef.MCU_ctr = 0;
|
|
5553 }
|
|
5554 /* Completed the iMCU row, advance counters for next one */
|
|
5555 if (++(cinfo.input_iMCU_row) < cinfo.total_iMCU_rows) {
|
|
5556 coef.start_iMCU_row(cinfo);
|
|
5557 return JPEG_ROW_COMPLETED;
|
|
5558 }
|
|
5559 /* Completed the scan */
|
|
5560 finish_input_pass (cinfo);
|
|
5561 return JPEG_SCAN_COMPLETED;
|
|
5562 }
|
|
5563
|
|
5564 static int consume_input (jpeg_decompress_struct cinfo) {
|
|
5565 switch (cinfo.inputctl.consume_input) {
|
|
5566 case COEF_CONSUME_INPUT:
|
|
5567 switch (cinfo.coef.consume_data) {
|
|
5568 case CONSUME_DATA: return consume_data(cinfo);
|
|
5569 case DUMMY_CONSUME_DATA: return dummy_consume_data(cinfo);
|
|
5570 default: error();
|
|
5571 }
|
|
5572 break;
|
|
5573 case INPUT_CONSUME_INPUT:
|
|
5574 return consume_markers(cinfo);
|
|
5575 default:
|
|
5576 error();
|
|
5577 }
|
|
5578 return 0;
|
|
5579 }
|
|
5580
|
|
5581 static bool fill_input_buffer(jpeg_decompress_struct cinfo) {
|
|
5582 try {
|
|
5583 InputStream inputStream = cinfo.inputStream;
|
|
5584 int nbytes = inputStream.read(cinfo.buffer);
|
|
5585 if (nbytes <= 0) {
|
|
5586 if (cinfo.start_of_file) /* Treat empty input file as fatal error */
|
|
5587 error();
|
|
5588 // ERREXIT(cinfo, JERR_INPUT_EMPTY);
|
|
5589 // WARNMS(cinfo, JWRN_JPEG_EOF);
|
|
5590 /* Insert a fake EOI marker */
|
|
5591 cinfo.buffer[0] = cast(byte)0xFF;
|
|
5592 cinfo.buffer[1] = cast(byte)M_EOI;
|
|
5593 nbytes = 2;
|
|
5594 }
|
|
5595 cinfo.bytes_in_buffer = nbytes;
|
|
5596 cinfo.bytes_offset = 0;
|
|
5597 cinfo.start_of_file = false;
|
|
5598 } catch (IOException e) {
|
|
5599 error(SWT.ERROR_IO);
|
|
5600 return false;
|
|
5601 }
|
|
5602 return true;
|
|
5603 }
|
|
5604
|
|
5605 static bool first_marker (jpeg_decompress_struct cinfo) {
|
|
5606 /* Like next_marker, but used to obtain the initial SOI marker. */
|
|
5607 /* For this marker, we do not allow preceding garbage or fill; otherwise,
|
|
5608 * we might well scan an entire input file before realizing it ain't JPEG.
|
|
5609 * If an application wants to process non-JFIF files, it must seek to the
|
|
5610 * SOI before calling the JPEG library.
|
|
5611 */
|
|
5612 int c, c2;
|
|
5613
|
|
5614 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5615 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5616 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5617 c2 = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5618 if (c !is 0xFF || c2 !is M_SOI)
|
|
5619 error();
|
|
5620 // ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
|
|
5621
|
|
5622 cinfo.unread_marker = c2;
|
|
5623
|
|
5624 return true;
|
|
5625 }
|
|
5626
|
|
5627 static bool next_marker (jpeg_decompress_struct cinfo) {
|
|
5628 int c;
|
|
5629
|
|
5630 for (;;) {
|
|
5631 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5632 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5633 /* Skip any non-FF bytes.
|
|
5634 * This may look a bit inefficient, but it will not occur in a valid file.
|
|
5635 * We sync after each discarded byte so that a suspending data source
|
|
5636 * can discard the byte from its buffer.
|
|
5637 */
|
|
5638 while (c !is 0xFF) {
|
|
5639 cinfo.marker.discarded_bytes++;
|
|
5640 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5641 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5642 }
|
|
5643 /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
|
|
5644 * pad bytes, so don't count them in discarded_bytes. We assume there
|
|
5645 * will not be so many consecutive FF bytes as to overflow a suspending
|
|
5646 * data source's input buffer.
|
|
5647 */
|
|
5648 do {
|
|
5649 if (cinfo.bytes_offset is cinfo.bytes_in_buffer) fill_input_buffer(cinfo);
|
|
5650 c = cinfo.buffer[cinfo.bytes_offset++] & 0xFF;
|
|
5651 } while (c is 0xFF);
|
|
5652 if (c !is 0)
|
|
5653 break; /* found a valid marker, exit loop */
|
|
5654 /* Reach here if we found a stuffed-zero data sequence (FF/00).
|
|
5655 * Discard it and loop back to try again.
|
|
5656 */
|
|
5657 cinfo.marker.discarded_bytes += 2;
|
|
5658 }
|
|
5659
|
|
5660 if (cinfo.marker.discarded_bytes !is 0) {
|
|
5661 // WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo.marker.discarded_bytes, c);
|
|
5662 cinfo.marker.discarded_bytes = 0;
|
|
5663 }
|
|
5664
|
|
5665 cinfo.unread_marker = c;
|
|
5666
|
|
5667 return true;
|
|
5668 }
|
|
5669
|
|
5670 static int read_markers (jpeg_decompress_struct cinfo) {
|
|
5671 /* Outer loop repeats once for each marker. */
|
|
5672 for (;;) {
|
|
5673 /* Collect the marker proper, unless we already did. */
|
|
5674 /* NB: first_marker() enforces the requirement that SOI appear first. */
|
|
5675 if (cinfo.unread_marker is 0) {
|
|
5676 if (! cinfo.marker.saw_SOI) {
|
|
5677 if (! first_marker(cinfo))
|
|
5678 return JPEG_SUSPENDED;
|
|
5679 } else {
|
|
5680 if (! next_marker(cinfo))
|
|
5681 return JPEG_SUSPENDED;
|
|
5682 }
|
|
5683 }
|
|
5684 /* At this point cinfo.unread_marker contains the marker code and the
|
|
5685 * input point is just past the marker proper, but before any parameters.
|
|
5686 * A suspension will cause us to return with this state still true.
|
|
5687 */
|
|
5688 switch (cinfo.unread_marker) {
|
|
5689 case M_SOI:
|
|
5690 if (! get_soi(cinfo))
|
|
5691 return JPEG_SUSPENDED;
|
|
5692 break;
|
|
5693
|
|
5694 case M_SOF0: /* Baseline */
|
|
5695 case M_SOF1: /* Extended sequential, Huffman */
|
|
5696 if (! get_sof(cinfo, false, false))
|
|
5697 return JPEG_SUSPENDED;
|
|
5698 break;
|
|
5699
|
|
5700 case M_SOF2: /* Progressive, Huffman */
|
|
5701 if (! get_sof(cinfo, true, false))
|
|
5702 return JPEG_SUSPENDED;
|
|
5703 break;
|
|
5704
|
|
5705 case M_SOF9: /* Extended sequential, arithmetic */
|
|
5706 if (! get_sof(cinfo, false, true))
|
|
5707 return JPEG_SUSPENDED;
|
|
5708 break;
|
|
5709
|
|
5710 case M_SOF10: /* Progressive, arithmetic */
|
|
5711 if (! get_sof(cinfo, true, true))
|
|
5712 return JPEG_SUSPENDED;
|
|
5713 break;
|
|
5714
|
|
5715 /* Currently unsupported SOFn types */
|
|
5716 case M_SOF3: /* Lossless, Huffman */
|
|
5717 case M_SOF5: /* Differential sequential, Huffman */
|
|
5718 case M_SOF6: /* Differential progressive, Huffman */
|
|
5719 case M_SOF7: /* Differential lossless, Huffman */
|
|
5720 case M_JPG: /* Reserved for JPEG extensions */
|
|
5721 case M_SOF11: /* Lossless, arithmetic */
|
|
5722 case M_SOF13: /* Differential sequential, arithmetic */
|
|
5723 case M_SOF14: /* Differential progressive, arithmetic */
|
|
5724 case M_SOF15: /* Differential lossless, arithmetic */
|
|
5725 error();
|
|
5726 // ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo.unread_marker);
|
|
5727 break;
|
|
5728
|
|
5729 case M_SOS:
|
|
5730 if (! get_sos(cinfo))
|
|
5731 return JPEG_SUSPENDED;
|
|
5732 cinfo.unread_marker = 0; /* processed the marker */
|
|
5733 return JPEG_REACHED_SOS;
|
|
5734
|
|
5735 case M_EOI:
|
|
5736 // TRACEMS(cinfo, 1, JTRC_EOI);
|
|
5737 cinfo.unread_marker = 0; /* processed the marker */
|
|
5738 return JPEG_REACHED_EOI;
|
|
5739
|
|
5740 case M_DAC:
|
|
5741 if (! get_dac(cinfo))
|
|
5742 return JPEG_SUSPENDED;
|
|
5743 break;
|
|
5744
|
|
5745 case M_DHT:
|
|
5746 if (! get_dht(cinfo))
|
|
5747 return JPEG_SUSPENDED;
|
|
5748 break;
|
|
5749
|
|
5750 case M_DQT:
|
|
5751 if (! get_dqt(cinfo))
|
|
5752 return JPEG_SUSPENDED;
|
|
5753 break;
|
|
5754
|
|
5755 case M_DRI:
|
|
5756 if (! get_dri(cinfo))
|
|
5757 return JPEG_SUSPENDED;
|
|
5758 break;
|
|
5759
|
|
5760 case M_APP0:
|
|
5761 case M_APP1:
|
|
5762 case M_APP2:
|
|
5763 case M_APP3:
|
|
5764 case M_APP4:
|
|
5765 case M_APP5:
|
|
5766 case M_APP6:
|
|
5767 case M_APP7:
|
|
5768 case M_APP8:
|
|
5769 case M_APP9:
|
|
5770 case M_APP10:
|
|
5771 case M_APP11:
|
|
5772 case M_APP12:
|
|
5773 case M_APP13:
|
|
5774 case M_APP14:
|
|
5775 case M_APP15:
|
|
5776 if (! process_APPn(cinfo.unread_marker - M_APP0, cinfo))
|
|
5777 return JPEG_SUSPENDED;
|
|
5778 break;
|
|
5779
|
|
5780 case M_COM:
|
|
5781 if (! process_COM(cinfo))
|
|
5782 return JPEG_SUSPENDED;
|
|
5783 break;
|
|
5784
|
|
5785 case M_RST0: /* these are all parameterless */
|
|
5786 case M_RST1:
|
|
5787 case M_RST2:
|
|
5788 case M_RST3:
|
|
5789 case M_RST4:
|
|
5790 case M_RST5:
|
|
5791 case M_RST6:
|
|
5792 case M_RST7:
|
|
5793 case M_TEM:
|
|
5794 // TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo.unread_marker);
|
|
5795 break;
|
|
5796
|
|
5797 case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
|
|
5798 if (! skip_variable(cinfo))
|
|
5799 return JPEG_SUSPENDED;
|
|
5800 break;
|
|
5801
|
|
5802 default: /* must be DHP, EXP, JPGn, or RESn */
|
|
5803 /* For now, we treat the reserved markers as fatal errors since they are
|
|
5804 * likely to be used to signal incompatible JPEG Part 3 extensions.
|
|
5805 * Once the JPEG 3 version-number marker is well defined, this code
|
|
5806 * ought to change!
|
|
5807 */
|
|
5808 error();
|
|
5809 // ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo.unread_marker);
|
|
5810 break;
|
|
5811 }
|
|
5812 /* Successfully processed marker, so reset state variable */
|
|
5813 cinfo.unread_marker = 0;
|
|
5814 } /* end loop */
|
|
5815 }
|
|
5816
|
|
5817 static long jdiv_round_up (long a, long b)
|
|
5818 /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
|
|
5819 /* Assumes a >= 0, b > 0 */
|
|
5820 {
|
|
5821 return (a + b - 1) / b;
|
|
5822 }
|
|
5823
|
|
5824 static void initial_setup (jpeg_decompress_struct cinfo)
|
|
5825 /* Called once, when first SOS marker is reached */
|
|
5826 {
|
|
5827 int ci;
|
|
5828 jpeg_component_info compptr;
|
|
5829
|
|
5830 /* Make sure image isn't bigger than I can handle */
|
|
5831 if (cinfo.image_height > JPEG_MAX_DIMENSION || cinfo.image_width > JPEG_MAX_DIMENSION)
|
|
5832 error();
|
|
5833 // ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
|
|
5834
|
|
5835 /* For now, precision must match compiled-in value... */
|
|
5836 if (cinfo.data_precision !is BITS_IN_JSAMPLE)
|
|
5837 error(" [data precision=" ~ to!(String)(cinfo.data_precision) ~ "]");
|
|
5838 // ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo.data_precision);
|
|
5839
|
|
5840 /* Check that number of components won't exceed internal array sizes */
|
|
5841 if (cinfo.num_components > MAX_COMPONENTS)
|
|
5842 error();
|
|
5843 // ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo.num_components, MAX_COMPONENTS);
|
|
5844
|
|
5845 /* Compute maximum sampling factors; check factor validity */
|
|
5846 cinfo.max_h_samp_factor = 1;
|
|
5847 cinfo.max_v_samp_factor = 1;
|
|
5848 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
5849 compptr = cinfo.comp_info[ci];
|
|
5850 if (compptr.h_samp_factor<=0 || compptr.h_samp_factor>MAX_SAMP_FACTOR || compptr.v_samp_factor<=0 || compptr.v_samp_factor>MAX_SAMP_FACTOR)
|
|
5851 error();
|
|
5852 // ERREXIT(cinfo, JERR_BAD_SAMPLING);
|
|
5853 cinfo.max_h_samp_factor = Math.max(cinfo.max_h_samp_factor, compptr.h_samp_factor);
|
|
5854 cinfo.max_v_samp_factor = Math.max(cinfo.max_v_samp_factor, compptr.v_samp_factor);
|
|
5855 }
|
|
5856
|
|
5857 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
|
|
5858 * In the full decompressor, this will be overridden by jdmaster.c;
|
|
5859 * but in the transcoder, jdmaster.c is not used, so we must do it here.
|
|
5860 */
|
|
5861 cinfo.min_DCT_scaled_size = DCTSIZE;
|
|
5862
|
|
5863 /* Compute dimensions of components */
|
|
5864 for (ci = 0; ci < cinfo.num_components; ci++) {
|
|
5865 compptr = cinfo.comp_info[ci];
|
|
5866 compptr.DCT_scaled_size = DCTSIZE;
|
|
5867 /* Size in DCT blocks */
|
|
5868 compptr.width_in_blocks = cast(int)jdiv_round_up(cast(long) cinfo.image_width * cast(long) compptr.h_samp_factor, (cinfo.max_h_samp_factor * DCTSIZE));
|
|
5869 compptr.height_in_blocks = cast(int)jdiv_round_up(cast(long) cinfo.image_height * cast(long) compptr.v_samp_factor, (cinfo.max_v_samp_factor * DCTSIZE));
|
|
5870 /* downsampled_width and downsampled_height will also be overridden by
|
|
5871 * jdmaster.c if we are doing full decompression. The transcoder library
|
|
5872 * doesn't use these values, but the calling application might.
|
|
5873 */
|
|
5874 /* Size in samples */
|
|
5875 compptr.downsampled_width = cast(int)jdiv_round_up(cast(long) cinfo.image_width * cast(long) compptr.h_samp_factor, cinfo.max_h_samp_factor);
|
|
5876 compptr.downsampled_height = cast(int)jdiv_round_up(cast(long) cinfo.image_height * cast(long) compptr.v_samp_factor, cinfo.max_v_samp_factor);
|
|
5877 /* Mark component needed, until color conversion says otherwise */
|
|
5878 compptr.component_needed = true;
|
|
5879 /* Mark no quantization table yet saved for component */
|
|
5880 compptr.quant_table = null;
|
|
5881 }
|
|
5882
|
|
5883 /* Compute number of fully interleaved MCU rows. */
|
|
5884 cinfo.total_iMCU_rows = cast(int)jdiv_round_up( cinfo.image_height, (cinfo.max_v_samp_factor*DCTSIZE));
|
|
5885
|
|
5886 /* Decide whether file contains multiple scans */
|
|
5887 if (cinfo.comps_in_scan < cinfo.num_components || cinfo.progressive_mode)
|
|
5888 cinfo.inputctl.has_multiple_scans = true;
|
|
5889 else
|
|
5890 cinfo.inputctl.has_multiple_scans = false;
|
|
5891 }
|
|
5892
|
|
5893
|
|
5894 static void per_scan_setup (jpeg_decompress_struct cinfo)
|
|
5895 /* Do computations that are needed before processing a JPEG scan */
|
|
5896 /* cinfo.comps_in_scan and cinfo.cur_comp_info[] were set from SOS marker */
|
|
5897 {
|
|
5898 int ci, mcublks, tmp = 0;
|
|
5899 jpeg_component_info compptr;
|
|
5900
|
|
5901 if (cinfo.comps_in_scan is 1) {
|
|
5902
|
|
5903 /* Noninterleaved (single-component) scan */
|
|
5904 compptr = cinfo.cur_comp_info[0];
|
|
5905
|
|
5906 /* Overall image size in MCUs */
|
|
5907 cinfo.MCUs_per_row = compptr.width_in_blocks;
|
|
5908 cinfo.MCU_rows_in_scan = compptr.height_in_blocks;
|
|
5909
|
|
5910 /* For noninterleaved scan, always one block per MCU */
|
|
5911 compptr.MCU_width = 1;
|
|
5912 compptr.MCU_height = 1;
|
|
5913 compptr.MCU_blocks = 1;
|
|
5914 compptr.MCU_sample_width = compptr.DCT_scaled_size;
|
|
5915 compptr.last_col_width = 1;
|
|
5916 /* For noninterleaved scans, it is convenient to define last_row_height
|
|
5917 * as the number of block rows present in the last iMCU row.
|
|
5918 */
|
|
5919 tmp = (compptr.height_in_blocks % compptr.v_samp_factor);
|
|
5920 if (tmp is 0) tmp = compptr.v_samp_factor;
|
|
5921 compptr.last_row_height = tmp;
|
|
5922
|
|
5923 /* Prepare array describing MCU composition */
|
|
5924 cinfo.blocks_in_MCU = 1;
|
|
5925 cinfo.MCU_membership[0] = 0;
|
|
5926
|
|
5927 } else {
|
|
5928
|
|
5929 /* Interleaved (multi-component) scan */
|
|
5930 if (cinfo.comps_in_scan <= 0 || cinfo.comps_in_scan > MAX_COMPS_IN_SCAN)
|
|
5931 error();
|
|
5932 // ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo.comps_in_scan, MAX_COMPS_IN_SCAN);
|
|
5933
|
|
5934 /* Overall image size in MCUs */
|
|
5935 cinfo.MCUs_per_row = cast(int)jdiv_round_up( cinfo.image_width, (cinfo.max_h_samp_factor*DCTSIZE));
|
|
5936 cinfo.MCU_rows_in_scan = cast(int)jdiv_round_up( cinfo.image_height, (cinfo.max_v_samp_factor*DCTSIZE));
|
|
5937
|
|
5938 cinfo.blocks_in_MCU = 0;
|
|
5939
|
|
5940 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
5941 compptr = cinfo.cur_comp_info[ci];
|
|
5942 /* Sampling factors give # of blocks of component in each MCU */
|
|
5943 compptr.MCU_width = compptr.h_samp_factor;
|
|
5944 compptr.MCU_height = compptr.v_samp_factor;
|
|
5945 compptr.MCU_blocks = compptr.MCU_width * compptr.MCU_height;
|
|
5946 compptr.MCU_sample_width = compptr.MCU_width * compptr.DCT_scaled_size;
|
|
5947 /* Figure number of non-dummy blocks in last MCU column & row */
|
|
5948 tmp = (compptr.width_in_blocks % compptr.MCU_width);
|
|
5949 if (tmp is 0) tmp = compptr.MCU_width;
|
|
5950 compptr.last_col_width = tmp;
|
|
5951 tmp = (compptr.height_in_blocks % compptr.MCU_height);
|
|
5952 if (tmp is 0) tmp = compptr.MCU_height;
|
|
5953 compptr.last_row_height = tmp;
|
|
5954 /* Prepare array describing MCU composition */
|
|
5955 mcublks = compptr.MCU_blocks;
|
|
5956 if (cinfo.blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
|
|
5957 error();
|
|
5958 // ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
|
|
5959 while (mcublks-- > 0) {
|
|
5960 cinfo.MCU_membership[cinfo.blocks_in_MCU++] = ci;
|
|
5961 }
|
|
5962 }
|
|
5963
|
|
5964 }
|
|
5965 }
|
|
5966
|
|
5967 static void latch_quant_tables (jpeg_decompress_struct cinfo) {
|
|
5968 int ci, qtblno;
|
|
5969 jpeg_component_info compptr;
|
|
5970 JQUANT_TBL qtbl;
|
|
5971
|
|
5972 for (ci = 0; ci < cinfo.comps_in_scan; ci++) {
|
|
5973 compptr = cinfo.cur_comp_info[ci];
|
|
5974 /* No work if we already saved Q-table for this component */
|
|
5975 if (compptr.quant_table !is null)
|
|
5976 continue;
|
|
5977 /* Make sure specified quantization table is present */
|
|
5978 qtblno = compptr.quant_tbl_no;
|
|
5979 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || cinfo.quant_tbl_ptrs[qtblno] is null)
|
|
5980 error();
|
|
5981 // ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
|
|
5982 /* OK, save away the quantization table */
|
|
5983 qtbl = new JQUANT_TBL();
|
|
5984 System.arraycopy(cinfo.quant_tbl_ptrs[qtblno].quantval, 0, qtbl.quantval, 0, qtbl.quantval.length);
|
|
5985 qtbl.sent_table = cinfo.quant_tbl_ptrs[qtblno].sent_table;
|
|
5986 compptr.quant_table = qtbl;
|
|
5987 }
|
|
5988 }
|
|
5989
|
|
5990 static void jpeg_make_d_derived_tbl (jpeg_decompress_struct cinfo, bool isDC, int tblno, d_derived_tbl dtbl) {
|
|
5991 JHUFF_TBL htbl;
|
|
5992 int p, i = 0, l, si, numsymbols;
|
|
5993 int lookbits, ctr;
|
|
5994 byte[] huffsize = new byte[257];
|
|
5995 int[] huffcode = new int[257];
|
|
5996 int code;
|
|
5997
|
|
5998 /* Note that huffsize[] and huffcode[] are filled in code-length order,
|
|
5999 * paralleling the order of the symbols themselves in htbl.huffval[].
|
|
6000 */
|
|
6001
|
|
6002 /* Find the input Huffman table */
|
|
6003 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
|
|
6004 error();
|
|
6005 // ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
|
6006 htbl = isDC ? cinfo.dc_huff_tbl_ptrs[tblno] : cinfo.ac_huff_tbl_ptrs[tblno];
|
|
6007 if (htbl is null)
|
|
6008 error();
|
|
6009 // ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
|
|
6010
|
|
6011 /* Allocate a workspace if we haven't already done so. */
|
|
6012 dtbl.pub = htbl; /* fill in back link */
|
|
6013
|
|
6014 /* Figure C.1: make table of Huffman code length for each symbol */
|
|
6015
|
|
6016 p = 0;
|
|
6017 for (l = 1; l <= 16; l++) {
|
|
6018 i = htbl.bits[l] & 0xFF;
|
|
6019 if (i < 0 || p + i > 256) /* protect against table overrun */
|
|
6020 error();
|
|
6021 // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
6022 while (i-- !is 0)
|
|
6023 huffsize[p++] = cast(byte) l;
|
|
6024 }
|
|
6025 huffsize[p] = 0;
|
|
6026 numsymbols = p;
|
|
6027
|
|
6028 /* Figure C.2: generate the codes themselves */
|
|
6029 /* We also validate that the counts represent a legal Huffman code tree. */
|
|
6030
|
|
6031 code = 0;
|
|
6032 si = huffsize[0];
|
|
6033 p = 0;
|
|
6034 while ( huffsize[p] !is 0) {
|
|
6035 while (( huffsize[p]) is si) {
|
|
6036 huffcode[p++] = code;
|
|
6037 code++;
|
|
6038 }
|
|
6039 /* code is now 1 more than the last code used for codelength si; but
|
|
6040 * it must still fit in si bits, since no code is allowed to be all ones.
|
|
6041 */
|
|
6042 if (( code) >= (( 1) << si))
|
|
6043 error();
|
|
6044 // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
6045 code <<= 1;
|
|
6046 si++;
|
|
6047 }
|
|
6048
|
|
6049 /* Figure F.15: generate decoding tables for bit-sequential decoding */
|
|
6050
|
|
6051 p = 0;
|
|
6052 for (l = 1; l <= 16; l++) {
|
|
6053 if ((htbl.bits[l] & 0xFF) !is 0) {
|
|
6054 /* valoffset[l] = huffval[] index of 1st symbol of code length l,
|
|
6055 * minus the minimum code of length l
|
|
6056 */
|
|
6057 dtbl.valoffset[l] = p - huffcode[p];
|
|
6058 p += (htbl.bits[l] & 0xFF);
|
|
6059 dtbl.maxcode[l] = huffcode[p-1]; /* maximum code of length l */
|
|
6060 } else {
|
|
6061 dtbl.maxcode[l] = -1; /* -1 if no codes of this length */
|
|
6062 }
|
|
6063 }
|
|
6064 dtbl.maxcode[17] = 0xFFFFF; /* ensures jpeg_huff_decode terminates */
|
|
6065
|
|
6066 /* Compute lookahead tables to speed up decoding.
|
|
6067 * First we set all the table entries to 0, indicating "too long";
|
|
6068 * then we iterate through the Huffman codes that are short enough and
|
|
6069 * fill in all the entries that correspond to bit sequences starting
|
|
6070 * with that code.
|
|
6071 */
|
|
6072
|
|
6073 for (int j = 0; j < dtbl.look_nbits.length; j++) {
|
|
6074 dtbl.look_nbits[j] = 0;
|
|
6075 }
|
|
6076
|
|
6077 p = 0;
|
|
6078 for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
|
|
6079 for (i = 1; i <= (htbl.bits[l] & 0xFF); i++, p++) {
|
|
6080 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
|
|
6081 /* Generate left-justified code followed by all possible bit sequences */
|
|
6082 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
|
|
6083 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
|
|
6084 dtbl.look_nbits[lookbits] = l;
|
|
6085 dtbl.look_sym[lookbits] = htbl.huffval[p];
|
|
6086 lookbits++;
|
|
6087 }
|
|
6088 }
|
|
6089 }
|
|
6090
|
|
6091 /* Validate symbols as being reasonable.
|
|
6092 * For AC tables, we make no check, but accept all byte values 0..255.
|
|
6093 * For DC tables, we require the symbols to be in range 0..15.
|
|
6094 * (Tighter bounds could be applied depending on the data depth and mode,
|
|
6095 * but this is sufficient to ensure safe decoding.)
|
|
6096 */
|
|
6097 if (isDC) {
|
|
6098 for (i = 0; i < numsymbols; i++) {
|
|
6099 int sym = htbl.huffval[i] & 0xFF;
|
|
6100 if (sym < 0 || sym > 15)
|
|
6101 error();
|
|
6102 // ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
|
|
6103 }
|
|
6104 }
|
|
6105 }
|
|
6106
|
|
6107 static void start_input_pass (jpeg_decompress_struct cinfo) {
|
|
6108 per_scan_setup(cinfo);
|
|
6109 latch_quant_tables(cinfo);
|
|
6110 cinfo.entropy.start_pass(cinfo);
|
|
6111 cinfo.coef.start_input_pass (cinfo);
|
|
6112 cinfo.inputctl.consume_input = COEF_CONSUME_INPUT;
|
|
6113 }
|
|
6114
|
|
6115 static void finish_input_pass (jpeg_decompress_struct cinfo) {
|
|
6116 cinfo.inputctl.consume_input = INPUT_CONSUME_INPUT;
|
|
6117 }
|
|
6118
|
|
6119 static int consume_markers (jpeg_decompress_struct cinfo) {
|
|
6120 jpeg_input_controller inputctl = cinfo.inputctl;
|
|
6121 int val;
|
|
6122
|
|
6123 if (inputctl.eoi_reached) /* After hitting EOI, read no further */
|
|
6124 return JPEG_REACHED_EOI;
|
|
6125
|
|
6126 val = read_markers (cinfo);
|
|
6127
|
|
6128 switch (val) {
|
|
6129 case JPEG_REACHED_SOS: /* Found SOS */
|
|
6130 if (inputctl.inheaders) { /* 1st SOS */
|
|
6131 initial_setup(cinfo);
|
|
6132 inputctl.inheaders = false;
|
|
6133 /* Note: start_input_pass must be called by jdmaster.c
|
|
6134 * before any more input can be consumed. jdapimin.c is
|
|
6135 * responsible for enforcing this sequencing.
|
|
6136 */
|
|
6137 } else { /* 2nd or later SOS marker */
|
|
6138 if (! inputctl.has_multiple_scans)
|
|
6139 error();
|
|
6140 // ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
|
|
6141 start_input_pass(cinfo);
|
|
6142 }
|
|
6143 break;
|
|
6144 case JPEG_REACHED_EOI: /* Found EOI */
|
|
6145 inputctl.eoi_reached = true;
|
|
6146 if (inputctl.inheaders) { /* Tables-only datastream, apparently */
|
|
6147 if (cinfo.marker.saw_SOF)
|
|
6148 error();
|
|
6149 // ERREXIT(cinfo, JERR_SOF_NO_SOS);
|
|
6150 } else {
|
|
6151 /* Prevent infinite loop in coef ctlr's decompress_data routine
|
|
6152 * if user set output_scan_number larger than number of scans.
|
|
6153 */
|
|
6154 if (cinfo.output_scan_number > cinfo.input_scan_number)
|
|
6155 cinfo.output_scan_number = cinfo.input_scan_number;
|
|
6156 }
|
|
6157 break;
|
|
6158 case JPEG_SUSPENDED:
|
|
6159 break;
|
|
6160 default:
|
|
6161 }
|
|
6162
|
|
6163 return val;
|
|
6164 }
|
|
6165
|
|
6166 static void default_decompress_parms (jpeg_decompress_struct cinfo) {
|
|
6167 /* Guess the input colorspace, and set output colorspace accordingly. */
|
|
6168 /* (Wish JPEG committee had provided a real way to specify this...) */
|
|
6169 /* Note application may override our guesses. */
|
|
6170 switch (cinfo.num_components) {
|
|
6171 case 1:
|
|
6172 cinfo.jpeg_color_space = JCS_GRAYSCALE;
|
|
6173 cinfo.out_color_space = JCS_GRAYSCALE;
|
|
6174 break;
|
|
6175
|
|
6176 case 3:
|
|
6177 if (cinfo.saw_JFIF_marker) {
|
|
6178 cinfo.jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
|
|
6179 } else if (cinfo.saw_Adobe_marker) {
|
|
6180 switch (cinfo.Adobe_transform) {
|
|
6181 case 0:
|
|
6182 cinfo.jpeg_color_space = JCS_RGB;
|
|
6183 break;
|
|
6184 case 1:
|
|
6185 cinfo.jpeg_color_space = JCS_YCbCr;
|
|
6186 break;
|
|
6187 default:
|
|
6188 // WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo.Adobe_transform);
|
|
6189 cinfo.jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
|
|
6190 break;
|
|
6191 }
|
|
6192 } else {
|
|
6193 /* Saw no special markers, try to guess from the component IDs */
|
|
6194 int cid0 = cinfo.comp_info[0].component_id;
|
|
6195 int cid1 = cinfo.comp_info[1].component_id;
|
|
6196 int cid2 = cinfo.comp_info[2].component_id;
|
|
6197
|
|
6198 if (cid0 is 1 && cid1 is 2 && cid2 is 3)
|
|
6199 cinfo.jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
|
|
6200 else if (cid0 is 82 && cid1 is 71 && cid2 is 66)
|
|
6201 cinfo.jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
|
|
6202 else {
|
|
6203 // TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
|
|
6204 cinfo.jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
|
|
6205 }
|
|
6206 }
|
|
6207 /* Always guess RGB is proper output colorspace. */
|
|
6208 cinfo.out_color_space = JCS_RGB;
|
|
6209 break;
|
|
6210
|
|
6211 case 4:
|
|
6212 if (cinfo.saw_Adobe_marker) {
|
|
6213 switch (cinfo.Adobe_transform) {
|
|
6214 case 0:
|
|
6215 cinfo.jpeg_color_space = JCS_CMYK;
|
|
6216 break;
|
|
6217 case 2:
|
|
6218 cinfo.jpeg_color_space = JCS_YCCK;
|
|
6219 break;
|
|
6220 default:
|
|
6221 // WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo.Adobe_transform);
|
|
6222 cinfo.jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
|
|
6223 break;
|
|
6224 }
|
|
6225 } else {
|
|
6226 /* No special markers, assume straight CMYK. */
|
|
6227 cinfo.jpeg_color_space = JCS_CMYK;
|
|
6228 }
|
|
6229 cinfo.out_color_space = JCS_CMYK;
|
|
6230 break;
|
|
6231
|
|
6232 default:
|
|
6233 cinfo.jpeg_color_space = JCS_UNKNOWN;
|
|
6234 cinfo.out_color_space = JCS_UNKNOWN;
|
|
6235 break;
|
|
6236 }
|
|
6237
|
|
6238 /* Set defaults for other decompression parameters. */
|
|
6239 cinfo.scale_num = 1; /* 1:1 scaling */
|
|
6240 cinfo.scale_denom = 1;
|
|
6241 cinfo.output_gamma = 1.0;
|
|
6242 cinfo.buffered_image = false;
|
|
6243 cinfo.raw_data_out = false;
|
|
6244 cinfo.dct_method = JDCT_DEFAULT;
|
|
6245 cinfo.do_fancy_upsampling = true;
|
|
6246 cinfo.do_block_smoothing = true;
|
|
6247 cinfo.quantize_colors = false;
|
|
6248 /* We set these in case application only sets quantize_colors. */
|
|
6249 cinfo.dither_mode = JDITHER_FS;
|
|
6250 cinfo.two_pass_quantize = true;
|
|
6251 cinfo.desired_number_of_colors = 256;
|
|
6252 cinfo.colormap = null;
|
|
6253 /* Initialize for no mode change in buffered-image mode. */
|
|
6254 cinfo.enable_1pass_quant = false;
|
|
6255 cinfo.enable_external_quant = false;
|
|
6256 cinfo.enable_2pass_quant = false;
|
|
6257 }
|
|
6258
|
|
6259 static void init_source(jpeg_decompress_struct cinfo) {
|
|
6260 cinfo.buffer = new byte[INPUT_BUFFER_SIZE];
|
|
6261 cinfo.bytes_in_buffer = 0;
|
|
6262 cinfo.bytes_offset = 0;
|
|
6263 cinfo.start_of_file = true;
|
|
6264 }
|
|
6265
|
|
6266 static int jpeg_consume_input (jpeg_decompress_struct cinfo) {
|
|
6267 int retcode = JPEG_SUSPENDED;
|
|
6268
|
|
6269 /* NB: every possible DSTATE value should be listed in this switch */
|
|
6270 switch (cinfo.global_state) {
|
|
6271 case DSTATE_START:
|
|
6272 /* Start-of-datastream actions: reset appropriate modules */
|
|
6273 reset_input_controller(cinfo);
|
|
6274 /* Initialize application's data source module */
|
|
6275 init_source (cinfo);
|
|
6276 cinfo.global_state = DSTATE_INHEADER;
|
|
6277 /*FALLTHROUGH*/
|
|
6278 case DSTATE_INHEADER:
|
|
6279 retcode = consume_input(cinfo);
|
|
6280 if (retcode is JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
|
|
6281 /* Set up default parameters based on header data */
|
|
6282 default_decompress_parms(cinfo);
|
|
6283 /* Set global state: ready for start_decompress */
|
|
6284 cinfo.global_state = DSTATE_READY;
|
|
6285 }
|
|
6286 break;
|
|
6287 case DSTATE_READY:
|
|
6288 /* Can't advance past first SOS until start_decompress is called */
|
|
6289 retcode = JPEG_REACHED_SOS;
|
|
6290 break;
|
|
6291 case DSTATE_PRELOAD:
|
|
6292 case DSTATE_PRESCAN:
|
|
6293 case DSTATE_SCANNING:
|
|
6294 case DSTATE_RAW_OK:
|
|
6295 case DSTATE_BUFIMAGE:
|
|
6296 case DSTATE_BUFPOST:
|
|
6297 case DSTATE_STOPPING:
|
|
6298 retcode = consume_input (cinfo);
|
|
6299 break;
|
|
6300 default:
|
|
6301 error();
|
|
6302 // ERREXIT1(cinfo, JERR_BAD_STATE, cinfo.global_state);
|
|
6303 }
|
|
6304 return retcode;
|
|
6305 }
|
|
6306
|
|
6307
|
|
6308 static void jpeg_abort (jpeg_decompress_struct cinfo) {
|
|
6309 // int pool;
|
|
6310 //
|
|
6311 // /* Releasing pools in reverse order might help avoid fragmentation
|
|
6312 // * with some (brain-damaged) malloc libraries.
|
|
6313 // */
|
|
6314 // for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
|
|
6315 // (*cinfo.mem.free_pool) (cinfo, pool);
|
|
6316 // }
|
|
6317
|
|
6318 /* Reset overall state for possible reuse of object */
|
|
6319 if (cinfo.is_decompressor) {
|
|
6320 cinfo.global_state = DSTATE_START;
|
|
6321 /* Try to keep application from accessing now-deleted marker list.
|
|
6322 * A bit kludgy to do it here, but this is the most central place.
|
|
6323 */
|
|
6324 // ((j_decompress_ptr) cinfo).marker_list = null;
|
|
6325 } else {
|
|
6326 cinfo.global_state = CSTATE_START;
|
|
6327 }
|
|
6328 }
|
|
6329
|
|
6330
|
|
6331 static bool isFileFormat(LEDataInputStream stream) {
|
|
6332 try {
|
|
6333 byte[] buffer = new byte[2];
|
|
6334 stream.read(buffer);
|
|
6335 stream.unread(buffer);
|
|
6336 return (buffer[0] & 0xFF) is 0xFF && (buffer[1] & 0xFF) is M_SOI;
|
|
6337 } catch (Exception e) {
|
|
6338 return false;
|
|
6339 }
|
|
6340 }
|
|
6341
|
|
6342 static ImageData[] loadFromByteStream(InputStream inputStream, ImageLoader loader) {
|
|
6343 jpeg_decompress_struct cinfo = new jpeg_decompress_struct();
|
|
6344 cinfo.inputStream = inputStream;
|
|
6345 jpeg_create_decompress(cinfo);
|
|
6346 jpeg_read_header(cinfo, true);
|
|
6347 cinfo.buffered_image = cinfo.progressive_mode && loader.hasListeners();
|
|
6348 jpeg_start_decompress(cinfo);
|
|
6349 PaletteData palette = null;
|
|
6350 switch (cinfo.out_color_space) {
|
|
6351 case JCS_RGB:
|
|
6352 palette = new PaletteData(0xFF, 0xFF00, 0xFF0000);
|
|
6353 break;
|
|
6354 case JCS_GRAYSCALE:
|
|
6355 RGB[] colors = new RGB[256];
|
|
6356 for (int i = 0; i < colors.length; i++) {
|
|
6357 colors[i] = new RGB(i, i, i);
|
|
6358 }
|
|
6359 palette = new PaletteData(colors);
|
|
6360 break;
|
|
6361 default:
|
|
6362 error();
|
|
6363 }
|
|
6364 int scanlinePad = 4;
|
|
6365 int row_stride = (((cinfo.output_width * cinfo.out_color_components * 8 + 7) / 8) + (scanlinePad - 1)) / scanlinePad * scanlinePad;
|
|
6366 byte[][] buffer = new byte[][]( 1, row_stride );
|
|
6367 byte[] data = new byte[row_stride * cinfo.output_height];
|
|
6368 ImageData imageData = ImageData.internal_new(
|
|
6369 cinfo.output_width, cinfo.output_height, palette.isDirect ? 24 : 8, palette, scanlinePad, data,
|
|
6370 0, null, null, -1, -1, SWT.IMAGE_JPEG, 0, 0, 0, 0);
|
|
6371 if (cinfo.buffered_image) {
|
|
6372 bool done;
|
|
6373 do {
|
|
6374 int incrementCount = cinfo.input_scan_number - 1;
|
|
6375 jpeg_start_output(cinfo, cinfo.input_scan_number);
|
|
6376 while (cinfo.output_scanline < cinfo.output_height) {
|
|
6377 int offset = row_stride * cinfo.output_scanline;
|
|
6378 jpeg_read_scanlines(cinfo, buffer, 1);
|
|
6379 System.arraycopy(buffer[0], 0, data, offset, row_stride);
|
|
6380 }
|
|
6381 jpeg_finish_output(cinfo);
|
|
6382 loader.notifyListeners(new ImageLoaderEvent(loader, cast(ImageData)imageData.clone(), incrementCount, done = jpeg_input_complete(cinfo)));
|
|
6383 } while (!done);
|
|
6384 } else {
|
|
6385 while (cinfo.output_scanline < cinfo.output_height) {
|
|
6386 int offset = row_stride * cinfo.output_scanline;
|
|
6387 jpeg_read_scanlines(cinfo, buffer, 1);
|
|
6388 System.arraycopy(buffer[0], 0, data, offset, row_stride);
|
|
6389 }
|
|
6390 }
|
|
6391 jpeg_finish_decompress(cinfo);
|
|
6392 jpeg_destroy_decompress(cinfo);
|
|
6393 return [imageData];
|
|
6394 }
|
|
6395
|
|
6396 }
|