Skip to content

Commit

Permalink
openjpeg: add support for setting lossless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Dec 21, 2023
1 parent 94c3b21 commit f3ed62c
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 82 deletions.
20 changes: 12 additions & 8 deletions libheif/plugins/encoder_openjpeg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct encoder_struct_opj
{
int quality = 70;
heif_chroma chroma = heif_chroma_undefined;
opj_cparameters_t parameters;

// --- output

Expand Down Expand Up @@ -124,6 +125,7 @@ struct heif_error opj_new_encoder(void** encoder_out)
*encoder_out = encoder;

opj_set_default_parameters(encoder);
opj_set_default_encoder_parameters(&(encoder->parameters));

return heif_error_ok;
}
Expand Down Expand Up @@ -156,13 +158,17 @@ struct heif_error opj_get_parameter_quality(void* encoder_raw, int* quality)
return heif_error_ok;
}

struct heif_error opj_set_parameter_lossless(void* encoder, int lossless)
struct heif_error opj_set_parameter_lossless(void* encoder_raw, int lossless)
{
auto* encoder = (struct encoder_struct_opj*) encoder_raw;
encoder->parameters.irreversible = lossless ? 0 : 1;
return heif_error_ok;
}

struct heif_error opj_get_parameter_lossless(void* encoder, int* lossless)
struct heif_error opj_get_parameter_lossless(void* encoder_raw, int* lossless)
{
auto* encoder = (struct encoder_struct_opj*) encoder_raw;
*lossless = (encoder->parameters.irreversible == 0);
return heif_error_ok;
}

Expand Down Expand Up @@ -358,12 +364,10 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_
{
heif_error error;
OPJ_BOOL success;
opj_cparameters_t parameters;
opj_set_default_encoder_parameters(&parameters);

parameters.cp_disto_alloc = 1;
parameters.tcp_numlayers = 1;
parameters.tcp_rates[0] = (float)(1 + (100 - encoder->quality)/2);
encoder->parameters.cp_disto_alloc = 1;
encoder->parameters.tcp_numlayers = 1;
encoder->parameters.tcp_rates[0] = (float)(1 + (100 - encoder->quality)/2);

#if 0
//Insert a human readable comment into the codestream
Expand All @@ -382,7 +386,7 @@ static heif_error generate_codestream(opj_image_t* image, struct encoder_struct_
//OPJ_CODEC_JP2 - Generate the entire jp2 file (which contains a codestream)
OPJ_CODEC_FORMAT codec_format = OPJ_CODEC_J2K;
opj_codec_t* codec = opj_create_compress(codec_format);
success = opj_setup_encoder(codec, &parameters, image);
success = opj_setup_encoder(codec, &(encoder->parameters), image);
if (!success) {
error = {heif_error_Encoding_error, heif_suberror_Unspecified, "Failed to setup OpenJPEG encoder"};
return error;
Expand Down
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ endif()
add_libheif_test(encode)
add_libheif_test(region)

if (WITH_OpenJPEG_ENCODER)
add_libheif_test(encode_jpeg2000)
else()
message(INFO "Disabling JPEG 2000 encoder tests because no JPEG 2000 codec is enabled")
endif()

if (WITH_UNCOMPRESSED_CODEC)
add_libheif_test(uncompressed_decode)
add_libheif_test(uncompressed_encode)
Expand Down
85 changes: 85 additions & 0 deletions tests/encode_jpeg2000.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
libheif unit tests
MIT License
Copyright (c) 2023 Brad Hards <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "libheif/heif.h"
#include "libheif/pixelimage.h"
#include "libheif/api_structs.h"

#include "test_utils.h"

#include <string.h>

static heif_encoding_options * set_encoding_options()
{
heif_encoding_options * options = heif_encoding_options_alloc();
options->macOS_compatibility_workaround = false;
options->macOS_compatibility_workaround_no_nclx_profile = true;
options->image_orientation = heif_orientation_normal;
return options;
}

static void do_encode(heif_image* input_image, const char* filename, bool lossless)
{
REQUIRE(input_image != nullptr);

heif_context *ctx = heif_context_alloc();
heif_encoder *encoder;
struct heif_error err;
err = heif_context_get_encoder_for_format(ctx, heif_compression_JPEG2000, &encoder);
REQUIRE(err.code == heif_error_Ok);

err = heif_encoder_set_lossless(encoder, lossless);
REQUIRE(err.code == heif_error_Ok);

struct heif_encoding_options *options = set_encoding_options();

heif_image_handle *output_image_handle;

err = heif_context_encode_image(ctx, input_image, encoder, options, &output_image_handle);
REQUIRE(err.code == heif_error_Ok);
err = heif_context_write_to_file(ctx, filename);
REQUIRE(err.code == heif_error_Ok);

heif_image_handle_release(output_image_handle);
heif_encoding_options_free(options);
heif_encoder_release(encoder);
heif_image_release(input_image);

heif_context_free(ctx);
}

TEST_CASE("Encode JPEG2000 lossy")
{
heif_image *input_image = createImage_RGB_planar();
do_encode(input_image, "encode_j2k_rgb_lossy.heif", false);
}

TEST_CASE("Encode JPEG2000 lossless")
{
heif_image *input_image = createImage_RGB_planar();
do_encode(input_image, "encode_j2k_rgb_lossless.heif", true);
}
72 changes: 71 additions & 1 deletion tests/test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,74 @@ void fill_new_plane(heif_image* img, heif_channel channel, int w, int h)
for (int y = 0; y < h; y++) {
memset(p + y * stride, 128, w);
}
}
}

struct heif_image * createImage_RGB_planar()
{
struct heif_image *image;
struct heif_error err;
int w = 1024;
int h = 768;
err = heif_image_create(w, h, heif_colorspace_RGB,
heif_chroma_444, &image);
if (err.code) {
return nullptr;
}

err = heif_image_add_plane(image, heif_channel_R, w, h, 8);
REQUIRE(err.code == heif_error_Ok);
err = heif_image_add_plane(image, heif_channel_G, w, h, 8);
REQUIRE(err.code == heif_error_Ok);
err = heif_image_add_plane(image, heif_channel_B, w, h, 8);
REQUIRE(err.code == heif_error_Ok);


int stride;
uint8_t *r = heif_image_get_plane(image, heif_channel_R, &stride);
uint8_t *g = heif_image_get_plane(image, heif_channel_G, &stride);
uint8_t *b = heif_image_get_plane(image, heif_channel_B, &stride);

int y = 0;
for (; y < h / 2; y++) {
int x = 0;
for (; x < w / 3; x++) {
r[y * stride + x] = 1;
g[y * stride + x] = 255;
b[y * stride + x] = 2;
}
for (; x < 2 * w / 3; x++) {
r[y * stride + x] = 4;
g[y * stride + x] = 5;
b[y * stride + x] = 255;
}
for (; x < w; x++) {
r[y * stride + x] = 255;
g[y * stride + x] = 6;
b[y * stride + x] = 7;
}
}
for (; y < h; y++) {
int x = 0;
for (; x < w / 3; x++) {
r[y * stride + x]= 8;
g[y * stride + x] = 9;
b[y * stride + x] = 255;
}
for (; x < 2 * w / 3; x++) {
r[y * stride + x] = 253;
g[y * stride + x] = 10;
b[y * stride + x] = 11;
}
for (; x < w; x++) {
r[y * stride + x] = 13;
g[y * stride + x] = 252;
b[y * stride + x] = 12;
}
}
if (err.code) {
heif_image_release(image);
return nullptr;
}

return image;
}
4 changes: 3 additions & 1 deletion tests/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ struct heif_image_handle * get_primary_image_handle(heif_context *context);

struct heif_image * get_primary_image(heif_image_handle * handle);

void fill_new_plane(heif_image* img, heif_channel channel, int w, int h);
void fill_new_plane(heif_image* img, heif_channel channel, int w, int h);

struct heif_image * createImage_RGB_planar();
72 changes: 0 additions & 72 deletions tests/uncompressed_encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,78 +486,6 @@ struct heif_image *createImage_RGBA_interleaved()
return image;
}


