diff --git a/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift b/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift index 5176e193ab8..b3de9b07a9d 100644 --- a/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift +++ b/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift @@ -11,16 +11,26 @@ import XCTest extension UIImage { func asImage() -> Image { - let targetWidth = 336 - let scaledHeight = Int((Double(targetWidth) * Double(size.height) / Double(size.width)).rounded()) + let targetSide = CGFloat(336) + let scale = max(targetSide / size.width, targetSide / size.height) + let scaledSize = CGSize(width: size.width * scale, height: size.height * scale) let format = UIGraphicsImageRendererFormat.default() format.scale = 1 - let resizedImage = UIGraphicsImageRenderer(size: CGSize(width: targetWidth, height: scaledHeight), format: format).image { _ in - draw(in: CGRect(origin: .zero, size: CGSize(width: targetWidth, height: scaledHeight))) + let scaledImage = UIGraphicsImageRenderer(size: scaledSize, format: format).image { _ in + draw(in: CGRect(origin: .zero, size: scaledSize)) } - let resizedCGImage = resizedImage.cgImage! - let imageWidth = resizedCGImage.width - let imageHeight = resizedCGImage.height + guard let scaledCGImage = scaledImage.cgImage else { + return Image(data: Data(), width: 336, height: 336, channels: 3) + } + let cropRect = CGRect( + x: ((scaledSize.width - targetSide) * 0.5).rounded(.down), + y: ((scaledSize.height - targetSide) * 0.5).rounded(.down), + width: targetSide.rounded(.down), + height: targetSide.rounded(.down) + ) + let croppedCGImage = scaledCGImage.cropping(to: cropRect) ?? scaledCGImage + let imageWidth = croppedCGImage.width + let imageHeight = croppedCGImage.height let pixelCount = imageWidth * imageHeight var rgbaBuffer = [UInt8](repeating: 0, count: pixelCount * 4) let context = CGContext( @@ -32,7 +42,7 @@ extension UIImage { space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue )! - context.draw(resizedCGImage, in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)) + context.draw(croppedCGImage, in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)) var planarRGB = [UInt8](repeating: 0, count: pixelCount * 3) for pixelIndex in 0..