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. # limitations under the License.
# copy of one of the converting functions # 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 add_library(convert_helper STATIC
convert_helper.h convert_helper.h
convert_helper.cc convert_helper.cc

View File

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

View File

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

View File

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