Compare commits

...

10 Commits

Author SHA1 Message Date
YK
64c4499569 docs 2024-07-11 18:57:33 +03:00
YK
c63d2bdcba refactor: remove nop from ops list 2024-07-11 18:55:44 +03:00
YK
1e405bff30 docs 2024-07-11 17:25:04 +03:00
YK
5b2d912cf8 refactor: remove vec alloc in format function, extending out vec instead 2024-07-11 16:43:02 +03:00
YK
8e79a7954a remove debug code 2024-07-11 16:32:58 +03:00
YK
f86d040c8b fix the rest of encoder bugs (trying to calculate luma/diff when alpha channels of cur and prev aren't equal) 2024-07-11 16:32:27 +03:00
YK
99c4262155 more correct encoder, big pngs don't work yet 2024-07-10 18:47:03 +03:00
YK
5c12875e81 encoder impl (not entirely correct yet) 2024-07-10 17:47:33 +03:00
YK
5b6c75de6a make diff/luma calc prettier 2024-07-05 16:54:06 +03:00
YK
470273d387 tests 2024-07-05 16:32:16 +03:00
13 changed files with 258 additions and 16 deletions

1
Cargo.lock generated
View File

@ -744,6 +744,7 @@ dependencies = [
name = "qoi2"
version = "0.1.0"
dependencies = [
"anyhow",
"bytemuck",
"imageproc",
"itertools 0.13.0",

View File

@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.86"
bytemuck = "1.16.1"
imageproc = "0.25.0"
itertools = "0.13.0"

4
README.md Normal file
View File

@ -0,0 +1,4 @@
@TODO replace Options with custom Errors
@TODO encoder wrappers for RgbaImage/RgbImage
@TODO write tests for the entire image set
@TODO write qoi-x-qoi and x-qoi-x tests to ensure correctness

BIN
in/1kb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
in/1kb.qoi Normal file

Binary file not shown.

BIN
out/1kb.qoi Normal file

Binary file not shown.

BIN
out/dice.qoi Normal file

Binary file not shown.

BIN
out/dice_generic.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 KiB

BIN
out/kodim23.qoi Normal file

Binary file not shown.

View File

@ -14,15 +14,103 @@ pub enum Colorspace {
Linear
}
impl Colorspace {
pub fn from_repr (i: u8) -> Option<Self> {
match i {
0 => Some(Self::Srgb),
1 => Some(Self::Linear),
_ => None
}
}
pub fn to_repr (&self) -> u8 {
match self {
Self::Srgb => 0,
Self::Linear => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Channels {
RGB,
RGBA
}
impl Channels {
pub fn num (&self) -> usize {
match self {
Self::RGBA => 4,
Self::RGB => 3,
}
}
pub fn ops_color (&self, bytes: &[u8]) -> Option<Ops> {
match self {
Channels::RGB => Some(Ops::Rgb(bytes.try_into().ok()?)),
Channels::RGBA => Some(Ops::Rgba(bytes.try_into().ok()?)),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Ops {
Index (usize),
Run (u8),
Luma ([i8; 3]),
Diff ([i8; 3]),
Rgb (RgbColor),
Rgba (RgbaColor),
}
type RgbColor = [u8; 3];
type RgbaColor = [u8; 4];
pub const MAGIC: [u8; 4] = [b'q', b'o', b'i', b'f'];
pub fn hash ([r, g, b, a]: [u8; 4]) -> usize {
(r as usize * 3 + g as usize * 5 + b as usize * 7 + a as usize * 11) % 64
}
pub fn calc_diff ([r0, g0, b0, _]: [i16; 4], [r1, g1, b1, _]: [i16; 4]) -> [i16; 3] {
[diff(r0, r1), diff(g0, g1), diff(b0, b1)]
}
pub fn diff_ops (left: [u8; 4], right: [u8; 4]) -> Option<Ops> {
if left[3] != right[3] { return None; }
let left: [i16; 4] = left.map(i16::from);
let right: [i16; 4] = right.map(i16::from);
let diff = calc_diff(left, right);
if diff.iter().all(|d| (-2..2).contains(d)) {
Some(Ops::Diff(diff.map(|e| e as i8)))
} else if let Some(luma) = new_luma(diff) {
Some(luma)
} else { None }
}
pub fn new_luma ([r, g, b]: [i16; 3]) -> Option<Ops> {
let drdg = r - g;
let dbdg = b - g;
if (-32..32).contains(&g) && (-8..8).contains(&drdg) && (-8..8).contains(&dbdg) {
return Some(Ops::Luma([drdg as i8, g as i8, dbdg as i8]));
}
return None;
}
fn diff (lhs: i16, rhs: i16) -> i16 {
let mut diff = lhs - rhs;
if diff.abs() > 128 {
diff = 256 - rhs + lhs + 1 * match diff.is_negative() {
true => 1,
false => -1
};
}
diff
}
pub fn cast_with_bias (num: i8, bias: i8) -> u8 {
(num + bias) as u8
}

View File

@ -11,19 +11,11 @@ impl Header {
let height = u32::from_be_bytes(header[8..12].try_into().ok()?);
let channels = Self::channels(u8::from_be_bytes(header[12..13].try_into().ok()?))?;
let colorspace = Self::colorspace(u8::from_be_bytes(header[13..14].try_into().ok()?))?;
let colorspace = Colorspace::from_repr(u8::from_be_bytes(header[13..14].try_into().ok()?))?;
Some(Self { width, height, channels, colorspace })
}
fn colorspace (space: u8) -> Option<Colorspace> {
match space {
0 => Some(Colorspace::Srgb),
1 => Some(Colorspace::Linear),
_ => None
}
}
fn channels (ch: u8) -> Option<Channels> {
match ch {
3 => Some(Channels::RGB),
@ -65,17 +57,16 @@ impl Decoder for Vec<[u8; 4]> {
(known[q as usize & 63], 1)
},
q if q >> 6 == 1 => { // QOI_OP_DIFF
let dr = ((q & 48) >> 4) as i8 - 2;
let dg = ((q & 12) >> 2) as i8 - 2;
let db = (q & 3) as i8 - 2;
([ prev[0].wrapping_add_signed(dr), prev[1].wrapping_add_signed(dg), prev[2].wrapping_add_signed(db), prev[3]], 1)
let mut diff = [((q & 48) >> 4) as i8 - 2, ((q & 12) >> 2) as i8 - 2, (q & 3) as i8 - 2, 0].into_iter();
(prev.map(|p| p.wrapping_add_signed(diff.next().unwrap())), 1)
},
q if q >> 6 == 2 => { // QOI_OP_LUMA
let extra = *iter.next()?;
let dg = (q & 63) as i8 - 32;
let dr = ((extra & 240) >> 4) as i8 - 8;
let db = (extra & 15) as i8 - 8;
([ prev[0].wrapping_add_signed(dr + dg), prev[1].wrapping_add_signed(dg), prev[2].wrapping_add_signed(db + dg), prev[3]], 1)
let dr = ((extra & 240) >> 4) as i8 - 8 + dg;
let db = (extra & 15) as i8 - 8 + dg;
let mut diff = [dr, dg, db, 0].into_iter();
(prev.map(|p| p.wrapping_add_signed(diff.next().unwrap())), 1)
},
q if q >> 6 == 3 => { // QOI_OP_RUN
let len = ((q & 63) + 1) as usize;
@ -199,4 +190,12 @@ mod test {
rgb.save_with_format("out/edgecase.jpg", imageproc::image::ImageFormat::Jpeg).unwrap();
}
#[test]
fn dice_generic() {
let file = std::fs::read("in/dice.qoi").unwrap();
let rgba: RgbaImage = decode(&file).unwrap();
rgba.save("out/dice_generic.png").unwrap();
}
}

View File

@ -0,0 +1,147 @@
use std::mem::discriminant;
use itertools::Itertools;
use crate::common::*;
impl Ops {
fn write_encoded (self, buf: &mut Vec<u8>) {
// cast_diff
let cd = |diff: i8| cast_with_bias(diff, 2);
// cast_luma_green
let clg = |diff: i8| cast_with_bias(diff, 32);
// cast_luma_red_blue
let clrb = |diff: i8| cast_with_bias(diff, 8);
match self {
Ops::Rgb([r, g, b]) => buf.extend_from_slice(&[254, r, g, b]),
Ops::Rgba([r, g, b, a]) => buf.extend_from_slice(&[255, r, g, b, a]),
Ops::Index(idx) => buf.push(idx as u8),
Ops::Run(len) => buf.push((3 << 6) | (len - 1)),
Ops::Diff([dr, dg, db]) => buf.push((1 << 6) | ((cd(dr) << 4) | (cd(dg) << 2) | cd(db))),
Ops::Luma([dr, dg, db]) => buf.extend_from_slice(&[(2 << 6) | clg(dg), (clrb(dr) << 4) | clrb(db)]),
}
}
}
fn encode_header (header: Header) -> Option<Vec<u8>> {
let mut out = vec![];
out.extend_from_slice(&MAGIC);
out.extend_from_slice(&header.width.to_be_bytes());
out.extend_from_slice(&header.height.to_be_bytes());
out.extend_from_slice(&(header.channels.num() as u8).to_be_bytes());
out.extend_from_slice(&header.colorspace.to_repr().to_be_bytes());
if out.len() != 14 { return None; }
Some(out)
}
fn encode_body (header: Header, data: &[u8]) -> Option<Vec<u8>> {
let mut known = [[0u8; 4]; 64];
let mut last = [0u8, 0, 0, 255];
let mut out = Vec::with_capacity(header.width as usize * header.height as usize);
let mut cur = Ops::Rgba([0, 0, 0, 255]);
for mut chunk in &data.iter().chunks(header.channels.num()) {
let (&r, &g, &b) = chunk.next_tuple()?;
// if we need only 3 channels, we use a placeholder 255 for alpha and ignore it during write
let a = *chunk.next().unwrap_or(&255);
let rgba = [r, g, b, a];
// If current pixel value is the same as of the previous pixel, we need to start/continue a run
if rgba == last {
match cur {
// if run is in bounds 0..62
Ops::Run(run) if run < 62 => cur = Ops::Run(run + 1),
// if run is full, write it, and start a new one
Ops::Run(run) if run >= 62 => {
cur = Ops::Run(62); // clamp for safety, or return None? @Q
cur.write_encoded(&mut out);
cur = Ops::Run(1);
},
// otherwise (default case, when encountering a match for the first time) start a run
_ => cur = Ops::Run(1),
}
} else {
// if current Op is Run (meaning we need to and it since there's no match)
if discriminant(&cur) == discriminant(&Ops::Run(0)) {
cur.write_encoded(&mut out);
}
let hash = hash(rgba);
// INDEX -> DIFF -> LUMA -> RGB(A)
if known[hash] == rgba {
cur = Ops::Index(hash);
} else if let Some(ops) = diff_ops([r, g, b, a], last) {
cur = ops;
} else {
cur = header.channels.ops_color(&rgba[..header.channels.num()])?;
}
cur.write_encoded(&mut out);
last = rgba;
known[hash] = rgba;
}
}
// if an image buffer ends with a run, flush it as well
if discriminant(&cur) == discriminant(&Ops::Run(0)) {
cur.write_encoded(&mut out);
}
Some(out)
}
pub fn encode_full (header: Header, bytes: &[u8]) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(header.width as usize * header.height as usize * 4);
out.extend_from_slice(&encode_header(header)?);
out.extend_from_slice(&encode_body(header, bytes)?);
out.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 1]);
Some(out)
}
#[cfg(test)]
mod test {
use std::time::Instant;
use super::*;
#[test]
fn basic () {
let file = imageproc::image::io::Reader::open("in/dice.png").unwrap().decode().unwrap();
let file = file.into_rgba8().into_raw();
let header = Header { width: 800, height: 600, channels: Channels::RGBA, colorspace: Colorspace::Srgb };
let inst = Instant::now();
let encoded = encode_full(header, &file).unwrap();
println!("dice encoding done in {} ms", inst.elapsed().as_millis());
let _write = std::fs::write("out/dice.qoi", &encoded);
}
#[test]
fn _1kb () {
let file = imageproc::image::io::Reader::open("in/1kb.png").unwrap().decode().unwrap();
let file = file.into_rgba8().into_raw();
let header = Header { width: 17, height: 14, channels: Channels::RGBA, colorspace: Colorspace::Srgb };
let inst = Instant::now();
let encoded = encode_full(header, &file).unwrap();
println!("1kb encoding done in {} ms", inst.elapsed().as_millis());
let _write = std::fs::write("out/1kb.qoi", &encoded);
}
#[test]
fn kodim23 () {
let file = imageproc::image::io::Reader::open("in/kodim23.png").unwrap().decode().unwrap();
let file = file.into_rgb8().into_raw();
let header = Header { width: 768, height: 512, channels: Channels::RGB, colorspace: Colorspace::Srgb };
let inst = Instant::now();
let encoded = encode_full(header, &file).unwrap();
println!("kodim23 encoding done in {} ms", inst.elapsed().as_millis());
let _write = std::fs::write("out/kodim23.qoi", &encoded);
}
}

View File

@ -1,3 +1,5 @@
#![feature(decl_macro)]
pub mod common;
pub mod encoder;