blob: b8faa29330c679ed0e29bac3b98d4fca8f14c7f1 [file] [log] [blame]
Ed Tanous93f987d2017-04-17 17:52:36 -07001#include "ast_jpeg_decoder.hpp"
Ed Tanous9140a672017-04-24 17:01:32 -07002#include <gmock/gmock.h>
3#include <gtest/gtest.h>
Ed Tanous93f987d2017-04-17 17:52:36 -07004
5#ifdef BUILD_CIMG
6#define cimg_display 0
7#include <CImg.h>
8#endif
9
10using namespace testing;
Ed Tanous911ac312017-08-15 09:37:42 -070011MATCHER_P2(IsBetween, a, b,
12 std::string(negation ? "isn't" : "is") + " between " +
13 PrintToString(a) + " and " + PrintToString(b)) {
Ed Tanous93f987d2017-04-17 17:52:36 -070014 return a <= arg && arg <= b;
15};
16
17TEST(AstJpegDecoder, AllBlue) {
18 AstVideo::RawVideoBuffer out;
19
20 // This binary blog was created on the aspeed hardware using a blue screen
21 // consisting of the color 0x8EFFFA in a web browser window
22 FILE *fp = fopen("test_resources/aspeedbluescreen.bin", "rb");
23 EXPECT_NE(fp, nullptr);
Ed Tanous911ac312017-08-15 09:37:42 -070024 size_t bufferlen =
25 fread(out.buffer.data(), sizeof(decltype(out.buffer)::value_type),
26 out.buffer.size(), fp);
Ed Tanous93f987d2017-04-17 17:52:36 -070027 fclose(fp);
28
29 ASSERT_GT(bufferlen, 0);
30
31 out.y_selector = 0;
32 out.uv_selector = 0;
33 out.mode = AstVideo::YuvMode::YUV444;
34 out.width = 800;
35 out.height = 600;
36
37 AstVideo::AstJpegDecoder d;
38 d.decode(out.buffer, out.width, out.height, out.mode, out.y_selector,
39 out.uv_selector);
40
41 int tolerance = 16;
42
43 // All pixels should be blue (0x8EFFFA) to within a tolerance (due to jpeg
44 // compression artifacts and quanitization)
45 for (int i = 0; i < out.width * out.height; i++) {
46 AstVideo::RGB &pixel = d.OutBuffer[i];
47 EXPECT_GT(pixel.R, 0x8E - tolerance);
48 EXPECT_LT(pixel.R, 0x8E + tolerance);
49 EXPECT_GT(pixel.G, 0xFF - tolerance);
50 EXPECT_LT(pixel.G, 0xFF + tolerance);
51 EXPECT_GT(pixel.B, 0xF1 - tolerance);
52 EXPECT_LT(pixel.B, 0xF1 + tolerance);
53 }
54}
55
56TEST(AstJpegDecoder, AllBlack) {
57 AstVideo::RawVideoBuffer out;
58
59 // This binary blog was created on the aspeed hardware using a black screen
60 FILE *fp = fopen("test_resources/aspeedblackscreen.bin", "rb");
61 EXPECT_NE(fp, nullptr);
62 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
63 out.buffer.size() * sizeof(long), fp);
64 fclose(fp);
65
66 ASSERT_GT(bufferlen, 0);
67
68 out.y_selector = 0;
69 out.uv_selector = 0;
70 out.mode = AstVideo::YuvMode::YUV444;
71 out.width = 800;
72 out.height = 600;
73
74 AstVideo::AstJpegDecoder d;
75 d.decode(out.buffer, out.width, out.height, out.mode, out.y_selector,
76 out.uv_selector);
77
78 // All pixels should be blue (0x8EFFFA) to within a tolerance (due to jpeg
79 // compression artifacts and quanitization)
80 for (int x = 0; x < out.width; x++) {
81 for (int y = 0; y < out.height; y++) {
82 AstVideo::RGB pixel = d.OutBuffer[x + (y * out.width)];
83 ASSERT_EQ(pixel.R, 0x00) << "X:" << x << " Y: " << y;
84 ASSERT_EQ(pixel.G, 0x00) << "X:" << x << " Y: " << y;
85 ASSERT_EQ(pixel.B, 0x00) << "X:" << x << " Y: " << y;
86 }
87 }
88}
Ed Tanous9140a672017-04-24 17:01:32 -070089
Ed Tanous93f987d2017-04-17 17:52:36 -070090TEST(AstJpegDecoder, TestColors) {
91 AstVideo::RawVideoBuffer out;
92
93 // This binary blog was created on the aspeed hardware using a blue screen
94 // consisting of the color 0x8EFFFA in a web browser window
95 FILE *fp = fopen("test_resources/ubuntu_444_800x600_0chrom_0lum.bin", "rb");
96 EXPECT_NE(fp, nullptr);
97 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
98 out.buffer.size() * sizeof(long), fp);
99 fclose(fp);
100
101 ASSERT_GT(bufferlen, 0);
102
103 out.y_selector = 0;
104 out.uv_selector = 0;
105 out.mode = AstVideo::YuvMode::YUV444;
106 out.width = 800;
107 out.height = 600;
108
109 AstVideo::AstJpegDecoder d;
110 d.decode(out.buffer, out.width, out.height, out.mode, out.y_selector,
111 out.uv_selector);
112
113 int tolerance = 16;
Ed Tanous9140a672017-04-24 17:01:32 -0700114 /*
Ed Tanous93f987d2017-04-17 17:52:36 -0700115 for (int i = 0; i < out.width * out.height; i++) {
116 AstVideo::RGB &pixel = d.OutBuffer[i];
117 EXPECT_GT(pixel.R, 0x8E - tolerance);
118 EXPECT_LT(pixel.R, 0x8E + tolerance);
119 EXPECT_GT(pixel.G, 0xFF - tolerance);
120 EXPECT_LT(pixel.G, 0xFF + tolerance);
121 EXPECT_GT(pixel.B, 0xF1 - tolerance);
122 EXPECT_LT(pixel.B, 0xF1 + tolerance);
123 }
Ed Tanous9140a672017-04-24 17:01:32 -0700124 */
Ed Tanous93f987d2017-04-17 17:52:36 -0700125}
Ed Tanous9140a672017-04-24 17:01:32 -0700126
Ed Tanous93f987d2017-04-17 17:52:36 -0700127// Tests the buffers around the screen aren't written to
128TEST(AstJpegDecoder, BufferLimits) {
129 AstVideo::RawVideoBuffer out;
130
131 // This binary blog was created on the aspeed hardware using a black screen
132 FILE *fp = fopen("test_resources/aspeedblackscreen.bin", "rb");
133 EXPECT_NE(fp, nullptr);
134 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
135 out.buffer.size() * sizeof(long), fp);
136 fclose(fp);
137
138 ASSERT_GT(bufferlen, 0);
139
140 out.y_selector = 0;
141 out.uv_selector = 0;
142 out.mode = AstVideo::YuvMode::YUV444;
143 out.width = 800;
144 out.height = 600;
145
146 AstVideo::AstJpegDecoder d;
147 d.decode(out.buffer, out.width, out.height, out.mode, out.y_selector,
148 out.uv_selector);
149 // Reserved pixel should be default value
150 for (auto &pixel : d.OutBuffer) {
151 EXPECT_EQ(pixel.Reserved, 0xAA);
152 }
153 // All pixels beyond the buffer should be zero
154 for (int i = out.width * out.height; i < d.OutBuffer.size(); i++) {
155 EXPECT_EQ(d.OutBuffer[i].R, 0x00) << "index:" << i;
156 EXPECT_EQ(d.OutBuffer[i].B, 0x00) << "index:" << i;
157 EXPECT_EQ(d.OutBuffer[i].G, 0x00) << "index:" << i;
158 }
159}