Skip to content

Commit

Permalink
remove the redundancy I introduced testing wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Jul 17, 2024
1 parent 6953293 commit cd62aef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
25 changes: 10 additions & 15 deletions src/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,28 @@ pub fn process_input(buffer: &[u8]) -> RawGrid {
.collect()
}

pub fn output(
path: PathBuf,
image_data: RawGrid,
palette: ColorPalette,
) -> Result<(), image::ImageError> {
pub fn convert_image(image_data: RawGrid, palette: ColorPalette) -> RgbImage {
let mut img = RgbImage::new(image_data[0].len() as u32, image_data.len() as u32);

for (x, row) in image_data.iter().enumerate() {
for (y, index) in row.iter().enumerate() {
img.put_pixel(y as u32, x as u32, palette[*index as usize].to_rgb());
}
}
img
}

img.save(path)
pub fn save(
path: PathBuf,
image_data: RawGrid,
palette: ColorPalette,
) -> Result<(), image::ImageError> {
convert_image(image_data, palette).save(path)
}

pub fn write_to(
bytes: &mut Vec<u8>,
image_data: RawGrid,
palette: ColorPalette,
) -> Result<(), image::ImageError> {
let mut img = RgbImage::new(image_data[0].len() as u32, image_data.len() as u32);

for (x, row) in image_data.iter().enumerate() {
for (y, index) in row.iter().enumerate() {
img.put_pixel(y as u32, x as u32, palette[*index as usize].to_rgb());
}
}
img.write_to(&mut Cursor::new(bytes), image::ImageFormat::Png)
convert_image(image_data, palette).write_to(&mut Cursor::new(bytes), image::ImageFormat::Png)
}
2 changes: 1 addition & 1 deletion src/terminal/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {

#[cfg(feature = "png")]
if let Some(output) = args.output_file {
png::output(output, image_data.clone(), palette.clone())?
png::save(output, image_data.clone(), palette.clone())?
}

if let Some(ga_file) = args.ga_file {
Expand Down

0 comments on commit cd62aef

Please sign in to comment.