mirror of
https://github.com/Cykooz/fast_image_resize.git
synced 2026-10-08 01:11:09 +00:00
Added error `InvalidBufferSizeError`.
This commit is contained in:
@@ -96,16 +96,14 @@ use fast_image_resize::{
|
||||
CropBox, FilterType, ImageData, PixelType, ResizeAlg, Resizer, SrcImageView,
|
||||
};
|
||||
|
||||
fn resize_lanczos3(src_pixels: &[u8], width: NonZeroU32, height: NonZeroU32) -> Vec<u8> {
|
||||
// Create wrapper for raw data of source image
|
||||
let src_image = ImageData::new(width, height, src_pixels, PixelType::U8x4).unwrap();
|
||||
// Get immutable view of image data
|
||||
let src_view = src_image.src_view();
|
||||
fn resize_lanczos3(src_pixels: &[u32], width: NonZeroU32, height: NonZeroU32) -> Vec<u8> {
|
||||
// Create immutable view of source image data
|
||||
let src_view = SrcImageView::from_pixels(width, height, src_pixels, PixelType::U8x4).unwrap();
|
||||
|
||||
let dst_width = NonZeroU32::new(1024).unwrap();
|
||||
let dst_height = NonZeroU32::new(768).unwrap();
|
||||
// Create wrapper that own data of destination image
|
||||
let mut dst_image = ImageData::new_owned(dst_width, dst_height, src_image.pixel_type());
|
||||
let mut dst_image = ImageData::new_owned(dst_width, dst_height, src_view.pixel_type());
|
||||
// Get mutable view of destination image data
|
||||
let mut dst_view = dst_image.dst_view();
|
||||
|
||||
@@ -117,7 +115,7 @@ fn resize_lanczos3(src_pixels: &[u8], width: NonZeroU32, height: NonZeroU32) ->
|
||||
dst_image.get_buffer().to_owned()
|
||||
}
|
||||
|
||||
fn crop_and_resize_image(mut src_view: SrcImageView) -> ImageData<Vec<u8>> {
|
||||
fn crop_and_resize_image(mut src_view: SrcImageView) -> ImageData<Vec<u32>> {
|
||||
// Set crop-box for view of source image
|
||||
src_view
|
||||
.set_crop_box(CropBox {
|
||||
|
||||
@@ -8,6 +8,10 @@ pub enum ImageRowsError {
|
||||
InvalidRowSize,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug, Clone, Copy)]
|
||||
#[error("Size of buffer don't match to image dimensions")]
|
||||
pub struct InvalidBufferSizeError;
|
||||
|
||||
#[derive(Error, Debug, Clone, Copy)]
|
||||
pub enum ImageBufferError {
|
||||
#[error("Size of buffer don't match to image dimensions")]
|
||||
|
||||
+10
-10
@@ -1,6 +1,6 @@
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
use crate::{DstImageView, ImageBufferError, PixelType, SrcImageView};
|
||||
use crate::{DstImageView, InvalidBufferSizeError, PixelType, SrcImageView};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImageData<T: AsRef<[u32]>> {
|
||||
@@ -16,10 +16,10 @@ impl<T: AsRef<[u32]>> ImageData<T> {
|
||||
height: NonZeroU32,
|
||||
pixels: T,
|
||||
pixel_type: PixelType,
|
||||
) -> Result<Self, ImageBufferError> {
|
||||
) -> Result<Self, InvalidBufferSizeError> {
|
||||
let size = (width.get() * height.get()) as usize;
|
||||
if pixels.as_ref().len() != size {
|
||||
return Err(ImageBufferError::InvalidBufferSize);
|
||||
return Err(InvalidBufferSizeError);
|
||||
}
|
||||
Ok(Self {
|
||||
width,
|
||||
@@ -89,19 +89,19 @@ impl ImageData<Vec<u32>> {
|
||||
height: NonZeroU32,
|
||||
buffer: &[u8],
|
||||
pixel_type: PixelType,
|
||||
) -> Result<Self, ImageBufferError> {
|
||||
) -> Result<Self, InvalidBufferSizeError> {
|
||||
let size = (width.get() * height.get()) as usize * 4;
|
||||
if buffer.len() != size {
|
||||
return Err(ImageBufferError::InvalidBufferSize);
|
||||
}
|
||||
let (head, pixels, _) = unsafe { buffer.align_to::<u32>() };
|
||||
if !head.is_empty() {
|
||||
return Err(ImageBufferError::InvalidBufferAlignment);
|
||||
return Err(InvalidBufferSizeError);
|
||||
}
|
||||
let pixels = buffer
|
||||
.chunks_exact(4)
|
||||
.map(|p| u32::from_le_bytes([p[0], p[1], p[2], p[3]]))
|
||||
.collect();
|
||||
Ok(Self {
|
||||
width,
|
||||
height,
|
||||
pixels: pixels.to_owned(),
|
||||
pixels,
|
||||
pixel_type,
|
||||
})
|
||||
}
|
||||
|
||||
+34
-1
@@ -2,7 +2,7 @@ use std::mem::transmute;
|
||||
use std::num::NonZeroU32;
|
||||
use std::slice;
|
||||
|
||||
use crate::errors::{CropBoxError, ImageBufferError, ImageRowsError};
|
||||
use crate::errors::{CropBoxError, ImageBufferError, ImageRowsError, InvalidBufferSizeError};
|
||||
|
||||
pub type TwoRows<'a> = (&'a [u32], &'a [u32]);
|
||||
pub type FourRows<'a> = (&'a [u32], &'a [u32], &'a [u32], &'a [u32]);
|
||||
@@ -95,6 +95,20 @@ impl<'a> SrcImageView<'a> {
|
||||
Ok(Self::from_rows(width, height, rows, pixel_type).unwrap())
|
||||
}
|
||||
|
||||
pub fn from_pixels(
|
||||
width: NonZeroU32,
|
||||
height: NonZeroU32,
|
||||
pixels: &'a [u32],
|
||||
pixel_type: PixelType,
|
||||
) -> Result<Self, InvalidBufferSizeError> {
|
||||
let size = (width.get() * height.get()) as usize;
|
||||
if pixels.len() != size {
|
||||
return Err(InvalidBufferSizeError);
|
||||
}
|
||||
let rows = pixels.chunks(width.get() as usize).collect();
|
||||
Ok(Self::from_rows(width, height, rows, pixel_type).unwrap())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn pixel_type(&self) -> PixelType {
|
||||
self.pixel_type
|
||||
@@ -263,6 +277,25 @@ impl<'a> DstImageView<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_buffer(
|
||||
width: NonZeroU32,
|
||||
height: NonZeroU32,
|
||||
buffer: &'a mut [u8],
|
||||
pixel_type: PixelType,
|
||||
) -> Result<Self, ImageBufferError> {
|
||||
let size = (width.get() * height.get()) as usize * 4;
|
||||
if buffer.len() != size {
|
||||
return Err(ImageBufferError::InvalidBufferSize);
|
||||
}
|
||||
let (head, pixels, _) = unsafe { buffer.align_to_mut::<u32>() };
|
||||
if !head.is_empty() {
|
||||
return Err(ImageBufferError::InvalidBufferAlignment);
|
||||
}
|
||||
|
||||
let rows = pixels.chunks_mut(width.get() as usize).collect();
|
||||
Ok(Self::from_rows(width, height, rows, pixel_type).unwrap())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn pixel_type(&self) -> PixelType {
|
||||
self.pixel_type
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
pub use alpha::{MulDiv, MulDivImageError, MulDivImagesError};
|
||||
pub use convolution::FilterType;
|
||||
pub use errors::{CropBoxError, ImageBufferError, ImageRowsError};
|
||||
pub use errors::{CropBoxError, ImageBufferError, ImageRowsError, InvalidBufferSizeError};
|
||||
pub use image_data::ImageData;
|
||||
pub use image_view::{CropBox, DstImageView, PixelType, SrcImageView};
|
||||
pub use resizer::{CpuExtensions, ResizeAlg, Resizer};
|
||||
|
||||
@@ -4,16 +4,14 @@ use fast_image_resize::{
|
||||
CropBox, FilterType, ImageData, PixelType, ResizeAlg, Resizer, SrcImageView,
|
||||
};
|
||||
|
||||
fn resize_lanczos3(src_pixels: &[u8], width: NonZeroU32, height: NonZeroU32) -> Vec<u8> {
|
||||
// Create wrapper for raw data of source image
|
||||
let src_image = ImageData::new(width, height, src_pixels, PixelType::U8x4).unwrap();
|
||||
// Get immutable view of image data
|
||||
let src_view = src_image.src_view();
|
||||
fn resize_lanczos3(src_pixels: &[u32], width: NonZeroU32, height: NonZeroU32) -> Vec<u8> {
|
||||
// Create immutable view of source image data
|
||||
let src_view = SrcImageView::from_pixels(width, height, src_pixels, PixelType::U8x4).unwrap();
|
||||
|
||||
let dst_width = NonZeroU32::new(1024).unwrap();
|
||||
let dst_height = NonZeroU32::new(768).unwrap();
|
||||
// Create wrapper that own data of destination image
|
||||
let mut dst_image = ImageData::new_owned(dst_width, dst_height, src_image.pixel_type());
|
||||
let mut dst_image = ImageData::new_owned(dst_width, dst_height, src_view.pixel_type());
|
||||
// Get mutable view of destination image data
|
||||
let mut dst_view = dst_image.dst_view();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user