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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Generates 19-circle identicons in `png` and in `svg` format from `&[u8]` input slice. Slice length could be arbitrary, as identicon generation starts with input hashing. Typical input is a public key.
//!
//! Identicon example:
//!
//! The identicon color scheme and elements arrangement follow the published javascript [code](https://github.com/paritytech/oo7/blob/master/packages/polkadot-identicon/src/index.jsx) for polkadot identicon generation. This crate is intended mainly for use by [Signer](https://github.com/paritytech/parity-signer).
//!
//! Crate also supports generation of identicon-like images with pre-set colors in RGBA format, mainly for test purposes.
//!
//! Feature `"pix"` supports generation of `png` images, feature `"vec"` - generation of `svg` images. Both are made available by default.
use ;
use Document;
pub use Color;
const SIZE_IN_PIXELS: u8 = 30;
const SCALING_FACTOR: u8 = 5;
const FILTER_TYPE: FilterType = Lanczos3;
/// Static 30x30 transparent `png`, for rare cases when the identicon
/// generation fails or when the data to generate identicon is unavailable
pub const EMPTY_PNG: & = &;
/// Polkadot identicon `png` data in `u8` vector format, from `&[u8]` input slice
///
/// Input slice could be of any length, as it gets hashed anyways;
/// typical input is a public key.
///
/// Resulting image quality depends on image size,
/// images start looking acceptable for sizes apporimately 100 pix and above.
///
/// ## Example
///
/// Let's generate a set of identicons of varying size.
///
/// ```
/// use image::load_from_memory;
/// use plot_icon::generate_png;
///
/// let alice: &[u8] = &[212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125];
/// let size_set = [16, 32, 45, 64, 128, 256, 512];
/// for size_in_pixels in size_set.into_iter() {
/// let content = generate_png(alice, size_in_pixels).unwrap();
/// let image = load_from_memory(&content).unwrap();
/// assert!(image.width() == size_in_pixels as u32);
/// assert!(image.height() == size_in_pixels as u32);
/// }
/// ```
/// Polkadot identicon `png` data in `u8` vector format, with given colors
///
/// Makes regular identicons if properly generated color set is plugged in.
/// Also could be used to generate test identicon-like pictures.
///
/// Input [`Color`] set is in RGBA format.
///
/// ## Example
///
/// Make identicon with gray circles.
/// Such identicon-like image could be used as constant for rare cases when real `png` identicon failed to generate.
///
/// ```
/// use plot_icon::{generate_png_with_colors, colors::Color};
///
/// let colors = [Color{red: 200, green: 200, blue: 200, alpha: 255}; 19];
/// let size_in_pixels = 250;
/// let content = generate_png_with_colors(colors, size_in_pixels).unwrap();
/// ```
/// Data for small-sized identicon `png`, from `&[u8]` input slice,
/// larger image is generated first and then scaled down to fit the required size
///
/// Input slice could be of any length, as it gets hashed anyways;
/// typical input is a public key.
///
/// Function is expected to be used with image size values approximately 100 pix and below.
///
/// # Scaling factor
///
/// Scaling factor here is how much bigger the image gets generated, before it is scaled down.
///
/// ## Example
///
/// Let's generate a set of small identicons by scaling with different scaling factors.
///
/// Set the filter type to CatmullRom, and vary the scaling factor.
/// ```
/// use image::imageops::FilterType;
/// use plot_icon::generate_png_scaled_custom;
///
/// let alice: &[u8] = &[212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125];
/// let size_in_pixels = 32;
/// let filter = FilterType::CatmullRom;
/// for scaling_factor in 1..=16 {
/// let content = generate_png_scaled_custom (alice, size_in_pixels, scaling_factor, filter).unwrap();
/// }
/// ```
/// For image size 32, the `scaling_factor = 1` results in strongly pixelated image,
/// it is identical to `generate_png` result.
/// With `scaling_factor = 2` image is already much less pixelated, off-centering still
/// visible; off-centering virtually disappears for `scaling factor = 4` and above,
/// after `scaling factor = 6` it is quite challenging to find any image differences at all.
///
/// # Filter type
///
/// [`FilterType`](https://docs.rs/image/latest/image/imageops/enum.FilterType.html) is the filter used during the scaling.
///
/// ## Example
///
/// Let's generate another set of small identicons by scaling with different filters.
///
/// Set the scaling factor to 8, and vary the filter.
/// ```
/// use image::imageops::FilterType;
/// use plot_icon::generate_png_scaled_custom;
///
/// let bob: &[u8] = &[142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72];
/// let filter_set = [
/// FilterType::Nearest,
/// FilterType::Triangle,
/// FilterType::CatmullRom,
/// FilterType::Gaussian,
/// FilterType::Lanczos3,
/// ];
/// let scaling_factor = 8;
/// let size_in_pixels = 32;
/// for filter in filter_set.into_iter() {
/// let content = generate_png_scaled_custom (bob, size_in_pixels, scaling_factor, filter).unwrap();
/// }
/// ```
/// All identicons are readable,
/// the one made with `FilterType::Nearest` seems too pixelated,
/// the one made with `FilterType::Gaussian` seems a bit blurry.
///
/// # Selecting the defaults for Signer
///
/// To select the default values properly, a set of images with different scaling factors
/// was generated for each of the filters.
/// Signer at the moment uses 30 pix images.
///
/// ## Making image set to choose from
///
/// ```
/// use image::imageops::FilterType;
/// use plot_icon::generate_png_scaled_custom;
///
/// let alice: &[u8] = &[212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125];
/// let bob: &[u8] = &[142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72];
/// let filter_set = [
/// FilterType::Nearest,
/// FilterType::Triangle,
/// FilterType::CatmullRom,
/// FilterType::Gaussian,
/// FilterType::Lanczos3,
/// ];
/// let size_in_pixels = 30;
/// for i in 2..=10 {
/// for filter in filter_set.iter() {
/// for id_slice in [alice, bob].into_iter() {
/// let content = generate_png_scaled_custom (id_slice, size_in_pixels, i, *filter).unwrap();
/// }
/// }
/// }
/// ```
/// For now the default scaling factor is selected to be `5`,
/// the default filter is selected to be `FilterType::Lanczos3`.
///
/// Data for small-sized identicon `png`, with given colors,
/// larger image is generated first and then scaled down to fit the required size
///
/// Makes regular identicons if properly generated color set is plugged in.
/// Also could be used to generate test identicon-like pictures.
///
/// Input [`Color`] set is in RGBA format.
///
/// Function is expected to be used with image size values approximately 100 pix and below.
///
/// ## Example
///
/// Make identicon with gray circles.
///
/// ```
/// use image::imageops::FilterType;
/// use plot_icon::{generate_png_scaled_custom_with_colors, colors::Color};
///
/// let rgb_value = 150;
/// let size_in_pixels = 30;
/// let scaling_factor = 5;
/// let filter_type = FilterType::Lanczos3;
/// let colors = [Color{red: rgb_value, green: rgb_value, blue: rgb_value, alpha: 255}; 19];
/// let content = generate_png_scaled_custom_with_colors(colors, size_in_pixels, scaling_factor, filter_type).unwrap();
/// ```
/// Data for small-sized identicon `png`, from `&[u8]` input slice,
/// with default settings used for Signer app
///
/// Generates larger image and then scales it down to fit the required size,
/// with default filter (`FilterType::Lanczos3`) and default scaling factor (`5`).
/// Function is unfallible. If `png` generation itself fails, is falls back to transparent 30x30 `png`.
///
/// ## Example
///
/// ```
/// use plot_icon::generate_png_scaled_default;
///
/// let alice: &[u8] = &[212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125];
/// let filename = "test_pics/default_signer_alice.png";
/// let content = generate_png_scaled_default(alice);
/// assert!(std::fs::read(&filename).unwrap() == content, "Generated different image for {}!", filename);
/// ```
///
/// Helper function to write calculated pixel-by-pixel `png` pixel data in `png` format, header and all
/// Errors in `png` identicon generation
/// Identicon [`svg::Document`](https://docs.rs/svg/latest/svg/type.Document.html)
/// data, from `&[u8]` input slice
///
/// Input slice could be of any length, as it gets hashed anyways;
/// typical input is a public key.
///
/// ## Example
///
/// ```
/// use plot_icon::generate_svg;
///
/// let alice: &[u8] = &[212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125];
/// let svg_document = generate_svg (&alice);
/// let mut svg_expected_content = String::new();
/// svg::open("test_pics/alice.svg", &mut svg_expected_content).unwrap();
/// assert!(svg_document.to_string() == svg_expected_content, "Expected different svg content, got: {}", svg_document);
/// ```
/// Identicon [`svg::Document`](https://docs.rs/svg/latest/svg/type.Document.html)
/// data, with given colors
///
/// Makes regular identicons if properly generated color set is plugged in.
/// Also could be used to generate test identicon-like pictures.
///
/// Input [`Color`] set is in RGBA format.
///
/// ## Example
///
/// ```
/// use plot_icon::{generate_svg_with_colors, colors::Color};
///
/// let colors = [Color{red: 200, green: 200, blue: 200, alpha: 255}; 19];
/// let svg_document = generate_svg_with_colors(colors);
/// ```