Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 1 | #include "ast_jpeg_decoder.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 3 | #include <gmock/gmock.h> |
| 4 | #include <gtest/gtest.h> |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 5 | |
| 6 | #ifdef BUILD_CIMG |
| 7 | #define cimg_display 0 |
| 8 | #include <CImg.h> |
| 9 | #endif |
| 10 | |
| 11 | using namespace testing; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 12 | MATCHER_P2(IsBetween, a, b, |
| 13 | std::string(negation ? "isn't" : "is") + " between " + |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | PrintToString(a) + " and " + PrintToString(b)) |
| 15 | { |
| 16 | return a <= arg && arg <= b; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 17 | }; |
| 18 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 19 | TEST(AstJpegDecoder, AllBlue) |
| 20 | { |
| 21 | ast_video::RawVideoBuffer out; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | // This binary blog was created on the aspeed hardware using a blue screen |
| 24 | // consisting of the color 0x8EFFFA in a web browser window |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 25 | FILE* fp = fopen("test_resources/aspeedbluescreen.bin", "rb"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | EXPECT_NE(fp, nullptr); |
| 27 | size_t bufferlen = |
| 28 | fread(out.buffer.data(), sizeof(decltype(out.buffer)::value_type), |
| 29 | out.buffer.size(), fp); |
| 30 | fclose(fp); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 31 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | ASSERT_GT(bufferlen, 0); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | out.ySelector = 0; |
| 35 | out.uvSelector = 0; |
| 36 | out.mode = ast_video::YuvMode::YUV444; |
| 37 | out.width = 800; |
| 38 | out.height = 600; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | ast_video::AstJpegDecoder d; |
| 41 | d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector, |
| 42 | out.uvSelector); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 44 | int tolerance = 16; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 45 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | // All pixels should be blue (0x8EFFFA) to within a tolerance (due to jpeg |
| 47 | // compression artifacts and quanitization) |
| 48 | for (int i = 0; i < out.width * out.height; i++) |
| 49 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 50 | ast_video::RGB& pixel = d.outBuffer[i]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | EXPECT_GT(pixel.r, 0x8E - tolerance); |
| 52 | EXPECT_LT(pixel.r, 0x8E + tolerance); |
| 53 | EXPECT_GT(pixel.g, 0xFF - tolerance); |
| 54 | EXPECT_LT(pixel.g, 0xFF + tolerance); |
| 55 | EXPECT_GT(pixel.b, 0xF1 - tolerance); |
| 56 | EXPECT_LT(pixel.b, 0xF1 + tolerance); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 57 | } |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 58 | } |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 59 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 60 | TEST(AstJpegDecoder, AllBlack) |
| 61 | { |
| 62 | ast_video::RawVideoBuffer out; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 63 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | // This binary blog was created on the aspeed hardware using a black screen |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 65 | FILE* fp = fopen("test_resources/aspeedblackscreen.bin", "rb"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | EXPECT_NE(fp, nullptr); |
| 67 | size_t bufferlen = fread(out.buffer.data(), sizeof(char), |
| 68 | out.buffer.size() * sizeof(long), fp); |
| 69 | fclose(fp); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 70 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | ASSERT_GT(bufferlen, 0); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 72 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | out.ySelector = 0; |
| 74 | out.uvSelector = 0; |
| 75 | out.mode = ast_video::YuvMode::YUV444; |
| 76 | out.width = 800; |
| 77 | out.height = 600; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 78 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | ast_video::AstJpegDecoder d; |
| 80 | d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector, |
| 81 | out.uvSelector); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 82 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | // All pixels should be blue (0x8EFFFA) to within a tolerance (due to jpeg |
| 84 | // compression artifacts and quanitization) |
| 85 | for (int x = 0; x < out.width; x++) |
| 86 | { |
| 87 | for (int y = 0; y < out.height; y++) |
| 88 | { |
| 89 | ast_video::RGB pixel = d.outBuffer[x + (y * out.width)]; |
| 90 | ASSERT_EQ(pixel.r, 0x00) << "X:" << x << " Y: " << y; |
| 91 | ASSERT_EQ(pixel.g, 0x00) << "X:" << x << " Y: " << y; |
| 92 | ASSERT_EQ(pixel.b, 0x00) << "X:" << x << " Y: " << y; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | TEST(AstJpegDecoder, TestColors) |
| 98 | { |
| 99 | ast_video::RawVideoBuffer out; |
| 100 | |
| 101 | // This binary blog was created on the aspeed hardware using a blue screen |
| 102 | // consisting of the color 0x8EFFFA in a web browser window |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 103 | FILE* fp = fopen("test_resources/ubuntu_444_800x600_0chrom_0lum.bin", "rb"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | EXPECT_NE(fp, nullptr); |
| 105 | size_t bufferlen = fread(out.buffer.data(), sizeof(char), |
| 106 | out.buffer.size() * sizeof(long), fp); |
| 107 | fclose(fp); |
| 108 | |
| 109 | ASSERT_GT(bufferlen, 0); |
| 110 | |
| 111 | out.ySelector = 0; |
| 112 | out.uvSelector = 0; |
| 113 | out.mode = ast_video::YuvMode::YUV444; |
| 114 | out.width = 800; |
| 115 | out.height = 600; |
| 116 | |
| 117 | ast_video::AstJpegDecoder d; |
| 118 | d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector, |
| 119 | out.uvSelector); |
| 120 | |
| 121 | int tolerance = 16; |
| 122 | /* |
| 123 | for (int i = 0; i < out.width * out.height; i++) { |
| 124 | ast_video::RGB &pixel = d.outBuffer[i]; |
| 125 | EXPECT_GT(pixel.r, 0x8E - tolerance); |
| 126 | EXPECT_LT(pixel.r, 0x8E + tolerance); |
| 127 | EXPECT_GT(pixel.g, 0xFF - tolerance); |
| 128 | EXPECT_LT(pixel.g, 0xFF + tolerance); |
| 129 | EXPECT_GT(pixel.b, 0xF1 - tolerance); |
| 130 | EXPECT_LT(pixel.b, 0xF1 + tolerance); |
| 131 | } |
| 132 | */ |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 133 | } |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 134 | |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 135 | // Tests the buffers around the screen aren't written to |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | TEST(AstJpegDecoder, BufferLimits) |
| 137 | { |
| 138 | ast_video::RawVideoBuffer out; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 139 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 140 | // This binary blog was created on the aspeed hardware using a black screen |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 141 | FILE* fp = fopen("test_resources/aspeedblackscreen.bin", "rb"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 142 | EXPECT_NE(fp, nullptr); |
| 143 | size_t bufferlen = fread(out.buffer.data(), sizeof(char), |
| 144 | out.buffer.size() * sizeof(long), fp); |
| 145 | fclose(fp); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 146 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 147 | ASSERT_GT(bufferlen, 0); |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 148 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | out.ySelector = 0; |
| 150 | out.uvSelector = 0; |
| 151 | out.mode = ast_video::YuvMode::YUV444; |
| 152 | out.width = 800; |
| 153 | out.height = 600; |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 154 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 155 | ast_video::AstJpegDecoder d; |
| 156 | d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector, |
| 157 | out.uvSelector); |
| 158 | // reserved pixel should be default value |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 159 | for (auto& pixel : d.outBuffer) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 160 | { |
| 161 | EXPECT_EQ(pixel.reserved, 0xAA); |
| 162 | } |
| 163 | // All pixels beyond the buffer should be zero |
| 164 | for (int i = out.width * out.height; i < d.outBuffer.size(); i++) |
| 165 | { |
| 166 | EXPECT_EQ(d.outBuffer[i].r, 0x00) << "index:" << i; |
| 167 | EXPECT_EQ(d.outBuffer[i].b, 0x00) << "index:" << i; |
| 168 | EXPECT_EQ(d.outBuffer[i].g, 0x00) << "index:" << i; |
| 169 | } |
Ed Tanous | 93f987d | 2017-04-17 17:52:36 -0700 | [diff] [blame] | 170 | } |