use alloc::vec;
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::num::NonZeroUsize;
use tiny_skia_path::{IntSize, ScreenIntRect};
use crate::{Color, IntRect};
use crate::color::PremultipliedColorU8;
#[cfg(feature = "png-format")]
use crate::color::{premultiply_u8, ALPHA_U8_OPAQUE};
pub const BYTES_PER_PIXEL: usize = 4;
#[derive(Clone, PartialEq)]
pub struct Pixmap {
data: Vec<u8>,
size: IntSize,
}
impl Pixmap {
pub fn new(width: u32, height: u32) -> Option<Self> {
let size = IntSize::from_wh(width, height)?;
let data_len = data_len_for_size(size)?;
Some(Pixmap {
data: vec![0; data_len],
size,
})
}
pub fn from_vec(data: Vec<u8>, size: IntSize) -> Option<Self> {
let data_len = data_len_for_size(size)?;
if data.len() != data_len {
return None;
}
Some(Pixmap { data, size })
}
#[cfg(feature = "png-format")]
pub fn decode_png(data: &[u8]) -> Result<Self, png::DecodingError> {
fn make_custom_png_error(msg: &str) -> png::DecodingError {
std::io::Error::new(std::io::ErrorKind::Other, msg).into()
}
let mut decoder = png::Decoder::new(data);
decoder.set_transformations(png::Transformations::normalize_to_color8());
let mut reader = decoder.read_info()?;
let mut img_data = vec![0; reader.output_buffer_size()];
let info = reader.next_frame(&mut img_data)?;
if info.bit_depth != png::BitDepth::Eight {
return Err(make_custom_png_error("unsupported bit depth"));
}
let size = IntSize::from_wh(info.width, info.height)
.ok_or_else(|| make_custom_png_error("invalid image size"))?;
let data_len =
data_len_for_size(size).ok_or_else(|| make_custom_png_error("image is too big"))?;
img_data = match info.color_type {
png::ColorType::Rgb => {
let mut rgba_data = Vec::with_capacity(data_len);
for rgb in img_data.chunks(3) {
rgba_data.push(rgb[0]);
rgba_data.push(rgb[1]);
rgba_data.push(rgb[2]);
rgba_data.push(ALPHA_U8_OPAQUE);
}
rgba_data
}
png::ColorType::Rgba => img_data,
png::ColorType::Grayscale => {
let mut rgba_data = Vec::with_capacity(data_len);
for gray in img_data {
rgba_data.push(gray);
rgba_data.push(gray);
rgba_data.push(gray);
rgba_data.push(ALPHA_U8_OPAQUE);
}
rgba_data
}
png::ColorType::GrayscaleAlpha => {
let mut rgba_data = Vec::with_capacity(data_len);
for slice in img_data.chunks(2) {
let gray = slice[0];
let alpha = slice[1];
rgba_data.push(gray);
rgba_data.push(gray);
rgba_data.push(gray);
rgba_data.push(alpha);
}
rgba_data
}
png::ColorType::Indexed => {
return Err(make_custom_png_error("indexed PNG is not supported"));
}
};
for pixel in img_data.as_mut_slice().chunks_mut(BYTES_PER_PIXEL) {
let a = pixel[3];
pixel[0] = premultiply_u8(pixel[0], a);
pixel[1] = premultiply_u8(pixel[1], a);
pixel[2] = premultiply_u8(pixel[2], a);
}
Pixmap::from_vec(img_data, size)
.ok_or_else(|| make_custom_png_error("failed to create a pixmap"))
}
#[cfg(feature = "png-format")]
pub fn load_png<P: AsRef<std::path::Path>>(path: P) -> Result<Self, png::DecodingError> {
let data = std::fs::read(path)?;
Self::decode_png(&data)
}
#[cfg(feature = "png-format")]
pub fn encode_png(&self) -> Result<Vec<u8>, png::EncodingError> {
self.as_ref().encode_png()
}
#[cfg(feature = "png-format")]
pub fn save_png<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), png::EncodingError> {
self.as_ref().save_png(path)
}
pub fn as_ref(&self) -> PixmapRef {
PixmapRef {
data: &self.data,
size: self.size,
}
}
pub fn as_mut(&mut self) -> PixmapMut {
PixmapMut {
data: &mut self.data,
size: self.size,
}
}
#[inline]
pub fn width(&self) -> u32 {
self.size.width()
}
#[inline]
pub fn height(&self) -> u32 {
self.size.height()
}
#[allow(dead_code)]
pub(crate) fn size(&self) -> IntSize {
self.size
}
pub fn fill(&mut self, color: Color) {
let c = color.premultiply().to_color_u8();
for p in self.as_mut().pixels_mut() {
*p = c;
}
}
pub fn data(&self) -> &[u8] {
self.data.as_slice()
}
pub fn data_mut(&mut self) -> &mut [u8] {
self.data.as_mut_slice()
}
pub fn pixel(&self, x: u32, y: u32) -> Option<PremultipliedColorU8> {
let idx = self.width().checked_mul(y)?.checked_add(x)?;
self.pixels().get(idx as usize).cloned()
}
pub fn pixels_mut(&mut self) -> &mut [PremultipliedColorU8] {
bytemuck::cast_slice_mut(self.data_mut())
}
pub fn pixels(&self) -> &[PremultipliedColorU8] {
bytemuck::cast_slice(self.data())
}
pub fn take(self) -> Vec<u8> {
self.data
}
pub fn clone_rect(&self, rect: IntRect) -> Option<Pixmap> {
self.as_ref().clone_rect(rect)
}
}
impl core::fmt::Debug for Pixmap {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Pixmap")
.field("data", &"...")
.field("width", &self.size.width())
.field("height", &self.size.height())
.finish()
}
}
#[derive(Clone, Copy, PartialEq)]
pub struct PixmapRef<'a> {
data: &'a [u8],
size: IntSize,
}
impl<'a> PixmapRef<'a> {
pub fn from_bytes(data: &'a [u8], width: u32, height: u32) -> Option<Self> {
let size = IntSize::from_wh(width, height)?;
let data_len = data_len_for_size(size)?;
if data.len() < data_len {
return None;
}
Some(PixmapRef { data, size })
}
pub fn to_owned(&self) -> Pixmap {
Pixmap {
data: self.data.to_vec(),
size: self.size,
}
}
#[inline]
pub fn width(&self) -> u32 {
self.size.width()
}
#[inline]
pub fn height(&self) -> u32 {
self.size.height()
}
pub(crate) fn size(&self) -> IntSize {
self.size
}
pub(crate) fn rect(&self) -> ScreenIntRect {
self.size.to_screen_int_rect(0, 0)
}
pub fn data(&self) -> &'a [u8] {
self.data
}
pub fn pixel(&self, x: u32, y: u32) -> Option<PremultipliedColorU8> {
let idx = self.width().checked_mul(y)?.checked_add(x)?;
self.pixels().get(idx as usize).cloned()
}
pub fn pixels(&self) -> &'a [PremultipliedColorU8] {
bytemuck::cast_slice(self.data())
}
pub fn clone_rect(&self, rect: IntRect) -> Option<Pixmap> {
let rect = self.rect().to_int_rect().intersect(&rect)?;
let mut new = Pixmap::new(rect.width(), rect.height())?;
{
let old_pixels = self.pixels();
let mut new_mut = new.as_mut();
let new_pixels = new_mut.pixels_mut();
for y in 0..rect.height() {
for x in 0..rect.width() {
let old_idx = (y + rect.y() as u32) * self.width() + (x + rect.x() as u32);
let new_idx = y * rect.width() + x;
new_pixels[new_idx as usize] = old_pixels[old_idx as usize];
}
}
}
Some(new)
}
#[cfg(feature = "png-format")]
pub fn encode_png(&self) -> Result<Vec<u8>, png::EncodingError> {
let mut tmp_pixmap = self.to_owned();
for pixel in tmp_pixmap.pixels_mut() {
let c = pixel.demultiply();
*pixel =
PremultipliedColorU8::from_rgba_unchecked(c.red(), c.green(), c.blue(), c.alpha());
}
let mut data = Vec::new();
{
let mut encoder = png::Encoder::new(&mut data, self.width(), self.height());
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header()?;
writer.write_image_data(&tmp_pixmap.data)?;
}
Ok(data)
}
#[cfg(feature = "png-format")]
pub fn save_png<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), png::EncodingError> {
let data = self.encode_png()?;
std::fs::write(path, data)?;
Ok(())
}
}
impl core::fmt::Debug for PixmapRef<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PixmapRef")
.field("data", &"...")
.field("width", &self.size.width())
.field("height", &self.size.height())
.finish()
}
}
#[derive(PartialEq)]
pub struct PixmapMut<'a> {
data: &'a mut [u8],
size: IntSize,
}
impl<'a> PixmapMut<'a> {
pub fn from_bytes(data: &'a mut [u8], width: u32, height: u32) -> Option<Self> {
let size = IntSize::from_wh(width, height)?;
let data_len = data_len_for_size(size)?;
if data.len() < data_len {
return None;
}
Some(PixmapMut { data, size })
}
pub fn to_owned(&self) -> Pixmap {
Pixmap {
data: self.data.to_vec(),
size: self.size,
}
}
pub fn as_ref(&self) -> PixmapRef {
PixmapRef {
data: self.data,
size: self.size,
}
}
#[inline]
pub fn width(&self) -> u32 {
self.size.width()
}
#[inline]
pub fn height(&self) -> u32 {
self.size.height()
}
pub(crate) fn size(&self) -> IntSize {
self.size
}
pub fn fill(&mut self, color: Color) {
let c = color.premultiply().to_color_u8();
for p in self.pixels_mut() {
*p = c;
}
}
pub fn data_mut(&mut self) -> &mut [u8] {
self.data
}
pub fn pixels_mut(&mut self) -> &mut [PremultipliedColorU8] {
bytemuck::cast_slice_mut(self.data_mut())
}
pub(crate) fn as_subpixmap(&mut self) -> SubPixmapMut {
SubPixmapMut {
size: self.size(),
real_width: self.width() as usize,
data: &mut self.data,
}
}
pub(crate) fn subpixmap(&mut self, rect: IntRect) -> Option<SubPixmapMut> {
let rect = self.size.to_int_rect(0, 0).intersect(&rect)?;
let row_bytes = self.width() as usize * BYTES_PER_PIXEL;
let offset = rect.top() as usize * row_bytes + rect.left() as usize * BYTES_PER_PIXEL;
Some(SubPixmapMut {
size: rect.size(),
real_width: self.width() as usize,
data: &mut self.data[offset..],
})
}
}
impl core::fmt::Debug for PixmapMut<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PixmapMut")
.field("data", &"...")
.field("width", &self.size.width())
.field("height", &self.size.height())
.finish()
}
}
pub struct SubPixmapMut<'a> {
pub data: &'a mut [u8],
pub size: IntSize,
pub real_width: usize,
}
impl<'a> SubPixmapMut<'a> {
pub fn pixels_mut(&mut self) -> &mut [PremultipliedColorU8] {
bytemuck::cast_slice_mut(self.data)
}
}
fn min_row_bytes(size: IntSize) -> Option<NonZeroUsize> {
let w = i32::try_from(size.width()).ok()?;
let w = w.checked_mul(BYTES_PER_PIXEL as i32)?;
NonZeroUsize::new(w as usize)
}
fn compute_data_len(size: IntSize, row_bytes: usize) -> Option<usize> {
let h = size.height().checked_sub(1)?;
let h = (h as usize).checked_mul(row_bytes)?;
let w = (size.width() as usize).checked_mul(BYTES_PER_PIXEL)?;
h.checked_add(w)
}
fn data_len_for_size(size: IntSize) -> Option<usize> {
let row_bytes = min_row_bytes(size)?;
compute_data_len(size, row_bytes.get())
}