4 #include "bytesource.h"
12 unsigned width, height;
13 unsigned num_components;
14 unsigned hsample[256], vsample[256], qtable[256];
17 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
19 return fread(buf, 1, count, (FILE*)userdata);
22 void read_sof(struct byte_source* source, struct jpeg_image* image)
24 unsigned len = read_uint16(byte_source_input_func, source);
26 image->precision = read_uint8(byte_source_input_func, source);
27 assert(image->precision == 8);
28 image->width = read_uint16(byte_source_input_func, source);
29 image->height = read_uint16(byte_source_input_func, source);
30 image->num_components = read_uint8(byte_source_input_func, source);
33 fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
34 image->precision, image->width, image->height, image->num_components);
36 for (unsigned i = 0; i < image->num_components; ++i) {
38 unsigned c = read_uint8(byte_source_input_func, source);
39 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
40 image->hsample[c] = sampling_factors >> 4;
41 image->vsample[c] = sampling_factors & 0x0f;
42 image->qtable[c] = read_uint8(byte_source_input_func, source);
45 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
46 c, image->hsample[c], image->vsample[c], image->qtable[c]);
50 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
52 unsigned len = read_uint16(byte_source_input_func, source);
57 unsigned num_components = read_uint8(byte_source_input_func, source);
60 unsigned component_num[256];
61 unsigned dc_huffman_table[256], ac_huffman_table[256];
62 unsigned ss, se, ah_al;
65 for (unsigned i = 0; i < num_components; ++i) {
68 component_num[i] = read_uint8(byte_source_input_func, source);
69 td_ta = read_uint8(byte_source_input_func, source);
71 dc_huffman_table[i] = td_ta >> 4;
72 ac_huffman_table[i] = td_ta & 0x0f;
77 ss = read_uint8(byte_source_input_func, source);
78 se = read_uint8(byte_source_input_func, source);
79 ah_al = read_uint8(byte_source_input_func, source);
83 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
86 struct bit_source bits;
87 init_bit_source(&bits, byte_source_input_func, source);
90 for (unsigned c = 0; c < num_components; ++c) {
91 unsigned cn = component_num[c];
92 unsigned nc = image->vsample[cn] * image->hsample[cn];
93 for (unsigned n = 0; n < nc; ++n) {
94 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
95 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
97 // decode DC component
98 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
99 possibly_refill(&bits, dc_category);
100 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
102 // printf("dc=%d ac=", last_dc[c]);
105 // decode AC components
107 for (unsigned i = 0; i < 63; ++i) {
108 unsigned rs = read_huffman_symbol(ac_table, &bits);
109 unsigned r = rs >> 4;
110 unsigned s = rs & 0xf;
117 /* 16 zero coefficients */
122 possibly_refill(&bits, s);
125 zz[unzigzag[i]] = extend(read_bits(&bits, s), s);
128 for (unsigned i = 0; i < 63; ++i) {
130 //printf("%d ", zz[i]);
137 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
141 void skip_segment(struct byte_source* source)
145 ssize_t ret = byte_source_input_func(source, buf, 4096);
147 fprintf(stderr, "Input error!\n");
158 struct jpeg_image jpeg;
161 struct byte_source source;
162 init_byte_source(&source, stdio_read, stdin);
164 huffman_tables_t tables;
167 uint8_t m2 = byte_source_read_marker(&source);
170 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
189 /* APP0 through APPF */
191 /* some EXIF stuff */
198 skip_segment(&source);
201 /* SOF0 (baseline DCT, Huffman encoded) */
202 read_sof(&source, &jpeg);
208 /* DHT (define Huffman tables) */
209 read_huffman_tables(&tables, byte_source_input_func, &source);
212 /* SOS (start of scan) */
213 read_scan(&source, &jpeg, &tables);
216 fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);