image is consumed

This commit is contained in:
azy 2025-02-01 18:47:48 +08:00
parent 9e1d9baaa4
commit 80e70bcc67

View File

@ -1,32 +1,38 @@
use image::DynamicImage;
use image::imageops::FilterType; use image::imageops::FilterType;
use image::DynamicImage;
pub const PIXEL: char = '▀' ; pub const PIXEL: char = '▀';
const FILTER: FilterType = FilterType::Triangle; const FILTER: FilterType = FilterType::Triangle;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Image { pub struct Image {
pub pixel_grid: Vec<Vec<(ImageColor, ImageColor)>> pub pixel_grid: Vec<Vec<(ImageColor, ImageColor)>>,
} }
impl Image { impl Image {
pub fn new(pixel_grid: Vec<Vec<(ImageColor, ImageColor)>>) -> Self { pub fn new(pixel_grid: Vec<Vec<(ImageColor, ImageColor)>>) -> Self {
Image { pixel_grid} Image { pixel_grid }
} }
pub fn from_dynamic_image(dynamic_image: DynamicImage, height: u32, width: u32) -> Self { pub fn from_dynamic_image(
dynamic_image: DynamicImage,
let image = if dynamic_image.height() > height || dynamic_image.width() > width { height: u32,
println!("{}", dynamic_image.height()); width: u32,
) -> Self {
let image = if dynamic_image.height() > height
|| dynamic_image.width() > width
{
dynamic_image.resize(width, height, FILTER) dynamic_image.resize(width, height, FILTER)
}else{ } else {
dynamic_image dynamic_image
}; };
let image = image.into_rgba8(); let image = image.into_rgba8();
let pixel_grid = image.rows() let pixel_grid = image
.rows()
.step_by(2) .step_by(2)
.zip(image.rows().skip(1).step_by(2)) .zip(image.rows().skip(1).step_by(2))
.map(|(row_1, row_2)| { .map(|(row_1, row_2)| {
row_1.zip(row_2) row_1
.map(|(pixel_1, pixel_2)| .zip(row_2)
.map(|(pixel_1, pixel_2)| {
( (
ImageColor { ImageColor {
r: pixel_1.0[0], r: pixel_1.0[0],
@ -39,18 +45,20 @@ impl Image {
g: pixel_2.0[1], g: pixel_2.0[1],
b: pixel_2.0[2], b: pixel_2.0[2],
a: pixel_1.0[3], a: pixel_1.0[3],
})) },
.collect::<Vec<(ImageColor,ImageColor)>>() )
}) })
.collect::<Vec<Vec<(ImageColor,ImageColor)>>>(); .collect::<Vec<(ImageColor, ImageColor)>>()
})
.collect::<Vec<Vec<(ImageColor, ImageColor)>>>();
Image::new(pixel_grid) Image::new(pixel_grid)
} }
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct ImageColor{ pub struct ImageColor {
pub r: u8, pub r: u8,
pub g: u8, pub g: u8,
pub b: u8, pub b: u8,
pub a: u8 pub a: u8,
} }