use std::io::{Error, ErrorKind, Result, Write};
use crate::common::{hash, Channel, Colorspace, Rgb, Rgba};
#[derive(Clone, Debug)]
pub struct QoiWriter<W: Write>
{
writer: W,
width: u32,
height: u32,
channels: Channel,
colorspace: Colorspace,
previous_color: Rgba,
index: [Rgba; 64],
pixels_seen: u64,
current_run_length: u8,
finished: bool,
}
impl<W: Write> Drop for QoiWriter<W>
{
fn drop(&mut self)
{
drop(self.finish());
drop(self.flush());
}
}
impl<W: Write> QoiWriter<W>
{
pub fn new(
mut writer: W,
width: u32,
height: u32,
channels: Channel,
colorspace: Colorspace,
) -> Result<Self>
{
let mut header = Vec::with_capacity(14);
header.extend([b'q', b'o', b'i', b'f']);
header.extend(width.to_be_bytes());
header.extend(height.to_be_bytes());
match channels
{
Channel::Rgb => header.push(3),
Channel::Rgba => header.push(4),
}
match colorspace
{
Colorspace::Srgb => header.push(0),
Colorspace::Linear => header.push(1),
}
writer.write_all(&header)?;
Ok(Self {
writer,
width,
height,
channels,
colorspace,
previous_color: Rgba::new(0, 0, 0, 255),
index: [Rgba::new(0, 0, 0, 0); 64],
pixels_seen: 0,
current_run_length: 0,
finished: false,
})
}
pub fn write_rgba(&mut self, color: Rgba) -> Result<()>
{
self.write_inner(color)?;
if self.width as u64 * self.height as u64 == self.pixels_seen
{
self.finish()?;
}
self.previous_color = color;
self.pixels_seen += 1;
self.index[hash(color)] = color;
Ok(())
}
pub fn write_rgb(&mut self, color: Rgb) -> Result<()>
{
let Rgb { r, g, b } = color;
let a = self.previous_color.a;
self.write_rgba(Rgba { r, g, b, a })
}
pub fn flush(&mut self) -> Result<()>
{
self.write_run_length()?;
self.writer.flush()
}
pub fn close(mut self) -> Result<()>
{
self.finish()?;
self.flush()
}
#[must_use]
pub const fn dimensions(&self) -> (u32, u32)
{
(self.width, self.height)
}
#[must_use]
pub const fn width(&self) -> u32
{
self.width
}
#[must_use]
pub const fn height(&self) -> u32
{
self.height
}
#[must_use]
pub const fn channels(&self) -> Channel
{
self.channels
}
#[allow(clippy::doc_markdown)]
#[must_use]
pub const fn colorspace(&self) -> Colorspace
{
self.colorspace
}
fn finish(&mut self) -> Result<()>
{
if self.finished
{
Ok(())
}
else
{
self.finished = true;
self.writer.write_all(&[0, 0, 0, 0, 0, 0, 0, 1])
}
}
#[allow(clippy::similar_names)]
fn write_inner(&mut self, color: Rgba) -> Result<()>
{
if self.width as u64 * self.height as u64 <= self.pixels_seen
{
return Err(Error::new(ErrorKind::Other, "all pixels already written"));
}
if color.a == self.previous_color.a
{
if color == self.previous_color
{
self.current_run_length += 1;
if self.current_run_length == 62
{
self.write_run_length()?;
}
return Ok(());
}
if let Some(index) = self.index.iter().position(|&x| x == color)
{
return self.writer.write_all(&[index as u8]);
}
let dg = color.g.wrapping_sub(self.previous_color.g).wrapping_add(2);
let dr = color.r.wrapping_sub(self.previous_color.r).wrapping_add(2);
let db = color.b.wrapping_sub(self.previous_color.b).wrapping_add(2);
if dr < 4 && dg < 4 && db < 4
{
return self.writer.write_all(&[64 + dr * 16 + dg * 4 + db]);
}
let dr_dg = dr.wrapping_sub(dg).wrapping_add(8);
let db_dg = db.wrapping_sub(dg).wrapping_add(8);
let dg = dg.wrapping_add(30);
if dg < 64 && dr_dg < 16 && db_dg < 16
{
return self.writer.write_all(&[128 + dg, dr_dg * 16 + db_dg]);
}
self.writer.write_all(&[0xfe, color.r, color.g, color.b])
}
else
{
self.writer
.write_all(&[0xff, color.r, color.g, color.b, color.a])
}
}
fn write_run_length(&mut self) -> Result<()>
{
if self.current_run_length == 0
{
return Ok(());
}
let val = 3 * 64 + self.current_run_length - 1;
self.writer.write_all(&[val])
}
}