|
1 | 1 | #include "ScreenCapture.h"
|
| 2 | +#include "ScreenCapture_C_API.h" |
2 | 3 | #include "internal/SCCommon.h" //DONT USE THIS HEADER IN PRODUCTION CODE!!!! ITS INTERNAL FOR A REASON IT WILL CHANGE!!! ITS HERE FOR TESTS ONLY!!!
|
3 | 4 | #include <algorithm>
|
4 | 5 | #include <atomic>
|
|
19 | 20 | #include "lodepng.h"
|
20 | 21 | /////////////////////////////////////////////////////////////////////////
|
21 | 22 |
|
| 23 | +void TestCopyContiguous() |
| 24 | +{ |
| 25 | + |
| 26 | + constexpr auto VALID(static_cast<unsigned char>(0xFF)); |
| 27 | + constexpr auto INVALID(static_cast<unsigned char>(0xFE)); |
| 28 | + constexpr auto PIXEL_DEPTH(sizeof(SL::Screen_Capture::ImageBGRA)); |
| 29 | + constexpr unsigned WIDTH(256), HEIGHT(256); |
| 30 | + |
| 31 | + std::vector<SL::Screen_Capture::ImageBGRA> strided; |
| 32 | + |
| 33 | + for (unsigned row(0); row < HEIGHT; ++row) |
| 34 | + { |
| 35 | + for (unsigned col(0); col < WIDTH; ++col) |
| 36 | + { |
| 37 | + strided.push_back(SL::Screen_Capture::ImageBGRA{VALID, VALID, VALID, VALID}); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + auto bytes = strided.size() * PIXEL_DEPTH; |
| 42 | + std::vector<unsigned char> contiguous(bytes, static_cast<unsigned char>(0)); |
| 43 | + auto image = SL::Screen_Capture::Image{{ 0, 0, WIDTH, HEIGHT }, 0, true, strided.data() }; |
| 44 | + |
| 45 | + auto result = SCL_Utility_CopyToContiguous(contiguous.data(), &image); |
| 46 | + auto distance = std::distance(contiguous.data(), static_cast<unsigned char*>(result)); |
| 47 | + |
| 48 | + if (distance != (WIDTH * HEIGHT * PIXEL_DEPTH)) std::abort(); |
| 49 | + |
| 50 | + auto const begin(contiguous.begin()), end(contiguous.end()); |
| 51 | + |
| 52 | + for (auto current(begin); current != end; ++current) |
| 53 | + { |
| 54 | + if (*current != VALID) std::abort(); |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +void TestCopyNonContiguous() |
| 60 | +{ |
| 61 | + |
| 62 | + constexpr auto VALID(static_cast<unsigned char>(0xFF)); |
| 63 | + constexpr auto INVALID(static_cast<unsigned char>(0xFE)); |
| 64 | + constexpr auto PIXEL_DEPTH(sizeof(SL::Screen_Capture::ImageBGRA)); |
| 65 | + constexpr unsigned WIDTH(256), HEIGHT(256), PADDING(64), STRIDE_IN_BYTES((WIDTH + PADDING) * PIXEL_DEPTH); |
| 66 | + |
| 67 | + std::vector<SL::Screen_Capture::ImageBGRA> strided; |
| 68 | + |
| 69 | + for (unsigned row(0); row < HEIGHT; ++row) |
| 70 | + { |
| 71 | + |
| 72 | + for (unsigned col(0); col < WIDTH; ++col) |
| 73 | + { |
| 74 | + strided.push_back(SL::Screen_Capture::ImageBGRA{VALID, VALID, VALID, VALID}); |
| 75 | + } |
| 76 | + |
| 77 | + for (unsigned pad(0); pad < PADDING; ++pad) |
| 78 | + { |
| 79 | + strided.push_back(SL::Screen_Capture::ImageBGRA{INVALID, INVALID, INVALID, INVALID}); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + auto bytes = strided.size() * PIXEL_DEPTH; |
| 85 | + std::vector<unsigned char> contiguous(bytes, static_cast<unsigned char>(0)); |
| 86 | + auto image = SL::Screen_Capture::Image{{ 0, 0, WIDTH, HEIGHT }, STRIDE_IN_BYTES, false, strided.data() }; |
| 87 | + |
| 88 | + auto result = SCL_Utility_CopyToContiguous(contiguous.data(), &image); |
| 89 | + auto distance = std::distance(contiguous.data(), static_cast<unsigned char*>(result)); |
| 90 | + |
| 91 | + // Ensures that the pointer incremented by only the amount written. |
| 92 | + if (distance != (WIDTH * HEIGHT * PIXEL_DEPTH)) std::abort(); |
| 93 | + |
| 94 | + auto const begin(contiguous.begin()); |
| 95 | + auto contiguousEnd(begin), absoluteEnd(contiguous.end()); |
| 96 | + |
| 97 | + std::advance(contiguousEnd, WIDTH * HEIGHT * PIXEL_DEPTH); |
| 98 | + |
| 99 | + for (auto current(begin); current != contiguousEnd; ++current) |
| 100 | + { |
| 101 | + if (*current != VALID) std::abort(); |
| 102 | + } |
| 103 | + |
| 104 | + for (auto current(contiguousEnd); current != absoluteEnd; ++current) |
| 105 | + { |
| 106 | + if (*current != 0) std::abort(); |
| 107 | + } |
| 108 | + |
| 109 | +} |
| 110 | + |
22 | 111 | void ExtractAndConvertToRGBA(const SL::Screen_Capture::Image &img, unsigned char *dst, size_t dst_size)
|
23 | 112 | {
|
24 | 113 | assert(dst_size >= static_cast<size_t>(SL::Screen_Capture::Width(img) * SL::Screen_Capture::Height(img) * sizeof(SL::Screen_Capture::ImageBGRA)));
|
@@ -73,7 +162,7 @@ void createframegrabber()
|
73 | 162 | return mons;
|
74 | 163 | })
|
75 | 164 | ->onFrameChanged([&](const SL::Screen_Capture::Image &img, const SL::Screen_Capture::Monitor &monitor) {
|
76 |
| - //std::cout << "Difference detected! " << img.Bounds << std::endl; |
| 165 | + // std::cout << "Difference detected! " << img.Bounds << std::endl; |
77 | 166 | // Uncomment the below code to write the image to disk for debugging
|
78 | 167 | /*
|
79 | 168 | auto r = realcounter.fetch_add(1);
|
@@ -135,18 +224,17 @@ void createpartialframegrabber()
|
135 | 224 | framgrabber =
|
136 | 225 | SL::Screen_Capture::CreateCaptureConfiguration([]() {
|
137 | 226 | auto mons = SL::Screen_Capture::GetMonitors();
|
| 227 | + auto newmons = std::vector<SL::Screen_Capture::Monitor>(); |
138 | 228 | std::cout << "Library is requesting the list of monitors to capture!" << std::endl;
|
139 | 229 | for (auto &m : mons) {
|
140 |
| - // capture just a 512x512 square... USERS SHOULD MAKE SURE bounds are |
141 |
| - // valid!!!! |
142 |
| - SL::Screen_Capture::OffsetX(m, SL::Screen_Capture::OffsetX(m) + 512); |
143 |
| - SL::Screen_Capture::OffsetY(m, SL::Screen_Capture::OffsetY(m) + 512); |
144 |
| - SL::Screen_Capture::Height(m, 512); |
145 |
| - SL::Screen_Capture::Width(m, 512); |
146 |
| - |
147 |
| - std::cout << m << std::endl; |
| 230 | + if (SL::Screen_Capture::Height(m) >= 512 * 2 && SL::Screen_Capture::Width(m) >= 512 * 2) { |
| 231 | + SL::Screen_Capture::Height(m, 512); |
| 232 | + SL::Screen_Capture::Width(m, 512); |
| 233 | + std::cout << m << std::endl; |
| 234 | + newmons.push_back(m); |
| 235 | + } |
148 | 236 | }
|
149 |
| - return mons; |
| 237 | + return newmons; |
150 | 238 | })
|
151 | 239 | ->onFrameChanged([&](const SL::Screen_Capture::Image &img, const SL::Screen_Capture::Monitor &monitor) {
|
152 | 240 | // Uncomment the below code to write the image to disk for debugging
|
@@ -279,6 +367,9 @@ int main()
|
279 | 367 | std::cout << "Starting Capture Demo/Test" << std::endl;
|
280 | 368 | std::cout << "Testing captured monitor bounds check" << std::endl;
|
281 | 369 |
|
| 370 | + TestCopyContiguous(); |
| 371 | + TestCopyNonContiguous(); |
| 372 | + |
282 | 373 | auto goodmonitors = SL::Screen_Capture::GetMonitors();
|
283 | 374 | for (auto &m : goodmonitors) {
|
284 | 375 | std::cout << m << std::endl;
|
|
0 commit comments