]> git.sesse.net Git - fjl/blobdiff - driver.c
In the driver, store the image data and output one of the components as a PGM file.
[fjl] / driver.c
index 2e5bda67f475f7e2fe58f2f899c1f2c7e2a17769..06d94d99d553e6aa4e784e22cfc99a4d19216df5 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -14,8 +14,11 @@ struct jpeg_image {
        unsigned width, height;
        unsigned num_components;
        unsigned hsample[256], vsample[256], qtable[256];
+       unsigned max_hsample, max_vsample;
+       unsigned num_blocks_horizontal, num_blocks_vertical;
        uint32_t qvalues[256][DCTSIZE2];
        void* idct_data[256];
+       uint8_t* pixel_data[256];
 };
 
 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count) 
@@ -79,9 +82,32 @@ void read_sof(struct byte_source* source, struct jpeg_image* image)
                image->qtable[c] = read_uint8(byte_source_input_func, source);
                len -= 3;
 
+               if (image->hsample[c] > image->max_hsample) {
+                       image->max_hsample = image->hsample[c];
+               }
+               if (image->vsample[c] > image->max_vsample) {
+                       image->max_vsample = image->vsample[c];
+               }
+
                fprintf(stderr, "Component %u: sampling factors %u x %x, quantization table %u\n",
                        c, image->hsample[c], image->vsample[c], image->qtable[c]);
        }
+       
+       image->num_blocks_horizontal = (image->width + image->max_hsample * DCTSIZE - 1) / (image->max_hsample * DCTSIZE);
+       image->num_blocks_vertical = (image->height + image->max_vsample * DCTSIZE - 1) / (image->max_vsample * DCTSIZE);
+
+       for (unsigned c = 0; c < 256; ++c) {
+               if (image->hsample[c] == 0) {
+                       continue;
+               }
+
+               unsigned width = image->num_blocks_horizontal * image->hsample[c] * DCTSIZE;
+               unsigned height = image->num_blocks_vertical * image->vsample[c] * DCTSIZE;
+               image->pixel_data[c] = (uint8_t*)malloc(width * height);
+               assert(image->pixel_data[c] != NULL);
+
+               fprintf(stderr, "Component %u: allocating %d x %d\n", c, width, height);
+       }
 }
 
 void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tables_t* tables)
@@ -123,16 +149,12 @@ 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);
                
-       // Some debugging code.
-       assert(image->width % 8 == 0);
-       int y_stride = image->width;
-       uint8_t* y_row_data = (uint8_t*)malloc(y_stride * DCTSIZE * image->vsample[1]);
-       assert(y_row_data != NULL);
-       int xb = 0;
+       unsigned mcu_x = 0, mcu_y = 0;
 
        for ( ;; ) {
                for (unsigned c = 0; c < num_components; ++c) {
                        unsigned cn = component_num[c];
+                       unsigned stride = image->num_blocks_horizontal * image->hsample[cn] * DCTSIZE;
                        assert(image->idct_data[image->qtable[cn]] != NULL);
                        
                        for (unsigned local_yb = 0; local_yb < image->vsample[cn]; ++local_yb) {
@@ -172,26 +194,28 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                                        uint8_t pixdata[DCTSIZE2];      
                                        idct_choice(coeff, image->idct_data[image->qtable[cn]], pixdata);
 
-                                       if (cn != 1) {
-                                               continue;
-                                       }
-
-                                       for (int y = 0; y < DCTSIZE; ++y) {
-                                               memcpy(y_row_data + (local_yb * DCTSIZE + y) * y_stride + (xb + local_xb) * DCTSIZE,
+                                       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);
                                        }
                                }
                        }
-
-                       if (cn != 1) {
-                               continue;
-                       }
-
-                       xb += image->hsample[cn];
-                       if (xb * DCTSIZE == y_stride) {
-                               fwrite(y_row_data, y_stride * DCTSIZE * image->vsample[cn], 1, stdout);
-                               xb = 0;
+               }
+               
+               if (++mcu_x == image->num_blocks_horizontal) {
+                       ++mcu_y;
+                       mcu_x = 0;
+
+                       // Some debug code.
+                       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);
                        }
                }
        }