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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use super::PixelFormat;
use super::{
    buffer::Mapped,
    texture::{
        Filter, FilterMode, Texture, TextureInfo, TextureType, TextureUpdate, Wrap, WrapMode,
    },
    viewport::Viewport,
    Context,
};

#[derive(Copy, Clone, Debug)]
pub struct Settings {
    pub mipmaps: bool,
    pub dpi_scale: f32,
    pub slices: usize,
    pub filter: FilterMode,
    pub wrap: WrapMode,
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            mipmaps: true,
            dpi_scale: 1.,
            slices: 1,
            filter: FilterMode::Linear,
            wrap: WrapMode::Clamp,
        }
    }
}

#[derive(Clone, Debug)]
pub struct Image {
    texture_key: super::TextureKey,
    texture_info: TextureInfo,
    texture_type: TextureType,
}

impl Image {
    pub fn new(
        ctx: &mut Context,
        texture_type: TextureType,
        format: PixelFormat,
        width: u32,
        height: u32,
        settings: Settings,
    ) -> Result<Self, super::GraphicsError> {
        assert!(
            texture_type.is_supported(),
            "Unsupported Texture Type: {:?}",
            texture_type
        );
        let texture_key = ctx.new_texture(texture_type)?;
        let filter = Filter::new(
            settings.filter,
            settings.filter,
            if settings.mipmaps {
                settings.filter
            } else {
                FilterMode::None
            },
            0.,
        );
        let wrap = Wrap::new(settings.wrap, settings.wrap, settings.wrap);
        ctx.set_texture_filter(texture_key, texture_type, filter);
        ctx.set_texture_wrap(texture_key, texture_type, wrap);
        Ok(Self {
            texture_type,
            texture_key,
            texture_info: TextureInfo::new(
                format,
                (width as f32 * settings.dpi_scale + 0.5) as u32,
                (height as f32 * settings.dpi_scale + 0.5) as u32,
                filter,
                wrap,
                settings.mipmaps,
            ),
        })
    }

    pub fn with_data(
        ctx: &mut Context,
        texture_type: TextureType,
        format: PixelFormat,
        width: u32,
        height: u32,
        data: &[u8],
        settings: Settings,
    ) -> Result<Self, super::GraphicsError> {
        let this = Image::new(ctx, texture_type, format, width, height, settings)?;
        ctx.set_texture_data(
            this.texture_key,
            this.texture_info,
            this.texture_type,
            Some(data),
        );
        Ok(this)
    }

    #[cfg(target_arch = "wasm32")]
    pub fn with_html_image(
        ctx: &mut Context,
        texture_type: TextureType,
        format: PixelFormat,
        width: u32,
        height: u32,
        data: &web_sys::HtmlImageElement,
        settings: Settings,
    ) -> Result<Self, super::GraphicsError> {
        let this = Image::new(ctx, texture_type, format, width, height, settings)?;
        ctx.set_texture_data_with_html_image(&this, data);
        Ok(this)
    }

    pub fn set_texture_info(&mut self, texture_info: TextureInfo) {
        self.texture_info = texture_info;
    }
}

impl Texture for Image {
    fn get_texture_key(&self) -> super::TextureKey {
        self.texture_key
    }

    fn get_texture_type(&self) -> TextureType {
        self.texture_type
    }

    fn get_texture_info(&self) -> TextureInfo {
        self.texture_info
    }
}

impl Texture for &Image {
    fn get_texture_key(&self) -> super::TextureKey {
        Image::get_texture_key(self)
    }

    fn get_texture_type(&self) -> TextureType {
        Image::get_texture_type(self)
    }

    fn get_texture_info(&self) -> TextureInfo {
        Image::get_texture_info(self)
    }
}

pub type MappedImage = Mapped<Image, ndarray::Ix2>;

impl MappedImage {
    pub fn new(
        ctx: &mut Context,
        texture_type: TextureType,
        format: PixelFormat,
        width: u32,
        height: u32,
        settings: Settings,
    ) -> Result<Self, super::GraphicsError> {
        let inner = Image::new(ctx, texture_type, format, width, height, settings)?;
        let pixel_stride = super::gl::pixel_format::size(inner.texture_info.get_format());
        Ok(Self::with_shape(
            inner,
            [height as usize, width as usize * pixel_stride],
        ))
    }

    pub fn with_data(
        ctx: &mut Context,
        texture_type: TextureType,
        format: PixelFormat,
        width: u32,
        height: u32,
        data: Vec<u8>,
        settings: Settings,
    ) -> Result<Self, super::GraphicsError> {
        let inner = Image::with_data(ctx, texture_type, format, width, height, &data, settings)?;
        let pixel_stride = super::gl::pixel_format::size(inner.texture_info.get_format());
        Ok(Self {
            inner,
            memory_map: ndarray::Array2::from_shape_vec(
                [height as usize, width as usize * pixel_stride],
                data,
            )
            .unwrap(),
            modified_range: None,
        })
    }

    pub fn set_pixels(&mut self, region: Viewport<usize>, data: &[u8]) {
        let pixel_stride = self.pixel_stride();
        let (v_width, v_height) = region.dimensions();
        let (x1, y1) = region.position();
        let (x1, y1) = (x1 * pixel_stride, y1);
        let (x2, y2) = (x1 + v_width * pixel_stride, y1 + v_height);
        assert_eq!(v_width * v_height * pixel_stride, data.len());
        let mut slice = self.memory_map.slice_mut(ndarray::s![y1..y2, x1..x2]);
        let data =
            ndarray::ArrayView2::from_shape([v_height, v_width * pixel_stride], data).unwrap();
        slice.assign(&data);
    }

    pub fn get_pixels(&self) -> &[u8] {
        self.memory_map()
    }

    pub fn get_pixel(&self, x: usize, y: usize) -> &[u8] {
        let pixel_stride = self.pixel_stride();
        let index = y * self.inner.texture_info.width() as usize * pixel_stride + x * pixel_stride;
        &self.memory_map()[index..(index + pixel_stride)]
    }

    pub fn set_pixel(&mut self, x: usize, y: usize, pixel: &[u8]) {
        let pixel_stride = self.pixel_stride();
        assert_eq!(pixel_stride, pixel.len());
        let region = Viewport::new(x, y, 1, 1);
        self.set_pixels(region, pixel)
    }

    pub fn pixel_stride(&self) -> usize {
        super::gl::pixel_format::size(self.inner.texture_info.get_format())
    }

    pub fn unmap(&mut self, ctx: &mut Context) -> &Image {
        // TODO, track modified range and texture sub data
        ctx.set_texture_data(
            self.inner.texture_key,
            self.inner.texture_info,
            self.inner.texture_type,
            Some(self.get_pixels()),
        );
        &self.inner
    }
}