Skip to main content

elemental_core/auth/
texture.rs

1// Skin model inference is adapted from skinview-utils:
2// https://github.com/bs-community/skinview-utils/blob/master/src/process.ts
3// and HMCL:
4// https://github.com/HMCL-dev/HMCL/blob/main/HMCLCore/src/main/java/org/jackhuang/hmcl/util/skin/NormalizedSkin.java
5
6use serde::{Deserialize, Serialize};
7
8/// Texture kinds that can be read from or changed on a Minecraft profile.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "lowercase")]
11pub enum TextureType {
12    Skin,
13    Cape,
14}
15
16#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
17#[serde(rename_all = "lowercase")]
18pub enum SkinModel {
19    #[default]
20    Classic,
21    Slim,
22}
23
24/// A decoded skin texture in row-major RGBA8 format.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct SkinPixels<'a> {
27    pub width: u16,
28    pub height: u16,
29    /// Row-major RGBA8 pixels, `width * height * 4` bytes.
30    pub rgba: &'a [u8],
31}
32
33/// Unused arm pixels of a slim 64x64 skin.
34const SLIM_ARM_GAPS: [(u16, u16, u16, u16); 4] = [
35    (50, 16, 2, 4),
36    (54, 20, 2, 12),
37    (42, 48, 2, 4),
38    (46, 52, 2, 12),
39];
40
41impl SkinModel {
42    /// Infers the default model of an already-decoded skin texture.
43    ///
44    /// Supports the vanilla 64x32 and 64x64 textures. Returns `None` when the
45    /// dimensions or pixel buffer are unsupported. This is only a heuristic;
46    /// provider metadata and explicit user choices take precedence.
47    pub fn infer(texture: SkinPixels<'_>) -> Option<Self> {
48        let pixels = usize::from(texture.width).checked_mul(usize::from(texture.height))?;
49        if texture.rgba.len() != pixels.checked_mul(4)? {
50            return None;
51        }
52
53        match (texture.width, texture.height) {
54            (64, 32) => Some(Self::Classic),
55            (64, 64) => Some(infer_64x64(texture)),
56            _ => None,
57        }
58    }
59}
60
61#[derive(Clone, Copy)]
62struct GapSummary {
63    transparent: bool,
64    black: bool,
65    white: bool,
66}
67
68fn infer_64x64(texture: SkinPixels<'_>) -> SkinModel {
69    let gaps = SLIM_ARM_GAPS.map(|region| summarize_gap(texture, region));
70    let has_transparency = gaps.iter().any(|gap| gap.transparent);
71    let all_black = gaps.iter().all(|gap| gap.black);
72    let all_white = gaps.iter().all(|gap| gap.white);
73
74    if has_transparency || all_black || all_white {
75        SkinModel::Slim
76    } else {
77        SkinModel::Classic
78    }
79}
80
81fn summarize_gap(
82    texture: SkinPixels<'_>,
83    (x, y, width, height): (u16, u16, u16, u16),
84) -> GapSummary {
85    let stride = usize::from(texture.width) * 4;
86    let mut summary = GapSummary {
87        transparent: false,
88        black: true,
89        white: true,
90    };
91
92    for row in 0..usize::from(height) {
93        for column in 0..usize::from(width) {
94            let offset = (usize::from(y) + row) * stride + (usize::from(x) + column) * 4;
95            let red = texture.rgba[offset];
96            let green = texture.rgba[offset + 1];
97            let blue = texture.rgba[offset + 2];
98            let alpha = texture.rgba[offset + 3];
99
100            summary.transparent |= alpha != u8::MAX;
101            summary.black &= red == 0 && green == 0 && blue == 0 && alpha == u8::MAX;
102            summary.white &=
103                red == u8::MAX && green == u8::MAX && blue == u8::MAX && alpha == u8::MAX;
104        }
105    }
106
107    summary
108}
109
110#[cfg(test)]
111mod tests;
112
113#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
114pub struct Skin {
115    pub url: String,
116    pub model: SkinModel,
117}
118
119#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
120pub struct Cape {
121    /// Selectable cape identifier when the provider exposes a cape inventory.
122    pub id: Option<String>,
123    pub alias: Option<String>,
124    pub url: String,
125    pub active: bool,
126}
127
128#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
129pub struct ProfileTextures {
130    pub skin: Option<Skin>,
131    /// Capes available for selection. Yggdrasil reports only the active texture.
132    pub capes: Vec<Cape>,
133    /// Texture kinds this account accepts through upload.
134    pub uploadable: Vec<TextureType>,
135}
136
137impl ProfileTextures {
138    pub fn active_cape(&self) -> Option<&Cape> {
139        self.capes.iter().find(|cape| cape.active)
140    }
141}