Browse Source

added an opengl-less version of texture_from_png
added libpng to externals


Former-commit-id: bbe794003a19915eaaea35bd121fa7626cf95008

Daniele Panozzo 10 years ago
parent
commit
62a5987078
3 changed files with 64 additions and 15 deletions
  1. 1 0
      .gitignore
  2. 39 6
      include/igl/png/texture_from_png.cpp
  3. 24 9
      include/igl/png/texture_from_png.h

+ 1 - 0
.gitignore

@@ -69,3 +69,4 @@ external/glfw/build
 *buildXcode*
 tutorial/build*
 tutorial/*/*.mexmaci64
+external/libpng/build

+ 39 - 6
include/igl/png/texture_from_png.cpp

@@ -1,16 +1,17 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "texture_from_png.h"
-#ifndef IGL_NO_OPENGL
 
 #include <YImage.hpp>
 #include <igl/report_gl_error.h>
 
+#ifndef IGL_NO_OPENGL
+
 IGL_INLINE bool igl::texture_from_png(const std::string png_file, GLuint & id)
 {
   YImage yimg;
@@ -27,7 +28,7 @@ IGL_INLINE bool igl::texture_from_png(const std::string png_file, GLuint & id)
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexImage2D(
-    GL_TEXTURE_2D, 0, GL_RGB, 
+    GL_TEXTURE_2D, 0, GL_RGB,
     yimg.width(), yimg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, yimg.data());
   glBindTexture(GL_TEXTURE_2D, 0);
   return true;
@@ -35,3 +36,35 @@ IGL_INLINE bool igl::texture_from_png(const std::string png_file, GLuint & id)
 
 #endif
 
+IGL_INLINE bool igl::texture_from_png(
+  const std::string png_file,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& A
+)
+{
+  YImage yimg;
+  if(!yimg.load(png_file.c_str()))
+  {
+    return false;
+  }
+
+  R.resize(yimg.height(),yimg.width());
+  G.resize(yimg.height(),yimg.width());
+  B.resize(yimg.height(),yimg.width());
+  A.resize(yimg.height(),yimg.width());
+
+  for (unsigned j=0; j<yimg.height(); ++j)
+  {
+    for (unsigned i=0; i<yimg.width(); ++i)
+    {
+      R(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).r;
+      G(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).g;
+      B(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).b;
+      A(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).a;
+    }
+  }
+
+  return true;
+}

+ 24 - 9
include/igl/png/texture_from_png.h

@@ -1,19 +1,18 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_TEXTURE_FROM_PNG_H
 #define IGL_TEXTURE_FROM_PNG_H
-#ifndef IGL_NO_OPENGL
 #include "../igl_inline.h"
+#include <string>
 
+#ifndef IGL_NO_OPENGL
 #include "../OpenGL_convenience.h"
 
-#include <string>
-
 namespace igl
 {
   // Read an image from a .png file and use it as a texture
@@ -25,11 +24,27 @@ namespace igl
   // Returns true on success, false on failure
   IGL_INLINE bool texture_from_png(const std::string png_file, GLuint & id);
 }
+#endif
+
+namespace igl
+{
+  // Read an image from a .png file and use it as a texture
+  //
+  // Input:
+  //  png_file  path to .png file
+  // Output:
+  //  R,G,B,A texture channels
+  // Returns true on success, false on failure
+  IGL_INLINE bool texture_from_png(const std::string png_file,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B,
+  Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& A
+  );
+}
 
 #ifndef IGL_STATIC_LIBRARY
 #  include "texture_from_png.cpp"
 #endif
 
 #endif
-#endif
-