📄 tinyjpeg.c
字号:
/* * Small jpeg decoder library * * Copyright (c) 2006, Luc Saillard <luc@saillard.org> * All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * - Neither the name of the author nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdint.h>#include <errno.h>#include "tinyjpeg.h"#include "tinyjpeg-internal.h"enum std_markers { DQT = 0xDB, /* Define Quantization Table */ SOF = 0xC0, /* Start of Frame (size information) */ DHT = 0xC4, /* Huffman Table */ SOI = 0xD8, /* Start of Image */ SOS = 0xDA, /* Start of Scan */ RST = 0xD0, /* Reset Marker d0 -> .. */ RST7 = 0xD7, /* Reset Marker .. -> d7 */ EOI = 0xD9, /* End of Image */ DRI = 0xDD, /* Define Restart Interval */ APP0 = 0xE0,};#define cY 0#define cCb 1#define cCr 2#define BLACK_Y 0#define BLACK_U 127#define BLACK_V 127#if DEBUG#if LOG2FILE#define trace(fmt, args...) do { \ FILE *f = fopen("/tmp/jpeg.log", "a"); \ fprintf(f, fmt, ## args); \ fflush(f); \ fclose(f); \} while(0)#else#define trace(fmt, args...) do { \ fprintf(stderr, fmt, ## args); \ fflush(stderr); \} while(0)#endif#else#define trace(fmt, args...) do { } while (0)#endif#define error(fmt, args...) do { \ snprintf(priv->error_string, sizeof(priv->error_string), fmt, ## args); \ return -1; \} while(0)#if 0static char *print_bits(unsigned int value, char *bitstr){ int i, j; i=31; while (i>0) { if (value & (1UL<<i)) break; i--; } j=0; while (i>=0) { bitstr[j++] = (value & (1UL<<i))?'1':'0'; i--; } bitstr[j] = 0; return bitstr;}static void print_next_16bytes(int offset, const unsigned char *stream){ trace("%4.4x: %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x %2.2x\n", offset, stream[0], stream[1], stream[2], stream[3], stream[4], stream[5], stream[6], stream[7], stream[8], stream[9], stream[10], stream[11], stream[12], stream[13], stream[14], stream[15]);}#endifstatic const unsigned char zigzag[64] ={ 0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63};/* Set up the standard Huffman tables (cf. JPEG standard section K.3) *//* IMPORTANT: these are only valid for 8-bit data precision! */static const unsigned char bits_dc_luminance[17] ={ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};static const unsigned char val_dc_luminance[] ={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};static const unsigned char bits_dc_chrominance[17] ={ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0};static const unsigned char val_dc_chrominance[] ={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};static const unsigned char bits_ac_luminance[17] ={ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d};static const unsigned char val_ac_luminance[] ={ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};static const unsigned char bits_ac_chrominance[17] ={ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77};static const unsigned char val_ac_chrominance[] ={ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa};const unsigned char pixart_quantization[][64] = { { 0x07, 0x07, 0x08, 0x0a, 0x09, 0x07, 0x0d, 0x0b, 0x0c, 0x0d, 0x11, 0x10, 0x0f, 0x12, 0x17, 0x27, 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25, 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33, 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57, 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71, 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63, }, { 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a, 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, },};/* * 4 functions to manage the stream * * fill_nbits: put at least nbits in the reservoir of bits. * But convert any 0xff,0x00 into 0xff * get_nbits: read nbits from the stream, and put it in result, * bits is removed from the stream and the reservoir is filled * automaticaly. The result is signed according to the number of * bits. * look_nbits: read nbits from the stream without marking as read. * skip_nbits: read nbits from the stream but do not return the result. * * stream: current pointer in the jpeg data (read bytes per bytes) * nbits_in_reservoir: number of bits filled into the reservoir * reservoir: register that contains bits information. Only nbits_in_reservoir * is valid. * nbits_in_reservoir * <-- 17 bits --> * Ex: 0000 0000 1010 0000 1111 0000 <== reservoir * ^ * bit 1 * To get two bits from this example * result = (reservoir >> 15) & 3 * */#define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ while (nbits_in_reservoir<nbits_wanted) \ { \ unsigned char c; \ if (stream >= priv->stream_end) { \ snprintf(priv->error_string, sizeof(priv->error_string), \ "fill_nbits error: need %u more bits\n", \ nbits_wanted - nbits_in_reservoir); \ longjmp(priv->jump_state, -EIO); \ } \ c = *stream++; \ reservoir <<= 8; \ if (c == 0xff && *stream == 0x00) \ stream++; \ reservoir |= c; \ nbits_in_reservoir+=8; \ } \} while(0);/* Signed version !!!! */#define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \ fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \ nbits_in_reservoir -= (nbits_wanted); \ reservoir &= ((1U<<nbits_in_reservoir)-1); \ if ((unsigned int)result < (1UL<<((nbits_wanted)-1))) \ result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \} while(0);#define look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \ fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \} while(0);/* To speed up the decoding, we assume that the reservoir have enough bit * slow version: * #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ * fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ * nbits_in_reservoir -= (nbits_wanted); \ * reservoir &= ((1U<<nbits_in_reservoir)-1); \ * } while(0); */#define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \ nbits_in_reservoir -= (nbits_wanted); \ reservoir &= ((1U<<nbits_in_reservoir)-1); \} while(0);/* Special Pixart versions of the *_nbits functions, these remove the special ff ff ff xx sequences pixart cams insert from the bitstream */#define pixart_fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) \do { \ while (nbits_in_reservoir<nbits_wanted) \ { \ unsigned char c; \ if (stream >= priv->stream_end) { \ snprintf(priv->error_string, sizeof(priv->error_string), \ "fill_nbits error: need %u more bits\n", \ nbits_wanted - nbits_in_reservoir); \ longjmp(priv->jump_state, -EIO); \ } \ c = *stream++; \ reservoir <<= 8; \ if (c == 0xff) { \ switch (stream[0]) { \ case 0x00: \ stream++; \ break; \ case 0xd9: /* EOF marker */ \ stream++; \ if (stream != priv->stream_end) { \ snprintf(priv->error_string, sizeof(priv->error_string), \ "Pixart JPEG error: premature EOF\n"); \ longjmp(priv->jump_state, -EIO); \ } \ break; \ case 0xff: \ if (stream[1] == 0xff && (stream[2] < 7 || stream[2] == 0xff)) { \ stream += 3; \ c = *stream++; \ break; \ } \ /* Error fall through */ \ default: \ snprintf(priv->error_string, sizeof(priv->error_string), \ "Pixart JPEG error: invalid JPEG marker: 0xff 0x%02x 0x%02x 0x%02x\n", \ (unsigned int)stream[0], (unsigned int)stream[1], \ (unsigned int)stream[2]); \ longjmp(priv->jump_state, -EIO); \ } \ } \ reservoir |= c; \ nbits_in_reservoir+=8; \ } \} while(0);/* Signed version !!!! */#define pixart_get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) \do { \ pixart_fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \ nbits_in_reservoir -= (nbits_wanted); \ reservoir &= ((1U<<nbits_in_reservoir)-1); \ if ((unsigned int)result < (1UL<<((nbits_wanted)-1))) \ result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \} while(0);#define pixart_look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) \do { \ pixart_fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \ result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \} while(0);/* Note skip_nbits is identical for both */#define be16_to_cpu(x) (((x)[0]<<8)|(x)[1])static void resync(struct jdec_private *priv);/** * Get the next (valid) huffman code in the stream. * * To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is * lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code * and the value by using two lookup table. * Else if the value is not found, just search (linear) into an array for each * bits is the code is present. * * If the code is not present for any reason, -1 is return. */static int get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table){ int value, hcode; unsigned int extra_nbits, nbits; uint16_t *slowtable; look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode); value = huffman_table->lookup[hcode]; if (value >= 0) { unsigned int code_size = huffman_table->code_size[value]; skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size); return value; } /* Decode more bits each time ... */ for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++) { nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits; look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode); slowtable = huffman_table->slowtable[extra_nbits]; /* Search if the code is in this array */ while (slowtable[0]) { if (slowtable[0] == hcode) { skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits); return slowtable[1]; } slowtable+=2; } } snprintf(priv->error_string, sizeof(priv->error_string), "unknown huffman code: %08x\n", (unsigned int)hcode); longjmp(priv->jump_state, -EIO); return 0;}/* identical as above but with *_nbits replaced with pixart_*_nbits */static int pixart_get_next_huffman_code(struct jdec_private *priv, struct huffman_table *huffman_table){ int value, hcode; unsigned int extra_nbits, nbits; uint16_t *slowtable; pixart_look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, HUFFMAN_HASH_NBITS, hcode); value = huffman_table->lookup[hcode]; if (value >= 0) { unsigned int code_size = huffman_table->code_size[value]; skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, code_size); return value; } /* Decode more bits each time ... */ for (extra_nbits=0; extra_nbits<16-HUFFMAN_HASH_NBITS; extra_nbits++) { nbits = HUFFMAN_HASH_NBITS + 1 + extra_nbits; pixart_look_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits, hcode); slowtable = huffman_table->slowtable[extra_nbits]; /* Search if the code is in this array */ while (slowtable[0]) { if (slowtable[0] == hcode) { skip_nbits(priv->reservoir, priv->nbits_in_reservoir, priv->stream, nbits); return slowtable[1]; } slowtable+=2; } } snprintf(priv->error_string, sizeof(priv->error_string), "unknown huffman code: %08x\n", (unsigned int)hcode); longjmp(priv->jump_state, -EIO); return 0;}/** * * Decode a single block that contains the DCT coefficients. * The table coefficients is already dezigzaged at the end of the operation. * */static void process_Huffman_data_unit(struct jdec_private *priv, int component){ unsigned char j; unsigned int huff_code; unsigned char size_val, count_0; struct component *c = &priv->component_infos[component]; short int DCT[64]; /* Initialize the DCT coef table */ memset(DCT, 0, sizeof(DCT)); /* DC coefficient decoding */ huff_code = get_next_huffman_code(priv, c->DC_table);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -