CaptureScreen.cpp 7.9 KB

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