struct heif_image *createImage_RGB_planar()
{
struct heif_image *image;
struct heif_error err;
int w = 1024;
int h = 768;
err = heif_image_create(w, h, heif_colorspace_RGB,
heif_chroma_444, &image);
if (err.code) {
return nullptr;
}

err = heif_image_add_plane(image, heif_channel_R, w, h, 8);
REQUIRE(err.code == heif_error_Ok);
err = heif_image_add_plane(image, heif_channel_G, w, h, 8);
REQUIRE(err.code == heif_error_Ok);
err = heif_image_add_plane(image, heif_channel_B, w, h, 8);
REQUIRE(err.code == heif_error_Ok);


int stride;
uint8_t *r = heif_image_get_plane(image, heif_channel_R, &stride);
uint8_t *g = heif_image_get_plane(image, heif_channel_G, &stride);
uint8_t *b = heif_image_get_plane(image, heif_channel_B, &stride);

int y = 0;
for (; y < h / 2; y++) {
int x = 0;
for (; x < w / 3; x++) {
r[y * stride + x] = 1;
g[y * stride + x] = 255;
b[y * stride + x] = 2;
}
for (; x < 2 * w / 3; x++) {
r[y * stride + x] = 4;
g[y * stride + x] = 5;
b[y * stride + x] = 255;
}
for (; x < w; x++) {
r[y * stride + x] = 255;
g[y * stride + x] = 6;
b[y * stride + x] = 7;
}
}
for (; y < h; y++) {
int x = 0;
for (; x < w / 3; x++) {
r[y * stride + x]= 8;
g[y * stride + x] = 9;
b[y * stride + x] = 255;
}
for (; x < 2 * w / 3; x++) {
r[y * stride + x] = 253;
g[y * stride + x] = 10;
b[y * stride + x] = 11;
}
for (; x < w; x++) {
r[y * stride + x] = 13;
g[y * stride + x] = 252;
b[y * stride + x] = 12;
}
}
if (err.code) {
heif_image_release(image);
return nullptr;
}

return image;
}


struct heif_image *createImage_RGBA_planar()
{
struct heif_image *image;
Expand Down

0 comments on commit f3ed62c

Please sign in to comment.