TestImageFile.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. *
  3. * NICE-Core - efficient algebra and computer vision methods
  4. * - libimage - An image/template for new NICE libraries
  5. * See file License for license information.
  6. */
  7. #ifdef NICE_USELIB_CPPUNIT
  8. #include "TestImageFile.h"
  9. #include <string>
  10. #include <exception>
  11. #include <core/image/ColorImageT.h>
  12. #include <core/image/ImageOperators.h>
  13. #include <sstream>
  14. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  15. #include <iocompression/md5.h>
  16. #endif
  17. using namespace NICE;
  18. using namespace std;
  19. CPPUNIT_TEST_SUITE_REGISTRATION( TestImageFile );
  20. void TestImageFile::setUp() {
  21. }
  22. void TestImageFile::tearDown() {
  23. }
  24. void TestImageFile::testConstructor() {
  25. }
  26. void TestImageFile::testFloatPXM()
  27. {
  28. /*
  29. int width=32;
  30. int height=8;
  31. ColorImage c(width,height);
  32. for(int j=0;j<height;j++)
  33. for(int i=0;i<width;i++)
  34. {
  35. c.setPixel(i,j,0,i*j);
  36. c.setPixel(i,j,1,i*j+i);
  37. c.setPixel(i,j,2,i*j+j*3);
  38. }
  39. Image g(width,height);
  40. rgbToGray(c,&g);
  41. c.write(ImageFile("test-float.ppm"));
  42. ColorImageT<Ipp32f> newc;
  43. newc.read(ImageFile("test-float.ppm"));
  44. FloatImage newf(width,height);
  45. newf.read(ImageFile("test-float.ppm"));
  46. Image newg(width,height);
  47. convertBitDepth(newf,&newg);
  48. newg.write(ImageFile("test-floatcmp.ppm"));
  49. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  50. {
  51. std::stringstream inout; inout << g;
  52. std::stringstream inout1; inout1 << newg;
  53. CPPUNIT_ASSERT_EQUAL(inout.str(),inout1.str());
  54. }
  55. {
  56. std::stringstream inout; inout << c;
  57. std::stringstream inout1; inout1 << newc;
  58. CPPUNIT_ASSERT_EQUAL(inout.str(),inout1.str());
  59. }
  60. #endif
  61. system("rm test-float.ppm");
  62. */
  63. }
  64. void TestImageFile::testFloatImage()
  65. {
  66. /*
  67. int width=32;
  68. int height=8;
  69. ColorImage c(width,height);
  70. for(int j=0;j<height;j++) {
  71. for(int i=0;i<width;i++) {
  72. c.setPixel(i,j,0,i*j);
  73. c.setPixel(i,j,1,i*j+i);
  74. c.setPixel(i,j,2,i*j+j*3);
  75. }
  76. }
  77. Image g(width,height);
  78. rgbToGray(c,&g);
  79. c.write(ImageFile("test-float.png"));
  80. ColorImageT<Ipp32f> newc;
  81. newc.read(ImageFile("test-float.png"));
  82. ColorImage newc2(width,height);
  83. convertBitDepth(newc,&newc2);
  84. FloatImage newf(width,height);
  85. newf.read(ImageFile("test-float.png"));
  86. Image newg(width,height);
  87. convertBitDepth(newf,&newg);
  88. #ifdef NICE_USELIB_LIMUN_IOCOMPRESSION
  89. {
  90. std::stringstream inout; inout << g;
  91. std::stringstream inout1; inout1 << newg;
  92. CPPUNIT_ASSERT_EQUAL(inout.str(),inout1.str());
  93. }
  94. {
  95. std::stringstream inout; inout << c;
  96. std::stringstream inout1; inout1 << newc2;
  97. CPPUNIT_ASSERT_EQUAL(inout.str(),inout1.str());
  98. }
  99. #endif
  100. system("rm test-float.png");
  101. */
  102. }
  103. void TestImageFile::testColorImage() {
  104. int width=31;
  105. int height=8;
  106. ColorImage c(width,height);
  107. for(int i=0;i<width;i++) {
  108. for(int j=0;j<height;j++) {
  109. c.setPixel(i,j,0,i*j);
  110. c.setPixel(i,j,1,i*j+i);
  111. c.setPixel(i,j,2,i*j+j*3);
  112. }
  113. }
  114. c.write(ImageFile("test-color.png"));
  115. ColorImage newc;
  116. newc.read(ImageFile("test-color.png"));
  117. newc.write(ImageFile("test-color.ppm"));
  118. ColorImage newc2(width,height);
  119. newc2.read(ImageFile("test-color.ppm"));
  120. std::stringstream inout;
  121. for(int w=0;w<width;w++) {
  122. for(int h=0;h<height;h++) {
  123. for(int channel=0;channel<3;channel++) {
  124. int g_c = static_cast<int>(c.getPixel(w,h,channel));
  125. int g_newc = static_cast<int>(newc.getPixel(w,h,channel));
  126. int g_newc2 = static_cast<int>(newc2.getPixel(w,h,channel));
  127. CPPUNIT_ASSERT_EQUAL( g_c, g_newc );
  128. CPPUNIT_ASSERT_EQUAL( g_c, g_newc2 );
  129. }
  130. }
  131. }
  132. system("rm test-color.png test-color.ppm");
  133. }
  134. void TestImageFile::testGrayImage() {
  135. int width=9;
  136. int height=32;
  137. Image orig(width,height);
  138. for(int i=0;i<width;i++) {
  139. for(int j=0;j<height;j++) {
  140. orig.setPixel(i,j,i*j);
  141. }
  142. }
  143. orig.write(ImageFile("test.png"));
  144. ImageFile imgf ("test.png");
  145. CPPUNIT_ASSERT_EQUAL( width, (int)imgf.width() );
  146. CPPUNIT_ASSERT_EQUAL( height, (int)imgf.height() );
  147. // getHeader() for ImageMagick formats is not yet defined.
  148. // Therefore, we need the PNG library here to issue getHeader().
  149. ImageFile::Header imgfheader = imgf.getHeader();
  150. CPPUNIT_ASSERT_EQUAL( height, imgfheader.height );
  151. CPPUNIT_ASSERT_EQUAL( width, imgfheader.width );
  152. vector<Image> gray(4);
  153. gray[0].read(ImageFile("test.png"));
  154. gray[0].write(ImageFile("test.pgm"));
  155. gray[1].read(ImageFile("test.pgm"));
  156. gray[1].write(ImageFile("test.ppm"));
  157. gray[2].read(ImageFile("test.ppm"));
  158. ColorImage c(width,height);
  159. c.read(ImageFile("test.pgm"));
  160. c.write(ImageFile("testc.pgm"));
  161. gray[3].read(ImageFile("testc.pgm"));
  162. for(int i=0;i<4;i++)
  163. {
  164. for(int w=0;w<width;w++) {
  165. for(int h=0;h<height;h++) {
  166. int g_w = static_cast<int>(gray[i].getPixel(w,h));
  167. int g_orig = static_cast<int>(orig.getPixel(w,h));
  168. CPPUNIT_ASSERT_EQUAL( g_orig, g_w );
  169. }
  170. }
  171. }
  172. system("rm test.png test.pgm test.ppm testc.pgm");
  173. }
  174. void TestImageFile::testJPG_IO()
  175. {
  176. // Image
  177. {
  178. Image src(4,1);
  179. src(0,0) = 0;
  180. src(1,0) = 100;
  181. src(2,0) = 160;
  182. src(3,0) = 255;
  183. src.write(ImageFile("gray-out.jpg"));
  184. ImageFile imgf ("gray-out.jpg");
  185. CPPUNIT_ASSERT_EQUAL( src.width(), (int)imgf.width() );
  186. CPPUNIT_ASSERT_EQUAL( src.height(), (int)imgf.height() );
  187. Image in;
  188. in.read(ImageFile("gray-out.jpg"));
  189. for(int i=0; i<src.width(); ++i)
  190. CPPUNIT_ASSERT_EQUAL(static_cast<int>(src(i,0)),static_cast<int>(in(i,0)));
  191. system("rm gray-out.jpg");
  192. }
  193. // ImageT<Ipp32f>
  194. {
  195. ImageT<Ipp32f> src(5,1);
  196. src(0,0) = -10.5;
  197. src(1,0) = 0.0;
  198. src(2,0) = 199.75;
  199. src(3,0) = 255.0;
  200. src(4,0) = 300.5;
  201. src.write(ImageFile("gray-float-out.jpg"));
  202. ImageT<Ipp32f> in;
  203. in.read(ImageFile("gray-float-out.jpg"));
  204. CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0,static_cast<int>(in(0,0)), 0.0001);
  205. CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0,static_cast<int>(in(1,0)), 0.0001);
  206. CPPUNIT_ASSERT_DOUBLES_EQUAL(199.0,static_cast<int>(in(2,0)), 0.0001);
  207. CPPUNIT_ASSERT_DOUBLES_EQUAL(255.0,static_cast<int>(in(3,0)), 0.0001);
  208. CPPUNIT_ASSERT_DOUBLES_EQUAL(255.0,static_cast<int>(in(4,0)), 0.0001);
  209. system("rm gray-float-out.jpg");
  210. }
  211. // ColorImage
  212. {
  213. ColorImage src(4,1);
  214. src.setPixelQuick(0,0, 0, 0, 0);
  215. src.setPixelQuick(1,0, 100,100,100);
  216. src.setPixelQuick(2,0, 160,160,160);
  217. src.setPixelQuick(3,0, 255,255,255);
  218. src.write(ImageFile("color-out.jpg"));
  219. ImageFile imgf ("color-out.jpg");
  220. CPPUNIT_ASSERT_EQUAL( src.width(), (int)imgf.width() );
  221. CPPUNIT_ASSERT_EQUAL( src.height(), (int)imgf.height() );
  222. CPPUNIT_ASSERT_EQUAL( imgf.getHeader().channel, 3 );
  223. ColorImage in;
  224. in.read(ImageFile("color-out.jpg"));
  225. for(int i=0; i<src.width(); ++i)
  226. for(int c=0; c<3; ++c)
  227. CPPUNIT_ASSERT_EQUAL(static_cast<int>(src(i,0,c)),static_cast<int>(in(i,0,c)));
  228. system("rm color-out.jpg");
  229. }
  230. // ColorImageT<Ipp32f>
  231. {
  232. ColorImageT<Ipp32f> src(5,1);
  233. src.setPixelQuick(0,0, -10.5, -10.5, -10.5);
  234. src.setPixelQuick(1,0, 0.0, 0.0, 0.0);
  235. src.setPixelQuick(2,0, 199.75, 199.75, 199.75);
  236. src.setPixelQuick(3,0, 255.0, 255.0, 255.0);
  237. src.setPixelQuick(4,0, 300.5, 300.5, 300.5);
  238. src.write(ImageFile("color-float-out.jpg"));
  239. ColorImageT<Ipp32f> in;
  240. in.read(ImageFile("color-float-out.jpg"));
  241. for(int i=0; i<3; ++i) {
  242. CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0,static_cast<int>(in(0,0,i)), 0.0001);
  243. CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0,static_cast<int>(in(1,0,i)), 0.0001);
  244. CPPUNIT_ASSERT_DOUBLES_EQUAL(199.0,static_cast<int>(in(2,0,i)), 0.0001);
  245. CPPUNIT_ASSERT_DOUBLES_EQUAL(255.0,static_cast<int>(in(3,0,i)), 0.0001);
  246. CPPUNIT_ASSERT_DOUBLES_EQUAL(255.0,static_cast<int>(in(4,0,i)), 0.0001);
  247. }
  248. system("rm color-float-out.jpg");
  249. }
  250. }
  251. void TestImageFile::testInvalidFileName() {
  252. ImageFile image_file;
  253. ImageFile::Format fileformat = image_file.name2Format("nodothere");
  254. CPPUNIT_ASSERT_EQUAL(fileformat,ImageFile::FormatUnknown);
  255. }
  256. #endif