Skip to main content

speedy2d/
image.rs

1/*
2 *  Copyright 2021 QuantumBadger
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16
17use crate::dimen::UVec2;
18use crate::glwrapper::GLTexture;
19
20/// The data type of the pixels making up the raw image data.
21#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
22pub enum ImageDataType
23{
24    /// Each pixel in the image is represented by three `u8` values: red, green,
25    /// and blue.
26    RGB,
27
28    /// Each pixel in the image is represented by four `u8` values: red, green,
29    /// blue, and alpha.
30    RGBA
31}
32
33/// Represents a handle for a loaded image.
34///
35/// Note: this handle can only be used in the graphics context in which it was
36/// created.
37#[derive(Debug, Hash, PartialEq, Eq, Clone)]
38pub struct ImageHandle
39{
40    pub(crate) size: UVec2,
41    pub(crate) texture: GLTexture
42}
43
44impl ImageHandle
45{
46    /// Returns the size of the image in pixels.
47    pub fn size(&self) -> &UVec2
48    {
49        &self.size
50    }
51}
52
53/// `ImageSmoothingMode` defines how images are rendered when the pixels of the
54/// source image don't align perfectly with the pixels of the screen. This could
55/// be because the image is a different size, or because it is rendered at a
56/// position which is a non-integer number of pixels.
57#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
58pub enum ImageSmoothingMode
59{
60    /// The pixel drawn on the screen will be the closest pixel from the source
61    /// image. This may cause aliasing/jagginess, so for a smoother result
62    /// the `Linear` mode may be more suitable.
63    NearestNeighbor,
64
65    /// The pixel drawn on the screen will be the weighted average of the four
66    /// nearest pixels in the source image. This produces a smoother result
67    /// than `NearestNeighbor`, but in cases where the image is intended to
68    /// be pixel-aligned it may cause unnecessary blurriness.
69    Linear
70}
71
72/// Supported image formats.
73///
74///  The following image formats are supported:
75///
76/// * `PNG`
77/// * `JPEG` (baseline and progressive)
78/// * `GIF`
79/// * `BMP`
80/// * `ICO`
81/// * `TIFF`: Baseline (no fax support) + LZW + PackBits
82/// * `WebP`: Lossy (luma channel only)
83/// * `AVIF`: Only 8-bit
84/// * `PNM`: PBM, PGM, PPM, standard PAM
85/// * `DDS`: DXT1, DXT3, DXT5
86/// * `TGA`
87/// * `farbfeld`
88#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
89#[allow(missing_docs)]
90pub enum ImageFileFormat
91{
92    PNG,
93    JPEG,
94    GIF,
95    BMP,
96    ICO,
97    TIFF,
98    WebP,
99    AVIF,
100    PNM,
101    DDS,
102    TGA,
103    Farbfeld
104}
105
106/// A type to represent some raw pixel data, with an associated width and height
107/// in pixels.
108#[derive(Clone)]
109pub struct RawBitmapData
110{
111    data: Vec<u8>,
112    size: UVec2,
113    format: ImageDataType
114}
115
116impl RawBitmapData
117{
118    pub(crate) fn new(
119        data: Vec<u8>,
120        size: impl Into<UVec2>,
121        format: ImageDataType
122    ) -> Self
123    {
124        Self {
125            data,
126            size: size.into(),
127            format
128        }
129    }
130
131    /// Returns a reference to the raw pixel data.
132    pub fn data(&self) -> &Vec<u8>
133    {
134        &self.data
135    }
136
137    /// Returns the width and height of this data in pixels.
138    pub fn size(&self) -> UVec2
139    {
140        self.size
141    }
142
143    /// Returns the format of this data.
144    pub fn format(&self) -> ImageDataType
145    {
146        self.format
147    }
148
149    /// Transfers ownership of the raw pixel data to the caller.
150    pub fn into_data(self) -> Vec<u8>
151    {
152        self.data
153    }
154}