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 // Perform decompression from *.jp2 to *.pnm format
16
17 #include <libgen.h>
18 #include <syscall.h>
19
20 #include <cstdlib>
21 #include <iostream>
22 #include <vector>
23
24 #include "gen_files/convert.h" // NOLINT(build/include)
25 #include "openjp2_sapi.sapi.h" // NOLINT(build/include)
26 #include "absl/status/statusor.h"
27
28 class Openjp2SapiSandbox : public Openjp2Sandbox {
29 public:
Openjp2SapiSandbox(std::string in_file)30 explicit Openjp2SapiSandbox(std::string in_file)
31 : in_file_(std::move(in_file)) {}
32
ModifyPolicy(sandbox2::PolicyBuilder *)33 std::unique_ptr<sandbox2::Policy> ModifyPolicy(
34 sandbox2::PolicyBuilder*) override {
35 return sandbox2::PolicyBuilder()
36 .AllowStaticStartup()
37 .AllowOpen()
38 .AllowRead()
39 .AllowWrite()
40 .AllowStat()
41 .AllowSystemMalloc()
42 .AllowExit()
43 .AllowSyscalls({
44 __NR_futex,
45 __NR_close,
46 __NR_lseek,
47 })
48 .AddFile(in_file_)
49 .BuildOrDie();
50 }
51
52 private:
53 std::string in_file_;
54 };
55
main(int argc,char * argv[])56 int main(int argc, char* argv[]) {
57 gflags::ParseCommandLineFlags(&argc, &argv, true);
58 sapi::InitLogging(argv[0]);
59
60 if (argc != 3) {
61 std::cerr << "Usage: " << basename(argv[0]) << " absolute/path/to/INPUT.jp2"
62 << " absolute/path/to/OUTPUT.pnm\n";
63 return EXIT_FAILURE;
64 }
65
66 std::string in_file(argv[1]);
67
68 // Initialize sandbox.
69 Openjp2SapiSandbox sandbox(in_file);
70 absl::Status status = sandbox.Init();
71 CHECK(status.ok()) << "Sandbox initialization failed " << status;
72
73 Openjp2Api api(&sandbox);
74 sapi::v::ConstCStr in_file_v(in_file.c_str());
75
76 // Initialize library's main data-holders.
77 absl::StatusOr<opj_stream_t*> stream =
78 api.opj_stream_create_default_file_stream(in_file_v.PtrBefore(), 1);
79 CHECK(stream.ok()) << "Stream initialization failed: " << stream.status();
80 sapi::v::RemotePtr stream_pointer(stream.value());
81
82 absl::StatusOr<opj_codec_t*> codec = api.opj_create_decompress(OPJ_CODEC_JP2);
83 CHECK(codec.ok()) << "Codec initialization failed: " << stream.status();
84 sapi::v::RemotePtr codec_pointer(codec.value());
85
86 sapi::v::Struct<opj_dparameters_t> parameters;
87 status = api.opj_set_default_decoder_parameters(parameters.PtrBoth());
88 CHECK(status.ok()) << "Parameters initialization failed " << status;
89
90 absl::StatusOr<OPJ_BOOL> bool_status =
91 api.opj_setup_decoder(&codec_pointer, parameters.PtrBefore());
92 CHECK(bool_status.ok() && bool_status.value()) << "Decoder setup failed";
93
94 // Start reading image from the input file.
95 sapi::v::GenericPtr image_pointer;
96 bool_status = api.opj_read_header(&stream_pointer, &codec_pointer,
97 image_pointer.PtrAfter());
98 CHECK(bool_status.ok() && bool_status.value())
99 << "Reading image header failed";
100
101 sapi::v::Struct<opj_image_t> image;
102 image.SetRemote(reinterpret_cast<void*>(image_pointer.GetValue()));
103 CHECK(sandbox.TransferFromSandboxee(&image).ok())
104 << "Transfer from sandboxee failed";
105
106 bool_status =
107 api.opj_decode(&codec_pointer, &stream_pointer, image.PtrAfter());
108 CHECK(bool_status.ok() && bool_status.value()) << "Decoding failed";
109
110 bool_status = api.opj_end_decompress(&codec_pointer, &stream_pointer);
111 CHECK(bool_status.ok() && bool_status.value()) << "Ending decompress failed";
112
113 int components = image.data().numcomps;
114
115 // Transfer the read data to the main process.
116 sapi::v::Array<opj_image_comp_t> image_components(components);
117 image_components.SetRemote(image.data().comps);
118 CHECK(sandbox.TransferFromSandboxee(&image_components).ok())
119 << "Transfer from sandboxee failed";
120
121 image.mutable_data()->comps =
122 static_cast<opj_image_comp_t*>(image_components.GetLocal());
123
124 unsigned int width = static_cast<unsigned int>(image.data().comps[0].w);
125 unsigned int height = static_cast<unsigned int>(image.data().comps[0].h);
126
127 std::vector<std::vector<OPJ_INT32>> data(components);
128 sapi::v::Array<OPJ_INT32> image_components_data(width * height);
129
130 for (int i = 0; i < components; ++i) {
131 image_components_data.SetRemote(image.data().comps[i].data);
132 CHECK(sandbox.TransferFromSandboxee(&image_components_data).ok())
133 << "Transfer from sandboxee failed";
134
135 std::vector<OPJ_INT32> component_data(
136 image_components_data.GetData(),
137 image_components_data.GetData() + (width * height));
138 data[i] = std::move(component_data);
139 image_components[i].data = &data[i][0];
140 }
141
142 // Convert the image to the desired format and save it to the file.
143 int error =
144 imagetopnm(static_cast<opj_image_t*>(image.GetLocal()), argv[2], 0);
145 CHECK(!error) << "Image convert failed";
146
147 // Clean up.
148 status = api.opj_image_destroy(image.PtrNone());
149 CHECK(status.ok()) << "Image destroy failed " << status;
150
151 status = api.opj_stream_destroy(&stream_pointer);
152 CHECK(status.ok()) << "Stream destroy failed " << status;
153
154 status = api.opj_destroy_codec(&codec_pointer);
155 CHECK(status.ok()) << "Codec destroy failed " << status;
156
157 return EXIT_SUCCESS;
158 }
159