readPNG.cpp 1.3 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 "readPNG.h"
  9. #include <stb_image.h>
  10. IGL_INLINE bool igl::png::readPNG(
  11. const std::string png_file,
  12. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  13. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  14. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  15. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A
  16. )
  17. {
  18. int cols,rows,n;
  19. unsigned char *data = stbi_load(png_file.c_str(), &cols, &rows, &n, 4);
  20. if(data == NULL) {
  21. return false;
  22. }
  23. R.resize(cols,rows);
  24. G.resize(cols,rows);
  25. B.resize(cols,rows);
  26. A.resize(cols,rows);
  27. for (unsigned i=0; i<rows; ++i) {
  28. for (unsigned j=0; j<cols; ++j) {
  29. R(j,rows-1-i) = data[4*(j + cols * i) + 0];
  30. G(j,rows-1-i) = data[4*(j + cols * i) + 1];
  31. B(j,rows-1-i) = data[4*(j + cols * i) + 2];
  32. A(j,rows-1-i) = data[4*(j + cols * i) + 3];
  33. }
  34. }
  35. stbi_image_free(data);
  36. return true;
  37. }
  38. #ifdef IGL_STATIC_LIBRARY
  39. // Explicit template specialization
  40. // generated by autoexplicit.sh
  41. #endif