2022-12-02 09:11:42 +11:00
|
|
|
pub use image::ImageError;
|
2022-12-22 13:39:31 +11:00
|
|
|
use librashader_common::Size;
|
|
|
|
use std::marker::PhantomData;
|
2022-12-02 09:11:42 +11:00
|
|
|
|
2024-02-06 17:32:08 +11:00
|
|
|
use std::path::Path;
|
2022-11-17 16:08:11 +11:00
|
|
|
|
2023-01-14 10:23:31 +11:00
|
|
|
/// An uncompressed raw image ready to upload to GPU buffers.
|
2022-12-22 13:13:35 +11:00
|
|
|
pub struct Image<P: PixelFormat = RGBA8> {
|
2023-01-14 10:23:31 +11:00
|
|
|
/// The raw bytes of the image.
|
2022-11-17 16:08:11 +11:00
|
|
|
pub bytes: Vec<u8>,
|
2023-01-14 10:23:31 +11:00
|
|
|
/// The size dimensions of the image.
|
2022-11-26 18:38:15 +11:00
|
|
|
pub size: Size<u32>,
|
2023-01-14 10:23:31 +11:00
|
|
|
/// The byte pitch of the image.
|
2022-11-26 18:38:15 +11:00
|
|
|
pub pitch: usize,
|
2022-12-22 13:39:31 +11:00
|
|
|
_pd: PhantomData<P>,
|
2022-12-22 13:13:35 +11:00
|
|
|
}
|
|
|
|
|
2023-01-14 10:23:31 +11:00
|
|
|
/// R8G8B8A8 pixel format.
|
|
|
|
///
|
|
|
|
/// Every RGB with alpha pixel is represented with 32 bits.
|
2022-12-22 13:13:35 +11:00
|
|
|
pub struct RGBA8;
|
2023-01-14 10:23:31 +11:00
|
|
|
|
|
|
|
/// B8G8R8A8 pixel format.
|
|
|
|
///
|
|
|
|
/// Every BGR with alpha pixel is represented with 32 bits.
|
2022-12-22 13:13:35 +11:00
|
|
|
pub struct BGRA8;
|
|
|
|
|
2024-03-07 17:36:04 +11:00
|
|
|
/// A8R8G8B8 pixel format.
|
|
|
|
///
|
|
|
|
/// Every BGR with alpha pixel is represented with 32 bits.
|
|
|
|
pub struct ARGB8;
|
|
|
|
|
2023-01-14 10:23:31 +11:00
|
|
|
/// Represents an image pixel format to convert images into.
|
2022-12-22 13:13:35 +11:00
|
|
|
pub trait PixelFormat {
|
|
|
|
#[doc(hidden)]
|
|
|
|
fn convert(pixels: &mut Vec<u8>);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PixelFormat for RGBA8 {
|
2022-12-22 13:39:31 +11:00
|
|
|
fn convert(_pixels: &mut Vec<u8>) {}
|
2022-12-22 13:13:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PixelFormat for BGRA8 {
|
|
|
|
fn convert(pixels: &mut Vec<u8>) {
|
2024-08-21 07:29:39 +10:00
|
|
|
const BGRA_SWIZZLE: &[usize; 32] = &generate_swizzle([2, 1, 0, 3]);
|
|
|
|
swizzle_pixels(pixels, BGRA_SWIZZLE);
|
2022-12-22 13:13:35 +11:00
|
|
|
}
|
2022-11-17 16:08:11 +11:00
|
|
|
}
|
|
|
|
|
2024-03-07 17:36:04 +11:00
|
|
|
impl PixelFormat for ARGB8 {
|
|
|
|
fn convert(pixels: &mut Vec<u8>) {
|
2024-08-21 07:29:39 +10:00
|
|
|
const ARGB_SWIZZLE: &[usize; 32] = &generate_swizzle([3, 0, 1, 2]);
|
|
|
|
swizzle_pixels(pixels, ARGB_SWIZZLE);
|
2024-03-07 17:36:04 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:11:42 +11:00
|
|
|
/// The direction of UV coordinates to load the image for.
|
2022-12-01 14:50:57 +11:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum UVDirection {
|
2022-12-02 09:11:42 +11:00
|
|
|
/// Origin is at the top left (Direct3D, Vulkan)
|
2022-12-01 14:50:57 +11:00
|
|
|
TopLeft,
|
2022-12-02 09:11:42 +11:00
|
|
|
/// Origin is at the bottom left (OpenGL)
|
2022-12-01 14:50:57 +11:00
|
|
|
BottomLeft,
|
|
|
|
}
|
2022-12-02 09:11:42 +11:00
|
|
|
|
2022-12-22 13:13:35 +11:00
|
|
|
impl<P: PixelFormat> Image<P> {
|
2022-12-02 09:11:42 +11:00
|
|
|
/// Load the image from the path as RGBA8.
|
2022-12-01 14:50:57 +11:00
|
|
|
pub fn load(path: impl AsRef<Path>, direction: UVDirection) -> Result<Self, ImageError> {
|
|
|
|
let mut image = image::open(path.as_ref())?;
|
|
|
|
|
2022-12-02 09:11:42 +11:00
|
|
|
if direction == UVDirection::BottomLeft {
|
2022-12-01 14:50:57 +11:00
|
|
|
image = image.flipv();
|
|
|
|
}
|
|
|
|
|
|
|
|
let image = image.to_rgba8();
|
2022-11-21 18:13:22 +11:00
|
|
|
|
2022-11-17 16:08:11 +11:00
|
|
|
let height = image.height();
|
|
|
|
let width = image.width();
|
2022-11-30 17:38:05 +11:00
|
|
|
let pitch = image
|
|
|
|
.sample_layout()
|
|
|
|
.height_stride
|
|
|
|
.max(image.sample_layout().width_stride);
|
2022-11-22 08:21:50 +11:00
|
|
|
|
2022-12-22 13:13:35 +11:00
|
|
|
let mut bytes = image.into_raw();
|
|
|
|
P::convert(&mut bytes);
|
2022-11-17 16:08:11 +11:00
|
|
|
Ok(Image {
|
2022-12-22 13:13:35 +11:00
|
|
|
bytes,
|
2022-11-26 18:38:15 +11:00
|
|
|
pitch,
|
2022-11-30 17:38:05 +11:00
|
|
|
size: Size { height, width },
|
2022-12-22 13:13:35 +11:00
|
|
|
_pd: Default::default(),
|
2022-11-17 16:08:11 +11:00
|
|
|
})
|
|
|
|
}
|
2022-11-22 08:21:50 +11:00
|
|
|
}
|
2024-08-21 07:29:39 +10:00
|
|
|
|
|
|
|
fn swizzle_pixels(pixels: &mut Vec<u8>, swizzle: &'static [usize; 32]) {
|
|
|
|
assert!(pixels.len() % 4 == 0);
|
|
|
|
let mut chunks = pixels.chunks_exact_mut(32);
|
|
|
|
|
|
|
|
// This should vectorize faster than a naive mem swap
|
|
|
|
for chunk in &mut chunks {
|
|
|
|
let tmp = swizzle.map(|i| chunk[i]);
|
|
|
|
chunk.copy_from_slice(&tmp[..])
|
|
|
|
}
|
|
|
|
|
|
|
|
let remainder = chunks.into_remainder();
|
|
|
|
for chunk in remainder.chunks_exact_mut(4) {
|
|
|
|
let argb = [chunk[3], chunk[0], chunk[1], chunk[2]];
|
|
|
|
chunk.copy_from_slice(&argb[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn generate_swizzle<const LEN: usize>(swizzle: [usize; 4]) -> [usize; LEN] {
|
|
|
|
assert!(LEN % 4 == 0, "length of swizzle must be divisible by 4");
|
|
|
|
let mut out: [usize; LEN] = [0; LEN];
|
|
|
|
|
|
|
|
let mut index = 0;
|
|
|
|
while index < LEN {
|
|
|
|
let chunk = [index, index + 1, index + 2, index + 3];
|
|
|
|
out[index + 0] = chunk[swizzle[0]];
|
|
|
|
out[index + 1] = chunk[swizzle[1]];
|
|
|
|
out[index + 2] = chunk[swizzle[2]];
|
|
|
|
out[index + 3] = chunk[swizzle[3]];
|
|
|
|
|
|
|
|
index += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::image::generate_swizzle;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn generate_normal_swizzle() {
|
|
|
|
let swizzle = generate_swizzle::<32>([0, 1, 2, 3]);
|
|
|
|
assert_eq!(
|
|
|
|
swizzle,
|
|
|
|
#[rustfmt::skip]
|
|
|
|
[
|
|
|
|
0, 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
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn generate_argb_swizzle() {
|
|
|
|
let swizzle = generate_swizzle::<32>([3, 0, 1, 2]);
|
|
|
|
assert_eq!(
|
|
|
|
swizzle,
|
|
|
|
#[rustfmt::skip]
|
|
|
|
[
|
|
|
|
3, 0, 1, 2,
|
|
|
|
7, 4, 5, 6,
|
|
|
|
11, 8, 9, 10,
|
|
|
|
15, 12, 13, 14,
|
|
|
|
19, 16, 17, 18,
|
|
|
|
23, 20, 21, 22,
|
|
|
|
27, 24, 25, 26,
|
|
|
|
31, 28, 29, 30
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|