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 19, 2023
1 parent 1fc7c4f commit 7ad15ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
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
5 changes: 5 additions & 0 deletions maplibre/src/render/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub enum RenderError {
Graph(#[from] RenderGraphError),
#[error("error while requesting device")]
RequestDevice(#[from] wgpu::RequestDeviceError),
#[cfg(feature = "headless")]
#[error("error while writing surface to png")]
WritePng(#[from] png::EncodingError),
#[error("error in file io")]
IoError(#[from] std::io::Error),
}

impl RenderError {
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::render::error::RenderError> {
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 7ad15ac

Please sign in to comment.