CaptureScreen.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "CaptureScreen.h"
  29. #include "TestConfig.h"
  30. #include <RendererExtensions.h>
  31. #include <RmlUi/Core/GeometryUtilities.h>
  32. #include <RmlUi/Core/Log.h>
  33. #include <RmlUi/Core/StringUtilities.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(), lodepng_error_text(lodepng_result));
  72. return false;
  73. }
  74. return true;
  75. }
  76. struct DeferFree {
  77. unsigned char* ptr = nullptr;
  78. ~DeferFree() { free(ptr); }
  79. };
  80. ComparisonResult CompareScreenToPreviousCapture(Rml::RenderInterface* render_interface, const Rml::String& filename, TextureGeometry* out_geometry)
  81. {
  82. using Image = RendererExtensions::Image;
  83. const Rml::String input_path = GetCompareInputDirectory() + "/" + filename;
  84. unsigned char* data_ref = nullptr;
  85. unsigned int w_ref = 0, h_ref = 0;
  86. unsigned int lodepng_result = lodepng_decode32_file(&data_ref, &w_ref, &h_ref, input_path.c_str());
  87. DeferFree defer_free{ data_ref };
  88. if (lodepng_result)
  89. {
  90. ComparisonResult result;
  91. result.success = false;
  92. result.error_msg = Rml::CreateString(1024, "Could not read the captured screenshot from %s: %s", input_path.c_str(), lodepng_error_text(lodepng_result));
  93. return result;
  94. }
  95. RMLUI_ASSERT(w_ref > 0 && h_ref > 0 && data_ref);
  96. // Optionally render the previous capture to a texture.
  97. if (out_geometry)
  98. {
  99. if (!render_interface->GenerateTexture(out_geometry->texture_handle, data_ref, Rml::Vector2i((int)w_ref, (int)h_ref)))
  100. {
  101. ComparisonResult result;
  102. result.success = false;
  103. result.error_msg = Rml::CreateString(1024, "Could not generate texture from file %s", input_path.c_str());
  104. return result;
  105. }
  106. const Rml::Colourb colour = {255, 255, 255, 255};
  107. const Rml::Vector2f uv_top_left = {0, 0};
  108. const Rml::Vector2f uv_bottom_right = {1, 1};
  109. Rml::GeometryUtilities::GenerateQuad(out_geometry->vertices, out_geometry->indices, Rml::Vector2f(0, 0),
  110. Rml::Vector2f((float)w_ref, (float)h_ref), colour, uv_top_left, uv_bottom_right, 0);
  111. }
  112. Image screen = RendererExtensions::CaptureScreen();
  113. if (!screen.data)
  114. {
  115. ComparisonResult result;
  116. result.success = false;
  117. result.error_msg = "Could not capture screenshot of window.";
  118. return result;
  119. }
  120. RMLUI_ASSERT(screen.num_components == 3);
  121. Image diff;
  122. diff.width = w_ref;
  123. diff.height = h_ref;
  124. diff.num_components = 3;
  125. diff.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[diff.width * diff.height * diff.num_components]);
  126. // So we have both images now, compare them! Also create a diff image.
  127. // In case they are not the same size, we require that the reference image size is smaller or equal to the screen
  128. // in both dimensions, and we compare them at the top-left corner.
  129. // Note that the loaded image is flipped vertically compared to the OpenGL capture!
  130. if (screen.width < (int)w_ref || screen.height < (int)h_ref)
  131. {
  132. ComparisonResult result;
  133. result.success = false;
  134. result.error_msg = "Test comparison failed. The screen is smaller than the reference image in one or both dimensions.";
  135. return result;
  136. }
  137. size_t sum_diff = 0;
  138. constexpr int c = 3;
  139. for (int y = 0; y < (int)h_ref; y++)
  140. {
  141. const int y_flipped_screen = screen.height - y - 1;
  142. const int yb_screen = y_flipped_screen * screen.width * c;
  143. const int wb_ref = w_ref * c;
  144. const int yb_ref = y * w_ref * c;
  145. for (int xb = 0; xb < wb_ref; xb++)
  146. {
  147. const int i_ref = yb_ref + xb;
  148. Rml::byte pix_ref = data_ref[(i_ref * 4) / 3];
  149. Rml::byte pix_screen = screen.data[yb_screen + xb];
  150. diff.data[i_ref] = (Rml::byte)std::abs((int)pix_ref - (int)pix_screen);
  151. sum_diff += (size_t)diff.data[i_ref];
  152. }
  153. }
  154. ComparisonResult result;
  155. result.skipped = false;
  156. result.success = true;
  157. result.is_equal = (sum_diff == 0);
  158. result.absolute_difference_sum = sum_diff;
  159. const size_t max_diff = size_t(c * 255) * size_t(w_ref) * size_t(h_ref);
  160. result.similarity_score = (sum_diff == 0 ? 1.0 : 1.0 - std::log(double(sum_diff)) / std::log(double(max_diff)));
  161. return result;
  162. }
  163. void RenderTextureGeometry(Rml::RenderInterface* render_interface, TextureGeometry& geometry)
  164. {
  165. if (geometry.texture_handle)
  166. render_interface->RenderGeometry(geometry.vertices, 4, geometry.indices, 6, geometry.texture_handle, Rml::Vector2f(0, 0));
  167. }
  168. void ReleaseTextureGeometry(Rml::RenderInterface* render_interface, TextureGeometry& geometry)
  169. {
  170. if (geometry.texture_handle)
  171. {
  172. render_interface->ReleaseTexture(geometry.texture_handle);
  173. geometry.texture_handle = 0;
  174. }
  175. }
  176. // Suppress warnings emitted by lodepng
  177. #if defined(RMLUI_PLATFORM_WIN32) && !defined(__MINGW32__)
  178. #pragma warning(disable : 4334)
  179. #pragma warning(disable : 4267)
  180. #endif
  181. #include <lodepng.cpp>