1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
use fast_image_resize as fr;
use fast_image_resize::images::{
CroppedImage, CroppedImageMut, Image, ImageRef, TypedCroppedImage, TypedCroppedImageMut,
TypedImage, TypedImageRef,
};
use fast_image_resize::pixels::{U8x4, U8};
use fast_image_resize::{ImageView, IntoImageView, PixelType, ResizeOptions};
#[test]
fn create_image_ref_from_small_buffer() {
let width = 64;
let height = 32;
let buffer = vec![0; 64 * 30];
let res = ImageRef::new(width, height, &buffer, PixelType::U8);
assert_eq!(res.unwrap_err(), fr::ImageBufferError::InvalidBufferSize);
}
#[test]
fn create_image_from_small_buffer() {
let width = 64;
let height = 32;
let mut buffer = vec![0; 64 * 30];
let res = Image::from_slice_u8(width, height, &mut buffer, PixelType::U8);
assert_eq!(res.unwrap_err(), fr::ImageBufferError::InvalidBufferSize);
let res = Image::from_vec_u8(width, height, buffer, PixelType::U8);
assert_eq!(res.unwrap_err(), fr::ImageBufferError::InvalidBufferSize);
}
#[test]
fn create_image_from_big_buffer() {
let width = 64;
let height = 32;
let mut buffer = vec![0; 65 * 32];
let res = Image::from_slice_u8(width, height, &mut buffer, PixelType::U8);
assert!(res.is_ok());
let res = Image::from_vec_u8(width, height, buffer, PixelType::U8);
assert!(res.is_ok());
}
#[test]
fn create_type_image_ref_from_small_buffer() {
let width = 64;
let height = 32;
let buffer = vec![U8::new(0); 64 * 30];
let res = TypedImageRef::<U8>::new(width, height, &buffer);
assert!(matches!(res, Err(fr::InvalidPixelsSize)));
}
#[test]
fn create_typed_image_from_small_buffer() {
let width = 64;
let height = 32;
let mut buffer = vec![0; 64 * 30];
let res = TypedImage::<U8>::from_buffer(width, height, &mut buffer);
assert_eq!(res.unwrap_err(), fr::ImageBufferError::InvalidBufferSize);
let res = TypedImageRef::<U8>::from_buffer(width, height, &buffer);
assert_eq!(res.unwrap_err(), fr::ImageBufferError::InvalidBufferSize);
}
#[test]
fn create_typed_image_from_big_buffer() {
let width = 64;
let height = 32;
let mut buffer = vec![0; 65 * 32];
let res = TypedImage::<U8>::from_buffer(width, height, &mut buffer);
assert!(res.is_ok());
let res = TypedImageRef::<U8>::from_buffer(width, height, &buffer);
assert!(res.is_ok());
}
#[test]
fn typed_cropped_image() {
const BLACK: U8x4 = U8x4::new([0; 4]);
const WHITE: U8x4 = U8x4::new([255; 4]);
let mut source_pixels = Vec::with_capacity(64 * 64);
source_pixels.extend((0..64 * 64).map(|i| {
let y = i / 64;
if (10..54).contains(&y) {
let x = i % 64;
if (10..54).contains(&x) {
return WHITE;
}
}
BLACK
}));
// Black source image with white square inside
let src_image = TypedImage::<U8x4>::from_pixels(64, 64, source_pixels).unwrap();
// Black destination image
let mut dst_image = TypedImage::<U8x4>::new(40, 40);
let cropped_src_image = TypedCroppedImage::from_ref(&src_image, 10, 10, 44, 44).unwrap();
assert_eq!(cropped_src_image.width(), 44);
assert_eq!(cropped_src_image.height(), 44);
let mut resizer = fr::Resizer::new();
resizer
.resize_typed(
&cropped_src_image,
&mut dst_image,
&ResizeOptions::new().resize_alg(fr::ResizeAlg::Nearest),
)
.unwrap();
let white_block = vec![WHITE; 40 * 40];
assert_eq!(dst_image.pixels(), white_block);
}
#[test]
fn cropped_image() {
const BLACK: U8x4 = U8x4::new([0; 4]);
const WHITE: U8x4 = U8x4::new([255; 4]);
let mut source_pixels = Vec::with_capacity(64 * 64);
source_pixels.extend((0..64 * 64).map(|i| {
let y = i / 64;
if (10..54).contains(&y) {
let x = i % 64;
if (10..54).contains(&x) {
return WHITE;
}
}
BLACK
}));
// Black source image with white square inside
let src_image = ImageRef::from_pixels(64, 64, &source_pixels).unwrap();
// Black destination image
let mut dst_image = Image::new(40, 40, PixelType::U8x4);
let cropped_src_image = CroppedImage::new(&src_image, 10, 10, 44, 44).unwrap();
assert_eq!(cropped_src_image.width(), 44);
assert_eq!(cropped_src_image.height(), 44);
let mut resizer = fr::Resizer::new();
resizer
.resize(
&cropped_src_image,
&mut dst_image,
&ResizeOptions::new().resize_alg(fr::ResizeAlg::Nearest),
)
.unwrap();
let dst_typed_image = dst_image.typed_image().unwrap();
let dst_pixels: &[U8x4] = dst_typed_image.pixels();
let white_block = vec![WHITE; 40 * 40];
assert_eq!(dst_pixels, white_block);
}
#[test]
fn typed_cropped_image_mut() {
const BLACK: U8x4 = U8x4::new([0; 4]);
const WHITE: U8x4 = U8x4::new([255; 4]);
// White source image
let src_image = TypedImage::from_pixels(64, 32, vec![WHITE; 64 * 32]).unwrap();
// Black destination image
let mut dst_image = TypedImage::<U8x4>::new(64, 32);
let mut cropped_dst_image =
TypedCroppedImageMut::from_ref(&mut dst_image, 10, 10, 44, 12).unwrap();
assert_eq!(cropped_dst_image.width(), 44);
assert_eq!(cropped_dst_image.height(), 12);
let mut resizer = fr::Resizer::new();
resizer
.resize_typed(
&src_image,
&mut cropped_dst_image,
&ResizeOptions::new().resize_alg(fr::ResizeAlg::Nearest),
)
.unwrap();
let dst_pixels = dst_image.pixels();
let row_size: usize = 64;
let black_block = vec![BLACK; 10 * row_size];
// Top border
assert_eq!(dst_pixels[0..10 * row_size], black_block);
// Middle rows
let mut middle_row = vec![BLACK; 10];
middle_row.extend(vec![WHITE; 44]);
middle_row.extend(vec![BLACK; 10]);
for row in dst_pixels.chunks_exact(row_size).skip(10).take(12) {
assert_eq!(row, middle_row);
}
// Bottom border
assert_eq!(dst_pixels[22 * row_size..], black_block);
}
#[test]
fn cropped_image_mut() {
const BLACK: U8x4 = U8x4::new([0; 4]);
const WHITE: U8x4 = U8x4::new([255; 4]);
// White source image
let src_pixels = vec![WHITE; 64 * 32];
let src_image = ImageRef::from_pixels(64, 32, &src_pixels).unwrap();
// Black destination image
let mut dst_image = Image::new(64, 32, PixelType::U8x4);
let mut cropped_dst_image = CroppedImageMut::new(&mut dst_image, 10, 10, 44, 12).unwrap();
assert_eq!(cropped_dst_image.width(), 44);
assert_eq!(cropped_dst_image.height(), 12);
let mut resizer = fr::Resizer::new();
resizer
.resize(
&src_image,
&mut cropped_dst_image,
&ResizeOptions::new().resize_alg(fr::ResizeAlg::Nearest),
)
.unwrap();
let dst_typed_image = dst_image.typed_image().unwrap();
let dst_pixels: &[U8x4] = dst_typed_image.pixels();
let row_size: usize = 64;
let black_block = vec![BLACK; 10 * row_size];
// Top border
assert_eq!(dst_pixels[0..10 * row_size], black_block);
// Middle rows
let mut middle_row = vec![BLACK; 10];
middle_row.extend(vec![WHITE; 44]);
middle_row.extend(vec![BLACK; 10]);
for row in dst_pixels.chunks_exact(row_size).skip(10).take(12) {
assert_eq!(row, middle_row);
}
// Bottom border
assert_eq!(dst_pixels[22 * row_size..], black_block);
}
|