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