5 #include "bytesource.h"
14 unsigned width, height;
15 unsigned num_components;
16 unsigned hsample[256], vsample[256], qtable[256];
17 uint32_t qvalues[256][DCTSIZE2];
21 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
23 return fread(buf, 1, count, (FILE*)userdata);
26 void read_dqt(struct byte_source* source, struct jpeg_image* image)
28 unsigned len = read_uint16(byte_source_input_func, source);
30 uint8_t precision_table = read_uint8(byte_source_input_func, source);
31 int precision = precision_table >> 4; // 0 = 8 bits, otherwise 16 bits.
32 int table = precision_table & 0x0f;
34 if (image->idct_data[table] != NULL) {
35 idct_choice_free(image->idct_data[table]);
40 fprintf(stderr, "Quantization table %u: 16 bits/entry\n", table);
43 fprintf(stderr, "Quantization table %u: 8 bits/entry\n", table);
46 for (unsigned i = 0; i < 64; ++i) {
48 image->qvalues[table][unzigzag[i]] =
49 read_uint16(byte_source_input_func, source);
51 image->qvalues[table][unzigzag[i]] =
52 read_uint8(byte_source_input_func, source);
56 image->idct_data[table] = idct_choice_alloc(image->qvalues[table]);
59 void read_sof(struct byte_source* source, struct jpeg_image* image)
61 unsigned len = read_uint16(byte_source_input_func, source);
63 image->precision = read_uint8(byte_source_input_func, source);
64 assert(image->precision == 8);
65 image->height = read_uint16(byte_source_input_func, source);
66 image->width = read_uint16(byte_source_input_func, source);
67 image->num_components = read_uint8(byte_source_input_func, source);
70 fprintf(stderr, "%u-bit %ux%u JPEG with %u components\n",
71 image->precision, image->width, image->height, image->num_components);
73 for (unsigned i = 0; i < image->num_components; ++i) {
75 unsigned c = read_uint8(byte_source_input_func, source);
76 unsigned sampling_factors = read_uint8(byte_source_input_func, source);
77 image->hsample[c] = sampling_factors >> 4;
78 image->vsample[c] = sampling_factors & 0x0f;
79 image->qtable[c] = read_uint8(byte_source_input_func, source);
82 fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
83 c, image->hsample[c], image->vsample[c], image->qtable[c]);
87 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
89 unsigned len = read_uint16(byte_source_input_func, source);
94 unsigned num_components = read_uint8(byte_source_input_func, source);
97 unsigned component_num[256];
98 unsigned dc_huffman_table[256], ac_huffman_table[256];
99 unsigned ss, se, ah_al;
102 for (unsigned i = 0; i < num_components; ++i) {
105 component_num[i] = read_uint8(byte_source_input_func, source);
106 td_ta = read_uint8(byte_source_input_func, source);
108 dc_huffman_table[i] = td_ta >> 4;
109 ac_huffman_table[i] = td_ta & 0x0f;
114 ss = read_uint8(byte_source_input_func, source);
115 se = read_uint8(byte_source_input_func, source);
116 ah_al = read_uint8(byte_source_input_func, source);
120 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
123 struct bit_source bits;
124 init_bit_source(&bits, byte_source_input_func, source);
126 // Some debugging code.
127 assert(image->width % 8 == 0);
128 int y_stride = image->width;
129 uint8_t* y_row_data = (uint8_t*)malloc(y_stride * DCTSIZE * image->vsample[1]);
130 assert(y_row_data != NULL);
134 for (unsigned c = 0; c < num_components; ++c) {
135 unsigned cn = component_num[c];
136 assert(image->idct_data[image->qtable[cn]] != NULL);
138 for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb) {
139 for (unsigned local_xb = 0; local_xb < image->hsample[cn]; ++local_xb) {
140 const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
141 const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
143 // decode DC component
144 unsigned dc_category = read_huffman_symbol(dc_table, &bits);
145 possibly_refill(&bits, dc_category);
146 last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
148 int16_t coeff[DCTSIZE2] = { 0 };
149 coeff[0] = last_dc[c];
151 // decode AC components
152 for (unsigned i = 1; i < DCTSIZE2; ++i) {
153 unsigned rs = read_huffman_symbol(ac_table, &bits);
154 unsigned r = rs >> 4;
155 unsigned s = rs & 0xf;
162 /* 16 zero coefficients */
168 possibly_refill(&bits, s);
169 coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
172 uint8_t pixdata[DCTSIZE2];
173 idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
179 for (int y = 0; y < DCTSIZE; ++y) {
180 memcpy(y_row_data + (local_yb * DCTSIZE + y) * y_stride + (xb + local_xb) * DCTSIZE,
181 pixdata + y * DCTSIZE,
191 xb += image->hsample[cn];
192 if (xb * DCTSIZE == y_stride) {
193 fwrite(y_row_data, y_stride * DCTSIZE * image->vsample[cn], 1, stdout);
199 fprintf(stderr, "Error: %u unused bytes at end of SOS segment\n", len);
203 void skip_segment(struct byte_source* source)
207 ssize_t ret = byte_source_input_func(source, buf, 4096);
209 fprintf(stderr, "Input error!\n");
220 struct jpeg_image jpeg;
221 memset(&jpeg, 0, sizeof(jpeg));
224 struct byte_source source;
225 init_byte_source(&source, stdio_read, stdin);
227 huffman_tables_t tables;
230 uint8_t m2 = byte_source_read_marker(&source);
233 fprintf(stderr, "Marker 0x%02x, at position %ld\n", m2, ftell(stdin) - source.bytes_available);
252 /* APP0 through APPF */
254 /* some EXIF stuff */
259 skip_segment(&source);
263 read_dqt(&source, &jpeg);
266 /* SOF0 (baseline DCT, Huffman encoded) */
267 read_sof(&source, &jpeg);
273 /* DHT (define Huffman tables) */
274 read_huffman_tables(&tables, byte_source_input_func, &source);
277 /* SOS (start of scan) */
278 read_scan(&source, &jpeg, &tables);
281 fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);