1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | /* |
---|
10 | * jdmarker.c |
---|
11 | * |
---|
12 | * Copyright (C) 1991-1996, Thomas G. Lane. |
---|
13 | * This file is part of the Independent JPEG Group's software. |
---|
14 | * For conditions of distribution and use, see the accompanying README file. |
---|
15 | * |
---|
16 | * This file contains routines to decode JPEG datastream markers. |
---|
17 | * Most of the complexity arises from our desire to support input |
---|
18 | * suspension: if not all of the data for a marker is available, |
---|
19 | * we must exit back to the application. On resumption, we reprocess |
---|
20 | * the marker. |
---|
21 | */ |
---|
22 | |
---|
23 | #define JPEG_INTERNALS |
---|
24 | #include "loaders/jpg/jinclude.h" |
---|
25 | #include "loaders/jpg/jpeglib.h" |
---|
26 | |
---|
27 | |
---|
28 | typedef enum { /* JPEG marker codes */ |
---|
29 | M_SOF0 = 0xc0, |
---|
30 | M_SOF1 = 0xc1, |
---|
31 | M_SOF2 = 0xc2, |
---|
32 | M_SOF3 = 0xc3, |
---|
33 | |
---|
34 | M_SOF5 = 0xc5, |
---|
35 | M_SOF6 = 0xc6, |
---|
36 | M_SOF7 = 0xc7, |
---|
37 | |
---|
38 | M_JPG = 0xc8, |
---|
39 | M_SOF9 = 0xc9, |
---|
40 | M_SOF10 = 0xca, |
---|
41 | M_SOF11 = 0xcb, |
---|
42 | |
---|
43 | M_SOF13 = 0xcd, |
---|
44 | M_SOF14 = 0xce, |
---|
45 | M_SOF15 = 0xcf, |
---|
46 | |
---|
47 | M_DHT = 0xc4, |
---|
48 | |
---|
49 | M_DAC = 0xcc, |
---|
50 | |
---|
51 | M_RST0 = 0xd0, |
---|
52 | M_RST1 = 0xd1, |
---|
53 | M_RST2 = 0xd2, |
---|
54 | M_RST3 = 0xd3, |
---|
55 | M_RST4 = 0xd4, |
---|
56 | M_RST5 = 0xd5, |
---|
57 | M_RST6 = 0xd6, |
---|
58 | M_RST7 = 0xd7, |
---|
59 | |
---|
60 | M_SOI = 0xd8, |
---|
61 | M_EOI = 0xd9, |
---|
62 | M_SOS = 0xda, |
---|
63 | M_DQT = 0xdb, |
---|
64 | M_DNL = 0xdc, |
---|
65 | M_DRI = 0xdd, |
---|
66 | M_DHP = 0xde, |
---|
67 | M_EXP = 0xdf, |
---|
68 | |
---|
69 | M_APP0 = 0xe0, |
---|
70 | M_APP1 = 0xe1, |
---|
71 | M_APP2 = 0xe2, |
---|
72 | M_APP3 = 0xe3, |
---|
73 | M_APP4 = 0xe4, |
---|
74 | M_APP5 = 0xe5, |
---|
75 | M_APP6 = 0xe6, |
---|
76 | M_APP7 = 0xe7, |
---|
77 | M_APP8 = 0xe8, |
---|
78 | M_APP9 = 0xe9, |
---|
79 | M_APP10 = 0xea, |
---|
80 | M_APP11 = 0xeb, |
---|
81 | M_APP12 = 0xec, |
---|
82 | M_APP13 = 0xed, |
---|
83 | M_APP14 = 0xee, |
---|
84 | M_APP15 = 0xef, |
---|
85 | |
---|
86 | M_JPG0 = 0xf0, |
---|
87 | M_JPG13 = 0xfd, |
---|
88 | M_COM = 0xfe, |
---|
89 | |
---|
90 | M_TEM = 0x01, |
---|
91 | |
---|
92 | M_ERROR = 0x100 |
---|
93 | } JPEG_MARKER; |
---|
94 | |
---|
95 | |
---|
96 | /* |
---|
97 | * Macros for fetching data from the data source module. |
---|
98 | * |
---|
99 | * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect |
---|
100 | * the current restart point; we update them only when we have reached a |
---|
101 | * suitable place to restart if a suspension occurs. |
---|
102 | */ |
---|
103 | |
---|
104 | /* Declare and initialize local copies of input pointer/count */ |
---|
105 | #define INPUT_VARS(cinfo) \ |
---|
106 | struct jpeg_source_mgr * datasrc = (cinfo)->src; \ |
---|
107 | const JOCTET * next_input_byte = datasrc->next_input_byte; \ |
---|
108 | size_t bytes_in_buffer = datasrc->bytes_in_buffer |
---|
109 | |
---|
110 | /* Unload the local copies --- do this only at a restart boundary */ |
---|
111 | #define INPUT_SYNC(cinfo) \ |
---|
112 | ( datasrc->next_input_byte = next_input_byte, \ |
---|
113 | datasrc->bytes_in_buffer = bytes_in_buffer ) |
---|
114 | |
---|
115 | /* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */ |
---|
116 | #define INPUT_RELOAD(cinfo) \ |
---|
117 | ( next_input_byte = datasrc->next_input_byte, \ |
---|
118 | bytes_in_buffer = datasrc->bytes_in_buffer ) |
---|
119 | |
---|
120 | /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available. |
---|
121 | * Note we do *not* do INPUT_SYNC before calling fill_input_buffer, |
---|
122 | * but we must reload the local copies after a successful fill. |
---|
123 | */ |
---|
124 | #define MAKE_BYTE_AVAIL(cinfo,action) \ |
---|
125 | if (bytes_in_buffer == 0) { \ |
---|
126 | if (! (*datasrc->fill_input_buffer) (cinfo)) \ |
---|
127 | { action; } \ |
---|
128 | INPUT_RELOAD(cinfo); \ |
---|
129 | } \ |
---|
130 | bytes_in_buffer-- |
---|
131 | |
---|
132 | /* Read a byte into variable V. |
---|
133 | * If must suspend, take the specified action (typically "return FALSE"). |
---|
134 | */ |
---|
135 | #define INPUT_BYTE(cinfo,V,action) \ |
---|
136 | MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ |
---|
137 | V = GETJOCTET(*next_input_byte++); ) |
---|
138 | |
---|
139 | /* As above, but read two bytes interpreted as an unsigned 16-bit integer. |
---|
140 | * V should be declared unsigned int or perhaps INT32. |
---|
141 | */ |
---|
142 | #define INPUT_2BYTES(cinfo,V,action) \ |
---|
143 | MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \ |
---|
144 | V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \ |
---|
145 | MAKE_BYTE_AVAIL(cinfo,action); \ |
---|
146 | V += GETJOCTET(*next_input_byte++); ) |
---|
147 | |
---|
148 | |
---|
149 | /* |
---|
150 | * Routines to process JPEG markers. |
---|
151 | * |
---|
152 | * Entry condition: JPEG marker itself has been read and its code saved |
---|
153 | * in cinfo->unread_marker; input restart point is just after the marker. |
---|
154 | * |
---|
155 | * Exit: if return TRUE, have read and processed any parameters, and have |
---|
156 | * updated the restart point to point after the parameters. |
---|
157 | * If return FALSE, was forced to suspend before reaching end of |
---|
158 | * marker parameters; restart point has not been moved. Same routine |
---|
159 | * will be called again after application supplies more input data. |
---|
160 | * |
---|
161 | * This approach to suspension assumes that all of a marker's parameters can |
---|
162 | * fit into a single input bufferload. This should hold for "normal" |
---|
163 | * markers. Some COM/APPn markers might have large parameter segments, |
---|
164 | * but we use skip_input_data to get past those, and thereby put the problem |
---|
165 | * on the source manager's shoulders. |
---|
166 | * |
---|
167 | * Note that we don't bother to avoid duplicate trace messages if a |
---|
168 | * suspension occurs within marker parameters. Other side effects |
---|
169 | * require more care. |
---|
170 | */ |
---|
171 | |
---|
172 | |
---|
173 | LOCAL(boolean) |
---|
174 | get_soi (j_decompress_ptr cinfo) |
---|
175 | /* Process an SOI marker */ |
---|
176 | { |
---|
177 | int i; |
---|
178 | |
---|
179 | TRACEMS(cinfo, 1, JTRC_SOI); |
---|
180 | |
---|
181 | if (cinfo->marker->saw_SOI) |
---|
182 | ERREXIT(cinfo, JERR_SOI_DUPLICATE); |
---|
183 | |
---|
184 | /* Reset all parameters that are defined to be reset by SOI */ |
---|
185 | |
---|
186 | for (i = 0; i < NUM_ARITH_TBLS; i++) { |
---|
187 | cinfo->arith_dc_L[i] = 0; |
---|
188 | cinfo->arith_dc_U[i] = 1; |
---|
189 | cinfo->arith_ac_K[i] = 5; |
---|
190 | } |
---|
191 | cinfo->restart_interval = 0; |
---|
192 | |
---|
193 | /* Set initial assumptions for colorspace etc */ |
---|
194 | |
---|
195 | cinfo->jpeg_color_space = JCS_UNKNOWN; |
---|
196 | cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */ |
---|
197 | |
---|
198 | cinfo->saw_JFIF_marker = FALSE; |
---|
199 | cinfo->density_unit = 0; /* set default JFIF APP0 values */ |
---|
200 | cinfo->X_density = 1; |
---|
201 | cinfo->Y_density = 1; |
---|
202 | cinfo->saw_Adobe_marker = FALSE; |
---|
203 | cinfo->Adobe_transform = 0; |
---|
204 | |
---|
205 | cinfo->marker->saw_SOI = TRUE; |
---|
206 | |
---|
207 | return TRUE; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | LOCAL(boolean) |
---|
212 | get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith) |
---|
213 | /* Process a SOFn marker */ |
---|
214 | { |
---|
215 | INT32 length; |
---|
216 | int c, ci; |
---|
217 | jpeg_component_info * compptr; |
---|
218 | INPUT_VARS(cinfo); |
---|
219 | |
---|
220 | cinfo->progressive_mode = is_prog; |
---|
221 | cinfo->arith_code = is_arith; |
---|
222 | |
---|
223 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
224 | |
---|
225 | INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE); |
---|
226 | INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE); |
---|
227 | INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE); |
---|
228 | INPUT_BYTE(cinfo, cinfo->num_components, return FALSE); |
---|
229 | |
---|
230 | length -= 8; |
---|
231 | |
---|
232 | TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker, |
---|
233 | (int) cinfo->image_width, (int) cinfo->image_height, |
---|
234 | cinfo->num_components); |
---|
235 | |
---|
236 | if (cinfo->marker->saw_SOF) |
---|
237 | ERREXIT(cinfo, JERR_SOF_DUPLICATE); |
---|
238 | |
---|
239 | /* We don't support files in which the image height is initially specified */ |
---|
240 | /* as 0 and is later redefined by DNL. As long as we have to check that, */ |
---|
241 | /* might as well have a general sanity check. */ |
---|
242 | if (cinfo->image_height <= 0 || cinfo->image_width <= 0 |
---|
243 | || cinfo->num_components <= 0) |
---|
244 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
---|
245 | |
---|
246 | if (length != (cinfo->num_components * 3)) |
---|
247 | ERREXIT(cinfo, JERR_BAD_LENGTH); |
---|
248 | |
---|
249 | if (cinfo->comp_info == NULL) /* do only once, even if suspend */ |
---|
250 | cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small) |
---|
251 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
---|
252 | cinfo->num_components * SIZEOF(jpeg_component_info)); |
---|
253 | |
---|
254 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
---|
255 | ci++, compptr++) { |
---|
256 | compptr->component_index = ci; |
---|
257 | INPUT_BYTE(cinfo, compptr->component_id, return FALSE); |
---|
258 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
259 | compptr->h_samp_factor = (c >> 4) & 15; |
---|
260 | compptr->v_samp_factor = (c ) & 15; |
---|
261 | INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE); |
---|
262 | |
---|
263 | TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT, |
---|
264 | compptr->component_id, compptr->h_samp_factor, |
---|
265 | compptr->v_samp_factor, compptr->quant_tbl_no); |
---|
266 | } |
---|
267 | |
---|
268 | cinfo->marker->saw_SOF = TRUE; |
---|
269 | |
---|
270 | INPUT_SYNC(cinfo); |
---|
271 | return TRUE; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | LOCAL(boolean) |
---|
276 | get_sos (j_decompress_ptr cinfo) |
---|
277 | /* Process a SOS marker */ |
---|
278 | { |
---|
279 | INT32 length; |
---|
280 | int i, ci, n, c, cc; |
---|
281 | jpeg_component_info * compptr; |
---|
282 | INPUT_VARS(cinfo); |
---|
283 | |
---|
284 | if (! cinfo->marker->saw_SOF) |
---|
285 | ERREXIT(cinfo, JERR_SOS_NO_SOF); |
---|
286 | |
---|
287 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
288 | |
---|
289 | INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */ |
---|
290 | |
---|
291 | if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN) |
---|
292 | ERREXIT(cinfo, JERR_BAD_LENGTH); |
---|
293 | |
---|
294 | TRACEMS1(cinfo, 1, JTRC_SOS, n); |
---|
295 | |
---|
296 | cinfo->comps_in_scan = n; |
---|
297 | |
---|
298 | /* Collect the component-spec parameters */ |
---|
299 | |
---|
300 | for (i = 0; i < n; i++) { |
---|
301 | INPUT_BYTE(cinfo, cc, return FALSE); |
---|
302 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
303 | |
---|
304 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
---|
305 | ci++, compptr++) { |
---|
306 | if (cc == compptr->component_id) |
---|
307 | goto id_found; |
---|
308 | } |
---|
309 | |
---|
310 | ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc); |
---|
311 | |
---|
312 | id_found: |
---|
313 | |
---|
314 | cinfo->cur_comp_info[i] = compptr; |
---|
315 | compptr->dc_tbl_no = (c >> 4) & 15; |
---|
316 | compptr->ac_tbl_no = (c ) & 15; |
---|
317 | |
---|
318 | TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc, |
---|
319 | compptr->dc_tbl_no, compptr->ac_tbl_no); |
---|
320 | } |
---|
321 | |
---|
322 | /* Collect the additional scan parameters Ss, Se, Ah/Al. */ |
---|
323 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
324 | cinfo->Ss = c; |
---|
325 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
326 | cinfo->Se = c; |
---|
327 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
328 | cinfo->Ah = (c >> 4) & 15; |
---|
329 | cinfo->Al = (c ) & 15; |
---|
330 | |
---|
331 | TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se, |
---|
332 | cinfo->Ah, cinfo->Al); |
---|
333 | |
---|
334 | /* Prepare to scan data & restart markers */ |
---|
335 | cinfo->marker->next_restart_num = 0; |
---|
336 | |
---|
337 | /* Count another SOS marker */ |
---|
338 | cinfo->input_scan_number++; |
---|
339 | |
---|
340 | INPUT_SYNC(cinfo); |
---|
341 | return TRUE; |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | METHODDEF(boolean) |
---|
346 | get_app0 (j_decompress_ptr cinfo) |
---|
347 | /* Process an APP0 marker */ |
---|
348 | { |
---|
349 | #define JFIF_LEN 14 |
---|
350 | INT32 length; |
---|
351 | UINT8 b[JFIF_LEN]; |
---|
352 | int buffp; |
---|
353 | INPUT_VARS(cinfo); |
---|
354 | |
---|
355 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
356 | length -= 2; |
---|
357 | |
---|
358 | /* See if a JFIF APP0 marker is present */ |
---|
359 | |
---|
360 | if (length >= JFIF_LEN) { |
---|
361 | for (buffp = 0; buffp < JFIF_LEN; buffp++) |
---|
362 | INPUT_BYTE(cinfo, b[buffp], return FALSE); |
---|
363 | length -= JFIF_LEN; |
---|
364 | |
---|
365 | if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) { |
---|
366 | /* Found JFIF APP0 marker: check version */ |
---|
367 | /* Major version must be 1, anything else signals an incompatible change. |
---|
368 | * We used to treat this as an error, but now it's a nonfatal warning, |
---|
369 | * because some bozo at Hijaak couldn't read the spec. |
---|
370 | * Minor version should be 0..2, but process anyway if newer. |
---|
371 | */ |
---|
372 | if (b[5] != 1) |
---|
373 | WARNMS2(cinfo, JWRN_JFIF_MAJOR, b[5], b[6]); |
---|
374 | else if (b[6] > 2) |
---|
375 | TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]); |
---|
376 | /* Save info */ |
---|
377 | cinfo->saw_JFIF_marker = TRUE; |
---|
378 | cinfo->density_unit = b[7]; |
---|
379 | cinfo->X_density = (b[8] << 8) + b[9]; |
---|
380 | cinfo->Y_density = (b[10] << 8) + b[11]; |
---|
381 | TRACEMS3(cinfo, 1, JTRC_JFIF, |
---|
382 | cinfo->X_density, cinfo->Y_density, cinfo->density_unit); |
---|
383 | if (b[12] | b[13]) |
---|
384 | TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]); |
---|
385 | if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3)) |
---|
386 | TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length); |
---|
387 | } else { |
---|
388 | /* Start of APP0 does not match "JFIF" */ |
---|
389 | TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN); |
---|
390 | } |
---|
391 | } else { |
---|
392 | /* Too short to be JFIF marker */ |
---|
393 | TRACEMS1(cinfo, 1, JTRC_APP0, (int) length); |
---|
394 | } |
---|
395 | |
---|
396 | INPUT_SYNC(cinfo); |
---|
397 | if (length > 0) /* skip any remaining data -- could be lots */ |
---|
398 | (*cinfo->src->skip_input_data) (cinfo, (long) length); |
---|
399 | |
---|
400 | return TRUE; |
---|
401 | } |
---|
402 | |
---|
403 | |
---|
404 | METHODDEF(boolean) |
---|
405 | get_app14 (j_decompress_ptr cinfo) |
---|
406 | /* Process an APP14 marker */ |
---|
407 | { |
---|
408 | #define ADOBE_LEN 12 |
---|
409 | INT32 length; |
---|
410 | UINT8 b[ADOBE_LEN]; |
---|
411 | int buffp; |
---|
412 | unsigned int version, flags0, flags1, transform; |
---|
413 | INPUT_VARS(cinfo); |
---|
414 | |
---|
415 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
416 | length -= 2; |
---|
417 | |
---|
418 | /* See if an Adobe APP14 marker is present */ |
---|
419 | |
---|
420 | if (length >= ADOBE_LEN) { |
---|
421 | for (buffp = 0; buffp < ADOBE_LEN; buffp++) |
---|
422 | INPUT_BYTE(cinfo, b[buffp], return FALSE); |
---|
423 | length -= ADOBE_LEN; |
---|
424 | |
---|
425 | if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) { |
---|
426 | /* Found Adobe APP14 marker */ |
---|
427 | version = (b[5] << 8) + b[6]; |
---|
428 | flags0 = (b[7] << 8) + b[8]; |
---|
429 | flags1 = (b[9] << 8) + b[10]; |
---|
430 | transform = b[11]; |
---|
431 | TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform); |
---|
432 | cinfo->saw_Adobe_marker = TRUE; |
---|
433 | cinfo->Adobe_transform = (UINT8) transform; |
---|
434 | } else { |
---|
435 | /* Start of APP14 does not match "Adobe" */ |
---|
436 | TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN); |
---|
437 | } |
---|
438 | } else { |
---|
439 | /* Too short to be Adobe marker */ |
---|
440 | TRACEMS1(cinfo, 1, JTRC_APP14, (int) length); |
---|
441 | } |
---|
442 | |
---|
443 | INPUT_SYNC(cinfo); |
---|
444 | if (length > 0) /* skip any remaining data -- could be lots */ |
---|
445 | (*cinfo->src->skip_input_data) (cinfo, (long) length); |
---|
446 | |
---|
447 | return TRUE; |
---|
448 | } |
---|
449 | |
---|
450 | |
---|
451 | LOCAL(boolean) |
---|
452 | get_dac (j_decompress_ptr cinfo) |
---|
453 | /* Process a DAC marker */ |
---|
454 | { |
---|
455 | INT32 length; |
---|
456 | int index, val; |
---|
457 | INPUT_VARS(cinfo); |
---|
458 | |
---|
459 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
460 | length -= 2; |
---|
461 | |
---|
462 | while (length > 0) { |
---|
463 | INPUT_BYTE(cinfo, index, return FALSE); |
---|
464 | INPUT_BYTE(cinfo, val, return FALSE); |
---|
465 | |
---|
466 | length -= 2; |
---|
467 | |
---|
468 | TRACEMS2(cinfo, 1, JTRC_DAC, index, val); |
---|
469 | |
---|
470 | if (index < 0 || index >= (2*NUM_ARITH_TBLS)) |
---|
471 | ERREXIT1(cinfo, JERR_DAC_INDEX, index); |
---|
472 | |
---|
473 | if (index >= NUM_ARITH_TBLS) { /* define AC table */ |
---|
474 | cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val; |
---|
475 | } else { /* define DC table */ |
---|
476 | cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F); |
---|
477 | cinfo->arith_dc_U[index] = (UINT8) (val >> 4); |
---|
478 | if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index]) |
---|
479 | ERREXIT1(cinfo, JERR_DAC_VALUE, val); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | INPUT_SYNC(cinfo); |
---|
484 | return TRUE; |
---|
485 | } |
---|
486 | |
---|
487 | |
---|
488 | LOCAL(boolean) |
---|
489 | get_dht (j_decompress_ptr cinfo) |
---|
490 | /* Process a DHT marker */ |
---|
491 | { |
---|
492 | INT32 length; |
---|
493 | UINT8 bits[17]; |
---|
494 | UINT8 huffval[256]; |
---|
495 | int i, index, count; |
---|
496 | JHUFF_TBL **htblptr; |
---|
497 | INPUT_VARS(cinfo); |
---|
498 | |
---|
499 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
500 | length -= 2; |
---|
501 | |
---|
502 | while (length > 0) { |
---|
503 | INPUT_BYTE(cinfo, index, return FALSE); |
---|
504 | |
---|
505 | TRACEMS1(cinfo, 1, JTRC_DHT, index); |
---|
506 | |
---|
507 | bits[0] = 0; |
---|
508 | count = 0; |
---|
509 | for (i = 1; i <= 16; i++) { |
---|
510 | INPUT_BYTE(cinfo, bits[i], return FALSE); |
---|
511 | count += bits[i]; |
---|
512 | } |
---|
513 | |
---|
514 | length -= 1 + 16; |
---|
515 | |
---|
516 | TRACEMS8(cinfo, 2, JTRC_HUFFBITS, |
---|
517 | bits[1], bits[2], bits[3], bits[4], |
---|
518 | bits[5], bits[6], bits[7], bits[8]); |
---|
519 | TRACEMS8(cinfo, 2, JTRC_HUFFBITS, |
---|
520 | bits[9], bits[10], bits[11], bits[12], |
---|
521 | bits[13], bits[14], bits[15], bits[16]); |
---|
522 | |
---|
523 | if (count > 256 || ((INT32) count) > length) |
---|
524 | ERREXIT(cinfo, JERR_DHT_COUNTS); |
---|
525 | |
---|
526 | for (i = 0; i < count; i++) |
---|
527 | INPUT_BYTE(cinfo, huffval[i], return FALSE); |
---|
528 | |
---|
529 | length -= count; |
---|
530 | |
---|
531 | if (index & 0x10) { /* AC table definition */ |
---|
532 | index -= 0x10; |
---|
533 | htblptr = &cinfo->ac_huff_tbl_ptrs[index]; |
---|
534 | } else { /* DC table definition */ |
---|
535 | htblptr = &cinfo->dc_huff_tbl_ptrs[index]; |
---|
536 | } |
---|
537 | |
---|
538 | if (index < 0 || index >= NUM_HUFF_TBLS) |
---|
539 | ERREXIT1(cinfo, JERR_DHT_INDEX, index); |
---|
540 | |
---|
541 | if (*htblptr == NULL) |
---|
542 | *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); |
---|
543 | |
---|
544 | MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); |
---|
545 | MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval)); |
---|
546 | } |
---|
547 | |
---|
548 | INPUT_SYNC(cinfo); |
---|
549 | return TRUE; |
---|
550 | } |
---|
551 | |
---|
552 | |
---|
553 | LOCAL(boolean) |
---|
554 | get_dqt (j_decompress_ptr cinfo) |
---|
555 | /* Process a DQT marker */ |
---|
556 | { |
---|
557 | INT32 length; |
---|
558 | int n, i, prec; |
---|
559 | unsigned int tmp; |
---|
560 | JQUANT_TBL *quant_ptr; |
---|
561 | INPUT_VARS(cinfo); |
---|
562 | |
---|
563 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
564 | length -= 2; |
---|
565 | |
---|
566 | while (length > 0) { |
---|
567 | INPUT_BYTE(cinfo, n, return FALSE); |
---|
568 | prec = n >> 4; |
---|
569 | n &= 0x0F; |
---|
570 | |
---|
571 | TRACEMS2(cinfo, 1, JTRC_DQT, n, prec); |
---|
572 | |
---|
573 | if (n >= NUM_QUANT_TBLS) |
---|
574 | ERREXIT1(cinfo, JERR_DQT_INDEX, n); |
---|
575 | |
---|
576 | if (cinfo->quant_tbl_ptrs[n] == NULL) |
---|
577 | cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo); |
---|
578 | quant_ptr = cinfo->quant_tbl_ptrs[n]; |
---|
579 | |
---|
580 | for (i = 0; i < DCTSIZE2; i++) { |
---|
581 | if (prec) |
---|
582 | INPUT_2BYTES(cinfo, tmp, return FALSE); |
---|
583 | else |
---|
584 | INPUT_BYTE(cinfo, tmp, return FALSE); |
---|
585 | /* We convert the zigzag-order table to natural array order. */ |
---|
586 | quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp; |
---|
587 | } |
---|
588 | |
---|
589 | if (cinfo->err->trace_level >= 2) { |
---|
590 | for (i = 0; i < DCTSIZE2; i += 8) { |
---|
591 | TRACEMS8(cinfo, 2, JTRC_QUANTVALS, |
---|
592 | quant_ptr->quantval[i], quant_ptr->quantval[i+1], |
---|
593 | quant_ptr->quantval[i+2], quant_ptr->quantval[i+3], |
---|
594 | quant_ptr->quantval[i+4], quant_ptr->quantval[i+5], |
---|
595 | quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]); |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | length -= DCTSIZE2+1; |
---|
600 | if (prec) length -= DCTSIZE2; |
---|
601 | } |
---|
602 | |
---|
603 | INPUT_SYNC(cinfo); |
---|
604 | return TRUE; |
---|
605 | } |
---|
606 | |
---|
607 | |
---|
608 | LOCAL(boolean) |
---|
609 | get_dri (j_decompress_ptr cinfo) |
---|
610 | /* Process a DRI marker */ |
---|
611 | { |
---|
612 | INT32 length; |
---|
613 | unsigned int tmp; |
---|
614 | INPUT_VARS(cinfo); |
---|
615 | |
---|
616 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
617 | |
---|
618 | if (length != 4) |
---|
619 | ERREXIT(cinfo, JERR_BAD_LENGTH); |
---|
620 | |
---|
621 | INPUT_2BYTES(cinfo, tmp, return FALSE); |
---|
622 | |
---|
623 | TRACEMS1(cinfo, 1, JTRC_DRI, tmp); |
---|
624 | |
---|
625 | cinfo->restart_interval = tmp; |
---|
626 | |
---|
627 | INPUT_SYNC(cinfo); |
---|
628 | return TRUE; |
---|
629 | } |
---|
630 | |
---|
631 | |
---|
632 | METHODDEF(boolean) |
---|
633 | skip_variable (j_decompress_ptr cinfo) |
---|
634 | /* Skip over an unknown or uninteresting variable-length marker */ |
---|
635 | { |
---|
636 | INT32 length; |
---|
637 | INPUT_VARS(cinfo); |
---|
638 | |
---|
639 | INPUT_2BYTES(cinfo, length, return FALSE); |
---|
640 | |
---|
641 | TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length); |
---|
642 | |
---|
643 | INPUT_SYNC(cinfo); /* do before skip_input_data */ |
---|
644 | (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L); |
---|
645 | |
---|
646 | return TRUE; |
---|
647 | } |
---|
648 | |
---|
649 | |
---|
650 | /* |
---|
651 | * Find the next JPEG marker, save it in cinfo->unread_marker. |
---|
652 | * Returns FALSE if had to suspend before reaching a marker; |
---|
653 | * in that case cinfo->unread_marker is unchanged. |
---|
654 | * |
---|
655 | * Note that the result might not be a valid marker code, |
---|
656 | * but it will never be 0 or FF. |
---|
657 | */ |
---|
658 | |
---|
659 | LOCAL(boolean) |
---|
660 | next_marker (j_decompress_ptr cinfo) |
---|
661 | { |
---|
662 | int c; |
---|
663 | INPUT_VARS(cinfo); |
---|
664 | |
---|
665 | for (;;) { |
---|
666 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
667 | /* Skip any non-FF bytes. |
---|
668 | * This may look a bit inefficient, but it will not occur in a valid file. |
---|
669 | * We sync after each discarded byte so that a suspending data source |
---|
670 | * can discard the byte from its buffer. |
---|
671 | */ |
---|
672 | while (c != 0xFF) { |
---|
673 | cinfo->marker->discarded_bytes++; |
---|
674 | INPUT_SYNC(cinfo); |
---|
675 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
676 | } |
---|
677 | /* This loop swallows any duplicate FF bytes. Extra FFs are legal as |
---|
678 | * pad bytes, so don't count them in discarded_bytes. We assume there |
---|
679 | * will not be so many consecutive FF bytes as to overflow a suspending |
---|
680 | * data source's input buffer. |
---|
681 | */ |
---|
682 | do { |
---|
683 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
684 | } while (c == 0xFF); |
---|
685 | if (c != 0) |
---|
686 | break; /* found a valid marker, exit loop */ |
---|
687 | /* Reach here if we found a stuffed-zero data sequence (FF/00). |
---|
688 | * Discard it and loop back to try again. |
---|
689 | */ |
---|
690 | cinfo->marker->discarded_bytes += 2; |
---|
691 | INPUT_SYNC(cinfo); |
---|
692 | } |
---|
693 | |
---|
694 | if (cinfo->marker->discarded_bytes != 0) { |
---|
695 | WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c); |
---|
696 | cinfo->marker->discarded_bytes = 0; |
---|
697 | } |
---|
698 | |
---|
699 | cinfo->unread_marker = c; |
---|
700 | |
---|
701 | INPUT_SYNC(cinfo); |
---|
702 | return TRUE; |
---|
703 | } |
---|
704 | |
---|
705 | |
---|
706 | LOCAL(boolean) |
---|
707 | first_marker (j_decompress_ptr cinfo) |
---|
708 | /* Like next_marker, but used to obtain the initial SOI marker. */ |
---|
709 | /* For this marker, we do not allow preceding garbage or fill; otherwise, |
---|
710 | * we might well scan an entire input file before realizing it ain't JPEG. |
---|
711 | * If an application wants to process non-JFIF files, it must seek to the |
---|
712 | * SOI before calling the JPEG library. |
---|
713 | */ |
---|
714 | { |
---|
715 | int c, c2; |
---|
716 | INPUT_VARS(cinfo); |
---|
717 | |
---|
718 | INPUT_BYTE(cinfo, c, return FALSE); |
---|
719 | INPUT_BYTE(cinfo, c2, return FALSE); |
---|
720 | if (c != 0xFF || c2 != (int) M_SOI) |
---|
721 | ERREXIT2(cinfo, JERR_NO_SOI, c, c2); |
---|
722 | |
---|
723 | cinfo->unread_marker = c2; |
---|
724 | |
---|
725 | INPUT_SYNC(cinfo); |
---|
726 | return TRUE; |
---|
727 | } |
---|
728 | |
---|
729 | |
---|
730 | /* |
---|
731 | * Read markers until SOS or EOI. |
---|
732 | * |
---|
733 | * Returns same codes as are defined for jpeg_consume_input: |
---|
734 | * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. |
---|
735 | */ |
---|
736 | |
---|
737 | METHODDEF(int) |
---|
738 | read_markers (j_decompress_ptr cinfo) |
---|
739 | { |
---|
740 | /* Outer loop repeats once for each marker. */ |
---|
741 | for (;;) { |
---|
742 | /* Collect the marker proper, unless we already did. */ |
---|
743 | /* NB: first_marker() enforces the requirement that SOI appear first. */ |
---|
744 | if (cinfo->unread_marker == 0) { |
---|
745 | if (! cinfo->marker->saw_SOI) { |
---|
746 | if (! first_marker(cinfo)) |
---|
747 | return JPEG_SUSPENDED; |
---|
748 | } else { |
---|
749 | if (! next_marker(cinfo)) |
---|
750 | return JPEG_SUSPENDED; |
---|
751 | } |
---|
752 | } |
---|
753 | /* At this point cinfo->unread_marker contains the marker code and the |
---|
754 | * input point is just past the marker proper, but before any parameters. |
---|
755 | * A suspension will cause us to return with this state still true. |
---|
756 | */ |
---|
757 | switch (cinfo->unread_marker) { |
---|
758 | case M_SOI: |
---|
759 | if (! get_soi(cinfo)) |
---|
760 | return JPEG_SUSPENDED; |
---|
761 | break; |
---|
762 | |
---|
763 | case M_SOF0: /* Baseline */ |
---|
764 | case M_SOF1: /* Extended sequential, Huffman */ |
---|
765 | if (! get_sof(cinfo, FALSE, FALSE)) |
---|
766 | return JPEG_SUSPENDED; |
---|
767 | break; |
---|
768 | |
---|
769 | case M_SOF2: /* Progressive, Huffman */ |
---|
770 | if (! get_sof(cinfo, TRUE, FALSE)) |
---|
771 | return JPEG_SUSPENDED; |
---|
772 | break; |
---|
773 | |
---|
774 | case M_SOF9: /* Extended sequential, arithmetic */ |
---|
775 | if (! get_sof(cinfo, FALSE, TRUE)) |
---|
776 | return JPEG_SUSPENDED; |
---|
777 | break; |
---|
778 | |
---|
779 | case M_SOF10: /* Progressive, arithmetic */ |
---|
780 | if (! get_sof(cinfo, TRUE, TRUE)) |
---|
781 | return JPEG_SUSPENDED; |
---|
782 | break; |
---|
783 | |
---|
784 | /* Currently unsupported SOFn types */ |
---|
785 | case M_SOF3: /* Lossless, Huffman */ |
---|
786 | case M_SOF5: /* Differential sequential, Huffman */ |
---|
787 | case M_SOF6: /* Differential progressive, Huffman */ |
---|
788 | case M_SOF7: /* Differential lossless, Huffman */ |
---|
789 | case M_JPG: /* Reserved for JPEG extensions */ |
---|
790 | case M_SOF11: /* Lossless, arithmetic */ |
---|
791 | case M_SOF13: /* Differential sequential, arithmetic */ |
---|
792 | case M_SOF14: /* Differential progressive, arithmetic */ |
---|
793 | case M_SOF15: /* Differential lossless, arithmetic */ |
---|
794 | ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker); |
---|
795 | break; |
---|
796 | |
---|
797 | case M_SOS: |
---|
798 | if (! get_sos(cinfo)) |
---|
799 | return JPEG_SUSPENDED; |
---|
800 | cinfo->unread_marker = 0; /* processed the marker */ |
---|
801 | return JPEG_REACHED_SOS; |
---|
802 | |
---|
803 | case M_EOI: |
---|
804 | TRACEMS(cinfo, 1, JTRC_EOI); |
---|
805 | cinfo->unread_marker = 0; /* processed the marker */ |
---|
806 | return JPEG_REACHED_EOI; |
---|
807 | |
---|
808 | case M_DAC: |
---|
809 | if (! get_dac(cinfo)) |
---|
810 | return JPEG_SUSPENDED; |
---|
811 | break; |
---|
812 | |
---|
813 | case M_DHT: |
---|
814 | if (! get_dht(cinfo)) |
---|
815 | return JPEG_SUSPENDED; |
---|
816 | break; |
---|
817 | |
---|
818 | case M_DQT: |
---|
819 | if (! get_dqt(cinfo)) |
---|
820 | return JPEG_SUSPENDED; |
---|
821 | break; |
---|
822 | |
---|
823 | case M_DRI: |
---|
824 | if (! get_dri(cinfo)) |
---|
825 | return JPEG_SUSPENDED; |
---|
826 | break; |
---|
827 | |
---|
828 | case M_APP0: |
---|
829 | case M_APP1: |
---|
830 | case M_APP2: |
---|
831 | case M_APP3: |
---|
832 | case M_APP4: |
---|
833 | case M_APP5: |
---|
834 | case M_APP6: |
---|
835 | case M_APP7: |
---|
836 | case M_APP8: |
---|
837 | case M_APP9: |
---|
838 | case M_APP10: |
---|
839 | case M_APP11: |
---|
840 | case M_APP12: |
---|
841 | case M_APP13: |
---|
842 | case M_APP14: |
---|
843 | case M_APP15: |
---|
844 | if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo)) |
---|
845 | return JPEG_SUSPENDED; |
---|
846 | break; |
---|
847 | |
---|
848 | case M_COM: |
---|
849 | if (! (*cinfo->marker->process_COM) (cinfo)) |
---|
850 | return JPEG_SUSPENDED; |
---|
851 | break; |
---|
852 | |
---|
853 | case M_RST0: /* these are all parameterless */ |
---|
854 | case M_RST1: |
---|
855 | case M_RST2: |
---|
856 | case M_RST3: |
---|
857 | case M_RST4: |
---|
858 | case M_RST5: |
---|
859 | case M_RST6: |
---|
860 | case M_RST7: |
---|
861 | case M_TEM: |
---|
862 | TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker); |
---|
863 | break; |
---|
864 | |
---|
865 | case M_DNL: /* Ignore DNL ... perhaps the wrong thing */ |
---|
866 | if (! skip_variable(cinfo)) |
---|
867 | return JPEG_SUSPENDED; |
---|
868 | break; |
---|
869 | |
---|
870 | default: /* must be DHP, EXP, JPGn, or RESn */ |
---|
871 | /* For now, we treat the reserved markers as fatal errors since they are |
---|
872 | * likely to be used to signal incompatible JPEG Part 3 extensions. |
---|
873 | * Once the JPEG 3 version-number marker is well defined, this code |
---|
874 | * ought to change! |
---|
875 | */ |
---|
876 | ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); |
---|
877 | break; |
---|
878 | } |
---|
879 | /* Successfully processed marker, so reset state variable */ |
---|
880 | cinfo->unread_marker = 0; |
---|
881 | } /* end loop */ |
---|
882 | } |
---|
883 | |
---|
884 | |
---|
885 | /* |
---|
886 | * Read a restart marker, which is expected to appear next in the datastream; |
---|
887 | * if the marker is not there, take appropriate recovery action. |
---|
888 | * Returns FALSE if suspension is required. |
---|
889 | * |
---|
890 | * This is called by the entropy decoder after it has read an appropriate |
---|
891 | * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder |
---|
892 | * has already read a marker from the data source. Under normal conditions |
---|
893 | * cinfo->unread_marker will be reset to 0 before returning; if not reset, |
---|
894 | * it holds a marker which the decoder will be unable to read past. |
---|
895 | */ |
---|
896 | |
---|
897 | METHODDEF(boolean) |
---|
898 | read_restart_marker (j_decompress_ptr cinfo) |
---|
899 | { |
---|
900 | /* Obtain a marker unless we already did. */ |
---|
901 | /* Note that next_marker will complain if it skips any data. */ |
---|
902 | if (cinfo->unread_marker == 0) { |
---|
903 | if (! next_marker(cinfo)) |
---|
904 | return FALSE; |
---|
905 | } |
---|
906 | |
---|
907 | if (cinfo->unread_marker == |
---|
908 | ((int) M_RST0 + cinfo->marker->next_restart_num)) { |
---|
909 | /* Normal case --- swallow the marker and let entropy decoder continue */ |
---|
910 | TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num); |
---|
911 | cinfo->unread_marker = 0; |
---|
912 | } else { |
---|
913 | /* Uh-oh, the restart markers have been messed up. */ |
---|
914 | /* Let the data source manager determine how to resync. */ |
---|
915 | if (! (*cinfo->src->resync_to_restart) (cinfo, |
---|
916 | cinfo->marker->next_restart_num)) |
---|
917 | return FALSE; |
---|
918 | } |
---|
919 | |
---|
920 | /* Update next-restart state */ |
---|
921 | cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7; |
---|
922 | |
---|
923 | return TRUE; |
---|
924 | } |
---|
925 | |
---|
926 | |
---|
927 | /* |
---|
928 | * This is the default resync_to_restart method for data source managers |
---|
929 | * to use if they don't have any better approach. Some data source managers |
---|
930 | * may be able to back up, or may have additional knowledge about the data |
---|
931 | * which permits a more intelligent recovery strategy; such managers would |
---|
932 | * presumably supply their own resync method. |
---|
933 | * |
---|
934 | * read_restart_marker calls resync_to_restart if it finds a marker other than |
---|
935 | * the restart marker it was expecting. (This code is *not* used unless |
---|
936 | * a nonzero restart interval has been declared.) cinfo->unread_marker is |
---|
937 | * the marker code actually found (might be anything, except 0 or FF). |
---|
938 | * The desired restart marker number (0..7) is passed as a parameter. |
---|
939 | * This routine is supposed to apply whatever error recovery strategy seems |
---|
940 | * appropriate in order to position the input stream to the next data segment. |
---|
941 | * Note that cinfo->unread_marker is treated as a marker appearing before |
---|
942 | * the current data-source input point; usually it should be reset to zero |
---|
943 | * before returning. |
---|
944 | * Returns FALSE if suspension is required. |
---|
945 | * |
---|
946 | * This implementation is substantially constrained by wanting to treat the |
---|
947 | * input as a data stream; this means we can't back up. Therefore, we have |
---|
948 | * only the following actions to work with: |
---|
949 | * 1. Simply discard the marker and let the entropy decoder resume at next |
---|
950 | * byte of file. |
---|
951 | * 2. Read forward until we find another marker, discarding intervening |
---|
952 | * data. (In theory we could look ahead within the current bufferload, |
---|
953 | * without having to discard data if we don't find the desired marker. |
---|
954 | * This idea is not implemented here, in part because it makes behavior |
---|
955 | * dependent on buffer size and chance buffer-boundary positions.) |
---|
956 | * 3. Leave the marker unread (by failing to zero cinfo->unread_marker). |
---|
957 | * This will cause the entropy decoder to process an empty data segment, |
---|
958 | * inserting dummy zeroes, and then we will reprocess the marker. |
---|
959 | * |
---|
960 | * #2 is appropriate if we think the desired marker lies ahead, while #3 is |
---|
961 | * appropriate if the found marker is a future restart marker (indicating |
---|
962 | * that we have missed the desired restart marker, probably because it got |
---|
963 | * corrupted). |
---|
964 | * We apply #2 or #3 if the found marker is a restart marker no more than |
---|
965 | * two counts behind or ahead of the expected one. We also apply #2 if the |
---|
966 | * found marker is not a legal JPEG marker code (it's certainly bogus data). |
---|
967 | * If the found marker is a restart marker more than 2 counts away, we do #1 |
---|
968 | * (too much risk that the marker is erroneous; with luck we will be able to |
---|
969 | * resync at some future point). |
---|
970 | * For any valid non-restart JPEG marker, we apply #3. This keeps us from |
---|
971 | * overrunning the end of a scan. An implementation limited to single-scan |
---|
972 | * files might find it better to apply #2 for markers other than EOI, since |
---|
973 | * any other marker would have to be bogus data in that case. |
---|
974 | */ |
---|
975 | |
---|
976 | GLOBAL(boolean) |
---|
977 | jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired) |
---|
978 | { |
---|
979 | int marker = cinfo->unread_marker; |
---|
980 | int action = 1; |
---|
981 | |
---|
982 | /* Always put up a warning. */ |
---|
983 | WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired); |
---|
984 | |
---|
985 | /* Outer loop handles repeated decision after scanning forward. */ |
---|
986 | for (;;) { |
---|
987 | if (marker < (int) M_SOF0) |
---|
988 | action = 2; /* invalid marker */ |
---|
989 | else if (marker < (int) M_RST0 || marker > (int) M_RST7) |
---|
990 | action = 3; /* valid non-restart marker */ |
---|
991 | else { |
---|
992 | if (marker == ((int) M_RST0 + ((desired+1) & 7)) || |
---|
993 | marker == ((int) M_RST0 + ((desired+2) & 7))) |
---|
994 | action = 3; /* one of the next two expected restarts */ |
---|
995 | else if (marker == ((int) M_RST0 + ((desired-1) & 7)) || |
---|
996 | marker == ((int) M_RST0 + ((desired-2) & 7))) |
---|
997 | action = 2; /* a prior restart, so advance */ |
---|
998 | else |
---|
999 | action = 1; /* desired restart or too far away */ |
---|
1000 | } |
---|
1001 | TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action); |
---|
1002 | switch (action) { |
---|
1003 | case 1: |
---|
1004 | /* Discard marker and let entropy decoder resume processing. */ |
---|
1005 | cinfo->unread_marker = 0; |
---|
1006 | return TRUE; |
---|
1007 | case 2: |
---|
1008 | /* Scan to the next marker, and repeat the decision loop. */ |
---|
1009 | if (! next_marker(cinfo)) |
---|
1010 | return FALSE; |
---|
1011 | marker = cinfo->unread_marker; |
---|
1012 | break; |
---|
1013 | case 3: |
---|
1014 | /* Return without advancing past this marker. */ |
---|
1015 | /* Entropy decoder will be forced to process an empty segment. */ |
---|
1016 | return TRUE; |
---|
1017 | } |
---|
1018 | } /* end loop */ |
---|
1019 | } |
---|
1020 | |
---|
1021 | |
---|
1022 | /* |
---|
1023 | * Reset marker processing state to begin a fresh datastream. |
---|
1024 | */ |
---|
1025 | |
---|
1026 | METHODDEF(void) |
---|
1027 | reset_marker_reader (j_decompress_ptr cinfo) |
---|
1028 | { |
---|
1029 | cinfo->comp_info = NULL; /* until allocated by get_sof */ |
---|
1030 | cinfo->input_scan_number = 0; /* no SOS seen yet */ |
---|
1031 | cinfo->unread_marker = 0; /* no pending marker */ |
---|
1032 | cinfo->marker->saw_SOI = FALSE; /* set internal state too */ |
---|
1033 | cinfo->marker->saw_SOF = FALSE; |
---|
1034 | cinfo->marker->discarded_bytes = 0; |
---|
1035 | } |
---|
1036 | |
---|
1037 | |
---|
1038 | /* |
---|
1039 | * Initialize the marker reader module. |
---|
1040 | * This is called only once, when the decompression object is created. |
---|
1041 | */ |
---|
1042 | |
---|
1043 | GLOBAL(void) |
---|
1044 | jinit_marker_reader (j_decompress_ptr cinfo) |
---|
1045 | { |
---|
1046 | int i; |
---|
1047 | |
---|
1048 | /* Create subobject in permanent pool */ |
---|
1049 | cinfo->marker = (struct jpeg_marker_reader *) |
---|
1050 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
---|
1051 | SIZEOF(struct jpeg_marker_reader)); |
---|
1052 | /* Initialize method pointers */ |
---|
1053 | cinfo->marker->reset_marker_reader = reset_marker_reader; |
---|
1054 | cinfo->marker->read_markers = read_markers; |
---|
1055 | cinfo->marker->read_restart_marker = read_restart_marker; |
---|
1056 | cinfo->marker->process_COM = skip_variable; |
---|
1057 | for (i = 0; i < 16; i++) |
---|
1058 | cinfo->marker->process_APPn[i] = skip_variable; |
---|
1059 | cinfo->marker->process_APPn[0] = get_app0; |
---|
1060 | cinfo->marker->process_APPn[14] = get_app14; |
---|
1061 | /* Reset marker processing state */ |
---|
1062 | reset_marker_reader(cinfo); |
---|
1063 | } |
---|