Skip to content

Commit

Permalink
Merge pull request #3 from ThibsG/add_tests
Browse files Browse the repository at this point in the history
Add tests for image processing
  • Loading branch information
ThibsG authored Sep 29, 2024
2 parents 62d873a + 59c5ae8 commit 1237662
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Cargo.lock
/result
/data/exif_work.jpg
/tmp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Filigram-rs
This library is a convenient wrapper to watermark image files in a folder, recursively.

<p align="center">
<img alt="original" src="./data/original.jpg" width="300" height="300"/>
<img alt="original" src="./data/original.jpg" width="400" height="300"/>
<img alt="watermarked" src="./data/watermarked.jpg" width="300" height="300"/>
</p>

Expand Down
Empty file modified data/input/ColdWarm.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified data/input/dir1/subdir2/subsubdir1/ColdWarm.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified data/input/dir2/subdir1/ColdWarm.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified data/input/dir2/subdir2/ColdWarm.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/filigram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};

// default parameters
let cfg = Config::new();
let cfg = Config::default();

let progress = ProgressBar::new(0).with_style(
ProgressStyle::default_bar()
Expand Down
6 changes: 0 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,3 @@ impl Default for Config {
}
}
}

impl Config {
pub fn new() -> Self {
Self::default()
}
}
17 changes: 10 additions & 7 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ use std::path::Path;

use crate::config::Config;

pub(crate) fn create_watermark_image(
cfg: &Config,
) -> Result<RgbaImage, Box<dyn std::error::Error>> {
pub fn create_watermark_image(cfg: &Config) -> Result<RgbaImage, Box<dyn std::error::Error>> {
let mut img: RgbaImage = ImageBuffer::new(500, 500);

// font for watermark
let font_bytes = include_bytes!("../fonts/Roboto-Bold.ttf");
let font = Font::try_from_bytes(font_bytes).expect("font is not valid");
let font = Font::try_from_bytes(font_bytes).ok_or_else(|| {
Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"font is not valid",
))
})?;

draw_text_mut(&mut img, cfg.color, 0, 210, cfg.scale, &font, &cfg.text);

Expand All @@ -24,9 +27,9 @@ pub(crate) fn create_watermark_image(
Ok(img)
}

pub(crate) fn overlay_watermark(
src: &impl AsRef<Path>,
dst: &impl AsRef<Path>,
pub fn overlay_watermark<P: AsRef<Path>>(
src: P,
dst: P,
watermark_img: &RgbaImage,
) -> Result<(), Box<dyn std::error::Error>> {
let mut img = ImageReader::open(src)?.decode()?;
Expand Down
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ pub mod config;
mod graphics;
pub mod rules;

use crate::graphics::{create_watermark_image, overlay_watermark};

pub use crate::config::Config;
pub use crate::rules::Rules;
pub use config::Config;
pub use graphics::{create_watermark_image, overlay_watermark};
pub use indicatif;
pub use rules::Rules;

use indicatif::ProgressBar;

Expand Down Expand Up @@ -86,7 +85,7 @@ pub fn spread_watermark<P: AsRef<Path> + std::fmt::Debug + Sync>(
if rules.is_file_qualified(&path) {
debug!("watermarking {path:?}");

if let Err(e) = overlay_watermark(&path, &target_path, &watermark_img) {
if let Err(e) = overlay_watermark(path, &target_path, &watermark_img) {
error!("Error watermarking: {:?} - {}", path, e.to_string());
} else {
recopy_metadata(&path, &target_path.as_path())
Expand Down
Binary file added tests/img/test.bmp
Binary file not shown.
Binary file added tests/img/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/img/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/img/test.webp
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/processing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use filigram_rs::{create_watermark_image, overlay_watermark, Config};

macro_rules! run_test {
($extension:literal) => {
let cfg = Config::default();
std::fs::create_dir("tmp").ok();
let watermark_img = create_watermark_image(&cfg).unwrap();
overlay_watermark(
format!("tests/img/test.{}", $extension),
format!("tmp/test.{}", $extension),
&watermark_img,
)
.unwrap();
};
}

#[test]
fn test_jpeg() {
run_test!("jpg");
}

#[test]
fn test_gif() {
run_test!("gif");
}

#[test]
fn test_webp() {
run_test!("webp");
}

#[test]
fn test_bmp() {
run_test!("bmp");
}

0 comments on commit 1237662

Please sign in to comment.