Write the correct version in the configure.ac files.
[synfig.git] / ETL / test / surface.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Surface Class Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #include <iostream>
25 #include <ETL/surface>
26 #include <ETL/gaussian>
27 #include <cstdio>
28
29 /* === M A C R O S ========================================================= */
30
31 using namespace etl;
32 using namespace std;
33
34 /* === C L A S S E S ======================================================= */
35
36
37 /* === P R O C E D U R E S ================================================= */
38
39 int display_pen(generic_pen<float> pen, int w, int h)
40 {
41         int ret=0;
42         int x, y;
43         // print out the after pic
44         for(y=0;y<h;y++,pen.inc_y())
45         {
46                 printf("|");
47                 for(x=0;x<w;x++,pen.inc_x())
48                 {
49                         if(pen.get_value()>=2.0f)
50                                 printf("#");
51                         else if(pen.get_value()>=1.0f)
52                                 printf("@");
53                         else if(pen.get_value()>=0.8f)
54                                 printf("%%");
55                         else if(pen.get_value()>=0.6f)
56                                 printf("O");
57                         else if(pen.get_value()>=0.4f)
58                                 printf(":");
59                         else if(pen.get_value()>=0.2f)
60                                 printf(".");
61                         else if(pen.get_value()>=-0.1f)
62                                 printf(" ");
63                         else
64                                 printf("X"),ret++;
65                 }
66                 pen.dec_x(x);
67                 printf("|\n");
68         }
69         pen.dec_y(y);
70         return ret;
71 }
72
73 void make_pattern(generic_pen<float> pen, int w, int h)
74 {
75         int x,y;
76         for(y=0;y<h;y++,pen.inc_y())
77         {
78                 for(x=0;x<w;x++,pen.inc_x())
79                 {
80                         if( (x-y<=1 && y-x<=1) || y==h/2 || x==w/2)
81                                 pen.put_value(2);
82                         else
83                                 pen.put_value(0);
84                 }
85                 pen.dec_x(x);
86         }
87         pen.dec_y(y);
88 }
89
90 int basic_test()
91 {
92         printf("Surface:basic_test(): Running...\n");
93
94         int ret=0;
95
96         surface<float> my_surface(100,100);
97
98         gaussian_blur(my_surface.begin(),my_surface.end(),10,10);
99
100         surface<float> my_surface2(my_surface);
101
102         my_surface2.fill(0.5);
103         my_surface2.clear();
104
105         my_surface2=my_surface;
106
107         my_surface2.fill(0.5);
108         my_surface2.clear();
109
110         my_surface.fill(0.5);
111         my_surface.clear();
112
113         surface<float> my_surface3;
114         my_surface3.mirror(my_surface2);
115
116         my_surface3.fill(0.5);
117         my_surface3.clear();
118
119         my_surface3=my_surface;
120
121         my_surface3.mirror(my_surface);
122
123         printf("Surface:basic_test(): %d errors.\n",ret);
124
125         return ret;
126 }
127
128 int linear_sample_test()
129 {
130         printf("Surface:linear_sample_test(): Running...\n");
131
132         int ret=0;
133
134         surface<float> my_surface(16,16);
135
136         my_surface.fill(0.0f);
137
138         make_pattern(my_surface.begin(),my_surface.get_w(),my_surface.get_h());
139
140         int extra(5);
141         surface<float> dest(18+extra*2,18+extra*2);
142
143         int x,y;
144         for(x=-extra;x<dest.get_w()-extra;x++)
145                 for(y=-extra;y<dest.get_h()-extra;y++)
146                 {
147                         dest[y+extra][x+extra]=my_surface.linear_sample(
148                                 float(x)/float(dest.get_w()-1-extra*2)*float(my_surface.get_w()-1),
149                                 float(y)/float(dest.get_h()-1-extra*2)*float(my_surface.get_h()-1)
150                         );
151                 }
152
153         display_pen(dest.begin(),dest.get_w(),dest.get_h());
154
155         printf("Surface:linear_sample_test(): %d errors.\n",ret);
156
157         return ret;
158 }
159
160 int cubic_sample_test()
161 {
162         printf("Surface:cubic_sample_test(): Running...\n");
163
164         int ret=0;
165
166         surface<float> my_surface(16,16);
167
168         my_surface.fill(0.0f);
169
170         make_pattern(my_surface.begin(),my_surface.get_w(),my_surface.get_h());
171
172         {
173                 surface<float> dest(24,24);
174
175                 int x,y;
176                 for(x=0;x<dest.get_w();x++)
177                         for(y=0;y<dest.get_h();y++)
178                         {
179                                 dest[y][x]=my_surface.cubic_sample(
180                                         float(x)/float(dest.get_w()-1)*float(my_surface.get_w()-1),
181                                         float(y)/float(dest.get_h()-1)*float(my_surface.get_h()-1)
182                                 );
183                         }
184
185                 display_pen(dest.begin(),dest.get_w(),dest.get_h());
186         }
187
188         display_pen(my_surface.begin(),my_surface.get_w(),my_surface.get_h());
189         {
190                 surface<float> dest(16,16);
191
192                 int x,y;
193                 for(x=0;x<dest.get_w();x++)
194                         for(y=0;y<dest.get_h();y++)
195                         {
196                                 dest[y][x]=my_surface.cubic_sample(
197                                         float(x)/float(dest.get_w()-1)*float(my_surface.get_w()-1),
198                                         float(y)/float(dest.get_h()-1)*float(my_surface.get_h()-1)
199                                 );
200                         }
201
202                 display_pen(dest.begin(),dest.get_w(),dest.get_h());
203         }
204
205         printf("Surface:cubic_sample_test(): %d errors.\n",ret);
206
207         return ret;
208 }
209
210 /* === E N T R Y P O I N T ================================================= */
211
212 int main()
213 {
214         int error=0;
215
216         error+=basic_test();
217         error+=linear_sample_test();
218         error+=cubic_sample_test();
219
220         return error;
221 }