-/*\r
- * jdmarker.c\r
- *\r
- * Copyright (C) 1991-1995, Thomas G. Lane.\r
- * This file is part of the Independent JPEG Group's software.\r
- * For conditions of distribution and use, see the accompanying README file.\r
- *\r
- * This file contains routines to decode JPEG datastream markers.\r
- * Most of the complexity arises from our desire to support input\r
- * suspension: if not all of the data for a marker is available,\r
- * we must exit back to the application. On resumption, we reprocess\r
- * the marker.\r
- */\r
-\r
-#define JPEG_INTERNALS\r
-#include "jinclude.h"\r
-#include "radiant_jpeglib.h"\r
-\r
-\r
-typedef enum { /* JPEG marker codes */\r
- M_SOF0 = 0xc0,\r
- M_SOF1 = 0xc1,\r
- M_SOF2 = 0xc2,\r
- M_SOF3 = 0xc3,\r
- \r
- M_SOF5 = 0xc5,\r
- M_SOF6 = 0xc6,\r
- M_SOF7 = 0xc7,\r
- \r
- M_JPG = 0xc8,\r
- M_SOF9 = 0xc9,\r
- M_SOF10 = 0xca,\r
- M_SOF11 = 0xcb,\r
- \r
- M_SOF13 = 0xcd,\r
- M_SOF14 = 0xce,\r
- M_SOF15 = 0xcf,\r
- \r
- M_DHT = 0xc4,\r
- \r
- M_DAC = 0xcc,\r
- \r
- M_RST0 = 0xd0,\r
- M_RST1 = 0xd1,\r
- M_RST2 = 0xd2,\r
- M_RST3 = 0xd3,\r
- M_RST4 = 0xd4,\r
- M_RST5 = 0xd5,\r
- M_RST6 = 0xd6,\r
- M_RST7 = 0xd7,\r
- \r
- M_SOI = 0xd8,\r
- M_EOI = 0xd9,\r
- M_SOS = 0xda,\r
- M_DQT = 0xdb,\r
- M_DNL = 0xdc,\r
- M_DRI = 0xdd,\r
- M_DHP = 0xde,\r
- M_EXP = 0xdf,\r
- \r
- M_APP0 = 0xe0,\r
- M_APP1 = 0xe1,\r
- M_APP2 = 0xe2,\r
- M_APP3 = 0xe3,\r
- M_APP4 = 0xe4,\r
- M_APP5 = 0xe5,\r
- M_APP6 = 0xe6,\r
- M_APP7 = 0xe7,\r
- M_APP8 = 0xe8,\r
- M_APP9 = 0xe9,\r
- M_APP10 = 0xea,\r
- M_APP11 = 0xeb,\r
- M_APP12 = 0xec,\r
- M_APP13 = 0xed,\r
- M_APP14 = 0xee,\r
- M_APP15 = 0xef,\r
- \r
- M_JPG0 = 0xf0,\r
- M_JPG13 = 0xfd,\r
- M_COM = 0xfe,\r
- \r
- M_TEM = 0x01,\r
- \r
- M_ERROR = 0x100\r
-} JPEG_MARKER;\r
-\r
-\r
-/*\r
- * Macros for fetching data from the data source module.\r
- *\r
- * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect\r
- * the current restart point; we update them only when we have reached a\r
- * suitable place to restart if a suspension occurs.\r
- */\r
-\r
-/* Declare and initialize local copies of input pointer/count */\r
-#define INPUT_VARS(cinfo) \\r
- struct jpeg_source_mgr * datasrc = (cinfo)->src; \\r
- const JOCTET * next_input_byte = datasrc->next_input_byte; \\r
- size_t bytes_in_buffer = datasrc->bytes_in_buffer\r
-\r
-/* Unload the local copies --- do this only at a restart boundary */\r
-#define INPUT_SYNC(cinfo) \\r
- ( datasrc->next_input_byte = next_input_byte, \\r
- datasrc->bytes_in_buffer = bytes_in_buffer )\r
-\r
-/* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */\r
-#define INPUT_RELOAD(cinfo) \\r
- ( next_input_byte = datasrc->next_input_byte, \\r
- bytes_in_buffer = datasrc->bytes_in_buffer )\r
-\r
-/* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.\r
- * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,\r
- * but we must reload the local copies after a successful fill.\r
- */\r
-#define MAKE_BYTE_AVAIL(cinfo,action) \\r
- if (bytes_in_buffer == 0) { \\r
- if (! (*datasrc->fill_input_buffer) (cinfo)) \\r
- { action; } \\r
- INPUT_RELOAD(cinfo); \\r
- } \\r
- bytes_in_buffer--\r
-\r
-/* Read a byte into variable V.\r
- * If must suspend, take the specified action (typically "return FALSE").\r
- */\r
-#define INPUT_BYTE(cinfo,V,action) \\r
- MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \\r
- V = GETJOCTET(*next_input_byte++); )\r
-\r
-/* As above, but read two bytes interpreted as an unsigned 16-bit integer.\r
- * V should be declared unsigned int or perhaps INT32.\r
- */\r
-#define INPUT_2BYTES(cinfo,V,action) \\r
- MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \\r
- V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \\r
- MAKE_BYTE_AVAIL(cinfo,action); \\r
- V += GETJOCTET(*next_input_byte++); )\r
-\r
-\r
-/*\r
- * Routines to process JPEG markers.\r
- *\r
- * Entry condition: JPEG marker itself has been read and its code saved\r
- * in cinfo->unread_marker; input restart point is just after the marker.\r
- *\r
- * Exit: if return TRUE, have read and processed any parameters, and have\r
- * updated the restart point to point after the parameters.\r
- * If return FALSE, was forced to suspend before reaching end of\r
- * marker parameters; restart point has not been moved. Same routine\r
- * will be called again after application supplies more input data.\r
- *\r
- * This approach to suspension assumes that all of a marker's parameters can\r
- * fit into a single input bufferload. This should hold for "normal"\r
- * markers. Some COM/APPn markers might have large parameter segments,\r
- * but we use skip_input_data to get past those, and thereby put the problem\r
- * on the source manager's shoulders.\r
- *\r
- * Note that we don't bother to avoid duplicate trace messages if a\r
- * suspension occurs within marker parameters. Other side effects\r
- * require more care.\r
- */\r
-\r
-\r
-LOCAL boolean\r
-get_soi (j_decompress_ptr cinfo)\r
-/* Process an SOI marker */\r
-{\r
- int i;\r
- \r
- TRACEMS(cinfo, 1, JTRC_SOI);\r
-\r
- if (cinfo->marker->saw_SOI)\r
- ERREXIT(cinfo, JERR_SOI_DUPLICATE);\r
-\r
- /* Reset all parameters that are defined to be reset by SOI */\r
-\r
- for (i = 0; i < NUM_ARITH_TBLS; i++) {\r
- cinfo->arith_dc_L[i] = 0;\r
- cinfo->arith_dc_U[i] = 1;\r
- cinfo->arith_ac_K[i] = 5;\r
- }\r
- cinfo->restart_interval = 0;\r
-\r
- /* Set initial assumptions for colorspace etc */\r
-\r
- cinfo->jpeg_color_space = JCS_UNKNOWN;\r
- cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */\r
-\r
- cinfo->saw_JFIF_marker = FALSE;\r
- cinfo->density_unit = 0; /* set default JFIF APP0 values */\r
- cinfo->X_density = 1;\r
- cinfo->Y_density = 1;\r
- cinfo->saw_Adobe_marker = FALSE;\r
- cinfo->Adobe_transform = 0;\r
-\r
- cinfo->marker->saw_SOI = TRUE;\r
-\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)\r
-/* Process a SOFn marker */\r
-{\r
- INT32 length;\r
- int c, ci;\r
- jpeg_component_info * compptr;\r
- INPUT_VARS(cinfo);\r
-\r
- cinfo->progressive_mode = is_prog;\r
- cinfo->arith_code = is_arith;\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
-\r
- INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);\r
- INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);\r
- INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);\r
- INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);\r
-\r
- length -= 8;\r
-\r
- TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,\r
- (int) cinfo->image_width, (int) cinfo->image_height,\r
- cinfo->num_components);\r
-\r
- if (cinfo->marker->saw_SOF)\r
- ERREXIT(cinfo, JERR_SOF_DUPLICATE);\r
-\r
- /* We don't support files in which the image height is initially specified */\r
- /* as 0 and is later redefined by DNL. As long as we have to check that, */\r
- /* might as well have a general sanity check. */\r
- if (cinfo->image_height <= 0 || cinfo->image_width <= 0\r
- || cinfo->num_components <= 0)\r
- ERREXIT(cinfo, JERR_EMPTY_IMAGE);\r
-\r
- if (length != (cinfo->num_components * 3))\r
- ERREXIT(cinfo, JERR_BAD_LENGTH);\r
-\r
- if (cinfo->comp_info == NULL) /* do only once, even if suspend */\r
- cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)\r
- ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
- cinfo->num_components * SIZEOF(jpeg_component_info));\r
- \r
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
- ci++, compptr++) {\r
- compptr->component_index = ci;\r
- INPUT_BYTE(cinfo, compptr->component_id, return FALSE);\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- compptr->h_samp_factor = (c >> 4) & 15;\r
- compptr->v_samp_factor = (c ) & 15;\r
- INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);\r
-\r
- TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,\r
- compptr->component_id, compptr->h_samp_factor,\r
- compptr->v_samp_factor, compptr->quant_tbl_no);\r
- }\r
-\r
- cinfo->marker->saw_SOF = TRUE;\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-get_sos (j_decompress_ptr cinfo)\r
-/* Process a SOS marker */\r
-{\r
- INT32 length;\r
- int i, ci, n, c, cc;\r
- jpeg_component_info * compptr;\r
- INPUT_VARS(cinfo);\r
-\r
- if (! cinfo->marker->saw_SOF)\r
- ERREXIT(cinfo, JERR_SOS_NO_SOF);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
-\r
- INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */\r
-\r
- if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)\r
- ERREXIT(cinfo, JERR_BAD_LENGTH);\r
-\r
- TRACEMS1(cinfo, 1, JTRC_SOS, n);\r
-\r
- cinfo->comps_in_scan = n;\r
-\r
- /* Collect the component-spec parameters */\r
-\r
- for (i = 0; i < n; i++) {\r
- INPUT_BYTE(cinfo, cc, return FALSE);\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- \r
- for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
- ci++, compptr++) {\r
- if (cc == compptr->component_id)\r
- goto id_found;\r
- }\r
-\r
- ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);\r
-\r
- id_found:\r
-\r
- cinfo->cur_comp_info[i] = compptr;\r
- compptr->dc_tbl_no = (c >> 4) & 15;\r
- compptr->ac_tbl_no = (c ) & 15;\r
- \r
- TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,\r
- compptr->dc_tbl_no, compptr->ac_tbl_no);\r
- }\r
-\r
- /* Collect the additional scan parameters Ss, Se, Ah/Al. */\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- cinfo->Ss = c;\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- cinfo->Se = c;\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- cinfo->Ah = (c >> 4) & 15;\r
- cinfo->Al = (c ) & 15;\r
-\r
- TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,\r
- cinfo->Ah, cinfo->Al);\r
-\r
- /* Prepare to scan data & restart markers */\r
- cinfo->marker->next_restart_num = 0;\r
-\r
- /* Count another SOS marker */\r
- cinfo->input_scan_number++;\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-METHODDEF boolean\r
-get_app0 (j_decompress_ptr cinfo)\r
-/* Process an APP0 marker */\r
-{\r
-#define JFIF_LEN 14\r
- INT32 length;\r
- UINT8 b[JFIF_LEN];\r
- int buffp;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- length -= 2;\r
-\r
- /* See if a JFIF APP0 marker is present */\r
-\r
- if (length >= JFIF_LEN) {\r
- for (buffp = 0; buffp < JFIF_LEN; buffp++)\r
- INPUT_BYTE(cinfo, b[buffp], return FALSE);\r
- length -= JFIF_LEN;\r
-\r
- if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {\r
- /* Found JFIF APP0 marker: check version */\r
- /* Major version must be 1, anything else signals an incompatible change.\r
- * We used to treat this as an error, but now it's a nonfatal warning,\r
- * because some bozo at Hijaak couldn't read the spec.\r
- * Minor version should be 0..2, but process anyway if newer.\r
- */\r
- if (b[5] != 1)\r
- WARNMS2(cinfo, JWRN_JFIF_MAJOR, b[5], b[6]);\r
- else if (b[6] > 2)\r
- TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]);\r
- /* Save info */\r
- cinfo->saw_JFIF_marker = TRUE;\r
- cinfo->density_unit = b[7];\r
- cinfo->X_density = (b[8] << 8) + b[9];\r
- cinfo->Y_density = (b[10] << 8) + b[11];\r
- TRACEMS3(cinfo, 1, JTRC_JFIF,\r
- cinfo->X_density, cinfo->Y_density, cinfo->density_unit);\r
- if (b[12] | b[13])\r
- TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);\r
- if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))\r
- TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length);\r
- } else {\r
- /* Start of APP0 does not match "JFIF" */\r
- TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN);\r
- }\r
- } else {\r
- /* Too short to be JFIF marker */\r
- TRACEMS1(cinfo, 1, JTRC_APP0, (int) length);\r
- }\r
-\r
- INPUT_SYNC(cinfo);\r
- if (length > 0) /* skip any remaining data -- could be lots */\r
- (*cinfo->src->skip_input_data) (cinfo, (long) length);\r
-\r
- return TRUE;\r
-}\r
-\r
-\r
-METHODDEF boolean\r
-get_app14 (j_decompress_ptr cinfo)\r
-/* Process an APP14 marker */\r
-{\r
-#define ADOBE_LEN 12\r
- INT32 length;\r
- UINT8 b[ADOBE_LEN];\r
- int buffp;\r
- unsigned int version, flags0, flags1, transform;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- length -= 2;\r
-\r
- /* See if an Adobe APP14 marker is present */\r
-\r
- if (length >= ADOBE_LEN) {\r
- for (buffp = 0; buffp < ADOBE_LEN; buffp++)\r
- INPUT_BYTE(cinfo, b[buffp], return FALSE);\r
- length -= ADOBE_LEN;\r
-\r
- if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) {\r
- /* Found Adobe APP14 marker */\r
- version = (b[5] << 8) + b[6];\r
- flags0 = (b[7] << 8) + b[8];\r
- flags1 = (b[9] << 8) + b[10];\r
- transform = b[11];\r
- TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);\r
- cinfo->saw_Adobe_marker = TRUE;\r
- cinfo->Adobe_transform = (UINT8) transform;\r
- } else {\r
- /* Start of APP14 does not match "Adobe" */\r
- TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN);\r
- }\r
- } else {\r
- /* Too short to be Adobe marker */\r
- TRACEMS1(cinfo, 1, JTRC_APP14, (int) length);\r
- }\r
-\r
- INPUT_SYNC(cinfo);\r
- if (length > 0) /* skip any remaining data -- could be lots */\r
- (*cinfo->src->skip_input_data) (cinfo, (long) length);\r
-\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-get_dac (j_decompress_ptr cinfo)\r
-/* Process a DAC marker */\r
-{\r
- INT32 length;\r
- int index, val;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- length -= 2;\r
- \r
- while (length > 0) {\r
- INPUT_BYTE(cinfo, index, return FALSE);\r
- INPUT_BYTE(cinfo, val, return FALSE);\r
-\r
- length -= 2;\r
-\r
- TRACEMS2(cinfo, 1, JTRC_DAC, index, val);\r
-\r
- if (index < 0 || index >= (2*NUM_ARITH_TBLS))\r
- ERREXIT1(cinfo, JERR_DAC_INDEX, index);\r
-\r
- if (index >= NUM_ARITH_TBLS) { /* define AC table */\r
- cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;\r
- } else { /* define DC table */\r
- cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);\r
- cinfo->arith_dc_U[index] = (UINT8) (val >> 4);\r
- if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])\r
- ERREXIT1(cinfo, JERR_DAC_VALUE, val);\r
- }\r
- }\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-get_dht (j_decompress_ptr cinfo)\r
-/* Process a DHT marker */\r
-{\r
- INT32 length;\r
- UINT8 bits[17];\r
- UINT8 huffval[256];\r
- int i, index, count;\r
- JHUFF_TBL **htblptr;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- length -= 2;\r
- \r
- while (length > 0) {\r
- INPUT_BYTE(cinfo, index, return FALSE);\r
-\r
- TRACEMS1(cinfo, 1, JTRC_DHT, index);\r
- \r
- bits[0] = 0;\r
- count = 0;\r
- for (i = 1; i <= 16; i++) {\r
- INPUT_BYTE(cinfo, bits[i], return FALSE);\r
- count += bits[i];\r
- }\r
-\r
- length -= 1 + 16;\r
-\r
- TRACEMS8(cinfo, 2, JTRC_HUFFBITS,\r
- bits[1], bits[2], bits[3], bits[4],\r
- bits[5], bits[6], bits[7], bits[8]);\r
- TRACEMS8(cinfo, 2, JTRC_HUFFBITS,\r
- bits[9], bits[10], bits[11], bits[12],\r
- bits[13], bits[14], bits[15], bits[16]);\r
-\r
- if (count > 256 || ((INT32) count) > length)\r
- ERREXIT(cinfo, JERR_DHT_COUNTS);\r
-\r
- for (i = 0; i < count; i++)\r
- INPUT_BYTE(cinfo, huffval[i], return FALSE);\r
-\r
- length -= count;\r
-\r
- if (index & 0x10) { /* AC table definition */\r
- index -= 0x10;\r
- htblptr = &cinfo->ac_huff_tbl_ptrs[index];\r
- } else { /* DC table definition */\r
- htblptr = &cinfo->dc_huff_tbl_ptrs[index];\r
- }\r
-\r
- if (index < 0 || index >= NUM_HUFF_TBLS)\r
- ERREXIT1(cinfo, JERR_DHT_INDEX, index);\r
-\r
- if (*htblptr == NULL)\r
- *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);\r
- \r
- MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));\r
- MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));\r
- }\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-get_dqt (j_decompress_ptr cinfo)\r
-/* Process a DQT marker */\r
-{\r
- INT32 length;\r
- int n, i, prec;\r
- unsigned int tmp;\r
- JQUANT_TBL *quant_ptr;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- length -= 2;\r
-\r
- while (length > 0) {\r
- INPUT_BYTE(cinfo, n, return FALSE);\r
- prec = n >> 4;\r
- n &= 0x0F;\r
-\r
- TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);\r
-\r
- if (n >= NUM_QUANT_TBLS)\r
- ERREXIT1(cinfo, JERR_DQT_INDEX, n);\r
- \r
- if (cinfo->quant_tbl_ptrs[n] == NULL)\r
- cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);\r
- quant_ptr = cinfo->quant_tbl_ptrs[n];\r
-\r
- for (i = 0; i < DCTSIZE2; i++) {\r
- if (prec)\r
- INPUT_2BYTES(cinfo, tmp, return FALSE);\r
- else\r
- INPUT_BYTE(cinfo, tmp, return FALSE);\r
- quant_ptr->quantval[i] = (UINT16) tmp;\r
- }\r
-\r
- for (i = 0; i < DCTSIZE2; i += 8) {\r
- TRACEMS8(cinfo, 2, JTRC_QUANTVALS,\r
- quant_ptr->quantval[i ], quant_ptr->quantval[i+1],\r
- quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],\r
- quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],\r
- quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);\r
- }\r
-\r
- length -= DCTSIZE2+1;\r
- if (prec) length -= DCTSIZE2;\r
- }\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-get_dri (j_decompress_ptr cinfo)\r
-/* Process a DRI marker */\r
-{\r
- INT32 length;\r
- unsigned int tmp;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- \r
- if (length != 4)\r
- ERREXIT(cinfo, JERR_BAD_LENGTH);\r
-\r
- INPUT_2BYTES(cinfo, tmp, return FALSE);\r
-\r
- TRACEMS1(cinfo, 1, JTRC_DRI, tmp);\r
-\r
- cinfo->restart_interval = tmp;\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-METHODDEF boolean\r
-skip_variable (j_decompress_ptr cinfo)\r
-/* Skip over an unknown or uninteresting variable-length marker */\r
-{\r
- INT32 length;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_2BYTES(cinfo, length, return FALSE);\r
- \r
- TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);\r
-\r
- INPUT_SYNC(cinfo); /* do before skip_input_data */\r
- (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L);\r
-\r
- return TRUE;\r
-}\r
-\r
-\r
-/*\r
- * Find the next JPEG marker, save it in cinfo->unread_marker.\r
- * Returns FALSE if had to suspend before reaching a marker;\r
- * in that case cinfo->unread_marker is unchanged.\r
- *\r
- * Note that the result might not be a valid marker code,\r
- * but it will never be 0 or FF.\r
- */\r
-\r
-LOCAL boolean\r
-next_marker (j_decompress_ptr cinfo)\r
-{\r
- int c;\r
- INPUT_VARS(cinfo);\r
-\r
- for (;;) {\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- /* Skip any non-FF bytes.\r
- * This may look a bit inefficient, but it will not occur in a valid file.\r
- * We sync after each discarded byte so that a suspending data source\r
- * can discard the byte from its buffer.\r
- */\r
- while (c != 0xFF) {\r
- cinfo->marker->discarded_bytes++;\r
- INPUT_SYNC(cinfo);\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- }\r
- /* This loop swallows any duplicate FF bytes. Extra FFs are legal as\r
- * pad bytes, so don't count them in discarded_bytes. We assume there\r
- * will not be so many consecutive FF bytes as to overflow a suspending\r
- * data source's input buffer.\r
- */\r
- do {\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- } while (c == 0xFF);\r
- if (c != 0)\r
- break; /* found a valid marker, exit loop */\r
- /* Reach here if we found a stuffed-zero data sequence (FF/00).\r
- * Discard it and loop back to try again.\r
- */\r
- cinfo->marker->discarded_bytes += 2;\r
- INPUT_SYNC(cinfo);\r
- }\r
-\r
- if (cinfo->marker->discarded_bytes != 0) {\r
- WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);\r
- cinfo->marker->discarded_bytes = 0;\r
- }\r
-\r
- cinfo->unread_marker = c;\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-LOCAL boolean\r
-first_marker (j_decompress_ptr cinfo)\r
-/* Like next_marker, but used to obtain the initial SOI marker. */\r
-/* For this marker, we do not allow preceding garbage or fill; otherwise,\r
- * we might well scan an entire input file before realizing it ain't JPEG.\r
- * If an application wants to process non-JFIF files, it must seek to the\r
- * SOI before calling the JPEG library.\r
- */\r
-{\r
- int c, c2;\r
- INPUT_VARS(cinfo);\r
-\r
- INPUT_BYTE(cinfo, c, return FALSE);\r
- INPUT_BYTE(cinfo, c2, return FALSE);\r
- if (c != 0xFF || c2 != (int) M_SOI)\r
- ERREXIT2(cinfo, JERR_NO_SOI, c, c2);\r
-\r
- cinfo->unread_marker = c2;\r
-\r
- INPUT_SYNC(cinfo);\r
- return TRUE;\r
-}\r
-\r
-\r
-/*\r
- * Read markers until SOS or EOI.\r
- *\r
- * Returns same codes as are defined for jpeg_consume_input:\r
- * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.\r
- */\r
-\r
-METHODDEF int\r
-read_markers (j_decompress_ptr cinfo)\r
-{\r
- /* Outer loop repeats once for each marker. */\r
- for (;;) {\r
- /* Collect the marker proper, unless we already did. */\r
- /* NB: first_marker() enforces the requirement that SOI appear first. */\r
- if (cinfo->unread_marker == 0) {\r
- if (! cinfo->marker->saw_SOI) {\r
- if (! first_marker(cinfo))\r
- return JPEG_SUSPENDED;\r
- } else {\r
- if (! next_marker(cinfo))\r
- return JPEG_SUSPENDED;\r
- }\r
- }\r
- /* At this point cinfo->unread_marker contains the marker code and the\r
- * input point is just past the marker proper, but before any parameters.\r
- * A suspension will cause us to return with this state still true.\r
- */\r
- switch (cinfo->unread_marker) {\r
- case M_SOI:\r
- if (! get_soi(cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- case M_SOF0: /* Baseline */\r
- case M_SOF1: /* Extended sequential, Huffman */\r
- if (! get_sof(cinfo, FALSE, FALSE))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- case M_SOF2: /* Progressive, Huffman */\r
- if (! get_sof(cinfo, TRUE, FALSE))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- case M_SOF9: /* Extended sequential, arithmetic */\r
- if (! get_sof(cinfo, FALSE, TRUE))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- case M_SOF10: /* Progressive, arithmetic */\r
- if (! get_sof(cinfo, TRUE, TRUE))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- /* Currently unsupported SOFn types */\r
- case M_SOF3: /* Lossless, Huffman */\r
- case M_SOF5: /* Differential sequential, Huffman */\r
- case M_SOF6: /* Differential progressive, Huffman */\r
- case M_SOF7: /* Differential lossless, Huffman */\r
- case M_JPG: /* Reserved for JPEG extensions */\r
- case M_SOF11: /* Lossless, arithmetic */\r
- case M_SOF13: /* Differential sequential, arithmetic */\r
- case M_SOF14: /* Differential progressive, arithmetic */\r
- case M_SOF15: /* Differential lossless, arithmetic */\r
- ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);\r
- break;\r
-\r
- case M_SOS:\r
- if (! get_sos(cinfo))\r
- return JPEG_SUSPENDED;\r
- cinfo->unread_marker = 0; /* processed the marker */\r
- return JPEG_REACHED_SOS;\r
- \r
- case M_EOI:\r
- TRACEMS(cinfo, 1, JTRC_EOI);\r
- cinfo->unread_marker = 0; /* processed the marker */\r
- return JPEG_REACHED_EOI;\r
- \r
- case M_DAC:\r
- if (! get_dac(cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
- \r
- case M_DHT:\r
- if (! get_dht(cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
- \r
- case M_DQT:\r
- if (! get_dqt(cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
- \r
- case M_DRI:\r
- if (! get_dri(cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
- \r
- case M_APP0:\r
- case M_APP1:\r
- case M_APP2:\r
- case M_APP3:\r
- case M_APP4:\r
- case M_APP5:\r
- case M_APP6:\r
- case M_APP7:\r
- case M_APP8:\r
- case M_APP9:\r
- case M_APP10:\r
- case M_APP11:\r
- case M_APP12:\r
- case M_APP13:\r
- case M_APP14:\r
- case M_APP15:\r
- if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
- \r
- case M_COM:\r
- if (! (*cinfo->marker->process_COM) (cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- case M_RST0: /* these are all parameterless */\r
- case M_RST1:\r
- case M_RST2:\r
- case M_RST3:\r
- case M_RST4:\r
- case M_RST5:\r
- case M_RST6:\r
- case M_RST7:\r
- case M_TEM:\r
- TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);\r
- break;\r
-\r
- case M_DNL: /* Ignore DNL ... perhaps the wrong thing */\r
- if (! skip_variable(cinfo))\r
- return JPEG_SUSPENDED;\r
- break;\r
-\r
- default: /* must be DHP, EXP, JPGn, or RESn */\r
- /* For now, we treat the reserved markers as fatal errors since they are\r
- * likely to be used to signal incompatible JPEG Part 3 extensions.\r
- * Once the JPEG 3 version-number marker is well defined, this code\r
- * ought to change!\r
- */\r
- ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);\r
- break;\r
- }\r
- /* Successfully processed marker, so reset state variable */\r
- cinfo->unread_marker = 0;\r
- } /* end loop */\r
-}\r
-\r
-\r
-/*\r
- * Read a restart marker, which is expected to appear next in the datastream;\r
- * if the marker is not there, take appropriate recovery action.\r
- * Returns FALSE if suspension is required.\r
- *\r
- * This is called by the entropy decoder after it has read an appropriate\r
- * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder\r
- * has already read a marker from the data source. Under normal conditions\r
- * cinfo->unread_marker will be reset to 0 before returning; if not reset,\r
- * it holds a marker which the decoder will be unable to read past.\r
- */\r
-\r
-METHODDEF boolean\r
-read_restart_marker (j_decompress_ptr cinfo)\r
-{\r
- /* Obtain a marker unless we already did. */\r
- /* Note that next_marker will complain if it skips any data. */\r
- if (cinfo->unread_marker == 0) {\r
- if (! next_marker(cinfo))\r
- return FALSE;\r
- }\r
-\r
- if (cinfo->unread_marker ==\r
- ((int) M_RST0 + cinfo->marker->next_restart_num)) {\r
- /* Normal case --- swallow the marker and let entropy decoder continue */\r
- TRACEMS1(cinfo, 2, JTRC_RST, cinfo->marker->next_restart_num);\r
- cinfo->unread_marker = 0;\r
- } else {\r
- /* Uh-oh, the restart markers have been messed up. */\r
- /* Let the data source manager determine how to resync. */\r
- if (! (*cinfo->src->resync_to_restart) (cinfo,\r
- cinfo->marker->next_restart_num))\r
- return FALSE;\r
- }\r
-\r
- /* Update next-restart state */\r
- cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;\r
-\r
- return TRUE;\r
-}\r
-\r
-\r
-/*\r
- * This is the default resync_to_restart method for data source managers\r
- * to use if they don't have any better approach. Some data source managers\r
- * may be able to back up, or may have additional knowledge about the data\r
- * which permits a more intelligent recovery strategy; such managers would\r
- * presumably supply their own resync method.\r
- *\r
- * read_restart_marker calls resync_to_restart if it finds a marker other than\r
- * the restart marker it was expecting. (This code is *not* used unless\r
- * a nonzero restart interval has been declared.) cinfo->unread_marker is\r
- * the marker code actually found (might be anything, except 0 or FF).\r
- * The desired restart marker number (0..7) is passed as a parameter.\r
- * This routine is supposed to apply whatever error recovery strategy seems\r
- * appropriate in order to position the input stream to the next data segment.\r
- * Note that cinfo->unread_marker is treated as a marker appearing before\r
- * the current data-source input point; usually it should be reset to zero\r
- * before returning.\r
- * Returns FALSE if suspension is required.\r
- *\r
- * This implementation is substantially constrained by wanting to treat the\r
- * input as a data stream; this means we can't back up. Therefore, we have\r
- * only the following actions to work with:\r
- * 1. Simply discard the marker and let the entropy decoder resume at next\r
- * byte of file.\r
- * 2. Read forward until we find another marker, discarding intervening\r
- * data. (In theory we could look ahead within the current bufferload,\r
- * without having to discard data if we don't find the desired marker.\r
- * This idea is not implemented here, in part because it makes behavior\r
- * dependent on buffer size and chance buffer-boundary positions.)\r
- * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).\r
- * This will cause the entropy decoder to process an empty data segment,\r
- * inserting dummy zeroes, and then we will reprocess the marker.\r
- *\r
- * #2 is appropriate if we think the desired marker lies ahead, while #3 is\r
- * appropriate if the found marker is a future restart marker (indicating\r
- * that we have missed the desired restart marker, probably because it got\r
- * corrupted).\r
- * We apply #2 or #3 if the found marker is a restart marker no more than\r
- * two counts behind or ahead of the expected one. We also apply #2 if the\r
- * found marker is not a legal JPEG marker code (it's certainly bogus data).\r
- * If the found marker is a restart marker more than 2 counts away, we do #1\r
- * (too much risk that the marker is erroneous; with luck we will be able to\r
- * resync at some future point).\r
- * For any valid non-restart JPEG marker, we apply #3. This keeps us from\r
- * overrunning the end of a scan. An implementation limited to single-scan\r
- * files might find it better to apply #2 for markers other than EOI, since\r
- * any other marker would have to be bogus data in that case.\r
- */\r
-\r
-GLOBAL boolean\r
-jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)\r
-{\r
- int marker = cinfo->unread_marker;\r
- int action = 1;\r
- \r
- /* Always put up a warning. */\r
- WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);\r
- \r
- /* Outer loop handles repeated decision after scanning forward. */\r
- for (;;) {\r
- if (marker < (int) M_SOF0)\r
- action = 2; /* invalid marker */\r
- else if (marker < (int) M_RST0 || marker > (int) M_RST7)\r
- action = 3; /* valid non-restart marker */\r
- else {\r
- if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||\r
- marker == ((int) M_RST0 + ((desired+2) & 7)))\r
- action = 3; /* one of the next two expected restarts */\r
- else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||\r
- marker == ((int) M_RST0 + ((desired-2) & 7)))\r
- action = 2; /* a prior restart, so advance */\r
- else\r
- action = 1; /* desired restart or too far away */\r
- }\r
- TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);\r
- switch (action) {\r
- case 1:\r
- /* Discard marker and let entropy decoder resume processing. */\r
- cinfo->unread_marker = 0;\r
- return TRUE;\r
- case 2:\r
- /* Scan to the next marker, and repeat the decision loop. */\r
- if (! next_marker(cinfo))\r
- return FALSE;\r
- marker = cinfo->unread_marker;\r
- break;\r
- case 3:\r
- /* Return without advancing past this marker. */\r
- /* Entropy decoder will be forced to process an empty segment. */\r
- return TRUE;\r
- }\r
- } /* end loop */\r
-}\r
-\r
-\r
-/*\r
- * Reset marker processing state to begin a fresh datastream.\r
- */\r
-\r
-METHODDEF void\r
-reset_marker_reader (j_decompress_ptr cinfo)\r
-{\r
- cinfo->comp_info = NULL; /* until allocated by get_sof */\r
- cinfo->input_scan_number = 0; /* no SOS seen yet */\r
- cinfo->unread_marker = 0; /* no pending marker */\r
- cinfo->marker->saw_SOI = FALSE; /* set internal state too */\r
- cinfo->marker->saw_SOF = FALSE;\r
- cinfo->marker->discarded_bytes = 0;\r
-}\r
-\r
-\r
-/*\r
- * Initialize the marker reader module.\r
- * This is called only once, when the decompression object is created.\r
- */\r
-\r
-GLOBAL void\r
-jinit_marker_reader (j_decompress_ptr cinfo)\r
-{\r
- int i;\r
-\r
- /* Create subobject in permanent pool */\r
- cinfo->marker = (struct jpeg_marker_reader *)\r
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,\r
- SIZEOF(struct jpeg_marker_reader));\r
- /* Initialize method pointers */\r
- cinfo->marker->reset_marker_reader = reset_marker_reader;\r
- cinfo->marker->read_markers = read_markers;\r
- cinfo->marker->read_restart_marker = read_restart_marker;\r
- cinfo->marker->process_COM = skip_variable;\r
- for (i = 0; i < 16; i++)\r
- cinfo->marker->process_APPn[i] = skip_variable;\r
- cinfo->marker->process_APPn[0] = get_app0;\r
- cinfo->marker->process_APPn[14] = get_app14;\r
- /* Reset marker processing state */\r
- reset_marker_reader(cinfo);\r
-}\r