render_to_png_async.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef IGL_NO_BOOST
  9. #include "render_to_png_async.h"
  10. #include <YImage.hpp>
  11. #ifdef __APPLE__
  12. # include <OpenGL/gl.h>
  13. #else
  14. # ifdef _WIN32
  15. # define NOMINMAX
  16. # include <Windows.h>
  17. # undef NOMINMAX
  18. # endif
  19. # include <GL/gl.h>
  20. #endif
  21. static IGL_INLINE bool render_to_png_async_helper(
  22. YImage * img,
  23. const std::string png_file,
  24. const bool alpha,
  25. const bool fast)
  26. {
  27. img->flip();
  28. const int width = img->width();
  29. const int height = img->height();
  30. if(!alpha)
  31. {
  32. for(int i = 0;i<width;i++)
  33. for(int j = 0;j<height;j++)
  34. {
  35. img->at(i,j).a = 255;
  36. }
  37. }
  38. return img->save(png_file.c_str(),fast);
  39. }
  40. IGL_INLINE boost::thread igl::render_to_png_async(
  41. const std::string png_file,
  42. const int width,
  43. const int height,
  44. const bool alpha,
  45. const bool fast)
  46. {
  47. // Part that should serial
  48. YImage * img = new YImage();
  49. img->resize(width,height);
  50. glReadPixels(
  51. 0,
  52. 0,
  53. width,
  54. height,
  55. GL_RGBA,
  56. GL_UNSIGNED_BYTE,
  57. img->data());
  58. // Part that should be asynchronous
  59. return boost::thread(render_to_png_async_helper,img,png_file,alpha,fast);
  60. }
  61. #endif