Skip to content

imgcodecs: png: add log if first chunk is not IHDR#27297

Merged
asmorkalov merged 2 commits intoopencv:4.xfrom
Kumataro:fix27295
May 12, 2025
Merged

imgcodecs: png: add log if first chunk is not IHDR#27297
asmorkalov merged 2 commits intoopencv:4.xfrom
Kumataro:fix27295

Conversation

@Kumataro
Copy link
Copy Markdown
Contributor

@Kumataro Kumataro commented May 10, 2025

Close #27295

To optimize for the native pixel format of the iPhone's early PowerVR GPUs, Apple implemented a non-standard PNG format.

Details: https://theapplewiki.com/wiki/PNG_CgBI_Format

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

@asmorkalov asmorkalov requested a review from vrabaud May 10, 2025 14:51
@asmorkalov
Copy link
Copy Markdown
Contributor

cc @sturkmen72

Mat img;

// If first is IHDR chunk, output shall not be empty.
// 8 means PNG sigunature length.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

signature

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review, I fixed this and next line.
(IHDR does not expect the length of chunk data is 4.)

-    // If first is IHDR chunk, output shall not be empty.
-    // 8 means PNG sigunature length.
-    // 4 means lenght of chunk.
+    // If IHDR chunk found as the first chunk, output shall not be empty.
+    // 8 means PNG signature length.
+    // 4 means length field(uint32_t).

// IHDR chunk shall be first. ( https://www.w3.org/TR/png-3/#5ChunkOrdering )
id = read_chunk(m_chunkIHDR);
if (id != id_IHDR)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

chatGPT suggests

if (id == id_CgBI)
{
    CV_LOG_ERROR(NULL, "CgBI chunk (Apple private) found as the first chunk. IHDR is expected.");
}
else
{
    CV_LOG_ERROR(NULL, "IHDR chunk shall be first. This data may be broken or malformed.");
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agreed to the message change.

This fix resulted in a complicated if statement, so I flattened it.

Before

if (id != id_IHDR)
{
    if (id == id_CgBI)
    {
        CV_LOG_ERROR(NULL, "CgBI chunk (Apple private) found as the first chunk. IHDR is expected.");
    }
    else
    {
        CV_LOG_ERROR(NULL, "IHDR chunk shall be first. This data may be broken or malformed.");
    }
    return false;
}

After

if (id == id_CgBI)
{
    CV_LOG_ERROR(NULL, "CgBI chunk (Apple private) found as the first chunk. IHDR is expected.");
    return false;
}
if (id != id_IHDR)
{
    CV_LOG_ERROR(NULL, "IHDR chunk shall be first. This data may be broken or malformed.");
    return false;
}

I have also reflected this correction in the comments of the test code. Thank you very much !

@asmorkalov asmorkalov self-assigned this May 12, 2025
@asmorkalov asmorkalov added this to the 4.12.0 milestone May 12, 2025
@asmorkalov asmorkalov merged commit 3a69b11 into opencv:4.x May 12, 2025
27 of 28 checks passed
@asmorkalov asmorkalov mentioned this pull request May 27, 2025
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.

ios pack 4.11 bring back useful libpng error: CgBI: unhandled critical chunk

3 participants