Skip to content

fix and optimize openjpeg J2C encoder #4017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

chanayane
Copy link
Contributor

@chanayane chanayane commented May 3, 2025

This is a follow up of my previous PR about JPEG2000 encoding with OpenJPEG. #2054

There were a few issues with my previous attempt:

  • parameters.tcp_rates were either wrong or ignored due to the use of max_cs_size, so possibly there were some layers that were over compressed or simply non existant
  • Use of some magical numbers (the 2, 4 and 15 multipliers) to compute rates. I used those numbers to try getting file sizes similar to the ones I got with Kakadu, but I decided to proceed differently with a clearer approach

Here's what I did in this PR:

  • Added a function that returns a number of layers, between 2 to 6, depending on the image surface
  • Added a function that compute and fills parameters.tcp_rates using a geometrical sequence. The file size will now respect the ideal goal of 1:8 compression ratio. Each subsequent layer will have a rate that is 4 times the previous rate.
  • Stopped using max_cs_size. It's not needed if tcp_rates are set correctly
  • Reworked the opj_read, opj_write, opj_seek, opj_skip, opj_free_user_data and opj_free_user_data_write functions with more optimized and safe equivalents
  • Simplified setImage function

After a lot of testing I came very close to the results we get
with Kakadu. Here are some test files for comparison:
test_files.zip

The encoded files now respect the 1:8 ratio and have the correct number of discards.

With many thanks to @TommyTheTerrible for his help.

Copy link
Contributor

@marchcat marchcat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

Please fix the whitespace issues so that it passes the pre-commit check.

Comment on lines +232 to +233
llassert(user_data);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
llassert(user_data);
llassert(user_data);

Comment on lines +272 to +273
parameters->tcp_numlayers = num_layers;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parameters->tcp_numlayers = num_layers;
parameters->tcp_numlayers = num_layers;

Comment on lines +558 to +559
parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0; // no color transform for RGBA images

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0; // no color transform for RGBA images
parameters.tcp_mct = (image->numcomps >= 3) ? 1 : 0; // no color transform for RGBA images

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants