]> git.sesse.net Git - fjl/blobdiff - driver.c
Save one refill from inner coefficient-reading loop.
[fjl] / driver.c
index fd861f25d7b56250aadf7f1666174e9fb2fe9b41..6db737ff7d585033109f1ba58253c71e500cd83d 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -164,7 +164,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
 
                                        // decode DC component
                                        unsigned dc_category = read_huffman_symbol(dc_table, &bits);
-                                       possibly_refill(&bits, dc_category);
+                                       possibly_refill(&bits, dc_category + DEHUF_TABLE_BITS);
                                        last_dc[c] += extend(read_bits(&bits, dc_category), dc_category);
                                        
                                        int16_t coeff[DCTSIZE2] = { 0 };
@@ -172,7 +172,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
 
                                        // decode AC components
                                        for (unsigned i = 1; i < DCTSIZE2; ++i) {
-                                               unsigned rs = read_huffman_symbol(ac_table, &bits);
+                                               unsigned rs = read_huffman_symbol_no_refill(ac_table, &bits);
                                                unsigned r = rs >> 4;
                                                unsigned s = rs & 0xf;
 
@@ -182,25 +182,24 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                                                }
                                                if (rs == 0xf0) {
                                                        /* 16 zero coefficients */
+                                                       possibly_refill(&bits, DEHUF_TABLE_BITS);
                                                        i += 15;
                                                        continue;
                                                }
                                                i += r;
 
-                                               possibly_refill(&bits, s);
+                                               possibly_refill(&bits, s + DEHUF_TABLE_BITS);
                                                coeff[unzigzag[i]] = extend(read_bits(&bits, s), s);
                                        }
                        
                                        uint8_t pixdata[DCTSIZE2];      
                                        idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
 
-                                       for (unsigned y = 0; y < DCTSIZE; ++y) {
-                                               unsigned real_x = (mcu_x * image->hsample[cn] + local_xb) * DCTSIZE;
-                                               unsigned real_y = (mcu_y * image->vsample[cn] + local_yb) * DCTSIZE + y;
-                                       
-                                               memcpy(image->pixel_data[cn] + real_y * stride + real_x,
-                                                      pixdata + y * DCTSIZE,
-                                                      DCTSIZE);
+                                       unsigned real_x = (mcu_x * image->hsample[cn] + local_xb) * DCTSIZE;
+                                       unsigned real_y = (mcu_y * image->vsample[cn] + local_yb) * DCTSIZE;
+                                       uint8_t* dest_pixdata = image->pixel_data[cn] + real_y * stride + real_x;
+                                       for (unsigned y = 0; y < DCTSIZE; ++y, dest_pixdata += stride) {
+                                               memcpy(dest_pixdata, pixdata + y * DCTSIZE, DCTSIZE);
                                        }
                                }
                        }
@@ -214,8 +213,9 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                        const int c = 1;
                        if (mcu_y == image->num_blocks_vertical) {
                                unsigned stride = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
-                               printf("P5\n%u %u\n255\n", stride, image->height);
-                               fwrite(image->pixel_data[c], stride * image->height, 1, stdout);
+                               unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE;
+                               printf("P5\n%u %u\n255\n", stride, height);
+                               fwrite(image->pixel_data[c], stride * height, 1, stdout);
                        }
                }
        }