CaptureScreen.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <Shell.h>
  30. #include <ShellRenderInterfaceOpenGL.h>
  31. #include <RmlUi/Core/Log.h>
  32. #include <RmlUi/Core/StringUtilities.h>
  33. #include <cmath>
  34. #define LODEPNG_NO_COMPILE_CPP
  35. #include <lodepng.h>
  36. Rml::String GetInputDirectory()
  37. {
  38. #ifdef RMLUI_VISUAL_TESTS_INPUT_DIRECTORY
  39. const Rml::String input_directory = Rml::String(RMLUI_VISUAL_TESTS_INPUT_DIRECTORY);
  40. #else
  41. const Rml::String input_directory = Shell::FindSamplesRoot() + "../Tests/Output";
  42. #endif
  43. return input_directory;
  44. }
  45. Rml::String GetOutputDirectory()
  46. {
  47. #ifdef RMLUI_VISUAL_TESTS_OUTPUT_DIRECTORY
  48. const Rml::String output_directory = Rml::String(RMLUI_VISUAL_TESTS_OUTPUT_DIRECTORY);
  49. #else
  50. const Rml::String output_directory = Shell::FindSamplesRoot() + "../Tests/Output";
  51. #endif
  52. return output_directory;
  53. }
  54. bool CaptureScreenshot(ShellRenderInterfaceOpenGL* shell_renderer, const Rml::String& filename, int clip_width)
  55. {
  56. using Image = ShellRenderInterfaceOpenGL::Image;
  57. Image image_orig = shell_renderer->CaptureScreen();
  58. if (!image_orig.data)
  59. {
  60. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not capture screenshot from OpenGL window.");
  61. return false;
  62. }
  63. if (clip_width == 0)
  64. clip_width = image_orig.width;
  65. // Create a new image flipped vertically, and clipped to the given clip width.
  66. Image image;
  67. image.width = clip_width;
  68. image.height = image_orig.height;
  69. image.num_components = image_orig.num_components;
  70. image.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[image.width * image.height * image.num_components]);
  71. const int c = image.num_components;
  72. for (int y = 0; y < image.height; y++)
  73. {
  74. const int flipped_y = image_orig.height - y - 1;
  75. const int yb = y * image.width * c;
  76. const int yb_orig = flipped_y * image_orig.width * c;
  77. const int wb = image.width * c;
  78. for (int xb = 0; xb < wb; xb++)
  79. {
  80. image.data[yb + xb] = image_orig.data[yb_orig + xb];
  81. }
  82. }
  83. const Rml::String output_path = GetOutputDirectory() + "/" + filename;
  84. unsigned int lodepng_result = lodepng_encode24_file(output_path.c_str(), image.data.get(), image.width, image.height);
  85. if (lodepng_result)
  86. {
  87. 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));
  88. return false;
  89. }
  90. return true;
  91. }
  92. struct DeferFree {
  93. unsigned char* ptr = nullptr;
  94. ~DeferFree() { free(ptr); }
  95. };
  96. ComparisonResult CompareScreenToPreviousCapture(ShellRenderInterfaceOpenGL* shell_renderer, const Rml::String& filename)
  97. {
  98. using Image = ShellRenderInterfaceOpenGL::Image;
  99. const Rml::String input_path = GetInputDirectory() + "/" + filename;
  100. unsigned char* data_ref = nullptr;
  101. unsigned int w_ref = 0, h_ref = 0;
  102. unsigned int lodepng_result = lodepng_decode24_file(&data_ref, &w_ref, &h_ref, input_path.c_str());
  103. DeferFree defer_free{ data_ref };
  104. if (lodepng_result)
  105. {
  106. ComparisonResult result{ false };
  107. result.error_msg = Rml::CreateString(1024, "Could not read the captured screenshot from %s: %s", input_path.c_str(), lodepng_error_text(lodepng_result));
  108. return result;
  109. }
  110. RMLUI_ASSERT(w_ref > 0 && h_ref > 0 && data_ref);
  111. Image screen = shell_renderer->CaptureScreen();
  112. if (!screen.data)
  113. {
  114. ComparisonResult result{ false };
  115. result.error_msg = "Could not capture screen from OpenGL window.";
  116. return result;
  117. }
  118. RMLUI_ASSERT(screen.num_components == 3);
  119. Image diff;
  120. diff.width = w_ref;
  121. diff.height = h_ref;
  122. diff.num_components = 3;
  123. diff.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[diff.width * diff.height * diff.num_components]);
  124. // So we have both images now, compare them! Also create a diff image.
  125. // In case they are not the same size, we require that the reference image size is smaller or equal to the screen
  126. // in both dimensions, and we compare them at the top-left corner.
  127. // Note that the loaded image is flipped vertically compared to the OpenGL capture!
  128. if (screen.width < (int)w_ref || screen.height < (int)h_ref)
  129. {
  130. ComparisonResult result{ false };
  131. result.error_msg = "Test comparison failed. The screen is smaller than the reference image in one or both dimensions.";
  132. return result;
  133. }
  134. size_t sum_diff = 0;
  135. constexpr int c = 3;
  136. for (int y = 0; y < (int)h_ref; y++)
  137. {
  138. const int y_flipped_screen = screen.height - y - 1;
  139. const int yb_screen = y_flipped_screen * screen.width * c;
  140. const int wb_ref = w_ref * c;
  141. const int yb_ref = y * w_ref * c;
  142. for (int xb = 0; xb < wb_ref; xb++)
  143. {
  144. const int i_ref = yb_ref + xb;
  145. diff.data[i_ref] = (Rml::byte)std::abs((int)data_ref[i_ref] - (int)screen.data[yb_screen + xb]);
  146. sum_diff += (size_t)diff.data[i_ref];
  147. }
  148. }
  149. ComparisonResult result;
  150. result.success = true;
  151. result.is_equal = (sum_diff == 0);
  152. result.absolute_difference_sum = sum_diff;
  153. const size_t max_diff = size_t(c * 255) * size_t(w_ref) * size_t(h_ref);
  154. result.similarity_score = (sum_diff == 0 ? 1.0 : 1.0 - std::log(double(sum_diff)) / std::log(double(max_diff)));
  155. // Write the diff image to file if they are not equal.
  156. if (!result.is_equal)
  157. {
  158. const Rml::String output_path = GetOutputDirectory() + "/diff-" + filename;
  159. lodepng_result = lodepng_encode24_file(output_path.c_str(), diff.data.get(), diff.width, diff.height);
  160. if (lodepng_result)
  161. {
  162. // We still report it as a success.
  163. result.error_msg = "Could not write output diff image to " + output_path + Rml::String(": ") + lodepng_error_text(lodepng_result);
  164. }
  165. }
  166. return result;
  167. }
  168. // Suppress warnings emitted by lodepng
  169. #if defined(RMLUI_PLATFORM_WIN32) && !defined(__MINGW32__)
  170. #pragma warning(disable : 4334)
  171. #pragma warning(disable : 4267)
  172. #endif
  173. #include <lodepng.cpp>