render_to_png_async.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_async.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. static IGL_INLINE bool render_to_png_async_helper(
  21. YImage * img,
  22. const std::string png_file,
  23. const bool alpha,
  24. const bool fast)
  25. {
  26. img->flip();
  27. const int width = img->width();
  28. const int height = img->height();
  29. if(!alpha)
  30. {
  31. for(int i = 0;i<width;i++)
  32. for(int j = 0;j<height;j++)
  33. {
  34. img->at(i,j).a = 255;
  35. }
  36. }
  37. return img->save(png_file.c_str(),fast);
  38. }
  39. IGL_INLINE std::thread igl::render_to_png_async(
  40. const std::string png_file,
  41. const int width,
  42. const int height,
  43. const bool alpha,
  44. const bool fast)
  45. {
  46. // Part that should serial
  47. YImage * img = new YImage();
  48. img->resize(width,height);
  49. glReadPixels(
  50. 0,
  51. 0,
  52. width,
  53. height,
  54. GL_RGBA,
  55. GL_UNSIGNED_BYTE,
  56. img->data());
  57. // Part that should be asynchronous
  58. return std::thread(render_to_png_async_helper,img,png_file,alpha,fast);
  59. }