Skip to content

Commit

Permalink
Merge pull request #27 from SalOne22/features/image-resize
Browse files Browse the repository at this point in the history
Features/image resize
  • Loading branch information
SalOne22 authored Apr 7, 2023
2 parents 5f960ec + 818818d commit 99a6637
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 33 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v0.5.0

- [Added] Image Resize
- [Added] Resize error to EncodingError
- [Added] Width and Height arguments to CLI
- [Added] Resize filter type argument to CLI
- [Changed] `Config::build` now require 5 arguments

## v0.4.0

- [Added] Image quantization
Expand Down
50 changes: 48 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exclude = ["tests/*", ".*"]
repository = "https://github.com/SalOne22/rimage"
license = "MIT OR Apache-2.0"

version = "0.4.0"
version = "0.5.0"
edition = "2021"

[profile.release]
Expand All @@ -36,6 +36,7 @@ rgb = "0.8.36"
threadpool = "1.8.1"
simple-error = "0.3.0"
wild = "2.1.0"
resize = "0.7.4"

[dev-dependencies]
criterion = { version = "0.4.0", features = ["html_reports"] }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ rimage -q 75 *.jpg
- Threads: `-t 4`, number of threads to use
- Quantization: `--quantization 50`, quality of quantization from 0 to 100, higher is better
- Dithering: `--dithering 0.5`, quality of dithering from 0 to 1, higher is better
- Resize: `--width 250` or `--height 100`, resizes image to specified width or height
- Filter: `--filter mitchell`, filter used to resizing
- More options will be added later

## To-Do
Expand Down
5 changes: 4 additions & 1 deletion benches/encode_jpg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ fn bench_encode_jpg(c: &mut Criterion) {
group.bench_function("Encoder", |b| {
b.iter(|| {
let data = rimage::Encoder::new(
black_box(&rimage::Config::build(75.0, rimage::OutputFormat::MozJpeg).unwrap()),
black_box(
&rimage::Config::build(75.0, rimage::OutputFormat::MozJpeg, None, None, None)
.unwrap(),
),
black_box(image.clone()),
)
.encode()
Expand Down
5 changes: 4 additions & 1 deletion benches/encode_png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ fn bench_encode_png(c: &mut Criterion) {
group.bench_function("Encoder", |b| {
b.iter(|| {
let data = rimage::Encoder::new(
black_box(&rimage::Config::build(75.0, rimage::OutputFormat::Oxipng).unwrap()),
black_box(
&rimage::Config::build(75.0, rimage::OutputFormat::Oxipng, None, None, None)
.unwrap(),
),
black_box(image.clone()),
)
.encode()
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub enum EncodingError {
Encoding(Box<dyn Error>),
/// A quantization error, if some error occurred during quantization
Quantization(Box<dyn Error>),
/// A resize error, if some error occurred during resizing
Resize(Box<dyn Error>),
}

impl Error for EncodingError {}
Expand Down Expand Up @@ -113,13 +115,21 @@ impl From<imagequant::Error> for EncodingError {
}
}

impl From<resize::Error> for EncodingError {
#[inline]
fn from(err: resize::Error) -> Self {
EncodingError::Encoding(Box::new(err))
}
}

impl fmt::Display for EncodingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EncodingError::IoError(io_err) => write!(f, "IO Error: {}", io_err),
EncodingError::Format(fmt_err) => write!(f, "Format Error: {}", fmt_err),
EncodingError::Encoding(enc_err) => write!(f, "Encoding Error: {}", enc_err),
EncodingError::Quantization(qnt_err) => write!(f, "Quantization Error: {}", qnt_err),
EncodingError::Resize(rsz_err) => write!(f, "Resize Error: {}", rsz_err),
}
}
}
Expand Down
44 changes: 43 additions & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, fmt};
use std::{borrow::Cow, fmt, str::FromStr};

/// Image data
///
Expand Down Expand Up @@ -75,3 +75,45 @@ impl fmt::Display for OutputFormat {
}
}
}

/// Wrapper around [`resize::Type`]
#[derive(Debug, Clone)]
pub enum ResizeType {
/// [`resize::Type::Point`]
Point,
/// [`resize::Type::Triangle`]
Triangle,
/// [`resize::Type::Catrom`]
CatmullRom,
/// [`resize::Type::Mitchell`]
Mitchell,
/// [`resize::Type::Lanczos3`]
Lanczos3,
}

// Implement to [`resize::Type`] for [`ResizeType`]
impl From<&ResizeType> for resize::Type {
fn from(resize_type: &ResizeType) -> Self {
match resize_type {
ResizeType::Point => resize::Type::Point,
ResizeType::Triangle => resize::Type::Triangle,
ResizeType::CatmullRom => resize::Type::Catrom,
ResizeType::Mitchell => resize::Type::Mitchell,
ResizeType::Lanczos3 => resize::Type::Lanczos3,
}
}
}

impl FromStr for ResizeType {
type Err = Box<dyn std::error::Error + Send + Sync + 'static>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"point" => Ok(Self::Point),
"triangle" => Ok(Self::Triangle),
"catmull-rom" => Ok(Self::CatmullRom),
"mitchell" => Ok(Self::Mitchell),
"lanczos3" => Ok(Self::Lanczos3),
_ => Err(format!("{} is not a valid resize type", s).into()),
}
}
}
Loading

0 comments on commit 99a6637

Please sign in to comment.