added requested changes

This commit is contained in:
Katarzyna Miernikiewicz 2020-08-26 12:17:18 +00:00
parent ba88cd3499
commit 9f120598c1
4 changed files with 34 additions and 65 deletions

View File

@ -13,7 +13,7 @@
# limitations under the License.
# copy of one of the converting functions
# put here to omit the library's complex
# put here to omit the library's complex build logic
add_library(convert_helper STATIC
convert_helper.h
convert_helper.cc

View File

@ -40,6 +40,9 @@
#include "convert_helper.h"
#define OPJ_TRUE 1
#define OPJ_FALSE 0
const char *opj_version(void) { return "2.3.1"; }
static int are_comps_similar(opj_image_t *image) {

View File

@ -3,9 +3,6 @@
#include "openjp2_sapi.sapi.h"
#define OPJ_TRUE 1
#define OPJ_FALSE 0
const char* opj_version(void);
static int are_comps_similar(opj_image_t* image);
int imagetopnm(opj_image_t* image, const char* outfile, int force_split);

View File

@ -27,8 +27,7 @@
class Openjp2SapiSandbox : public Openjp2Sandbox {
public:
Openjp2SapiSandbox(const std::string in_file)
: in_file_(std::move(in_file)) {}
Openjp2SapiSandbox(std::string in_file) : in_file_(std::move(in_file)) {}
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
@ -68,10 +67,7 @@ int main(int argc, char* argv[]) {
// initialize sandbox
Openjp2SapiSandbox sandbox(in_file);
absl::Status status = sandbox.Init();
if (!status.ok()) {
LOG(FATAL) << "sandbox initialization status: " << status;
return EXIT_FAILURE;
}
CHECK(status.ok()) << "sandbox initialization failed" << status;
Openjp2Api api(&sandbox);
sapi::v::ConstCStr in_file_v(in_file.c_str());
@ -79,110 +75,83 @@ int main(int argc, char* argv[]) {
// initialize library's main data-holders
sapi::StatusOr<opj_stream_t*> stream =
api.opj_stream_create_default_file_stream(in_file_v.PtrBefore(), 1);
if (!stream.ok()) {
LOG(FATAL) << "opj_stream initialization failed: " << stream.status();
return EXIT_FAILURE;
}
CHECK(stream.ok()) << "opj_stream initialization failed: " << stream.status();
sapi::v::RemotePtr stream_pointer(stream.value());
sapi::StatusOr<opj_codec_t*> codec = api.opj_create_decompress(OPJ_CODEC_JP2);
if (!codec.ok()) {
LOG(FATAL) << "opj_codec initialization failed: " << codec.status();
return EXIT_FAILURE;
}
CHECK(codec.ok()) << "opj_codec initialization failed: " << stream.status();
sapi::v::RemotePtr codec_pointer(codec.value());
sapi::v::Struct<opj_dparameters_t> parameters;
status = api.opj_set_default_decoder_parameters(parameters.PtrBoth());
if (!status.ok()) {
LOG(FATAL) << "parameters initialization failed: " << status;
return EXIT_FAILURE;
}
CHECK(status.ok()) << "parameters initialization failed" << status;
sapi::StatusOr<OPJ_BOOL> bool_status =
api.opj_setup_decoder(&codec_pointer, parameters.PtrBefore());
if (!bool_status.ok() || !bool_status.value()) {
LOG(FATAL) << "decoder setup failed";
return EXIT_FAILURE;
}
CHECK(bool_status.ok() && bool_status.value()) << "decoder setup failed";
// start reading image from the input file
sapi::v::GenericPtr image_pointer;
bool_status = api.opj_read_header(&stream_pointer, &codec_pointer,
image_pointer.PtrAfter());
if (!bool_status.ok() || !bool_status.value()) {
LOG(FATAL) << "reading image header failed";
return EXIT_FAILURE;
}
CHECK(bool_status.ok() && bool_status.value())
<< "reading image header failed";
sapi::v::Struct<opj_image_t> image;
image.SetRemote((void*)image_pointer.GetValue());
if (!sandbox.TransferFromSandboxee(&image).ok()) {
LOG(FATAL) << "transfer from sandboxee failed";
return EXIT_FAILURE;
}
image.SetRemote(reinterpret_cast<void*>(image_pointer.GetValue()));
CHECK(sandbox.TransferFromSandboxee(&image).ok())
<< "transfer from sandboxee failed";
sapi::v::RemotePtr image_remote_pointer(image.GetRemote());
bool_status =
api.opj_decode(&codec_pointer, &stream_pointer, &image_remote_pointer);
if (!bool_status.ok() || !bool_status.value()) {
LOG(FATAL) << "decoding failed";
return EXIT_FAILURE;
}
api.opj_decode(&codec_pointer, &stream_pointer, image.PtrAfter());
CHECK(bool_status.ok() && bool_status.value()) << "decoding failed";
bool_status = api.opj_end_decompress(&codec_pointer, &stream_pointer);
if (!bool_status.ok() || !bool_status.value()) {
LOG(FATAL) << "ending decompress failed";
return EXIT_FAILURE;
}
CHECK(bool_status.ok() && bool_status.value()) << "ending decompress failed";
int components = image.data().numcomps;
// transfer the read data to the main process
sapi::v::Array<opj_image_comp_t> image_components(components);
image_components.SetRemote(image.data().comps);
if (!sandbox.TransferFromSandboxee(&image_components).ok()) {
LOG(FATAL) << "transfer from sandboxee failed";
return EXIT_FAILURE;
}
CHECK(sandbox.TransferFromSandboxee(&image_components).ok())
<< "transfer from sandboxee failed";
image.mutable_data()->comps = (opj_image_comp_t*)image_components.GetLocal();
int width = (int)image.data().comps[0].w;
int height = (int)image.data().comps[0].h;
unsigned int width = reinterpret_cast<unsigned int>(image.data().comps[0].w);
unsigned height = reinterpret_cast<unsigned int>(image.data().comps[0].h);
std::vector<std::vector<OPJ_INT32>> data;
std::vector<std::vector<OPJ_INT32>> data(components);
sapi::v::Array<OPJ_INT32> image_components_data(width * height);
for (int i = 0; i < components; ++i) {
image_components_data.SetRemote(image.data().comps[i].data);
if (!sandbox.TransferFromSandboxee(&image_components_data).ok()) {
LOG(FATAL) << "transfer from sandboxee failed";
return EXIT_FAILURE;
}
CHECK(sandbox.TransferFromSandboxee(&image_components_data).ok())
<< "transfer from sandboxee failed";
std::vector<OPJ_INT32> component_data(
image_components_data.GetData(),
image_components_data.GetData() + (width * height));
data.push_back(component_data);
}
for (int i = 0; i < components; ++i) {
data[i] = std::move(component_data);
image_components[i].data = &data[i][0];
}
// convert the image to the desired format and save it to the file
int error = imagetopnm((opj_image_t*)image.GetLocal(), argv[2], 0);
if (error) LOG(FATAL) << "image convert failed";
int error =
imagetopnm(reinterpret_cast<opj_image_t*>(image.GetLocal()), argv[2], 0);
CHECK(!error) << "image convert failed";
// cleanup
status = api.opj_image_destroy(image.PtrNone());
if (!status.ok()) LOG(FATAL) << "image destroy failed: " << status;
CHECK(status.ok()) << "image destroy failed" << status;
status = api.opj_stream_destroy(&stream_pointer);
if (!status.ok()) LOG(FATAL) << "stream destroy failed: " << status;
CHECK(status.ok()) << "stream destroy failed" << status;
status = api.opj_destroy_codec(&codec_pointer);
if (!status.ok()) LOG(FATAL) << "codec destroy failed: " << status;
CHECK(status.ok()) << "codec destroy failed" << status;
return EXIT_SUCCESS;
}