nengel@0: #include nengel@0: #include nengel@0: #include nengel@0: #include nengel@0: #include nengel@0: nengel@0: #include "tinyjpeg.h" nengel@0: #include "tinyjpeg-internal.h" nengel@0: nengel@0: extern char error_string[256]; nengel@0: nengel@0: static const unsigned char zigzag[64] = nengel@0: { nengel@0: 0, 1, 5, 6, 14, 15, 27, 28, nengel@0: 2, 4, 7, 13, 16, 26, 29, 42, nengel@0: 3, 8, 12, 17, 25, 30, 41, 43, nengel@0: 9, 11, 18, 24, 31, 40, 44, 53, nengel@0: 10, 19, 23, 32, 39, 45, 52, 54, nengel@0: 20, 22, 33, 38, 46, 51, 55, 60, nengel@0: 21, 34, 37, 47, 50, 56, 59, 61, nengel@0: 35, 36, 48, 49, 57, 58, 62, 63 nengel@0: }; nengel@0: nengel@0: /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ nengel@0: /* IMPORTANT: these are only valid for 8-bit data precision! */ nengel@0: static const unsigned char bits_dc_luminance[17] = nengel@0: { nengel@0: 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 nengel@0: }; nengel@0: static const unsigned char val_dc_luminance[] = nengel@0: { nengel@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 nengel@0: }; nengel@0: nengel@0: static const unsigned char bits_dc_chrominance[17] = nengel@0: { nengel@0: 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 nengel@0: }; nengel@0: static const unsigned char val_dc_chrominance[] = nengel@0: { nengel@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 nengel@0: }; nengel@0: nengel@0: static const unsigned char bits_ac_luminance[17] = nengel@0: { nengel@0: 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d nengel@0: }; nengel@0: static const unsigned char val_ac_luminance[] = nengel@0: { nengel@0: 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, nengel@0: 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, nengel@0: 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, nengel@0: 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, nengel@0: 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, nengel@0: 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, nengel@0: 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, nengel@0: 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, nengel@0: 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, nengel@0: 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, nengel@0: 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, nengel@0: 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, nengel@0: 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, nengel@0: 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, nengel@0: 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, nengel@0: 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, nengel@0: 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, nengel@0: 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, nengel@0: 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, nengel@0: 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, nengel@0: 0xf9, 0xfa nengel@0: }; nengel@0: nengel@0: static const unsigned char bits_ac_chrominance[17] = nengel@0: { nengel@0: 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 nengel@0: }; nengel@0: nengel@0: static const unsigned char val_ac_chrominance[] = nengel@0: { nengel@0: 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, nengel@0: 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, nengel@0: 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, nengel@0: 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, nengel@0: 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, nengel@0: 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, nengel@0: 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, nengel@0: 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, nengel@0: 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, nengel@0: 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, nengel@0: 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, nengel@0: 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, nengel@0: 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, nengel@0: 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, nengel@0: 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, nengel@0: 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, nengel@0: 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, nengel@0: 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, nengel@0: 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, nengel@0: 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, nengel@0: 0xf9, 0xfa nengel@0: }; nengel@0: nengel@0: static void print_SOF(const unsigned char *stream) nengel@0: { nengel@0: int width, height, nr_components, precision; nengel@0: nengel@0: precision = stream[2]; nengel@0: height = be16_to_cpu(stream+3); nengel@0: width = be16_to_cpu(stream+5); nengel@0: nr_components = stream[7]; nengel@0: nengel@0: trace("> SOF marker\n"); nengel@0: trace("Size:%dx%d nr_components:%d precision:%d\n", nengel@0: width, height, nengel@0: nr_components, nengel@0: precision); nengel@0: } nengel@0: nengel@0: nengel@0: /* nengel@0: * Takes two array of bits, and build the huffman table for size, and code nengel@0: * nengel@0: * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS. nengel@0: * code_size will be used to known how many bits this symbol is encoded. nengel@0: * slowtable will be used when the first lookup didn't give the result. nengel@0: */ nengel@0: static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table) nengel@0: { nengel@0: unsigned int i, j, code, code_size, val, nbits; nengel@0: unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz; nengel@0: unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc; nengel@0: int next_free_entry; nengel@0: nengel@0: /* nengel@0: * Build a temp array nengel@0: * huffsize[X] => numbers of bits to write vals[X] nengel@0: */ nengel@0: hz = huffsize; nengel@0: for (i=1; i<=16; i++) nengel@0: { nengel@0: for (j=1; j<=bits[i]; j++) nengel@0: *hz++ = i; nengel@0: } nengel@0: *hz = 0; nengel@0: nengel@0: memset(table->lookup, 0xff, sizeof(table->lookup)); nengel@0: for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++) nengel@0: table->slowtable[i][0] = 0; nengel@0: nengel@0: /* Build a temp array nengel@0: * huffcode[X] => code used to write vals[X] nengel@0: */ nengel@0: code = 0; nengel@0: hc = huffcode; nengel@0: hz = huffsize; nengel@0: nbits = *hz; nengel@0: while (*hz) nengel@0: { nengel@0: while (*hz == nbits) nengel@0: { nengel@0: *hc++ = code++; nengel@0: hz++; nengel@0: } nengel@0: code <<= 1; nengel@0: nbits++; nengel@0: } nengel@0: nengel@0: /* nengel@0: * Build the lookup table, and the slowtable if needed. nengel@0: */ nengel@0: next_free_entry = -1; nengel@0: for (i=0; huffsize[i]; i++) nengel@0: { nengel@0: val = vals[i]; nengel@0: code = huffcode[i]; nengel@0: code_size = huffsize[i]; nengel@0: nengel@0: trace("val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size); nengel@0: nengel@0: table->code_size[val] = code_size; nengel@0: if (code_size <= HUFFMAN_HASH_NBITS) nengel@0: { nengel@0: /* nengel@0: * Good: val can be put in the lookup table, so fill all value of this nengel@0: * column with value val nengel@0: */ nengel@0: int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size); nengel@0: code <<= HUFFMAN_HASH_NBITS - code_size; nengel@0: while ( repeat-- ) nengel@0: table->lookup[code++] = val; nengel@0: nengel@0: } nengel@0: else nengel@0: { nengel@0: /* Perhaps sorting the array will be an optimization */ nengel@0: uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1]; nengel@0: while(slowtable[0]) nengel@0: slowtable+=2; nengel@0: slowtable[0] = code; nengel@0: slowtable[1] = val; nengel@0: slowtable[2] = 0; nengel@0: } nengel@0: nengel@0: } nengel@0: } nengel@0: nengel@0: static void build_default_huffman_tables(struct jdec_private *priv) nengel@0: { nengel@0: if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE) nengel@0: && priv->default_huffman_table_initialized) nengel@0: return; nengel@0: nengel@0: build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]); nengel@0: build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]); nengel@0: nengel@0: build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]); nengel@0: build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]); nengel@0: nengel@0: priv->default_huffman_table_initialized = 1; nengel@0: } nengel@0: nengel@0: /******************************************************************************* nengel@0: * nengel@0: * JPEG/JFIF Parsing functions nengel@0: * nengel@0: * Note: only a small subset of the jpeg file format is supported. No markers, nengel@0: * nor progressive stream is supported. nengel@0: * nengel@0: ******************************************************************************/ nengel@0: nengel@0: static void build_quantization_table(float *qtable, const unsigned char *ref_table) nengel@0: { nengel@0: /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct. nengel@0: * For float AA&N IDCT method, divisors are equal to quantization nengel@0: * coefficients scaled by scalefactor[row]*scalefactor[col], where nengel@0: * scalefactor[0] = 1 nengel@0: * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7 nengel@0: * We apply a further scale factor of 8. nengel@0: * What's actually stored is 1/divisor so that the inner loop can nengel@0: * use a multiplication rather than a division. nengel@0: */ nengel@0: int i, j; nengel@0: static const double aanscalefactor[8] = { nengel@0: 1.0, 1.387039845, 1.306562965, 1.175875602, nengel@0: 1.0, 0.785694958, 0.541196100, 0.275899379 nengel@0: }; nengel@0: const unsigned char *zz = zigzag; nengel@0: nengel@0: for (i=0; i<8; i++) { nengel@0: for (j=0; j<8; j++) { nengel@0: *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j]; nengel@0: } nengel@0: } nengel@0: nengel@0: } nengel@0: nengel@0: static int parse_DQT(struct jdec_private *priv, const unsigned char *stream) nengel@0: { nengel@0: int qi; nengel@0: float *table; nengel@0: const unsigned char *dqt_block_end; nengel@0: nengel@0: trace("> DQT marker\n"); nengel@0: dqt_block_end = stream + be16_to_cpu(stream); nengel@0: stream += 2; /* Skip length */ nengel@0: nengel@0: while (stream < dqt_block_end) nengel@0: { nengel@0: qi = *stream++; nengel@0: #if SANITY_CHECK nengel@0: if (qi>>4) nengel@0: error("16 bits quantization table is not supported\n"); nengel@0: if (qi>4) nengel@0: error("No more 4 quantization table is supported (got %d)\n", qi); nengel@0: #endif nengel@0: table = priv->Q_tables[qi]; nengel@0: build_quantization_table(table, stream); nengel@0: stream += 64; nengel@0: } nengel@0: trace("< DQT marker\n"); nengel@0: return 0; nengel@0: } nengel@0: nengel@0: static int parse_SOF(struct jdec_private *priv, const unsigned char *stream) nengel@0: { nengel@0: int i, width, height, nr_components, cid, sampling_factor; nengel@0: int Q_table; nengel@0: struct component *c; nengel@0: nengel@0: trace("> SOF marker\n"); nengel@0: print_SOF(stream); nengel@0: nengel@0: height = be16_to_cpu(stream+3); nengel@0: width = be16_to_cpu(stream+5); nengel@0: nr_components = stream[7]; nengel@0: #if SANITY_CHECK nengel@0: if (stream[2] != 8) nengel@0: error("Precision other than 8 is not supported\n"); nengel@0: if (nr_components != 3) nengel@0: error("We only support YUV images\n"); nengel@0: if (height%16) nengel@0: error("Height need to be a multiple of 16 (current height is %d)\n", height); nengel@0: if (width%16) nengel@0: error("Width need to be a multiple of 16 (current Width is %d)\n", width); nengel@0: #endif nengel@0: stream += 8; nengel@0: for (i=0; icomponent_infos[i]; nengel@0: #if SANITY_CHECK nengel@0: c->cid = cid; nengel@0: if (Q_table >= COMPONENTS) nengel@0: error("Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1); nengel@0: #endif nengel@0: c->Vfactor = sampling_factor&0xf; nengel@0: c->Hfactor = sampling_factor>>4; nengel@0: c->Q_table = priv->Q_tables[Q_table]; nengel@0: trace("Component:%d factor:%dx%d Quantization table:%d\n", cid, c->Hfactor, c->Hfactor, Q_table ); nengel@0: nengel@0: } nengel@0: priv->width = width; nengel@0: priv->height = height; nengel@0: priv->mcus_in_width = width/16; nengel@0: priv->mcus_in_height = height/16; nengel@0: priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height; nengel@0: nengel@0: trace("< SOF marker\n"); nengel@0: nengel@0: return 0; nengel@0: } nengel@0: nengel@0: static int parse_SOS(struct jdec_private *priv, const unsigned char *stream) nengel@0: { nengel@0: unsigned int i, cid, table; nengel@0: unsigned int nr_components = stream[2]; nengel@0: nengel@0: trace("> SOS marker\n"); nengel@0: nengel@0: #if SANITY_CHECK nengel@0: if (nr_components != 3) nengel@0: error("We only support YCbCr image\n"); nengel@0: #endif nengel@0: nengel@0: stream += 3; nengel@0: for (i=0;i=4) nengel@0: error("We do not support more than 2 AC Huffman table\n"); nengel@0: if ((table>>4)>=4) nengel@0: error("We do not support more than 2 DC Huffman table\n"); nengel@0: if (cid != priv->component_infos[i].cid) nengel@0: error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n", nengel@0: i, cid, i, priv->component_infos[i].cid); nengel@0: trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4); nengel@0: #endif nengel@0: priv->component_infos[i].AC_table = &priv->HTAC[table&0xf]; nengel@0: priv->component_infos[i].DC_table = &priv->HTDC[table>>4]; nengel@0: } nengel@0: priv->stream = stream+3; nengel@0: trace("< SOS marker\n"); nengel@0: return 0; nengel@0: } nengel@0: nengel@0: static int parse_DHT(struct jdec_private *priv, const unsigned char *stream) nengel@0: { nengel@0: unsigned int count, i; nengel@0: unsigned char huff_bits[17]; nengel@0: int length, index; nengel@0: nengel@0: length = be16_to_cpu(stream) - 2; nengel@0: stream += 2; /* Skip length */ nengel@0: nengel@0: trace("> DHT marker (length=%d)\n", length); nengel@0: nengel@0: while (length>0) { nengel@0: index = *stream++; nengel@0: nengel@0: /* We need to calculate the number of bytes 'vals' will takes */ nengel@0: huff_bits[0] = 0; nengel@0: count = 0; nengel@0: for (i=1; i<17; i++) { nengel@0: huff_bits[i] = *stream++; nengel@0: count += huff_bits[i]; nengel@0: } nengel@0: #if SANITY_CHECK nengel@0: if (count >= HUFFMAN_BITS_SIZE) nengel@0: error("No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE); nengel@0: if ( (index &0xf) >= HUFFMAN_TABLES) nengel@0: error("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf); nengel@0: trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count); nengel@0: #endif nengel@0: nengel@0: if (index & 0xf0 ) nengel@0: build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]); nengel@0: else nengel@0: build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]); nengel@0: nengel@0: length -= 1; nengel@0: length -= 16; nengel@0: length -= count; nengel@0: stream += count; nengel@0: } nengel@0: trace("< DHT marker\n"); nengel@0: return 0; nengel@0: } nengel@0: nengel@0: static int parse_DRI(struct jdec_private *priv, const unsigned char *stream) nengel@0: { nengel@0: unsigned int length; nengel@0: nengel@0: trace("> DRI marker\n"); nengel@0: nengel@0: length = be16_to_cpu(stream); nengel@0: nengel@0: #if SANITY_CHECK nengel@0: if (length != 4) nengel@0: error("Length of DRI marker need to be 4\n"); nengel@0: #endif nengel@0: nengel@0: priv->restart_interval = be16_to_cpu(stream+2); nengel@0: if (priv->restart_interval == 0) nengel@0: priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height; nengel@0: nengel@0: #if DEBUG nengel@0: trace("Restart interval = %d\n", priv->restart_interval); nengel@0: #endif nengel@0: nengel@0: trace("< DRI marker\n"); nengel@0: nengel@0: return 0; nengel@0: } nengel@0: nengel@0: int parse_JFIF(struct jdec_private *priv, const unsigned char *stream) nengel@0: { nengel@0: int chuck_len; nengel@0: int marker; nengel@0: int sos_marker_found = 0; nengel@0: int dht_marker_found = 0; nengel@0: const unsigned char *next_chunck; nengel@0: nengel@0: /* Parse marker */ nengel@0: while (!sos_marker_found) nengel@0: { nengel@0: if (*stream++ != 0xff) { nengel@0: trace("Bogus jpeg format\n"); nengel@0: return -1; nengel@0: } nengel@0: /* Skip any padding ff byte (this is normal) */ nengel@0: while (*stream == 0xff) nengel@0: stream++; nengel@0: nengel@0: marker = *stream++; nengel@0: chuck_len = be16_to_cpu(stream); nengel@0: next_chunck = stream + chuck_len; nengel@0: switch (marker) nengel@0: { nengel@0: case SOF: nengel@0: if (parse_SOF(priv, stream) < 0) nengel@0: return -1; nengel@0: break; nengel@0: case DQT: nengel@0: if (parse_DQT(priv, stream) < 0) nengel@0: return -1; nengel@0: break; nengel@0: case SOS: nengel@0: if (parse_SOS(priv, stream) < 0) nengel@0: return -1; nengel@0: sos_marker_found = 1; nengel@0: break; nengel@0: case DHT: nengel@0: if (parse_DHT(priv, stream) < 0) nengel@0: return -1; nengel@0: dht_marker_found = 1; nengel@0: break; nengel@0: case DRI: nengel@0: if (parse_DRI(priv, stream) < 0) nengel@0: return -1; nengel@0: break; nengel@0: default: nengel@0: trace("> Unknown marker %2.2x\n", marker); nengel@0: break; nengel@0: } nengel@0: nengel@0: stream = next_chunck; nengel@0: } nengel@0: nengel@0: if (!dht_marker_found) { nengel@0: trace("No Huffman table loaded, using the default one\n"); nengel@0: build_default_huffman_tables(priv); nengel@0: } nengel@0: nengel@0: #ifdef SANITY_CHECK nengel@0: if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor) nengel@0: || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor)) nengel@0: error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n"); nengel@0: if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor) nengel@0: || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor)) nengel@0: error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n"); nengel@0: if ( (priv->component_infos[cCb].Hfactor!=1) nengel@0: || (priv->component_infos[cCr].Hfactor!=1) nengel@0: || (priv->component_infos[cCb].Vfactor!=1) nengel@0: || (priv->component_infos[cCr].Vfactor!=1)) nengel@0: error("Sampling other than 1x1 for Cr and Cb is not supported"); nengel@0: #endif nengel@0: nengel@0: return 0; nengel@0: }