Skip to content

Support JPEG encoding of alpha images on GTK - #4798

Merged
freakboy3742 merged 5 commits into
beeware:mainfrom
kattni:codex/gtk4-images
Nov 11, 2026
Merged

freakboy3742 merged 5 commits into
beeware:mainfrom
kattni:codex/gtk4-images

Conversation

@kattni

@kattni kattni commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Convert GTK images with an alpha channel to RGB before JPEG encoding. Newer GdkPixbuf/Glycin encoders reject RGBA input for JPEG; the conversion preserves RGB values and leaves the source image unchanged. The image-save test now exercises both RGB and RGBA sources.

Found while validating the GTK4 migration in #3069.

Validation: 9 image testbed tests passed on GTK3, GTK4, GTK4/libadwaita, and macOS. Pre-commit, docs lint, and core/Travertino tests passed with 100% coverage.

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

Assisted-by: OpenAI Codex (GPT-6)

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch - I've pushed a couple of cleanups, but otherwise this looks great!

Comment on lines +43 to +44
- On GTK, saving an image as JPEG discards its alpha channel; the original image retains its transparency.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't require a note.

Suggested change
- On GTK, saving an image as JPEG discards its alpha channel; the original image retains its transparency.

Comment thread changes/4798.bugfix.md Outdated
@@ -0,0 +1 @@
Images with an alpha channel could be saved as JPEG files with newer GTK image encoders.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The tense of this change note is wrong:

Suggested change
Images with an alpha channel could be saved as JPEG files with newer GTK image encoders.
On GTK, images with an alpha channel can now be saved as JPEG when GdkPixbuf uses Glycin-based encoders.

Comment thread gtk/src/toga_gtk/images.py Outdated
rgb[y * width * 3 + channel : (y + 1) * width * 3 : 3] = row[
channel::4
]
native = GdkPixbuf.Pixbuf.new_from_bytes(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works, but it's involves direct memory manipulation, which makes me nervous. I'm pretty sure this can be solved with GTK's existing image compositor, mixing the RGBA source image onto a "blank" RGB image, and then saving the composite.

@freakboy3742
freakboy3742 merged commit 315a56a into beeware:main Nov 11, 2026
121 of 122 checks passed
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.

2 participants