sandboxed-api/oss-internship-2020/sapi_lodepng/examples/main_unsandboxed.cc

108 lines
2.8 KiB
C++
Raw Normal View History

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
//
// 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 <glog/logging.h>
2020-08-07 19:01:41 +08:00
#include <iostream>
#include "../lodepng/lodepng.h"
#include "helpers.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
void EncodeDecodeOneStep(const std::string &images_path) {
// generate the values
std::vector<uint8_t> image(GenerateValues());
2020-08-07 19:01:41 +08:00
// encode the image
const std::string filename = images_path + "/out_generated1.png";
unsigned int result =
lodepng_encode32_file(filename.c_str(), image.data(), kWidth, kHeight);
CHECK(!result);
// after the image has been encoded, decode it to check that the
// pixel values are the same
unsigned int width2, height2;
uint8_t *image2 = 0;
result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str());
CHECK(!result);
CHECK(width2 == kWidth);
CHECK(height2 == kHeight);
// now, we can compare the values
CHECK(std::equal(image.begin(), image.end(), image2));
free(image2);
}
void EncodeDecodeTwoSteps(const std::string &images_path) {
// generate the values
std::vector<uint8_t> image(GenerateValues());
// encode the image into memory first
const std::string filename = images_path + "/out_generated2.png";
uint8_t *png;
size_t pngsize;
unsigned int result =
lodepng_encode32(&png, &pngsize, image.data(), kWidth, kHeight);
CHECK(!result);
// write the image into the file (from memory)
result = lodepng_save_file(png, pngsize, filename.c_str());
CHECK(!result);
// now, decode the image using the 2 steps in order to compare the values
unsigned int width2, height2;
uint8_t *png2;
size_t pngsize2;
// load the file in memory
result = lodepng_load_file(&png2, &pngsize2, filename.c_str());
CHECK(!result);
CHECK(pngsize == pngsize2);
uint8_t *image2;
result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2);
CHECK(!result);
CHECK(width2 == kWidth);
CHECK(height2 == kHeight);
// compare values
CHECK(std::equal(image.begin(), image.end(), image2));
free(png);
free(png2);
free(image2);
}
2020-08-07 19:01:41 +08:00
int main(int argc, char *argv[]) {
google::InitGoogleLogging(argv[0]);
const std::string images_path = CreateTempDirAtCWD();
EncodeDecodeOneStep(images_path);
EncodeDecodeTwoSteps(images_path);
CHECK(sandbox2::file_util::fileops::DeleteRecursively(images_path));
2020-08-07 19:01:41 +08:00
2020-08-13 23:46:21 +08:00
return EXIT_SUCCESS;
}