]> git.sesse.net Git - fjl/blobdiff - driver.c
Correct PGM height for vertically sampled components.
[fjl] / driver.c
index 06d94d99d553e6aa4e784e22cfc99a4d19216df5..b12b226c336dcc29ea9993f521fbd8f393ec8f79 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -147,11 +147,11 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
        }
 
        struct bit_source bits;
-       init_bit_source(&bits, byte_source_input_func, source);
+       init_bit_source(&bits, byte_source_input_func, 8, source);
                
        unsigned mcu_x = 0, mcu_y = 0;
 
-       for ( ;; ) {
+       while (!bits.source_eof) {
                for (unsigned c = 0; c < num_components; ++c) {
                        unsigned cn = component_num[c];
                        unsigned stride = image->num_blocks_horizontal * image->hsample[cn] * DCTSIZE;
@@ -214,8 +214,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);
                        }
                }
        }
@@ -293,6 +294,9 @@ int main(void)
                case 0xd8:
                        /* SOI */
                        break;
+               case 0xd9:
+                       /* EOI */
+                       exit(0);
                case 0xc4:
                        /* DHT (define Huffman tables) */
                        read_huffman_tables(&tables, byte_source_input_func, &source);