writePNG.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Daniele Panozzo <daniele.panozzo@gmail.com>
  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 "writePNG.h"
  9. #include <stb_image_write.h>
  10. #include <vector>
  11. IGL_INLINE bool igl::png::writePNG(
  12. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  13. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  14. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  15. const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A,
  16. const std::string png_file
  17. )
  18. {
  19. assert((R.rows() == G.rows()) && (G.rows() == B.rows()) && (B.rows() == A.rows()));
  20. assert((R.cols() == G.cols()) && (G.cols() == B.cols()) && (B.cols() == A.cols()));
  21. const int comp = 4; // 4 Channels Red, Green, Blue, Alpha
  22. const int stride_in_bytes = R.rows()*comp; // Lenght of one row in bytes
  23. std::vector<unsigned char> data(R.size()*comp,0); // The image itself;
  24. for (unsigned i = 0; i<R.rows();++i)
  25. {
  26. for (unsigned j = 0; j < R.cols(); ++j)
  27. {
  28. data[(j * R.rows() * comp) + (i * comp) + 0] = R(i,R.cols()-1-j);
  29. data[(j * R.rows() * comp) + (i * comp) + 1] = G(i,R.cols()-1-j);
  30. data[(j * R.rows() * comp) + (i * comp) + 2] = B(i,R.cols()-1-j);
  31. data[(j * R.rows() * comp) + (i * comp) + 3] = A(i,R.cols()-1-j);
  32. }
  33. }
  34. stbi_write_png(png_file.c_str(), R.rows(), R.cols(), comp, data.data(), stride_in_bytes);
  35. return true;
  36. }
  37. #ifdef IGL_STATIC_LIBRARY
  38. // Explicit template specialization
  39. // generated by autoexplicit.sh
  40. #endif