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
//! Support for the [`image`][::image] crate.

use std::ops::Deref;

use crate::error::ImageDataError;
use crate::Alpha;
use crate::AsImageView;
use crate::BoxImage;
use crate::Image;
use crate::ImageInfo;
use crate::ImageView;
use crate::PixelFormat;
use crate::error::UnsupportedImageFormat;

impl AsImageView for image::DynamicImage {
	fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
		let info = dynamic_image_info(self)?;
		let data = dynamic_image_as_bytes(self);
		Ok(ImageView::new(info, data))
	}
}

impl AsImageView for &'_ image::DynamicImage {
	fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
		(*self).as_image_view()
	}
}

impl From<image::DynamicImage> for Image {
	fn from(other: image::DynamicImage) -> Self {
		let info = match dynamic_image_info(&other) {
			Ok(x) => x,
			Err(e) => return Self::Invalid(e),
		};
		let data = dynamic_image_into_bytes(other);
		BoxImage::new(info, data).into()
	}
}

impl<P, Container> AsImageView for image::ImageBuffer<P, Container>
where
	P: image::Pixel<Subpixel = u8> + image::PixelWithColorType,
	Container: Deref<Target = [u8]>,
{
	fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
		let info = info(self)?;
		let data = as_bytes(self);
		Ok(ImageView::new(info, data))
	}
}

impl<P, Container> AsImageView for &'_ image::ImageBuffer<P, Container>
where
	P: image::Pixel<Subpixel = u8> + image::PixelWithColorType,
	Container: Deref<Target = [u8]>,
{
	fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
		(*self).as_image_view()
	}
}

impl<P, Container> From<image::ImageBuffer<P, Container>> for Image
where
	P: image::Pixel<Subpixel = u8> + image::PixelWithColorType,
	Container: Deref<Target = [u8]>,
{
	fn from(other: image::ImageBuffer<P, Container>) -> Self {
		let info = match info(&other) {
			Ok(x) => x,
			Err(e) => return Self::Invalid(e),
		};
		let data = into_bytes(other);
		BoxImage::new(info, data).into()
	}
}

/// Consume an [`image::ImageBuffer`] and return the pixel data as boxed slice.
fn into_bytes<P, Container>(buffer: image::ImageBuffer<P, Container>) -> Box<[u8]>
where
	P: image::Pixel<Subpixel = u8> + image::PixelWithColorType,
	Container: Deref<Target = [u8]>,
{
	// TODO: Specialize this for Vec<u8> to avoid copying when
	// https://github.com/rust-lang/rust/issues/31844 lands in stable.
	Box::from(buffer.into_raw().deref())
}

fn dynamic_image_into_bytes(image: image::DynamicImage) -> Box<[u8]> {
	match image {
		image::DynamicImage::ImageLuma8(x) => into_bytes(x),
		image::DynamicImage::ImageLumaA8(x) => into_bytes(x),
		image::DynamicImage::ImageLuma16(_) => panic!("unsupported pixel format: Luma16"),
		image::DynamicImage::ImageLumaA16(_) => panic!("unsupported pixel format: LumaA16"),
		image::DynamicImage::ImageRgb8(x) => into_bytes(x),
		image::DynamicImage::ImageRgba8(x) => into_bytes(x),
		image::DynamicImage::ImageRgb16(_) => panic!("unsupported pixel format: Rgb16"),
		image::DynamicImage::ImageRgba16(_) => panic!("unsupported pixel format: Rgba16"),
		image::DynamicImage::ImageRgb32F(_) => panic!("unsupported pixel format: Rgb32F"),
		image::DynamicImage::ImageRgba32F(_) => panic!("unsupported pixel format: Rgba32F"),
		x => panic!("unsupported pixel format: {:?}", x),
	}
}

/// Get the pixel data of an [`image::ImageBuffer`] to as a byte slice.
fn as_bytes<P, Container>(buffer: &image::ImageBuffer<P, Container>) -> &[u8]
where
	P: image::Pixel<Subpixel = u8> + image::PixelWithColorType,
	Container: Deref<Target = [u8]>,
{
	buffer
}

fn dynamic_image_as_bytes(image: &image::DynamicImage) -> &[u8] {
	match image {
		image::DynamicImage::ImageLuma8(x) => as_bytes(x),
		image::DynamicImage::ImageLumaA8(x) => as_bytes(x),
		image::DynamicImage::ImageLuma16(_) => panic!("unsupported pixel format: Luma16"),
		image::DynamicImage::ImageLumaA16(_) => panic!("unsupported pixel format: LumaA16"),
		image::DynamicImage::ImageRgb8(x) => as_bytes(x),
		image::DynamicImage::ImageRgba8(x) => as_bytes(x),
		image::DynamicImage::ImageRgb16(_) => panic!("unsupported pixel format: Rgb16"),
		image::DynamicImage::ImageRgba16(_) => panic!("unsupported pixel format: Rgba16"),
		image::DynamicImage::ImageRgb32F(_) => panic!("unsupported pixel format: Rgb32F"),
		image::DynamicImage::ImageRgba32F(_) => panic!("unsupported pixel format: Rgba32F"),
		x => panic!("unsupported pixel format: {:?}", x),
	}
}

/// Extract the [`ImageInfo`] from an [`image::ImageBuffer`].
fn info<P, C>(image: &image::ImageBuffer<P, C>) -> Result<ImageInfo, ImageDataError>
where
	P: image::Pixel<Subpixel = u8> + image::PixelWithColorType,
	C: std::ops::Deref<Target = [u8]>,
{
	Ok(ImageInfo {
		pixel_format: pixel_format::<P>()?,
		size: glam::UVec2::new(image.width(), image.height()),
		stride: glam::UVec2::new(
			image.sample_layout().width_stride as u32,
			image.sample_layout().height_stride as u32,
		),
	})
}

fn dynamic_image_info(image: &image::DynamicImage) -> Result<ImageInfo, ImageDataError> {
	match image {
		image::DynamicImage::ImageLuma8(x) => info(x),
		image::DynamicImage::ImageLumaA8(x) => info(x),
		image::DynamicImage::ImageRgb8(x) => info(x),
		image::DynamicImage::ImageRgba8(x) => info(x),
		x => Err(UnsupportedImageFormat { format: format!("{:?}", x) }.into()),
	}
}

/// Extract the PixelFormat from an [`image::Pixel`].
fn pixel_format<P: image::PixelWithColorType>() -> Result<PixelFormat, ImageDataError> {
	match P::COLOR_TYPE {
		image::ExtendedColorType::L8 => Ok(PixelFormat::Mono8),
		image::ExtendedColorType::La8 => Ok(PixelFormat::MonoAlpha8(Alpha::Unpremultiplied)),
		image::ExtendedColorType::Rgb8 => Ok(PixelFormat::Rgb8),
		image::ExtendedColorType::Rgba8 => Ok(PixelFormat::Rgba8(Alpha::Unpremultiplied)),
		x => Err(UnsupportedImageFormat { format: format!("{:?}", x) }.into()),
	}
}