pen.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Handle Template Class Test
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. **
  7. ** This package is free software; you can redistribute it and/or
  8. ** modify it under the terms of the GNU General Public License as
  9. ** published by the Free Software Foundation; either version 2 of
  10. ** the License, or (at your option) any later version.
  11. **
  12. ** This package is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ** General Public License for more details.
  16. **
  17. ** === N O T E S ===========================================================
  18. **
  19. ** ========================================================================= */
  20. #include <list>
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <string>
  24. #include <utility>
  25. #include <memory>
  26. #include <map>
  27. #include <ETL/pen>
  28. #include <ETL/boxblur>
  29. #include <cmath>
  30. using namespace std;
  31. using namespace etl;
  32. int generic_pen_test(int w, int h)
  33. {
  34. printf("generic_pen(w:%d,h:%d): ", w, h);
  35. auto_ptr<float> data(new float[w * h]);
  36. if (!data.get()) {
  37. printf("Um..... malloc failure on line %d of "__FILE__"...\n", __LINE__);
  38. abort();
  39. }
  40. generic_pen<float> pen(data.get(), w, h);
  41. generic_pen<float> pen2;
  42. if (!pen) {
  43. printf("FAILURE! "__FILE__"@%d: On pen bool test\n", __LINE__);
  44. return 1;
  45. }
  46. if (&pen.x()[2] != &pen[0][2]) {
  47. printf("FAILURE! "__FILE__"@%d: On request for horizontal iterator\n", __LINE__);
  48. return 1;
  49. }
  50. if (&pen.y()[2] != &pen[2][0]) {
  51. printf("FAILURE! "__FILE__"@%d: On request for vertical iterator\n", __LINE__);
  52. return 1;
  53. }
  54. pen.move(1, 1);
  55. pen2 = pen;
  56. if (pen != pen2) {
  57. printf("FAILURE! "__FILE__"@%d: On pen assignment or pen comparison\n", __LINE__);
  58. return 1;
  59. }
  60. pen2.move(w, h);
  61. generic_pen<float>::difference_type diff(pen2 - pen);
  62. if (diff.x != w || diff.y != h) {
  63. printf("FAILURE! "__FILE__"@%d: pen difference inconsistency ([%d,%d]!=[%d,%d])\n", __LINE__, diff.x, diff.y, w, h);
  64. return 1;
  65. }
  66. if (pen.end_x() - pen.x() != w - 1) {
  67. printf("FAILURE! "__FILE__"@%d: iterator_x inconsistency (%ld!=%d)\n", __LINE__, pen.end_x() - pen.x(), w);
  68. return 1;
  69. }
  70. if (pen.end_y() - pen.y() != h - 1) {
  71. printf("FAILURE! "__FILE__"@%d: iterator_y inconsistency (%d!=%d)\n", __LINE__, pen.end_y() - pen.y(), h);
  72. return 1;
  73. }
  74. if (&pen.end_y()[-1] != &pen.y()[(h - 2)]) {
  75. printf("FAILURE! "__FILE__"@%d: iterator_y inconsistency\n", __LINE__);
  76. return 1;
  77. }
  78. if (&pen.end_x()[-1] != &pen.x()[(w - 2)]) {
  79. printf("FAILURE! "__FILE__"@%d: iterator_x inconsistency\n", __LINE__);
  80. return 1;
  81. }
  82. printf("PASSED\n");
  83. return 0;
  84. }
  85. int alpha_pen_test(void)
  86. {
  87. printf("alpha_pen: ");
  88. printf("SKIPPED\n");
  89. return 0;
  90. }
  91. int bbox_pen_test(void)
  92. {
  93. printf("bbox_pen: ");
  94. printf("SKIPPED\n");
  95. return 0;
  96. }
  97. int display_pen(generic_pen<float> pen, int w, int h)
  98. {
  99. int ret = 0;
  100. int x, y;
  101. // print out the after pic
  102. for (y = 0; y < h; y++, pen.inc_y()) {
  103. printf("|");
  104. for (x = 0; x < w; x++, pen.inc_x()) {
  105. if (pen.get_value() >= 2.0f) {
  106. printf("#");
  107. } else if (pen.get_value() >= 1.0f) {
  108. printf("@");
  109. } else if (pen.get_value() >= 0.8f) {
  110. printf("%%");
  111. } else if (pen.get_value() >= 0.6f) {
  112. printf("O");
  113. } else if (pen.get_value() >= 0.4f) {
  114. printf(":");
  115. } else if (pen.get_value() >= 0.2f) {
  116. printf(".");
  117. } else if (pen.get_value() >= -0.0001f) {
  118. printf(" ");
  119. } else {
  120. printf("X"), ret++;
  121. }
  122. }
  123. pen.dec_x(x);
  124. printf("|\n");
  125. }
  126. pen.dec_y(y);
  127. return ret;
  128. }
  129. int display_pen(generic_pen<double> pen, int w, int h)
  130. {
  131. int ret = 0;
  132. int x, y;
  133. // print out the after pic
  134. for (y = 0; y < h; y++, pen.inc_y()) {
  135. printf("|");
  136. for (x = 0; x < w; x++, pen.inc_x()) {
  137. if (pen.get_value() >= 2.0f) {
  138. printf("#");
  139. } else if (pen.get_value() >= 1.0f) {
  140. printf("@");
  141. } else if (pen.get_value() >= 0.8f) {
  142. printf("%%");
  143. } else if (pen.get_value() >= 0.6f) {
  144. printf("O");
  145. } else if (pen.get_value() >= 0.4f) {
  146. printf(":");
  147. } else if (pen.get_value() >= 0.2f) {
  148. printf(".");
  149. } else if (pen.get_value() >= -0.0001f) {
  150. printf(" ");
  151. } else {
  152. printf("X"), ret++;
  153. }
  154. }
  155. pen.dec_x(x);
  156. printf("|\n");
  157. }
  158. pen.dec_y(y);
  159. return ret;
  160. }
  161. void emptyfunction(int v)
  162. {
  163. static int stupid = 0;
  164. stupid = v;
  165. // printf("Called... %d\n",v);
  166. }
  167. int box_blur_test(void)
  168. {
  169. typedef float boxblur_float;
  170. printf("box_blur: ");
  171. int w = 25, h = 25;
  172. auto_ptr<boxblur_float> data(new boxblur_float[w * h]);
  173. auto_ptr<boxblur_float> data2(new boxblur_float[w * h]);
  174. if (!data.get()) {
  175. printf("Um..... malloc failure on line %d of "__FILE__"...\n", __LINE__);
  176. abort();
  177. }
  178. generic_pen<boxblur_float> pen(data.get(), w, h);
  179. generic_pen<boxblur_float> pen2;
  180. generic_pen<boxblur_float> pen3(data2.get(), w, h);
  181. int x, y;
  182. for (y = 0; y < h; y++, pen.inc_y()) {
  183. for (x = 0; x < w; x++, pen.inc_x()) {
  184. if ((x - y <= 1 && y - x <= 1) || y == h / 2 || x == w / 2) {
  185. pen.put_value(2);
  186. } else {
  187. pen.put_value(0);
  188. }
  189. }
  190. pen.dec_x(x);
  191. }
  192. pen.dec_y(y);
  193. int bad_values = 0;
  194. printf("\nBEFORE BOX BLUR:\n");
  195. // print out the before pic
  196. display_pen(pen, w, h);
  197. // Pen 2 will be the end
  198. pen2 = pen;
  199. pen2.move(w, h);
  200. // temporary
  201. vbox_blur(pen, pen2, 2, pen3);
  202. printf("\n VBLUR ONLY:\n");
  203. display_pen(pen3, w, h);
  204. // box_blur(pen,w,h,4);
  205. hbox_blur(pen, pen2, 2, pen3);
  206. printf("\n HBLUR ONLY:\n");
  207. display_pen(pen3, w, h);
  208. pen2 = pen3;
  209. pen2.move(w, h);
  210. vbox_blur(pen3, pen2, 2, pen);
  211. printf("\nAFTER BOX BLUR:\n");
  212. // print out the after pic
  213. bad_values = display_pen(pen, w, h);
  214. if (bad_values) {
  215. printf("FAILURE! "__FILE__"@%d: blur result contained %d bad values\n", __LINE__, bad_values);
  216. return 1;
  217. }
  218. boxblur_float max = 0;
  219. printf("CHECK BOXBLUR RESULTS %d,%d:\n", pen.diff_begin().x, pen.diff_begin().y);
  220. for (y = 0; y < h; y++, pen.inc_y()) {
  221. for (x = 0; x < w; x++, pen.inc_x()) {
  222. boxblur_float f = 0;
  223. for (int oy = -2; oy <= 2; ++oy) {
  224. int iy = y + oy;
  225. if (iy < 0) {
  226. iy = 0;
  227. }
  228. if (iy >= h) {
  229. iy = h - 1;
  230. }
  231. for (int ox = -2; ox <= 2; ++ox) {
  232. int ix = x + ox;
  233. if (ix < 0) {
  234. ix = 0;
  235. }
  236. if (ix >= w) {
  237. ix = w - 1;
  238. }
  239. if ((ix - iy <= 1 && iy - ix <= 1) || iy == h / 2 || ix == w / 2) {
  240. f += 2;
  241. }
  242. }
  243. }
  244. // print out if the relative error is high
  245. boxblur_float diff = fabs(pen.get_value() - f / 25);
  246. if (diff > max) {
  247. max = diff;
  248. }
  249. pen.put_value(f / 25); // if length = 2 then dim = 5.. area = 25
  250. }
  251. pen.dec_x(x);
  252. }
  253. pen.dec_y(y);
  254. printf("\nCorrect results:\n");
  255. display_pen(pen, w, h);
  256. printf("PASSED\n");
  257. return 0;
  258. }
  259. /*
  260. float:
  261. |@@%O. ::::: |
  262. |@@@%:. ::::: |
  263. |%@@%O:. ::::: |
  264. |O%%@%O:. ::::: |
  265. |.:O%@%O:. ::::: |
  266. | .:O%@%O:.::::: |
  267. | .:O%@%O:O:::: |
  268. | .:O%@%O%O::: |
  269. | .:O%@%@%O:: |
  270. | .:O%@@@%:: |
  271. |::.:::O%@@@@@%O::::::::::|
  272. |::.::::O%@@@@@%::::::::::|
  273. |::.:::::OO@@@@@%O::::::::|
  274. |::.:::::::%@@@@@%O:::::::|
  275. |::.::::::.O%@@@@@%O::::::|
  276. | ::%@@@%O:. |
  277. | ::O%@%@%O:. |
  278. | :.:O%O%@%O:. |
  279. | :.::O:O%@%O:. |
  280. | :.:.:.:O%@%O:. |
  281. | :.:.: .:O%@%O:.|
  282. | :.:.: .:O%@%%O|
  283. | :.:.: .:O%@@%|
  284. | :.:.: .:%@@@|
  285. | :.:.: .O%@@|
  286. double:
  287. |@@%O. ..... |
  288. |@@@O:. ..... |
  289. |%@@%O:. ..... |
  290. |OO%@%O:. ..... |
  291. |.:O%@%O:. ..... |
  292. | .:O%@%O:.:.... |
  293. | .:O%@%O:O:... |
  294. | .:O%@%O%O:.. |
  295. | .:O%@%@%O:. |
  296. | .:O%@@@O:. |
  297. |.....:O%@@@@@%O..........|
  298. |......:O%@@@@@%::........|
  299. |.......:OO@@@@@OO:.......|
  300. |........::%@@@@@%O:......|
  301. |..........O%@@@@@%O:.....|
  302. | .:O@@@%O:. |
  303. | .:O%@%@%O:. |
  304. | ..:O%O%@%O:. |
  305. | ...:O:O%@%O:. |
  306. | ....:.:O%@%O:. |
  307. | ..... .:O%@%O:.|
  308. | ..... .:O%@%OO|
  309. | ..... .:O%@@%|
  310. | ..... .:O@@@|
  311. | ..... .O%@@|
  312. */
  313. int gaussian_blur_test(void)
  314. {
  315. printf("gaussian_blur: ");
  316. printf("PASSED\n");
  317. return 0;
  318. }
  319. int main()
  320. {
  321. int error = 0;
  322. error += generic_pen_test(40, 40);
  323. error += generic_pen_test(10, 40);
  324. error += generic_pen_test(40, 10);
  325. if (error) {
  326. return error;
  327. }
  328. error += alpha_pen_test();
  329. error += bbox_pen_test();
  330. error += box_blur_test();
  331. if (error) {
  332. return error;
  333. }
  334. error += gaussian_blur_test();
  335. return error;
  336. }