Update for recent libpng
[svrt.git] / vision_problem_23.cc
1 /*
2  *  svrt is the ``Synthetic Visual Reasoning Test'', an image
3  *  generator for evaluating classification performance of machine
4  *  learning systems, humans and primates.
5  *
6  *  Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7  *  Written by Francois Fleuret <francois.fleuret@idiap.ch>
8  *
9  *  This file is part of svrt.
10  *
11  *  svrt is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  svrt is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with svrt.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "vision_problem_23.h"
26 #include "shape.h"
27
28
29 VisionProblem_23::VisionProblem_23() { }
30
31 void VisionProblem_23::generate(int label, Vignette *vignette) {
32   int x_big, y_big, x_small, y_small;
33   Shape big_shape, small_shape;
34
35   int error;
36   do {
37
38     vignette->clear();
39
40     error = 0;
41
42     big_shape.randomize(big_part_size / 2, big_part_hole_size / 2);
43
44     do {
45       x_big = int(drand48() * Vignette::width);
46       y_big = int(drand48() * Vignette::height);
47     } while(big_shape.overwrites(vignette, x_big, y_big));
48
49     if(!error) {
50       big_shape.draw(vignette, x_big, y_big);
51
52       if(label) {
53         // We fill outside
54         vignette->fill(x_big, y_big, 128);
55         vignette->switch_values(128, 255);
56         // Find a location for a small shape inside
57         int nb_attempts = 0;
58         do {
59           small_shape.randomize(small_part_size / 2, small_part_hole_size / 2);
60           x_small = int(drand48() * Vignette::width);
61           y_small = int(drand48() * Vignette::height);
62           error = small_shape.overwrites(vignette, x_small, y_small);
63           nb_attempts++;
64         } while(error && nb_attempts < 10);
65
66         if(!error) {
67           // Found it, unfill outside, fill inside and draw
68           vignette->replace_value(128, 255);
69           vignette->fill(x_big, y_big, 128);
70
71           small_shape.draw(vignette, x_small, y_small);
72
73           int nb_attempts = 0;
74           do {
75             small_shape.randomize(small_part_size / 2, small_part_hole_size / 2);
76             x_small = int(drand48() * Vignette::width);
77             y_small = int(drand48() * Vignette::height);
78             error = small_shape.overwrites(vignette, x_small, y_small);
79             nb_attempts++;
80           } while(error && nb_attempts < 10);
81           if(!error) {
82             // Found it, unfill and draw
83             vignette->replace_value(128, 255);
84             small_shape.draw(vignette, x_small, y_small);
85           }
86         }
87       } else {
88         vignette->fill(x_big, y_big, 128);
89
90         if(drand48() < 0.5) {
91           vignette->switch_values(128, 255);
92         }
93
94         int nb_attempts = 0;
95         do {
96           small_shape.randomize(small_part_size / 2, small_part_hole_size / 2);
97           x_small = int(drand48() * Vignette::width);
98           y_small = int(drand48() * Vignette::height);
99           error = small_shape.overwrites(vignette, x_small, y_small);
100           nb_attempts++;
101         } while(error && nb_attempts < 10);
102
103         if(!error) {
104           small_shape.draw(vignette, x_small, y_small);
105           vignette->fill(x_small, y_small, 128);
106           int nb_attempts = 0;
107           do {
108             small_shape.randomize(small_part_size / 2, small_part_hole_size / 2);
109             x_small = int(drand48() * Vignette::width);
110             y_small = int(drand48() * Vignette::height);
111             error = small_shape.overwrites(vignette, x_small, y_small);
112             nb_attempts++;
113           } while(error && nb_attempts < 10);
114
115           if(!error) {
116             small_shape.draw(vignette, x_small, y_small);
117             vignette->replace_value(128, 255);
118           }
119         }
120       }
121
122     }
123
124   } while(error);
125 }