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

Merged
merged 4 commits into from
May 6, 2025

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.

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.

I've briefly checked the build and haven't noticed any related issues.

Overall, this PR makes the encoder’s layering and rate logic much more maintainable and predictable — nice work, thank you very much!

@marchcat marchcat merged commit a30342a into secondlife:develop May 6, 2025
11 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 6, 2025
@marchcat marchcat linked an issue May 6, 2025 that may be closed by this pull request
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate #4017 fix and optimize openjpeg J2C encoder
2 participants