2
0

CaptureScreen.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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-2023 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 "CaptureScreen.h"
  29. #include "TestConfig.h"
  30. #include <RmlUi/Core/Log.h>
  31. #include <RmlUi/Core/MeshUtilities.h>
  32. #include <RmlUi/Core/StringUtilities.h>
  33. #include <RendererExtensions.h>
  34. #include <Shell.h>
  35. #include <cmath>
  36. #define LODEPNG_NO_COMPILE_CPP
  37. #include <lodepng.h>
  38. bool CaptureScreenshot(const Rml::String& filename, int clip_width)
  39. {
  40. using Image = RendererExtensions::Image;
  41. Image image_orig = RendererExtensions::CaptureScreen();
  42. if (!image_orig.data)
  43. {
  44. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not capture screenshot of window.");
  45. return false;
  46. }
  47. if (clip_width == 0)
  48. clip_width = image_orig.width;
  49. // Create a new image flipped vertically, and clipped to the given clip width.
  50. Image image;
  51. image.width = clip_width;
  52. image.height = image_orig.height;
  53. image.num_components = image_orig.num_components;
  54. image.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[image.width * image.height * image.num_components]);
  55. const int c = image.num_components;
  56. for (int y = 0; y < image.height; y++)
  57. {
  58. const int flipped_y = image_orig.height - y - 1;
  59. const int yb = y * image.width * c;
  60. const int yb_orig = flipped_y * image_orig.width * c;
  61. const int wb = image.width * c;
  62. for (int xb = 0; xb < wb; xb++)
  63. {
  64. image.data[yb + xb] = image_orig.data[yb_orig + xb];
  65. }
  66. }
  67. const Rml::String output_path = GetCaptureOutputDirectory() + "/" + filename;
  68. unsigned int lodepng_result = lodepng_encode24_file(output_path.c_str(), image.data.get(), image.width, image.height);
  69. if (lodepng_result)
  70. {
  71. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not write the captured screenshot to %s: %s", output_path.c_str(),
  72. lodepng_error_text(lodepng_result));
  73. return false;
  74. }
  75. return true;
  76. }
  77. struct DeferFree {
  78. unsigned char* ptr = nullptr;
  79. ~DeferFree() { free(ptr); }
  80. };
  81. ComparisonResult CompareScreenToPreviousCapture(Rml::RenderInterface* render_interface, const Rml::String& filename, TextureGeometry* out_reference,
  82. TextureGeometry* out_highlight)
  83. {
  84. using Image = RendererExtensions::Image;
  85. const Rml::String input_path = GetCompareInputDirectory() + "/" + filename;
  86. unsigned char* data_ref = nullptr;
  87. unsigned int w_ref = 0, h_ref = 0;
  88. unsigned int lodepng_result = lodepng_decode32_file(&data_ref, &w_ref, &h_ref, input_path.c_str());
  89. DeferFree defer_free{data_ref};
  90. if (lodepng_result)
  91. {
  92. ComparisonResult result;
  93. result.success = false;
  94. result.error_msg =
  95. Rml::CreateString("Could not read the captured screenshot from %s: %s", input_path.c_str(), lodepng_error_text(lodepng_result));
  96. return result;
  97. }
  98. RMLUI_ASSERT(w_ref > 0 && h_ref > 0 && data_ref);
  99. Image screen = RendererExtensions::CaptureScreen();
  100. if (!screen.data)
  101. {
  102. ComparisonResult result;
  103. result.success = false;
  104. result.error_msg = "Could not capture screenshot of window.";
  105. return result;
  106. }
  107. RMLUI_ASSERT(screen.num_components == 3);
  108. const size_t image_ref_diff_byte_size = w_ref * h_ref * 4;
  109. Image diff;
  110. diff.width = w_ref;
  111. diff.height = h_ref;
  112. diff.num_components = 4;
  113. diff.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[image_ref_diff_byte_size]);
  114. // So we have both images now, compare them! Also create a diff image.
  115. // In case they are not the same size, we require that the reference image size is smaller or equal to the screen
  116. // in both dimensions, and we compare them at the top-left corner.
  117. // Note that the loaded image is flipped vertically compared to the OpenGL capture!
  118. if (screen.width < (int)w_ref || screen.height < (int)h_ref)
  119. {
  120. ComparisonResult result;
  121. result.success = false;
  122. result.error_msg = "Test comparison failed. The screen is smaller than the reference image in one or both dimensions.";
  123. return result;
  124. }
  125. const Rml::Colourb highlight_color(255, 0, 255, 255);
  126. size_t sum_diff = 0;
  127. size_t max_pixel_diff = 0;
  128. for (int y = 0; y < (int)h_ref; y++)
  129. {
  130. const int y_flipped_screen = screen.height - y - 1;
  131. for (int x = 0; x < (int)w_ref; x++)
  132. {
  133. const int i0_screen = (y_flipped_screen * screen.width + x) * 3;
  134. const int i0_ref = (y * w_ref + x) * 4;
  135. const int i0_diff = (y * diff.width + x) * 4;
  136. int pixel_diff = 0;
  137. for (int z = 0; z < 3; z++)
  138. {
  139. const Rml::byte pix_ref = data_ref[i0_ref + z];
  140. const Rml::byte pix_screen = screen.data[i0_screen + z];
  141. pixel_diff += Rml::Math::Absolute((int)pix_ref - (int)pix_screen);
  142. }
  143. diff.data[i0_diff + 0] = (pixel_diff ? highlight_color[0] : screen.data[i0_screen + 0]);
  144. diff.data[i0_diff + 1] = (pixel_diff ? highlight_color[1] : screen.data[i0_screen + 1]);
  145. diff.data[i0_diff + 2] = (pixel_diff ? highlight_color[2] : screen.data[i0_screen + 2]);
  146. diff.data[i0_diff + 3] = highlight_color[3];
  147. sum_diff += (size_t)pixel_diff;
  148. max_pixel_diff = Rml::Math::Max(max_pixel_diff, (size_t)pixel_diff);
  149. }
  150. }
  151. ComparisonResult result;
  152. result.skipped = false;
  153. result.success = true;
  154. result.is_equal = (sum_diff == 0);
  155. result.absolute_difference_sum = sum_diff;
  156. result.max_absolute_difference_single_pixel = max_pixel_diff;
  157. const size_t max_diff = size_t(3 * 255) * size_t(w_ref) * size_t(h_ref);
  158. result.similarity_score = (sum_diff == 0 ? 1.0 : 1.0 - std::log(double(sum_diff)) / std::log(double(max_diff)));
  159. // Optionally render the screen capture or diff to a texture.
  160. auto GenerateGeometry = [&](TextureGeometry& geometry, Rml::Span<const Rml::byte> data, Rml::Vector2i dimensions) -> bool {
  161. ReleaseTextureGeometry(render_interface, geometry);
  162. const Rml::ColourbPremultiplied colour = {255, 255, 255, 255};
  163. const Rml::Vector2f uv_top_left = {0, 0};
  164. const Rml::Vector2f uv_bottom_right = {1, 1};
  165. Rml::MeshUtilities::GenerateQuad(geometry.mesh, Rml::Vector2f(0, 0), Rml::Vector2f((float)w_ref, (float)h_ref), colour, uv_top_left,
  166. uv_bottom_right);
  167. geometry.texture_handle = render_interface->GenerateTexture(data, dimensions);
  168. geometry.geometry_handle = render_interface->CompileGeometry(geometry.mesh.vertices, geometry.mesh.indices);
  169. return geometry.texture_handle && geometry.geometry_handle;
  170. };
  171. if (out_reference && result.success)
  172. result.success = GenerateGeometry(*out_reference, {data_ref, image_ref_diff_byte_size}, {(int)w_ref, (int)h_ref});
  173. if (out_highlight && result.success)
  174. result.success = GenerateGeometry(*out_highlight, {diff.data.get(), image_ref_diff_byte_size}, {diff.width, diff.height});
  175. if (!result.success)
  176. result.error_msg = Rml::CreateString("Could not generate texture from file %s", input_path.c_str());
  177. return result;
  178. }
  179. void RenderTextureGeometry(Rml::RenderInterface* render_interface, TextureGeometry& geometry)
  180. {
  181. if (geometry.geometry_handle && geometry.texture_handle)
  182. {
  183. render_interface->RenderGeometry(geometry.geometry_handle, Rml::Vector2f(0, 0), geometry.texture_handle);
  184. }
  185. }
  186. void ReleaseTextureGeometry(Rml::RenderInterface* render_interface, TextureGeometry& geometry)
  187. {
  188. if (geometry.geometry_handle)
  189. {
  190. render_interface->ReleaseGeometry(geometry.geometry_handle);
  191. geometry.geometry_handle = 0;
  192. }
  193. if (geometry.texture_handle)
  194. {
  195. render_interface->ReleaseTexture(geometry.texture_handle);
  196. geometry.texture_handle = 0;
  197. }
  198. }
  199. // Suppress warnings emitted by lodepng
  200. #if defined(RMLUI_PLATFORM_WIN32) && !defined(__MINGW32__)
  201. #pragma warning(disable : 4334)
  202. #pragma warning(disable : 4267)
  203. #endif
  204. #include <lodepng.cpp>