aboutsummaryrefslogtreecommitdiff
path: root/samples/ppm_test.cc
diff options
context:
space:
mode:
authorHugues Evrard <hevrard@users.noreply.github.com>2019-01-28 17:06:16 +0000
committerdan sinclair <dj2@everburning.com>2019-01-28 12:06:16 -0500
commit970ba7a99eba1cd8d8a1ea10e4525db8b93fd284 (patch)
treee721902a817418e544d52eeb3cd7385ea214dd31 /samples/ppm_test.cc
parentbca10fbe5f6fe21cae4c8cca0ccf0d07719c8670 (diff)
downloadamber-970ba7a99eba1cd8d8a1ea10e4525db8b93fd284.tar.gz
Add framebuffer dump in PPM format (#227)
This CL plumbs through the ability to dump the framebuffer into a PPM image.
Diffstat (limited to 'samples/ppm_test.cc')
-rw-r--r--samples/ppm_test.cc94
1 files changed, 94 insertions, 0 deletions
diff --git a/samples/ppm_test.cc b/samples/ppm_test.cc
new file mode 100644
index 0000000..9821455
--- /dev/null
+++ b/samples/ppm_test.cc
@@ -0,0 +1,94 @@
+// Copyright 2019 The Amber Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/ppm.h"
+
+#include <algorithm>
+#include <cstring>
+#include <utility>
+#include <vector>
+
+#include "amber/result.h"
+#include "gtest/gtest.h"
+#include "src/make_unique.h"
+
+namespace amber {
+namespace {
+
+const uint8_t kExpectedPPM[] = {
+ 0x50, 0x36, 0x0a, 0x31, 0x32, 0x20, 0x36, 0x0a, 0x32, 0x35, 0x35, 0x0a,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00,
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00,
+ 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff,
+ 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff,
+ 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff,
+ 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff,
+ 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff,
+ 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff};
+
+} // namespace
+
+using PpmTest = testing::Test;
+
+TEST_F(PpmTest, ConvertToPPM) {
+ const uint32_t width = 12;
+ const uint32_t height = 6;
+
+ std::vector<amber::Value> data;
+
+ const uint32_t MaskRed = 0x000000FF;
+ const uint32_t MaskBlue = 0x0000FF00;
+ const uint32_t MaskAplha = 0xFF000000;
+
+ for (uint32_t y = 0; y < height; ++y) {
+ for (uint32_t x = 0; x < width; ++x) {
+ uint32_t pixel = MaskAplha;
+ if (x < width / 2) {
+ pixel |= MaskRed;
+ } else {
+ pixel |= MaskBlue;
+ }
+ if (y > height / 2) {
+ // invert colors
+ pixel = ~pixel;
+ // reset alpha to 1
+ pixel |= MaskAplha;
+ }
+ v.SetIntValue(static_cast<uint64_t>(pixel));
+ data.push_back(v);
+ }
+ }
+
+ amber::Result r;
+ std::string image;
+ std::tie(r, image) = ConvertToPPM(width, height, &data);
+
+ EXPECT_TRUE(r.IsSuccess());
+
+ // Compare
+ EXPECT_EQ(image.size(), sizeof(kExpectedPPM));
+ EXPECT_EQ(std::memcmp(image.c_str(), kExpectedPPM, sizeof(kExpectedPPM)), 0);
+}
+
+} // namespace amber