image-wrapper 0.1.0

Simple wrapper library around Image for use with pixels-graphics-lib
Documentation
mod tilesets;

use image::{DynamicImage, GenericImageView};
use pixels_graphics_lib::color::Color;
use pixels_graphics_lib::GraphicsError;
use pixels_graphics_lib::image::Image;
use thiserror::Error;
use crate::ImageWrapperError::GraphicsLibError;

#[derive(Error, Debug)]
pub enum ImageWrapperError {
    #[error("Creating image")]
    GraphicsLibError(#[from] GraphicsError),
    #[error("Creating tile {0}")]
    TileError(String, #[source] GraphicsError),
    #[error("Tileset file error")]
    TilesetFileError(#[from] std::io::Error),
    #[error("Tileset format error: {0}")]
    TilesetFormatError(String),
    #[error("Tileset image reading error")]
    TilesetImageError(#[from] image::ImageError),
}

fn convert_image(image: DynamicImage) -> Result<Image, ImageWrapperError> {
    let width=  image.width() as usize;
    let height = image.height() as usize;
    let pixels = image.into_rgba8()
        .chunks_exact(4)
        .map(|px| Color::rgba(px[0], px[1], px[2], px[3]))
        .collect();

    Image::new(pixels, width, height)
        .map_err(|err| GraphicsLibError(err))
}