Skip to content

Commit

Permalink
Remove GenericImage<Pixel = Rgba<u8>> impl for DynamicImage
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia authored and HeroicKatora committed Oct 17, 2024
1 parent 0307a47 commit 065456c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 152 deletions.
12 changes: 9 additions & 3 deletions examples/concat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ use image::{GenericImage, GenericImageView, ImageBuffer, Pixel, Primitive};
/// cargo run --release --example concat
fn main() {
h_concat(&[
image::open("examples/concat/200x300.png").unwrap(),
image::open("examples/concat/300x300.png").unwrap(),
image::open("examples/concat/400x300.png").unwrap(),
image::open("examples/concat/200x300.png")
.unwrap()
.to_rgba8(),
image::open("examples/concat/300x300.png")
.unwrap()
.to_rgba8(),
image::open("examples/concat/400x300.png")
.unwrap()
.to_rgba8(),
])
.save("examples/concat/concatenated_900x300.png")
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/opening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::env;
use std::fs::File;
use std::path::Path;

use image::{GenericImageView, ImageFormat};
use image::ImageFormat;

fn main() {
let file = if env::args().count() == 2 {
Expand Down
4 changes: 3 additions & 1 deletion examples/tile/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use image::RgbaImage;

fn main() {
let mut img = RgbaImage::new(1920, 1080);
let tile = image::open("examples/scaleup/tinycross.png").unwrap();
let tile = image::open("examples/scaleup/tinycross.png")
.unwrap()
.to_rgba8();

image::imageops::tile(&mut img, &tile);
img.save("tiled_wallpaper.png").unwrap();
Expand Down
148 changes: 7 additions & 141 deletions src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ use crate::buffer_::{
ConvertBuffer, Gray16Image, GrayAlpha16Image, GrayAlphaImage, GrayImage, ImageBuffer,
Rgb16Image, RgbImage, Rgba16Image, RgbaImage,
};
use crate::color::{self, IntoColor};
use crate::color;
use crate::error::{ImageError, ImageResult, ParameterError, ParameterErrorKind};
use crate::flat::FlatSamples;
use crate::image::{GenericImage, GenericImageView, ImageDecoder, ImageEncoder, ImageFormat};
use crate::image::{ImageDecoder, ImageEncoder, ImageFormat};
use crate::image_reader::free_functions;
use crate::math::resize_dimensions;
use crate::metadata::Orientation;
use crate::traits::Pixel;
use crate::ImageReader;
use crate::{image, Luma, LumaA};
use crate::{imageops, ExtendedColorType};
Expand Down Expand Up @@ -704,6 +703,11 @@ impl DynamicImage {
dynamic_map!(*self, ref p, { p.height() })
}

/// Returns the dimensions of the underlying image
pub fn dimensions(&self) -> (u32, u32) {
(self.width(), self.height())
}

/// Return a grayscale version of this image.
/// Returns `Luma` images in most cases. However, for `f32` images,
/// this will return a grayscale `Rgb/Rgba` image instead.
Expand Down Expand Up @@ -1108,66 +1112,6 @@ impl From<ImageBuffer<LumaA<f32>, Vec<f32>>> for DynamicImage {
DynamicImage::ImageRgba32F(image.convert())
}
}

#[allow(deprecated)]
impl GenericImageView for DynamicImage {
type Pixel = color::Rgba<u8>; // TODO use f32 as default for best precision and unbounded color?

fn dimensions(&self) -> (u32, u32) {
dynamic_map!(*self, ref p, p.dimensions())
}

fn get_pixel(&self, x: u32, y: u32) -> color::Rgba<u8> {
dynamic_map!(*self, ref p, p.get_pixel(x, y).to_rgba().into_color())
}
}

#[allow(deprecated)]
impl GenericImage for DynamicImage {
fn put_pixel(&mut self, x: u32, y: u32, pixel: color::Rgba<u8>) {
match *self {
DynamicImage::ImageLuma8(ref mut p) => p.put_pixel(x, y, pixel.to_luma()),
DynamicImage::ImageLumaA8(ref mut p) => p.put_pixel(x, y, pixel.to_luma_alpha()),
DynamicImage::ImageRgb8(ref mut p) => p.put_pixel(x, y, pixel.to_rgb()),
DynamicImage::ImageRgba8(ref mut p) => p.put_pixel(x, y, pixel),
DynamicImage::ImageLuma16(ref mut p) => p.put_pixel(x, y, pixel.to_luma().into_color()),
DynamicImage::ImageLumaA16(ref mut p) => {
p.put_pixel(x, y, pixel.to_luma_alpha().into_color());
}
DynamicImage::ImageRgb16(ref mut p) => p.put_pixel(x, y, pixel.to_rgb().into_color()),
DynamicImage::ImageRgba16(ref mut p) => p.put_pixel(x, y, pixel.into_color()),
DynamicImage::ImageRgb32F(ref mut p) => p.put_pixel(x, y, pixel.to_rgb().into_color()),
DynamicImage::ImageRgba32F(ref mut p) => p.put_pixel(x, y, pixel.into_color()),
}
}

fn blend_pixel(&mut self, x: u32, y: u32, pixel: color::Rgba<u8>) {
match *self {
DynamicImage::ImageLuma8(ref mut p) => p.blend_pixel(x, y, pixel.to_luma()),
DynamicImage::ImageLumaA8(ref mut p) => p.blend_pixel(x, y, pixel.to_luma_alpha()),
DynamicImage::ImageRgb8(ref mut p) => p.blend_pixel(x, y, pixel.to_rgb()),
DynamicImage::ImageRgba8(ref mut p) => p.blend_pixel(x, y, pixel),
DynamicImage::ImageLuma16(ref mut p) => {
p.blend_pixel(x, y, pixel.to_luma().into_color());
}
DynamicImage::ImageLumaA16(ref mut p) => {
p.blend_pixel(x, y, pixel.to_luma_alpha().into_color());
}
DynamicImage::ImageRgb16(ref mut p) => p.blend_pixel(x, y, pixel.to_rgb().into_color()),
DynamicImage::ImageRgba16(ref mut p) => p.blend_pixel(x, y, pixel.into_color()),
DynamicImage::ImageRgb32F(ref mut p) => {
p.blend_pixel(x, y, pixel.to_rgb().into_color());
}
DynamicImage::ImageRgba32F(ref mut p) => p.blend_pixel(x, y, pixel.into_color()),
}
}

/// Do not use is function: It is unimplemented!
fn get_pixel_mut(&mut self, _: u32, _: u32) -> &mut color::Rgba<u8> {
unimplemented!()
}
}

impl Default for DynamicImage {
fn default() -> Self {
Self::ImageRgba8(Default::default())
Expand Down Expand Up @@ -1388,84 +1332,6 @@ mod test {
assert_eq!(image.color(), ColorType::Rgba16);
}

fn test_grayscale(mut img: super::DynamicImage, alpha_discarded: bool) {
use crate::image::{GenericImage, GenericImageView};
img.put_pixel(0, 0, crate::color::Rgba([255, 0, 0, 100]));
let expected_alpha = if alpha_discarded { 255 } else { 100 };
assert_eq!(
img.grayscale().get_pixel(0, 0),
crate::color::Rgba([54, 54, 54, expected_alpha])
);
}

fn test_grayscale_alpha_discarded(img: super::DynamicImage) {
test_grayscale(img, true);
}

fn test_grayscale_alpha_preserved(img: super::DynamicImage) {
test_grayscale(img, false);
}

#[test]
fn test_grayscale_luma8() {
test_grayscale_alpha_discarded(super::DynamicImage::new_luma8(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::L8));
}

#[test]
fn test_grayscale_luma_a8() {
test_grayscale_alpha_preserved(super::DynamicImage::new_luma_a8(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::La8));
}

#[test]
fn test_grayscale_rgb8() {
test_grayscale_alpha_discarded(super::DynamicImage::new_rgb8(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::Rgb8));
}

#[test]
fn test_grayscale_rgba8() {
test_grayscale_alpha_preserved(super::DynamicImage::new_rgba8(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::Rgba8));
}

#[test]
fn test_grayscale_luma16() {
test_grayscale_alpha_discarded(super::DynamicImage::new_luma16(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::L16));
}

#[test]
fn test_grayscale_luma_a16() {
test_grayscale_alpha_preserved(super::DynamicImage::new_luma_a16(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::La16));
}

#[test]
fn test_grayscale_rgb16() {
test_grayscale_alpha_discarded(super::DynamicImage::new_rgb16(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::Rgb16));
}

#[test]
fn test_grayscale_rgba16() {
test_grayscale_alpha_preserved(super::DynamicImage::new_rgba16(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::Rgba16));
}

#[test]
fn test_grayscale_rgb32f() {
test_grayscale_alpha_discarded(super::DynamicImage::new_rgb32f(1, 1));
test_grayscale_alpha_discarded(super::DynamicImage::new(1, 1, ColorType::Rgb32F));
}

#[test]
fn test_grayscale_rgba32f() {
test_grayscale_alpha_preserved(super::DynamicImage::new_rgba32f(1, 1));
test_grayscale_alpha_preserved(super::DynamicImage::new(1, 1, ColorType::Rgba32F));
}

#[test]
fn test_dynamic_image_default_implementation() {
// Test that structs wrapping a DynamicImage are able to auto-derive the Default trait
Expand Down
2 changes: 1 addition & 1 deletion src/imageops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ where
/// use image::{RgbaImage};
///
/// let mut img = RgbaImage::new(1920, 1080);
/// let tile = image::open("tile.png").unwrap();
/// let tile = image::open("tile.png").unwrap().to_rgba8();
///
/// image::imageops::tile(&mut img, &tile);
/// img.save("tiled_wallpaper.png").unwrap();
Expand Down
16 changes: 11 additions & 5 deletions src/imageops/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ where
#[cfg(test)]
mod tests {
use super::{resize, sample_bilinear, sample_nearest, FilterType};
use crate::{GenericImageView, ImageBuffer, RgbImage};
use crate::{ImageBuffer, RgbImage};
#[cfg(feature = "benchmarks")]
use test;

Expand All @@ -1079,16 +1079,20 @@ mod tests {
#[cfg(feature = "png")]
fn test_resize_same_size() {
use std::path::Path;
let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
let resize = img.resize(img.width(), img.height(), FilterType::Triangle);
let img = crate::open(Path::new("./examples/fractal.png"))
.unwrap()
.to_rgba8();
let resize = resize(&img, img.width(), img.height(), FilterType::Triangle);
assert!(img.pixels().eq(resize.pixels()))
}

#[test]
#[cfg(feature = "png")]
fn test_sample_bilinear() {
use std::path::Path;
let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
let img = crate::open(Path::new("./examples/fractal.png"))
.unwrap()
.to_rgb8();
assert!(sample_bilinear(&img, 0., 0.).is_some());
assert!(sample_bilinear(&img, 1., 0.).is_some());
assert!(sample_bilinear(&img, 0., 1.).is_some());
Expand All @@ -1107,7 +1111,9 @@ mod tests {
#[cfg(feature = "png")]
fn test_sample_nearest() {
use std::path::Path;
let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
let img = crate::open(Path::new("./examples/fractal.png"))
.unwrap()
.to_rgba8();
assert!(sample_nearest(&img, 0., 0.).is_some());
assert!(sample_nearest(&img, 1., 0.).is_some());
assert!(sample_nearest(&img, 0., 1.).is_some());
Expand Down

0 comments on commit 065456c

Please sign in to comment.