Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/dither.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Image dithering algorithms and utilities.

use std::path::PathBuf;
use std::path::Path;

use image::{ExtendedColorType, ImageReader};

Expand Down Expand Up @@ -215,7 +215,7 @@ pub const SIERRALITE: [f32; 6] = [0.0, 0.0, 2.0 / 4.0, 1.0 / 4.0, 1.0 / 4.0, 0.0
/// - The image file cannot be opened
/// - The image cannot be decoded
#[must_use]
pub fn open_image(path: &PathBuf) -> (Vec<u8>, u32, u32) {
pub fn open_image<P: AsRef<Path>>(path: P) -> (Vec<u8>, u32, u32) {
//let image = ImageReader::open(path).unwrap().decode().unwrap().into_rgba8();
let image = ImageReader::open(path).unwrap().decode().unwrap().into_rgb8();

Expand All @@ -224,7 +224,7 @@ pub fn open_image(path: &PathBuf) -> (Vec<u8>, u32, u32) {
(buffer, width, height)
}

pub fn save_image(buffer: Vec<u8>, path: PathBuf, width: u32, height: u32) {
pub fn save_image<P: AsRef<Path>>(buffer: Vec<u8>, path: P, width: u32, height: u32) {
let _ = image::save_buffer(path, &buffer, width, height, ExtendedColorType::Rgb8);
}

Expand Down
20 changes: 10 additions & 10 deletions tests/dither_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_image_exists() {

#[test]
fn test_open_image() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));

assert!(width > 0, "Image width should be greater than 0");
assert!(height > 0, "Image height should be greater than 0");
Expand All @@ -25,7 +25,7 @@ fn test_open_image() {

#[test]
fn test_floyd_steinberg_all_palettes() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));
let original_buffer = buffer.clone();

// Test with monochrome
Expand All @@ -46,7 +46,7 @@ fn test_floyd_steinberg_all_palettes() {

#[test]
fn test_error_diffusion_algorithms() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));
let original_buffer = buffer.clone();

let algorithms = [
Expand All @@ -70,7 +70,7 @@ fn test_error_diffusion_algorithms() {

#[test]
fn test_bayer_algorithms() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));
let original_buffer = buffer.clone();

let algorithms = [DitherMethod::Bayer2x2, DitherMethod::Bayer4x4, DitherMethod::Bayer8x8];
Expand All @@ -84,7 +84,7 @@ fn test_bayer_algorithms() {

#[test]
fn test_no_dithering() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));
let original_buffer = buffer.clone();

let mut test_buffer = original_buffer.clone();
Expand All @@ -96,7 +96,7 @@ fn test_no_dithering() {

#[test]
fn test_monochrome_palette_output() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));
let mut test_buffer = buffer;

dither(&mut test_buffer, DitherMethod::FloydSteinberg, ColorPalette::Monochrome, width, height);
Expand All @@ -116,7 +116,7 @@ fn test_monochrome_palette_output() {

#[test]
fn test_save_and_cleanup() {
let (mut buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (mut buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));

dither(&mut buffer, DitherMethod::FloydSteinberg, ColorPalette::Monochrome, width, height);

Expand All @@ -131,7 +131,7 @@ fn test_save_and_cleanup() {

#[test]
fn test_all_algorithms_with_all_palettes() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));

let algorithms = [
DitherMethod::None,
Expand Down Expand Up @@ -177,7 +177,7 @@ fn test_all_algorithms_with_all_palettes() {

#[test]
fn test_buffer_bounds() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));
let mut test_buffer = buffer;

// Test with edge case: 1x1 image would be too small, so test with actual image
Expand All @@ -190,7 +190,7 @@ fn test_buffer_bounds() {

#[test]
fn test_different_algorithms_produce_different_results() {
let (buffer, width, height) = open_image(&PathBuf::from(TEST_IMAGE));
let (buffer, width, height) = open_image(PathBuf::from(TEST_IMAGE));

let mut floyd_buffer = buffer.clone();
let mut atkinson_buffer = buffer.clone();
Expand Down