render_to_png.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "render_to_png.h"
  9. #include <stb_image_write.h>
  10. #include "../opengl/OpenGL_convenience.h"
  11. IGL_INLINE bool igl::png::render_to_png(
  12. const std::string png_file,
  13. const int width,
  14. const int height,
  15. const bool alpha,
  16. const bool fast)
  17. {
  18. unsigned char * data = new unsigned char[width*height];
  19. glReadPixels(
  20. 0,
  21. 0,
  22. width,
  23. height,
  24. GL_RGBA,
  25. GL_UNSIGNED_BYTE,
  26. data);
  27. //img->flip();
  28. if(!alpha)
  29. {
  30. for(int i = 0;i<width;i++)
  31. for(int j = 0;j<height;j++)
  32. {
  33. data[4*(i+j*width)+3] = 255;
  34. }
  35. }
  36. bool ret = stbi_write_png(png_file.c_str(), width, height, 4, data, width*sizeof(unsigned char));
  37. delete [] data;
  38. return ret;
  39. }
  40. #ifdef IGL_STATIC_LIBRARY
  41. // Explicit template specialization
  42. #endif