msla_format 0.2.0

Library for encoding and decoding various MSLA file formats: Elegoo (.goo), Chitu Encrypted (.ctb), NanoDLP (.nanodlp).
Documentation
//! NanoDLP format (`.nanodlp`).
//!
//! Implementation of the `.nanodlp` format.
//! With a custom PNG encoder (deflate implementation) optimized for writing run length encoded layer data.
//!
//! ## References
//!
//! - <https://docs.nanodlp.com/manual/format>
//! - <https://docs.nano3dtech.com/manual/code>

use std::io::{Cursor, Read};

use anyhow::{Ok, Result};
use image::{DynamicImage, ImageFormat, codecs::png::PngDecoder};

mod file;
mod layer;
mod types;
pub use crate::nanodlp::{
    file::File,
    layer::{Layer, LayerDecoder, LayerEncoder},
};

fn read_to_bytes<T: Read>(mut reader: T) -> Result<Vec<u8>> {
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf)?;
    Ok(buf)
}

fn decode_png(png: &[u8]) -> Result<DynamicImage> {
    let decoder = PngDecoder::new(Cursor::new(png))?;
    let image = DynamicImage::from_decoder(decoder)?;
    Ok(image)
}

fn encode_png(image: &DynamicImage) -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    image.write_to(&mut Cursor::new(&mut bytes), ImageFormat::Png)?;
    Ok(bytes)
}