Browse Source

replacing libpng and yimg by stb_image

Former-commit-id: b0463fd0d4779b3a2e318e3198cda638a0d2b30c
Romain Prévost 9 years ago
parent
commit
41dc4e741b

+ 7 - 8
include/igl/png/render_to_png.cpp

@@ -6,7 +6,7 @@
 // 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 "render_to_png.h"
-#include <YImage.hpp>
+#include <stb_image_write.h>
 
 #ifdef __APPLE__
 #  include <OpenGL/gl.h>
@@ -26,8 +26,7 @@ IGL_INLINE bool igl::png::render_to_png(
   const bool alpha,
   const bool fast)
 {
-  YImage *img = new YImage();
-  img->resize(width,height);
+  unsigned char * data = new unsigned char[width*height];
   glReadPixels(
     0,
     0,
@@ -35,18 +34,18 @@ IGL_INLINE bool igl::png::render_to_png(
     height,
     GL_RGBA,
     GL_UNSIGNED_BYTE,
-    img->data());
-  img->flip();
+    data);
+  //img->flip();
   if(!alpha)
   {
     for(int i = 0;i<width;i++)
     for(int j = 0;j<height;j++)
     {
-      img->at(i,j).a = 255;
+      data[4*(i+j*width)+3] = 255;
     }
   }
-  bool ret = img->save(png_file.c_str(),fast);
-  delete img;
+  bool ret = stbi_write_png(png_file.c_str(), width, height, 4, data, width*sizeof(unsigned char));
+  delete [] data;
   return ret;
 }
 

+ 15 - 19
include/igl/png/render_to_png_async.cpp

@@ -1,12 +1,12 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2013 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 "render_to_png_async.h"
-#include <YImage.hpp>
+#include <stb_image_write.h>
 
 #ifdef __APPLE__
 #  include <OpenGL/gl.h>
@@ -19,28 +19,25 @@
 #  include <GL/gl.h>
 #endif
 
-  
+
 static IGL_INLINE bool render_to_png_async_helper(
-  YImage * img,
+  unsigned char * img, int width, int height,
   const std::string png_file,
   const bool alpha,
   const bool fast)
 {
-
-  img->flip();
-  const int width = img->width();
-  const int height = img->height();
+  //img->flip();
   if(!alpha)
   {
     for(int i = 0;i<width;i++)
     for(int j = 0;j<height;j++)
     {
-      img->at(i,j).a = 255;
+      img[4*(i+j*width)+3] = 255;
     }
   }
 
-  bool ret = img->save(png_file.c_str(),fast);
-  delete img;
+  bool ret = stbi_write_png(png_file.c_str(), width, height, 4, img, width*sizeof(unsigned char));
+  delete [] img;
   return ret;
 }
 
@@ -52,8 +49,7 @@ IGL_INLINE std::thread igl::png::render_to_png_async(
   const bool fast)
 {
   // Part that should serial
-  YImage * img = new YImage();
-  img->resize(width,height);
+  unsigned char * data = new unsigned char[width*height];
   glReadPixels(
     0,
     0,
@@ -61,9 +57,9 @@ IGL_INLINE std::thread igl::png::render_to_png_async(
     height,
     GL_RGBA,
     GL_UNSIGNED_BYTE,
-    img->data());
-  // Part that should be asynchronous  
-  std::thread t(render_to_png_async_helper,img,png_file,alpha,fast);
+    data);
+  // Part that should be asynchronous
+  std::thread t(render_to_png_async_helper,data,width,height,png_file,alpha,fast);
   t.detach();
   return t;
 }

+ 4 - 5
include/igl/png/texture_from_file.cpp

@@ -1,9 +1,9 @@
 // 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_file.h"
 
@@ -12,7 +12,6 @@
 #include "../pathinfo.h"
 #include "../opengl/report_gl_error.h"
 #include "../opengl/texture_from_tga.h"
-#include <YImage.hpp>
 #include <string>
 #include <algorithm>
 #include <iostream>

+ 30 - 22
include/igl/png/texture_from_png.cpp

@@ -8,20 +8,22 @@
 #include "texture_from_png.h"
 
 #include "../opengl/report_gl_error.h"
-#include <YImage.hpp>
+#include <stb_image.h>
 
 IGL_INLINE bool igl::png::texture_from_png(const std::string png_file, const bool flip, GLuint & id)
 {
-  YImage yimg;
-  if(!yimg.load(png_file.c_str()))
-  {
+  int width,height,n;
+  unsigned char *data = stbi_load(png_file.c_str(), &width, &height, &n, 4);
+  if(data == NULL) {
     return false;
   }
+
   // Why do I need to flip?
-  if(flip)
+  /*if(flip)
   {
     yimg.flip();
-  }
+  }*/
+  
   glGenTextures(1, &id);
   glBindTexture(GL_TEXTURE_2D, id);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
@@ -30,8 +32,11 @@ IGL_INLINE bool igl::png::texture_from_png(const std::string png_file, const boo
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   glTexImage2D(
     GL_TEXTURE_2D, 0, GL_RGB,
-    yimg.width(), yimg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, yimg.data());
+    width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
   glBindTexture(GL_TEXTURE_2D, 0);
+
+  stbi_image_free(data);
+
   return true;
 }
 
@@ -49,27 +54,30 @@ IGL_INLINE bool igl::png::texture_from_png(
   Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& A
 )
 {
-  YImage yimg;
-  if(!yimg.load(png_file.c_str()))
-  {
+  int width,height,n;
+  unsigned char *data = stbi_load(png_file.c_str(), &width, &height, &n, 4);
+  if(data == NULL) {
     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());
+  R.resize(height,width);
+  G.resize(height,width);
+  B.resize(height,width);
+  A.resize(height,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;
-      //1A(i,j) = yimg.at(yimg.width()-1-i,yimg.height()-1-j).a;
+  for (unsigned j=0; j<height; ++j) {
+    for (unsigned i=0; i<width; ++i) {
+      // used to flip with libPNG, but I'm not sure if
+      // simply j*width + i wouldn't be better
+      // stb_image uses horizontal scanline an starts top-left corner
+      R(i,j) = data[4*( (width-1-i) + width * (height-1-j) )];
+      G(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 1];
+      B(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 2];
+      //A(i,j) = data[4*( (width-1-i) + width * (height-1-j) ) + 3];
     }
   }
 
+  stbi_image_free(data);
+
   return true;
 }

+ 0 - 7
include/igl/png/texture_from_png.h

@@ -10,7 +10,6 @@
 #include "../igl_inline.h"
 #include <Eigen/Core>
 #include <string>
-#include <Eigen/Core>
 
 #include "../opengl/OpenGL_convenience.h"
 
@@ -28,13 +27,7 @@ namespace igl
     // Returns true on success, false on failure
     IGL_INLINE bool texture_from_png(const std::string png_file, const bool flip, GLuint & id);
     IGL_INLINE bool texture_from_png(const std::string png_file, GLuint & id);
-  }
-}
 
-namespace igl
-{
-  namespace png
-  {
     // Read an image from a .png file and use it as a texture
     //
     // Input: