4 #include "bytesource.h"
11 unsigned width, height;
12 unsigned num_components;
13 unsigned hsample[256], vsample[256], qtable[256];
16 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
18 return fread(buf, 1, count, (FILE*)userdata);
21 void read_sof(struct byte_source* source, struct jpeg_image* image)
23 unsigned len = read_uint16(byte_source_input_func, source);
25 image->precision = read_uint8(byte_source_input_func, source);
26 assert(image->precision == 8);
27 image->width = read_uint16(byte_source_input_func, source);
28 image->height = read_uint16(byte_source_input_func, source);
29 image->num_components = read_uint8(byte_source_input_func, source);
32 fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
33 image->precision, image->width, image->height, image->num_components);
35 for (unsigned i = 0; i < image->num_components; ++i) {
37 unsigned c = read_uint8(byte_source_input_func, source);
38 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
39 image->hsample[c] = sampling_factors >> 4;
40 image->vsample[c] = sampling_factors & 0x0f;
41 image->qtable[c] = read_uint8(byte_source_input_func, source);
44 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
45 c, image->hsample[c], image->vsample[c], image->qtable[c]);
49 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
51 unsigned len = read_uint16(byte_source_input_func, source);
56 unsigned num_components = read_uint8(byte_source_input_func, source);
59 unsigned component_num[256];
60 unsigned dc_huffman_table[256], ac_huffman_table[256];
61 unsigned ss, se, ah_al;
64 for (unsigned i = 0; i < num_components; ++i) {
67 component_num[i] = read_uint8(byte_source_input_func, source);
68 td_ta = read_uint8(byte_source_input_func, source);
70 dc_huffman_table[i] = td_ta >> 4;
71 ac_huffman_table[i] = td_ta & 0x0f;
76 ss = read_uint8(byte_source_input_func, source);
77 se = read_uint8(byte_source_input_func, source);
78 ah_al = read_uint8(byte_source_input_func, source);
82 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
85 struct bit_source bits;
86 init_bit_source(&bits, byte_source_input_func, source);
89 for (unsigned c = 0; c < num_components; ++c) {
90 unsigned cn = component_num[c];
91 unsigned nc = image->vsample[cn] * image->hsample[cn];
92 for (unsigned n = 0; n < nc; ++n) {
93 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
94 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
96 // decode DC component
97 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
98 possibly_refill(&bits, dc_category);
99 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
101 // printf("dc=%d ac=", last_dc[c]);
104 // decode AC components
106 for (unsigned i = 0; i < 63; ++i) {
107 unsigned rs = read_huffman_symbol(ac_table, &bits);
108 unsigned r = rs >> 4;
109 unsigned s = rs & 0xf;
116 /* 16 zero coefficients */
121 possibly_refill(&bits, s);
124 zz[i] = extend(read_bits(&bits, s), s);
127 for (unsigned i = 0; i < 63; ++i) {
129 //printf("%d ", zz[i]);
136 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
140 void skip_segment(struct byte_source* source)
144 ssize_t ret = byte_source_input_func(source, buf, 4096);
146 fprintf(stderr, "Input error!\n");
157 struct jpeg_image jpeg;
160 struct byte_source source;
161 init_byte_source(&source, stdio_read, stdin);
163 huffman_tables_t tables;
166 uint8_t m2 = byte_source_read_marker(&source);
169 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
188 /* APP0 through APPF */
190 /* some EXIF stuff */
197 skip_segment(&source);
200 /* SOF0 (baseline DCT, Huffman encoded) */
201 read_sof(&source, &jpeg);
207 /* DHT (define Huffman tables) */
208 read_huffman_tables(&tables, byte_source_input_func, &source);
211 /* SOS (start of scan) */
212 read_scan(&source, &jpeg, &tables);
215 fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);