| 1 | #include "test_utility.h" |
| 2 | |
| 3 | #include <cvd/image.h> |
| 4 | #include <cvd/vision.h> |
| 5 | |
| 6 | #include <chrono> |
| 7 | #include <random> |
| 8 | |
| 9 | using CVD::Image; |
| 10 | using CVD::ImageRef; |
| 11 | using CVD::Testing::assert_equal; |
| 12 | using CVD::Testing::assert_image_equal; |
| 13 | using CVD::Testing::init; |
| 14 | |
| 15 | int main() |
| 16 | { |
| 17 | Image<int> a = init({ { 1, 2 }, { 3, 4 } }); |
| 18 | flipVertical(a); |
| 19 | assert_image_equal(init({ { 3, 4 }, { 1, 2 } }), a, "Even sized flipV failed"); |
| 20 | |
| 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | a = init({ { 1, 2 }, { 3, 4 }, { 5, 6 } }); |
| 23 | flipVertical(a); |
| 24 | assert_image_equal(init({ { 5, 6 }, { 3, 4 }, { 1, 2 } }), a, "Odd sized flipV failed"); |
| 25 | |
| 26 | //////////////////////////////////////////////////////////////////////////////// |
| 27 | a = init({ { 1, 2 }, { 3, 4 }, { 5, 6 } }); |
| 28 | Image<int> b(a.size().transpose()); |
| 29 | CVD::Internal::simpleTranspose(a, b); |
| 30 | assert_image_equal(init({ { 1, 3, 5 }, { 2, 4, 6 } }), b, "Simple transpose failed"); |
| 31 | |
| 32 | //////////////////////////////////////////////////////////////////////////////// |
| 33 | b.resize(a.size().transpose()); |
| 34 | CVD::Internal::recursiveTranspose(a, b, 1); |
| 35 | assert_image_equal(init({ { 1, 3, 5 }, { 2, 4, 6 } }), b, "Recursive transpose failed (small)"); |
| 36 | |
| 37 | //////////////////////////////////////////////////////////////////////////////// |
| 38 | const int N = 100; |
| 39 | const int R = 10; |
| 40 | |
| 41 | std::mt19937 eng; |
| 42 | for(int i = 0; i < N; i++) |
| 43 | { |
| 44 | std::uniform_int_distribution<> rng(1, 1024); |
| 45 | std::uniform_int_distribution<> byte(0, 255); |
| 46 | |
| 47 | ImageRef size; |
| 48 | size.x = rng(eng); |
| 49 | size.y = rng(eng); |
| 50 | |
| 51 | double s_simple = 0, s_recursive = 0; |
| 52 | |
| 53 | for(int j = 0; j < R; j++) |
| 54 | { |
| 55 | Image<uint8_t> im1(size), im2; |
| 56 | for(auto& p : im1) |
| 57 | p = static_cast<uint8_t>(byte(eng)); |
| 58 | |
| 59 | auto t1 = std::chrono::steady_clock::now(); |
| 60 | Image<uint8_t> recurs = CVD::transpose(im1); |
| 61 | auto t2 = std::chrono::steady_clock::now(); |
| 62 | |
| 63 | Image<uint8_t> simple(recurs.size()); |
| 64 | CVD::Internal::simpleTranspose(im1, simple); |
| 65 | auto t3 = std::chrono::steady_clock::now(); |
| 66 | |
| 67 | CVD::Testing::assert_image_equal(simple, recurs); |
| 68 | |
| 69 | s_recursive += std::chrono::duration<double>(t2 - t1).count(); |
| 70 | s_simple += std::chrono::duration<double>(t3 - t2).count(); |
| 71 | } |
| 72 | std::cout << size << " " << size.area() << " " << s_simple / R << " " << s_recursive / R << "\n"; |
| 73 | } |
| 74 | } |