surface.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Surface 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 <iostream>
  21. #include <ETL/surface>
  22. #include <ETL/gaussian>
  23. #include <cstdio>
  24. using namespace etl;
  25. using namespace std;
  26. int display_pen(generic_pen<float> pen, int w, int h)
  27. {
  28. int ret = 0;
  29. int x, y;
  30. // print out the after pic
  31. for (y = 0; y < h; y++, pen.inc_y()) {
  32. printf("|");
  33. for (x = 0; x < w; x++, pen.inc_x()) {
  34. if (pen.get_value() >= 2.0f) {
  35. printf("#");
  36. } else if (pen.get_value() >= 1.0f) {
  37. printf("@");
  38. } else if (pen.get_value() >= 0.8f) {
  39. printf("%%");
  40. } else if (pen.get_value() >= 0.6f) {
  41. printf("O");
  42. } else if (pen.get_value() >= 0.4f) {
  43. printf(":");
  44. } else if (pen.get_value() >= 0.2f) {
  45. printf(".");
  46. } else if (pen.get_value() >= -0.1f) {
  47. printf(" ");
  48. } else {
  49. printf("X"), ret++;
  50. }
  51. }
  52. pen.dec_x(x);
  53. printf("|\n");
  54. }
  55. pen.dec_y(y);
  56. return ret;
  57. }
  58. void make_pattern(generic_pen<float> pen, int w, int h)
  59. {
  60. int x, y;
  61. for (y = 0; y < h; y++, pen.inc_y()) {
  62. for (x = 0; x < w; x++, pen.inc_x()) {
  63. if ((x - y <= 1 && y - x <= 1) || y == h / 2 || x == w / 2) {
  64. pen.put_value(2);
  65. } else {
  66. pen.put_value(0);
  67. }
  68. }
  69. pen.dec_x(x);
  70. }
  71. pen.dec_y(y);
  72. }
  73. int basic_test()
  74. {
  75. printf("Surface:basic_test(): Running...\n");
  76. int ret = 0;
  77. surface<float> my_surface(100, 100);
  78. gaussian_blur(my_surface.begin(), my_surface.end(), 10, 10);
  79. surface<float> my_surface2(my_surface);
  80. my_surface2.fill(0.5);
  81. my_surface2.clear();
  82. my_surface2 = my_surface;
  83. my_surface2.fill(0.5);
  84. my_surface2.clear();
  85. my_surface.fill(0.5);
  86. my_surface.clear();
  87. surface<float> my_surface3;
  88. my_surface3.mirror(my_surface2);
  89. my_surface3.fill(0.5);
  90. my_surface3.clear();
  91. my_surface3 = my_surface;
  92. my_surface3.mirror(my_surface);
  93. printf("Surface:basic_test(): %d errors.\n", ret);
  94. return ret;
  95. }
  96. int linear_sample_test()
  97. {
  98. printf("Surface:linear_sample_test(): Running...\n");
  99. int ret = 0;
  100. surface<float> my_surface(16, 16);
  101. my_surface.fill(0.0f);
  102. make_pattern(my_surface.begin(), my_surface.get_w(), my_surface.get_h());
  103. int extra(5);
  104. surface<float> dest(18 + extra * 2, 18 + extra * 2);
  105. int x, y;
  106. for (x = -extra; x < dest.get_w() - extra; x++)
  107. for (y = -extra; y < dest.get_h() - extra; y++) {
  108. dest[y + extra][x + extra] = my_surface.linear_sample(
  109. float(x) / float(dest.get_w() - 1 - extra * 2) * float(my_surface.get_w() - 1),
  110. float(y) / float(dest.get_h() - 1 - extra * 2) * float(my_surface.get_h() - 1)
  111. );
  112. }
  113. display_pen(dest.begin(), dest.get_w(), dest.get_h());
  114. printf("Surface:linear_sample_test(): %d errors.\n", ret);
  115. return ret;
  116. }
  117. int cubic_sample_test()
  118. {
  119. printf("Surface:cubic_sample_test(): Running...\n");
  120. int ret = 0;
  121. surface<float> my_surface(16, 16);
  122. my_surface.fill(0.0f);
  123. make_pattern(my_surface.begin(), my_surface.get_w(), my_surface.get_h());
  124. {
  125. surface<float> dest(24, 24);
  126. int x, y;
  127. for (x = 0; x < dest.get_w(); x++)
  128. for (y = 0; y < dest.get_h(); y++) {
  129. dest[y][x] = my_surface.cubic_sample(
  130. float(x) / float(dest.get_w() - 1) * float(my_surface.get_w() - 1),
  131. float(y) / float(dest.get_h() - 1) * float(my_surface.get_h() - 1)
  132. );
  133. }
  134. display_pen(dest.begin(), dest.get_w(), dest.get_h());
  135. }
  136. display_pen(my_surface.begin(), my_surface.get_w(), my_surface.get_h());
  137. {
  138. surface<float> dest(16, 16);
  139. int x, y;
  140. for (x = 0; x < dest.get_w(); x++)
  141. for (y = 0; y < dest.get_h(); y++) {
  142. dest[y][x] = my_surface.cubic_sample(
  143. float(x) / float(dest.get_w() - 1) * float(my_surface.get_w() - 1),
  144. float(y) / float(dest.get_h() - 1) * float(my_surface.get_h() - 1)
  145. );
  146. }
  147. display_pen(dest.begin(), dest.get_w(), dest.get_h());
  148. }
  149. printf("Surface:cubic_sample_test(): %d errors.\n", ret);
  150. return ret;
  151. }
  152. int main()
  153. {
  154. int error = 0;
  155. error += basic_test();
  156. error += linear_sample_test();
  157. error += cubic_sample_test();
  158. return error;
  159. }