The value desc smart link now does the scale conversion.
[synfig.git] / ETL / test / pen.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Handle Template 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 <list>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <string>
28 #include <utility>
29 #include <memory>
30 #include <map>
31 #include <ETL/pen>
32 #include <ETL/boxblur>
33 //#include <ETL/gaussian>
34 #include <math.h>
35
36 /* === M A C R O S ========================================================= */
37
38 using namespace std;
39 using namespace etl;
40
41 /* === C L A S S E S ======================================================= */
42
43 int generic_pen_test(int w, int h)
44 {
45         printf("generic_pen(w:%d,h:%d): ",w,h);
46
47         auto_ptr<float> data(new float[w*h]);
48         if(!data.get())
49         {
50                 printf("Um..... malloc failure on line %d of "__FILE__"...\n",__LINE__);
51                 abort();
52         }
53
54         generic_pen<float> pen(data.get(),w,h);
55         generic_pen<float> pen2;
56
57         if(!pen)
58         {
59                 printf("FAILURE! "__FILE__"@%d: On pen bool test\n",__LINE__);
60                 return 1;
61         }
62
63         if(&pen.x()[2]!=&pen[0][2])
64         {
65                 printf("FAILURE! "__FILE__"@%d: On request for horizontal iterator\n",__LINE__);
66                 return 1;
67         }
68
69         if(&pen.y()[2]!=&pen[2][0])
70         {
71                 printf("FAILURE! "__FILE__"@%d: On request for vertical iterator\n",__LINE__);
72                 return 1;
73         }
74
75         pen.move(1,1);
76         pen2=pen;
77
78         if(pen!=pen2)
79         {
80                 printf("FAILURE! "__FILE__"@%d: On pen assignment or pen comparison\n",__LINE__);
81                 return 1;
82         }
83
84         pen2.move(w,h);
85         generic_pen<float>::difference_type diff(pen2-pen);
86
87         if(diff.x!=w || diff.y!=h)
88         {
89                 printf("FAILURE! "__FILE__"@%d: pen difference inconsistency ([%d,%d]!=[%d,%d])\n",__LINE__,diff.x,diff.y,w,h);
90                 return 1;
91         }
92
93         if(pen.end_x()-pen.x()!=w-1)
94         {
95                 printf("FAILURE! "__FILE__"@%d: iterator_x inconsistency (%ld!=%d)\n",__LINE__,pen.end_x()-pen.x(),w);
96                 return 1;
97         }
98
99         if(pen.end_y()-pen.y()!=h-1)
100         {
101                 printf("FAILURE! "__FILE__"@%d: iterator_y inconsistency (%d!=%d)\n",__LINE__,pen.end_y()-pen.y(),h);
102                 return 1;
103         }
104
105         if(&pen.end_y()[-1]!=&pen.y()[(h-2)])
106         {
107                 printf("FAILURE! "__FILE__"@%d: iterator_y inconsistency\n",__LINE__);
108                 return 1;
109         }
110
111         if(&pen.end_x()[-1]!=&pen.x()[(w-2)])
112         {
113                 printf("FAILURE! "__FILE__"@%d: iterator_x inconsistency\n",__LINE__);
114                 return 1;
115         }
116
117         printf("PASSED\n");
118
119         return 0;
120 }
121
122 int alpha_pen_test(void)
123 {
124         printf("alpha_pen: ");
125         printf("SKIPPED\n");
126
127         return 0;
128 }
129
130 int bbox_pen_test(void)
131 {
132         printf("bbox_pen: ");
133
134
135
136         printf("SKIPPED\n");
137
138         return 0;
139 }
140
141 int display_pen(generic_pen<float> pen, int w, int h)
142 {
143         int ret=0;
144         int x, y;
145         // print out the after pic
146         for(y=0;y<h;y++,pen.inc_y())
147         {
148                 printf("|");
149                 for(x=0;x<w;x++,pen.inc_x())
150                 {
151                         if(pen.get_value()>=2.0f)
152                                 printf("#");
153                         else if(pen.get_value()>=1.0f)
154                                 printf("@");
155                         else if(pen.get_value()>=0.8f)
156                                 printf("%%");
157                         else if(pen.get_value()>=0.6f)
158                                 printf("O");
159                         else if(pen.get_value()>=0.4f)
160                                 printf(":");
161                         else if(pen.get_value()>=0.2f)
162                                 printf(".");
163                         else if(pen.get_value()>=-0.0001f)
164                                 printf(" ");
165                         else
166                                 printf("X"),ret++;
167                 }
168                 pen.dec_x(x);
169                 printf("|\n");
170         }
171         pen.dec_y(y);
172         return ret;
173 }
174
175 int display_pen(generic_pen<double> pen, int w, int h)
176 {
177         int ret=0;
178         int x, y;
179         // print out the after pic
180         for(y=0;y<h;y++,pen.inc_y())
181         {
182                 printf("|");
183                 for(x=0;x<w;x++,pen.inc_x())
184                 {
185                         if(pen.get_value()>=2.0f)
186                                 printf("#");
187                         else if(pen.get_value()>=1.0f)
188                                 printf("@");
189                         else if(pen.get_value()>=0.8f)
190                                 printf("%%");
191                         else if(pen.get_value()>=0.6f)
192                                 printf("O");
193                         else if(pen.get_value()>=0.4f)
194                                 printf(":");
195                         else if(pen.get_value()>=0.2f)
196                                 printf(".");
197                         else if(pen.get_value()>=-0.0001f)
198                                 printf(" ");
199                         else
200                                 printf("X"),ret++;
201                 }
202                 pen.dec_x(x);
203                 printf("|\n");
204         }
205         pen.dec_y(y);
206         return ret;
207 }
208
209 void emptyfunction(int v)
210 {
211         static int stupid = 0;
212         stupid = v;
213         //printf("Called... %d\n",v);
214 }
215
216 int box_blur_test(void)
217 {
218         typedef float boxblur_float;
219
220         printf("box_blur: ");
221
222         int w=25,h=25;
223
224         auto_ptr<boxblur_float> data(new boxblur_float[w*h]);
225         auto_ptr<boxblur_float> data2(new boxblur_float[w*h]);
226         if(!data.get())
227         {
228                 printf("Um..... malloc failure on line %d of "__FILE__"...\n",__LINE__);
229                 abort();
230         }
231
232         generic_pen<boxblur_float> pen(data.get(),w,h);
233         generic_pen<boxblur_float> pen2;
234
235         generic_pen<boxblur_float> pen3(data2.get(),w,h);
236         int x,y;
237
238         for(y=0;y<h;y++,pen.inc_y())
239         {
240                 for(x=0;x<w;x++,pen.inc_x())
241                 {
242                         if( (x-y<=1 && y-x<=1) || y==h/2 || x==w/2)
243                                 pen.put_value(2);
244                         else
245                                 pen.put_value(0);
246                 }
247                 pen.dec_x(x);
248         }
249         pen.dec_y(y);
250
251         int bad_values=0;
252
253         printf("\nBEFORE BOX BLUR:\n");
254
255         // print out the before pic
256         display_pen(pen,w,h);
257
258         // Pen 2 will be the end
259         pen2=pen;
260         pen2.move(w,h);
261
262         //temporary
263         vbox_blur(pen,pen2,2,pen3);
264         printf("\n VBLUR ONLY:\n");
265         display_pen(pen3,w,h);
266
267 //      box_blur(pen,w,h,4);
268         hbox_blur(pen,pen2,2,pen3);
269
270         printf("\n HBLUR ONLY:\n");
271         display_pen(pen3,w,h);
272
273         pen2=pen3;
274         pen2.move(w,h);
275         vbox_blur(pen3,pen2,2,pen);
276
277         printf("\nAFTER BOX BLUR:\n");
278
279         // print out the after pic
280         bad_values=display_pen(pen,w,h);
281
282         if(bad_values)
283         {
284                 printf("FAILURE! "__FILE__"@%d: blur result contained %d bad values\n",__LINE__,bad_values);
285                 return 1;
286         }
287
288         boxblur_float max=0;
289         printf("CHECK BOXBLUR RESULTS %d,%d:\n",pen.diff_begin().x, pen.diff_begin().y);
290         for(y=0;y<h;y++,pen.inc_y())
291         {
292                 for(x=0;x<w;x++,pen.inc_x())
293                 {
294                         boxblur_float f = 0;
295
296                         for(int oy=-2; oy <= 2; ++oy)
297                         {
298                                 int iy = y+oy;
299                                 if(iy < 0) iy = 0;
300                                 if(iy >= h) iy = h-1;
301
302                                 for(int ox=-2; ox <= 2; ++ox)
303                                 {
304                                         int ix = x+ox;
305                                         if(ix < 0) ix = 0;
306                                         if(ix >= w) ix = w-1;
307
308                                         if( (ix-iy<=1 && iy-ix<=1) || iy==h/2 || ix==w/2)
309                                                 f += 2;
310                                 }
311                         }
312
313                         //print out if the relative error is high
314                         /*f /= 25;
315                         float rf = pen.get_value() - f/25;
316                         if(f && rf > 0.3)
317                         {
318                                 printf("pixel (%d,%d) off by %f\n",x,y,rf);
319                         }*/
320                         boxblur_float diff = fabs(pen.get_value() - f/25);
321                         if(diff > max) max = diff;
322                         pen.put_value(f/25); //if length = 2 then dim = 5.. area = 25
323                 }
324                 pen.dec_x(x);
325         }
326         pen.dec_y(y);
327
328         /*if(max)
329         {
330                 for(y=0;y<h;y++,pen.inc_y())
331                 {
332                         for(x=0;x<w;x++,pen.inc_x())
333                         {
334                                 pen.put_value(pen.get_value()/max);
335                         }
336                         pen.dec_x(x);
337                 }
338                 pen.dec_y(y);
339         }*/
340
341         //printf("\nHBOXBLUR ERROR (max = %e):\n",max);
342         printf("\nCorrect results:\n");
343         display_pen(pen,w,h);
344
345         printf("PASSED\n");
346
347         return 0;
348 }
349
350 /*
351 float:
352 |@@%O.     :::::          |
353 |@@@%:.    :::::          |
354 |%@@%O:.   :::::          |
355 |O%%@%O:.  :::::          |
356 |.:O%@%O:. :::::          |
357 | .:O%@%O:.:::::          |
358 |  .:O%@%O:O::::          |
359 |   .:O%@%O%O:::          |
360 |    .:O%@%@%O::          |
361 |     .:O%@@@%::          |
362 |::.:::O%@@@@@%O::::::::::|
363 |::.::::O%@@@@@%::::::::::|
364 |::.:::::OO@@@@@%O::::::::|
365 |::.:::::::%@@@@@%O:::::::|
366 |::.::::::.O%@@@@@%O::::::|
367 |          ::%@@@%O:.     |
368 |          ::O%@%@%O:.    |
369 |          :.:O%O%@%O:.   |
370 |          :.::O:O%@%O:.  |
371 |          :.:.:.:O%@%O:. |
372 |          :.:.: .:O%@%O:.|
373 |          :.:.:  .:O%@%%O|
374 |          :.:.:   .:O%@@%|
375 |          :.:.:    .:%@@@|
376 |          :.:.:     .O%@@|
377
378 double:
379 |@@%O.     .....          |
380 |@@@O:.    .....          |
381 |%@@%O:.   .....          |
382 |OO%@%O:.  .....          |
383 |.:O%@%O:. .....          |
384 | .:O%@%O:.:....          |
385 |  .:O%@%O:O:...          |
386 |   .:O%@%O%O:..          |
387 |    .:O%@%@%O:.          |
388 |     .:O%@@@O:.          |
389 |.....:O%@@@@@%O..........|
390 |......:O%@@@@@%::........|
391 |.......:OO@@@@@OO:.......|
392 |........::%@@@@@%O:......|
393 |..........O%@@@@@%O:.....|
394 |          .:O@@@%O:.     |
395 |          .:O%@%@%O:.    |
396 |          ..:O%O%@%O:.   |
397 |          ...:O:O%@%O:.  |
398 |          ....:.:O%@%O:. |
399 |          ..... .:O%@%O:.|
400 |          .....  .:O%@%OO|
401 |          .....   .:O%@@%|
402 |          .....    .:O@@@|
403 |          .....     .O%@@|
404
405
406 */
407
408 int gaussian_blur_test(void)
409 {
410         printf("gaussian_blur: ");
411 #if 0
412         int w=25,h=25;
413         int bad_values=0;
414
415         auto_ptr<float> data(new float[w*h]);
416         if(!data.get())
417         {
418                 printf("Um..... malloc failure on line %d of "__FILE__"...\n",__LINE__);
419                 abort();
420         }
421
422         generic_pen<float> pen(data.get(),w,h);
423         generic_pen<float> pen2;
424         int x,y;
425
426         for(y=0;y<h;y++,pen.inc_y())
427         {
428                 for(x=0;x<w;x++,pen.inc_x())
429                 {
430                         if((x-y<=1 && y-x<=1) || y==h/2)
431                                 pen.put_value(2);
432                         else
433                                 pen.put_value(0);
434                 }
435                 pen.dec_x(x);
436         }
437         pen.dec_y(y);
438
439         printf("\nBEFORE GAUSSIAN BLUR:\n");
440
441         // print out the before pic
442         for(y=0;y<h;y++,pen.inc_y())
443         {
444                 printf("|");
445                 for(x=0;x<w;x++,pen.inc_x())
446                 {
447                         if(pen.get_value()>=2.0f)
448                                 printf("#");
449                         else if(pen.get_value()>=1.0f)
450                                 printf("@");
451                         else if(pen.get_value()>=0.8f)
452                                 printf("%%");
453                         else if(pen.get_value()>=0.6f)
454                                 printf("O");
455                         else if(pen.get_value()>=0.4f)
456                                 printf(":");
457                         else if(pen.get_value()>=0.2f)
458                                 printf(".");
459                         else if(pen.get_value()>=0.0f)
460                                 printf(" ");
461                         else
462                                 printf("X"),bad_values++;
463                 }
464                 pen.dec_x(x);
465                 printf("|\n");
466         }
467         pen.dec_y(y);
468
469         // Pen 2 will be the end
470         pen2=pen;
471         pen2.move(w,h);
472
473 #if 0
474         gaussian_blur_5x5(pen,pen2);
475         gaussian_blur_5x5(pen,pen2);
476         gaussian_blur_5x5(pen,pen2);
477 #endif
478
479 #if 0
480         gaussian_blur_3x3(pen,pen2);
481         gaussian_blur_3x3(pen,pen2);
482         gaussian_blur_3x3(pen,pen2);
483         gaussian_blur_3x3(pen,pen2);
484         gaussian_blur_3x3(pen,pen2);
485 #endif
486
487 //      gaussian_blur(pen,pen2,15);
488         gaussian_blur(pen,pen2,10,10);
489
490         printf("\nAFTER GAUSSIAN BLUR:\n");
491
492         // print out the after pic
493         for(y=0;y<h;y++,pen.inc_y())
494         {
495                 printf("|");
496                 for(x=0;x<w;x++,pen.inc_x())
497                 {
498                         if(pen.get_value()>=2.0f)
499                                 printf("#");
500                         else if(pen.get_value()>=1.0f)
501                                 printf("@");
502                         else if(pen.get_value()>=0.8f)
503                                 printf("%%");
504                         else if(pen.get_value()>=0.6f)
505                                 printf("O");
506                         else if(pen.get_value()>=0.4f)
507                                 printf(":");
508                         else if(pen.get_value()>=0.2f)
509                                 printf(".");
510                         else if(pen.get_value()>=0.0f)
511                                 printf(" ");
512                         else
513                                 printf("X"),bad_values++;
514                 }
515                 pen.dec_x(x);
516                 printf("|\n");
517         }
518         pen.dec_y(y);
519
520         if(bad_values)
521         {
522                 printf("FAILURE! "__FILE__"@%d: blur result contained bad values\n",__LINE__);
523                 return 1;
524         }
525 #endif
526         printf("PASSED\n");
527
528         return 0;
529 }
530
531 /* === E N T R Y P O I N T ================================================= */
532
533 int main()
534 {
535         int error=0;
536
537         error+=generic_pen_test(40,40);
538         error+=generic_pen_test(10,40);
539         error+=generic_pen_test(40,10);
540     if(error)return error;
541         error+=alpha_pen_test();
542         error+=bbox_pen_test();
543         error+=box_blur_test();
544     if(error)return error;
545         error+=gaussian_blur_test();
546
547         return error;
548 }