1 module dlibwebp.random;
2 
3 import dlib.image;
4 import std.random;
5 import std.algorithm.comparison;
6 import std.conv;
7 import std.digest.sha;
8 import std.algorithm.searching;
9 
10 public class RandomImages {
11     public static SuperImage circles(PixelFormat format)(int w, int h) {
12         return randomCircles!(format)(w, h);
13     }
14     public static SuperImage circles(int w, int h) {
15         return RandomImages.circles!(PixelFormat.RGBA8)(w, h);
16     }
17 }
18 
19 private auto randHsv() {
20     return hsv(uniform01() * 360f, uniform01(), uniform01());
21 }
22 
23 /** A very naive implementation. */
24 private void fillCircle(SuperImage img, Color4f color, int x0, int y0, int r) {
25     if (r > 0) {
26         auto maxR = min(r, max(img.width(), img.height()));
27         for (int i = 1; i <= maxR; i++) {
28             img.drawCircle(color, x0, y0, i);
29         }
30     }
31 }
32 private void randomCircles(SuperImage img, Color4f color, int minCount, int maxCount) {
33     int countOfElements = uniform(minCount, 1 + maxCount);
34     int maxR = uniform(1, max(img.width(), img.height()) / 2);
35     for (int i = 0; i < countOfElements; i++) {
36         img.fillCircle(color, uniform(0, img.width()), uniform(0, img.height()), maxR);
37         maxR = uniform(1, 1 + maxR);
38     }
39 }
40 private SuperImage randomCircles(PixelFormat format)(int w, int h) {
41     SuperImage img = new Image!(format)(w, h);
42     img.fillColor(randHsv());
43     img.randomCircles(randHsv(), 1, 10);
44     img.randomCircles(randHsv(), 1, 10);
45     return img;
46 }
47 
48 unittest {
49     string[] hashes = [];
50     for (int i = 0; i < 10; i++) {
51         string fn = "test" ~ to!string(i) ~ ".png";
52         SuperImage image = RandomImages.circles(320, 240);
53 
54         // I suppose, this is a strong enough way to check uniqueness.
55         auto hash = (new SHA512Digest()).digest(image.data()).toHexString();
56         assert(!hashes.canFind(hash));
57         hashes ~= hash;
58 
59         image.savePNG(fn);
60     }
61 }
62