xref: /aosp_15_r20/external/sandboxed-api/contrib/lodepng/examples/main_unsandboxed.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2020 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include <cstdint>
16*ec63e07aSXin Li #include <iostream>
17*ec63e07aSXin Li #include <vector>
18*ec63e07aSXin Li 
19*ec63e07aSXin Li #include "helpers.h"      // NOLINT(build/include)
20*ec63e07aSXin Li #include "lodepng.gen.h"  // NOLINT(build/include)
21*ec63e07aSXin Li #include "absl/flags/parse.h"
22*ec63e07aSXin Li #include "absl/log/check.h"
23*ec63e07aSXin Li #include "absl/log/globals.h"
24*ec63e07aSXin Li #include "absl/log/initialize.h"
25*ec63e07aSXin Li #include "sandboxed_api/util/fileops.h"
26*ec63e07aSXin Li #include "sandboxed_api/util/path.h"
27*ec63e07aSXin Li 
EncodeDecodeOneStep(const std::string & images_path)28*ec63e07aSXin Li void EncodeDecodeOneStep(const std::string& images_path) {
29*ec63e07aSXin Li   // Generate the values.
30*ec63e07aSXin Li   std::vector<uint8_t> image = GenerateValues();
31*ec63e07aSXin Li 
32*ec63e07aSXin Li   // Encode the image.
33*ec63e07aSXin Li   const std::string filename =
34*ec63e07aSXin Li       sapi::file::JoinPath(images_path, "/out_generated1.png");
35*ec63e07aSXin Li   unsigned int result =
36*ec63e07aSXin Li       lodepng_encode32_file(filename.c_str(), image.data(), kWidth, kHeight);
37*ec63e07aSXin Li 
38*ec63e07aSXin Li   CHECK(!result) << "Unexpected result from encode32_file call";
39*ec63e07aSXin Li 
40*ec63e07aSXin Li   // After the image has been encoded, decode it to check that the
41*ec63e07aSXin Li   // pixel values are the same.
42*ec63e07aSXin Li   unsigned int width, height;
43*ec63e07aSXin Li   uint8_t* image2 = 0;
44*ec63e07aSXin Li 
45*ec63e07aSXin Li   result = lodepng_decode32_file(&image2, &width, &height, filename.c_str());
46*ec63e07aSXin Li 
47*ec63e07aSXin Li   CHECK(!result) << "Unexpected result from decode32_file call";
48*ec63e07aSXin Li 
49*ec63e07aSXin Li   CHECK(width == kWidth) << "Widths differ";
50*ec63e07aSXin Li   CHECK(height == kHeight) << "Heights differ";
51*ec63e07aSXin Li 
52*ec63e07aSXin Li   // Now, we can compare the values.
53*ec63e07aSXin Li   CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
54*ec63e07aSXin Li       << "Values differ";
55*ec63e07aSXin Li 
56*ec63e07aSXin Li   free(image2);
57*ec63e07aSXin Li }
58*ec63e07aSXin Li 
EncodeDecodeTwoSteps(const std::string & images_path)59*ec63e07aSXin Li void EncodeDecodeTwoSteps(const std::string& images_path) {
60*ec63e07aSXin Li   // Generate the values.
61*ec63e07aSXin Li   std::vector<uint8_t> image = GenerateValues();
62*ec63e07aSXin Li 
63*ec63e07aSXin Li   // Encode the image into memory first.
64*ec63e07aSXin Li   const std::string filename =
65*ec63e07aSXin Li       sapi::file::JoinPath(images_path, "/out_generated2.png");
66*ec63e07aSXin Li   uint8_t* png;
67*ec63e07aSXin Li   size_t pngsize;
68*ec63e07aSXin Li 
69*ec63e07aSXin Li   unsigned int result =
70*ec63e07aSXin Li       lodepng_encode32(&png, &pngsize, image.data(), kWidth, kHeight);
71*ec63e07aSXin Li 
72*ec63e07aSXin Li   CHECK(!result) << "Unexpected result from encode32 call";
73*ec63e07aSXin Li 
74*ec63e07aSXin Li   // Write the image into the file (from memory).
75*ec63e07aSXin Li   result = lodepng_save_file(png, pngsize, filename.c_str());
76*ec63e07aSXin Li 
77*ec63e07aSXin Li   CHECK(!result) << "Unexpected result from save_file call";
78*ec63e07aSXin Li 
79*ec63e07aSXin Li   // Now, decode the image using the 2 steps in order to compare the values.
80*ec63e07aSXin Li   unsigned int width, height;
81*ec63e07aSXin Li   uint8_t* png2;
82*ec63e07aSXin Li   size_t pngsize2;
83*ec63e07aSXin Li 
84*ec63e07aSXin Li   // Load the file in memory.
85*ec63e07aSXin Li   result = lodepng_load_file(&png2, &pngsize2, filename.c_str());
86*ec63e07aSXin Li 
87*ec63e07aSXin Li   CHECK(!result) << "Unexpected result from load_file call";
88*ec63e07aSXin Li   CHECK(pngsize == pngsize2) << "Png sizes differ";
89*ec63e07aSXin Li 
90*ec63e07aSXin Li   uint8_t* image2;
91*ec63e07aSXin Li   result = lodepng_decode32(&image2, &width, &height, png2, pngsize2);
92*ec63e07aSXin Li 
93*ec63e07aSXin Li   CHECK(!result) << "Unexpected result from decode32 call";
94*ec63e07aSXin Li   CHECK(width == kWidth) << "Widths differ";
95*ec63e07aSXin Li   CHECK(height == kHeight) << "Heights differ";
96*ec63e07aSXin Li 
97*ec63e07aSXin Li   // Compare the values.
98*ec63e07aSXin Li   CHECK(absl::equal(image.begin(), image.end(), image2, image2 + kImgLen))
99*ec63e07aSXin Li       << "Values differ";
100*ec63e07aSXin Li 
101*ec63e07aSXin Li   free(png);
102*ec63e07aSXin Li   free(png2);
103*ec63e07aSXin Li   free(image2);
104*ec63e07aSXin Li }
105*ec63e07aSXin Li 
main(int argc,char * argv[])106*ec63e07aSXin Li int main(int argc, char* argv[]) {
107*ec63e07aSXin Li   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
108*ec63e07aSXin Li   absl::ParseCommandLine(argc, argv);
109*ec63e07aSXin Li   absl::InitializeLog();
110*ec63e07aSXin Li 
111*ec63e07aSXin Li   const std::string images_path = CreateTempDirAtCWD();
112*ec63e07aSXin Li   CHECK(sapi::file_util::fileops::Exists(images_path, false))
113*ec63e07aSXin Li       << "Temporary directory does not exist";
114*ec63e07aSXin Li 
115*ec63e07aSXin Li   EncodeDecodeOneStep(images_path);
116*ec63e07aSXin Li   EncodeDecodeTwoSteps(images_path);
117*ec63e07aSXin Li 
118*ec63e07aSXin Li   if (sapi::file_util::fileops::DeleteRecursively(images_path)) {
119*ec63e07aSXin Li     LOG(WARNING) << "Temporary folder could not be deleted";
120*ec63e07aSXin Li   }
121*ec63e07aSXin Li 
122*ec63e07aSXin Li   return EXIT_SUCCESS;
123*ec63e07aSXin Li }
124