diff options
author | dan sinclair <dj2@everburning.com> | 2019-03-18 07:35:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-18 07:35:29 -0700 |
commit | 60f04069c0967703e1260db2bf6133a44002f92a (patch) | |
tree | 5e10f4512f2928aa034843cd27b8f1163544e6ae /samples | |
parent | 6064a402932975f3d359b9798bc156886f6b18e8 (diff) | |
download | amber-60f04069c0967703e1260db2bf6133a44002f92a.tar.gz |
Integrate PPM unit tests. (#379)
This Cl integrates the PPM test into the test suite. Part of this work
involved updating the conversion code to return a vector<uint8_t>
instead of a std::string.
Diffstat (limited to 'samples')
-rw-r--r-- | samples/amber.cc | 13 | ||||
-rw-r--r-- | samples/png.cc | 20 | ||||
-rw-r--r-- | samples/png.h | 8 | ||||
-rw-r--r-- | samples/ppm.cc | 36 | ||||
-rw-r--r-- | samples/ppm.h | 10 | ||||
-rw-r--r-- | samples/ppm_test.cc | 43 |
6 files changed, 63 insertions, 67 deletions
diff --git a/samples/amber.cc b/samples/amber.cc index 19c323f..3c71de0 100644 --- a/samples/amber.cc +++ b/samples/amber.cc @@ -394,7 +394,7 @@ int main(int argc, const char** argv) { } if (!options.image_filename.empty()) { - std::string image; + std::vector<uint8_t> out_buf; auto pos = options.image_filename.find_last_of('.'); bool usePNG = pos != std::string::npos && @@ -402,11 +402,12 @@ int main(int argc, const char** argv) { for (amber::BufferInfo buffer_info : amber_options.extractions) { if (buffer_info.buffer_name == "framebuffer") { if (usePNG) { - std::tie(result, image) = png::ConvertToPNG( - buffer_info.width, buffer_info.height, buffer_info.values); + result = png::ConvertToPNG(buffer_info.width, buffer_info.height, + buffer_info.values, &out_buf); } else { - std::tie(result, image) = ppm::ConvertToPPM( - buffer_info.width, buffer_info.height, buffer_info.values); + ppm::ConvertToPPM(buffer_info.width, buffer_info.height, + buffer_info.values, &out_buf); + result = {}; } break; } @@ -420,7 +421,7 @@ int main(int argc, const char** argv) { std::cerr << options.image_filename << std::endl; continue; } - image_file << image; + image_file << std::string(out_buf.begin(), out_buf.end()); image_file.close(); } else { std::cerr << result.Error() << std::endl; diff --git a/samples/png.cc b/samples/png.cc index 1446534..bddb3a7 100644 --- a/samples/png.cc +++ b/samples/png.cc @@ -46,13 +46,13 @@ unsigned char byte3(uint32_t word) { } // namespace -std::pair<amber::Result, std::string> ConvertToPNG( - uint32_t width, - uint32_t height, - const std::vector<amber::Value>& values) { +amber::Result ConvertToPNG(uint32_t width, + uint32_t height, + const std::vector<amber::Value>& values, + std::vector<uint8_t>* buffer) { assert(values.size() == width * height); - std::vector<unsigned char> data; + std::vector<uint8_t> data; // Prepare data as lodepng expects it for (amber::Value value : values) { @@ -63,7 +63,6 @@ std::pair<amber::Result, std::string> ConvertToPNG( data.push_back(byte3(pixel)); // A } - std::vector<unsigned char> png; lodepng::State state; // Force RGBA color type, otherwise many PNG decoders will ignore the alpha @@ -74,13 +73,10 @@ std::pair<amber::Result, std::string> ConvertToPNG( state.info_png.color.colortype = LodePNGColorType::LCT_RGBA; state.info_png.color.bitdepth = 8; - unsigned error = lodepng::encode(png, data, width, height, state); - if (error) { - return std::make_pair(amber::Result("lodepng::encode() returned non-zero"), - nullptr); - } + if (lodepng::encode(*buffer, data, width, height, state) != 0) + return amber::Result("lodepng::encode() returned non-zero"); - return std::make_pair(amber::Result(), std::string(png.begin(), png.end())); + return {}; } } // namespace png diff --git a/samples/png.h b/samples/png.h index 2008e9e..74802e7 100644 --- a/samples/png.h +++ b/samples/png.h @@ -26,10 +26,10 @@ namespace png { /// Converts the image of dimensions |width| and |height| and with pixels stored /// in row-major order in |values| with format B8G8R8A8 into PNG format, /// returning the PNG binary as a string. -std::pair<amber::Result, std::string> ConvertToPNG( - uint32_t width, - uint32_t height, - const std::vector<amber::Value>& values); +amber::Result ConvertToPNG(uint32_t width, + uint32_t height, + const std::vector<amber::Value>& values, + std::vector<uint8_t>* buffer); } // namespace png diff --git a/samples/ppm.cc b/samples/ppm.cc index cf7ec47..db53835 100644 --- a/samples/ppm.cc +++ b/samples/ppm.cc @@ -23,43 +23,45 @@ namespace ppm { namespace { -char byte0(uint32_t word) { - return static_cast<char>(word); +const uint32_t kMaximumColorValue = 255; + +uint8_t byte0(uint32_t word) { + return static_cast<uint8_t>(word & 0xff); } -char byte1(uint32_t word) { - return static_cast<char>(word >> 8); +uint8_t byte1(uint32_t word) { + return static_cast<uint8_t>((word >> 8) & 0xff); } -char byte2(uint32_t word) { - return static_cast<char>(word >> 16); +uint8_t byte2(uint32_t word) { + return static_cast<uint8_t>((word >> 16) & 0xff); } } // namespace -std::pair<amber::Result, std::string> ConvertToPPM( - uint32_t width, - uint32_t height, - const std::vector<amber::Value>& values) { +void ConvertToPPM(uint32_t width, + uint32_t height, + const std::vector<amber::Value>& values, + std::vector<uint8_t>* buffer) { assert(values.size() == width * height); // Write PPM header - const uint32_t maximum_color_value = 255; std::string image = "P6\n"; image += std::to_string(width) + " " + std::to_string(height) + "\n"; - image += std::to_string(maximum_color_value) + "\n"; + image += std::to_string(kMaximumColorValue) + "\n"; + + for (char ch : image) + buffer->push_back(static_cast<uint8_t>(ch)); // Write PPM data for (amber::Value value : values) { const uint32_t pixel = value.AsUint32(); // We assume B8G8R8A8_UNORM here: - image.push_back(byte2(pixel)); // R - image.push_back(byte1(pixel)); // G - image.push_back(byte0(pixel)); // B + buffer->push_back(byte2(pixel)); // R + buffer->push_back(byte1(pixel)); // G + buffer->push_back(byte0(pixel)); // B // PPM does not support alpha channel } - - return std::make_pair(amber::Result(), image); } } // namespace ppm diff --git a/samples/ppm.h b/samples/ppm.h index 9b237f3..b8e680d 100644 --- a/samples/ppm.h +++ b/samples/ppm.h @@ -25,11 +25,11 @@ namespace ppm { /// Converts the image of dimensions |width| and |height| and with pixels stored /// in row-major order in |values| with format B8G8R8A8 into PPM format, -/// returning the PPM binary as a string. -std::pair<amber::Result, std::string> ConvertToPPM( - uint32_t width, - uint32_t height, - const std::vector<amber::Value>& values); +/// returning the PPM binary in |buffer|. +void ConvertToPPM(uint32_t width, + uint32_t height, + const std::vector<amber::Value>& values, + std::vector<uint8_t>* buffer); } // namespace ppm diff --git a/samples/ppm_test.cc b/samples/ppm_test.cc index 9821455..7a695af 100644 --- a/samples/ppm_test.cc +++ b/samples/ppm_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "src/ppm.h" +#include "samples/ppm.h" #include <algorithm> #include <cstring> @@ -28,30 +28,30 @@ 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, 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, - 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, 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, - 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, 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, - 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, 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, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff, - 0x00, 0xff, 0xff, 0x00, 0xff, 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, - 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, 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}; } // namespace -using PpmTest = testing::Test; +using PPMTest = testing::Test; -TEST_F(PpmTest, ConvertToPPM) { +TEST_F(PPMTest, ConvertToPPM) { const uint32_t width = 12; const uint32_t height = 6; @@ -75,20 +75,17 @@ TEST_F(PpmTest, ConvertToPPM) { // reset alpha to 1 pixel |= MaskAplha; } + Value v; 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); + std::vector<uint8_t> out_buf; + ppm::ConvertToPPM(width, height, data, &out_buf); - EXPECT_TRUE(r.IsSuccess()); - - // Compare - EXPECT_EQ(image.size(), sizeof(kExpectedPPM)); - EXPECT_EQ(std::memcmp(image.c_str(), kExpectedPPM, sizeof(kExpectedPPM)), 0); + EXPECT_EQ(out_buf.size(), sizeof(kExpectedPPM)); + EXPECT_EQ(std::memcmp(out_buf.data(), kExpectedPPM, sizeof(kExpectedPPM)), 0); } } // namespace amber |