- i += r;
- zz[unzigzag[i]] = extend(read_bits(&bits, s), s);
- }
-
- for (unsigned i = 0; i < 63; ++i) {
- putchar(zz[i]);
- //printf("%d ", zz[i]);
+ // decode DC component
+ unsigned dc_category = read_huffman_symbol(dc_table, &bits);
+ possibly_refill(&bits, dc_category + DEHUF_TABLE_BITS);
+ last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
+
+ int16_t coeff[DCTSIZE2] = { 0 };
+ coeff[0] = last_dc[c];
+
+ // decode AC components
+ for (unsigned i = 1; i < DCTSIZE2; ++i) {
+ unsigned rs = read_huffman_symbol_no_refill(ac_table, &bits);
+ unsigned r = rs >> 4;
+ unsigned s = rs & 0xf;
+ i += r;
+ possibly_refill(&bits, s + DEHUF_TABLE_BITS);
+
+ if (rs == 0x00) {
+ /* end of block */
+ break;
+ }
+ if (rs == 0xf0) {
+ /* 16 zero coefficients */
+ continue;
+ }
+
+ coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
+ }
+
+ uint8_t pixdata[DCTSIZE2];
+ idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
+
+ uint8_t* dest_pixdata = pixel_write_pointer;
+ for (unsigned y = 0; y < DCTSIZE; ++y, dest_pixdata += image->stride[cn]) {
+ memcpy(dest_pixdata, pixdata + y * DCTSIZE, DCTSIZE);
+ }