TestImageOperators.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimage - An image/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. #ifdef NICE_USELIB_CPPUNIT
  7. #include "TestImageOperators.h"
  8. #include <string>
  9. #include <exception>
  10. #include <sstream>
  11. #include <iostream>
  12. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  13. #include <iocompression/md5.h>
  14. #endif
  15. using namespace NICE;
  16. using namespace std;
  17. CPPUNIT_TEST_SUITE_REGISTRATION( TestImageOperators );
  18. void TestImageOperators::setUp() {
  19. }
  20. void TestImageOperators::tearDown() {
  21. }
  22. void TestImageOperators::testConstructor() {
  23. }
  24. void TestImageOperators::testOperators()
  25. {
  26. // inline GrayImage16s operator-(const GrayImage16s& a)
  27. {
  28. GrayImage16s img(2,2);
  29. img(0,0) = 10;
  30. img(1,0) = -15;
  31. img(0,1) = 0;
  32. img(1,1) = -33;
  33. img = -img;
  34. CPPUNIT_ASSERT_EQUAL(-10, static_cast<int>(img(0,0)));
  35. CPPUNIT_ASSERT_EQUAL( 15, static_cast<int>(img(1,0)));
  36. CPPUNIT_ASSERT_EQUAL( 0, static_cast<int>(img(0,1)));
  37. CPPUNIT_ASSERT_EQUAL( 33, static_cast<int>(img(1,1)));
  38. }
  39. // inline FloatImage& operator-=(FloatImage& a, const FloatImage& b)
  40. {
  41. FloatImage fimg1(3,3);
  42. for(int y=0; y<fimg1.height(); ++y)
  43. for(int x=0; x<fimg1.width(); ++x)
  44. fimg1(x,y) = (x+1)+y*fimg1.height();
  45. FloatImage fsub(3,3);
  46. fsub.set(1.5);
  47. fimg1 -= fsub;
  48. for(int y=0; y<fimg1.height(); ++y)
  49. for(int x=0; x<fimg1.width(); ++x)
  50. CPPUNIT_ASSERT_DOUBLES_EQUAL((x+1)+y*fimg1.height()-1.5,
  51. static_cast<double>(fimg1.getPixel(x,y)), 0.0);
  52. }
  53. // inline const FloatImage operator-(const FloatImage& a, const FloatImage& b)
  54. {
  55. FloatImage fimg1(2,2);
  56. fimg1(0,0) = -15.5;
  57. fimg1(1,0) = 3.3;
  58. fimg1(0,1) = 0.25;
  59. fimg1(1,1) = -1.7;
  60. FloatImage fimg2(2,2);
  61. fimg2(0,0) = -1.3;
  62. fimg2(1,0) = -4.5;
  63. fimg2(0,1) = 1.1;
  64. fimg2(1,1) = 0.2;
  65. FloatImage fres(2,2);
  66. fres = fimg1-fimg2;
  67. CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.2, static_cast<double>(fres.getPixel(0,0)), 0.000001);
  68. CPPUNIT_ASSERT_DOUBLES_EQUAL( 7.8, static_cast<double>(fres.getPixel(1,0)), 0.000001);
  69. CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.85, static_cast<double>(fres.getPixel(0,1)), 0.000001);
  70. CPPUNIT_ASSERT_DOUBLES_EQUAL(- 1.9, static_cast<double>(fres.getPixel(1,1)), 0.000001);
  71. }
  72. // inline FloatImage& operator-=(FloatImage& a, Ipp32f c)
  73. {
  74. FloatImage fimg(3,3);
  75. for(int y=0; y<fimg.height(); ++y)
  76. for(int x=0; x<fimg.width(); ++x)
  77. fimg(x,y) = (x+y+1.0)/2.0;
  78. fimg -= 0.45;
  79. for(int y=0; y<fimg.height(); ++y)
  80. for(int x=0; x<fimg.width(); ++x)
  81. CPPUNIT_ASSERT_DOUBLES_EQUAL((x+y+1.0)/2.0-0.45, static_cast<double>(fimg(x,y)), 0.000001);
  82. }
  83. // inline const FloatImage operator-(const FloatImage& a, Ipp32f c)
  84. {
  85. FloatImage fimg(3,3);
  86. for(int y=0; y<fimg.height(); ++y)
  87. for(int x=0; x<fimg.width(); ++x)
  88. fimg(x,y) = (x+y+1.0)/2.0;
  89. FloatImage fres(3,3);
  90. fres = fimg - 0.025;
  91. for(int y=0; y<fimg.height(); ++y)
  92. for(int x=0; x<fimg.width(); ++x)
  93. CPPUNIT_ASSERT_DOUBLES_EQUAL(fimg(x,y)-0.025, static_cast<double>(fres(x,y)), 0.000001);
  94. }
  95. // inline FloatImage& operator+=(FloatImage& a, const FloatImage& b)
  96. {
  97. FloatImage fimg1(3,3), fimg2(3,3);
  98. for(int y=0; y<fimg1.height(); ++y)
  99. for(int x=0; x<fimg1.width(); ++x)
  100. {
  101. fimg1(x,y) = (x+y+1.0)/2.0;
  102. fimg2(x,y) = (x+y+1.0)*2.0;
  103. }
  104. fimg1 += fimg2;
  105. for(int y=0; y<fimg1.height(); ++y)
  106. for(int x=0; x<fimg1.width(); ++x)
  107. CPPUNIT_ASSERT_DOUBLES_EQUAL((x+y+1)*(5.0/2.0), static_cast<double>(fimg1(x,y)), 0.000001);
  108. }
  109. // inline GrayImage16s& operator+=(GrayImage16s& a, const Ipp16s& c)
  110. {
  111. GrayImage16s src(3,3);
  112. src.set(1);
  113. src += 5;
  114. for(int y=0; y<src.height(); ++y)
  115. for(int x=0; x<src.width(); ++x)
  116. CPPUNIT_ASSERT_EQUAL(6, static_cast<int>(src(x,y)));
  117. src += (-11);
  118. for(int y=0; y<src.height(); ++y)
  119. for(int x=0; x<src.width(); ++x)
  120. CPPUNIT_ASSERT_EQUAL(-5, static_cast<int>(src(x,y)));
  121. }
  122. // inline const FloatImage operator+(const FloatImage& a, const FloatImage& b)
  123. {
  124. FloatImage fimg1(3,3), fimg2(3,3);
  125. for(int y=0; y<fimg1.height(); ++y)
  126. for(int x=0; x<fimg1.width(); ++x)
  127. {
  128. fimg1(x,y) = (x+y+1.0)/2.0;
  129. fimg2(x,y) = (x+y+1.0)*2.0;
  130. }
  131. FloatImage fres = fimg1 + fimg2;
  132. for(int y=0; y<fimg1.height(); ++y)
  133. for(int x=0; x<fimg1.width(); ++x)
  134. CPPUNIT_ASSERT_DOUBLES_EQUAL((x+y+1)*(5.0/2.0), static_cast<double>(fres(x,y)), 0.000001);
  135. }
  136. // inline FloatImage& operator+=(FloatImage& a, Ipp32f c)
  137. {
  138. FloatImage fimg(3,3);
  139. for(int y=0; y<fimg.height(); ++y)
  140. for(int x=0; x<fimg.width(); ++x)
  141. fimg(x,y) = (x+y+1.0)/2.0;
  142. fimg += 115.25;
  143. for(int y=0; y<fimg.height(); ++y)
  144. for(int x=0; x<fimg.width(); ++x)
  145. CPPUNIT_ASSERT_DOUBLES_EQUAL((x+y+1.0)/2.0+115.25, static_cast<double>(fimg(x,y)), 0.000001);
  146. }
  147. // inline const FloatImage operator+(const FloatImage& a, Ipp32f c)
  148. {
  149. FloatImage fimg(3,3);
  150. for(int y=0; y<fimg.height(); ++y)
  151. for(int x=0; x<fimg.width(); ++x)
  152. fimg(x,y) = (x+y+1.0)/2.0;
  153. FloatImage fres = fimg + 115.25;
  154. for(int y=0; y<fimg.height(); ++y)
  155. for(int x=0; x<fimg.width(); ++x)
  156. CPPUNIT_ASSERT_DOUBLES_EQUAL((x+y+1.0)/2.0+115.25, static_cast<double>(fres(x,y)), 0.000001);
  157. }
  158. // inline FloatImage& operator*=(FloatImage& a, const FloatImage& b)
  159. {
  160. FloatImage fimg1(5,5), fimg2(5,5);
  161. for(int y=0; y<fimg1.height(); ++y)
  162. for(int x=0; x<fimg1.width(); ++x)
  163. {
  164. fimg1(x,y) = (x+y+1.0)/2.0;
  165. fimg2(x,y) = (x+y+1.0)*1.25;
  166. }
  167. fimg1 *= fimg2;
  168. for(int y=0; y<fimg1.height(); ++y)
  169. for(int x=0; x<fimg1.width(); ++x)
  170. CPPUNIT_ASSERT_DOUBLES_EQUAL(((x+y+1.0)/2.0)*((x+y+1.0)*1.25),
  171. static_cast<double>(fimg1(x,y)), 0.000001);
  172. }
  173. // inline const FloatImage operator*(const FloatImage& a, const FloatImage& b)
  174. {
  175. FloatImage fimg1(5,5), fimg2(5,5);
  176. for(int y=0; y<fimg1.height(); ++y)
  177. for(int x=0; x<fimg1.width(); ++x)
  178. {
  179. fimg1(x,y) = (x+y+1.0)/2.0;
  180. fimg2(x,y) = (x+y+1.0)*1.25;
  181. }
  182. FloatImage fres = fimg1 * fimg2;
  183. for(int y=0; y<fimg1.height(); ++y)
  184. for(int x=0; x<fimg1.width(); ++x)
  185. CPPUNIT_ASSERT_DOUBLES_EQUAL(((x+y+1.0)/2.0)*((x+y+1.0)*1.25),
  186. static_cast<double>(fres(x,y)), 0.000001);
  187. }
  188. // inline FloatImage& operator*=(FloatImage& a, Ipp32f c)
  189. {
  190. FloatImage fimg(5,5);
  191. for(int y=0; y<fimg.height(); ++y)
  192. for(int x=0; x<fimg.width(); ++x)
  193. fimg(x,y) = (x+y+1.0)/2.0;
  194. fimg *= 2.0;
  195. for(int y=0; y<fimg.height(); ++y)
  196. for(int x=0; x<fimg.width(); ++x)
  197. CPPUNIT_ASSERT_DOUBLES_EQUAL(x+y+1.0, static_cast<double>(fimg(x,y)), 0.000001);
  198. }
  199. // inline Image operator*=(Image& a, Ipp8u c)
  200. {
  201. Image gimg(5,5);
  202. for(int y=0; y<gimg.height(); ++y)
  203. for(int x=0; x<gimg.width(); ++x)
  204. gimg(x,y) = x+y+1;
  205. gimg *= 2;
  206. for(int y=0; y<gimg.height(); ++y)
  207. for(int x=0; x<gimg.width(); ++x)
  208. CPPUNIT_ASSERT_EQUAL((x+y+1)*2, static_cast<int>(gimg(x,y)));
  209. }
  210. // inline const FloatImage operator*(const FloatImage& a, Ipp32f c)
  211. {
  212. FloatImage fimg(5,5);
  213. for(int y=0; y<fimg.height(); ++y)
  214. for(int x=0; x<fimg.width(); ++x)
  215. fimg(x,y) = (x+y+1.0)/2.0;
  216. FloatImage fres = fimg * 2.0;
  217. for(int y=0; y<fimg.height(); ++y)
  218. for(int x=0; x<fimg.width(); ++x)
  219. CPPUNIT_ASSERT_DOUBLES_EQUAL(x+y+1.0, static_cast<double>(fres(x,y)), 0.000001);
  220. }
  221. // inline const Image operator*(const Image& a, Ipp8u c)
  222. {
  223. Image gimg(5,5);
  224. for(int y=0; y<gimg.height(); ++y)
  225. for(int x=0; x<gimg.width(); ++x)
  226. gimg(x,y) = x+y+1;
  227. Image gres = gimg * 2;
  228. for(int y=0; y<gimg.height(); ++y)
  229. for(int x=0; x<gimg.width(); ++x)
  230. CPPUNIT_ASSERT_EQUAL((x+y+1)*2, static_cast<int>(gres(x,y)));
  231. }
  232. // inline FloatImage& operator/=(FloatImage& a, const FloatImage& b)
  233. {
  234. FloatImage fimg1(5,5), fimg2(5,5);
  235. for(int y=0; y<fimg1.height(); ++y)
  236. for(int x=0; x<fimg1.width(); ++x)
  237. {
  238. fimg1(x,y) = (x+y+1.0)*2.0;
  239. fimg2(x,y) = (x+y+1.0);
  240. }
  241. fimg1 /= fimg2;
  242. for(int y=0; y<fimg1.height(); ++y)
  243. for(int x=0; x<fimg1.width(); ++x)
  244. CPPUNIT_ASSERT_DOUBLES_EQUAL(2, static_cast<double>(fimg1(x,y)), 0.000001);
  245. }
  246. // inline const FloatImage operator/(const FloatImage& a, const FloatImage& b)
  247. {
  248. FloatImage fimg1(5,5), fimg2(5,5);
  249. for(int y=0; y<fimg1.height(); ++y)
  250. for(int x=0; x<fimg1.width(); ++x)
  251. {
  252. fimg1(x,y) = (x+y+1.0)*2.0;
  253. fimg2(x,y) = (x+y+1.0);
  254. }
  255. FloatImage fres = fimg1 / fimg2;
  256. for(int y=0; y<fimg1.height(); ++y)
  257. for(int x=0; x<fimg1.width(); ++x)
  258. CPPUNIT_ASSERT_DOUBLES_EQUAL(2, static_cast<double>(fres(x,y)), 0.000001);
  259. }
  260. // inline FloatImage& operator/=(FloatImage& a, Ipp32f c)
  261. {
  262. FloatImage fimg(5,5);
  263. for(int y=0; y<fimg.height(); ++y)
  264. for(int x=0; x<fimg.width(); ++x)
  265. fimg(x,y) = (x+y+1.0)*2.0;
  266. fimg /= 2.0;
  267. for(int y=0; y<fimg.height(); ++y)
  268. for(int x=0; x<fimg.width(); ++x)
  269. CPPUNIT_ASSERT_DOUBLES_EQUAL(x+y+1.0, static_cast<double>(fimg(x,y)), 0.000001);
  270. }
  271. // inline const FloatImage operator/(const FloatImage& a, Ipp32f c)
  272. {
  273. FloatImage fimg(5,5);
  274. for(int y=0; y<fimg.height(); ++y)
  275. for(int x=0; x<fimg.width(); ++x)
  276. fimg(x,y) = (x+y+1.0)*2.0;
  277. FloatImage fres = fimg / 2.0;
  278. for(int y=0; y<fimg.height(); ++y)
  279. for(int x=0; x<fimg.width(); ++x)
  280. CPPUNIT_ASSERT_DOUBLES_EQUAL(x+y+1.0, static_cast<double>(fres(x,y)), 0.000001);
  281. }
  282. }
  283. #endif