Most of the decoders, but the match is broken

I was going to try to match over the op codes, but that isn't possible.

Most of the decoders are done, too, but I'm quite sure that the delta
operations will encounter a wrap around and then panic. The OP_RUN is a
problem, as well. I think I'm going to cheat this problem by holding a
counter. This way I can see if I'm in a run, and then emit another pixel
copy until counter == 0.
This commit is contained in:
2024-05-06 14:18:45 -05:00
parent 4a31b1e20a
commit ef4b31dab6

View File

@@ -92,7 +92,55 @@ impl<'input, I> Iterator for Decoder<I> where I: Iterator<Item=u8> {
fn next(&mut self) -> Option<Self::Item> {
let byte = self.bytes.next()?;
todo!();
match byte {
QOI_OP_RGB => {
return Some(PixelRGBA {
r: self.bytes.next()?,
g: self.bytes.next()?,
b: self.bytes.next()?,
a: self.prev_pixel.a,
});
},
QOI_OP_RGBA => {
return Some(PixelRGBA {
r: self.bytes.next()?,
g: self.bytes.next()?,
b: self.bytes.next()?,
a: self.bytes.next()?,
});
},
QOI_OP_INDEX => {
let idx = (byte | 0b0011_1111) as usize;
return Some(self.back_buffer[idx]);
},
QOI_OP_DIFF => {
let dr = ((byte | 0b0011_0000) >> 4) - 2;
let dg = ((byte | 0b0000_1100) >> 2) - 2;
let db = (byte | 0b0000_0011) - 2;
return Some(PixelRGBA {
r: self.prev_pixel.r + dr,
g: self.prev_pixel.g + dg,
b: self.prev_pixel.b + db,
a: self.prev_pixel.a
});
},
QOI_OP_LUMA => {
let dg = (byte | 0b0011_1111) - 32;
let packed = self.bytes.next()?;
let drdg = ((packed | 0b1111_0000) >> 4) - 8;
let dbdg = (packed | 0b0000_1111) - 8;
let dr = drdg + dg;
let db = dbdg + dg;
return Some(PixelRGBA {
r: self.prev_pixel.r + dr,
g: self.prev_pixel.g + dg,
b: self.prev_pixel.b + db,
a: self.prev_pixel.a
});
},
QOI_OP_RUN => {todo!("Impl Decode QOI_OP_RUN")},
_ => {panic!("bad op code")}
}
}
}