Skip to content

Commit

Permalink
Move to image-rs' png encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
NickAcPT committed Aug 11, 2024
1 parent e621000 commit e621000
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions nmsr-aas/src/utils/png.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
use mtpng::{
encoder::{Encoder, Options},
ColorType, Header,
};
use image::{codecs::png::PngEncoder, ImageEncoder};
use tracing::trace_span;

use crate::error::{ExplainableExt, Result};
use crate::error::{ExplainableExt, NMSRaaSError, Result};

pub(crate) fn create_png_from_bytes(size: (u32, u32), bytes: &[u8]) -> Result<Vec<u8>> {
let render_bytes = Vec::new();
pub(crate) fn create_png_from_bytes((width, height): (u32, u32), bytes: &[u8]) -> Result<Vec<u8>> {
let mut out = Vec::new();

let _guard = trace_span!("write_image_bytes").entered();

let mut header = Header::new();
header
.set_size(size.0, size.1)
.explain_closure(|| "Unable to set size for output PNG".to_string())?;
header
.set_color(ColorType::TruecolorAlpha, 8)
.explain_closure(|| "Unable to set color type for output PNG".to_string())?;

let options = Options::new();

let mut encoder = Encoder::new(render_bytes, &options);

let encoder = PngEncoder::new(&mut out);

encoder
.write_header(&header)
.explain_closure(|| "Unable to write header for output PNG".to_string())?;
encoder
.write_image_rows(bytes)
.explain_closure(|| "Unable to write image rows for output PNG".to_string())?;
.write_image(&bytes, width, height, image::ExtendedColorType::Rgba8)
.map_err(|e| NMSRaaSError::ClonedError(e.to_string()))?;

encoder
.finish()
.explain_closure(|| "Unable to finish writing output PNG".to_string())
Ok(out)
}

0 comments on commit e621000

Please sign in to comment.