png/
chunk.rs

1//! Chunk types and functions
2#![allow(dead_code)]
3#![allow(non_upper_case_globals)]
4use core::fmt;
5
6#[derive(Clone, Copy, PartialEq, Eq, Hash)]
7pub struct ChunkType(pub [u8; 4]);
8
9// -- Critical chunks --
10
11/// Image header
12pub const IHDR: ChunkType = ChunkType(*b"IHDR");
13/// Palette
14pub const PLTE: ChunkType = ChunkType(*b"PLTE");
15/// Image data
16pub const IDAT: ChunkType = ChunkType(*b"IDAT");
17/// Image trailer
18pub const IEND: ChunkType = ChunkType(*b"IEND");
19
20// -- Ancillary chunks --
21
22/// Transparency
23pub const tRNS: ChunkType = ChunkType(*b"tRNS");
24/// Background colour
25pub const bKGD: ChunkType = ChunkType(*b"bKGD");
26/// Image last-modification time
27pub const tIME: ChunkType = ChunkType(*b"tIME");
28/// Physical pixel dimensions
29pub const pHYs: ChunkType = ChunkType(*b"pHYs");
30/// Source system's pixel chromaticities
31pub const cHRM: ChunkType = ChunkType(*b"cHRM");
32/// Source system's gamma value
33pub const gAMA: ChunkType = ChunkType(*b"gAMA");
34/// sRGB color space chunk
35pub const sRGB: ChunkType = ChunkType(*b"sRGB");
36/// ICC profile chunk
37pub const iCCP: ChunkType = ChunkType(*b"iCCP");
38/// Coding-independent code points for video signal type identification chunk
39pub const cICP: ChunkType = ChunkType(*b"cICP");
40/// Mastering Display Color Volume chunk
41pub const mDCV: ChunkType = ChunkType(*b"mDCV");
42/// Content Light Level Information chunk
43pub const cLLI: ChunkType = ChunkType(*b"cLLI");
44/// EXIF metadata chunk
45pub const eXIf: ChunkType = ChunkType(*b"eXIf");
46/// Latin-1 uncompressed textual data
47pub const tEXt: ChunkType = ChunkType(*b"tEXt");
48/// Latin-1 compressed textual data
49pub const zTXt: ChunkType = ChunkType(*b"zTXt");
50/// UTF-8 textual data
51pub const iTXt: ChunkType = ChunkType(*b"iTXt");
52// Significant bits
53pub const sBIT: ChunkType = ChunkType(*b"sBIT");
54
55// -- Extension chunks --
56
57/// Animation control
58pub const acTL: ChunkType = ChunkType(*b"acTL");
59/// Frame control
60pub const fcTL: ChunkType = ChunkType(*b"fcTL");
61/// Frame data
62pub const fdAT: ChunkType = ChunkType(*b"fdAT");
63
64// -- Chunk type determination --
65
66/// Returns true if the chunk is critical.
67pub fn is_critical(ChunkType(type_): ChunkType) -> bool {
68    type_[0] & 32 == 0
69}
70
71/// Returns true if the chunk is private.
72pub fn is_private(ChunkType(type_): ChunkType) -> bool {
73    type_[1] & 32 != 0
74}
75
76/// Checks whether the reserved bit of the chunk name is set.
77/// If it is set the chunk name is invalid.
78pub fn reserved_set(ChunkType(type_): ChunkType) -> bool {
79    type_[2] & 32 != 0
80}
81
82/// Returns true if the chunk is safe to copy if unknown.
83pub fn safe_to_copy(ChunkType(type_): ChunkType) -> bool {
84    type_[3] & 32 != 0
85}
86
87impl fmt::Debug for ChunkType {
88    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89        struct DebugType([u8; 4]);
90
91        impl fmt::Debug for DebugType {
92            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93                for &c in &self.0[..] {
94                    write!(f, "{}", char::from(c).escape_debug())?;
95                }
96                Ok(())
97            }
98        }
99
100        f.debug_struct("ChunkType")
101            .field("type", &DebugType(self.0))
102            .field("critical", &is_critical(*self))
103            .field("private", &is_private(*self))
104            .field("reserved", &reserved_set(*self))
105            .field("safecopy", &safe_to_copy(*self))
106            .finish()
107    }
108}