Repository Public License
1// ORIX OS Automation2// File: Workflows/Samples/batch-image-conversion/make-sample-images.swift3// Description: Regenerates the synthetic sample images of the "Batch image conversion and naming" example with CoreGraphics and ImageIO.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7//8// Usage: swift make-sample-images.swift <output folder> (the committed files were made with: swift make-sample-images.swift Originals)9import CoreGraphics10import Foundation11import ImageIO12import UniformTypeIdentifiers1314let out = URL(fileURLWithPath: CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : "Originals")15try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)1617/// A two-colour gradient with a few rectangles: enough structure to see that a conversion kept the picture.18func picture(width: Int, height: Int, from: [CGFloat], to: [CGFloat], alpha: Bool) -> CGImage {19 let space = CGColorSpace(name: CGColorSpace.sRGB)!20 let info = alpha ? CGImageAlphaInfo.premultipliedLast.rawValue : CGImageAlphaInfo.noneSkipLast.rawValue21 let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: space, bitmapInfo: info)!22 let gradient = CGGradient(colorSpace: space, colorComponents: from + [1] + to + [1], locations: [0, 1], count: 2)!23 ctx.drawLinearGradient(gradient, start: .zero, end: CGPoint(x: width, y: height), options: [])24 ctx.setFillColor(CGColor(srgbRed: 1, green: 1, blue: 1, alpha: 0.8))25 for i in 0..<4 { ctx.fill(CGRect(x: width / 8 + i * width / 5, y: height / 3, width: width / 10, height: height / 3)) }26 if alpha { ctx.clear(CGRect(x: 0, y: 0, width: width / 6, height: height / 6)) }27 return ctx.makeImage()!28}2930func write(_ image: CGImage, _ name: String, _ type: UTType) {31 let dest = CGImageDestinationCreateWithURL(out.appendingPathComponent(name) as CFURL, type.identifier as CFString, 1, nil)!32 CGImageDestinationAddImage(dest, image, [kCGImageDestinationLossyCompressionQuality: 0.8] as CFDictionary)33 guard CGImageDestinationFinalize(dest) else { fatalError("cannot write \(name)") }34 print("wrote \(name)")35}3637write(picture(width: 320, height: 200, from: [0.42, 0.36, 0.91], to: [0.10, 0.60, 0.95], alpha: true), "diagram.png", .png)38write(picture(width: 640, height: 360, from: [0.95, 0.55, 0.10], to: [0.80, 0.10, 0.30], alpha: false), "sunset.jpg", .jpeg)39write(picture(width: 200, height: 200, from: [0.10, 0.70, 0.40], to: [0.05, 0.30, 0.20], alpha: false), "tile.tiff", .tiff)40write(picture(width: 120, height: 80, from: [0.20, 0.20, 0.20], to: [0.90, 0.90, 0.90], alpha: false), "badge.gif", .gif)41write(picture(width: 96, height: 96, from: [0.90, 0.85, 0.20], to: [0.95, 0.40, 0.10], alpha: false), "Icon Large.PNG", .png)42