]> git.sesse.net Git - fjl/blobdiff - driver.c
Add DQT parsing.
[fjl] / driver.c
index 08b684a0715162f058a12fe920c7e9c4fb90fd62..bbc569e5d143e1b41c35b460f53f1b1c25721863 100644 (file)
--- a/driver.c
+++ b/driver.c
@@ -5,12 +5,14 @@
 #include "choice.h"
 #include "dehuff.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];
+       uint32_t qvalues[256][DCTSIZE2];
 };
 
 ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count) 
@@ -18,6 +20,33 @@ ssize_t stdio_read(void* userdata, uint8_t* buf, size_t count)
        return fread(buf, 1, count, (FILE*)userdata);
 }
 
+void read_dqt(struct byte_source* source, struct jpeg_image* image)
+{
+       unsigned len = read_uint16(byte_source_input_func, source);
+       assert(len >= 67);
+       uint8_t precision_table = read_uint8(byte_source_input_func, source);
+       int precision = precision_table >> 4;  // 0 = 8 bits, otherwise 16 bits.
+       int table = precision_table & 0x0f;
+
+       if (precision != 0) {
+               assert(len == 131);
+               fprintf(stderr, "Quantization table %u: 16 bits/entry\n", table);
+       } else {
+               assert(len == 67);
+               fprintf(stderr, "Quantization table %u: 8 bits/entry\n", table);
+       }
+       
+       for (unsigned i = 0; i < 64; ++i) {
+               if (precision != 0) {
+                       image->qvalues[table][unzigzag[i]] =
+                               read_uint16(byte_source_input_func, source);
+               } else {
+                       image->qvalues[table][unzigzag[i]] =
+                               read_uint8(byte_source_input_func, source);
+               }       
+       }
+}
+
 void read_sof(struct byte_source* source, struct jpeg_image* image)
 {
        unsigned len = read_uint16(byte_source_input_func, source);
@@ -121,7 +150,7 @@ void read_scan(struct byte_source* source, struct jpeg_image* image, huffman_tab
                                        possibly_refill(&bits, s);
 
                                        i += r;
-                                       zz[i] = extend(read_bits(&bits, s), s);
+                                       zz[unzigzag[i]] = extend(read_bits(&bits, s), s);
                                }
                                
                                for (unsigned i = 0; i < 63; ++i) {
@@ -192,9 +221,11 @@ int main(void)
                        /* comment */
                case 0xff:
                        /* ignore */
+                       skip_segment(&source);
+                       break;
                case 0xdb:
                        /* DQT */
-                       skip_segment(&source);
+                       read_dqt(&source, &jpeg);
                        break;
                case 0xc0:
                        /* SOF0 (baseline DCT, Huffman encoded) */