texture_from_png.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, const bool flip, 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. if(flip)
  20. {
  21. yimg.flip();
  22. }
  23. glGenTextures(1, &id);
  24. glBindTexture(GL_TEXTURE_2D, id);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  29. glTexImage2D(
  30. GL_TEXTURE_2D, 0, GL_RGB,
  31. yimg.width(), yimg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, yimg.data());
  32. glBindTexture(GL_TEXTURE_2D, 0);
  33. return true;
  34. }
  35. IGL_INLINE bool igl::png::texture_from_png(const std::string png_file, GLuint & id)
  36. {
  37. return texture_from_png(png_file,false,id);
  38. }
  39. IGL_INLINE bool igl::png::texture_from_png(
  40. const std::string png_file,
  41. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  42. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  43. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B,
  44. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& A
  45. )
  46. {
  47. YImage yimg;
  48. if(!yimg.load(png_file.c_str()))
  49. {
  50. return false;
  51. }
  52. R.resize(yimg.height(),yimg.width());
  53. G.resize(yimg.height(),yimg.width());
  54. B.resize(yimg.height(),yimg.width());
  55. A.resize(yimg.height(),yimg.width());
  56. for (unsigned j=0; j<yimg.height(); ++j)
  57. {
  58. for (unsigned i=0; i<yimg.width(); ++i)
  59. {
  60. R(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).r;
  61. G(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).g;
  62. B(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).b;
  63. //1A(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).a;
  64. }
  65. }
  66. return true;
  67. }