1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "samples/ppm.h"
16
17 #include <algorithm>
18 #include <cstring>
19 #include <utility>
20 #include <vector>
21
22 #include "amber/result.h"
23 #include "gtest/gtest.h"
24 #include "src/make_unique.h"
25
26 namespace amber {
27 namespace {
28
29 const uint8_t kExpectedPPM[] = {
30 0x50, 0x36, 0x0a, 0x31, 0x32, 0x20, 0x36, 0x0a, 0x32, 0x35, 0x35, 0x0a,
31 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
32 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
33 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
34 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
35 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
36 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
37 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
38 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
39 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
40 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
41 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
42 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
43 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00,
44 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff,
45 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff,
46 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00,
47 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff,
48 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff};
49
50 } // namespace
51
52 using PPMTest = testing::Test;
53
TEST_F(PPMTest,ConvertToPPM)54 TEST_F(PPMTest, ConvertToPPM) {
55 const uint32_t width = 12;
56 const uint32_t height = 6;
57
58 std::vector<amber::Value> data;
59
60 const uint32_t MaskRed = 0x000000FF;
61 const uint32_t MaskBlue = 0x0000FF00;
62 const uint32_t MaskAplha = 0xFF000000;
63
64 for (uint32_t y = 0; y < height; ++y) {
65 for (uint32_t x = 0; x < width; ++x) {
66 uint32_t pixel = MaskAplha;
67 if (x < width / 2) {
68 pixel |= MaskRed;
69 } else {
70 pixel |= MaskBlue;
71 }
72 if (y > height / 2) {
73 // invert colors
74 pixel = ~pixel;
75 // reset alpha to 1
76 pixel |= MaskAplha;
77 }
78 Value v;
79 v.SetIntValue(static_cast<uint64_t>(pixel));
80 data.push_back(v);
81 }
82 }
83
84 std::vector<uint8_t> out_buf;
85 ppm::ConvertToPPM(width, height, data, &out_buf);
86
87 EXPECT_EQ(out_buf.size(), sizeof(kExpectedPPM));
88 EXPECT_EQ(std::memcmp(out_buf.data(), kExpectedPPM, sizeof(kExpectedPPM)), 0);
89 }
90
91 } // namespace amber
92