2 * Small jpeg decoder library
4 * Copyright (c) 2006, Luc Saillard <luc@saillard.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
40 #include "tinyjpeg-internal.h"
42 /* Global variable to return the last error found while deconding */
43 static char error_string
[256];
45 static const unsigned char zigzag
[64] =
47 0, 1, 5, 6, 14, 15, 27, 28,
48 2, 4, 7, 13, 16, 26, 29, 42,
49 3, 8, 12, 17, 25, 30, 41, 43,
50 9, 11, 18, 24, 31, 40, 44, 53,
51 10, 19, 23, 32, 39, 45, 52, 54,
52 20, 22, 33, 38, 46, 51, 55, 60,
53 21, 34, 37, 47, 50, 56, 59, 61,
54 35, 36, 48, 49, 57, 58, 62, 63
57 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
58 /* IMPORTANT: these are only valid for 8-bit data precision! */
59 static const unsigned char bits_dc_luminance
[17] =
61 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
63 static const unsigned char val_dc_luminance
[] =
65 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
68 static const unsigned char bits_dc_chrominance
[17] =
70 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
72 static const unsigned char val_dc_chrominance
[] =
74 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
77 static const unsigned char bits_ac_luminance
[17] =
79 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
81 static const unsigned char val_ac_luminance
[] =
83 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
84 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
85 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
86 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
87 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
88 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
89 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
90 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
91 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
92 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
93 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
94 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
95 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
96 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
97 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
98 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
99 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
100 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
101 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
102 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
106 static const unsigned char bits_ac_chrominance
[17] =
108 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
111 static const unsigned char val_ac_chrominance
[] =
113 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
114 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
115 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
116 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
117 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
118 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
119 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
120 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
121 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
122 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
123 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
124 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
125 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
126 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
127 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
128 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
129 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
130 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
131 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
132 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
138 * 4 functions to manage the stream
140 * fill_nbits: put at least nbits in the reservoir of bits.
141 * But convert any 0xff,0x00 into 0xff
142 * get_nbits: read nbits from the stream, and put it in result,
143 * bits is removed from the stream and the reservoir is filled
144 * automaticaly. The result is signed according to the number of
146 * look_nbits: read nbits from the stream without marking as read.
147 * skip_nbits: read nbits from the stream but do not return the result.
149 * stream: current pointer in the jpeg data (read bytes per bytes)
150 * nbits_in_reservoir: number of bits filled into the reservoir
151 * reservoir: register that contains bits information. Only nbits_in_reservoir
155 * Ex: 0000 0000 1010 0000 1111 0000 <== reservoir
158 * To get two bits from this example
159 * result = (reservoir >> 15) & 3
162 #define fill_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
163 while (nbits_in_reservoir<nbits_wanted) \
165 const unsigned char c = *stream++; \
167 if (c == 0xff && *stream == 0x00) \
170 nbits_in_reservoir+=8; \
174 /* Signed version !!!! */
175 #define get_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
176 fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
177 result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
178 nbits_in_reservoir -= (nbits_wanted); \
179 reservoir &= ((1U<<nbits_in_reservoir)-1); \
180 if (result < (1UL<<((nbits_wanted)-1))) \
181 result += (0xFFFFFFFFUL<<(nbits_wanted))+1; \
184 #define look_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted,result) do { \
185 fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
186 result = ((reservoir)>>(nbits_in_reservoir-(nbits_wanted))); \
189 #define skip_nbits(reservoir,nbits_in_reservoir,stream,nbits_wanted) do { \
190 fill_nbits(reservoir,nbits_in_reservoir,stream,(nbits_wanted)); \
191 nbits_in_reservoir -= (nbits_wanted); \
192 reservoir &= ((1U<<nbits_in_reservoir)-1); \
196 #define be16_to_cpu(x) (((x)[0]<<8)|(x)[1])
200 * Get the next (valid) huffman code in the stream.
202 * To speedup the procedure, we look HUFFMAN_HASH_NBITS bits and the code is
203 * lower than HUFFMAN_HASH_NBITS we have automaticaly the length of the code
204 * and the value by using two lookup table.
205 * Else if the value is not found, just search (linear) into an array for each
206 * bits is the code is present.
208 * If the code is not present for any reason, -1 is return.
210 static int get_next_huffman_code(struct jdec_private
*priv
, struct huffman_table
*huffman_table
)
213 unsigned int extra_nbits
, nbits
;
216 look_nbits(priv
->reservoir
, priv
->nbits_in_reservoir
, priv
->stream
, HUFFMAN_HASH_NBITS
, hcode
);
217 value
= huffman_table
->lookup
[hcode
];
220 int code_size
= huffman_table
->code_size
[value
];
221 skip_nbits(priv
->reservoir
, priv
->nbits_in_reservoir
, priv
->stream
, code_size
);
225 /* Decode more bits each time ... */
226 for (extra_nbits
=0; extra_nbits
<16-HUFFMAN_HASH_NBITS
; extra_nbits
++)
228 nbits
= HUFFMAN_HASH_NBITS
+ 1 + extra_nbits
;
230 look_nbits(priv
->reservoir
, priv
->nbits_in_reservoir
, priv
->stream
, nbits
, hcode
);
231 slowtable
= huffman_table
->slowtable
[extra_nbits
];
232 /* Search if the code is in this array */
233 while (slowtable
[0]) {
234 if (slowtable
[0] == hcode
) {
235 skip_nbits(priv
->reservoir
, priv
->nbits_in_reservoir
, priv
->stream
, nbits
);
249 * Decode a single block that contains the DCT coefficients.
250 * The table coefficients is already dezigzaged at the end of the operation.
253 void tinyjpeg_process_Huffman_data_unit(struct jdec_private
*priv
, int component
)
257 unsigned char size_val
, count_0
;
259 struct component
*c
= &priv
->component_infos
[component
];
262 /* Initialize the DCT coef table */
263 memset(DCT
, 0, sizeof(DCT
));
265 /* DC coefficient decoding */
266 huff_code
= get_next_huffman_code(priv
, c
->DC_table
);
268 get_nbits(priv
->reservoir
, priv
->nbits_in_reservoir
, priv
->stream
, huff_code
, DCT
[0]);
269 DCT
[0] += c
->previous_DC
;
270 c
->previous_DC
= DCT
[0];
272 DCT
[0] = c
->previous_DC
;
275 /* AC coefficient decoding */
279 huff_code
= get_next_huffman_code(priv
, c
->AC_table
);
281 size_val
= huff_code
& 0xF;
282 count_0
= huff_code
>> 4;
287 break; /* EOB found, go out */
288 else if (count_0
== 0xF)
289 j
+= 16; /* skip 16 zeros */
293 j
+= count_0
; /* skip count_0 zeroes */
294 get_nbits(priv
->reservoir
, priv
->nbits_in_reservoir
, priv
->stream
, size_val
, DCT
[j
]);
299 for (j
= 0; j
< 64; j
++)
300 c
->DCT
[j
] = DCT
[zigzag
[j
]];
305 * Takes two array of bits, and build the huffman table for size, and code
307 * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
308 * code_size will be used to known how many bits this symbol is encoded.
309 * slowtable will be used when the first lookup didn't give the result.
311 static void build_huffman_table(const unsigned char *bits
, const unsigned char *vals
, struct huffman_table
*table
)
313 unsigned int i
, j
, code
, code_size
, val
, nbits
;
314 unsigned char huffsize
[257], *hz
;
315 unsigned int huffcode
[257], *hc
;
320 * huffsize[X] => numbers of bits to write vals[X]
323 for (i
=1; i
<=16; i
++)
325 for (j
=1; j
<=bits
[i
]; j
++)
330 memset(table
->lookup
, 0xff, sizeof(table
->lookup
));
331 for (i
=0; i
<(16-HUFFMAN_HASH_NBITS
); i
++)
332 table
->slowtable
[i
][0] = 0;
334 /* Build a temp array
335 * huffcode[X] => code used to write vals[X]
343 while (*hz
== nbits
) {
352 * Build the lookup table, and the slowtable if needed.
354 next_free_entry
= -1;
355 for (i
=0; huffsize
[i
]; i
++)
359 code_size
= huffsize
[i
];
361 trace("val=%2.2x code=%8.8x codesize=%2.2d\n", i
, code
, code_size
);
363 table
->code_size
[val
] = code_size
;
364 if (code_size
<= HUFFMAN_HASH_NBITS
)
367 * Good: val can be put in the lookup table, so fill all value of this
368 * column with value val
370 int repeat
= 1UL<<(HUFFMAN_HASH_NBITS
- code_size
);
371 code
<<= HUFFMAN_HASH_NBITS
- code_size
;
373 table
->lookup
[code
++] = val
;
378 /* Perhaps sorting the array will be an optimization */
379 uint16_t *slowtable
= table
->slowtable
[code_size
-HUFFMAN_HASH_NBITS
-1];
385 /* TODO: NEED TO CHECK FOR AN OVERFLOW OF THE TABLE */
392 static void build_default_huffman_tables(struct jdec_private
*priv
)
394 if ( (priv
->flags
& TINYJPEG_FLAGS_MJPEG_TABLE
)
395 && priv
->default_huffman_table_initialized
)
398 build_huffman_table(bits_dc_luminance
, val_dc_luminance
, &priv
->HTDC
[0]);
399 build_huffman_table(bits_ac_luminance
, val_ac_luminance
, &priv
->HTAC
[0]);
401 build_huffman_table(bits_dc_chrominance
, val_dc_chrominance
, &priv
->HTDC
[1]);
402 build_huffman_table(bits_ac_chrominance
, val_ac_chrominance
, &priv
->HTAC
[1]);
404 priv
->default_huffman_table_initialized
= 1;
409 /*******************************************************************************
411 * Colorspace conversion routine
415 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
416 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
417 * The conversion equations to be implemented are therefore
418 * R = Y + 1.40200 * Cr
419 * G = Y - 0.34414 * Cb - 0.71414 * Cr
420 * B = Y + 1.77200 * Cb
422 ******************************************************************************/
424 static void print_SOF(const unsigned char *stream
)
426 int width
, height
, nr_components
, precision
;
428 const char *nr_components_to_string
[] = {
437 precision
= stream
[2];
438 height
= be16_to_cpu(stream
+3);
439 width
= be16_to_cpu(stream
+5);
440 nr_components
= stream
[7];
442 trace("> SOF marker\n");
443 trace("Size:%dx%d nr_components:%d (%s) precision:%d\n",
445 nr_components
, nr_components_to_string
[nr_components
],
449 /*******************************************************************************
451 * JPEG/JFIF Parsing functions
453 * Note: only a small subset of the jpeg file format is supported. No markers,
454 * nor progressive stream is supported.
456 ******************************************************************************/
458 static void build_quantization_table(float *qtable
, const unsigned char *ref_table
)
460 /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
461 * For float AA&N IDCT method, divisors are equal to quantization
462 * coefficients scaled by scalefactor[row]*scalefactor[col], where
464 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
465 * We apply a further scale factor of 8.
466 * What's actually stored is 1/divisor so that the inner loop can
467 * use a multiplication rather than a division.
470 static const double aanscalefactor
[8] = {
471 1.0, 1.387039845, 1.306562965, 1.175875602,
472 1.0, 0.785694958, 0.541196100, 0.275899379
474 const unsigned char *zz
= zigzag
;
476 for (i
=0; i
<8; i
++) {
477 for (j
=0; j
<8; j
++) {
478 *qtable
++ = ref_table
[*zz
++] * aanscalefactor
[i
] * aanscalefactor
[j
];
484 static int parse_DQT(struct jdec_private
*priv
, const unsigned char *stream
)
489 trace("> DQT marker\n");
490 length
= be16_to_cpu(stream
) - 2;
491 stream
+= 2; /* Skip length */
498 error("16 bits quantization table is not supported\n");
500 error("No more 4 quantization table is supported (got %d)\n", qi
);
502 table
= priv
->Q_tables
[qi
];
503 build_quantization_table(table
, stream
);
510 static int parse_SOF(struct jdec_private
*priv
, const unsigned char *stream
)
512 int i
, width
, height
, nr_components
, cid
, sampling_factor
;
518 height
= be16_to_cpu(stream
+3);
519 width
= be16_to_cpu(stream
+5);
520 nr_components
= stream
[7];
523 error("Precision other than 8 is not supported\n");
524 if (width
>2048 || height
>2048)
525 error("Width and Height (%dx%d) seems suspicious\n", width
, height
);
526 if (nr_components
!= 3)
527 error("We only support YUV images\n");
529 error("Height need to be a multiple of 16 (current height is %d)\n", height
);
531 error("Width need to be a multiple of 16 (current Width is %d)\n", width
);
534 for (i
=0; i
<nr_components
; i
++) {
536 sampling_factor
= *stream
++;
538 c
= &priv
->component_infos
[cid
];
539 c
->Vfactor
= sampling_factor
&0xf;
540 c
->Hfactor
= sampling_factor
>>4;
541 c
->Q_table
= priv
->Q_tables
[Q_table
];
542 trace("Component:%d factor:%dx%d Quantization table:%d\n",
543 cid
, c
->Hfactor
, c
->Hfactor
, Q_table
);
547 priv
->height
= height
;
552 static int parse_SOS(struct jdec_private
*priv
, const unsigned char *stream
)
554 unsigned int i
, cid
, table
;
555 unsigned int nr_components
= stream
[2];
557 trace("> SOS marker\n");
560 if (nr_components
!= 3)
561 error("We only support YCbCr image\n");
565 for (i
=0;i
<nr_components
;i
++) {
570 error("We do not support more than 2 AC Huffman table\n");
572 error("We do not support more than 2 DC Huffman table\n");
573 trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid
, table
&0xf, table
>>4);
575 priv
->component_infos
[cid
].AC_table
= &priv
->HTAC
[table
&0xf];
576 priv
->component_infos
[cid
].DC_table
= &priv
->HTDC
[table
>>4];
578 priv
->stream
= stream
+3;
582 static int parse_DHT(struct jdec_private
*priv
, const unsigned char *stream
)
584 unsigned int count
, i
;
585 unsigned char huff_bits
[17];
588 length
= be16_to_cpu(stream
) - 2;
589 stream
+= 2; /* Skip length */
591 trace("> DHT marker (length=%d)\n", length
);
596 /* We need to calculate the number of bytes 'vals' will takes */
599 for (i
=1; i
<17; i
++) {
600 huff_bits
[i
] = *stream
++;
601 count
+= huff_bits
[i
];
605 error("No more than 1024 bytes is allowed to describe a huffman table");
606 if ( (index
&0xf) >= HUFFMAN_TABLES
)
607 error("No mode than %d Huffman tables is supported\n", HUFFMAN_TABLES
);
608 trace("Huffman table %s n%d\n", (index
&0xf0)?"AC":"DC", index
&0xf);
609 trace("Length of the table: %d\n", count
);
613 build_huffman_table(huff_bits
, stream
, &priv
->HTAC
[index
&0xf]);
615 build_huffman_table(huff_bits
, stream
, &priv
->HTDC
[index
&0xf]);
621 trace("< DHT marker\n");
625 static void resync(struct jdec_private
*priv
)
629 /* Init DC coefficients */
630 for (i
=0; i
<COMPONENTS
; i
++)
631 priv
->component_infos
[i
].previous_DC
= 0;
634 priv
->nbits_in_reservoir
= 0;
639 static int parse_JFIF(struct jdec_private
*priv
, const unsigned char *stream
)
643 int sos_marker_found
= 0;
644 int dht_marker_found
= 0;
645 const unsigned char *next_chunck
;
648 while (!sos_marker_found
)
650 if (*stream
++ != 0xff)
651 goto bogus_jpeg_format
;
652 /* Skip any padding ff byte (this is normal) */
653 while (*stream
== 0xff)
657 chuck_len
= be16_to_cpu(stream
);
658 next_chunck
= stream
+ chuck_len
;
662 if (parse_SOF(priv
, stream
) < 0)
666 if (parse_DQT(priv
, stream
) < 0)
670 if (parse_SOS(priv
, stream
) < 0)
672 sos_marker_found
= 1;
675 if (parse_DHT(priv
, stream
) < 0)
677 dht_marker_found
= 1;
680 trace("> Unknown marker %2.2x\n", marker
);
684 stream
= next_chunck
;
687 if (!dht_marker_found
) {
688 trace("No Huffman table loaded, using the default one\n");
689 build_default_huffman_tables(priv
);
693 if ( (priv
->component_infos
[cY
].Hfactor
< priv
->component_infos
[cCb
].Hfactor
)
694 || (priv
->component_infos
[cY
].Hfactor
< priv
->component_infos
[cCr
].Hfactor
))
695 error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
696 if ( (priv
->component_infos
[cY
].Vfactor
< priv
->component_infos
[cCb
].Vfactor
)
697 || (priv
->component_infos
[cY
].Vfactor
< priv
->component_infos
[cCr
].Vfactor
))
698 error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
699 if ( (priv
->component_infos
[cCb
].Hfactor
!=1)
700 || (priv
->component_infos
[cCr
].Hfactor
!=1)
701 || (priv
->component_infos
[cCb
].Vfactor
!=1)
702 || (priv
->component_infos
[cCr
].Vfactor
!=1))
703 error("Sampling other than 1x1 for Cr and Cb is not supported");
708 trace("Bogus jpeg format\n");
712 /*******************************************************************************
714 * Functions exported of the library.
716 * Note: Some applications can access directly to internal pointer of the
717 * structure. It's is not recommended, but if you have many images to
718 * uncompress with the same parameters, some functions can be called to speedup
721 ******************************************************************************/
724 * Allocate a new tinyjpeg decoder object.
726 * Before calling any other functions, an object need to be called.
728 struct jdec_private
*tinyjpeg_init(void)
730 struct jdec_private
*priv
;
732 priv
= (struct jdec_private
*)calloc(1, sizeof(struct jdec_private
));
739 * Free a tinyjpeg object.
741 * No others function can be called after this one.
743 void tinyjpeg_free(struct jdec_private
*priv
)
746 for (i
=0; i
<COMPONENTS
; i
++) {
747 if (priv
->components
[i
])
748 free(priv
->components
[i
]);
749 priv
->components
[i
] = NULL
;
755 * Initialize the tinyjpeg object and prepare the decoding of the stream.
757 * Check if the jpeg can be decoded with this jpeg decoder.
758 * Fill some table used for preprocessing.
760 int tinyjpeg_parse_header(struct jdec_private
*priv
, const unsigned char *buf
, unsigned int size
)
764 /* Identify the file */
765 if ((buf
[0] != 0xFF) || (buf
[1] != SOI
))
766 error("Not a JPG file ?\n");
768 priv
->stream_begin
= buf
+2;
769 priv
->stream_length
= size
-2;
771 ret
= parse_JFIF(priv
, priv
->stream_begin
);
777 * Decode and convert the jpeg image into @pixfmt@ image
779 * Note: components will be automaticaly allocated if no memory is attached.
781 int tinyjpeg_decode(struct jdec_private
*priv
,
782 const struct tinyjpeg_colorspace
*pixfmt
)
784 unsigned int x
, y
, xstride_by_mcu
, ystride_by_mcu
;
785 unsigned int bytes_per_blocklines
[3], bytes_per_mcu
[3];
786 decode_MCU_fct decode_MCU
;
787 const decode_MCU_fct
*decode_mcu_table
;
788 convert_colorspace_fct convert_to_pixfmt
;
790 decode_mcu_table
= pixfmt
->decode_mcu_table
;
792 /* Fix: check return value */
793 pixfmt
->initialize(priv
, bytes_per_blocklines
, bytes_per_mcu
);
795 xstride_by_mcu
= ystride_by_mcu
= 8;
796 if ((priv
->component_infos
[cY
].Hfactor
| priv
->component_infos
[cY
].Vfactor
) == 1) {
797 decode_MCU
= decode_mcu_table
[0];
798 convert_to_pixfmt
= pixfmt
->convert_colorspace
[0];
799 trace("Use decode 1x1 sampling\n");
800 } else if (priv
->component_infos
[cY
].Hfactor
== 1) {
801 decode_MCU
= decode_mcu_table
[1];
802 convert_to_pixfmt
= pixfmt
->convert_colorspace
[1];
804 trace("Use decode 1x2 sampling (not supported)\n");
805 } else if (priv
->component_infos
[cY
].Vfactor
== 2) {
806 decode_MCU
= decode_mcu_table
[3];
807 convert_to_pixfmt
= pixfmt
->convert_colorspace
[3];
810 trace("Use decode 2x2 sampling\n");
812 decode_MCU
= decode_mcu_table
[2];
813 convert_to_pixfmt
= pixfmt
->convert_colorspace
[2];
815 trace("Use decode 2x1 sampling\n");
820 /* Don't forget to that block can be either 8 or 16 lines */
821 bytes_per_blocklines
[0] *= ystride_by_mcu
;
822 bytes_per_blocklines
[1] *= ystride_by_mcu
;
823 bytes_per_blocklines
[2] *= ystride_by_mcu
;
825 bytes_per_mcu
[0] *= xstride_by_mcu
/8;
826 bytes_per_mcu
[1] *= xstride_by_mcu
/8;
827 bytes_per_mcu
[2] *= xstride_by_mcu
/8;
829 /* Just the decode the image by macroblock (size is 8x8, 8x16, or 16x16) */
830 for (y
=0; y
< priv
->height
/ystride_by_mcu
; y
++)
832 //trace("Decoding row %d\n", y);
833 priv
->plane
[0] = priv
->components
[0] + (y
* bytes_per_blocklines
[0]);
834 priv
->plane
[1] = priv
->components
[1] + (y
* bytes_per_blocklines
[1]);
835 priv
->plane
[2] = priv
->components
[2] + (y
* bytes_per_blocklines
[2]);
836 for (x
=0; x
< priv
->width
; x
+=xstride_by_mcu
)
839 convert_to_pixfmt(priv
);
840 priv
->plane
[0] += bytes_per_mcu
[0];
841 priv
->plane
[1] += bytes_per_mcu
[1];
842 priv
->plane
[2] += bytes_per_mcu
[2];
850 const char *tinyjpeg_get_errorstring(struct jdec_private
*priv
)
855 void tinyjpeg_get_size(struct jdec_private
*priv
, unsigned int *width
, unsigned int *height
)
857 *width
= priv
->width
;
858 *height
= priv
->height
;
861 int tinyjpeg_get_components(struct jdec_private
*priv
, unsigned char **components
, unsigned int ncomponents
)
864 if (ncomponents
> COMPONENTS
)
865 ncomponents
= COMPONENTS
;
866 for (i
=0; i
<ncomponents
; i
++)
867 components
[i
] = priv
->components
[i
];
871 int tinyjpeg_set_components(struct jdec_private
*priv
, unsigned char * const *components
, unsigned int ncomponents
)
874 if (ncomponents
> COMPONENTS
)
875 ncomponents
= COMPONENTS
;
876 for (i
=0; i
<ncomponents
; i
++)
877 priv
->components
[i
] = components
[i
];
881 int tinyjpeg_get_bytes_per_row(struct jdec_private
*priv
,
883 unsigned int ncomponents
)
886 if (ncomponents
> COMPONENTS
)
887 ncomponents
= COMPONENTS
;
888 for (i
=0; i
<ncomponents
; i
++)
889 bytes
[i
] = priv
->bytes_per_row
[i
];
893 int tinyjpeg_set_bytes_per_row(struct jdec_private
*priv
,
894 const unsigned int *bytes
,
895 unsigned int ncomponents
)
898 if (ncomponents
> COMPONENTS
)
899 ncomponents
= COMPONENTS
;
900 for (i
=0; i
<ncomponents
; i
++)
901 priv
->bytes_per_row
[i
] = bytes
[i
];
905 int tinyjpeg_set_flags(struct jdec_private
*priv
, int flags
)
907 int oldflags
= priv
->flags
;