render_to_png.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 <YImage.hpp>
  10. #ifdef __APPLE__
  11. # include <OpenGL/gl.h>
  12. #else
  13. # ifdef _WIN32
  14. # define NOMINMAX
  15. # include <Windows.h>
  16. # undef NOMINMAX
  17. # endif
  18. # include <GL/gl.h>
  19. #endif
  20. IGL_INLINE bool igl::render_to_png(
  21. const std::string png_file,
  22. const int width,
  23. const int height,
  24. const bool alpha,
  25. const bool fast)
  26. {
  27. YImage *img = new YImage();
  28. img->resize(width,height);
  29. glReadPixels(
  30. 0,
  31. 0,
  32. width,
  33. height,
  34. GL_RGBA,
  35. GL_UNSIGNED_BYTE,
  36. img->data());
  37. img->flip();
  38. if(!alpha)
  39. {
  40. for(int i = 0;i<width;i++)
  41. for(int j = 0;j<height;j++)
  42. {
  43. img->at(i,j).a = 255;
  44. }
  45. }
  46. bool ret = img->save(png_file.c_str(),fast);
  47. delete img;
  48. return ret;
  49. }