fast image operations
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
//! indexed images! whoo! (palette and `Image<[u8], 1>`, basically.)
#![allow(private_bounds)]
mod builder;

use std::mem::MaybeUninit;

use crate::Image;

#[allow(non_camel_case_types)]
trait uint: Default + Copy + TryInto<usize> {
    fn nat(self) -> usize {
        self.try_into().ok().unwrap()
    }
}

macro_rules! int {
    ($($t:ty)+) => {
        $(impl uint for $t {})+
    };
}
int!(u8 u16 u32 u64 usize u128);

/// An image with a palette.
#[derive(Clone)]
pub struct IndexedImage<INDEX, PALETTE> {
    // likely Box<[u8]>, …
    // safety invariant: when INDEX<impl uint>, and PALETTE: Buffer, U must be < len(PALETTE)
    buffer: Image<INDEX, 1>,
    palette: PALETTE, // likely Box<[[f32; 4]]>, …
}

impl<I, P> IndexedImage<I, P> {
    /// Indexes the palette with each index.
    pub fn to<PIXEL: Copy, INDEX: uint, const CHANNELS: usize>(
        &self,
    ) -> Image<Box<[PIXEL]>, CHANNELS>
    where
        P: AsRef<[[PIXEL; CHANNELS]]>,
        I: AsRef<[INDEX]>,
    {
        unsafe {
            self.buffer.map(|x| {
                x.as_ref()
                    .iter()
                    // SAFETY: invariant.
                    .flat_map(|x| *self.palette.as_ref().get_unchecked(x.nat()))
                    .collect()
            })
        }
    }

    /// Sets the pixel. Does not check if the index is in bounds, nor if it is even a valid pixel.
    pub unsafe fn set_unchecked<INDEX: Copy>(&mut self, x: u32, y: u32, pixel: INDEX)
    where
        I: AsMut<[INDEX]>,
    {
        // SAFETY: they checked!
        unsafe { *self.pixel_mut(x, y) = pixel };
    }

    /// Gets a mutref to pixel. pls (is ub!) no set out of bound.
    pub unsafe fn pixel_mut<INDEX: Copy>(&mut self, x: u32, y: u32) -> &mut INDEX
    where
        I: AsMut<[INDEX]>,
    {
        let p = self.buffer.at(x, y);
        unsafe { self.raw().buffer.get_unchecked_mut(p) }
    }

    /// Sets the pixel. Panics if bounds are not met.
    pub fn set<INDEX: uint, PIXEL>(&mut self, x: u32, y: u32, pixel: INDEX)
    where
        I: AsMut<[INDEX]>,
        P: AsRef<[PIXEL]>,
    {
        assert!(self.buffer.width() < x);
        assert!(self.buffer.height() < x);
        assert!(pixel.nat() < self.palette.as_ref().len());
        // SAFETY: we checked!
        unsafe { self.set_unchecked(x, y, pixel) };
    }

    /// Gets a mut ref to raw parts.
    pub unsafe fn raw<INDEX>(&mut self) -> Image<&mut [INDEX], 1>
    where
        I: AsMut<[INDEX]>,
    {
        self.buffer.as_mut()
    }

    /// Provides the buffer and palette of this image.
    pub fn into_raw_parts(self) -> (Image<I, 1>, P) {
        (self.buffer, self.palette)
    }

    /// Creates a indexed image from its 1 channel image representation and palette.
    pub fn from_raw_parts<INDEX: uint, PIXEL>(
        buffer: Image<I, 1>,
        palette: P,
    ) -> Result<Self, &'static str>
    where
        I: AsRef<[INDEX]>,
        P: AsRef<[PIXEL]>,
    {
        let good = buffer.chunked().all(|[x]| x.nat() < palette.as_ref().len());
        good.then_some(Self { buffer, palette })
            .ok_or("not all indexes are in palette")
    }
}

impl<P, I: uint> IndexedImage<Box<[MaybeUninit<I>]>, P> {
    /// Assumes this MU image is, in fact, initialized. You must be very sure that it is. Also, none of the pixels can be out of bounds.
    pub unsafe fn assume_init(self) -> IndexedImage<Box<[I]>, P> {
        IndexedImage {
            // SAFETY: it really isnt.
            buffer: unsafe { self.buffer.mapped(|x| x.assume_init()) },
            ..self
        }
    }
}