Cotton code
39 lines · 1.9 KB · SwiftRaw
// 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 <screenshot.png> <reference.png> <out.png>import AppKitimport CoreGraphicsimport Foundationimport ImageIOimport UniformTypeIdentifiersfunc 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.argumentsguard args.count == 4 else { fatalError("usage: side-by-side <screenshot> <reference> <out>") }let left = image(args[1]), right = image(args[2])let height = max(left.height, right.height)let gap = 24func 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 = .highcontext.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])