]> git.sesse.net Git - fjl/blobdiff - driver.c
More debug code moving.
[fjl] / driver.c
index d5bec84f8db5101aa33d6010c2de831e8f85339a..94cce90332e7df8186c83b0aa311ef3afec61d6f 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -5,24 +5,11 @@
 #include "bytesource.h"
 #include "choice.h"
 #include "dehuff.h"
+#include "driver.h"
 #include "idct.h"
 #include "input.h"
 #include "zigzag.h"
 
-struct jpeg_image {
-       unsigned precision;
-       unsigned width, height;
-       unsigned num_components;
-       unsigned hsample[256], vsample[256], qtable[256];
-       unsigned max_hsample, max_vsample;
-       unsigned stride[256];
-       unsigned num_blocks_horizontal, num_blocks_vertical;
-       uint32_t qvalues[256][DCTSIZE2];
-       void* idct_data[256];
-       uint8_t* pixel_data[256];
-       uint8_t* pixel_write_pointer[256];
-};
-
 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count) 
 {
        return fread(buf, 1, count, (FILE*)userdata);
@@ -114,6 +101,40 @@ void read_sof(struct byte_source* source, struct jpeg_image* image)
        }
 }
 
+void decode_ac_coefficients(const struct huffman_table* tbl, struct bit_source* bits, int16_t* coeff)
+{
+       possibly_refill(bits, DEHUF_AC_TABLE_BITS);
+       for (unsigned i = 0; i < DCTSIZE2 - 1; ) {
+               unsigned lookup = peek_bits(bits, DEHUF_AC_TABLE_BITS);
+               int code = tbl->ac_table_codes[lookup];
+               unsigned length = tbl->ac_table_length[lookup];
+               unsigned r = tbl->ac_table_skip[lookup];
+
+               if (code == AC_DEHUF_SLOW_PATH) {
+                       unsigned rs = read_huffman_symbol_no_refill(tbl, bits);
+                       if (rs == 0x00) {
+                               /* end of block */
+                               break;
+                       }
+
+                       unsigned r = rs >> 4;
+                       unsigned s = rs & 0xf;
+                       i += r + 1;
+                       possibly_refill(bits, s);
+                       coeff[unzigzag[i]] = extend(read_bits(bits, s), s);
+                       possibly_refill(bits, DEHUF_AC_TABLE_BITS);
+               } else {
+                       assert(bits->bits_available >= length);
+                       read_bits(bits, length);
+                       possibly_refill(bits, DEHUF_AC_TABLE_BITS);
+
+                       assert(r >= 1);
+                       i += r;
+                       coeff[unzigzag[i]] = code;
+               }
+       }
+}
+
 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
 {
        unsigned len = read_uint16(byte_source_input_func, source);
@@ -169,33 +190,13 @@ 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 + DEHUF_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_coefficients(ac_table, &bits, coeff);
 
-                                       // 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;
-
-                                               if (rs == 0x00) {
-                                                       /* end of block */
-                                                       break;
-                                               }
-                                               if (rs == 0xf0) {
-                                                       /* 16 zero coefficients */
-                                                       possibly_refill(&bits, DEHUF_TABLE_BITS);
-                                                       continue;
-                                               }
-
-                                               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);
 
@@ -216,14 +217,8 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                                unsigned cn = component_num[c];
                                image->pixel_write_pointer[cn] += (image->vsample[cn] * DCTSIZE - 1) * image->stride[cn];
                        }
-
-                       // Some debug code.
-                       const int c = 1;
                        if (mcu_y == image->num_blocks_vertical) {
-                               unsigned stride = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
-                               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);
+                               return;
                        }
                }
        }
@@ -247,14 +242,12 @@ void skip_segment(struct byte_source* source)
        }
 }
        
-int main(void)
+void read_jpeg(struct jpeg_image* jpeg, FILE *input)
 {
-       struct jpeg_image jpeg;
-       memset(&jpeg, 0, sizeof(jpeg));
-       init_choices();
-
+       memset(jpeg, 0, sizeof(*jpeg));
+       
        struct byte_source source;
-       init_byte_source(&source, stdio_read, stdin);
+       init_byte_source(&source, stdio_read, input);
 
        huffman_tables_t tables;
 
@@ -292,25 +285,25 @@ int main(void)
                        break;
                case 0xdb:
                        /* DQT */
-                       read_dqt(&source, &jpeg);
+                       read_dqt(&source, jpeg);
                        break;
                case 0xc0:
                        /* SOF0 (baseline DCT, Huffman encoded) */
-                       read_sof(&source, &jpeg);
+                       read_sof(&source, jpeg);
                        break;
                case 0xd8:
                        /* SOI */
                        break;
                case 0xd9:
                        /* EOI */
-                       exit(0);
+                       return;
                case 0xc4:
                        /* DHT (define Huffman tables) */
                        read_huffman_tables(&tables, byte_source_input_func, &source);
                        break;
                case 0xda:
                        /* SOS (start of scan) */
-                       read_scan(&source, &jpeg, &tables);
+                       read_scan(&source, jpeg, &tables);
                        break;
                default:
                        fprintf(stderr, "Error: Unknown marker 0x%02x\n", m2);
@@ -318,3 +311,18 @@ int main(void)
                }
        }
 }
+       
+int main(void)
+{
+       init_choices();
+
+       struct jpeg_image jpeg;
+       read_jpeg(&jpeg, stdin);
+
+       // Some debug code.
+       const int c = 1;
+       unsigned stride = jpeg.num_blocks_horizontal * jpeg.hsample[c] * DCTSIZE;
+       unsigned height = jpeg.num_blocks_vertical * jpeg.vsample[c] * DCTSIZE;
+       printf("P5\n%u %u\n255\n", stride, height);
+       fwrite(jpeg.pixel_data[c], stride * height, 1, stdout);
+}