Skip to content

Fixing imread() function 16 bit reading png problem with libspng #27113

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 61 commits into
base: 4.x
Choose a base branch
from

Conversation

sturkmen72
Copy link
Contributor

@sturkmen72 sturkmen72 commented Mar 20, 2025

The purpose of the PR was to load bit-exact compatible results with libspng and libpng. To test this, Imgcodecs_Png_PngSuite was improved. Files containing gamma correction were moved to a separate test called Imgcodecs_Png_PngSuite_Gamma because the logic created for the other files did not apply to those with gamma correction. As a result, libspng now works in bit-exact compatibility with libpng. The code can be refactored later.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@sturkmen72 sturkmen72 force-pushed the spng-CV_16U branch 2 times, most recently from 640563f to bc40042 Compare March 23, 2025 22:20
@asmorkalov asmorkalov self-requested a review March 24, 2025 09:39
@asmorkalov asmorkalov self-assigned this Mar 24, 2025
@asmorkalov asmorkalov added this to the 4.12.0 milestone Mar 24, 2025
@sturkmen72
Copy link
Contributor Author

@asmorkalov the PR is still WIP. no need to start CI yet.

@sturkmen72 sturkmen72 marked this pull request as ready for review April 23, 2025 06:37
@sturkmen72
Copy link
Contributor Author

The purpose of the PR was to load bit-exact compatible results with libspng and libpng. To test this, Imgcodecs_Png_PngSuite was improved. Files containing gamma correction were moved to a separate test called Imgcodecs_Png_PngSuite_Gamma because the logic created for the other files did not apply to those with gamma correction. As a result, libspng now works in bit-exact compatibility with libpng. The code can be refactored later.

@asmorkalov
Copy link
Contributor

I added the motivation to the PR description to save it in Git history after merge.

Comment on lines 679 to 684
#ifdef HAVE_PNG
EXPECT_EQ(19519, img.at<ushort>(0, 0));
#endif
#ifdef HAVE_SPNG
EXPECT_EQ(19517, img.at<ushort>(0, 0));
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds like wrong rounding somewhere in libspng.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

last commit is temporarily to show that the spng conversion code is the same as libpng. i temporarily used if (m_frame_count > 5) that means the difference caused by cvtColor which i used in apng decoding.

[ RUN      ] Imgcodecs_APNG.imread_animation_16u
/build/precommit_linux64/4.x/opencv/modules/imgcodecs/test/test_animation.cpp:680: Failure
Expected equality of these values:
  19519
  img.at<ushort>(0, 0)
    Which is: 19517
[  FAILED  ] Imgcodecs_APNG.imread_animation_16u (2 ms)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you find it appropriate, I can add a to-do note and let's merge this. I'll work on apng and try to find a solution for this difference later.

To show that the spng conversion code is the same as libpng
@asmorkalov
Copy link
Contributor

The last commit looks suspicious.

@sturkmen72
Copy link
Contributor Author

The last commit looks suspicious.

[ RUN      ] Imgcodecs_APNG.imread_animation_16u
/build/precommit_linux64/4.x/opencv/modules/imgcodecs/test/test_animation.cpp:680: Failure
Expected equality of these values:
  19519
  img.at<ushort>(0, 0)
    Which is: 19517
[  FAILED  ] Imgcodecs_APNG.imread_animation_16u (2 ms)

now the value is 19517 same as libspng. this is because the code related apng support which use cvtColor did not worked.

@sturkmen72
Copy link
Contributor Author

in conclusion, I think this PR is correct. I suggest leaving a to-do note about libpng and postponing the solution.


// Uncomment this block to write out the decoded images for visual/manual inspection
// or for regenerating expected ground truth PNGs (for example, after changing decoder logic).
#if 0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I forgot to mention that i used this commented blocks to be sure libspng and libpng loads exact same data. first i set 1 the upper block with OpenCV compiled with libpng. and for each pngsuite file saved what loaded with each flag. then set 0 the upper block and set 1 the lower block with OpenCV compiled with libspng...

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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy