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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

use std::str::FromStr;

use crate::error::IdenticonError;
use image::codecs::jpeg::JpegEncoder;
use image::codecs::png::PngEncoder;
use image::imageops::FilterType;
use image::{DynamicImage, GenericImage, ImageBuffer, ImageEncoder};
use sha3::{Digest, Sha3_256};

mod color;

/// Identicon errors.
pub mod error;
mod grid;
mod map_values;

/// Generic Identicon struct.
///
/// This is the base struct to be used.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Identicon {
    hash: Vec<u8>,
    border: u32,
    size: u32,
    scale: u32,
    background_color: (u8, u8, u8),
    mirrored: bool,
}

/// Generates a new identicon.
///
/// This is a wrapper around [`identicon_rs::Identicon::new`].
///
/// [`identicon_rs::Identicon::new`]: struct.Identicon.html#method.new
pub fn new(input_value: &str) -> Identicon {
    Identicon::new(input_value)
}

impl Identicon {
    /// Generates a new identicon from an input value.
    ///
    /// The defaults are:
    /// - border: 50
    /// - size: 5
    /// - scale: 500
    /// - background_color: (240, 240, 240)
    /// - mirrored: true
    pub fn new(input_value: &str) -> Self {
        let mut identicon = Self::default();
        identicon.set_input(input_value);
        identicon
    }

    /// Sets the identicon input value, regenerating the hash.
    pub fn set_input(&mut self, input_value: &str) -> &mut Self {
        self.hash = Self::hash_value(input_value);
        self
    }

    /// Gets the identicon border size.
    pub fn border(&self) -> u32 {
        self.border
    }

    /// Sets the identicon border size.
    ///
    /// Default is 5
    pub fn set_border(&mut self, border: u32) -> &mut Self {
        self.border = border;
        self
    }

    /// Gets the identicon size.
    ///
    /// The size represents the number of viewable blocks of the identicon.
    pub fn size(&self) -> u32 {
        self.size
    }

    /// Sets the number of viewable blocks of the identicon.
    ///
    /// This must be <= the scale.
    ///
    /// Default is 5, representing an identicon with a grid of 5x5.
    pub fn set_size(&mut self, size: u32) -> Result<&mut Self, IdenticonError> {
        if size <= self.scale {
            self.size = size;
            Ok(self)
        } else {
            Err(IdenticonError::SizeTooLargeError {
                size,
                scale: self.scale,
            })
        }
    }

    /// Gets the identicon scale.
    ///
    /// The scale represents the height and width of the identicon portion of any generated image.
    ///
    /// The full image size is: `scale + ( 2 * border )`
    pub fn scale(&self) -> u32 {
        self.scale
    }

    /// Sets the scale of the image.
    ///
    /// The full image size is: `scale + ( 2 * border )`
    ///
    /// This must be >= the size.
    pub fn set_scale(&mut self, scale: u32) -> Result<&mut Self, IdenticonError> {
        if scale >= self.size {
            self.scale = scale;
            Ok(self)
        } else {
            Err(IdenticonError::ScaleTooSmallError {
                scale,
                size: self.size,
            })
        }
    }

    /// Gets the identicon background color.
    pub fn background_color(&self) -> (u8, u8, u8) {
        self.background_color
    }

    /// Sets the background, non-active color of the identicon.
    ///
    /// This is a tuble of (red, green, blue) values.
    pub fn set_background_color(&mut self, background_color: (u8, u8, u8)) -> &mut Self {
        self.background_color = background_color;
        self
    }

    /// Gets if the identicon is mirrored.
    pub fn mirrored(&self) -> bool {
        self.mirrored
    }

    /// Sets whether the identicon is mirrored along the y axis.
    ///
    /// This is a boolean.
    pub fn set_mirrored(&mut self, mirrored: bool) -> &mut Self {
        self.mirrored = mirrored;
        self
    }

    fn hash_value(input_value: &str) -> Vec<u8> {
        let input_trimmed = input_value.trim();
        Sha3_256::digest(input_trimmed.as_bytes())
            .as_slice()
            .to_vec()
    }

    /// Generates the DynamicImage representing the Identicon.
    pub fn generate_image(&self) -> Result<DynamicImage, IdenticonError> {
        // Create a new grid
        let grid = grid::generate_full_grid(self.size, &self.hash);

        // Create pixel objects
        let color_active = color::generate_color(&self.hash);
        let pixel_active = image::Rgb([color_active.0, color_active.1, color_active.2]);
        let pixel_background = image::Rgb([
            self.background_color.0,
            self.background_color.1,
            self.background_color.2,
        ]);

        // Create image buffer from grid
        let image_buffer = ImageBuffer::from_fn(self.size, self.size, |x, y| {
            let x_location = if self.mirrored && x > self.size / 2 {
                self.size - x - 1
            } else {
                x
            };

            // Get location within the generated grid
            let grid_location = (x_location + y * self.size) % self.size.pow(2);

            // Set the pixel color based on the value within the grid at the given position
            if grid[grid_location as usize] {
                pixel_active
            } else {
                pixel_background
            }
        });

        let scaled_image_buffer = DynamicImage::ImageRgb8(image_buffer)
            .resize(self.scale, self.scale, FilterType::Nearest)
            .to_rgb8();

        let final_size = self.scale + (2 * self.border);
        let mut bordered_image_buffer =
            ImageBuffer::from_fn(final_size, final_size, |_, _| pixel_background);

        match bordered_image_buffer.copy_from(&scaled_image_buffer, self.border, self.border) {
            Ok(_) => Ok(DynamicImage::ImageRgb8(bordered_image_buffer)),
            Err(_) => Err(error::IdenticonError::GenerateImageError),
        }
    }

