Skip to content

Commit

Permalink
use image from qoi loading and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed May 12, 2024
1 parent f322a99 commit 960c752
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 61 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ num-traits = "0.2.15"
once_cell = "1.14.0"
pollster = "0.3.0"
psd = "0.3.3"
qoi = "0.4.1"
rand = "0.8.5"
rawloader = "0.37.0"
resvg = "0.40.0"
Expand Down
4 changes: 0 additions & 4 deletions src/app/load_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,8 @@ pub fn load_from_bytes(bytes: &[u8], path_buf: Option<PathBuf>) -> Result<ImageD
load_un_detectable_raster,
load_jxl,
load_heif,
load_qoi,
];

if QOI.contains(&extension.as_str()) {
loaders.swap(0, 7);
}
if HEIF.contains(&extension.as_str()) {
loaders.swap(0, 6);
} else if JXL.contains(&extension.as_str()) {
Expand Down
4 changes: 2 additions & 2 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, qoi, save_with_format, tiff, webp, webp_animation},
image_io::save::{exr, farbfeld, gif, jpeg, save_with_format, tiff, webp, webp_animation},
util::{Image, ImageData, UserEvent},
WgpuState,
};
Expand Down Expand Up @@ -87,6 +87,7 @@ pub fn save(

let res = match ext.as_str() {
"png" => save_with_format(path, &frames[0], ImageFormat::Png),
"qoi" => save_with_format(path, &frames[0], ImageFormat::Qoi),
"jpg" | "jpeg" | "jpe" | "jif" | "jfif" => {
let quality = match get_jpeg_quality(dialog_proxy.clone()) {
Some(quality) => quality,
Expand Down Expand Up @@ -119,7 +120,6 @@ pub fn save(
}
}
"exr" => exr(path, &frames[0]),
"qoi" => qoi(path, &frames[0]),
_ => {
path.set_extension("png");
save_with_format(path, &frames[0], ImageFormat::Png)
Expand Down
25 changes: 2 additions & 23 deletions src/image_io/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use fontdb::Database;
use image::{
codecs::{gif::GifDecoder, openexr::OpenExrDecoder, png::PngDecoder},
io::Reader as ImageReader,
AnimationDecoder, DynamicImage, Frame, ImageBuffer, ImageFormat, Rgb, RgbImage, Rgba,
RgbaImage,
AnimationDecoder, DynamicImage, Frame, ImageBuffer, ImageFormat, Rgb, Rgba,
};
use imagepipe::{ImageSource, Pipeline};
use psd::Psd;
Expand Down Expand Up @@ -247,24 +246,4 @@ pub fn load_heif(bytes: &[u8]) -> Option<Vec<Image>> {
let _ = bytes;
None
}
}

pub fn load_qoi(bytes: &[u8]) -> Option<Vec<Image>> {
let (
qoi::Header {
width,
height,
channels,
//colorspace, FIXME handle linear color
..
},
decoded,
) = qoi::decode_to_vec(bytes).ok()?;
let image = match channels {
qoi::Channels::Rgb => DynamicImage::ImageRgb8(RgbImage::from_raw(width, height, decoded)?),
qoi::Channels::Rgba => {
DynamicImage::ImageRgba8(RgbaImage::from_raw(width, height, decoded)?)
}
};
Some(vec![Image::new(image)])
}
}
27 changes: 0 additions & 27 deletions src/image_io/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub enum SaveError {
#[allow(unused)]
WebpAnimation(webp_animation::Error),
LibWebp(libwebp::error::WebPSimpleError),
Qoi(qoi::Error),
}

impl fmt::Display for SaveError {
Expand All @@ -36,7 +35,6 @@ impl fmt::Display for SaveError {
SaveError::Io(ref e) => e.fmt(f),
SaveError::WebpAnimation(_) => write!(f, "error encoding webp"),
SaveError::LibWebp(ref e) => e.fmt(f),
SaveError::Qoi(ref e) => e.fmt(f),
}
}
}
Expand All @@ -49,7 +47,6 @@ impl error::Error for SaveError {
SaveError::Io(ref e) => Some(e),
SaveError::WebpAnimation(_) => None,
SaveError::LibWebp(ref e) => Some(e),
SaveError::Qoi(ref e) => Some(e),
}
}
}
Expand Down Expand Up @@ -82,13 +79,6 @@ impl From<libwebp::error::WebPSimpleError> for SaveError {
}
}

impl From<qoi::Error> for SaveError {
#[inline]
fn from(err: qoi::Error) -> SaveError {
SaveError::Qoi(err)
}
}

fn open_file(path: impl AsRef<Path>) -> Result<File, std::io::Error> {
OpenOptions::new()
.write(true)
Expand Down Expand Up @@ -281,20 +271,3 @@ pub fn exr(path: impl AsRef<Path>, image: &Image) -> SaveResult<()> {

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

#[inline]
pub fn qoi(path: impl AsRef<Path>, image: &Image) -> SaveResult<()> {
let temp_path = get_temp_path(path.as_ref());
let file = open_file(&temp_path)?;
let (width, height) = image.buffer().dimensions();
let data = if image.buffer().color().has_alpha() {
image.buffer().to_rgba8().into_vec()
} else {
image.buffer().to_rgb8().into_vec()
};

// FIXME handle linear color
let encoder = qoi::Encoder::new(&data, width, height)?.with_colorspace(qoi::ColorSpace::Srgb);
encoder.encode_to_stream(&mut BufWriter::new(file))?;
Ok(fs::rename(temp_path, path)?)
}
4 changes: 1 addition & 3 deletions src/util/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use once_cell::sync::Lazy;

pub static QOI: Lazy<&[&'static str]> = Lazy::new(|| &["qoi"]);

pub static JXL: Lazy<&[&'static str]> = Lazy::new(|| &["jxl"]);

pub static HEIF: Lazy<&[&'static str]> = Lazy::new(|| &["heif", "heic"]);

pub static RASTER: Lazy<&[&'static str]> = Lazy::new(|| {
&[
"png", "jpg", "jpeg", "jpe", "jif", "jfif", "gif", "bmp", "ico", "tiff", "webp", "avif",
"pnm", "pbm", "pgm", "ppm", "pam", "dds", "tga", "ff", "farbfeld", "exr",
"pnm", "pbm", "pgm", "ppm", "pam", "dds", "tga", "ff", "farbfeld", "exr", "qoi",
]
});

Expand Down

0 comments on commit 960c752

Please sign in to comment.