-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
9 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |