blob: 5b9e7693eff2edc44a5156d03a5ce047603c2884 [file] [log] [blame]
Ed Tanous93f987d2017-04-17 17:52:36 -07001#include "ast_jpeg_decoder.hpp"
Ed Tanous93f987d2017-04-17 17:52:36 -07002#include "gmock/gmock.h"
Ed Tanous1ff48782017-04-18 12:45:08 -07003#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 Tanous1ff48782017-04-18 12:45:08 -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;
113
114 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 }
123}
Ed Tanous1ff48782017-04-18 12:45:08 -0700124*/
Ed Tanous93f987d2017-04-17 17:52:36 -0700125// Tests the buffers around the screen aren't written to
126TEST(AstJpegDecoder, BufferLimits) {
127 AstVideo::RawVideoBuffer out;
128
129 // This binary blog was created on the aspeed hardware using a black screen
130 FILE *fp = fopen("test_resources/aspeedblackscreen.bin", "rb");
131 EXPECT_NE(fp, nullptr);
132 size_t bufferlen = fread(out.buffer.data(), sizeof(char),
133 out.buffer.size() * sizeof(long), fp);
134 fclose(fp);
135
136 ASSERT_GT(bufferlen, 0);
137
138 out.y_selector = 0;
139 out.uv_selector = 0;
140 out.mode = AstVideo::YuvMode::YUV444;
141 out.width = 800;
142 out.height = 600;
143
144 AstVideo::AstJpegDecoder d;
145 d.decode(out.buffer, out.width, out.height, out.mode, out.y_selector,
146 out.uv_selector);
147 // Reserved pixel should be default value
148 for (auto &pixel : d.OutBuffer) {
149 EXPECT_EQ(pixel.Reserved, 0xAA);
150 }
151 // All pixels beyond the buffer should be zero
152 for (int i = out.width * out.height; i < d.OutBuffer.size(); i++) {
153 EXPECT_EQ(d.OutBuffer[i].R, 0x00) << "index:" << i;
154 EXPECT_EQ(d.OutBuffer[i].B, 0x00) << "index:" << i;
155 EXPECT_EQ(d.OutBuffer[i].G, 0x00) << "index:" << i;
156 }
157}