Fix Metal texture size mismatch in mask loading

Scale mask and image to texture dimensions when loading into Metal
textures, fixing crash when brush mask has different dimensions
than the source image.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-24 12:54:27 -05:00
parent ab46586ece
commit 4fbf1abb57

View File

@@ -294,8 +294,9 @@ final class PatchMatchInpainter {
// MARK: - Texture Utilities
private func loadCGImage(_ cgImage: CGImage, into texture: MTLTexture) throws {
let width = cgImage.width
let height = cgImage.height
// Use texture dimensions for consistency
let width = texture.width
let height = texture.height
guard let context = CGContext(
data: nil,
@@ -309,6 +310,8 @@ final class PatchMatchInpainter {
throw PatchMatchError.contextCreationFailed
}
// Draw the image scaled to fit the texture dimensions
context.interpolationQuality = .high
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
guard let data = context.data else {
@@ -324,8 +327,9 @@ final class PatchMatchInpainter {
}
private func loadCGImageGrayscale(_ cgImage: CGImage, into texture: MTLTexture) throws {
let width = cgImage.width
let height = cgImage.height
// Use texture dimensions, scaling the mask if needed
let width = texture.width
let height = texture.height
guard let context = CGContext(
data: nil,
@@ -339,6 +343,8 @@ final class PatchMatchInpainter {
throw PatchMatchError.contextCreationFailed
}
// Draw the mask scaled to fit the texture dimensions
context.interpolationQuality = .high
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
guard let data = context.data else {