Let the bit source alert about EOF as soon as we have started eating into padding...
authorsgunderson@bigfoot.com <>
Sun, 31 May 2009 18:15:06 +0000 (20:15 +0200)
committersgunderson@bigfoot.com <>
Sun, 31 May 2009 18:15:06 +0000 (20:15 +0200)
bitsource.c
bitsource.h
driver.c

index 7a4ea4a0ca3c553e276be095932061313e833441..b1593aa6ec554959b4bba6bbe3aceb75d4e04863 100644 (file)
@@ -45,17 +45,20 @@ void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits)
                        fprintf(stderr, "Input function returned error\n");
                        exit(1);
                }
-               if (bytes_read == 0 && source->padding_bytes_available > 0) {
-                       unsigned padding_to_add = source->padding_bytes_available;
-                       if (padding_to_add > bytes_to_read) {
-                               padding_to_add = bytes_to_read;
+               if (bytes_read == 0) {
+                       source->source_eof = true;
+                       if (source->padding_bytes_available > 0) {
+                               unsigned padding_to_add = source->padding_bytes_available;
+                               if (padding_to_add > bytes_to_read) {
+                                       padding_to_add = bytes_to_read;
+                               }
+                               memset(source->bytes + source->bytes_available, 0, padding_to_add);
+                               source->padding_bytes_available -= padding_to_add;
+                               source->bytes_available += padding_to_add;
+                       } else {
+                               fprintf(stderr, "Premature EOF\n");
+                               exit(1);
                        }
-                       memset(source->bytes + source->bytes_available, 0, padding_to_add);
-                       source->padding_bytes_available -= padding_to_add;
-                       source->bytes_available += padding_to_add;
-               } else if (bytes_read == 0) {
-                       fprintf(stderr, "Premature EOF\n");
-                       exit(1);
                } else {
                        source->bytes_available += bytes_read;
                }
index ae29b4c4a2200abce210242a541be2c289ea8163..60744b32e12bdc7809f8b0d7cf47d3271a8b8295 100644 (file)
@@ -2,6 +2,7 @@
 #define _BITSOURCE_H 1
 
 #include <assert.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/types.h>
@@ -45,6 +46,7 @@ struct bit_source {
        // Data source.
        input_func_t* input_func;
        void* userdata;
+       bool source_eof;
 };
 
 void init_bit_source(struct bit_source* source, input_func_t* input_func,
index aa803d6ed00aa67266495b65df93e6582bc89151..35934da1d72d711efafdb588b62dae22cdcd8e54 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -151,7 +151,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                
        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;