Fix for RGBA conversion and additional tests
This commit is contained in:
parent
f94f53185c
commit
476279176b
|
@ -1,7 +1,7 @@
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use image::ImageFormat;
|
use image::{ColorType, DynamicImage, ImageFormat};
|
||||||
use image::io::Reader as ImageReader;
|
use image::io::Reader as ImageReader;
|
||||||
use img_parts::{DynImage, ImageEXIF, ImageICC};
|
use img_parts::{DynImage, ImageEXIF, ImageICC};
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ pub fn convert_in_memory(in_file: Vec<u8>, format: SupportedFileTypes, parameter
|
||||||
}
|
}
|
||||||
|
|
||||||
let i = in_file.as_slice();
|
let i = in_file.as_slice();
|
||||||
let original_image = ImageReader::new(Cursor::new(i)).with_guessed_format()
|
let mut original_image = ImageReader::new(Cursor::new(i)).with_guessed_format()
|
||||||
.map_err(|e| CaesiumError {
|
.map_err(|e| CaesiumError {
|
||||||
message: e.to_string(),
|
message: e.to_string(),
|
||||||
code: 10402,
|
code: 10402,
|
||||||
|
@ -32,6 +32,15 @@ pub fn convert_in_memory(in_file: Vec<u8>, format: SupportedFileTypes, parameter
|
||||||
code: 10403,
|
code: 10403,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
if format == SupportedFileTypes::Jpeg {
|
||||||
|
original_image = match original_image.color() {
|
||||||
|
ColorType::Rgba8 => DynamicImage::from(original_image.to_rgb8()),
|
||||||
|
ColorType::Rgba16 => DynamicImage::from(original_image.to_rgb16()),
|
||||||
|
ColorType::Rgba32F => DynamicImage::from(original_image.to_rgb32f()),
|
||||||
|
_ => original_image,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
let mut output_image: Vec<u8> = Vec::new();
|
let mut output_image: Vec<u8> = Vec::new();
|
||||||
original_image.write_to(&mut Cursor::new(&mut output_image), output_format)
|
original_image.write_to(&mut Cursor::new(&mut output_image), output_format)
|
||||||
.map_err(|e| CaesiumError {
|
.map_err(|e| CaesiumError {
|
||||||
|
|
|
@ -175,7 +175,7 @@ pub fn compress_in_memory(
|
||||||
SupportedFileTypes::Tiff => tiff::compress_in_memory(in_file, parameters)?,
|
SupportedFileTypes::Tiff => tiff::compress_in_memory(in_file, parameters)?,
|
||||||
_ => {
|
_ => {
|
||||||
return Err(CaesiumError {
|
return Err(CaesiumError {
|
||||||
message: "Format not supported for compression to size".into(),
|
message: "Format not supported for compression in memory".into(),
|
||||||
code: 10200,
|
code: 10200,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,257 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::sync::Once;
|
||||||
|
|
||||||
|
use caesium::SupportedFileTypes;
|
||||||
|
|
||||||
|
use crate::cleanup::remove_compressed_test_file;
|
||||||
|
|
||||||
|
mod cleanup;
|
||||||
|
|
||||||
|
static INIT: Once = Once::new();
|
||||||
|
|
||||||
|
pub fn initialize(file: &str) {
|
||||||
|
INIT.call_once(|| {
|
||||||
|
remove_compressed_test_file(file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_jpg_to_png() {
|
||||||
|
let output = "tests/samples/output/jpg.to.png";
|
||||||
|
initialize(output);
|
||||||
|
let mut params = caesium::initialize_parameters();
|
||||||
|
params.keep_metadata = true;
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Png).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/png"
|
||||||
|
);
|
||||||
|
assert!(metadata_is_equal(
|
||||||
|
Path::new("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
|
Path::new(output)
|
||||||
|
));
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_jpg_to_webp() {
|
||||||
|
let output = "tests/samples/output/jpg.to.webp";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::WebP).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/webp"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_jpg_to_tiff() {
|
||||||
|
let output = "tests/samples/output/jpg.to.tiff";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_드림캐쳐.jpg"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Tiff).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/tiff"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_png_to_jpg() {
|
||||||
|
let output = "tests/samples/output/png.to.jpg";
|
||||||
|
initialize(output);
|
||||||
|
let mut params = caesium::initialize_parameters();
|
||||||
|
params.keep_metadata = true;
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_드림캐쳐.png"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Jpeg).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/jpeg"
|
||||||
|
);
|
||||||
|
// assert!(metadata_is_equal(
|
||||||
|
// Path::new("tests/samples/uncompressed_드림캐쳐.png"),
|
||||||
|
// Path::new(output)
|
||||||
|
// ));
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_png_to_webp() {
|
||||||
|
let output = "tests/samples/output/png.to.webp";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_드림캐쳐.png"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::WebP).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/webp"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_png_to_tiff() {
|
||||||
|
let output = "tests/samples/output/png.to.tiff";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_드림캐쳐.png"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Tiff).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/tiff"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_webp_to_jpg() {
|
||||||
|
let output = "tests/samples/output/webp.to.jpg";
|
||||||
|
initialize(output);
|
||||||
|
let mut params = caesium::initialize_parameters();
|
||||||
|
params.keep_metadata = true;
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_家.webp"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Jpeg).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/jpeg"
|
||||||
|
);
|
||||||
|
// assert!(metadata_is_equal(
|
||||||
|
// Path::new("tests/samples/uncompressed_家.webp"),
|
||||||
|
// Path::new(output)
|
||||||
|
// ));
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_webp_to_png() {
|
||||||
|
let output = "tests/samples/output/webp.to.png";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_家.webp"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Png).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/png"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_webp_to_tiff() {
|
||||||
|
let output = "tests/samples/output/webp.to.tiff";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/uncompressed_家.webp"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Tiff).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/tiff"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_tiff_to_jpg() {
|
||||||
|
let output = "tests/samples/output/tiff.to.jpg";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/rgba8.tif"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Jpeg).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/jpeg"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_tiff_to_png() {
|
||||||
|
let output = "tests/samples/output/tiff.to.png";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/rgba8.tif"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::Png).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/png"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn convert_tiff_to_webp() {
|
||||||
|
let output = "tests/samples/output/tiff.to.webp";
|
||||||
|
initialize(output);
|
||||||
|
let params = caesium::initialize_parameters();
|
||||||
|
caesium::convert(String::from("tests/samples/rgba8.tif"),
|
||||||
|
String::from(output),
|
||||||
|
¶ms,
|
||||||
|
SupportedFileTypes::WebP).expect("Image converted successfully");
|
||||||
|
assert!(std::path::Path::new(output).exists());
|
||||||
|
assert_eq!(
|
||||||
|
infer::get_from_path(output).unwrap().unwrap().mime_type(),
|
||||||
|
"image/webp"
|
||||||
|
);
|
||||||
|
remove_compressed_test_file(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_exif(path: &Path) -> HashMap<String, String> {
|
||||||
|
let file = fs::File::open(path).unwrap();
|
||||||
|
let mut bufreader = std::io::BufReader::new(&file);
|
||||||
|
let exif_reader = exif::Reader::new();
|
||||||
|
let exif = exif_reader.read_from_container(&mut bufreader).unwrap();
|
||||||
|
let mut exif_map = HashMap::new();
|
||||||
|
for f in exif.fields() {
|
||||||
|
exif_map.insert(format!("{}", f.tag), f.display_value().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
exif_map
|
||||||
|
}
|
||||||
|
|
||||||
|
fn metadata_is_equal(input: &Path, output: &Path) -> bool {
|
||||||
|
let original_exif_map = extract_exif(input);
|
||||||
|
let compressed_exif_map = extract_exif(output);
|
||||||
|
|
||||||
|
original_exif_map.eq(&compressed_exif_map)
|
||||||
|
}
|
|
@ -78,6 +78,27 @@ fn jpeg_resize_optimize_with_metadata() {
|
||||||
));
|
));
|
||||||
remove_compressed_test_file(output)
|
remove_compressed_test_file(output)
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// #[test]
|
||||||
|
// fn webp_compress_80_with_metadata() {
|
||||||
|
// let output = "tests/samples/output/compressed_80_metadata.webp";
|
||||||
|
// initialize(output);
|
||||||
|
// let mut pars = caesium::initialize_parameters();
|
||||||
|
// pars.webp.quality = 80;
|
||||||
|
// pars.keep_metadata = true;
|
||||||
|
// caesium::compress(
|
||||||
|
// String::from("tests/samples/uncompressed_家.webp"),
|
||||||
|
// String::from(output),
|
||||||
|
// &pars,
|
||||||
|
// )
|
||||||
|
// .unwrap();
|
||||||
|
// assert!(Path::new(output).exists());
|
||||||
|
// assert!(metadata_is_equal(
|
||||||
|
// Path::new("tests/samples/uncompressed_家.webp"),
|
||||||
|
// Path::new(output)
|
||||||
|
// ));
|
||||||
|
// remove_compressed_test_file(output)
|
||||||
|
// }
|
||||||
|
|
||||||
fn extract_exif(path: &Path) -> HashMap<String, String> {
|
fn extract_exif(path: &Path) -> HashMap<String, String> {
|
||||||
let file = fs::File::open(path).unwrap();
|
let file = fs::File::open(path).unwrap();
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
*
|
||||||
|
!.gitignore
|
Binary file not shown.
Before Width: | Height: | Size: 29 KiB |
Binary file not shown.
Before Width: | Height: | Size: 630 KiB After Width: | Height: | Size: 2.8 MiB |
Loading…
Reference in New Issue