2020-08-12 19:24:30 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-08-14 18:38:27 +08:00
|
|
|
// Perform decompression from *.jp2 to *.pnm format
|
2020-08-12 19:24:30 +08:00
|
|
|
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
|
2020-08-14 18:38:27 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2020-08-24 23:01:42 +08:00
|
|
|
#include <vector>
|
2020-08-12 19:24:30 +08:00
|
|
|
|
2020-09-17 20:28:19 +08:00
|
|
|
#include "gen_files/convert.h" // NOLINT(build/include)
|
|
|
|
#include "openjp2_sapi.sapi.h" // NOLINT(build/include)
|
2020-08-12 19:24:30 +08:00
|
|
|
|
|
|
|
class Openjp2SapiSandbox : public Openjp2Sandbox {
|
|
|
|
public:
|
2020-08-27 16:39:59 +08:00
|
|
|
explicit Openjp2SapiSandbox(std::string in_file)
|
|
|
|
: in_file_(std::move(in_file)) {}
|
2020-08-12 19:24:30 +08:00
|
|
|
|
|
|
|
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
|
|
|
sandbox2::PolicyBuilder*) override {
|
|
|
|
return sandbox2::PolicyBuilder()
|
|
|
|
.AllowStaticStartup()
|
|
|
|
.AllowOpen()
|
|
|
|
.AllowRead()
|
|
|
|
.AllowWrite()
|
|
|
|
.AllowStat()
|
|
|
|
.AllowSystemMalloc()
|
|
|
|
.AllowExit()
|
|
|
|
.AllowSyscalls({
|
2020-08-14 18:38:27 +08:00
|
|
|
__NR_futex,
|
|
|
|
__NR_close,
|
|
|
|
__NR_lseek,
|
2020-08-12 19:24:30 +08:00
|
|
|
})
|
|
|
|
.AddFile(in_file_)
|
|
|
|
.BuildOrDie();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string in_file_;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2020-08-14 18:38:27 +08:00
|
|
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
2020-08-24 23:01:42 +08:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
if (argc != 3) {
|
2020-08-27 16:46:12 +08:00
|
|
|
std::cerr << "Usage: " << basename(argv[0]) << " absolute/path/to/INPUT.jp2"
|
2020-08-14 18:38:27 +08:00
|
|
|
<< " absolute/path/to/OUTPUT.pnm\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string in_file(argv[1]);
|
|
|
|
|
2020-08-27 16:46:12 +08:00
|
|
|
// Initialize sandbox.
|
2020-08-14 18:38:27 +08:00
|
|
|
Openjp2SapiSandbox sandbox(in_file);
|
|
|
|
absl::Status status = sandbox.Init();
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(status.ok()) << "Sandbox initialization failed " << status;
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
Openjp2Api api(&sandbox);
|
|
|
|
sapi::v::ConstCStr in_file_v(in_file.c_str());
|
|
|
|
|
2020-08-27 16:46:12 +08:00
|
|
|
// Initialize library's main data-holders.
|
2020-08-24 23:01:42 +08:00
|
|
|
sapi::StatusOr<opj_stream_t*> stream =
|
2020-08-14 18:38:27 +08:00
|
|
|
api.opj_stream_create_default_file_stream(in_file_v.PtrBefore(), 1);
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(stream.ok()) << "Stream initialization failed: " << stream.status();
|
2020-08-24 23:01:42 +08:00
|
|
|
sapi::v::RemotePtr stream_pointer(stream.value());
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-24 23:01:42 +08:00
|
|
|
sapi::StatusOr<opj_codec_t*> codec = api.opj_create_decompress(OPJ_CODEC_JP2);
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(codec.ok()) << "Codec initialization failed: " << stream.status();
|
2020-08-24 23:01:42 +08:00
|
|
|
sapi::v::RemotePtr codec_pointer(codec.value());
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-24 23:01:42 +08:00
|
|
|
sapi::v::Struct<opj_dparameters_t> parameters;
|
2020-08-14 18:38:27 +08:00
|
|
|
status = api.opj_set_default_decoder_parameters(parameters.PtrBoth());
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(status.ok()) << "Parameters initialization failed " << status;
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
sapi::StatusOr<OPJ_BOOL> bool_status =
|
|
|
|
api.opj_setup_decoder(&codec_pointer, parameters.PtrBefore());
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(bool_status.ok() && bool_status.value()) << "Decoder setup failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-27 16:46:12 +08:00
|
|
|
// Start reading image from the input file.
|
2020-08-14 18:38:27 +08:00
|
|
|
sapi::v::GenericPtr image_pointer;
|
|
|
|
bool_status = api.opj_read_header(&stream_pointer, &codec_pointer,
|
|
|
|
image_pointer.PtrAfter());
|
2020-08-26 20:17:18 +08:00
|
|
|
CHECK(bool_status.ok() && bool_status.value())
|
2020-08-27 16:46:12 +08:00
|
|
|
<< "Reading image header failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-24 23:01:42 +08:00
|
|
|
sapi::v::Struct<opj_image_t> image;
|
2020-08-26 20:17:18 +08:00
|
|
|
image.SetRemote(reinterpret_cast<void*>(image_pointer.GetValue()));
|
|
|
|
CHECK(sandbox.TransferFromSandboxee(&image).ok())
|
2020-08-27 16:46:12 +08:00
|
|
|
<< "Transfer from sandboxee failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-25 22:53:53 +08:00
|
|
|
bool_status =
|
2020-08-26 20:17:18 +08:00
|
|
|
api.opj_decode(&codec_pointer, &stream_pointer, image.PtrAfter());
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(bool_status.ok() && bool_status.value()) << "Decoding failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
bool_status = api.opj_end_decompress(&codec_pointer, &stream_pointer);
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(bool_status.ok() && bool_status.value()) << "Ending decompress failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
int components = image.data().numcomps;
|
|
|
|
|
2020-08-27 16:46:12 +08:00
|
|
|
// Transfer the read data to the main process.
|
2020-08-14 18:38:27 +08:00
|
|
|
sapi::v::Array<opj_image_comp_t> image_components(components);
|
|
|
|
image_components.SetRemote(image.data().comps);
|
2020-08-26 20:17:18 +08:00
|
|
|
CHECK(sandbox.TransferFromSandboxee(&image_components).ok())
|
2020-08-27 16:46:12 +08:00
|
|
|
<< "Transfer from sandboxee failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-09-12 00:35:52 +08:00
|
|
|
image.mutable_data()->comps =
|
|
|
|
static_cast<opj_image_comp_t*>(image_components.GetLocal());
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-09-12 00:35:52 +08:00
|
|
|
unsigned int width = static_cast<unsigned int>(image.data().comps[0].w);
|
|
|
|
unsigned int height = static_cast<unsigned int>(image.data().comps[0].h);
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-26 20:17:18 +08:00
|
|
|
std::vector<std::vector<OPJ_INT32>> data(components);
|
2020-08-14 18:38:27 +08:00
|
|
|
sapi::v::Array<OPJ_INT32> image_components_data(width * height);
|
|
|
|
|
2020-08-24 23:01:42 +08:00
|
|
|
for (int i = 0; i < components; ++i) {
|
2020-08-14 18:38:27 +08:00
|
|
|
image_components_data.SetRemote(image.data().comps[i].data);
|
2020-08-26 20:17:18 +08:00
|
|
|
CHECK(sandbox.TransferFromSandboxee(&image_components_data).ok())
|
2020-08-27 16:46:12 +08:00
|
|
|
<< "Transfer from sandboxee failed";
|
2020-08-26 20:17:18 +08:00
|
|
|
|
2020-08-25 22:53:53 +08:00
|
|
|
std::vector<OPJ_INT32> component_data(
|
|
|
|
image_components_data.GetData(),
|
|
|
|
image_components_data.GetData() + (width * height));
|
2020-08-26 20:17:18 +08:00
|
|
|
data[i] = std::move(component_data);
|
2020-08-25 22:53:53 +08:00
|
|
|
image_components[i].data = &data[i][0];
|
2020-08-14 18:38:27 +08:00
|
|
|
}
|
|
|
|
|
2020-08-27 16:46:12 +08:00
|
|
|
// Convert the image to the desired format and save it to the file.
|
2020-08-26 20:17:18 +08:00
|
|
|
int error =
|
2020-09-12 00:35:52 +08:00
|
|
|
imagetopnm(static_cast<opj_image_t*>(image.GetLocal()), argv[2], 0);
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(!error) << "Image convert failed";
|
2020-08-14 18:38:27 +08:00
|
|
|
|
2020-08-27 16:46:12 +08:00
|
|
|
// Clean up.
|
2020-08-24 23:01:42 +08:00
|
|
|
status = api.opj_image_destroy(image.PtrNone());
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(status.ok()) << "Image destroy failed " << status;
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
status = api.opj_stream_destroy(&stream_pointer);
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(status.ok()) << "Stream destroy failed " << status;
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
status = api.opj_destroy_codec(&codec_pointer);
|
2020-08-27 16:46:12 +08:00
|
|
|
CHECK(status.ok()) << "Codec destroy failed " << status;
|
2020-08-14 18:38:27 +08:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2020-08-12 19:24:30 +08:00
|
|
|
}
|