From 4fbf1abb5799f02eb55983dd6806be720412ee6c Mon Sep 17 00:00:00 2001 From: jared Date: Sat, 24 Jan 2026 12:54:27 -0500 Subject: [PATCH] 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 --- .../Services/InpaintEngine/PatchMatch.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CheapRetouch/Services/InpaintEngine/PatchMatch.swift b/CheapRetouch/Services/InpaintEngine/PatchMatch.swift index 54e88d7..736bb90 100644 --- a/CheapRetouch/Services/InpaintEngine/PatchMatch.swift +++ b/CheapRetouch/Services/InpaintEngine/PatchMatch.swift @@ -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 {