texture_packer/texture/
mod.rs

1//! Traits for a texture and its pixel contents.
2pub use self::{memory_rgba8_texture::MemoryRGBA8Texture, sub_texture::SubTexture};
3use std::ops::{Deref, DerefMut};
4
5pub mod image_texture;
6pub mod memory_rgba8_texture;
7pub mod sub_texture;
8
9/// Describes a texture type.
10pub trait Texture {
11    /// Pixel type of this texture.
12    type Pixel: Pixel;
13
14    /// Get the width of this texture.
15    fn width(&self) -> u32;
16    /// Get the height of this texture.
17    fn height(&self) -> u32;
18    // TODO: Chanage return value to &-ptr
19    /// Get the pixel value at a specific coordinate.
20    fn get(&self, x: u32, y: u32) -> Option<Self::Pixel>;
21    /// Set the pixel value at a specific coordinate.
22    fn set(&mut self, x: u32, y: u32, val: Self::Pixel);
23
24    /// Get the pixel if it were transformed by a rotation.
25    fn get_rotated(&self, x: u32, y: u32) -> Option<Self::Pixel> {
26        let w = self.height();
27        self.get(y, w - x - 1)
28    }
29
30    /// Check if a column of the texture is transparent.
31    fn is_column_transparent(&self, col: u32) -> bool {
32        for y in 0..self.height() {
33            if let Some(p) = self.get(col, y) {
34                if !p.is_transparent() {
35                    return false;
36                }
37            }
38        }
39        true
40    }
41
42    /// Check if a row of the texture is transparent.
43    fn is_row_transparent(&self, row: u32) -> bool {
44        for x in 0..self.width() {
45            if let Some(p) = self.get(x, row) {
46                if !p.is_transparent() {
47                    return false;
48                }
49            }
50        }
51        true
52    }
53}
54
55/// Describes a pixel type.
56pub trait Pixel: Sized {
57    /// If the pixel is transparent.
58    fn is_transparent(&self) -> bool;
59    /// The transparent value for this pixel type.
60    fn transparency() -> Option<Self>;
61    /// Outline value for this pixel type.
62    fn outline() -> Self;
63}
64
65impl<P: Pixel> Texture for Box<dyn Texture<Pixel = P> + 'static> {
66    type Pixel = P;
67
68    fn width(&self) -> u32 {
69        self.deref().width()
70    }
71
72    fn height(&self) -> u32 {
73        self.deref().height()
74    }
75
76    fn get(&self, x: u32, y: u32) -> Option<P> {
77        self.deref().get(x, y)
78    }
79
80    fn set(&mut self, x: u32, y: u32, val: P) {
81        self.deref_mut().set(x, y, val);
82    }
83}