| 1 | import Foundation |
| 2 | import ImageIO |
| 3 | import UniformTypeIdentifiers |
| 4 | import Darwin |
| 5 | import EquipoShareCore |
| 6 | |
| 7 | // Separate processes: fixture generation must not inflate the preparation peak. |
| 8 | let args = CommandLine.arguments |
| 9 | let input = URL(fileURLWithPath: args[2]) |
| 10 | if args[1] == "generate" { |
| 11 | let canvas = CGContext(data: nil, width: 8064, height: 6048, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)! |
| 12 | for y in stride(from: 0, to: 6048, by: 32) { |
| 13 | for x in stride(from: 0, to: 8064, by: 32) { |
| 14 | canvas.setFillColor(CGColor(red: CGFloat((x * 17 + y) % 255) / 255, green: CGFloat((y * 13 + x) % 255) / 255, blue: CGFloat((x + y * 19) % 255) / 255, alpha: 1)) |
| 15 | canvas.fill(CGRect(x: x, y: y, width: 32, height: 32)) |
| 16 | } |
| 17 | } |
| 18 | let destination = CGImageDestinationCreateWithURL(input as CFURL, UTType.heic.identifier as CFString, 1, nil)! |
| 19 | CGImageDestinationAddImage(destination, canvas.makeImage()!, [kCGImagePropertyOrientation: 6, kCGImagePropertyGPSDictionary: [kCGImagePropertyGPSLatitude: 53.3, kCGImagePropertyGPSLongitude: -6.2], kCGImageDestinationLossyCompressionQuality: 0.85] as CFDictionary) |
| 20 | guard CGImageDestinationFinalize(destination) else { fatalError("fixture encoding failed") } |
| 21 | print("Generated 8064 x 6048 HEIC") |
| 22 | } else { |
| 23 | let output = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 24 | defer { try? FileManager.default.removeItem(at: output) } |
| 25 | var files: [FileAttachment] = [] |
| 26 | for _ in 0..<5 { |
| 27 | files.append(try autoreleasepool { try AttachmentFile.prepareFile(input, directory: output) }) |
| 28 | } |
| 29 | var usage = rusage(); getrusage(RUSAGE_SELF, &usage) |
| 30 | print("Prepared \(files.count) images, \(files[0].width) x \(files[0].height); peak RSS \(usage.ru_maxrss) bytes") |
| 31 | guard files.allSatisfy({ max($0.width, $0.height) == 2560 }) else { exit(2) } |
| 32 | guard usage.ru_maxrss < 120 * 1024 * 1024 else { exit(3) } |
| 33 | } |
| 34 | |