Skip to content

Conversation

ankith26
Copy link
Member

These changes are relevant for when SDL_image isn't using the libpng backend (so not really relevant for our release builds, but useful for when we compiled against system SDL_image on some linux distro like fedora)

precursor to #3562 and SDL3 runtime support work considering we don't use libpng backend on those builds as of now.

@ankith26 ankith26 requested a review from a team as a code owner August 28, 2025 04:54
Copy link
Contributor

coderabbitai bot commented Aug 28, 2025

📝 Walkthrough

Walkthrough

Adds a post-load vendor fix to preserve opaque alpha for color-key entries on indexed surfaces, adds an environment-driven skip flag for two PNG save tests, and relaxes a palette equality assertion to compare only the first two entries.

Changes

Cohort / File(s) Summary of Changes
Indexed surface color-key alpha fix
src_c/imageext.c
After loading a surface, if its pixel format is indexed and a color key is set, retrieve the palette, clone the color-key palette entry, set its alpha to SDL_ALPHA_OPAQUE, and write it back via SDL_SetPaletteColors. Guarded by a vendor comment referencing upstream SDL_image PR; applied before wrapping the surface into the Python object.
PNG save test skips with system deps
test/image_test.py
Introduces module flag PG_DEPS_FROM_SYSTEM = "PG_DEPS_FROM_SYSTEM" in os.environ and uses @unittest.skipIf(PG_DEPS_FROM_SYSTEM, ...) to skip testSavePNG24 and testSavePNG8 when system PNG deps are used; updates an existing decorator to use the new flag.
Palette assertion loosening
test/surface_test.py
Adjusts test_image_convert_bug_131 to compare only the first two palette entries (get_palette()[:2]) for both images instead of full palette arrays, with a comment noting backend differences.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Py as image_load_ext
  participant IMG as SDL_image loader
  participant S as SDL_Surface
  Note over Py: Load image into SDL_Surface
  Py->>IMG: IMG_Load(path/bytes)
  IMG-->>Py: SDL_Surface* (S)
  alt Surface format is indexed
    Py->>S: Get color key (SDL_GetSurfaceColorKey / SDL_GetColorKey)
    alt valid color key and palette exists
      Py->>S: Get palette (SDL_GetPaletteColors / similar)
      Note right of Py: Clone palette entry at index, set .a = SDL_ALPHA_OPAQUE
      Py->>S: SDL_SetPaletteColors(updated entry)
    else no valid color key or no palette
      Note over Py: No palette alpha fix
    end
  else Non-indexed format
    Note over Py: No post-fix applied
  end
  Py-->>Py: Wrap SDL_Surface into Python object
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

sdl3

Suggested reviewers

  • MyreMylar

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between b46fa7d and 570bd98.

📒 Files selected for processing (3)
  • src_c/imageext.c (1 hunks)
  • test/image_test.py (4 hunks)
  • test/surface_test.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • test/surface_test.py
  • src_c/imageext.c
  • test/image_test.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: arm64
  • GitHub Check: aarch64
  • GitHub Check: x86_64
  • GitHub Check: i686
  • GitHub Check: x86
  • GitHub Check: AMD64
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: Debian (Bookworm - 12) [s390x]
  • GitHub Check: Debian (Bookworm - 12) [armv7]
  • GitHub Check: Debian (Bookworm - 12) [ppc64le]
  • GitHub Check: dev-check
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.13.5)
  • GitHub Check: build (macos-14)
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.14.0rc1)
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.9.23)
  • GitHub Check: build (ubuntu-22.04)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ankith26-png-fixes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
test/surface_test.py (1)

1231-1235: LGTM; reduce brittleness across PNG backends

Relaxing to the first two palette entries makes this test portable with non-libpng loaders. Consider adding a brief comment to document the rationale.

Apply this diff to add clarity:

             im2 = pygame.image.load(example_path(os.path.join("data", "brick.png")))
-
-            self.assertEqual(
+            # Only validate the first two entries; remaining palette entries can
+            # vary across PNG backends (e.g., system SDL_image vs. libpng).
+            self.assertEqual(
                 im.get_palette()[:2], ((0, 0, 0, 255), (255, 255, 255, 255))
             )
             self.assertEqual(im2.get_palette()[:2], ((0, 0, 0, 255), (0, 0, 0, 255)))
test/image_test.py (2)

206-210: LGTM; skip PNG24 save test with system deps to avoid non-libpng discrepancies

This aligns the suite with environments where SDL_image may use a different PNG backend.

Optional: introduce a module-level flag to avoid repeating the env check:

@@
 _sdl_image_ver = pygame.image.get_sdl_image_version()
@@
 sdl_image_svg_jpeg_save_bug = (
     _sdl_image_ver <= (2, 0, 5) and pygame.get_sdl_byteorder() == pygame.BIG_ENDIAN
 )
+USING_SYSTEM_DEPS = "PG_DEPS_FROM_SYSTEM" in os.environ
@@
-@unittest.skipIf(
-    "PG_DEPS_FROM_SYSTEM" in os.environ,
+@unittest.skipIf(
+    USING_SYSTEM_DEPS,
     "If we are using system dependencies, we don't know the backend used "
     "for PNG saving, and this test only works with libpng.",
 )

For changes outside this hunk (adding USING_SYSTEM_DEPS), insert:

USING_SYSTEM_DEPS = "PG_DEPS_FROM_SYSTEM" in os.environ

near the other module-level feature flags.


246-250: LGTM; skip PNG8 save test with system deps for the same reason

Consistent with PNG24 handling and existing PaletteAsPNG8 skip below.

If you add USING_SYSTEM_DEPS as suggested above, mirror it here:

-@unittest.skipIf(
-    "PG_DEPS_FROM_SYSTEM" in os.environ,
+@unittest.skipIf(
+    USING_SYSTEM_DEPS,
     "If we are using system dependencies, we don't know the backend used "
     "for PNG saving, and this test only works with libpng.",
 )
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 0940fb6 and b46fa7d.

📒 Files selected for processing (3)
  • src_c/imageext.c (1 hunks)
  • test/image_test.py (2 hunks)
  • test/surface_test.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src_c/imageext.c (1)
src_c/_pygame.h (1)
  • PG_GetSurfacePalette (249-253)
test/surface_test.py (1)
buildconfig/stubs/pygame/surface.pyi (1)
  • get_palette (588-598)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: x86_64
  • GitHub Check: arm64
  • GitHub Check: Debian (Bookworm - 12) [s390x]
  • GitHub Check: Debian (Bookworm - 12) [armv7]
  • GitHub Check: Debian (Bookworm - 12) [ppc64le]
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.9.23)
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.14.0rc1)
  • GitHub Check: i686
  • GitHub Check: x86_64
  • GitHub Check: aarch64
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: AMD64
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.13.5)
  • GitHub Check: x86
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: build (ubuntu-22.04)
  • GitHub Check: dev-check

@ankith26 ankith26 force-pushed the ankith26-png-fixes branch from b46fa7d to 570bd98 Compare August 28, 2025 08:15
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

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

I haven't tested or anything, but the code makes sense.

@ankith26 ankith26 added this to the 2.5.6 milestone Aug 31, 2025
@ankith26 ankith26 merged commit c7e9c15 into main Aug 31, 2025
28 checks passed
@ankith26 ankith26 deleted the ankith26-png-fixes branch August 31, 2025 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants