write_image.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Daniele Panozzo <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "write_image.h"
  9. #include <stb_image_write.h>
  10. #include <vector>
  11. #include "../pathinfo.h"
  12. IGL_INLINE bool igl::stb::write_image(
  13. const std::string image_file,
  14. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  15. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  16. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  17. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A,
  18. const int quality
  19. )
  20. {
  21. assert((R.rows() == G.rows()) && (G.rows() == B.rows()) && (B.rows() == A.rows()));
  22. assert((R.cols() == G.cols()) && (G.cols() == B.cols()) && (B.cols() == A.cols()));
  23. const int comp = 4; // 4 Channels Red, Green, Blue, Alpha
  24. std::vector<unsigned char> data(R.size()*comp,0); // The image itself;
  25. for (unsigned i = 0; i<R.rows();++i)
  26. {
  27. for (unsigned j = 0; j < R.cols(); ++j)
  28. {
  29. data[(j * R.rows() * comp) + (i * comp) + 0] = R(i,R.cols()-1-j);
  30. data[(j * R.rows() * comp) + (i * comp) + 1] = G(i,R.cols()-1-j);
  31. data[(j * R.rows() * comp) + (i * comp) + 2] = B(i,R.cols()-1-j);
  32. data[(j * R.rows() * comp) + (i * comp) + 3] = A(i,R.cols()-1-j);
  33. }
  34. }
  35. return write_image(image_file,R.rows(),R.cols(),data.data(),quality);
  36. }
  37. IGL_INLINE bool igl::stb::write_image(
  38. const std::string image_file,
  39. const int width,
  40. const int height,
  41. const unsigned char * rgba_data,
  42. const int quality)
  43. {
  44. const int comp = 4; // 4 Channels Red, Green, Blue, Alpha
  45. const int stride_in_bytes = width*comp; // Length of one row in bytes
  46. std::string d,b,e,f;
  47. pathinfo(image_file,d,b,e,f);
  48. if(e == "png")
  49. {
  50. return stbi_write_png(image_file.c_str(), width, height, comp, rgba_data, stride_in_bytes)!=0;
  51. } else if( e == "tga")
  52. {
  53. return stbi_write_tga(image_file.c_str(), width, height, comp, rgba_data)!=0;
  54. } else if( e == "bmp")
  55. {
  56. return stbi_write_bmp(image_file.c_str(), width, height, comp, rgba_data)!=0;
  57. } else if( e == "jpg")
  58. {
  59. return stbi_write_jpg(image_file.c_str(), width, height, comp, rgba_data,quality)!=0;
  60. } else
  61. {
  62. // unsupported file format
  63. return false;
  64. }
  65. // not yet.
  66. //} else if( e == "hdr")
  67. // {
  68. // return stbi_write_hdr(image_file.c_str(), width, height, comp, rgba_data.rgba_data())!=0;
  69. // }
  70. }
  71. #ifdef IGL_STATIC_LIBRARY
  72. // Explicit template instantiation
  73. // generated by autoexplicit.sh
  74. #endif