Simplified things.
[svrt.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 selector.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #include "vignette.h"
26 #include "rgb_image.h"
27
28 void Vignette::clear() {
29   for(int k = 0; k < width * height; k++) {
30     content[k] = 255;
31   }
32 }
33
34 void Vignette::write_png(const char *name, int delta) {
35   RGBImage result(width * delta - (delta > 1 ? 1 : 0), height * delta - (delta > 1 ? 1 : 0));
36   for(int y = 0; y < result.height(); y++) {
37     for(int x = 0; x < result.width(); x++) {
38       int c;
39       if(delta > 4 && (x%delta == 0 || y%delta == 0)) {
40         c = 255;
41       } else {
42         c = content[(x / delta) + width * (y / delta)];
43       }
44       result.set_pixel(x, y, c, c, c);
45     }
46   }
47
48   result.write_png(name);
49 }
50
51 void Vignette::fill(int x, int y, int v) {
52   if(x >= 0 && x < Vignette::width && y >= 0 && y < Vignette::height &&
53      content[x + Vignette::width * y] == 255) {
54     content[x + Vignette::width * y] = v;
55     fill(x + 1, y    , v);
56     fill(x - 1, y    , v);
57     fill(x    , y + 1, v);
58     fill(x    , y - 1, v);
59   }
60 }
61
62 void Vignette::switch_values(int v1, int v2) {
63   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
64     if(content[k] == v1) {
65       content[k] = v2;
66     } else if(content[k] == v2) {
67       content[k] = v1;
68     }
69   }
70 }
71
72 void Vignette::replace_value(int from, int to) {
73   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
74     if(content[k] == from) {
75       content[k] = to;
76     }
77   }
78 }
79
80 void Vignette::superpose(Vignette *infront, Vignette *inback) {
81   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
82     if(infront->content[k] < 255) {
83       content[k] = infront->content[k];
84     } else {
85       content[k] = inback->content[k];
86     }
87   }
88 }
89
90 int Vignette::surface() {
91   int n = 0;
92   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
93     if(content[k] < 255) {
94       n++;
95     }
96   }
97   return n;
98 }
99
100 int Vignette::intersection(Vignette *v) {
101   int n = 0;
102   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
103     if(content[k] < 255 && v->content[k] < 255) {
104       n++;
105     }
106   }
107   return n;
108 }
109
110 void Vignette::grow() {
111   int tmp[Vignette::width * Vignette::height];
112   for(int k = 0; k < Vignette::height * Vignette::width; k++) {
113     tmp[k] = content[k];
114   }
115   int k;
116   for(int y = 1; y < Vignette::height - 1; y++) {
117     for(int x = 1; x < Vignette::width - 1; x++) {
118       k = x + Vignette::width * y;
119       content[k] = min(tmp[k],
120                        min(min(tmp[k - Vignette::width], tmp[k - 1]),
121                            min(tmp[k + 1], tmp[k + Vignette::width])));
122     }
123   }
124 }