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