Minor update.
[pysvrt.git] / vignette.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 "vignette.h"
26
27 void Vignette::clear() {
28   for(int k = 0; k < width * height; k++) {
29     content[k] = 255;
30 #ifdef KEEP_PART_PRESENCE
31     part_presence[k] = 0;
32 #endif
33   }
34 }
35
36 void Vignette::fill(int x, int y, int v) {
37   if(x >= 0 && x < Vignette::width && y >= 0 && y < Vignette::height &&
38      content[x + Vignette::width * y] == 255) {
39     content[x + Vignette::width * y] = v;
40     fill(x + 1, y    , v);
41     fill(x - 1, y    , v);
42     fill(x    , y + 1, v);
43     fill(x    , y - 1, v);
44   }
45 }
46
47 void Vignette::switch_values(int v1, int v2) {
48   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
49     if(content[k] == v1) {
50       content[k] = v2;
51     } else if(content[k] == v2) {
52       content[k] = v1;
53     }
54   }
55 }
56
57 void Vignette::replace_value(int from, int to) {
58   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
59     if(content[k] == from) {
60       content[k] = to;
61     }
62   }
63 }
64
65 void Vignette::superpose(Vignette *infront, Vignette *inback) {
66   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
67     if(infront->content[k] < 255) {
68       content[k] = infront->content[k];
69     } else {
70       content[k] = inback->content[k];
71     }
72   }
73 }
74
75 int Vignette::intersection(Vignette *v) {
76   int n = 0;
77   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
78     if(content[k] < 255 && v->content[k] < 255) {
79       n++;
80     }
81   }
82   return n;
83 }
84
85 void Vignette::grow() {
86   int tmp[Vignette::width * Vignette::height];
87   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
88     tmp[k] = content[k];
89   }
90   int k;
91   for(int y = 1; y < Vignette::height - 1; y++) {
92     for(int x = 1; x < Vignette::width - 1; x++) {
93       k = x + Vignette::width * y;
94       content[k] = min(tmp[k],
95                        min(min(tmp[k - Vignette::width], tmp[k - 1]),
96                            min(tmp[k + 1], tmp[k + Vignette::width])));
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