// The list tile's ground (5 Nov 2026): a Books view tile's worn cream card // with no object on it, made from the card itself — its blank lower half // kept, and mirrored over its upper half where the painted object stood. // Called by `scripts/import-books-materials.py list-ground`. // // swift scripts/list-ground.swift import CoreGraphics import Foundation import ImageIO import UniformTypeIdentifiers let arguments = CommandLine.arguments guard arguments.count == 3, let source = CGImageSourceCreateWithURL(URL(fileURLWithPath: arguments[1]) as CFURL, nil), let tile = CGImageSourceCreateImageAtIndex(source, 0, nil) else { FileHandle.standardError.write(Data("usage: list-ground.swift \n".utf8)); exit(1) } let width = tile.width, height = tile.height, half = height / 2 guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue), let lower = tile.cropping(to: CGRect(x: 0, y: height - half, width: width, height: half)) else { exit(1) } // Core Graphics draws from the bottom: the lower half where it stands, then // the same half turned over in the upper half. context.draw(lower, in: CGRect(x: 0, y: 0, width: width, height: half)) context.saveGState() context.translateBy(x: 0, y: CGFloat(height)) context.scaleBy(x: 1, y: -1) context.draw(lower, in: CGRect(x: 0, y: 0, width: width, height: half)) context.restoreGState() guard let ground = context.makeImage(), let out = CGImageDestinationCreateWithURL(URL(fileURLWithPath: arguments[2]) as CFURL, UTType.png.identifier as CFString, 1, nil) else { exit(1) } CGImageDestinationAddImage(out, ground, nil) exit(CGImageDestinationFinalize(out) ? 0 : 1)