Skip to content

feat(ImageAsset): toNativeImage and toImageSource - #195

Merged
triniwiz merged 1 commit into
masterfrom
feat/image-asset-native-image
Nov 8, 2026
Merged

triniwiz merged 1 commit into
masterfrom
feat/image-asset-native-image

Conversation

@triniwiz

@triniwiz triniwiz commented Nov 8, 2026

Copy link
Copy Markdown
Member

Adds a way to get an ImageAsset's pixels as the platform's own image type, e.g. to show them in an <Image>:

asset.toNativeImageSync(); // Bitmap | UIImage | WriteableBitmap | GdkTexture | null
asset.toNativeImage();     // the same, decoding off the main thread
asset.toImageSourceSync(); // core ImageSource wrapping it
asset.toImageSource();

It works for assets loaded from encoded data, which are decoded for the call, and for raw-pixel assets.

Platform Returns How
Android Bitmap (ARGB_8888) nativeWriteToBitmap writes premultiplied RGBA straight into the locked bitmap
iOS UIImage new C ABI canvas_native_image_asset_read_pixels_8888, wrapped in a CGImage (sRGB, premultiplied)
Windows WriteableBitmap readPixels8888Into writes BGRA straight into its PixelBuffer
Linux GdkTexture pointer (what core's ImageSource.linux holds) a GdkMemoryTexture made on the GTK thread; its reference is dropped once the JS pointer is collected

Underneath, canvas-core gains read_pixels_into / read_pixels_8888_into: one Skia conversion into the requested 8-bit layout.

The async forms keep the asset referenced until native code is done with its pointer. The Android callback holds only an id, like the loads since #192.

Testing

  • Android: run on an emulator. Bitmaps match the canvas's getImageData exactly for an opaque PNG and within 1 (premultiply rounding) for a PNG with alpha, sync and async. A JPEG shows in a core <Image> through toImageSource().
  • iOS: the Swift type-checks against the generated header (swiftc -typecheck, simulator SDK). The xcframework was not rebuilt and nothing was run.
  • Windows and Linux: not built locally. This PR's CI is the first compile.

canvas_native.h gets the new declaration by hand in cbindgen's format (both copies); the iOS pre-build and the Android CMake build regenerate it.

Not checked: whether core's Windows ImageSource accepts a WriteableBitmap. It is a valid XAML image source, but core may assume BitmapImage.

`toNativeImageSync()` / `toNativeImage()` return the asset's pixels as the platform's image:
an ARGB_8888 Bitmap on Android, a UIImage on iOS, a WriteableBitmap on Windows and a GdkTexture
on Linux. `toImageSourceSync()` / `toImageSource()` wrap it in core's ImageSource. The async
forms decode off the main thread; encoded assets are decoded for the call.

- canvas-core: read_pixels_into / read_pixels_8888_into write the pixels in a requested 8-bit
  layout in one Skia conversion.
- Android: nativeWriteToBitmap writes straight into the locked bitmap.
- iOS: canvas_native_image_asset_read_pixels_8888 plus NSCImageAsset.toUIImage(Async).
- Node-API: readPixels8888Into writes into a JS buffer (Windows: the WriteableBitmap's
  PixelBuffer); toGdkTexture(Sync) builds a GdkMemoryTexture on the GTK thread.

Android was checked on an emulator: bitmaps match the canvas's pixels exactly for an opaque PNG
and within 1 for an alpha PNG, sync and async. iOS type-checks against the generated header;
Windows and Linux are not built here.
@coderabbitai

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 787beae6-52bd-474c-8afc-86a2e88954c8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@triniwiz
triniwiz marked this pull request as ready for review November 8, 2026 04:27
@triniwiz
triniwiz merged commit e18c01e into master Nov 8, 2026
10 of 25 checks passed
@triniwiz
triniwiz deleted the feat/image-asset-native-image branch November 8, 2026 04:27
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.

1 participant