Skip to main content

unityrs_texture/
format.rs

1// Ported from UnityPy 1.25.2 (MIT): UnityPy/enums/TextureFormat.py (the member names and values), and the output modes and block sizes of export/Texture2DConverter.py's CONV_TABLE and TEXTURE_FORMAT_BLOCK_SIZE_TABLE for the formats decoded here. See THIRD_PARTY_LICENSES.
2// Ported from Pillow 9.5.0 (MIT-CMU): the bits a pixel of each raw mode UnityPy names takes (src/libImaging/Unpack.c's unpackers table). See THIRD_PARTY_LICENSES.
3//! Texture formats: every value UnityPy 1.25.2 names ([`texture_format_name`]),
4//! the ones this crate decodes ([`Format`]), of which version 1 of the batch
5//! engine exports five ([`Format::exported`]), and the image modes they
6//! decode to ([`Mode`]).
7
8mod names;
9pub use names::{TEXTURE_FORMATS, texture_format_name};
10mod mode;
11pub use mode::Mode;
12
13/// The formats this crate decodes: the five of `docs/plans/extract.md` E15
14/// and the rest of the formats UnityPy decodes with Pillow's raw and `bcn`
15/// decoders (`raw`'s and `bcn`'s module docs), the eight ETC and EAC
16/// formats texture2ddecoder decodes for it (`etc`'s module docs), and the
17/// twelve ASTC LDR formats astcenc decodes for it (`astc`'s module docs),
18/// the four PVRTC and two ATC formats (`pvrtc` and `atc`),
19/// and twenty-two raw formats using independent closed forms (`tierb`).
20/// `ArgbFloat` is a refusal-only sentinel, excluded from `ALL`.
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
22pub enum Format {
23    /// `Alpha8` (1): Pillow's raw `A` into `RGBA`.
24    Alpha8,
25    /// `ARGB4444` (2): Pillow's raw `RGBA;4B` into `RGBA`, its bands
26    /// reordered (2, 1, 0, 3).
27    Argb4444,
28    /// `RGB24` (3): Pillow's raw `RGB` into `RGB`.
29    Rgb24,
30    /// `RGBA32` (4): Pillow's raw `RGBA` into `RGBA`.
31    Rgba32,
32    /// `ARGB32` (5): Pillow's raw `ARGB` into `RGBA`.
33    Argb32,
34    /// `ARGBFloat` (6): refusal only; not in `ALL` or `Formats::DECODED`.
35    /// Its channel order is unknown (PARITY.md section 17.1, Tier U).
36    ArgbFloat,
37    /// `RGB565` (7): Pillow's raw `BGR;16` into `RGB`.
38    Rgb565,
39    /// `BGR24` (8): Pillow's raw `BGR` into `RGB`.
40    Bgr24,
41    /// `R16` (9): closed form of PARITY.md sections 17.1 and 19.
42    R16,
43    /// `DXT1` (10): Pillow's `bcn` 1 (BC1) into `RGBA`.
44    Dxt1,
45    /// `DXT3` (11): Pillow's `bcn` 2 (BC2) into `RGBA`.
46    Dxt3,
47    /// `DXT5` (12): Pillow's `bcn` 3 (BC3) into `RGBA`.
48    Dxt5,
49    /// `RGBA4444` (13): Pillow's raw `RGBA;4B` into `RGBA`, its bands
50    /// reordered (3, 2, 1, 0).
51    Rgba4444,
52    /// `BGRA32` (14): Pillow's raw `BGRA` into `RGBA`.
53    Bgra32,
54    /// `RHalf` (15): closed form of PARITY.md sections 17.1 and 19.
55    RHalf,
56    /// `RGHalf` (16): closed form of PARITY.md sections 17.1 and 19.
57    RgHalf,
58    /// `RGBAHalf` (17): closed form of PARITY.md sections 17.1 and 19.
59    RgbaHalf,
60    /// `RFloat` (18): closed form of PARITY.md sections 17.1 and 19.
61    RFloat,
62    /// `RGFloat` (19): closed form of PARITY.md sections 17.1 and 19.
63    RgFloat,
64    /// `RGBAFloat` (20): closed form of PARITY.md sections 17.1 and 19.
65    RgbaFloat,
66    /// `YUY2` (21): closed form of PARITY.md sections 17.1 and 19.
67    Yuy2,
68    /// `RGB9e5Float` (22): closed form of PARITY.md sections 17.1 and 19.
69    Rgb9e5Float,
70    /// `RGBFloat` (23): closed form of PARITY.md sections 17.1 and 19.
71    RgbFloat,
72    /// `BC6H` (24): specification-defined unsigned RGB half floats.
73    Bc6h,
74    /// `BC7` (25): Pillow's `bcn` 7 into `RGBA`.
75    Bc7,
76    /// `BC4` (26): Pillow's `bcn` 4 into `L`.
77    Bc4,
78    /// `BC5` (27): Pillow's `bcn` 5 into `RGB`.
79    Bc5,
80    /// `DXT1Crunched` (28): legacy CRN, then Pillow BC1; below Unity 2017.3.
81    Dxt1Crunched,
82    /// Unity 2017.3+ `DXT1Crunched` (28), explicitly selecting Unity's fork.
83    UnityDxt1Crunched,
84    /// `DXT5Crunched` (29): legacy CRN, then Pillow BC3; below Unity 2017.3.
85    Dxt5Crunched,
86    /// Unity 2017.3+ `DXT5Crunched` (29), explicitly selecting Unity's fork.
87    UnityDxt5Crunched,
88    /// `PVRTC_RGB2` (30): texture2ddecoder into `RGBA`.
89    PvrtcRgb2,
90    /// `PVRTC_RGBA2` (31): texture2ddecoder into `RGBA`.
91    PvrtcRgba2,
92    /// `PVRTC_RGB4` (32): texture2ddecoder into `RGBA`.
93    PvrtcRgb4,
94    /// `PVRTC_RGBA4` (33): texture2ddecoder into `RGBA`.
95    PvrtcRgba4,
96    /// `ETC_RGB4` (34): texture2ddecoder's ETC1 into `RGBA`.
97    EtcRgb4,
98    /// `ATC_RGB4` (35): texture2ddecoder into `RGBA`.
99    AtcRgb4,
100    /// `ATC_RGBA8` (36): texture2ddecoder into `RGBA`.
101    AtcRgba8,
102    /// `EAC_R` (41): texture2ddecoder's EAC R into `RGBA`.
103    EacR,
104    /// `EAC_R_SIGNED` (42): texture2ddecoder's signed EAC R into `RGBA`.
105    EacRSigned,
106    /// `EAC_RG` (43): texture2ddecoder's EAC RG into `RGBA`.
107    EacRg,
108    /// `EAC_RG_SIGNED` (44): texture2ddecoder's signed EAC RG into `RGBA`.
109    EacRgSigned,
110    /// `ETC2_RGB` (45): texture2ddecoder's ETC2 into `RGBA`.
111    Etc2Rgb,
112    /// `ETC2_RGBA1` (46): texture2ddecoder's ETC2 A1 into `RGBA`, with
113    /// its transparent pixels (0, 0, 0, 0) (Tier C, `PARITY.md` section
114    /// 19.1).
115    Etc2Rgba1,
116    /// `ETC2_RGBA8` (47): texture2ddecoder's ETC2 A8 into `RGBA`.
117    Etc2Rgba8,
118    /// `ASTC_RGB_4x4` (48): astcenc's LDR decode of 4x4 blocks into
119    /// `RGBA` (the `astc` module).
120    AstcRgb4x4,
121    /// `ASTC_RGB_5x5` (49): astcenc's LDR decode of 5x5 blocks into
122    /// `RGBA` (the `astc` module).
123    AstcRgb5x5,
124    /// `ASTC_RGB_6x6` (50): astcenc's LDR decode of 6x6 blocks into
125    /// `RGBA` (the `astc` module).
126    AstcRgb6x6,
127    /// `ASTC_RGB_8x8` (51): astcenc's LDR decode of 8x8 blocks into
128    /// `RGBA` (the `astc` module).
129    AstcRgb8x8,
130    /// `ASTC_RGB_10x10` (52): astcenc's LDR decode of 10x10 blocks into
131    /// `RGBA` (the `astc` module).
132    AstcRgb10x10,
133    /// `ASTC_RGB_12x12` (53): astcenc's LDR decode of 12x12 blocks into
134    /// `RGBA` (the `astc` module).
135    AstcRgb12x12,
136    /// `ASTC_RGBA_4x4` (54): astcenc's LDR decode of 4x4 blocks into
137    /// `RGBA` (the `astc` module).
138    AstcRgba4x4,
139    /// `ASTC_RGBA_5x5` (55): astcenc's LDR decode of 5x5 blocks into
140    /// `RGBA` (the `astc` module).
141    AstcRgba5x5,
142    /// `ASTC_RGBA_6x6` (56): astcenc's LDR decode of 6x6 blocks into
143    /// `RGBA` (the `astc` module).
144    AstcRgba6x6,
145    /// `ASTC_RGBA_8x8` (57): astcenc's LDR decode of 8x8 blocks into
146    /// `RGBA` (the `astc` module).
147    AstcRgba8x8,
148    /// `ASTC_RGBA_10x10` (58): astcenc's LDR decode of 10x10 blocks into
149    /// `RGBA` (the `astc` module).
150    AstcRgba10x10,
151    /// `ASTC_RGBA_12x12` (59): astcenc's LDR decode of 12x12 blocks into
152    /// `RGBA` (the `astc` module).
153    AstcRgba12x12,
154    /// `RG16` (62): closed form of PARITY.md sections 17.1 and 19.
155    Rg16,
156    /// `R8` (63): Pillow's raw `R` into `RGB`.
157    R8,
158    /// `ETC_RGB4Crunched` (64): Unity fork, then ETC1.
159    EtcRgb4Crunched,
160    /// `ETC2_RGBA8Crunched` (65): Unity fork, then ETC2 A8.
161    Etc2Rgba8Crunched,
162    /// `ASTC_HDR_4x4`: astcenc's HDR profile, then the half rule.
163    AstcHdr4x4,
164    /// `ASTC_HDR_5x5`: astcenc's HDR profile, then the half rule.
165    AstcHdr5x5,
166    /// `ASTC_HDR_6x6`: astcenc's HDR profile, then the half rule.
167    AstcHdr6x6,
168    /// `ASTC_HDR_8x8`: astcenc's HDR profile, then the half rule.
169    AstcHdr8x8,
170    /// `ASTC_HDR_10x10`: astcenc's HDR profile, then the half rule.
171    AstcHdr10x10,
172    /// `ASTC_HDR_12x12`: astcenc's HDR profile, then the half rule.
173    AstcHdr12x12,
174    /// `RG32` (72): closed form of PARITY.md sections 17.1 and 19.
175    Rg32,
176    /// `RGB48` (73): closed form of PARITY.md sections 17.1 and 19.
177    Rgb48,
178    /// `RGBA64` (74): closed form of PARITY.md sections 17.1 and 19.
179    Rgba64,
180    /// `R8_SIGNED` (75): closed form of PARITY.md sections 17.1 and 19.
181    R8Signed,
182    /// `RG16_SIGNED` (76): closed form of PARITY.md sections 17.1 and 19.
183    Rg16Signed,
184    /// `RGB24_SIGNED` (77): closed form of PARITY.md sections 17.1 and 19.
185    Rgb24Signed,
186    /// `RGBA32_SIGNED` (78): closed form of PARITY.md sections 17.1 and 19.
187    Rgba32Signed,
188    /// `R16_SIGNED` (79): closed form of PARITY.md sections 17.1 and 19.
189    R16Signed,
190    /// `RG32_SIGNED` (80): closed form of PARITY.md sections 17.1 and 19.
191    Rg32Signed,
192    /// `RGB48_SIGNED` (81): closed form of PARITY.md sections 17.1 and 19.
193    Rgb48Signed,
194    /// `RGBA64_SIGNED` (82): closed form of PARITY.md sections 17.1 and 19.
195    Rgba64Signed,
196}
197
198const _: () = crate::send_sync::<Format>();
199
200/// The library that decodes a [`Format`] for UnityPy, which decides how the
201/// data's length is read and which of Pillow's limits apply
202/// (`decode`'s module docs).
203#[derive(Clone, Copy, Debug, PartialEq, Eq)]
204pub(crate) enum Library {
205    /// Pillow's raw decoder, on the texture's own size.
206    PillowRaw,
207    /// Pillow's `bcn` decoder, on the size padded to whole 4x4 blocks.
208    PillowBcn,
209    /// texture2ddecoder, on the size padded to whole 4x4 blocks, its
210    /// output then read by Pillow's raw decoder as `BGRA`.
211    Texture2DDecoder,
212    /// astcenc (astc-encoder-py), on the size padded to whole blocks, its
213    /// output then read by Pillow's raw decoder as `RGBA`.
214    Astcenc,
215    /// Independent closed forms, with no Pillow raw decoder or its width cap.
216    ClosedForm,
217}
218
219impl Format {
220    /// One variant per decoded `TextureFormat` value, in numeric order.
221    /// DXT entries select legacy CRN;
222    /// `select_in` and `select_crunch` choose Unity fork variants at Unity 2017.3+.
223    pub const ALL: [Self; 75] = [
224        Self::Alpha8,
225        Self::Argb4444,
226        Self::Rgb24,
227        Self::Rgba32,
228        Self::Argb32,
229        Self::Rgb565,
230        Self::Bgr24,
231        Self::R16,
232        Self::Dxt1,
233        Self::Dxt3,
234        Self::Dxt5,
235        Self::Rgba4444,
236        Self::Bgra32,
237        Self::RHalf,
238        Self::RgHalf,
239        Self::RgbaHalf,
240        Self::RFloat,
241        Self::RgFloat,
242        Self::RgbaFloat,
243        Self::Yuy2,
244        Self::Rgb9e5Float,
245        Self::RgbFloat,
246        Self::Bc6h,
247        Self::Bc7,
248        Self::Bc4,
249        Self::Bc5,
250        Self::Dxt1Crunched,
251        Self::Dxt5Crunched,
252        Self::PvrtcRgb2,
253        Self::PvrtcRgba2,
254        Self::PvrtcRgb4,
255        Self::PvrtcRgba4,
256        Self::EtcRgb4,
257        Self::AtcRgb4,
258        Self::AtcRgba8,
259        Self::EacR,
260        Self::EacRSigned,
261        Self::EacRg,
262        Self::EacRgSigned,
263        Self::Etc2Rgb,
264        Self::Etc2Rgba1,
265        Self::Etc2Rgba8,
266        Self::AstcRgb4x4,
267        Self::AstcRgb5x5,
268        Self::AstcRgb6x6,
269        Self::AstcRgb8x8,
270        Self::AstcRgb10x10,
271        Self::AstcRgb12x12,
272        Self::AstcRgba4x4,
273        Self::AstcRgba5x5,
274        Self::AstcRgba6x6,
275        Self::AstcRgba8x8,
276        Self::AstcRgba10x10,
277        Self::AstcRgba12x12,
278        Self::Rg16,
279        Self::R8,
280        Self::EtcRgb4Crunched,
281        Self::Etc2Rgba8Crunched,
282        Self::AstcHdr4x4,
283        Self::AstcHdr5x5,
284        Self::AstcHdr6x6,
285        Self::AstcHdr8x8,
286        Self::AstcHdr10x10,
287        Self::AstcHdr12x12,
288        Self::Rg32,
289        Self::Rgb48,
290        Self::Rgba64,
291        Self::R8Signed,
292        Self::Rg16Signed,
293        Self::Rgb24Signed,
294        Self::Rgba32Signed,
295        Self::R16Signed,
296        Self::Rg32Signed,
297        Self::Rgb48Signed,
298        Self::Rgba64Signed,
299    ];
300
301    /// The five version 1 of the batch engine exports
302    /// (`docs/BATCH_SPEC.md` section 3), in `TextureFormat` order.
303    pub const EXPORTED: [Self; 5] = [Self::Rgb24, Self::Rgba32, Self::Dxt1, Self::Dxt5, Self::Bc7];
304
305    /// The canonical format of a decoded `TextureFormat` value. DXT crunch
306    /// entries name legacy input; use [`crate::select_in`] with the Unity
307    /// version to choose the decoder. Both variants have Unity's same name.
308    #[must_use]
309    pub const fn from_texture_format(value: i32) -> Option<Self> {
310        match value {
311            9 => Some(Self::R16),
312            15 => Some(Self::RHalf),
313            16 => Some(Self::RgHalf),
314            17 => Some(Self::RgbaHalf),
315            18 => Some(Self::RFloat),
316            19 => Some(Self::RgFloat),
317            20 => Some(Self::RgbaFloat),
318            21 => Some(Self::Yuy2),
319            22 => Some(Self::Rgb9e5Float),
320            23 => Some(Self::RgbFloat),
321            62 => Some(Self::Rg16),
322            72 => Some(Self::Rg32),
323            73 => Some(Self::Rgb48),
324            74 => Some(Self::Rgba64),
325            75 => Some(Self::R8Signed),
326            76 => Some(Self::Rg16Signed),
327            77 => Some(Self::Rgb24Signed),
328            78 => Some(Self::Rgba32Signed),
329            79 => Some(Self::R16Signed),
330            80 => Some(Self::Rg32Signed),
331            81 => Some(Self::Rgb48Signed),
332            82 => Some(Self::Rgba64Signed),
333
334            1 => Some(Self::Alpha8),
335            2 => Some(Self::Argb4444),
336            3 => Some(Self::Rgb24),
337            4 => Some(Self::Rgba32),
338            5 => Some(Self::Argb32),
339            7 => Some(Self::Rgb565),
340            8 => Some(Self::Bgr24),
341            10 => Some(Self::Dxt1),
342            11 => Some(Self::Dxt3),
343            12 => Some(Self::Dxt5),
344            13 => Some(Self::Rgba4444),
345            14 => Some(Self::Bgra32),
346            24 => Some(Self::Bc6h),
347            66 => Some(Self::AstcHdr4x4),
348            67 => Some(Self::AstcHdr5x5),
349            68 => Some(Self::AstcHdr6x6),
350            69 => Some(Self::AstcHdr8x8),
351            70 => Some(Self::AstcHdr10x10),
352            71 => Some(Self::AstcHdr12x12),
353            25 => Some(Self::Bc7),
354            26 => Some(Self::Bc4),
355            27 => Some(Self::Bc5),
356            28 => Some(Self::Dxt1Crunched),
357            29 => Some(Self::Dxt5Crunched),
358            30 => Some(Self::PvrtcRgb2),
359            31 => Some(Self::PvrtcRgba2),
360            32 => Some(Self::PvrtcRgb4),
361            33 => Some(Self::PvrtcRgba4),
362            34 => Some(Self::EtcRgb4),
363            35 => Some(Self::AtcRgb4),
364            36 => Some(Self::AtcRgba8),
365            41 => Some(Self::EacR),
366            42 => Some(Self::EacRSigned),
367            43 => Some(Self::EacRg),
368            44 => Some(Self::EacRgSigned),
369            45 => Some(Self::Etc2Rgb),
370            46 => Some(Self::Etc2Rgba1),
371            47 => Some(Self::Etc2Rgba8),
372            48 => Some(Self::AstcRgb4x4),
373            49 => Some(Self::AstcRgb5x5),
374            50 => Some(Self::AstcRgb6x6),
375            51 => Some(Self::AstcRgb8x8),
376            52 => Some(Self::AstcRgb10x10),
377            53 => Some(Self::AstcRgb12x12),
378            54 => Some(Self::AstcRgba4x4),
379            55 => Some(Self::AstcRgba5x5),
380            56 => Some(Self::AstcRgba6x6),
381            57 => Some(Self::AstcRgba8x8),
382            58 => Some(Self::AstcRgba10x10),
383            59 => Some(Self::AstcRgba12x12),
384            63 => Some(Self::R8),
385            64 => Some(Self::EtcRgb4Crunched),
386            65 => Some(Self::Etc2Rgba8Crunched),
387            _ => None,
388        }
389    }
390
391    /// Its `TextureFormat` value.
392    #[must_use]
393    pub const fn texture_format(self) -> i32 {
394        match self {
395            Self::ArgbFloat => 6,
396            Self::R16 => 9,
397            Self::RHalf => 15,
398            Self::RgHalf => 16,
399            Self::RgbaHalf => 17,
400            Self::RFloat => 18,
401            Self::RgFloat => 19,
402            Self::RgbaFloat => 20,
403            Self::Yuy2 => 21,
404            Self::Rgb9e5Float => 22,
405            Self::RgbFloat => 23,
406            Self::Rg16 => 62,
407            Self::Rg32 => 72,
408            Self::Rgb48 => 73,
409            Self::Rgba64 => 74,
410            Self::R8Signed => 75,
411            Self::Rg16Signed => 76,
412            Self::Rgb24Signed => 77,
413            Self::Rgba32Signed => 78,
414            Self::R16Signed => 79,
415            Self::Rg32Signed => 80,
416            Self::Rgb48Signed => 81,
417            Self::Rgba64Signed => 82,
418
419            Self::Alpha8 => 1,
420            Self::Argb4444 => 2,
421            Self::Rgb24 => 3,
422            Self::Rgba32 => 4,
423            Self::Argb32 => 5,
424            Self::Rgb565 => 7,
425            Self::Bgr24 => 8,
426            Self::Dxt1 => 10,
427            Self::Dxt3 => 11,
428            Self::Dxt5 => 12,
429            Self::Rgba4444 => 13,
430            Self::Bgra32 => 14,
431            Self::Bc6h => 24,
432            Self::AstcHdr4x4 => 66,
433            Self::AstcHdr5x5 => 67,
434            Self::AstcHdr6x6 => 68,
435            Self::AstcHdr8x8 => 69,
436            Self::AstcHdr10x10 => 70,
437            Self::AstcHdr12x12 => 71,
438            Self::Bc7 => 25,
439            Self::Bc4 => 26,
440            Self::Bc5 => 27,
441            Self::Dxt1Crunched | Self::UnityDxt1Crunched => 28,
442            Self::Dxt5Crunched | Self::UnityDxt5Crunched => 29,
443            Self::PvrtcRgb2 => 30,
444            Self::PvrtcRgba2 => 31,
445            Self::PvrtcRgb4 => 32,
446            Self::PvrtcRgba4 => 33,
447            Self::EtcRgb4 => 34,
448            Self::AtcRgb4 => 35,
449            Self::AtcRgba8 => 36,
450            Self::EacR => 41,
451            Self::EacRSigned => 42,
452            Self::EacRg => 43,
453            Self::EacRgSigned => 44,
454            Self::Etc2Rgb => 45,
455            Self::Etc2Rgba1 => 46,
456            Self::Etc2Rgba8 => 47,
457            Self::R8 => 63,
458            Self::EtcRgb4Crunched => 64,
459            Self::Etc2Rgba8Crunched => 65,
460            Self::AstcRgb4x4 => 48,
461            Self::AstcRgb5x5 => 49,
462            Self::AstcRgb6x6 => 50,
463            Self::AstcRgb8x8 => 51,
464            Self::AstcRgb10x10 => 52,
465            Self::AstcRgb12x12 => 53,
466            Self::AstcRgba4x4 => 54,
467            Self::AstcRgba5x5 => 55,
468            Self::AstcRgba6x6 => 56,
469            Self::AstcRgba8x8 => 57,
470            Self::AstcRgba10x10 => 58,
471            Self::AstcRgba12x12 => 59,
472        }
473    }
474
475    /// Its `TextureFormat` member name.
476    #[must_use]
477    pub const fn name(self) -> &'static str {
478        match self {
479            Self::ArgbFloat => "ARGBFloat",
480            Self::R16 => "R16",
481            Self::RHalf => "RHalf",
482            Self::RgHalf => "RGHalf",
483            Self::RgbaHalf => "RGBAHalf",
484            Self::RFloat => "RFloat",
485            Self::RgFloat => "RGFloat",
486            Self::RgbaFloat => "RGBAFloat",
487            Self::Yuy2 => "YUY2",
488            Self::Rgb9e5Float => "RGB9e5Float",
489            Self::RgbFloat => "RGBFloat",
490            Self::Rg16 => "RG16",
491            Self::Rg32 => "RG32",
492            Self::Rgb48 => "RGB48",
493            Self::Rgba64 => "RGBA64",
494            Self::R8Signed => "R8_SIGNED",
495            Self::Rg16Signed => "RG16_SIGNED",
496            Self::Rgb24Signed => "RGB24_SIGNED",
497            Self::Rgba32Signed => "RGBA32_SIGNED",
498            Self::R16Signed => "R16_SIGNED",
499            Self::Rg32Signed => "RG32_SIGNED",
500            Self::Rgb48Signed => "RGB48_SIGNED",
501            Self::Rgba64Signed => "RGBA64_SIGNED",
502
503            Self::Alpha8 => "Alpha8",
504            Self::Argb4444 => "ARGB4444",
505            Self::Rgb24 => "RGB24",
506            Self::Rgba32 => "RGBA32",
507            Self::Argb32 => "ARGB32",
508            Self::Rgb565 => "RGB565",
509            Self::Bgr24 => "BGR24",
510            Self::Dxt1 => "DXT1",
511            Self::Dxt3 => "DXT3",
512            Self::Dxt5 => "DXT5",
513            Self::Rgba4444 => "RGBA4444",
514            Self::Bgra32 => "BGRA32",
515            Self::Bc6h => "BC6H",
516            Self::AstcHdr4x4 => "ASTC_HDR_4x4",
517            Self::AstcHdr5x5 => "ASTC_HDR_5x5",
518            Self::AstcHdr6x6 => "ASTC_HDR_6x6",
519            Self::AstcHdr8x8 => "ASTC_HDR_8x8",
520            Self::AstcHdr10x10 => "ASTC_HDR_10x10",
521            Self::AstcHdr12x12 => "ASTC_HDR_12x12",
522            Self::Bc7 => "BC7",
523            Self::Bc4 => "BC4",
524            Self::Bc5 => "BC5",
525            Self::Dxt1Crunched | Self::UnityDxt1Crunched => "DXT1Crunched",
526            Self::Dxt5Crunched | Self::UnityDxt5Crunched => "DXT5Crunched",
527            Self::PvrtcRgb2 => "PVRTC_RGB2",
528            Self::PvrtcRgba2 => "PVRTC_RGBA2",
529            Self::PvrtcRgb4 => "PVRTC_RGB4",
530            Self::PvrtcRgba4 => "PVRTC_RGBA4",
531            Self::EtcRgb4 => "ETC_RGB4",
532            Self::AtcRgb4 => "ATC_RGB4",
533            Self::AtcRgba8 => "ATC_RGBA8",
534            Self::EacR => "EAC_R",
535            Self::EacRSigned => "EAC_R_SIGNED",
536            Self::EacRg => "EAC_RG",
537            Self::EacRgSigned => "EAC_RG_SIGNED",
538            Self::Etc2Rgb => "ETC2_RGB",
539            Self::Etc2Rgba1 => "ETC2_RGBA1",
540            Self::Etc2Rgba8 => "ETC2_RGBA8",
541            Self::R8 => "R8",
542            Self::EtcRgb4Crunched => "ETC_RGB4Crunched",
543            Self::Etc2Rgba8Crunched => "ETC2_RGBA8Crunched",
544            Self::AstcRgb4x4 => "ASTC_RGB_4x4",
545            Self::AstcRgb5x5 => "ASTC_RGB_5x5",
546            Self::AstcRgb6x6 => "ASTC_RGB_6x6",
547            Self::AstcRgb8x8 => "ASTC_RGB_8x8",
548            Self::AstcRgb10x10 => "ASTC_RGB_10x10",
549            Self::AstcRgb12x12 => "ASTC_RGB_12x12",
550            Self::AstcRgba4x4 => "ASTC_RGBA_4x4",
551            Self::AstcRgba5x5 => "ASTC_RGBA_5x5",
552            Self::AstcRgba6x6 => "ASTC_RGBA_6x6",
553            Self::AstcRgba8x8 => "ASTC_RGBA_8x8",
554            Self::AstcRgba10x10 => "ASTC_RGBA_10x10",
555            Self::AstcRgba12x12 => "ASTC_RGBA_12x12",
556        }
557    }
558
559    /// Whether version 1 of the batch engine exports it: one of
560    /// [`Self::EXPORTED`], the five of `docs/plans/extract.md` E15
561    /// ([`crate::select()`]).
562    #[must_use]
563    pub const fn exported(self) -> bool {
564        matches!(
565            self,
566            Self::Rgb24 | Self::Rgba32 | Self::Dxt1 | Self::Dxt5 | Self::Bc7
567        )
568    }
569
570    /// The bytes one block holds, for a block format: 8 for DXT1, BC4,
571    /// PVRTC, `ATC_RGB4`, `ETC_RGB4`, `EAC_R`, `EAC_R_SIGNED`, `ETC2_RGB`
572    /// and `ETC2_RGBA1`, 16
573    /// for the rest, the ASTC formats' included (`calculate_astc_compressed_size`,
574    /// `export/Texture2DConverter.py:390-397`); `None` for the raw formats,
575    /// which UnityPy does not pad ([`Self::block_size`]).
576    #[must_use]
577    pub const fn block_bytes(self) -> Option<u64> {
578        match self {
579            Self::Dxt1
580            | Self::Dxt1Crunched
581            | Self::UnityDxt1Crunched
582            | Self::Bc4
583            | Self::PvrtcRgb2
584            | Self::PvrtcRgba2
585            | Self::PvrtcRgb4
586            | Self::PvrtcRgba4
587            | Self::AtcRgb4
588            | Self::EtcRgb4
589            | Self::EtcRgb4Crunched
590            | Self::EacR
591            | Self::EacRSigned
592            | Self::Etc2Rgb
593            | Self::Etc2Rgba1 => Some(8),
594            Self::Bc6h
595            | Self::AstcHdr4x4
596            | Self::AstcHdr5x5
597            | Self::AstcHdr6x6
598            | Self::AstcHdr8x8
599            | Self::AstcHdr10x10
600            | Self::AstcHdr12x12
601            | Self::AtcRgba8
602            | Self::Dxt3
603            | Self::Dxt5
604            | Self::Dxt5Crunched
605            | Self::UnityDxt5Crunched
606            | Self::Bc7
607            | Self::Bc5
608            | Self::EacRg
609            | Self::EacRgSigned
610            | Self::Etc2Rgba8
611            | Self::Etc2Rgba8Crunched
612            | Self::AstcRgb4x4
613            | Self::AstcRgb5x5
614            | Self::AstcRgb6x6
615            | Self::AstcRgb8x8
616            | Self::AstcRgb10x10
617            | Self::AstcRgb12x12
618            | Self::AstcRgba4x4
619            | Self::AstcRgba5x5
620            | Self::AstcRgba6x6
621            | Self::AstcRgba8x8
622            | Self::AstcRgba10x10
623            | Self::AstcRgba12x12 => Some(16),
624            Self::ArgbFloat
625            | Self::R16
626            | Self::RHalf
627            | Self::RgHalf
628            | Self::RgbaHalf
629            | Self::RFloat
630            | Self::RgFloat
631            | Self::RgbaFloat
632            | Self::Yuy2
633            | Self::Rgb9e5Float
634            | Self::RgbFloat
635            | Self::Rg16
636            | Self::Rg32
637            | Self::Rgb48
638            | Self::Rgba64
639            | Self::R8Signed
640            | Self::Rg16Signed
641            | Self::Rgb24Signed
642            | Self::Rgba32Signed
643            | Self::R16Signed
644            | Self::Rg32Signed
645            | Self::Rgb48Signed
646            | Self::Rgba64Signed
647            | Self::Alpha8
648            | Self::Argb4444
649            | Self::Rgb24
650            | Self::Rgba32
651            | Self::Argb32
652            | Self::Rgb565
653            | Self::Bgr24
654            | Self::Rgba4444
655            | Self::Bgra32
656            | Self::R8 => None,
657        }
658    }
659
660    /// The block a block format is padded to, width by height
661    /// (`get_compressed_image_size`, `export/Texture2DConverter.py:36-50`,
662    /// with `TEXTURE_FORMAT_BLOCK_SIZE_TABLE`, `:22-33`: an ASTC name's
663    /// own `WxH`, 4x4 for names starting `DXT`, `BC`, `ETC` or `EAC`);
664    /// PVRTC uses 8x4 at 2bpp and 4x4 at 4bpp.
665    /// `None` for raw formats and ATC, which UnityPy does not pad (ATC
666    /// still stores 4x4 blocks: [`Self::block_bytes`]).
667    #[must_use]
668    pub const fn block_size(self) -> Option<(u8, u8)> {
669        match self {
670            Self::AtcRgb4 | Self::AtcRgba8 => None,
671            Self::PvrtcRgb2 | Self::PvrtcRgba2 => Some((8, 4)),
672            Self::AstcRgb4x4 | Self::AstcRgba4x4 | Self::AstcHdr4x4 => Some((4, 4)),
673            Self::AstcRgb5x5 | Self::AstcRgba5x5 | Self::AstcHdr5x5 => Some((5, 5)),
674            Self::AstcRgb6x6 | Self::AstcRgba6x6 | Self::AstcHdr6x6 => Some((6, 6)),
675            Self::AstcRgb8x8 | Self::AstcRgba8x8 | Self::AstcHdr8x8 => Some((8, 8)),
676            Self::AstcRgb10x10 | Self::AstcRgba10x10 | Self::AstcHdr10x10 => Some((10, 10)),
677            Self::AstcRgb12x12 | Self::AstcRgba12x12 | Self::AstcHdr12x12 => Some((12, 12)),
678            _ if self.block_bytes().is_some() => Some((4, 4)),
679            _ => None,
680        }
681    }
682
683    /// The bits a pixel takes where Pillow's raw decoder reads the data:
684    /// its unpacker's for the raw mode UnityPy names (`CONV_TABLE`;
685    /// Pillow's `src/libImaging/Unpack.c`): 8 for `A` and `R`, 16 for
686    /// `RGBA;4B` and `BGR;16`, 24 for `RGB` and `BGR`, 32 for `RGBA`,
687    /// `BGRA` and `ARGB`; 32 for texture2ddecoder's output, which Pillow
688    /// reads as `BGRA`, and astcenc's, which it reads as `RGBA`
689    /// (`export/Texture2DConverter.py:356-366`); `None` for the `bcn` decoder, which has no such
690    /// reader. Pillow's raw decoder refuses a row of more than
691    /// `INT_MAX / bits - 7` pixels (`decode`'s module docs).
692    pub(crate) const fn raw_bits(self) -> Option<u32> {
693        match self.library() {
694            Library::PillowBcn | Library::ClosedForm => None,
695            Library::Texture2DDecoder | Library::Astcenc => Some(32),
696            Library::PillowRaw => Some(match self {
697                Self::Alpha8 | Self::R8 => 8,
698                Self::Argb4444 | Self::Rgba4444 | Self::Rgb565 => 16,
699                Self::Rgb24 | Self::Bgr24 => 24,
700                _ => 32,
701            }),
702        }
703    }
704
705    /// Whether this uses the corrected HDR profile.
706    #[must_use]
707    pub const fn is_astc_hdr(self) -> bool {
708        matches!(
709            self,
710            Self::AstcHdr4x4
711                | Self::AstcHdr5x5
712                | Self::AstcHdr6x6
713                | Self::AstcHdr8x8
714                | Self::AstcHdr10x10
715                | Self::AstcHdr12x12
716        )
717    }
718
719    /// The PVRTC modulation footprint, or `None` for another decoder.
720    pub(crate) const fn pvrtc_2bpp(self) -> Option<bool> {
721        match self {
722            Self::PvrtcRgb2 | Self::PvrtcRgba2 => Some(true),
723            Self::PvrtcRgb4 | Self::PvrtcRgba4 => Some(false),
724            _ => None,
725        }
726    }
727
728    /// Whether this format expects CRN rather than plain blocks.
729    /// Use `select_in` or `select_crunch` to choose the DXT variant for the Unity version.
730    #[must_use]
731    pub const fn is_crunched(self) -> bool {
732        matches!(self, Self::Dxt1Crunched | Self::Dxt5Crunched) || self.is_unity_crunched()
733    }
734
735    /// Whether the input uses Unity's fork of CRN.
736    #[must_use]
737    pub const fn is_unity_crunched(self) -> bool {
738        matches!(
739            self,
740            Self::UnityDxt1Crunched
741                | Self::UnityDxt5Crunched
742                | Self::EtcRgb4Crunched
743                | Self::Etc2Rgba8Crunched
744        )
745    }
746
747    /// The library that decodes it for UnityPy (`CONV_TABLE`).
748    pub(crate) const fn library(self) -> Library {
749        if crate::tierb::layout(self).is_some() || matches!(self, Self::ArgbFloat | Self::Bc6h) {
750            return Library::ClosedForm;
751        }
752        match self {
753            Self::Dxt1
754            | Self::Dxt3
755            | Self::Dxt5
756            | Self::Dxt1Crunched
757            | Self::UnityDxt1Crunched
758            | Self::Dxt5Crunched
759            | Self::UnityDxt5Crunched
760            | Self::Bc4
761            | Self::Bc5
762            | Self::Bc7 => Library::PillowBcn,
763            Self::PvrtcRgb2
764            | Self::PvrtcRgba2
765            | Self::PvrtcRgb4
766            | Self::PvrtcRgba4
767            | Self::AtcRgb4
768            | Self::AtcRgba8
769            | Self::EtcRgb4
770            | Self::EtcRgb4Crunched
771            | Self::EacR
772            | Self::EacRSigned
773            | Self::EacRg
774            | Self::EacRgSigned
775            | Self::Etc2Rgb
776            | Self::Etc2Rgba1
777            | Self::Etc2Rgba8
778            | Self::Etc2Rgba8Crunched => Library::Texture2DDecoder,
779            Self::AstcRgb4x4
780            | Self::AstcRgb5x5
781            | Self::AstcRgb6x6
782            | Self::AstcRgb8x8
783            | Self::AstcRgb10x10
784            | Self::AstcRgb12x12
785            | Self::AstcRgba4x4
786            | Self::AstcRgba5x5
787            | Self::AstcRgba6x6
788            | Self::AstcRgba8x8
789            | Self::AstcRgba10x10
790            | Self::AstcRgba12x12
791            | Self::AstcHdr4x4
792            | Self::AstcHdr5x5
793            | Self::AstcHdr6x6
794            | Self::AstcHdr8x8
795            | Self::AstcHdr10x10
796            | Self::AstcHdr12x12 => Library::Astcenc,
797            _ => Library::PillowRaw,
798        }
799    }
800}