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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Pixelate  Copyright (C) 2019  Maciej Hirsz
//
// This file is part of Pixelate. This program comes with ABSOLUTELY NO WARRANTY;
// This is free software, and you are welcome to redistribute it under the
// conditions of the GNU General Public License version 3.0.
//
// You should have received a copy of the GNU General Public License
// along with Pixelate.  If not, see <http://www.gnu.org/licenses/>

//! ![Pixelate](https://raw.githubusercontent.com/maciejhirsz/pixelate/master/pixelate.png)
//! ## This logo is rendered with this crate
//!
//! It's only 861 bytes for a 720x128 image. [Check out the code](https://github.com/maciejhirsz/pixelate/blob/master/tests/lib.rs).
//!
//! ## Why is this even a thing?!
//!
//! Rendering up-scaled pixelated PNG files can be quite useful for things like:
//! + QR codes.
//! + Identicons like [Blockies](https://crates.io/crates/blockies).
//! + Pixel Art?
//!
//! Usually a PNG image data, before compression, is a bitmap that packs 3 or 4 bytes per pixel, depending whether transparency is present or not. For all of the cases above this is way more information than necessary.
//!
//! PNG supports an **indexed palette** format. Using the indexing palette makes it possible not only to pack a single pixel into a single byte, but for small palettes **a pixel can be 4, 2, or even just 1 _bit_**. The logo here is using 3 colors (black, transparent background and shadow), which allows Pixelate to produce a bitmap where each pixel takes only 2 bits.
//!
//! Not only does this produce smaller images after compression, smaller bitmap is also much faster to compress, as much as 10x for small palettes. This makes it ideal for rendering QR codes or identicons on the fly.

use std::io;
use std::iter::repeat;
use png::{BitDepth, ColorType, HasParameters};

/// Generic error type
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
    /// There has been an internal error when doing the PNG encoding
    PngEncoding,

    /// There has been an error writing the PNG to IO
    Io,

    /// The palette is over 256 colors
    PaletteTooBig,

    /// The palette is smaller than 2 colors (what's the point?)
    PaletteTooSmall,
}

impl From<png::EncodingError> for Error {
    fn from(err: png::EncodingError) -> Error {
        use png::EncodingError;

        match err {
            EncodingError::IoError(_) => Error::Io,
            EncodingError::Format(_) => Error::PngEncoding,
        }
    }
}

/// Pretty self descriptive
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Color {
    /// Red, Green, and Blue, in that order
    Rgb(u8, u8, u8),

    /// Red, Green, Blue, and Alpha, in that order
    Rgba(u8, u8, u8, u8),
}

pub const WHITE: Color = Color::Rgb(255, 255, 255);
pub const BLACK: Color = Color::Rgb(0, 0, 0);

pub struct Image<'a> {
    /// Palette of colors, up to 255 colors
    pub palette: &'a [Color],

    /// Unscaled pixels where each byte is a valid index into `palette`
    pub pixels: &'a [u8],

    /// Width of the unscaled image, `pixels` length must be divisible by `width`
    pub width: usize,

    /// Scale to render the image at
    pub scale: usize,
}

impl<'a> Image<'a> {
    /// Render the
    pub fn render<W: io::Write>(&self, writer: W) -> Result<(), Error> {
        if self.palette.len() > 256 {
            return Err(Error::PaletteTooBig);
        }

        if self.palette.len() < 2 {
            return Err(Error::PaletteTooSmall);
        }

        let bit_depth = self.bit_depth();
        let depth = bit_depth as usize;
        let pixels_in_byte = 8 / depth;

        let (img_width, img_height) = self.dimensions();
        let bytes_per_line = ceil_div(img_width, pixels_in_byte);

        let mut data = vec![0; bytes_per_line * img_height];

        let chunk_size = bytes_per_line * self.scale;

        for (chunk, pixels) in data.chunks_mut(chunk_size).zip(self.pixels.chunks(self.width)) {
            let (first_line, chunk) = chunk.split_at_mut(bytes_per_line);

            // Take the original row of pixels, repeat each pixel `scale` times
            let mut pixels = pixels.iter().flat_map(|pixel| repeat(*pixel).take(self.scale));

            // Rasterize the first row of pixels
            for byte in first_line.iter_mut() {
                // Pack as many pixels as necessary into the byte
                for (idx, pixel) in (&mut pixels).take(pixels_in_byte).enumerate() {
                    *byte |= pixel << (8 - depth) - (idx * depth);
                }
            }

            // Now repeat it until we've filled up the whole `scale`-tall section
            for row in chunk.chunks_mut(bytes_per_line) {
                row.copy_from_slice(first_line);
            }
        }

        let mut encoder = png::Encoder::new(writer, img_width as u32, img_height as u32);

        encoder.set(ColorType::Indexed).set(bit_depth);

        let mut writer = encoder.write_header()?;

        writer.write_chunk(png::chunk::PLTE, &self.palette_data())?;

        if let Some(transparency) = self.transparency() {
            writer.write_chunk(png::chunk::tRNS, &transparency)?;
        }

        writer.write_image_data(&data)?;

        Ok(())
    }

    fn dimensions(&self) -> (usize, usize) {
        let width = self.width * self.scale;
        let height = (self.pixels.len() / self.width) * self.scale;

        (width, height)
    }

    fn bit_depth(&self) -> BitDepth {
        match self.palette.len() as u8 {
            0...2  => BitDepth::One,
            3...4  => BitDepth::Two,
            5...16 => BitDepth::Four,
            _      => BitDepth::Eight,
        }
    }

    fn palette_data(&self) -> Vec<u8> {
        let mut data = Vec::with_capacity(self.palette.len() * 3);

        for color in self.palette.iter().cloned() {
            match color {
                Color::Rgb(r, g, b) | Color::Rgba(r, g, b, _) => data.extend_from_slice(&[r,g,b])
            }
        }

        data
    }

    fn transparency(&self) -> Option<Vec<u8>> {
        let mut data = None;

        let len = self.palette.len();

        for (idx, color) in self.palette.iter().enumerate() {
            let alpha = match color {
                Color::Rgb(_, _, _) => continue,
                Color::Rgba(_, _, _, alpha) => *alpha,
            };

            let mut buf = data.take().unwrap_or_else(|| vec![255; len]);

            buf[idx] = alpha;

            data = Some(buf);
        }

        data
    }
}

fn ceil_div(a: usize, b: usize) -> usize {
    a / b + (a % b != 0) as usize
}