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