1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use alloc::vec::Vec;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use miniz_oxide::deflate::compress_to_vec_zlib;
use miniz_oxide::inflate::decompress_to_vec_zlib;

use super::PngChunk;
use crate::encoder::{EncodeAt, ImageEncoder};
use crate::util::read_u8_len8_array;
use crate::{Error, ImageEXIF, ImageICC, Result};

// the 8 byte signature
pub(crate) const SIGNATURE: &[u8] = &[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];

pub const CHUNK_ICCP: [u8; 4] = [b'i', b'C', b'C', b'P'];
pub const CHUNK_EXIF: [u8; 4] = [b'e', b'X', b'I', b'f'];
pub const CHUNK_IEND: [u8; 4] = [b'I', b'E', b'N', b'D'];

/// The representation of a Png image
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Png {
    chunks: Vec<PngChunk>,
}

#[allow(clippy::len_without_is_empty)]
impl Png {
    /// Create a `Png` from `Bytes`
    ///
    /// # Errors
    ///
    /// This method fails if the file signature doesn't match or if
    /// it is corrupted or truncated.
    pub fn from_bytes(mut b: Bytes) -> Result<Png> {
        let signature: [u8; SIGNATURE.len()] = read_u8_len8_array(&mut b)?;
        if signature != SIGNATURE {
            return Err(Error::WrongSignature);
        }

        let mut chunks = Vec::with_capacity(8);
        while !b.is_empty() {
            let chunk = PngChunk::from_bytes(&mut b)?;

            // Often PNG images found in the internet contain garbage after IEND chunk.
            // Most PNG parsers simply ignore everything after IEND chunk
            let is_end = chunk.kind() == CHUNK_IEND;
            chunks.push(chunk);

            if is_end {
                break;
            }
        }

        Ok(Png { chunks })
    }

    /// Get the chunks of this `Png`
    #[inline]
    pub fn chunks(&self) -> &Vec<PngChunk> {
        &self.chunks
    }

    /// Get a mutable reference to the chunks of this `Png`
    #[inline]
    pub fn chunks_mut(&mut self) -> &mut Vec<PngChunk> {
        &mut self.chunks
    }

    /// Get the first chunk with a type of `kind`
    pub fn chunk_by_type(&self, kind: [u8; 4]) -> Option<&PngChunk> {
        self.chunks.iter().find(|chunk| chunk.kind() == kind)
    }

    /// Get every chunk with a type of `kind`
    pub fn chunks_by_type(&self, kind: [u8; 4]) -> impl Iterator<Item = &PngChunk> {
        self.chunks.iter().filter(move |chunk| chunk.kind() == kind)
    }

    /// Remove every chunk with a type of `kind`
    pub fn remove_chunks_by_type(&mut self, kind: [u8; 4]) {
        self.chunks_mut().retain(|chunk| chunk.kind() != kind);
    }

    /// Get the total size of the `Png` once it is encoded.
    ///
    /// The size is the sum of:
    ///
    /// - The signature (8 bytes).
    /// - The size of every chunk.
    pub fn len(&self) -> usize {
        // signature (8 bytes) + length of every chunk
        8 + self.chunks.iter().map(|chunk| chunk.len()).sum::<usize>()
    }

    /// Create an [encoder][crate::ImageEncoder] for this `Png`
    #[inline]
    pub fn encoder(self) -> ImageEncoder<Self> {
        ImageEncoder::from(self)
    }
}

impl EncodeAt for Png {
    fn encode_at(&self, pos: &mut usize) -> Option<Bytes> {
        match pos {
            0 => Some(Bytes::from_static(SIGNATURE)),
            _ => {
                *pos -= 1;

                for chunk in &self.chunks {
                    if let Some(bytes) = chunk.encode_at(pos) {
                        return Some(bytes);
                    }
                }

                None
            }
        }
    }

    fn len(&self) -> usize {
        self.len()
    }
}

// http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.iCCP
impl ImageICC for Png {
    fn icc_profile(&self) -> Option<Bytes> {
        let mut contents = self.chunk_by_type(CHUNK_ICCP)?.contents().clone();

        // skip nul-terminated profile name
        while contents.get_u8() != 0 {}

        // match on the compression method
        match contents.get_u8() {
            0 => decompress_to_vec_zlib(&contents).ok().map(Bytes::from),
            _ => None,
        }
    }

    fn set_icc_profile(&mut self, profile: Option<Bytes>) {
        self.remove_chunks_by_type(CHUNK_ICCP);

        if let Some(profile) = profile {
            let mut contents = BytesMut::with_capacity(profile.len());
            // profile name written as a C string
            contents.extend_from_slice(b"icc\0");
            // compression method
            contents.put_u8(0);
            // compressed profile
            let compressed = compress_to_vec_zlib(&profile, 10);
            contents.extend_from_slice(&compressed);

            let chunk = PngChunk::new(CHUNK_ICCP, contents.freeze());
            self.chunks.insert(1, chunk);
        }
    }
}

// https://ftp-osl.osuosl.org/pub/libpng/documents/pngext-1.5.0.html#C.eXIf
impl ImageEXIF for Png {
    fn exif(&self) -> Option<Bytes> {
        Some(self.chunk_by_type(CHUNK_EXIF)?.contents().clone())
    }

    fn set_exif(&mut self, exif: Option<Bytes>) {
        self.remove_chunks_by_type(CHUNK_EXIF);

        if let Some(exif) = exif {
            let chunk = PngChunk::new(CHUNK_EXIF, exif);
            self.chunks.insert(self.chunks.len() - 1, chunk);
        }
    }
}