pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

pygame.transform.pixelate

Open oddbookworm opened this issue 2 years ago • 5 comments
trafficstars

Addressing #2302

TODO:

  • [x] tests

oddbookworm avatar Jul 27 '23 03:07 oddbookworm

Could this effect be replicated with a smoothscale down and a scale up? It could even do that in C code, and it might be faster because those are SIMD optimized?

Initial testing is promising that it replicates the effect, I didn't test performance.

Starbuck5 avatar Oct 15 '23 04:10 Starbuck5

Could this effect be replicated with a smoothscale down and a scale up? It could even do that in C code, and it might be faster because those are SIMD optimized?

Initial testing is promising that it replicates the effect, I didn't test performance.

Shouldn't even need smoothscale for this, a bit of math with pixel_size versus the smallest dimension of the original image then two calls to SDL_SoftStretch should probably do it I'd have thought? Unless smoothscale is now faster than SDL_SoftStretch after recent SIMD-ifying?

Would probably be worth a look at least to see if it is faster than this current method.

MyreMylar avatar Oct 15 '23 10:10 MyreMylar

I've converted this to a draft due to large open questions about how the implementation should work.

Starbuck5 avatar Nov 19 '23 10:11 Starbuck5

I'll revive this soon, I promise

oddbookworm avatar Nov 26 '23 20:11 oddbookworm

How about we implement this by way of generalizing the blur transforms by adding a stride argument to them? Then we could implement this as a blur with stride equal to the blur radius. Then we'd also have the stride argument for blurs and pixelate could be like a convenience function that simply calls one of the blur functions with stride equal to radius, though I suppose it'd be called pixel size or something.

Matiiss avatar Mar 15 '25 01:03 Matiiss

Here's a test script you can use btw, I'm just running it from pygame-ce's root repo so the image is readily available

import pygame

pygame.init()

win = pygame.Window()
screen = win.get_surface()

surf = pygame.image.load("examples/data/alien1.png").convert_alpha()
surf = pygame.transform.scale(surf, screen.get_size())
pixel_size = 1

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEWHEEL:
            pixel_size = max(1, min(pixel_size + event.y, min(*surf.get_size())))

    screen.fill("black")
    screen.blit(pygame.transform.pixelate(surf, pixel_size))
    win.flip()

oddbookworm avatar Jun 25 '25 23:06 oddbookworm

@ankith26 comments fixed

oddbookworm avatar Jun 25 '25 23:06 oddbookworm

Re-requesting @MyreMylar review because of how ancient it is lol

oddbookworm avatar Jun 26 '25 00:06 oddbookworm

[!WARNING]

Rate limit exceeded

@MyreMylar has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 0 minutes and 37 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between dbb504a932f174fb1ecf8b79bd5a50377954a96f and 9ee2c8c63b65c792b253cd626f4f2f9ee08174a9.

📒 Files selected for processing (2)
  • buildconfig/stubs/pygame/transform.pyi (1 hunks)
  • src_c/transform.c (3 hunks)

Walkthrough

Introduces a new pygame.transform API pixelate(surface, pixel_size, dest_surface=None) with a C implementation performing downscale-then-upscale. Adds corresponding documentation macro and type stub, new tests for behavior and errors, and updates build-system dependencies by adding astroid<=3.3.8.

Changes

Cohort / File(s) Summary
Transform API & Implementation
src_c/transform.c, src_c/doc/transform_doc.h, buildconfig/stubs/pygame/transform.pyi
Add pixelate() public API: C function surf_pixelate with PyMethodDef entry, doc macro DOC_TRANSFORM_PIXELATE, and stub declaration with docstring.
Tests
test/transform_test.py
Add helper surfaces_have_same_pixels and test_pixelate validating identity at size=1, full block effect at large size, and error handling (ValueError, OverflowError, TypeError).
Build System
pyproject.toml
Append build requirement astroid<=3.3.8 under [build-system].

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant pygame.transform
  participant surf_pixelate
  participant scale_to

  User->>pygame.transform: pixelate(surface, pixel_size, dest_surface?)
  pygame.transform->>surf_pixelate: dispatch
  surf_pixelate->>surf_pixelate: validate args
  surf_pixelate->>scale_to: downscale by pixel_size
  scale_to-->>surf_pixelate: intermediate surface
  surf_pixelate->>scale_to: upscale to original size (into dest or new)
  scale_to-->>surf_pixelate: pixelated surface
  surf_pixelate-->>User: return dest or new Surface

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I hop through pixels, blocky and bright,
Shrinking, then stretching—chunky delight. 🧩
A tap of transform, a bump of a byte,
Tests nibble errors, all set right.
New build snacks added—what a sight!
Pixel-party tonight—boop! 🐇✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings
🧪 Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

🪧 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 @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai 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:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai 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 @coderabbitai help to get the list of available commands.

Other keywords and placeholders

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

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

coderabbitai[bot] avatar Aug 17 '25 14:08 coderabbitai[bot]