read_write.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <test_common.h>
  2. #include <catch2/catch.hpp>
  3. #include <igl/stb/write_image.h>
  4. #include <igl/stb/read_image.h>
  5. namespace {
  6. // magick -size 17x29 gradient:white-black -depth 8 test.png
  7. // xxd -i test.png
  8. #include "test_png.h"
  9. // convert test.png test.bmp
  10. #include "test_bmp.h"
  11. //convert -quality 100% test.png test.jpg
  12. #include "test_jpg.h"
  13. // convert test.png test.tga
  14. #include "test_tga.h"
  15. // convert test.png test.pgm
  16. #include "test_pgm.h"
  17. #include "test_reference.h"
  18. static bool dump_to_file(const unsigned char *buf,int size,const char* fname)
  19. {
  20. //generate file:
  21. FILE *out;
  22. if(out=fopen(fname,"wb"))
  23. {
  24. bool r=(fwrite(buf,1,size,out)==size);
  25. r=(fclose(out)==0)&&r;
  26. return r;
  27. } else {
  28. return false;
  29. }
  30. }
  31. }
  32. TEST_CASE("read_image","[igl/stb]")
  33. {
  34. // convert -verbose test.png -depth 8 -colorspace Gray -format pgm test.pgm
  35. // xxd -i test.pgm # skip header
  36. REQUIRE(dump_to_file(test_png,test_png_len,"test.png"));
  37. REQUIRE(dump_to_file(test_bmp,test_bmp_len,"test.bmp"));
  38. REQUIRE(dump_to_file(test_tga,test_tga_len,"test.tga"));
  39. REQUIRE(dump_to_file(test_jpg,test_jpg_len,"test.jpg"));
  40. REQUIRE(dump_to_file(test_pgm,test_pgm_len,"test.pgm"));
  41. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R,G,B,A;
  42. Eigen::Map<const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic, Eigen::RowMajor > > ref_image(ref_image_raw, 29, 17) ;
  43. for(auto i:{"test.png","test.bmp","test.tga","test.jpg", "test.pgm"})
  44. {
  45. WHEN(i) {
  46. REQUIRE(igl::stb::read_image(i,R,G,B,A));
  47. //check the size
  48. REQUIRE(R.rows()==17);
  49. REQUIRE(R.cols()==29);
  50. REQUIRE(G.rows()==17);
  51. REQUIRE(G.cols()==29);
  52. REQUIRE(B.rows()==17);
  53. REQUIRE(B.cols()==29);
  54. REQUIRE(A.rows()==17);
  55. REQUIRE(A.cols()==29);
  56. // check the contents, it is transposed and upside down
  57. for(int i=0;i<17;++i)
  58. for(int j=0;j<29;++j)
  59. {
  60. REQUIRE( (unsigned int)R(i,28-j) == (unsigned int)ref_image(j,i) );
  61. REQUIRE( (unsigned int)G(i,28-j) == (unsigned int)ref_image(j,i) );
  62. REQUIRE( (unsigned int)A(i,j) == 255 );
  63. }
  64. }
  65. }
  66. }
  67. TEST_CASE("write_image","[igl/stb]")
  68. {
  69. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R(17,29),G(17,29),B(17,29),A(17,29),A2(17,29);
  70. for(int i=0;i<17;++i)
  71. for(int j=0;j<29;++j)
  72. {
  73. R(i,j)=(i+j*17)%255;
  74. G(i,j)=(i+j*17+1)%255;
  75. B(i,j)=(i+j*17+2)%255;
  76. A(i,j)=i+j;
  77. A2(i,j)=255;
  78. }
  79. for(auto i:{"check.png","check.bmp","check.tga","check.jpg"})
  80. {
  81. WHEN(i) {
  82. if(i=="check.png" || i=="check.tga")
  83. REQUIRE(igl::stb::write_image(R, G, B, A, i, 100));
  84. else // Alpha channel somehow affects other colors in BMP, even though it's not saved
  85. REQUIRE(igl::stb::write_image(R, G, B, A2, i, 100));
  86. // read it back
  87. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> _R,_G,_B,_A;
  88. REQUIRE(igl::stb::read_image(i, _R,_G,_B,_A));
  89. //only PNG and TGA files support alpha channel
  90. if(i=="check.png" || i=="check.tga")
  91. REQUIRE((A.array()==_A.array()).all());
  92. if(i=="check.jpg")
  93. {
  94. // somehow jpeg loses information even with 100% quality
  95. REQUIRE((R.cast<float>()-_R.cast<float>()).norm()<R.size()/20.);
  96. REQUIRE((G.cast<float>()-_G.cast<float>()).norm()<G.size()/20.);
  97. REQUIRE((B.cast<float>()-_B.cast<float>()).norm()<B.size()/20.);
  98. } else {
  99. REQUIRE((R.array()==_R.array()).all());
  100. REQUIRE((G.array()==_G.array()).all());
  101. REQUIRE((B.array()==_B.array()).all());
  102. }
  103. }
  104. }
  105. }