Skip to content

Commit

Permalink
simplify image encoding code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed May 12, 2024
1 parent f352a0e commit 25305f0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 69 deletions.
4 changes: 3 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,9 @@ impl App {
};
color_space = format!("{color_space}: {color_str}");
}
ui.label(color_space);
if ui.label(color_space).clicked() {
self.color_space_visible = true;
}
}
}
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
Expand Down
8 changes: 4 additions & 4 deletions src/app/save_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
preferences::PREFERENCES,
};
use crate::{
image_io::save::{exr, farbfeld, gif, jpeg, save_with_format, tiff, webp, webp_animation},
image_io::save::{gif, jpeg, save_with_format, webp, webp_animation},
util::{Image, ImageData, UserEvent},
WgpuState,
};
Expand Down Expand Up @@ -103,8 +103,8 @@ pub fn save(
}
"ico" => save_with_format(path, &frames[0], ImageFormat::Ico),
"tga" => save_with_format(path, &frames[0], ImageFormat::Tga),
"ff" | "farbfeld" => farbfeld(path, &frames[0]),
"tiff" | "tif" => tiff(path, &frames[0]),
"ff" | "farbfeld" => save_with_format(path, &frames[0], ImageFormat::Farbfeld),
"tiff" | "tif" => save_with_format(path, &frames[0], ImageFormat::Tiff),
"gif" => gif(path, frames),
"webp" => {
let (quality, lossy) = match get_webp_quality(dialog_proxy.clone()) {
Expand All @@ -121,7 +121,7 @@ pub fn save(
webp(path, &frames[0], quality, lossy)
}
}
"exr" => exr(path, &frames[0]),
"exr" => save_with_format(path, &frames[0], ImageFormat::OpenExr),
_ => {
path.set_extension("png");
save_with_format(path, &frames[0], ImageFormat::Png)
Expand Down
67 changes: 3 additions & 64 deletions src/image_io/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ use std::{
};

use image::{
codecs::{
farbfeld::FarbfeldEncoder, gif::GifEncoder, jpeg::JpegEncoder, openexr::OpenExrEncoder,
tiff::TiffEncoder,
},
EncodableLayout, Frame, GenericImageView, ImageEncoder, ImageError, ImageFormat,
codecs::{gif::GifEncoder, jpeg::JpegEncoder},
Frame, GenericImageView, ImageError, ImageFormat,
};
use webp_animation::prelude::*;

use crate::util::{HasAlpha, Image};
use crate::util::Image;

type SaveResult<T> = Result<T, SaveError>;

Expand Down Expand Up @@ -117,24 +114,6 @@ pub fn save_with_format(
Ok(())
}

#[inline]
pub fn tiff(path: impl AsRef<Path>, image: &Image) -> SaveResult<()> {
let temp_path = get_temp_path(path.as_ref());
let file = open_file(&temp_path)?;

let encoder = TiffEncoder::new(BufWriter::new(file));
let buffer = image.buffer();

encoder.encode(
buffer.as_bytes(),
buffer.width(),
buffer.height(),
buffer.color().into(),
)?;

Ok(fs::rename(temp_path, path)?)
}

#[inline]
pub fn jpeg(path: impl AsRef<Path>, image: &Image, quality: u8) -> SaveResult<()> {
let temp_path = get_temp_path(path.as_ref());
Expand Down Expand Up @@ -165,20 +144,6 @@ pub fn gif(path: impl AsRef<Path>, images: Vec<Image>) -> SaveResult<()> {
Ok(fs::rename(temp_path, path)?)
}

#[inline]
pub fn farbfeld(path: impl AsRef<Path>, image: &Image) -> SaveResult<()> {
let temp_path = get_temp_path(path.as_ref());
let file = open_file(&temp_path)?;
let encoder = FarbfeldEncoder::new(BufWriter::new(file));
encoder.encode(
image.buffer().to_rgba16().as_bytes(),
image.buffer().width(),
image.buffer().height(),
)?;

Ok(fs::rename(temp_path, path)?)
}

#[inline]
pub fn webp_animation(
path: impl AsRef<Path>,
Expand Down Expand Up @@ -245,29 +210,3 @@ pub fn webp(path: impl AsRef<Path>, image: &Image, quality: f32, lossy: bool) ->

Ok(fs::rename(temp_path, path)?)
}

#[inline]
pub fn exr(path: impl AsRef<Path>, image: &Image) -> SaveResult<()> {
let temp_path = get_temp_path(path.as_ref());
let file = open_file(&temp_path)?;
let encoder = OpenExrEncoder::new(BufWriter::new(file));
let (width, height) = image.buffer().dimensions();

if image.buffer().has_alpha() {
encoder.write_image(
image.buffer().to_rgba32f().as_bytes(),
width,
height,
image::ColorType::Rgba32F.into(),
)?;
} else {
encoder.write_image(
image.buffer().to_rgb32f().as_bytes(),
width,
height,
image::ColorType::Rgb32F.into(),
)?;
}

Ok(fs::rename(temp_path, path)?)
}

0 comments on commit 25305f0

Please sign in to comment.