CaptureScreen.cpp 8.5 KB

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