From d7f2e2cbb2a3b69a62632e7ad2aa9c3b95f23abb Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Mon, 21 Dec 2020 19:56:55 -0700 Subject: [PATCH] Works --- .../xcschemes/ImageCreation.xcscheme | 85 ++++++++++++++ .../xcdebugger/Breakpoints_v2.xcbkptlist | 6 + .../xcschemes/xcschememanagement.plist | 8 ++ ImageCreation/main.swift | 111 +++++++++++++++++- 4 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 ImageCreation.xcodeproj/xcshareddata/xcschemes/ImageCreation.xcscheme create mode 100644 ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist diff --git a/ImageCreation.xcodeproj/xcshareddata/xcschemes/ImageCreation.xcscheme b/ImageCreation.xcodeproj/xcshareddata/xcschemes/ImageCreation.xcscheme new file mode 100644 index 0000000..7bd5b30 --- /dev/null +++ b/ImageCreation.xcodeproj/xcshareddata/xcschemes/ImageCreation.xcscheme @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..7b8f445 --- /dev/null +++ b/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,6 @@ + + + diff --git a/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcschemes/xcschememanagement.plist b/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcschemes/xcschememanagement.plist index 5cf9496..7e7b5a5 100644 --- a/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/ImageCreation.xcodeproj/xcuserdata/jon.xcuserdatad/xcschemes/xcschememanagement.plist @@ -10,5 +10,13 @@ 0 + SuppressBuildableAutocreation + + FC74E21A2591889F00ACBBFF + + primary + + + diff --git a/ImageCreation/main.swift b/ImageCreation/main.swift index 9cd5955..16377b0 100644 --- a/ImageCreation/main.swift +++ b/ImageCreation/main.swift @@ -1,3 +1,4 @@ + // // main.swift // ImageCreation @@ -7,5 +8,113 @@ import Foundation -print("Hello, World!") +let width = 2048 +let height = width +let bitmapBytesPerRow = Int(width) * 4 +let bitmapByteCount = bitmapBytesPerRow * height + +enum Color { + case red + case green + case blue + case white +} + +func nextColor(_ color: Color) -> Color { + switch color { + case .red: + return .green + case .green: + return .blue + case .blue: + return .white + case .white: + return .red + } +} + +func redVal(_ currentColor: Color) -> UInt8 { + switch currentColor { + case .red: + return 255 + case .green: + return 0 + case .blue: + return 0 + case .white: + return 255 + } +} + +func greenVal(_ currentColor: Color) -> UInt8 { + switch currentColor { + case .red: + return 0 + case .green: + return 255 + case .blue: + return 0 + case .white: + return 255 + } +} + +func blueVal(_ currentColor: Color) -> UInt8 { + switch currentColor { + case .red: + return 0 + case .green: + return 0 + case .blue: + return 255 + case .white: + return 255 + } +} + +func createPixels() -> [UInt8] { + var bitmapData = [UInt8](repeating: 0, count: bitmapByteCount) + + var currentColor = Color.red + + let rectWidth = 256 / 4 + let rectHeight = Int(Double(rectWidth) * 1.5) + + for i in stride(from: 0, through: bitmapByteCount - 4, by: 4) { + bitmapData[i + 0] = redVal(currentColor) + bitmapData[i + 1] = greenVal(currentColor) + bitmapData[i + 2] = blueVal(currentColor) + bitmapData[i + 3] = 255 // alpha + + if i % rectWidth == 0 && (i % (bitmapBytesPerRow * rectHeight)) != 0{ + currentColor = nextColor(currentColor) + } + } + + return bitmapData +} + +func createBitmap() -> CGContext { + + let colorSpace = CGColorSpaceCreateDeviceRGB() + + var bitmapData = createPixels() + let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) + + let context = CGContext.init(data: &bitmapData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bitmapBytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) + + return context! +} + +func buildImage() { + let context = createBitmap() + + let image = context.makeImage() + let url = URL(fileURLWithPath: "/Users/jon/Documents/testImage.png") + let dest = CGImageDestinationCreateWithURL(url as CFURL, kUTTypePNG, 1, nil) + CGImageDestinationAddImage(dest!, image!, nil) + CGImageDestinationFinalize(dest!) +} + +buildImage()