render_to_png_async.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <stb_image_write.h>
  10. #include "../opengl/OpenGL_convenience.h"
  11. static IGL_INLINE bool render_to_png_async_helper(
  12. unsigned char * img, int width, int height,
  13. const std::string png_file,
  14. const bool alpha,
  15. const bool fast)
  16. {
  17. //img->flip();
  18. if(!alpha)
  19. {
  20. for(int i = 0;i<width;i++)
  21. for(int j = 0;j<height;j++)
  22. {
  23. img[4*(i+j*width)+3] = 255;
  24. }
  25. }
  26. bool ret = stbi_write_png(png_file.c_str(), width, height, 4, img, width*sizeof(unsigned char));
  27. delete [] img;
  28. return ret;
  29. }
  30. IGL_INLINE std::thread igl::png::render_to_png_async(
  31. const std::string png_file,
  32. const int width,
  33. const int height,
  34. const bool alpha,
  35. const bool fast)
  36. {
  37. // Part that should serial
  38. unsigned char * data = new unsigned char[width*height];
  39. glReadPixels(
  40. 0,
  41. 0,
  42. width,
  43. height,
  44. GL_RGBA,
  45. GL_UNSIGNED_BYTE,
  46. data);
  47. // Part that should be asynchronous
  48. std::thread t(render_to_png_async_helper,data,width,height,png_file,alpha,fast);
  49. t.detach();
  50. return t;
  51. }