blob: 277ba2cc8b36f08652815c0722190e7ee9d409ab [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) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070018 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -070019
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
Ed Tanous55c7b7a2018-05-22 15:27:24 -070031 out.ySelector = 0;
32 out.uvSelector = 0;
33 out.mode = ast_video::YuvMode::YUV444;
Ed Tanous93f987d2017-04-17 17:52:36 -070034 out.width = 800;
35 out.height = 600;
36
Ed Tanous55c7b7a2018-05-22 15:27:24 -070037 ast_video::AstJpegDecoder d;
38 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
39 out.uvSelector);
Ed Tanous93f987d2017-04-17 17:52:36 -070040
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++) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070046 ast_video::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);
Ed Tanous93f987d2017-04-17 17:52:36 -070053 }
54}
55
56TEST(AstJpegDecoder, AllBlack) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070057 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -070058
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
Ed Tanous55c7b7a2018-05-22 15:27:24 -070068 out.ySelector = 0;
69 out.uvSelector = 0;
70 out.mode = ast_video::YuvMode::YUV444;
Ed Tanous93f987d2017-04-17 17:52:36 -070071 out.width = 800;
72 out.height = 600;
73
Ed Tanous55c7b7a2018-05-22 15:27:24 -070074 ast_video::AstJpegDecoder d;
75 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
76 out.uvSelector);
Ed Tanous93f987d2017-04-17 17:52:36 -070077
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++) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070082 ast_video::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;
Ed Tanous93f987d2017-04-17 17:52:36 -070086 }
87 }
88}
Ed Tanous9140a672017-04-24 17:01:32 -070089
Ed Tanous93f987d2017-04-17 17:52:36 -070090TEST(AstJpegDecoder, TestColors) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070091 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -070092
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
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700103 out.ySelector = 0;
104 out.uvSelector = 0;
105 out.mode = ast_video::YuvMode::YUV444;
Ed Tanous93f987d2017-04-17 17:52:36 -0700106 out.width = 800;
107 out.height = 600;
108
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700109 ast_video::AstJpegDecoder d;
110 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
111 out.uvSelector);
Ed Tanous93f987d2017-04-17 17:52:36 -0700112
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++) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700116 ast_video::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);
Ed Tanous93f987d2017-04-17 17:52:36 -0700123 }
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) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700129 ast_video::RawVideoBuffer out;
Ed Tanous93f987d2017-04-17 17:52:36 -0700130
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
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700140 out.ySelector = 0;
141 out.uvSelector = 0;
142 out.mode = ast_video::YuvMode::YUV444;
Ed Tanous93f987d2017-04-17 17:52:36 -0700143 out.width = 800;
144 out.height = 600;
145
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700146 ast_video::AstJpegDecoder d;
147 d.decode(out.buffer, out.width, out.height, out.mode, out.ySelector,
148 out.uvSelector);
149 // reserved pixel should be default value
150 for (auto &pixel : d.outBuffer) {
151 EXPECT_EQ(pixel.reserved, 0xAA);
Ed Tanous93f987d2017-04-17 17:52:36 -0700152 }
153 // All pixels beyond the buffer should be zero
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700154 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;
Ed Tanous93f987d2017-04-17 17:52:36 -0700158 }
159}