mirror of
https://github.com/Cykooz/fast_image_resize.git
synced 2026-10-08 01:11:09 +00:00
Added method `ImageData<Vec<u32>>::from_buffer()`
This commit is contained in:
+29
-1
@@ -45,10 +45,16 @@ impl<T: AsRef<[u32]>> ImageData<T> {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_buffer(&self) -> &[u32] {
|
||||
pub fn get_pixels(&self) -> &[u32] {
|
||||
self.pixels.as_ref()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_buffer(&self) -> &[u8] {
|
||||
let (_, buffer, _) = unsafe { self.pixels.as_ref().align_to::<u8>() };
|
||||
buffer
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn src_view(&self) -> SrcImageView {
|
||||
let pixels = self.pixels.as_ref();
|
||||
@@ -77,4 +83,26 @@ impl ImageData<Vec<u32>> {
|
||||
pixel_type,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_buffer(
|
||||
width: NonZeroU32,
|
||||
height: NonZeroU32,
|
||||
buffer: &[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::<u32>() };
|
||||
if !head.is_empty() {
|
||||
return Err(ImageBufferError::InvalidBufferAlignment);
|
||||
}
|
||||
Ok(Self {
|
||||
width,
|
||||
height,
|
||||
pixels: pixels.to_owned(),
|
||||
pixel_type,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -82,14 +82,14 @@ impl<'a> SrcImageView<'a> {
|
||||
buffer: &'a [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::<u32>() };
|
||||
if !head.is_empty() {
|
||||
return Err(ImageBufferError::InvalidBufferAlignment);
|
||||
}
|
||||
let size = (width.get() * height.get()) as usize;
|
||||
if pixels.len() != size {
|
||||
return Err(ImageBufferError::InvalidBufferSize);
|
||||
}
|
||||
|
||||
let rows = pixels.chunks(width.get() as usize).collect();
|
||||
Ok(Self::from_rows(width, height, rows, pixel_type).unwrap())
|
||||
|
||||
+2
-11
@@ -1,5 +1,4 @@
|
||||
use fast_image_resize::{CpuExtensions, DstImageView, ImageData, MulDiv, PixelType, SrcImageView};
|
||||
use std::convert::TryInto;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
const fn p(r: u8, g: u8, b: u8, a: u8) -> u32 {
|
||||
@@ -46,11 +45,7 @@ fn multiply_alpha_test(cpu_extensions: CpuExtensions) {
|
||||
.multiply_alpha(&src_image_view, &mut dst_image_view)
|
||||
.unwrap();
|
||||
|
||||
let dst_pixels: Vec<u32> = dst_image
|
||||
.get_buffer()
|
||||
.chunks_exact(4)
|
||||
.map(|c| u32::from_le_bytes(c.try_into().unwrap()))
|
||||
.collect();
|
||||
let dst_pixels: Vec<u32> = dst_image.get_pixels().to_vec();
|
||||
let dst_rows = dst_pixels.chunks_exact(width as usize);
|
||||
for (row, &valid_pixel) in dst_rows.zip(res_pixels.iter()) {
|
||||
for &pixel in row.iter() {
|
||||
@@ -133,11 +128,7 @@ fn divide_alpha_test(cpu_extensions: CpuExtensions) {
|
||||
.divide_alpha(&src_image_view, &mut dst_image_view)
|
||||
.unwrap();
|
||||
|
||||
let dst_pixels: Vec<u32> = dst_image
|
||||
.get_buffer()
|
||||
.chunks_exact(4)
|
||||
.map(|c| u32::from_le_bytes(c.try_into().unwrap()))
|
||||
.collect();
|
||||
let dst_pixels: Vec<u32> = dst_image.get_pixels().to_vec();
|
||||
let dst_rows = dst_pixels.chunks_exact(width as usize);
|
||||
for (row, &valid_pixel) in dst_rows.zip(res_pixels.iter()) {
|
||||
for &pixel in row.iter() {
|
||||
|
||||
@@ -25,7 +25,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 {
|
||||
|
||||
+7
-17
@@ -15,16 +15,11 @@ fn get_source_image() -> ImageData<Vec<u32>> {
|
||||
.unwrap();
|
||||
let width = img.width();
|
||||
let height = img.height();
|
||||
let rgb = img.to_rgba8();
|
||||
let buf = rgb
|
||||
.as_raw()
|
||||
.chunks_exact(4)
|
||||
.map(|p| u32::from_le_bytes([p[0], p[1], p[2], p[3]]))
|
||||
.collect();
|
||||
ImageData::new(
|
||||
let rgba = img.to_rgba8();
|
||||
ImageData::from_buffer(
|
||||
NonZeroU32::new(width).unwrap(),
|
||||
NonZeroU32::new(height).unwrap(),
|
||||
buf,
|
||||
rgba.as_raw(),
|
||||
PixelType::U8x4,
|
||||
)
|
||||
.unwrap()
|
||||
@@ -37,16 +32,11 @@ fn get_small_source_image() -> ImageData<Vec<u32>> {
|
||||
.unwrap();
|
||||
let width = img.width();
|
||||
let height = img.height();
|
||||
let rgb = img.to_rgba8();
|
||||
let buf = rgb
|
||||
.as_raw()
|
||||
.chunks_exact(4)
|
||||
.map(|p| u32::from_le_bytes([p[0], p[1], p[2], p[3]]))
|
||||
.collect();
|
||||
ImageData::new(
|
||||
let rgba = img.to_rgba8();
|
||||
ImageData::from_buffer(
|
||||
NonZeroU32::new(width).unwrap(),
|
||||
NonZeroU32::new(height).unwrap(),
|
||||
buf,
|
||||
rgba.as_raw(),
|
||||
PixelType::U8x4,
|
||||
)
|
||||
.unwrap()
|
||||
@@ -115,7 +105,7 @@ fn resize_lanczos3(src_pixels: &[u32], width: NonZeroU32, height: NonZeroU32) ->
|
||||
let dst_height = NonZeroU32::new(768).unwrap();
|
||||
let mut dst_image = ImageData::new_owned(dst_width, dst_height, src_image.pixel_type());
|
||||
resizer.resize(&src_image.src_view(), &mut dst_image.dst_view());
|
||||
dst_image.get_buffer().to_owned()
|
||||
dst_image.get_pixels().to_owned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user