CaptureScreen.cpp 6.6 KB

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