texture_from_png.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@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 "texture_from_png.h"
  9. #include <YImage.hpp>
  10. #include <igl/report_gl_error.h>
  11. #ifndef IGL_NO_OPENGL
  12. IGL_INLINE bool igl::texture_from_png(const std::string png_file, GLuint & id)
  13. {
  14. YImage yimg;
  15. if(!yimg.load(png_file.c_str()))
  16. {
  17. return false;
  18. }
  19. // Why do I need to flip?
  20. //yimg.flip();
  21. glGenTextures(1, &id);
  22. glBindTexture(GL_TEXTURE_2D, id);
  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  27. glTexImage2D(
  28. GL_TEXTURE_2D, 0, GL_RGB,
  29. yimg.width(), yimg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, yimg.data());
  30. glBindTexture(GL_TEXTURE_2D, 0);
  31. return true;
  32. }
  33. #endif
  34. IGL_INLINE bool igl::texture_from_png(
  35. const std::string png_file,
  36. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  37. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  38. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B,
  39. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& A
  40. )
  41. {
  42. YImage yimg;
  43. if(!yimg.load(png_file.c_str()))
  44. {
  45. return false;
  46. }
  47. R.resize(yimg.height(),yimg.width());
  48. G.resize(yimg.height(),yimg.width());
  49. B.resize(yimg.height(),yimg.width());
  50. A.resize(yimg.height(),yimg.width());
  51. for (unsigned j=0; j<yimg.height(); ++j)
  52. {
  53. for (unsigned i=0; i<yimg.width(); ++i)
  54. {
  55. R(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).r;
  56. G(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).g;
  57. B(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).b;
  58. A(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).a;
  59. }
  60. }
  61. return true;
  62. }