From 0bc5c28cb5c13ec6a2991b43bb8b99fc15d13660 Mon Sep 17 00:00:00 2001 From: Andrei Medar Date: Mon, 14 Sep 2020 15:23:29 +0000 Subject: [PATCH] Applied requested changes --- .../sapi_lodepng/examples/helpers.cc | 13 ++++++---- .../sapi_lodepng/examples/helpers.h | 6 ++--- .../sapi_lodepng/examples/main_sandboxed.cc | 20 ++++++++-------- .../sapi_lodepng/examples/main_unit_test.cc | 24 +++++++++---------- .../sapi_lodepng/examples/main_unsandboxed.cc | 20 ++++++++-------- 5 files changed, 43 insertions(+), 40 deletions(-) diff --git a/oss-internship-2020/sapi_lodepng/examples/helpers.cc b/oss-internship-2020/sapi_lodepng/examples/helpers.cc index a2e35d1..6811427 100644 --- a/oss-internship-2020/sapi_lodepng/examples/helpers.cc +++ b/oss-internship-2020/sapi_lodepng/examples/helpers.cc @@ -15,15 +15,18 @@ #include "helpers.h" std::vector GenerateValues() { - std::vector image(kImgLen); + std::vector image; + image.reserve(kImgLen); + for (int y = 0; y < kHeight; ++y) { for (int x = 0; x < kWidth; ++x) { - image[4 * kWidth * y + 4 * x + 0] = 255 * !(x & y); - image[4 * kWidth * y + 4 * x + 1] = x ^ y; - image[4 * kWidth * y + 4 * x + 2] = x | y; - image[4 * kWidth * y + 4 * x + 3] = 255; + image.push_back(255 * !(x & y)); + image.push_back(x ^ y); + image.push_back(x | y); + image.push_back(255); } } + return image; } diff --git a/oss-internship-2020/sapi_lodepng/examples/helpers.h b/oss-internship-2020/sapi_lodepng/examples/helpers.h index 0459831..7f52802 100644 --- a/oss-internship-2020/sapi_lodepng/examples/helpers.h +++ b/oss-internship-2020/sapi_lodepng/examples/helpers.h @@ -22,9 +22,9 @@ #include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/temp_file.h" -constexpr uint32_t kWidth = 512; -constexpr uint32_t kHeight = 512; -constexpr uint32_t kImgLen = kWidth * kHeight * 4; +constexpr size_t kWidth = 512; +constexpr size_t kHeight = 512; +constexpr size_t kImgLen = kWidth * kHeight * 4; // Returns a vector that contains values used for testing. // This part of code is taken from diff --git a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc index 36a19ba..d7b9761 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_sandboxed.cc @@ -21,7 +21,7 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { // Generate the values. - std::vector image(GenerateValues()); + std::vector image = GenerateValues(); // Encode the image. sapi::v::Array sapi_image(kImgLen); @@ -38,18 +38,18 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { // After the image has been encoded, decode it to check that the // pixel values are the same. - sapi::v::UInt sapi_width2, sapi_height2; + sapi::v::UInt sapi_width, sapi_height; sapi::v::IntBase sapi_image_ptr(0); result = api.lodepng_decode32_file( - sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), + sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(), sapi_filename.PtrBefore()); CHECK(result.ok()) << "decode32_file call failes"; CHECK(!result.value()) << "Unexpected result from decode32_file call"; - CHECK(sapi_width2.GetValue() == kWidth) << "Widths differ"; - CHECK(sapi_height2.GetValue() == kHeight) << "Heights differ"; + CHECK(sapi_width.GetValue() == kWidth) << "Widths differ"; + CHECK(sapi_height.GetValue() == kHeight) << "Heights differ"; // The pixels have been allocated inside the sandboxed process // memory, so we need to transfer them to this process. @@ -77,7 +77,7 @@ void EncodeDecodeOneStep(SapiLodepngSandbox &sandbox, LodepngApi &api) { void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { // Generate the values. - std::vector image(GenerateValues()); + std::vector image = GenerateValues(); // Encode the image into memory first. sapi::v::Array sapi_image(kImgLen); @@ -115,7 +115,7 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { CHECK(!result.value()) << "Unexpected result from save_file call"; // Now, decode the image using the 2 steps in order to compare the values. - sapi::v::UInt sapi_width2, sapi_height2; + sapi::v::UInt sapi_width, sapi_height; sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2; @@ -141,14 +141,14 @@ void EncodeDecodeTwoSteps(SapiLodepngSandbox &sandbox, LodepngApi &api) { // directly. sapi::v::IntBase sapi_png_ptr3(0); result = api.lodepng_decode32( - sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), + sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(), sapi_height.PtrBoth(), sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue()); CHECK(result.ok()) << "decode32 call failed"; CHECK(!result.value()) << "Unexpected result from decode32 call"; - CHECK(sapi_width2.GetValue() == kWidth) << "Widths differ"; - CHECK(sapi_height2.GetValue() == kHeight) << "Heights differ"; + CHECK(sapi_width.GetValue() == kWidth) << "Widths differ"; + CHECK(sapi_height.GetValue() == kHeight) << "Heights differ"; // Transfer the pixels so they can be used here. sapi::v::Array sapi_pixels(kImgLen); diff --git a/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc b/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc index 6c4d6d6..8ad3ae6 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unit_test.cc @@ -65,7 +65,7 @@ TEST(LodePngTest, EncodeDecodeOneStep) { ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox initialization"; LodepngApi api(&sandbox); - std::vector image(GenerateValues()); + std::vector image = GenerateValues(); sapi::v::Array sapi_image(kImgLen); EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()), @@ -81,18 +81,18 @@ TEST(LodePngTest, EncodeDecodeOneStep) { ASSERT_THAT(result, Eq(0)) << "Unexpected result from encode32_file call"; - sapi::v::UInt sapi_width2, sapi_height2; + sapi::v::UInt sapi_width, sapi_height; sapi::v::IntBase sapi_image_ptr(0); SAPI_ASSERT_OK_AND_ASSIGN( result, api.lodepng_decode32_file( - sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), - sapi_height2.PtrBoth(), sapi_filename.PtrBefore())); + sapi_image_ptr.PtrBoth(), sapi_width.PtrBoth(), + sapi_height.PtrBoth(), sapi_filename.PtrBefore())); ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32_file call"; - EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ"; - EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ"; + EXPECT_THAT(sapi_width.GetValue(), Eq(kWidth)) << "Widths differ"; + EXPECT_THAT(sapi_height.GetValue(), Eq(kHeight)) << "Heights differ"; sapi::v::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_image_ptr.GetValue()); @@ -126,7 +126,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { ASSERT_THAT(sandbox.Init(), IsOk()) << "Error during sandbox init"; LodepngApi api(&sandbox); - std::vector image(GenerateValues()); + std::vector image = GenerateValues(); sapi::v::Array sapi_image(kImgLen); EXPECT_THAT(std::copy(image.begin(), image.end(), sapi_image.GetData()), @@ -158,7 +158,7 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { ASSERT_THAT(result, Eq(0)) << "Unexpected result from save_file call"; - sapi::v::UInt sapi_width2, sapi_height2; + sapi::v::UInt sapi_width, sapi_height; sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2; @@ -181,14 +181,14 @@ TEST(LodePngTest, EncodeDecodeTwoSteps) { sapi::v::IntBase sapi_png_ptr3(0); SAPI_ASSERT_OK_AND_ASSIGN( result, - api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), - sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(), + api.lodepng_decode32(sapi_png_ptr3.PtrBoth(), sapi_width.PtrBoth(), + sapi_height.PtrBoth(), sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue())); ASSERT_THAT(result, Eq(0)) << "Unexpected result from decode32 call"; - EXPECT_THAT(sapi_width2.GetValue(), Eq(kWidth)) << "Widths differ"; - EXPECT_THAT(sapi_height2.GetValue(), Eq(kHeight)) << "Heights differ"; + EXPECT_THAT(sapi_width.GetValue(), Eq(kWidth)) << "Widths differ"; + EXPECT_THAT(sapi_height.GetValue(), Eq(kHeight)) << "Heights differ"; sapi::v::Array sapi_pixels(kImgLen); sapi_pixels.SetRemote(sapi_png_ptr3.GetValue()); diff --git a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc index 8481324..2feb306 100644 --- a/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc @@ -22,7 +22,7 @@ void EncodeDecodeOneStep(const std::string &images_path) { // Generate the values. - std::vector image(GenerateValues()); + std::vector image = GenerateValues(); // Encode the image. const std::string filename = images_path + "/out_generated1.png"; @@ -33,15 +33,15 @@ void EncodeDecodeOneStep(const std::string &images_path) { // After the image has been encoded, decode it to check that the // pixel values are the same. - unsigned int width2, height2; + unsigned int width, height; uint8_t *image2 = 0; - result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str()); + result = lodepng_decode32_file(&image2, &width, &height, filename.c_str()); CHECK(!result) << "Unexpected result from decode32_file call"; - CHECK(width2 == kWidth) << "Widths differ"; - CHECK(height2 == kHeight) << "Heights differ"; + CHECK(width == kWidth) << "Widths differ"; + CHECK(height == kHeight) << "Heights differ"; // Now, we can compare the values. CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen)) @@ -52,7 +52,7 @@ void EncodeDecodeOneStep(const std::string &images_path) { void EncodeDecodeTwoSteps(const std::string &images_path) { // Generate the values. - std::vector image(GenerateValues()); + std::vector image = GenerateValues(); // Encode the image into memory first. const std::string filename = images_path + "/out_generated2.png"; @@ -70,7 +70,7 @@ void EncodeDecodeTwoSteps(const std::string &images_path) { CHECK(!result) << "Unexpected result from save_file call"; // Now, decode the image using the 2 steps in order to compare the values. - unsigned int width2, height2; + unsigned int width, height; uint8_t *png2; size_t pngsize2; @@ -81,11 +81,11 @@ void EncodeDecodeTwoSteps(const std::string &images_path) { CHECK(pngsize == pngsize2) << "Png sizes differ"; uint8_t *image2; - result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2); + result = lodepng_decode32(&image2, &width, &height, png2, pngsize2); CHECK(!result) << "Unexpected result from decode32 call"; - CHECK(width2 == kWidth) << "Widths differ"; - CHECK(height2 == kHeight) << "Heights differ"; + CHECK(width == kWidth) << "Widths differ"; + CHECK(height == kHeight) << "Heights differ"; // Compare the values. CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))