Skip to content

Add SKImageFilter.CreateCrop() - Fixes #3938#4229

Merged
mattleibow merged 1 commit into
mainfrom
dev/issue-3938-imagefilter-crop
Jun 25, 2026
Merged

Add SKImageFilter.CreateCrop() - Fixes #3938#4229
mattleibow merged 1 commit into
mainfrom
dev/issue-3938-imagefilter-crop

Conversation

@mattleibow

@mattleibow mattleibow commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Implements SKImageFilter.CreateCrop(SKRect, SKShaderTileMode, SKImageFilter?) to wrap the modern Skia SkImageFilters::Crop API.

This is the modern replacement for the deprecated cropRect parameter pattern on individual filters. From Skia's header comment:

The optional CropRect argument for many of the factories is equivalent to creating the filter without a CropRect and then wrapping it in ::Crop(rect, kDecal).

Closes #3938

Dependencies

Changes

C API (externals/skia submodule - mono/skia#261)

  • Added sk_imagefilter_new_crop to sk_imagefilter.h
  • Implemented wrapper in sk_imagefilter.cpp

C# Wrapper

  • Added CreateCrop overloads to SKImageFilter.cs:
    • CreateCrop(SKRect) - defaults to Decal mode, null input
    • CreateCrop(SKRect, SKShaderTileMode) - null input
    • CreateCrop(SKRect, SKShaderTileMode, SKImageFilter?) - full control
  • Regenerated SkiaApi.generated.cs

Gallery Sample

Added ImageFilterCropSample.cs demonstrating:

  • Interactive crop rect adjustment (Left, Top, Right, Bottom sliders)
  • All tile modes selectable (Decal, Clamp, Repeat, Mirror)
  • Optional blur composition to show filter chaining
  • Visual crop rect outline for clarity

Tests

Added 6 comprehensive tests in SKImageFilterTest.cs that validate:

  • ✅ API returns non-null for basic usage
  • ✅ All tile modes work (Decal, Clamp, Repeat, Mirror)
  • ✅ Null input handling (uses source image)
  • ✅ Composition with other filters (blur)
  • Pixel-level rendering verification: Validates that pixels outside crop rect remain untouched (white background preserved) and pixels inside crop rect are rendered correctly (red color applied), proving Decal mode transparency behavior works as expected

Testing

All 5712 tests passed:

  • Passed: 5712
  • Skipped: 13
  • Failed: 0

Native build completed successfully on macOS ARM64.

Merge commit message
[api] Add SKImageFilter.CreateCrop() (#4229)

Context: https://github.com/mono/SkiaSharp/issues/3938
Fixes: https://github.com/mono/SkiaSharp/issues/3938
Changes: https://github.com/mono/skia/pull/261

Exposes Skia's `SkImageFilters::Crop()` API, which crops an image filter
output to a rectangle. This is the modern replacement for the deprecated
`cropRect` parameter pattern that existed on individual filter factories.

From Skia's header comment:
> The optional CropRect argument for many of the factories is equivalent
> to creating the filter without a CropRect and then wrapping it in
> ::Crop(rect, kDecal).

The new API allows explicit composition of filters with crop boundaries,
making the intent clearer and enabling more flexible filter chains. For
example, applying a blur then cropping the result is now expressed as:

    var blur = SKImageFilter.CreateBlur(5, 5);
    var cropped = SKImageFilter.CreateCrop(rect, SKShaderTileMode.Decal, blur);

instead of passing cropRect parameters deep into each filter factory.

Changes:
  * C API: added sk_imagefilter_new_crop to externals/skia C shim
  * C# wrapper: added three CreateCrop overloads to SKImageFilter
  * Gallery sample: interactive ImageFilterCropSample with all tile modes
  * Tests: six comprehensive tests including pixel-level rendering validation

All 5712 existing tests continue to pass.

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@github-actions

Copy link
Copy Markdown
Contributor

📦 Try the packages from this PR

Warning

Do not run these scripts without first reviewing the code in this PR.

Step 1 — Download the packages

bash / macOS / Linux:

curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 4229

PowerShell / Windows:

iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 4229"

Step 2 — Add the local NuGet source

dotnet nuget add source ~/.skiasharp/hives/pr-4229/packages --name skiasharp-pr-4229
More options
Option Description
--successful-only / -SuccessfulOnly Only use successful builds
--force / -Force Overwrite previously downloaded packages
--list / -List List available artifacts without downloading
--build-id ID / -BuildId ID Download from a specific build

Or download manually from Azure Pipelines — look for the nuget artifact on the build for this PR.

Remove the source when you're done:

dotnet nuget remove source skiasharp-pr-4229

@github-actions

Copy link
Copy Markdown
Contributor

📖 Documentation Preview

The documentation for this PR has been deployed and is available at:

🔗 View Staging Site
🔗 View Staging Docs
🔗 View Staging Gallery (Blazor)
🔗 View Staging Gallery (Uno Platform)
🔗 View Staging SkiaFiddle

This preview will be updated automatically when you push new commits to this PR.


This comment is automatically updated by the documentation staging workflow.

@mattleibow mattleibow force-pushed the dev/issue-3938-imagefilter-crop branch from bc420ec to 9dd4017 Compare June 24, 2026 22:00
Wraps Skia's modern SkImageFilters::Crop(rect, tileMode, input), the
replacement for the deprecated cropRect parameter pattern on individual
filters. Crop restricts an image filter's output to a rectangle using a
tile mode for the area outside the rect.

Changes:
- C API: sk_imagefilter_new_crop in the skia submodule (mono/skia#261),
  rebased onto main's skia which includes CreateEmpty (#260) + m150 fixes.
- C# wrapper: three CreateCrop overloads (no default parameters) -
  (rect), (rect, tileMode), (rect, tileMode, input) - with GC.KeepAlive.
- Regenerated SkiaApi.generated.cs P/Invoke binding.
- Tests validating non-null, null input, blur composition, pixel-level
  Decal crop rendering, and Decal-vs-Clamp behavioral differences.
- ImageFilterCropSample gallery demo with enable toggle, crop sliders,
  tile mode picker, blur composition, and an aligned crop-rect outline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mattleibow mattleibow force-pushed the dev/issue-3938-imagefilter-crop branch from 9dd4017 to 72904bf Compare June 24, 2026 22:53
@mattleibow mattleibow merged commit a569d69 into main Jun 25, 2026
6 of 7 checks passed
@mattleibow mattleibow deleted the dev/issue-3938-imagefilter-crop branch June 25, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[api] Add SKImageFilter.CreateCrop()

1 participant