// A simulator screenshot beside the image it is matched against, both scaled // to the same height, on one canvas — the pairs the cardtrial lanes hand the // architect. // // swift scripts/side-by-side.swift import AppKit import CoreGraphics import Foundation import ImageIO import UniformTypeIdentifiers func image(_ path: String) -> CGImage { guard let source = CGImageSourceCreateWithURL(URL(fileURLWithPath: path) as CFURL, nil), let image = CGImageSourceCreateImageAtIndex(source, 0, nil) else { fatalError("cannot read \(path)") } return image } let args = CommandLine.arguments guard args.count == 4 else { fatalError("usage: side-by-side ") } let left = image(args[1]), right = image(args[2]) let height = max(left.height, right.height) let gap = 24 func width(_ image: CGImage) -> Int { Int((Double(image.width) * Double(height) / Double(image.height)).rounded()) } let total = width(left) + gap + width(right) guard let context = CGContext(data: nil, width: total, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { fatalError("no canvas") } context.setFillColor(CGColor(red: 1, green: 1, blue: 1, alpha: 1)) context.fill(CGRect(x: 0, y: 0, width: total, height: height)) context.interpolationQuality = .high context.draw(left, in: CGRect(x: 0, y: 0, width: width(left), height: height)) context.draw(right, in: CGRect(x: width(left) + gap, y: 0, width: width(right), height: height)) guard let out = context.makeImage(), let destination = CGImageDestinationCreateWithURL(URL(fileURLWithPath: args[3]) as CFURL, UTType.png.identifier as CFString, 1, nil) else { fatalError("cannot write \(args[3])") } CGImageDestinationAddImage(destination, out, nil) guard CGImageDestinationFinalize(destination) else { fatalError("cannot write \(args[3])") } print(args[3])