texture_from_png.cpp 2.0 KB

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