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