    /// Saves the generated image to the given filename.
    ///
    /// The file formats `.png`, `.jpg`, `.jpeg`, `.bmp`, and `.ico` work.
    pub fn save_image(&self, output_filename: &str) -> Result<(), error::IdenticonError> {
        let image = self.generate_image()?;
        image
            .save(output_filename)
            .map_err(|_| error::IdenticonError::SaveImageError)
    }

    /// Export a PNG file buffer as a Vec<u8>.
    ///
    /// This is for creating a file for a buffer or network response without creating a file on the
    /// filesystem.
    pub fn export_png_data(&self) -> Result<Vec<u8>, error::IdenticonError> {
        let image = self.generate_image()?;
        let image_size = image.to_rgb8().width();
        let mut buffer = Vec::new();

        PngEncoder::new(&mut buffer)
            .write_image(
                image.to_rgb8().into_raw().as_slice(),
                image_size,
                image_size,
                image::ColorType::Rgb8,
            )
            .map_err(|_| error::IdenticonError::EncodeImageError)?;
        Ok(buffer)
    }

    /// Export a JPEG file buffer as a Vec<u8>.
    ///
    /// This is for creating a file for a buffer or network response without creating a file on the
    /// filesystem.
    pub fn export_jpeg_data(&self) -> Result<Vec<u8>, error::IdenticonError> {
        let image = self.generate_image()?;
        let image_size = image.to_rgb8().width();
        let mut buffer = Vec::new();

        JpegEncoder::new(&mut buffer)
            .write_image(
                image.to_rgb8().into_raw().as_slice(),
                image_size,
                image_size,
                image::ColorType::Rgb8,
            )
            .map_err(|_| error::IdenticonError::EncodeImageError)?;
        Ok(buffer)
    }
}

impl Default for Identicon {
    fn default() -> Self {
        let default_background_color = 240;
        Self {
            hash: Self::hash_value(""),
            border: 50,
            size: 5,
            scale: 500,
            background_color: (
                default_background_color,
                default_background_color,
                default_background_color,
            ),
            mirrored: true,
        }
    }
}

impl FromStr for Identicon {
    type Err = IdenticonError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::new(s))
    }
}

#[cfg(test)]
mod tests {
    use crate::Identicon;

    #[test]
    fn consistency() {
        let expected_color = (111, 212, 172);
        let expected_grid = vec![
            true, true, true, true, false, true, true, true, false, true, true, true, false, true,
            true, false, true, true, true, true, true, true, false, true, true,
        ];

        let image = Identicon::new("test");
        let grid = crate::grid::generate_full_grid(image.size, &image.hash);
        let color = crate::color::generate_color(&image.hash);

        assert_eq!(expected_color, color);

        assert_eq!(expected_grid, grid);
    }

    #[test]
    fn trim_of_input_works() {
        let image_normal = Identicon::new("test").generate_image().unwrap();
        let image_padded = Identicon::new("  test  ").generate_image().unwrap();
        assert_eq!(
            image_normal.to_rgb8().into_raw(),
            image_padded.to_rgb8().into_raw()
        );
    }

    #[test]
    fn trim_of_input_failure_works() {
        let image_normal = Identicon::new("test").generate_image().unwrap();
        let image_padded = Identicon::new("  test1  ").generate_image().unwrap();
        assert_ne!(
            image_normal.to_rgb8().into_raw(),
            image_padded.to_rgb8().into_raw()
        );
    }

    #[test]
    fn chained_setters_work() {
        let identicon_chained = Identicon::new("test")
            .set_border(10)
            .set_background_color((0, 0, 0))
            .clone();

        let mut identicon_mutated = Identicon::new("test");
        identicon_mutated.set_border(10);
        identicon_mutated.set_background_color((0, 0, 0));

        assert_eq!(identicon_chained, identicon_mutated);
    }

    #[test]
    fn getters_work() {
        let identicon = Identicon::new("test")
            .set_border(10)
            .set_background_color((0, 0, 0))
            .clone();

        assert_eq!(identicon.border(), identicon.border);
    }

    #[test]
    fn from_str_works() {
        let identicon = Identicon::new("test");
        let identicon_from_str = "test".parse::<Identicon>().unwrap();
        assert_eq!(identicon, identicon_from_str);
    }

    #[test]
    fn from_str_failure_works() {
        let identicon = Identicon::new("test");
        let identicon_from_str = "test1".parse::<Identicon>().unwrap();
        assert_ne!(identicon, identicon_from_str);
    }
}