Screenshot.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Screenshot.h"
  29. #include <Shell.h>
  30. #include <ShellRenderInterfaceOpenGL.h>
  31. #include <RmlUi/Core/Log.h>
  32. #define LODEPNG_NO_COMPILE_CPP
  33. #include <lodepng.h>
  34. bool CaptureScreenshot(ShellRenderInterfaceOpenGL* shell_renderer, const Rml::String& filename, int clip_width)
  35. {
  36. using Image = ShellRenderInterfaceOpenGL::Image;
  37. Image image_orig = shell_renderer->CaptureScreen();
  38. if (!image_orig.data)
  39. {
  40. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not capture screenshot from OpenGL window.");
  41. return false;
  42. }
  43. if (clip_width == 0)
  44. clip_width = image_orig.width;
  45. // Create a new image flipped vertically, and clipped to the given clip width.
  46. Image image;
  47. image.width = clip_width;
  48. image.height = image_orig.height;
  49. image.num_components = image_orig.num_components;
  50. image.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[image.width * image.height * image.num_components]);
  51. const int c = image.num_components;
  52. for (int y = 0; y < image.height; y++)
  53. {
  54. const int flipped_y = image_orig.height - y - 1;
  55. const int yb = y * image.width * c;
  56. const int yb_orig = flipped_y * image_orig.width * c;
  57. const int wb = image.width * c;
  58. for (int xb = 0; xb < wb; xb++)
  59. {
  60. image.data[yb + xb] = image_orig.data[yb_orig + xb];
  61. }
  62. }
  63. const Rml::String output_directory = GetOutputDirectory() + "/" + filename;
  64. if (lodepng_encode24_file(output_directory.c_str(), image.data.get(), image.width, image.height))
  65. {
  66. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not write the captured screenshot to %s", output_directory.c_str());
  67. return false;
  68. }
  69. return true;
  70. }
  71. Rml::String GetOutputDirectory()
  72. {
  73. #ifdef RMLUI_VISUAL_TESTS_OUTPUT_DIRECTORY
  74. const Rml::String output_directory = Rml::String(RMLUI_VISUAL_TESTS_OUTPUT_DIRECTORY);
  75. #else
  76. const Rml::String output_directory = Shell::FindSamplesRoot() + "../Tests/Output";
  77. #endif
  78. return output_directory;
  79. }
  80. // Suppress warnings emitted by lodepng
  81. #if defined(RMLUI_PLATFORM_WIN32) && !defined(__MINGW32__)
  82. #pragma warning(disable : 4334)
  83. #pragma warning(disable : 4267)
  84. #endif
  85. #include <lodepng.cpp>