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;
18 unsigned num_blocks_horizontal, num_blocks_vertical;
19 uint32_t qvalues[256][DCTSIZE2];
21 uint8_t* pixel_data[256];
24 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
26 return fread(buf, 1, count, (FILE*)userdata);
29 void read_dqt(struct byte_source* source, struct jpeg_image* image)
31 unsigned len = read_uint16(byte_source_input_func, source);
33 uint8_t precision_table = read_uint8(byte_source_input_func, source);
34 int precision = precision_table >> 4; // 0 = 8 bits, otherwise 16 bits.
35 int table = precision_table & 0x0f;
37 if (image->idct_data[table] != NULL) {
38 idct_choice_free(image->idct_data[table]);
43 fprintf(stderr, "Quantization table %u: 16 bits/entry\n", table);
46 fprintf(stderr, "Quantization table %u: 8 bits/entry\n", table);
49 for (unsigned i = 0; i < 64; ++i) {
51 image->qvalues[table][unzigzag[i]] =
52 read_uint16(byte_source_input_func, source);
54 image->qvalues[table][unzigzag[i]] =
55 read_uint8(byte_source_input_func, source);
59 image->idct_data[table] = idct_choice_alloc(image->qvalues[table]);
62 void read_sof(struct byte_source* source, struct jpeg_image* image)
64 unsigned len = read_uint16(byte_source_input_func, source);
66 image->precision = read_uint8(byte_source_input_func, source);
67 assert(image->precision == 8);
68 image->height = read_uint16(byte_source_input_func, source);
69 image->width = read_uint16(byte_source_input_func, source);
70 image->num_components = read_uint8(byte_source_input_func, source);
73 fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
74 image->precision, image->width, image->height, image->num_components);
76 for (unsigned i = 0; i < image->num_components; ++i) {
78 unsigned c = read_uint8(byte_source_input_func, source);
79 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
80 image->hsample[c] = sampling_factors >> 4;
81 image->vsample[c] = sampling_factors & 0x0f;
82 image->qtable[c] = read_uint8(byte_source_input_func, source);
85 if (image->hsample[c] > image->max_hsample) {
86 image->max_hsample = image->hsample[c];
88 if (image->vsample[c] > image->max_vsample) {
89 image->max_vsample = image->vsample[c];
92 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
93 c, image->hsample[c], image->vsample[c], image->qtable[c]);
96 image->num_blocks_horizontal = (image->width + image->max_hsample * DCTSIZE - 1) / (image->max_hsample * DCTSIZE);
97 image->num_blocks_vertical = (image->height + image->max_vsample * DCTSIZE - 1) / (image->max_vsample * DCTSIZE);
99 for (unsigned c = 0; c < 256; ++c) {
100 if (image->hsample[c] == 0) {
104 unsigned width = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
105 unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE;
106 image->pixel_data[c] = (uint8_t*)malloc(width * height);
107 assert(image->pixel_data[c] != NULL);
109 fprintf(stderr, "Component %u: allocating %d x %d\n", c, width, height);
113 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
115 unsigned len = read_uint16(byte_source_input_func, source);
120 unsigned num_components = read_uint8(byte_source_input_func, source);
123 unsigned component_num[256];
124 unsigned dc_huffman_table[256], ac_huffman_table[256];
125 unsigned ss, se, ah_al;
128 for (unsigned i = 0; i < num_components; ++i) {
131 component_num[i] = read_uint8(byte_source_input_func, source);
132 td_ta = read_uint8(byte_source_input_func, source);
134 dc_huffman_table[i] = td_ta >> 4;
135 ac_huffman_table[i] = td_ta & 0x0f;
140 ss = read_uint8(byte_source_input_func, source);
141 se = read_uint8(byte_source_input_func, source);
142 ah_al = read_uint8(byte_source_input_func, source);
146 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
149 struct bit_source bits;
150 init_bit_source(&bits, byte_source_input_func, 8, source);
152 unsigned mcu_x = 0, mcu_y = 0;
154 while (!bits.source_eof) {
155 for (unsigned c = 0; c < num_components; ++c) {
156 unsigned cn = component_num[c];
157 unsigned stride = image->num_blocks_horizontal * image->hsample[cn] * DCTSIZE;
158 assert(image->idct_data[image->qtable[cn]] != NULL);
160 for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb) {
161 for (unsigned local_xb = 0; local_xb < image->hsample[cn]; ++local_xb) {
162 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
163 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
165 // decode DC component
166 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
167 possibly_refill(&bits, dc_category);
168 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
170 int16_t coeff[DCTSIZE2] = { 0 };
171 coeff[0] = last_dc[c];
173 // decode AC components
174 for (unsigned i = 1; i < DCTSIZE2; ++i) {
175 unsigned rs = read_huffman_symbol(ac_table, &bits);
176 unsigned r = rs >> 4;
177 unsigned s = rs & 0xf;
184 /* 16 zero coefficients */
190 possibly_refill(&bits, s);
191 coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
194 uint8_t pixdata[DCTSIZE2];
195 idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
197 for (unsigned y = 0; y < DCTSIZE; ++y) {
198 unsigned real_x = (mcu_x * image->hsample[cn] + local_xb) * DCTSIZE;
199 unsigned real_y = (mcu_y * image->vsample[cn] + local_yb) * DCTSIZE + y;
201 memcpy(image->pixel_data[cn] + real_y * stride + real_x,
202 pixdata + y * DCTSIZE,
209 if (++mcu_x == image->num_blocks_horizontal) {
215 if (mcu_y == image->num_blocks_vertical) {
216 unsigned stride = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
217 unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE;
218 printf("P5\n%u %u\n255\n", stride, height);
219 fwrite(image->pixel_data[c], stride * height, 1, stdout);
224 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
228 void skip_segment(struct byte_source* source)
232 ssize_t ret = byte_source_input_func(source, buf, 4096);
234 fprintf(stderr, "Input error!\n");
245 struct jpeg_image jpeg;
246 memset(&jpeg, 0, sizeof(jpeg));
249 struct byte_source source;
250 init_byte_source(&source, stdio_read, stdin);
252 huffman_tables_t tables;
255 uint8_t m2 = byte_source_read_marker(&source);
258 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
277 /* APP0 through APPF */
279 /* some EXIF stuff */
284 skip_segment(&source);
288 read_dqt(&source, &jpeg);
291 /* SOF0 (baseline DCT, Huffman encoded) */
292 read_sof(&source, &jpeg);
301 /* DHT (define Huffman tables) */
302 read_huffman_tables(&tables, byte_source_input_func, &source);
305 /* SOS (start of scan) */
306 read_scan(&source, &jpeg, &tables);
309 fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);