2020-08-07 19:01:41 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-08-07 19:01:41 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2022-10-20 21:48:06 +08:00
|
|
|
#include <cstdint>
|
2020-08-07 19:01:41 +08:00
|
|
|
#include <iostream>
|
2022-10-20 21:48:06 +08:00
|
|
|
#include <vector>
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
#include "helpers.h" // NOLINT(build/include)
|
|
|
|
#include "lodepng.gen.h" // NOLINT(build/include)
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/flags/parse.h"
|
|
|
|
#include "absl/log/check.h"
|
|
|
|
#include "absl/log/globals.h"
|
|
|
|
#include "absl/log/initialize.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
void EncodeDecodeOneStep(const std::string& images_path) {
|
2020-08-28 21:29:12 +08:00
|
|
|
// Generate the values.
|
2020-09-14 23:23:29 +08:00
|
|
|
std::vector<uint8_t> image = GenerateValues();
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Encode the image.
|
2020-09-25 02:17:27 +08:00
|
|
|
const std::string filename =
|
2022-06-10 19:23:39 +08:00
|
|
|
sapi::file::JoinPath(images_path, "/out_generated1.png");
|
2020-08-13 16:34:43 +08:00
|
|
|
unsigned int result =
|
2020-08-28 03:53:06 +08:00
|
|
|
lodepng_encode32_file(filename.c_str(), image.data(), kWidth, kHeight);
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(!result) << "Unexpected result from encode32_file call";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// After the image has been encoded, decode it to check that the
|
|
|
|
// pixel values are the same.
|
2020-09-14 23:23:29 +08:00
|
|
|
unsigned int width, height;
|
2020-09-24 01:12:38 +08:00
|
|
|
uint8_t* image2 = 0;
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
result = lodepng_decode32_file(&image2, &width, &height, filename.c_str());
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(!result) << "Unexpected result from decode32_file call";
|
2020-08-11 15:15:53 +08:00
|
|
|
|
2020-09-14 23:23:29 +08:00
|
|
|
CHECK(width == kWidth) << "Widths differ";
|
|
|
|
CHECK(height == kHeight) << "Heights differ";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Now, we can compare the values.
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
|
|
|
|
<< "Values differ";
|
2020-08-28 21:29:12 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
free(image2);
|
2020-08-11 15:15:53 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
void EncodeDecodeTwoSteps(const std::string& images_path) {
|
2020-08-28 21:29:12 +08:00
|
|
|
// Generate the values.
|
2020-09-14 23:23:29 +08:00
|
|
|
std::vector<uint8_t> image = GenerateValues();
|
2020-08-11 15:15:53 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Encode the image into memory first.
|
2020-09-25 02:17:27 +08:00
|
|
|
const std::string filename =
|
2022-06-10 19:23:39 +08:00
|
|
|
sapi::file::JoinPath(images_path, "/out_generated2.png");
|
2020-09-24 01:12:38 +08:00
|
|
|
uint8_t* png;
|
2020-08-13 16:34:43 +08:00
|
|
|
size_t pngsize;
|
|
|
|
|
2020-08-13 22:44:18 +08:00
|
|
|
unsigned int result =
|
2020-08-28 03:53:06 +08:00
|
|
|
lodepng_encode32(&png, &pngsize, image.data(), kWidth, kHeight);
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(!result) << "Unexpected result from encode32 call";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Write the image into the file (from memory).
|
2020-08-13 16:34:43 +08:00
|
|
|
result = lodepng_save_file(png, pngsize, filename.c_str());
|
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(!result) << "Unexpected result from save_file call";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Now, decode the image using the 2 steps in order to compare the values.
|
2020-09-14 23:23:29 +08:00
|
|
|
unsigned int width, height;
|
2020-09-24 01:12:38 +08:00
|
|
|
uint8_t* png2;
|
2020-08-13 16:34:43 +08:00
|
|
|
size_t pngsize2;
|
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Load the file in memory.
|
2020-08-13 16:34:43 +08:00
|
|
|
result = lodepng_load_file(&png2, &pngsize2, filename.c_str());
|
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(!result) << "Unexpected result from load_file call";
|
|
|
|
CHECK(pngsize == pngsize2) << "Png sizes differ";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
uint8_t* image2;
|
2020-09-14 23:23:29 +08:00
|
|
|
result = lodepng_decode32(&image2, &width, &height, png2, pngsize2);
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-30 02:54:08 +08:00
|
|
|
CHECK(!result) << "Unexpected result from decode32 call";
|
2020-09-14 23:23:29 +08:00
|
|
|
CHECK(width == kWidth) << "Widths differ";
|
|
|
|
CHECK(height == kHeight) << "Heights differ";
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 21:29:12 +08:00
|
|
|
// Compare the values.
|
2020-09-08 22:21:57 +08:00
|
|
|
CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
|
|
|
|
<< "Values differ";
|
2020-08-28 21:29:12 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
free(png);
|
|
|
|
free(png2);
|
|
|
|
free(image2);
|
2020-08-11 15:15:53 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 01:12:38 +08:00
|
|
|
int main(int argc, char* argv[]) {
|
2022-10-20 21:48:06 +08:00
|
|
|
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
|
|
|
|
absl::ParseCommandLine(argc, argv);
|
|
|
|
absl::InitializeLog();
|
2020-08-13 16:34:43 +08:00
|
|
|
|
2020-08-28 03:53:06 +08:00
|
|
|
const std::string images_path = CreateTempDirAtCWD();
|
2022-06-10 19:23:39 +08:00
|
|
|
CHECK(sapi::file_util::fileops::Exists(images_path, false))
|
2020-08-30 02:54:08 +08:00
|
|
|
<< "Temporary directory does not exist";
|
2020-08-28 03:53:06 +08:00
|
|
|
|
|
|
|
EncodeDecodeOneStep(images_path);
|
|
|
|
EncodeDecodeTwoSteps(images_path);
|
|
|
|
|
2022-06-10 19:23:39 +08:00
|
|
|
if (sapi::file_util::fileops::DeleteRecursively(images_path)) {
|
2020-08-30 02:54:08 +08:00
|
|
|
LOG(WARNING) << "Temporary folder could not be deleted";
|
|
|
|
}
|
2020-08-07 19:01:41 +08:00
|
|
|
|
2020-08-13 23:46:21 +08:00
|
|
|
return EXIT_SUCCESS;
|
2020-08-28 03:53:06 +08:00
|
|
|
}
|