Skip to content

Commit

Permalink
[backend.rs] PPM optimize performance
Browse files Browse the repository at this point in the history
Signed-off-by: Shinyzenith <[email protected]>
  • Loading branch information
AmusedAstronaut authored and Shinyzenith committed Jul 21, 2022
1 parent 1ea7198 commit ac252a8
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub fn write_to_file(
frame_copy.frame_format.height,
frame_copy.frame_color_type,
)?;
let _ = output_file.flush()?;
output_file.flush()?;
}
EncodingFormat::Png => {
PngEncoder::new(&mut output_file).write_image(
Expand All @@ -379,20 +379,21 @@ pub fn write_to_file(
frame_copy.frame_format.height,
frame_copy.frame_color_type,
)?;
let _ = output_file.flush()?;
output_file.flush()?;
}
EncodingFormat::Ppm => {
let rgb8_data = if let ColorType::Rgba8 = frame_copy.frame_color_type {
let data = frame_copy
.frame_mmap
.iter()
.enumerate()
.filter_map(|(i, &v)| if i % 4 != 3 { Some(v) } else { None })
.collect::<Vec<u8>>();
let mut data = Vec::with_capacity(
(3 * frame_copy.frame_format.width * frame_copy.frame_format.height) as _,
);
for chunk in frame_copy.frame_mmap.chunks_exact(4) {
data.extend_from_slice(&chunk[..3]);
}
data
} else {
unimplemented!("Currently only ColorType::Rgba8 is supported")
};

PnmEncoder::new(&mut output_file)
.with_subtype(pnm::PnmSubtype::Pixmap(pnm::SampleEncoding::Binary))
.write_image(
Expand All @@ -401,7 +402,7 @@ pub fn write_to_file(
frame_copy.frame_format.height,
ColorType::Rgb8,
)?;
let _ = output_file.flush()?;
output_file.flush()?;
}
}

Expand Down

0 comments on commit ac252a8

Please sign in to comment.