1 /*
2  * Small jpeg decoder library (Internal header)
3  *
4  * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
5  * All rights reserved.
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  *  this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  *  this list of conditions and the following disclaimer in the documentation
14  *  and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the author nor the names of its contributors may be
17  *  used to endorse or promote products derived from this software without
18  *  specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 
35 #ifndef __TINYJPEG_INTERNAL_H_
36 #define __TINYJPEG_INTERNAL_H_
37 
38 #include <setjmp.h>
39 
40 #define SANITY_CHECK 1
41 
42 struct jdec_private;
43 
44 #define HUFFMAN_HASH_NBITS 9
45 #define HUFFMAN_HASH_SIZE  (1UL<<HUFFMAN_HASH_NBITS)
46 #define HUFFMAN_HASH_MASK  (HUFFMAN_HASH_SIZE-1)
47 
48 #define HUFFMAN_TABLES	   4
49 #define COMPONENTS	   3
50 #define JPEG_MAX_WIDTH	   2048
51 #define JPEG_MAX_HEIGHT	   2048
52 
53 struct huffman_table {
54 	/* Fast look up table, using HUFFMAN_HASH_NBITS bits we can have directly the symbol,
55 	 * if the symbol is <0, then we need to look into the tree table */
56 	short int lookup[HUFFMAN_HASH_SIZE];
57 	/* code size: give the number of bits of a symbol is encoded */
58 	unsigned char code_size[HUFFMAN_HASH_SIZE];
59 	/* some place to store value that is not encoded in the lookup table
60 	 * IMPROVEME: Calculate if 256 value is enough to store all values
61 	 */
62 	uint16_t slowtable[16 - HUFFMAN_HASH_NBITS][256];
63 };
64 
65 struct component {
66 	unsigned int Hfactor;
67 	unsigned int Vfactor;
68 	float *Q_table;		/* Pointer to the quantisation table to use */
69 	struct huffman_table *AC_table;
70 	struct huffman_table *DC_table;
71 	short int previous_DC;	/* Previous DC coefficient */
72 	short int DCT[64];		/* DCT coef */
73 #if SANITY_CHECK
74 	unsigned int cid;
75 #endif
76 };
77 
78 
79 typedef void (*decode_MCU_fct) (struct jdec_private *priv);
80 typedef void (*convert_colorspace_fct) (struct jdec_private *priv);
81 
82 struct jdec_private {
83 	/* Public variables */
84 	uint8_t *components[COMPONENTS];
85 	unsigned int width, height;	/* Size of the image */
86 	unsigned int flags;
87 
88 	/* Private variables */
89 	const unsigned char *stream_end;
90 	const unsigned char *stream;	/* Pointer to the current stream */
91 	unsigned char *stream_filtered;
92 	int stream_filtered_bufsize;
93 	unsigned int reservoir, nbits_in_reservoir;
94 
95 	struct component component_infos[COMPONENTS];
96 	float Q_tables[COMPONENTS][64];		/* quantization tables */
97 	struct huffman_table HTDC[HUFFMAN_TABLES];	/* DC huffman tables   */
98 	struct huffman_table HTAC[HUFFMAN_TABLES];	/* AC huffman tables   */
99 	int default_huffman_table_initialized;
100 	int restart_interval;
101 	int restarts_to_go;				/* MCUs left in this restart interval */
102 	int last_rst_marker_seen;			/* Rst marker is incremented each time */
103 #if SANITY_CHECK
104 	unsigned int current_cid;			/* For planar JPEG */
105 #endif
106 	unsigned char marker;			/* for PJPG (Pixart JPEG) */
107 	unsigned char first_marker;		/* for PJPG (Pixart JPEG) */
108 
109 	/* Temp space used after the IDCT to store each components */
110 	uint8_t Y[64 * 4], Cr[64], Cb[64];
111 
112 	jmp_buf jump_state;
113 	/* Internal Pointer use for colorspace conversion, do not modify it !!! */
114 	uint8_t *plane[COMPONENTS];
115 
116 	char error_string[256];
117 
118 	/* Temp buffers for multipass planar JPG -> RGB decoding */
119 	int tmp_buf_y_size;
120 	uint8_t *tmp_buf[COMPONENTS];
121 };
122 
123 #define IDCT tinyjpeg_idct_float
124 void tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride);
125 
126 #endif
127 
128