From 278cb4533e1526e182d28e9def7c247c2c93463c Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Sat, 21 Nov 2020 22:13:23 -0600 Subject: [PATCH] quantization tables --- src/pixie/fileformats/jpg.nim | 58 ++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/src/pixie/fileformats/jpg.nim b/src/pixie/fileformats/jpg.nim index 035780d..7d83c5e 100644 --- a/src/pixie/fileformats/jpg.nim +++ b/src/pixie/fileformats/jpg.nim @@ -12,6 +12,7 @@ type Jpg = object width, height: int components: array[3, Component] + quantizationTables: array[4, array[64, uint8]] template failInvalid() = raise newException(PixieError, "Invalid JPG buffer, unable to load") @@ -71,10 +72,59 @@ proc decodeSOF(jpg: var Jpg, data: seq[uint8], pos: var int) = pos += 3 proc decodeDHT(data: seq[uint8], pos: var int) = - skipSegment(data, pos) + # skipSegment(data, pos) + # debugEcho pos + let + segmentLen = readSegmentLen(data, pos) + stop = pos + segmentLen + pos += 2 -proc decodeDQT(data: seq[uint8], pos: var int) = - skipSegment(data, pos) + while stop - pos >= 17: + let info = data[pos] + if (info and 0b11100000) != 0: + failInvalid() + + var counts: array[17, int] + for codeLen in 1 .. 16: + counts[codeLen] = data[pos + codeLen].int + + debugEcho counts + + pos += 17 + + for codeLen in 1 .. 16: + discard + + break + + pos = stop + +proc decodeDQT(jpg: var Jpg, data: seq[uint8], pos: var int) = + let + segmentLen = readSegmentLen(data, pos) + stop = pos + segmentLen + pos += 2 + + while stop - pos >= 65: + let + info = data[pos] + qt = info and 0b00001111 + precision = info and 0b11110000 + + if qt > 3: + failInvalid() + + if precision != 0: + raise newException( + PixieError, "Unsuppored JPG qantization table precision" + ) + + inc pos + + for i in 0 ..< 64: + jpg.quantizationTables[qt][i] = data[pos + i] + + pos += 64 proc decodeSOS(data: seq[uint8], pos: var int) = let segmentLen = readSegmentLen(data, pos) @@ -144,7 +194,7 @@ proc decodeJpg*(data: seq[uint8]): Image = of 0xC4: # Define Huffman Tables decodeDHT(data, pos) of 0xDB: # Define Quantanization Table(s) - decodeDQT(data, pos) + jpg.decodeDQT(data, pos) # of 0xDD: # Define Restart Interval of 0xDA: # Start of Scan decodeSOS(data, pos)