6 #include <liboil/liboil.h>
7 #include <liboil/liboil-stdint.h>
17 extern uint8_t jpeg_standard_tables
[];
18 extern int jpeg_standard_tables_size
;
20 void jpeg_decoder_define_huffman_tables (JpegDecoder
* dec
);
21 void jpeg_decoder_define_arithmetic_conditioning (JpegDecoder
*dec
);
22 void jpeg_decoder_define_quantization_tables (JpegDecoder
*dec
);
23 void jpeg_decoder_define_restart_interval (JpegDecoder
*dec
);
24 void jpeg_decoder_start_of_frame (JpegDecoder
* dec
, int marker
);
25 void jpeg_decoder_start_of_scan (JpegDecoder
* dec
);
28 /* misc helper function declarations */
30 static void jpeg_load_standard_huffman_tables (JpegDecoder
* dec
);
32 static void jpeg_decoder_verify_header (JpegDecoder
*dec
);
33 static void jpeg_decoder_init_decoder (JpegDecoder
*dec
);
37 jpeg_decoder_error(JpegDecoder
*dec
, const char *fmt
, ...)
41 if (dec
->error
) return;
43 va_start (varargs
, fmt
);
45 vasprintf(&dec
->error_message
, fmt
, varargs
);
47 dec
->error_message
= malloc(250);
48 vsnprintf(dec
->error_message
, 250 - 1, fmt
, varargs
);
49 dec
->error_message
[250 - 1] = 0;
56 #define jpeg_decoder_error(dec, ...) { \
57 COG_ERROR("decoder error: "__VA_ARGS__); \
58 jpeg_decoder_error (dec, __VA_ARGS__); \
62 jpeg_decoder_verify_header (JpegDecoder
*dec
)
64 int max_quant_table
= 0;
67 if (dec
->sof_type
!= JPEG_MARKER_SOF_0
) {
68 COG_ERROR("only handle baseline DCT");
73 COG_ERROR("width can't be 0");
77 if (dec
->height
< 1) {
78 COG_ERROR("height can't be 0");
82 switch (dec
->sof_type
) {
83 case JPEG_MARKER_SOF_0
:
86 if (dec
->depth
!= 8) {
87 COG_ERROR("depth must be 8 (%d)", dec
->depth
);
91 case JPEG_MARKER_SOF_1
:
94 if (dec
->depth
!= 8 && dec
->depth
!= 12) {
95 COG_ERROR("depth must be 8 or 12 (%d)", dec
->depth
);
99 case JPEG_MARKER_SOF_2
:
100 /* progressive DCT */
102 if (dec
->depth
!= 8 && dec
->depth
!= 12) {
103 COG_ERROR("depth must be 8 or 12 (%d)", dec
->depth
);
107 case JPEG_MARKER_SOF_3
:
110 if (dec
->depth
< 2 || dec
->depth
> 16) {
111 COG_ERROR("depth must be between 2 and 16 (%d)", dec
->depth
);
119 if (dec
->n_components
< 0 || dec
->n_components
> 255) {
120 COG_ERROR("n_components must be in the range 0-255 (%d)",
124 if (dec
->sof_type
== JPEG_MARKER_SOF_2
&& dec
->n_components
> 4) {
125 COG_ERROR("n_components must be <= 4 for progressive DCT (%d)",
130 for (i
= 0; i
< dec
->n_components
; i
++) {
131 if (dec
->components
[i
].id
< 0 || dec
->components
[i
].id
> 255) {
132 COG_ERROR("component ID out of range");
136 if (dec
->components
[i
].h_sample
< 1 || dec
->components
[i
].h_sample
> 4 ||
137 dec
->components
[i
].v_sample
< 1 || dec
->components
[i
].v_sample
> 4) {
138 COG_ERROR("sample factor(s) for component %d out of range %d %d",
139 i
, dec
->components
[i
].h_sample
, dec
->components
[i
].v_sample
);
143 if (dec
->components
[i
].quant_table
< 0 ||
144 dec
->components
[i
].quant_table
> max_quant_table
) {
145 COG_ERROR("quant table for component %d out of range (%d)",
146 i
, dec
->components
[i
].quant_table
);
152 if ((1 << 30) / dec
->width
/ dec
->height
< dec
->n_components
) {
153 COG_ERROR ("image is too big (width %d, height %d, %d components)",
154 dec
->width
, dec
->height
, dec
->n_components
);
160 jpeg_decoder_init_decoder (JpegDecoder
*dec
)
162 int max_h_sample
= 0;
163 int max_v_sample
= 0;
166 /* decoder limitations */
167 if (dec
->n_components
!= 3) {
168 jpeg_decoder_error(dec
, "wrong number of components %d", dec
->n_components
);
171 if (dec
->sof_type
!= JPEG_MARKER_SOF_0
) {
172 jpeg_decoder_error(dec
, "only handle baseline DCT");
176 for (i
=0; i
< dec
->n_components
; i
++) {
177 max_h_sample
= MAX (max_h_sample
, dec
->components
[i
].h_sample
);
178 max_v_sample
= MAX (max_v_sample
, dec
->components
[i
].v_sample
);
182 (dec
->width
+ 8 * max_h_sample
- 1) / (8 * max_h_sample
);
184 (dec
->height
+ 8 * max_v_sample
- 1) / (8 * max_v_sample
);
185 for (i
= 0; i
< dec
->n_components
; i
++) {
189 dec
->components
[i
].h_subsample
= max_h_sample
/
190 dec
->components
[i
].h_sample
;
191 dec
->components
[i
].v_subsample
= max_v_sample
/
192 dec
->components
[i
].v_sample
;
194 rowstride
= dec
->width_blocks
* 8 * max_h_sample
/
195 dec
->components
[i
].h_subsample
;
196 image_size
= rowstride
*
197 (dec
->height_blocks
* 8 * max_v_sample
/
198 dec
->components
[i
].v_subsample
);
199 dec
->components
[i
].rowstride
= rowstride
;
200 dec
->components
[i
].image
= malloc (image_size
);
206 sprintbits (char *str
, unsigned int bits
, int n
)
209 int bit
= 1 << (n
- 1);
211 for (i
= 0; i
< n
; i
++) {
212 str
[i
] = (bits
& bit
) ? '1' : '0';
221 generate_code_table (int *huffsize
)
233 for (i
= 0; i
< 16; i
++) {
234 for (j
= 0; j
< huffsize
[i
]; j
++) {
235 COG_DEBUG ("huffcode[%d] = %s", k
,
236 sprintbits (str
, code
>> (15 - i
), i
+ 1));
247 huffman_table_init_jpeg (JpegDecoder
*decoder
, HuffmanTable
*table
, JpegBits
* bits
)
255 huffman_table_init (table
);
257 /* huffsize[i] is the number of symbols that have length
258 * (i+1) bits. Maximum bit length is 16 bits, so there are
261 for (i
= 0; i
< 16; i
++) {
262 huffsize
[i
] = jpeg_bits_get_u8 (bits
);
264 n_symbols
+= huffsize
[i
];
267 /* Build up the symbol table. The first symbol is all 0's, with
268 * the number of bits determined by the first non-zero entry in
269 * huffsize[]. Subsequent symbols with the same bit length are
270 * incremented by 1. Increasing the bit length shifts the
271 * symbol 1 bit to the left. */
274 for (i
= 0; i
< 16; i
++) {
275 for (j
= 0; j
< huffsize
[i
]; j
++) {
276 huffman_table_add (table
, symbol
, i
+ 1, jpeg_bits_get_u8 (bits
));
281 /* This checks that our symbol is actually less than the
282 * number of bits we think it is. This is only triggered
283 * for bad huffsize[] arrays. */
284 if (symbol
>= (1U << (i
+ 1))) {
285 jpeg_decoder_error (decoder
, "bad huffsize[] array");
292 huffman_table_dump (table
);
298 jpeg_decoder_find_component_by_id (JpegDecoder
* dec
, int id
)
302 for (i
= 0; i
< dec
->n_components
; i
++) {
303 if (dec
->components
[i
].id
== id
)
306 COG_DEBUG ("undefined component id %d", id
);
311 jpeg_decoder_application0 (JpegDecoder
* dec
, JpegBits
* bits
)
317 length
= get_be_u16 (bits
);
318 COG_DEBUG ("length=%d", length
);
320 if (memcmp (bits
->ptr
, "JFIF", 4) == 0 && bits
->ptr
[4] == 0) {
331 version
= get_be_u16 (bits
);
332 units
= get_u8 (bits
);
333 x_density
= get_be_u16 (bits
);
334 y_density
= get_be_u16 (bits
);
335 x_thumbnail
= get_u8 (bits
);
336 y_thumbnail
= get_u8 (bits
);
338 COG_DEBUG ("version = %04x", version
);
339 COG_DEBUG ("units = %d", units
);
340 COG_DEBUG ("x_density = %d", x_density
);
341 COG_DEBUG ("y_density = %d", y_density
);
342 COG_DEBUG ("x_thumbnail = %d", x_thumbnail
);
343 COG_DEBUG ("y_thumbnail = %d", y_thumbnail
);
347 if (memcmp (bits
->ptr
, "JFXX", 4) == 0 && bits
->ptr
[4] == 0) {
348 COG_DEBUG ("JFIF extension (not handled)");
349 bits
->ptr
+= length
- 2;
356 jpeg_decoder_application_misc (JpegDecoder
* dec
, JpegBits
* bits
)
362 length
= get_be_u16 (bits
);
363 COG_DEBUG ("length=%d", length
);
365 COG_DEBUG ("JPEG application tag X ignored");
368 bits
->ptr
+= length
- 2;
374 jpeg_decoder_comment (JpegDecoder
* dec
, JpegBits
* bits
)
378 COG_DEBUG ("comment");
380 length
= get_be_u16 (bits
);
381 COG_DEBUG ("length=%d", length
);
385 bits
->ptr
+= length
- 2;
391 jpeg_decoder_restart_interval (JpegDecoder
* dec
, JpegBits
* bits
)
395 COG_DEBUG ("comment");
397 length
= get_be_u16 (bits
);
398 COG_DEBUG ("length=%d", length
);
400 dec
->restart_interval
= get_be_u16 (bits
);
401 COG_DEBUG ("restart_interval=%d", dec
->restart_interval
);
407 jpeg_decoder_restart (JpegDecoder
* dec
, JpegBits
* bits
)
409 COG_DEBUG ("restart");
415 jpeg_decoder_decode_entropy_segment (JpegDecoder
* dec
)
417 JpegBits
* bits
= &dec
->bits
;
418 JpegBits b2
, *bits2
= &b2
;
421 unsigned char *newptr
;
432 maxlen
= jpeg_bits_available (bits
) - 1;
434 while (len
< maxlen
) {
435 if (bits
->ptr
[len
] == 0xff && bits
->ptr
[len
+ 1] != 0x00) {
440 COG_DEBUG ("entropy length = %d", len
);
442 /* we allocate extra space, since the getbits() code can
443 * potentially read past the end of the buffer */
444 newptr
= malloc (len
+ 2);
445 for (i
= 0; i
< len
; i
++) {
446 newptr
[j
] = bits
->ptr
[i
];
448 if (bits
->ptr
[i
] == 0xff)
455 bits2
->end
= newptr
+ j
;
459 dec
->dc
[0] = dec
->dc
[1] = dec
->dc
[2] = dec
->dc
[3] = 128 * 8;
463 n
= dec
->restart_interval
;
464 if (n
== 0) n
= (1<<26); /* max number of blocks */
465 while (go
&& n
-- > 0) {
466 for (i
= 0; i
< dec
->scan_list_length
; i
++) {
473 COG_DEBUG ("%d,%d: component=%d dc_table=%d ac_table=%d",
475 dec
->scan_list
[i
].component_index
,
476 dec
->scan_list
[i
].dc_table
, dec
->scan_list
[i
].ac_table
);
478 component_index
= dec
->scan_list
[i
].component_index
;
479 dc_table_index
= dec
->scan_list
[i
].dc_table
;
480 ac_table_index
= dec
->scan_list
[i
].ac_table
;
481 quant_index
= dec
->scan_list
[i
].quant_table
;
483 ret
= huffman_table_decode_macroblock (dec
, block
,
484 &dec
->dc_huff_table
[dc_table_index
],
485 &dec
->ac_huff_table
[ac_table_index
], bits2
);
487 COG_DEBUG ("%d,%d: component=%d dc_table=%d ac_table=%d",
489 dec
->scan_list
[i
].component_index
,
490 dec
->scan_list
[i
].dc_table
, dec
->scan_list
[i
].ac_table
);
495 COG_DEBUG ("using quant table %d", quant_index
);
496 oil_mult8x8_s16 (block2
, block
, dec
->quant_tables
[quant_index
].quantizer
,
497 sizeof (short) * 8, sizeof(short) * 8, sizeof (short) * 8);
498 dec
->dc
[component_index
] += block2
[0];
499 block2
[0] = dec
->dc
[component_index
];
500 oil_unzigzag8x8_s16 (block
, sizeof (short) * 8, block2
,
502 oil_idct8x8_s16 (block2
, sizeof (short) * 8, block
, sizeof (short) * 8);
503 oil_trans8x8_s16 (block
, sizeof (short) * 8, block2
, sizeof (short) * 8);
505 ptr
= dec
->components
[component_index
].image
+
506 x
* dec
->components
[component_index
].h_sample
+
507 dec
->scan_list
[i
].offset
+
508 dec
->components
[component_index
].rowstride
* y
*
509 dec
->components
[component_index
].v_sample
;
511 oil_clipconv8x8_u8_s16 (ptr
,
512 dec
->components
[component_index
].rowstride
,
513 block
, sizeof (short) * 8);
516 if (x
* dec
->scan_h_subsample
>= dec
->width
) {
520 if (y
* dec
->scan_v_subsample
>= dec
->height
) {
532 jpeg_decoder_new (void)
538 dec
= malloc (sizeof(JpegDecoder
));
539 memset (dec
, 0, sizeof(JpegDecoder
));
541 jpeg_load_standard_huffman_tables (dec
);
547 jpeg_decoder_free (JpegDecoder
* dec
)
551 for (i
= 0; i
< JPEG_LIMIT_COMPONENTS
; i
++) {
552 if (dec
->components
[i
].image
)
553 free (dec
->components
[i
].image
);
563 jpeg_decoder_get_marker (JpegDecoder
*dec
, int *marker
)
566 JpegBits
*bits
= &dec
->bits
;
568 if (jpeg_bits_available(bits
) < 2) {
572 a
= jpeg_bits_get_u8(bits
);
574 jpeg_decoder_error(dec
, "expected marker, not 0x%02x", a
);
579 b
= jpeg_bits_get_u8 (bits
);
580 } while (b
== 0xff && jpeg_bits_error(bits
));
587 jpeg_decoder_skip (JpegDecoder
*dec
)
591 length
= jpeg_bits_get_u16_be (&dec
->bits
);
592 jpeg_bits_skip (&dec
->bits
, length
- 2);
596 jpeg_decoder_decode (JpegDecoder
*dec
)
605 /* Note: The spec is ambiguous as to whether fill bytes can come
606 * before the first marker. We'll assume yes. */
607 if (!jpeg_decoder_get_marker (dec
, &marker
)) {
610 if (dec
->strict
&& marker
!= JPEG_MARKER_SOI
) {
611 jpeg_decoder_error(dec
, "not a JPEG image");
615 /* Interpret markers up to the start of frame */
616 while (!dec
->error
) {
617 if (!jpeg_decoder_get_marker (dec
, &marker
)) {
621 if (marker
== JPEG_MARKER_DEFINE_HUFFMAN_TABLES
) {
622 jpeg_decoder_define_huffman_tables (dec
);
623 } else if (marker
== JPEG_MARKER_DEFINE_ARITHMETIC_CONDITIONING
) {
624 jpeg_decoder_define_arithmetic_conditioning (dec
);
625 } else if (marker
== JPEG_MARKER_DEFINE_QUANTIZATION_TABLES
) {
626 jpeg_decoder_define_quantization_tables (dec
);
627 } else if (marker
== JPEG_MARKER_DEFINE_RESTART_INTERVAL
) {
628 jpeg_decoder_define_restart_interval (dec
);
629 } else if (JPEG_MARKER_IS_APP(marker
)) {
630 /* FIXME decode app segment */
631 jpeg_decoder_skip (dec
);
632 } else if (marker
== JPEG_MARKER_COMMENT
) {
633 jpeg_decoder_skip (dec
);
634 } else if (JPEG_MARKER_IS_START_OF_FRAME(marker
)) {
636 } else if (marker
== JPEG_MARKER_SOI
) {
638 jpeg_decoder_error (dec
, "unexpected SOI");
641 } else if (marker
== JPEG_MARKER_EOI
) {
643 jpeg_decoder_error (dec
, "unexpected EOI");
647 jpeg_decoder_error(dec
, "unexpected marker 0x%02x", marker
);
655 jpeg_decoder_start_of_frame(dec
, marker
);
657 jpeg_decoder_verify_header (dec
);
662 jpeg_decoder_init_decoder (dec
);
667 /* In this section, we loop over parse units until we reach the end
669 while (!dec
->error
) {
670 if (!jpeg_decoder_get_marker (dec
, &marker
)) {
674 if (marker
== JPEG_MARKER_DEFINE_HUFFMAN_TABLES
) {
675 jpeg_decoder_define_huffman_tables (dec
);
676 } else if (marker
== JPEG_MARKER_DEFINE_ARITHMETIC_CONDITIONING
) {
677 jpeg_decoder_define_arithmetic_conditioning (dec
);
678 } else if (marker
== JPEG_MARKER_DEFINE_QUANTIZATION_TABLES
) {
679 jpeg_decoder_define_quantization_tables (dec
);
680 } else if (marker
== JPEG_MARKER_DEFINE_RESTART_INTERVAL
) {
681 jpeg_decoder_define_restart_interval (dec
);
682 } else if (JPEG_MARKER_IS_APP(marker
)) {
683 jpeg_decoder_skip (dec
);
684 } else if (marker
== JPEG_MARKER_COMMENT
) {
685 jpeg_decoder_skip (dec
);
686 } else if (marker
== JPEG_MARKER_SOS
) {
687 jpeg_decoder_start_of_scan (dec
);
691 jpeg_decoder_decode_entropy_segment (dec
);
692 } else if (JPEG_MARKER_IS_RESET(marker
)) {
693 jpeg_decoder_decode_entropy_segment (dec
);
694 } else if (marker
== JPEG_MARKER_SOI
) {
696 jpeg_decoder_error (dec
, "unexpected SOI");
699 } else if (marker
== JPEG_MARKER_EOI
) {
701 jpeg_decoder_error (dec
, "unexpected EOI");
707 jpeg_decoder_error(dec
, "unexpected marker 0x%02x", marker
);
721 jpeg_decoder_define_huffman_tables (JpegDecoder
* dec
)
723 JpegBits
*bits
= &dec
->bits
;
728 HuffmanTable
*hufftab
;
730 COG_DEBUG ("define huffman tables");
732 length
= jpeg_bits_get_u16_be (bits
);
734 jpeg_decoder_error(dec
, "length too short");
740 x
= jpeg_bits_get_u8 (bits
);
746 COG_DEBUG ("huff table type %d (%s) idx %d", tc
, tc
? "ac" : "dc", th
);
747 if (tc
> 1 || th
> 3) {
748 jpeg_decoder_error(dec
, "huffman table type or idx out of range");
753 hufftab
= &dec
->ac_huff_table
[th
];
754 length
-= huffman_table_init_jpeg (dec
, hufftab
, bits
);
756 hufftab
= &dec
->dc_huff_table
[th
];
757 length
-= huffman_table_init_jpeg (dec
, hufftab
, bits
);
764 jpeg_decoder_error(dec
, "huffman table overran available bytes");
770 jpeg_decoder_define_quantization_tables (JpegDecoder
*dec
)
772 JpegBits
*bits
= &dec
->bits
;
773 JpegQuantTable
*table
;
779 COG_INFO ("define quantization table");
781 length
= jpeg_bits_get_u16_be (bits
);
783 jpeg_decoder_error(dec
, "length too short");
791 x
= jpeg_bits_get_u8 (bits
);
797 jpeg_decoder_error (dec
, "bad pq value");
801 jpeg_decoder_error (dec
, "bad tq value");
805 table
= &dec
->quant_tables
[tq
];
807 for (i
= 0; i
< 64; i
++) {
808 table
->quantizer
[i
] = jpeg_bits_get_u16_be (bits
);
812 for (i
= 0; i
< 64; i
++) {
813 table
->quantizer
[i
] = jpeg_bits_get_u8 (bits
);
819 jpeg_decoder_error(dec
, "quantization table overran available bytes");
825 jpeg_decoder_define_restart_interval (JpegDecoder
*dec
)
827 JpegBits
*bits
= &dec
->bits
;
830 length
= jpeg_bits_get_u16_be (bits
);
832 jpeg_decoder_error(dec
, "length supposed to be 4 (%d)", length
);
836 /* FIXME this needs to be checked somewhere */
837 dec
->restart_interval
= jpeg_bits_get_u16_be (bits
);
841 jpeg_decoder_define_arithmetic_conditioning (JpegDecoder
*dec
)
843 /* we don't handle arithmetic coding, so skip it */
844 jpeg_decoder_skip (dec
);
848 jpeg_decoder_start_of_frame (JpegDecoder
* dec
, int marker
)
850 JpegBits
*bits
= &dec
->bits
;
854 COG_INFO ("start of frame");
856 dec
->sof_type
= marker
;
858 length
= jpeg_bits_get_u16_be (bits
);
860 if (jpeg_bits_available(bits
) < length
) {
861 jpeg_decoder_error(dec
, "not enough data for start_of_frame (%d < %d)",
862 length
, jpeg_bits_available(bits
));
866 dec
->depth
= jpeg_bits_get_u8 (bits
);
867 dec
->height
= jpeg_bits_get_u16_be (bits
);
868 dec
->width
= jpeg_bits_get_u16_be (bits
);
869 dec
->n_components
= jpeg_bits_get_u8 (bits
);
872 "frame_length=%d depth=%d height=%d width=%d n_components=%d", length
,
873 dec
->depth
, dec
->height
, dec
->width
, dec
->n_components
);
875 if (dec
->n_components
* 3 + 8 != length
) {
876 jpeg_decoder_error(dec
, "inconsistent header");
880 for (i
= 0; i
< dec
->n_components
; i
++) {
881 dec
->components
[i
].id
= get_u8 (bits
);
882 dec
->components
[i
].h_sample
= getbits (bits
, 4);
883 dec
->components
[i
].v_sample
= getbits (bits
, 4);
884 dec
->components
[i
].quant_table
= get_u8 (bits
);
887 "[%d] id=%d h_sample=%d v_sample=%d quant_table=%d", i
,
888 dec
->components
[i
].id
, dec
->components
[i
].h_sample
,
889 dec
->components
[i
].v_sample
, dec
->components
[i
].quant_table
);
894 jpeg_decoder_start_of_scan (JpegDecoder
* dec
)
896 JpegBits
*bits
= &dec
->bits
;
907 COG_DEBUG ("start of scan");
909 length
= jpeg_bits_get_u16_be (bits
);
910 COG_DEBUG ("length=%d", length
);
912 n_components
= jpeg_bits_get_u8 (bits
);
914 dec
->scan_h_subsample
= 0;
915 dec
->scan_v_subsample
= 0;
916 for (i
= 0; i
< n_components
; i
++) {
927 component_id
= jpeg_bits_get_u8 (bits
);
928 tmp
= jpeg_bits_get_u8 (bits
);
930 ac_table
= tmp
& 0xf;
931 idx
= jpeg_decoder_find_component_by_id (dec
, component_id
);
933 h_subsample
= dec
->components
[idx
].h_sample
;
934 v_subsample
= dec
->components
[idx
].v_sample
;
935 quant_index
= dec
->components
[idx
].quant_table
;
937 for (y
= 0; y
< v_subsample
; y
++) {
938 for (x
= 0; x
< h_subsample
; x
++) {
939 dec
->scan_list
[n
].component_index
= idx
;
940 dec
->scan_list
[n
].dc_table
= dc_table
;
941 dec
->scan_list
[n
].ac_table
= ac_table
;
942 dec
->scan_list
[n
].quant_table
= quant_index
;
943 dec
->scan_list
[n
].x
= x
;
944 dec
->scan_list
[n
].y
= y
;
945 dec
->scan_list
[n
].offset
=
946 y
* 8 * dec
->components
[idx
].rowstride
+ x
* 8;
948 if (n
> JPEG_LIMIT_SCAN_LIST_LENGTH
) {
949 jpeg_decoder_error(dec
, "scan list too long");
955 dec
->scan_h_subsample
= MAX (dec
->scan_h_subsample
, h_subsample
);
956 dec
->scan_v_subsample
= MAX (dec
->scan_v_subsample
, v_subsample
);
958 COG_DEBUG ("component %d: idx=%d dc_table=%d ac_table=%d n=%d",
959 component_id
, idx
, dc_table
, ac_table
, n
);
961 dec
->scan_list_length
= n
;
964 spectral_start
= jpeg_bits_get_u8 (bits
);
965 spectral_end
= jpeg_bits_get_u8 (bits
);
966 COG_DEBUG ("spectral range [%d,%d]", spectral_start
, spectral_end
);
967 tmp
= jpeg_bits_get_u8 (bits
);
968 approx_high
= tmp
>> 4;
969 approx_low
= tmp
& 0xf;
970 COG_DEBUG ("approx range [%d,%d]", approx_low
, approx_high
);
974 dec
->dc
[0] = dec
->dc
[1] = dec
->dc
[2] = dec
->dc
[3] = 128 * 8;
989 jpeg_decoder_addbits (JpegDecoder
* dec
, unsigned char *data
, unsigned int len
)
993 offset
= dec
->bits
.ptr
- dec
->data
;
995 dec
->data
= realloc (dec
->data
, dec
->data_len
+ len
);
996 memcpy (dec
->data
+ dec
->data_len
, data
, len
);
997 dec
->data_len
+= len
;
999 dec
->bits
.ptr
= dec
->data
+ offset
;
1000 dec
->bits
.end
= dec
->data
+ dec
->data_len
;
1006 jpeg_decoder_get_image_size (JpegDecoder
* dec
, unsigned int *width
, unsigned int *height
)
1009 *width
= (unsigned) dec
->width
;
1011 *height
= (unsigned) dec
->height
;
1017 jpeg_decoder_get_component_ptr (JpegDecoder
* dec
, int id
,
1018 unsigned char **image
, int *rowstride
)
1022 i
= jpeg_decoder_find_component_by_id (dec
, id
);
1024 *image
= dec
->components
[i
].image
;
1026 *rowstride
= dec
->components
[i
].rowstride
;
1032 jpeg_decoder_get_component_size (JpegDecoder
* dec
, int id
,
1033 int *width
, int *height
)
1037 /* subsampling sizes are rounded up */
1039 i
= jpeg_decoder_find_component_by_id (dec
, id
);
1041 *width
= (dec
->width
- 1) / dec
->components
[i
].h_subsample
+ 1;
1043 *height
= (dec
->height
- 1) / dec
->components
[i
].v_subsample
+ 1;
1049 jpeg_decoder_get_component_subsampling (JpegDecoder
* dec
, int id
,
1050 int *h_subsample
, int *v_subsample
)
1054 i
= jpeg_decoder_find_component_by_id (dec
, id
);
1056 *h_subsample
= dec
->components
[i
].h_subsample
;
1058 *v_subsample
= dec
->components
[i
].v_subsample
;
1065 jpeg_decoder_parse (JpegDecoder
* dec
)
1067 JpegBits
*bits
= &dec
->bits
;
1073 while (bits
->ptr
< bits
->end
) {
1082 OIL_DEBUG ("lost sync, skipped %d bytes", n
);
1088 OIL_DEBUG ("tag %02x", tag
);
1092 for (i
= 0; i
< n_jpeg_markers
- 1; i
++) {
1093 if (tag
== jpeg_markers
[i
].tag
) {
1097 OIL_DEBUG ("tag: %s", jpeg_markers
[i
].name
);
1098 if (jpeg_markers
[i
].func
) {
1099 jpeg_markers
[i
].func (dec
, &b2
);
1101 OIL_DEBUG ("unhandled or illegal JPEG marker (0x%02x)", tag
);
1104 if (jpeg_markers
[i
].flags
& JPEG_ENTROPY_SEGMENT
) {
1105 jpeg_decoder_decode_entropy_segment (dec
, &b2
);
1116 /* misc helper functins */
1119 jpeg_load_standard_huffman_tables (JpegDecoder
* dec
)
1121 JpegBits b
, *bits
= &b
;
1123 bits
->ptr
= jpeg_standard_tables
;
1125 bits
->end
= jpeg_standard_tables
+ jpeg_standard_tables_size
;
1127 huffman_table_init_jpeg (dec
, &dec
->dc_huff_table
[0], bits
);
1128 huffman_table_init_jpeg (dec
, &dec
->ac_huff_table
[0], bits
);
1129 huffman_table_init_jpeg (dec
, &dec
->dc_huff_table
[1], bits
);
1130 huffman_table_init_jpeg (dec
, &dec
->ac_huff_table
[1], bits
);