- unsigned nc = image->vsample[cn] * image->hsample[cn];
- for (unsigned n = 0; n < nc; ++n) {
- const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
- const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
-
- // decode DC component
- unsigned dc_category = read_huffman_symbol(dc_table, &bits);
- possibly_refill(&bits, dc_category);
- last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
-
- // printf("dc=%d ac=", last_dc[c]);
- putchar(last_dc[c]);
-
- // decode AC components
- int zz[63] = { 0 };
- for (unsigned i = 0; i < 63; ++i) {
- unsigned rs = read_huffman_symbol(ac_table, &bits);
- unsigned r = rs >> 4;
- unsigned s = rs & 0xf;
-
- if (rs == 0x00) {
- /* end of block */
- break;
+ assert(image->idct_data[image->qtable[cn]] != NULL);
+
+ for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb) {
+ for (unsigned local_xb = 0; local_xb < image->hsample[cn]; ++local_xb) {
+ const struct huffman_table* dc_table = &((*tables)[DC_CLASS][dc_huffman_table[c]]);
+ const struct huffman_table* ac_table = &((*tables)[AC_CLASS][ac_huffman_table[c]]);
+
+ // decode DC component
+ unsigned dc_category = read_huffman_symbol(dc_table, &bits);
+ possibly_refill(&bits, dc_category);
+ 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(ac_table, &bits);
+ unsigned r = rs >> 4;
+ unsigned s = rs & 0xf;
+
+ if (rs == 0x00) {
+ /* end of block */
+ break;
+ }
+ if (rs == 0xf0) {
+ /* 16 zero coefficients */
+ i += 15;
+ continue;
+ }
+
+ possibly_refill(&bits, s);
+
+ i += r;
+ coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);