-
Notifications
You must be signed in to change notification settings - Fork 2
[codex] Move preview pixmap creation to UI thread #107
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Preview image marshaling helpers for Qt display.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
| import OpenImageIO as oiio | ||
|
|
||
| from renderkit.ui.qt_compat import QImage, QPixmap | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PreviewImageData: | ||
| """Contiguous uint8 preview pixels that can cross the worker/UI boundary.""" | ||
|
|
||
| pixels: np.ndarray | ||
| width: int | ||
| height: int | ||
| channels: int | ||
|
|
||
|
|
||
| def imagebuf_to_preview_image(buf: oiio.ImageBuf) -> PreviewImageData: | ||
| """Convert an ImageBuf into contiguous uint8 RGB/RGBA preview data.""" | ||
| image = buf.get_pixels(oiio.FLOAT) | ||
| if image is None or image.size == 0: | ||
| raise ValueError("Failed to extract preview pixels.") | ||
|
|
||
| spec = buf.spec() | ||
| if image.ndim == 1: | ||
| image = image.reshape((spec.height, spec.width, spec.nchannels)) | ||
|
|
||
| if image.ndim != 3: | ||
| raise ValueError(f"Unsupported preview image dimensions: {image.ndim}") | ||
|
|
||
| height, width, channels = image.shape | ||
| if channels not in (3, 4): | ||
| raise ValueError(f"Unsupported image channels: {channels}") | ||
|
|
||
| if image.dtype != np.uint8: | ||
| image_f32 = image.astype(np.float32, copy=False) | ||
| image = np.clip(image_f32, 0.0, 1.0) | ||
| image = (image * np.float32(255.0)).astype(np.uint8) | ||
|
|
||
| image = np.ascontiguousarray(image) | ||
| return PreviewImageData( | ||
| pixels=image, | ||
| width=width, | ||
| height=height, | ||
| channels=channels, | ||
| ) | ||
|
|
||
|
|
||
| def preview_image_to_qimage(data: PreviewImageData) -> QImage: | ||
| """Create a QImage from preview image data.""" | ||
| q_format = _qimage_format(data.channels) | ||
| bytes_per_line = data.width * data.channels | ||
| image = QImage( | ||
| data.pixels.data, | ||
| data.width, | ||
| data.height, | ||
| bytes_per_line, | ||
| q_format, | ||
| ) | ||
| return image.copy() | ||
|
|
||
|
|
||
| def preview_image_to_pixmap(data: PreviewImageData) -> QPixmap: | ||
| """Create a QPixmap from preview image data.""" | ||
| return QPixmap.fromImage(preview_image_to_qimage(data)) | ||
|
|
||
|
|
||
| def _qimage_format(channels: int) -> Any: | ||
| if channels == 3: | ||
| name = "Format_RGB888" | ||
| elif channels == 4: | ||
| name = "Format_RGBA8888" | ||
| else: | ||
| raise ValueError(f"Unsupported image channels: {channels}") | ||
|
|
||
| enum = getattr(QImage, "Format", None) | ||
| if enum is not None: | ||
| value = getattr(enum, name, None) | ||
| if value is not None: | ||
| return value | ||
|
|
||
| value = getattr(QImage, name, None) | ||
| if value is None: | ||
| raise ValueError(f"Qt image format is unavailable: {name}") | ||
| return value | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 286
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 1508
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 397
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 2491
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 815
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 134
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 830
🏁 Script executed:
Repository: Ahmed-Hindy/renderkit
Length of output: 3431
Add test coverage for 1D array reshape in
imagebuf_to_preview_image().The 1D-to-3D reshape logic (lines 29–30) is not exercised by the existing test suite. Current tests for
imagebuf_to_preview_image()only pass 3D pixel arrays, leaving this code path untested. The team's use of the same reshape pattern in other modules (e.g.,_buf_to_array()in test_color_space.py) confirms this is a real edge case. Add a test case wherebuf.get_pixels()returns a 1D array to ensure the reshape works correctly.🤖 Prompt for AI Agents