Skip to main content

image_wrapper/
lib.rs

1mod tilesets;
2
3use image::{DynamicImage, GenericImageView};
4use pixels_graphics_lib::color::Color;
5use pixels_graphics_lib::GraphicsError;
6use pixels_graphics_lib::image::Image;
7use thiserror::Error;
8use crate::ImageWrapperError::GraphicsLibError;
9
10#[derive(Error, Debug)]
11pub enum ImageWrapperError {
12    #[error("Creating image")]
13    GraphicsLibError(#[from] GraphicsError),
14    #[error("Creating tile {0}")]
15    TileError(String, #[source] GraphicsError),
16    #[error("Tileset file error")]
17    TilesetFileError(#[from] std::io::Error),
18    #[error("Tileset format error: {0}")]
19    TilesetFormatError(String),
20    #[error("Tileset image reading error")]
21    TilesetImageError(#[from] image::ImageError),
22}
23
24fn convert_image(image: DynamicImage) -> Result<Image, ImageWrapperError> {
25    let width=  image.width() as usize;
26    let height = image.height() as usize;
27    let pixels = image.into_rgba8()
28        .chunks_exact(4)
29        .map(|px| Color::rgba(px[0], px[1], px[2], px[3]))
30        .collect();
31
32    Image::new(pixels, width, height)
33        .map_err(|err| GraphicsLibError(err))
34}