4 #include "bytesource.h"
12 unsigned width, height;
13 unsigned num_components;
14 unsigned hsample[256], vsample[256], qtable[256];
15 uint32_t qvalues[256][DCTSIZE2];
18 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
20 return fread(buf, 1, count, (FILE*)userdata);
23 void read_dqt(struct byte_source* source, struct jpeg_image* image)
25 unsigned len = read_uint16(byte_source_input_func, source);
27 uint8_t precision_table = read_uint8(byte_source_input_func, source);
28 int precision = precision_table >> 4; // 0 = 8 bits, otherwise 16 bits.
29 int table = precision_table & 0x0f;
33 fprintf(stderr, "Quantization table %u: 16 bits/entry\n", table);
36 fprintf(stderr, "Quantization table %u: 8 bits/entry\n", table);
39 for (unsigned i = 0; i < 64; ++i) {
41 image->qvalues[table][unzigzag[i]] =
42 read_uint16(byte_source_input_func, source);
44 image->qvalues[table][unzigzag[i]] =
45 read_uint8(byte_source_input_func, source);
50 void read_sof(struct byte_source* source, struct jpeg_image* image)
52 unsigned len = read_uint16(byte_source_input_func, source);
54 image->precision = read_uint8(byte_source_input_func, source);
55 assert(image->precision == 8);
56 image->width = read_uint16(byte_source_input_func, source);
57 image->height = read_uint16(byte_source_input_func, source);
58 image->num_components = read_uint8(byte_source_input_func, source);
61 fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
62 image->precision, image->width, image->height, image->num_components);
64 for (unsigned i = 0; i < image->num_components; ++i) {
66 unsigned c = read_uint8(byte_source_input_func, source);
67 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
68 image->hsample[c] = sampling_factors >> 4;
69 image->vsample[c] = sampling_factors & 0x0f;
70 image->qtable[c] = read_uint8(byte_source_input_func, source);
73 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
74 c, image->hsample[c], image->vsample[c], image->qtable[c]);
78 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
80 unsigned len = read_uint16(byte_source_input_func, source);
85 unsigned num_components = read_uint8(byte_source_input_func, source);
88 unsigned component_num[256];
89 unsigned dc_huffman_table[256], ac_huffman_table[256];
90 unsigned ss, se, ah_al;
93 for (unsigned i = 0; i < num_components; ++i) {
96 component_num[i] = read_uint8(byte_source_input_func, source);
97 td_ta = read_uint8(byte_source_input_func, source);
99 dc_huffman_table[i] = td_ta >> 4;
100 ac_huffman_table[i] = td_ta & 0x0f;
105 ss = read_uint8(byte_source_input_func, source);
106 se = read_uint8(byte_source_input_func, source);
107 ah_al = read_uint8(byte_source_input_func, source);
111 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
114 struct bit_source bits;
115 init_bit_source(&bits, byte_source_input_func, source);
118 for (unsigned c = 0; c < num_components; ++c) {
119 unsigned cn = component_num[c];
120 unsigned nc = image->vsample[cn] * image->hsample[cn];
121 for (unsigned n = 0; n < nc; ++n) {
122 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
123 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
125 // decode DC component
126 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
127 possibly_refill(&bits, dc_category);
128 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
130 // printf("dc=%d ac=", last_dc[c]);
133 // decode AC components
135 for (unsigned i = 0; i < 63; ++i) {
136 unsigned rs = read_huffman_symbol(ac_table, &bits);
137 unsigned r = rs >> 4;
138 unsigned s = rs & 0xf;
145 /* 16 zero coefficients */
150 possibly_refill(&bits, s);
153 zz[unzigzag[i]] = extend(read_bits(&bits, s), s);
156 for (unsigned i = 0; i < 63; ++i) {
158 //printf("%d ", zz[i]);
165 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
169 void skip_segment(struct byte_source* source)
173 ssize_t ret = byte_source_input_func(source, buf, 4096);
175 fprintf(stderr, "Input error!\n");
186 struct jpeg_image jpeg;
189 struct byte_source source;
190 init_byte_source(&source, stdio_read, stdin);
192 huffman_tables_t tables;
195 uint8_t m2 = byte_source_read_marker(&source);
198 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
217 /* APP0 through APPF */
219 /* some EXIF stuff */
224 skip_segment(&source);
228 read_dqt(&source, &jpeg);
231 /* SOF0 (baseline DCT, Huffman encoded) */
232 read_sof(&source, &jpeg);
238 /* DHT (define Huffman tables) */
239 read_huffman_tables(&tables, byte_source_input_func, &source);
242 /* SOS (start of scan) */
243 read_scan(&source, &jpeg, &tables);
246 fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);