render_to_png_async.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. bool ret = img->save(png_file.c_str(),fast);
  38. delete img;
  39. return ret;
  40. }
  41. IGL_INLINE std::thread igl::render_to_png_async(
  42. const std::string png_file,
  43. const int width,
  44. const int height,
  45. const bool alpha,
  46. const bool fast)
  47. {
  48. // Part that should serial
  49. YImage * img = new YImage();
  50. img->resize(width,height);
  51. glReadPixels(
  52. 0,
  53. 0,
  54. width,
  55. height,
  56. GL_RGBA,
  57. GL_UNSIGNED_BYTE,
  58. img->data());
  59. // Part that should be asynchronous
  60. std::thread t(render_to_png_async_helper,img,png_file,alpha,fast);
  61. t.detach();
  62. return t;
  63. }