xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/libpng/examples/example1.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 <string>
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #include "../sandboxed.h"     // NOLINT(build/include)
18*ec63e07aSXin Li #include "../tests/libpng.h"  // NOLINT(build/include)
19*ec63e07aSXin Li #include "sandboxed_api/vars.h"
20*ec63e07aSXin Li 
LibPNGMain(const std::string & infile,const std::string & outfile)21*ec63e07aSXin Li absl::Status LibPNGMain(const std::string& infile, const std::string& outfile) {
22*ec63e07aSXin Li   LibPNGSapiSandbox sandbox;
23*ec63e07aSXin Li   sandbox.AddFile(infile);
24*ec63e07aSXin Li   sandbox.AddFile(outfile);
25*ec63e07aSXin Li 
26*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(sandbox.Init());
27*ec63e07aSXin Li   LibPNGApi api(&sandbox);
28*ec63e07aSXin Li 
29*ec63e07aSXin Li   sapi::v::Struct<png_image> image;
30*ec63e07aSXin Li   sapi::v::ConstCStr infile_var(infile.c_str());
31*ec63e07aSXin Li   sapi::v::ConstCStr outfile_var(outfile.c_str());
32*ec63e07aSXin Li 
33*ec63e07aSXin Li   image.mutable_data()->version = PNG_IMAGE_VERSION;
34*ec63e07aSXin Li 
35*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(
36*ec63e07aSXin Li       int result, api.png_image_begin_read_from_file(image.PtrBoth(),
37*ec63e07aSXin Li                                                      infile_var.PtrBefore()));
38*ec63e07aSXin Li   if (!result) {
39*ec63e07aSXin Li     return absl::InternalError(
40*ec63e07aSXin Li         absl::StrCat("begin read error: ", image.mutable_data()->message));
41*ec63e07aSXin Li   }
42*ec63e07aSXin Li 
43*ec63e07aSXin Li   image.mutable_data()->format = PNG_FORMAT_RGBA;
44*ec63e07aSXin Li 
45*ec63e07aSXin Li   sapi::v::Array<uint8_t> buffer(PNG_IMAGE_SIZE(*image.mutable_data()));
46*ec63e07aSXin Li 
47*ec63e07aSXin Li   sapi::v::NullPtr null = sapi::v::NullPtr();
48*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(result,
49*ec63e07aSXin Li                         api.png_image_finish_read(image.PtrBoth(), &null,
50*ec63e07aSXin Li                                                   buffer.PtrBoth(), 0, &null));
51*ec63e07aSXin Li   if (!result) {
52*ec63e07aSXin Li     return absl::InternalError(
53*ec63e07aSXin Li         absl::StrCat("finish read error: ", image.mutable_data()->message));
54*ec63e07aSXin Li   }
55*ec63e07aSXin Li 
56*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(result, api.png_image_write_to_file(
57*ec63e07aSXin Li                                     image.PtrBoth(), outfile_var.PtrBefore(), 0,
58*ec63e07aSXin Li                                     buffer.PtrBoth(), 0, &null));
59*ec63e07aSXin Li   if (!result) {
60*ec63e07aSXin Li     return absl::InternalError(
61*ec63e07aSXin Li         absl::StrCat("write error: ", image.mutable_data()->message));
62*ec63e07aSXin Li   }
63*ec63e07aSXin Li 
64*ec63e07aSXin Li   return absl::OkStatus();
65*ec63e07aSXin Li }
66*ec63e07aSXin Li 
main(int argc,char * argv[])67*ec63e07aSXin Li int main(int argc, char* argv[]) {
68*ec63e07aSXin Li   if (argc != 3) {
69*ec63e07aSXin Li     LOG(ERROR) << "usage: example input-file output-file";
70*ec63e07aSXin Li     return EXIT_FAILURE;
71*ec63e07aSXin Li   }
72*ec63e07aSXin Li 
73*ec63e07aSXin Li   auto status = LibPNGMain(argv[1], argv[2]);
74*ec63e07aSXin Li   if (!status.ok()) {
75*ec63e07aSXin Li     LOG(ERROR) << "LibPNGMain failed with error:\n"
76*ec63e07aSXin Li                << status.ToString() << '\n';
77*ec63e07aSXin Li     return EXIT_FAILURE;
78*ec63e07aSXin Li   }
79*ec63e07aSXin Li 
80*ec63e07aSXin Li   return EXIT_SUCCESS;
81*ec63e07aSXin Li }
82