diff --git a/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift b/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift index 55bcbb0f407..e1ee4372187 100644 --- a/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift +++ b/extension/llm/apple/ExecuTorchLLM/__tests__/MultimodalRunnerTest.swift @@ -9,25 +9,65 @@ import ExecuTorchLLM import XCTest +extension UIImage { + func asImage() -> Image { + let targetWidth = 336 + let scaledHeight = Int((Double(targetWidth) * Double(size.height) / Double(size.width)).rounded()) + 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 resizedCGImage = resizedImage.cgImage! + let imageWidth = resizedCGImage.width + let imageHeight = resizedCGImage.height + let pixelCount = imageWidth * imageHeight + var rgbaBuffer = [UInt8](repeating: 0, count: pixelCount * 4) + let context = CGContext( + data: &rgbaBuffer, + width: imageWidth, + height: imageHeight, + bitsPerComponent: 8, + bytesPerRow: imageWidth * 4, + space: CGColorSpaceCreateDeviceRGB(), + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue + )! + context.draw(resizedCGImage, in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)) + var planarRGB = [UInt8](repeating: 0, count: pixelCount * 3) + for pixelIndex in 0..