5 #include "bytesource.h"
14 unsigned width, height;
15 unsigned num_components;
16 unsigned hsample[256], vsample[256], qtable[256];
17 unsigned max_hsample, max_vsample;
19 unsigned num_blocks_horizontal, num_blocks_vertical;
20 uint32_t qvalues[256][DCTSIZE2];
22 uint8_t* pixel_data[256];
23 uint8_t* pixel_write_pointer[256];
26 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
28 return fread(buf, 1, count, (FILE*)userdata);
31 void read_dqt(struct byte_source* source, struct jpeg_image* image)
33 unsigned len = read_uint16(byte_source_input_func, source);
35 uint8_t precision_table = read_uint8(byte_source_input_func, source);
36 int precision = precision_table >> 4; // 0 = 8 bits, otherwise 16 bits.
37 int table = precision_table & 0x0f;
39 if (image->idct_data[table] != NULL) {
40 idct_choice_free(image->idct_data[table]);
45 fprintf(stderr, "Quantization table %u: 16 bits/entry\n", table);
48 fprintf(stderr, "Quantization table %u: 8 bits/entry\n", table);
51 for (unsigned i = 0; i < 64; ++i) {
53 image->qvalues[table][unzigzag[i]] =
54 read_uint16(byte_source_input_func, source);
56 image->qvalues[table][unzigzag[i]] =
57 read_uint8(byte_source_input_func, source);
61 image->idct_data[table] = idct_choice_alloc(image->qvalues[table]);
64 void read_sof(struct byte_source* source, struct jpeg_image* image)
66 unsigned len = read_uint16(byte_source_input_func, source);
68 image->precision = read_uint8(byte_source_input_func, source);
69 assert(image->precision == 8);
70 image->height = read_uint16(byte_source_input_func, source);
71 image->width = read_uint16(byte_source_input_func, source);
72 image->num_components = read_uint8(byte_source_input_func, source);
75 fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
76 image->precision, image->width, image->height, image->num_components);
78 for (unsigned i = 0; i < image->num_components; ++i) {
80 unsigned c = read_uint8(byte_source_input_func, source);
81 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
82 image->hsample[c] = sampling_factors >> 4;
83 image->vsample[c] = sampling_factors & 0x0f;
84 image->qtable[c] = read_uint8(byte_source_input_func, source);
87 if (image->hsample[c] > image->max_hsample) {
88 image->max_hsample = image->hsample[c];
90 if (image->vsample[c] > image->max_vsample) {
91 image->max_vsample = image->vsample[c];
94 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
95 c, image->hsample[c], image->vsample[c], image->qtable[c]);
98 image->num_blocks_horizontal = (image->width + image->max_hsample * DCTSIZE - 1) / (image->max_hsample * DCTSIZE);
99 image->num_blocks_vertical = (image->height + image->max_vsample * DCTSIZE - 1) / (image->max_vsample * DCTSIZE);
101 for (unsigned c = 0; c < 256; ++c) {
102 if (image->hsample[c] == 0) {
106 unsigned width = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
107 unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE;
108 image->stride[c] = width;
109 image->pixel_data[c] = (uint8_t*)malloc(width * height);
110 assert(image->pixel_data[c] != NULL);
111 image->pixel_write_pointer[c] = image->pixel_data[c];
113 fprintf(stderr, "Component %u: allocating %d x %d\n", c, width, height);
117 void decode_ac_coefficients(const struct huffman_table* tbl, struct bit_source* bits, int16_t* coeff)
119 possibly_refill(bits, DEHUF_AC_TABLE_BITS);
120 for (unsigned i = 0; i < DCTSIZE2 - 1; ) {
121 unsigned lookup = peek_bits(bits, DEHUF_AC_TABLE_BITS);
122 int code = tbl->ac_table_codes[lookup];
123 unsigned length = tbl->ac_table_length[lookup];
124 unsigned r = tbl->ac_table_skip[lookup];
126 if (__builtin_expect(code == AC_DEHUF_SLOW_PATH, 0)) {
127 unsigned rs = read_huffman_symbol_no_refill(tbl, bits);
133 unsigned r = rs >> 4;
134 unsigned s = rs & 0xf;
136 possibly_refill(bits, s);
137 coeff[unzigzag[i]] = extend(read_bits(bits, s), s);
138 possibly_refill(bits, DEHUF_AC_TABLE_BITS);
140 assert(bits->bits_available >= length);
141 read_bits(bits, length);
142 possibly_refill(bits, DEHUF_AC_TABLE_BITS);
146 coeff[unzigzag[i]] = code;
151 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
153 unsigned len = read_uint16(byte_source_input_func, source);
158 unsigned num_components = read_uint8(byte_source_input_func, source);
161 unsigned component_num[256];
162 unsigned dc_huffman_table[256], ac_huffman_table[256];
163 unsigned ss, se, ah_al;
166 for (unsigned i = 0; i < num_components; ++i) {
169 component_num[i] = read_uint8(byte_source_input_func, source);
170 td_ta = read_uint8(byte_source_input_func, source);
172 dc_huffman_table[i] = td_ta >> 4;
173 ac_huffman_table[i] = td_ta & 0x0f;
178 ss = read_uint8(byte_source_input_func, source);
179 se = read_uint8(byte_source_input_func, source);
180 ah_al = read_uint8(byte_source_input_func, source);
184 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
187 struct bit_source bits;
188 init_bit_source(&bits, byte_source_input_func, 8, source);
190 unsigned mcu_x = 0, mcu_y = 0;
192 while (!bits.source_eof) {
193 for (unsigned c = 0; c < num_components; ++c) {
194 unsigned cn = component_num[c];
195 assert(image->idct_data[image->qtable[cn]] != NULL);
197 uint8_t* pixel_write_pointer_y = image->pixel_write_pointer[cn];
198 for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb, pixel_write_pointer_y += image->stride[cn] * DCTSIZE) {
199 uint8_t* pixel_write_pointer = pixel_write_pointer_y;
200 for (unsigned local_xb = 0; local_xb < image->hsample[cn]; ++local_xb, pixel_write_pointer += DCTSIZE) {
201 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
202 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
204 // decode DC component
205 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
206 possibly_refill(&bits, dc_category);
207 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
209 int16_t coeff[DCTSIZE2] = { 0 };
210 coeff[0] = last_dc[c];
211 decode_ac_coefficients(ac_table, &bits, coeff);
213 uint8_t pixdata[DCTSIZE2];
214 idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
216 uint8_t* dest_pixdata = pixel_write_pointer;
217 for (unsigned y = 0; y < DCTSIZE; ++y, dest_pixdata += image->stride[cn]) {
218 memcpy(dest_pixdata, pixdata + y * DCTSIZE, DCTSIZE);
222 image->pixel_write_pointer[cn] += DCTSIZE * image->hsample[cn];
225 if (++mcu_x == image->num_blocks_horizontal) {
229 for (unsigned c = 0; c < num_components; ++c) {
230 unsigned cn = component_num[c];
231 image->pixel_write_pointer[cn] += (image->vsample[cn] * DCTSIZE - 1) * image->stride[cn];
236 if (mcu_y == image->num_blocks_vertical) {
237 unsigned stride = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
238 unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE;
239 printf("P5\n%u %u\n255\n", stride, height);
240 fwrite(image->pixel_data[c], stride * height, 1, stdout);
245 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
249 void skip_segment(struct byte_source* source)
253 ssize_t ret = byte_source_input_func(source, buf, 4096);
255 fprintf(stderr, "Input error!\n");
266 struct jpeg_image jpeg;
267 memset(&jpeg, 0, sizeof(jpeg));
270 struct byte_source source;
271 init_byte_source(&source, stdio_read, stdin);
273 huffman_tables_t tables;
276 uint8_t m2 = byte_source_read_marker(&source);
279 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
298 /* APP0 through APPF */
300 /* some EXIF stuff */
305 skip_segment(&source);
309 read_dqt(&source, &jpeg);
312 /* SOF0 (baseline DCT, Huffman encoded) */
313 read_sof(&source, &jpeg);
322 /* DHT (define Huffman tables) */
323 read_huffman_tables(&tables, byte_source_input_func, &source);
326 /* SOS (start of scan) */
327 read_scan(&source, &jpeg, &tables);
330 fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);