Skip to main content

gltf_reader/
image.rs

1use alloc::borrow::Cow;
2use ownable::IntoOwned;
3use serde::Deserialize;
4
5use crate::buffer::BufferView;
6use crate::{Extensions, Extras, Idx};
7
8/// glTF known mime types.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum MimeTypeEnum {
11    Jpeg,
12    Png,
13}
14
15/// The image's media type.
16#[derive(Clone, PartialEq, Eq, Deserialize, IntoOwned)]
17#[serde(transparent)]
18pub struct MimeType<'a>(#[serde(borrow)] pub Cow<'a, str>);
19
20impl core::fmt::Debug for MimeType<'_> {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        if let Some(e) = self.to_enum() {
23            e.fmt(f)
24        } else {
25            self.0.fmt(f)
26        }
27    }
28}
29
30impl MimeType<'_> {
31    pub const JPEG: Self = Self(Cow::Borrowed("image/jpeg"));
32    pub const PNG: Self = Self(Cow::Borrowed("image/png"));
33
34    pub fn to_enum(&self) -> Option<MimeTypeEnum> {
35        if *self == Self::JPEG {
36            return Some(MimeTypeEnum::Jpeg);
37        } else if *self == Self::PNG {
38            return Some(MimeTypeEnum::Png);
39        }
40
41        None
42    }
43}
44
45/// Image data used to create a texture.
46#[derive(Debug, Clone, Deserialize, IntoOwned)]
47pub struct Image<'a> {
48    /// The user-defined name of this object.
49    #[serde(borrow)]
50    pub name: Option<Cow<'a, str>>,
51
52    /// The URI (or IRI) of the image.
53    #[serde(borrow)]
54    pub uri: Option<Cow<'a, str>>,
55    /// The index of the buffer view.
56    #[serde(rename = "bufferView")]
57    pub buffer_view: Option<Idx<BufferView<'static>>>,
58    /// The image's media type.
59    #[serde(borrow)]
60    pub mime_type: Option<MimeType<'a>>,
61
62    #[serde(borrow)]
63    pub extensions: Option<Extensions<'a>>,
64    #[serde(borrow)]
65    pub extras: Option<Extras<'a>>,
66}