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