Skip to content

Commit

Permalink
Remove unwrap from write_png
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristramg committed Feb 21, 2023
1 parent 1fc7c4f commit 77c5de8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
9 changes: 9 additions & 0 deletions maplibre/src/headless/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum HeadlessRenderError {
#[error("error while rendering to png")]
WritePng(#[from] png::EncodingError),
#[error("could not create file to save as an image")]
CreateImageFileFailed(#[from] std::io::Error),
}
1 change: 1 addition & 0 deletions maplibre/src/headless/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod graph_node;
mod system;

pub mod environment;
pub mod error;
pub mod map;
pub mod window;

Expand Down
10 changes: 6 additions & 4 deletions maplibre/src/headless/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ impl System for WriteSurfaceBufferSystem {
let padded_buffer = buffer_slice.get_mapped_range();

if self.write_to_disk {
buffered_texture.write_png(
&padded_buffer,
format!("frame_{}.png", current_frame).as_str(),
);
buffered_texture
.write_png(
&padded_buffer,
format!("frame_{}.png", current_frame).as_str(),
)
.expect("Could save frame to disk");
}

// With the current interface, we have to make sure all mapped views are
Expand Down
24 changes: 12 additions & 12 deletions maplibre/src/render/resource/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,32 @@ impl BufferedTextureHead {
self.output_buffer.unmap();
}

pub fn write_png<'a>(&self, padded_buffer: &wgpu::BufferView<'a>, png_output_path: &str) {
pub fn write_png<'a>(
&self,
padded_buffer: &wgpu::BufferView<'a>,
png_output_path: &str,
) -> Result<(), crate::headless::error::HeadlessRenderError> {
use std::{fs::File, io::Write};
let mut png_encoder = png::Encoder::new(
File::create(png_output_path).unwrap(), // TODO: Remove unwrap
File::create(png_output_path)?,
self.buffer_dimensions.width as u32,
self.buffer_dimensions.height as u32,
);
png_encoder.set_depth(png::BitDepth::Eight);
png_encoder.set_color(png::ColorType::Rgba);
let mut png_writer = png_encoder
.write_header()
.unwrap() // TODO: Remove unwrap
.into_stream_writer_with_size(
self.buffer_dimensions.unpadded_bytes_per_row.get() as usize
)
.unwrap(); // TODO: Remove unwrap
let mut png_writer = png_encoder.write_header()?.into_stream_writer_with_size(
self.buffer_dimensions.unpadded_bytes_per_row.get() as usize,
)?;

// from the padded_buffer we write just the unpadded bytes into the image
for chunk in
padded_buffer.chunks(self.buffer_dimensions.padded_bytes_per_row.get() as usize)
{
png_writer
.write_all(&chunk[..self.buffer_dimensions.unpadded_bytes_per_row.get() as usize])
.unwrap(); // TODO: Remove unwrap
.write_all(&chunk[..self.buffer_dimensions.unpadded_bytes_per_row.get() as usize])?
}
png_writer.finish().unwrap(); // TODO: Remove unwrap
png_writer.finish()?;
Ok(())
}

pub fn copy_texture(&self) -> wgpu::ImageCopyTexture<'_> {
Expand Down

0 comments on commit 77c5de8

Please sign in to comment.