diff --git a/libheif/plugins/encoder_openjpeg.cc b/libheif/plugins/encoder_openjpeg.cc index d69904d326..6be62566fd 100644 --- a/libheif/plugins/encoder_openjpeg.cc +++ b/libheif/plugins/encoder_openjpeg.cc @@ -388,6 +388,7 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_ opj_codec_t* codec = opj_create_compress(codec_format); success = opj_setup_encoder(codec, &(encoder->parameters), image); if (!success) { + opj_destroy_codec(codec); error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed to setup OpenJPEG encoder"}; return error; } @@ -397,6 +398,7 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_ size_t bufferSize = 64 * 1024; // 64k opj_stream_t* stream = opj_stream_create(bufferSize, false /* read only mode */); if (stream == NULL) { + opj_destroy_codec(codec); error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed to create opj_stream_t"}; return error; } @@ -415,22 +417,31 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_ success = opj_start_compress(codec, image, stream); if (!success) { + opj_stream_destroy(stream); + opj_destroy_codec(codec); error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed opj_start_compress()"}; return error; } success = opj_encode(codec, stream); if (!success) { + opj_stream_destroy(stream); + opj_destroy_codec(codec); error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed opj_encode()"}; return error; } success = opj_end_compress(codec, stream); if (!success) { + opj_stream_destroy(stream); + opj_destroy_codec(codec); error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed opj_end_compress()"}; return error; } + opj_stream_destroy(stream); + opj_destroy_codec(codec); + return heif_error_ok; } @@ -531,6 +542,8 @@ struct heif_error opj_encode_image(void* encoder_raw, const struct heif_image* i //Encodes the image into a 'codestream' which is stored in the 'encoder' variable err = generate_codestream(opj_image, encoder); + opj_image_destroy(opj_image); + return err; } diff --git a/tests/encode_jpeg2000.cc b/tests/encode_jpeg2000.cc index 492de47956..bb2c7cb2e0 100644 --- a/tests/encode_jpeg2000.cc +++ b/tests/encode_jpeg2000.cc @@ -45,7 +45,7 @@ static heif_encoding_options * set_encoding_options() static void do_encode(heif_image* input_image, const char* filename, bool lossless) { REQUIRE(input_image != nullptr); - + heif_init(nullptr); heif_context *ctx = heif_context_alloc(); heif_encoder *encoder; struct heif_error err; @@ -70,6 +70,7 @@ static void do_encode(heif_image* input_image, const char* filename, bool lossle heif_image_release(input_image); heif_context_free(ctx); + heif_deinit(); } TEST_CASE("Encode JPEG2000 lossy")