Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alyfreym/c7d810241c99588eb5757c390d56dd1b to your computer and use it in GitHub Desktop.
Save alyfreym/c7d810241c99588eb5757c390d56dd1b to your computer and use it in GitHub Desktop.

Revisions

  1. @JoshuaSullivan JoshuaSullivan revised this gist Oct 2, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions in_use.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    guard let colorCubeImage = UIImage(named: "MyColorEffect-64.png") else {
    preconditionFailure("Couldn't load the cube data image.")
    }
    do {
    let data = try ColorCubeHelper.createColorCubeData(inputImage: colorCubeImage, cubeDimension: 64)
    let params: [String : Any] = [ "inputCubeData" : data ]
    let filter = CIFilter(name: "CIColorCube", withInputParameters: params)
    // Now you can use the filter.
    } catch {
    debugPrint("Unable to create cube data: \(error)")
    }
  2. @JoshuaSullivan JoshuaSullivan revised this gist Oct 2, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ColorCubeHelper-swift3.swift
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    import UIKit
    import Accelerate

    public final class ColorCubeHelper {
    public class ColorCubeHelper {

    public enum ColorCubeError: Error {
    case incorrectImageSize
    @@ -17,7 +17,7 @@ public final class ColorCubeHelper {
    case unableToGetBitmpaDataBuffer
    }

    public static func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {
    public static func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> Data {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
    @@ -62,7 +62,7 @@ public final class ColorCubeHelper {
    // Don't copy the bytes, just have the NSData take ownership of the buffer.
    let cubeData = NSData(bytesNoCopy: finalBuffer, length: floatSize, freeWhenDone: true)

    return cubeData
    return cubeData as Data

    }
    }
  3. @JoshuaSullivan JoshuaSullivan revised this gist Oct 2, 2016. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion ColorCubeHelper-swift2.swift
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ public final class ColorCubeHelper {
    case UnableToCreateDataProvider
    }

    public class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {
    public static func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
    2 changes: 1 addition & 1 deletion ColorCubeHelper-swift3.swift
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ public final class ColorCubeHelper {
    case unableToGetBitmpaDataBuffer
    }

    public class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {
    public static func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
  4. @JoshuaSullivan JoshuaSullivan revised this gist Oct 1, 2016. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion ColorCubeHelper-swift2.swift
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    import UIKit
    import Accelerate

    public class ColorCubeHelper {
    public final class ColorCubeHelper {

    public enum ColorCubeError: ErrorType {
    case IncorrectImageSize
    4 changes: 2 additions & 2 deletions ColorCubeHelper-swift3.swift
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    import UIKit
    import Accelerate

    public class ColorCubeHelper {
    public final class ColorCubeHelper {

    public enum ColorCubeError: Error {
    case incorrectImageSize
    @@ -17,7 +17,7 @@ public class ColorCubeHelper {
    case unableToGetBitmpaDataBuffer
    }

    class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {
    public class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
  5. @JoshuaSullivan JoshuaSullivan revised this gist Oct 1, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ColorCubeHelper-swift2.swift
    Original file line number Diff line number Diff line change
    @@ -10,13 +10,13 @@ import Accelerate

    public class ColorCubeHelper {

    enum ColorCubeError: ErrorType {
    public enum ColorCubeError: ErrorType {
    case IncorrectImageSize
    case MissingImageData
    case UnableToCreateDataProvider
    }

    class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {
    public class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
  6. @JoshuaSullivan JoshuaSullivan created this gist Oct 1, 2016.
    65 changes: 65 additions & 0 deletions ColorCubeHelper-swift2.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    //
    // ColorCubeHelper.swift
    //
    // Created by Joshua Sullivan on 10/01/16.
    // Copyright © 2016 Joshua Sullivan. All rights reserved.
    //

    import UIKit
    import Accelerate

    public class ColorCubeHelper {

    enum ColorCubeError: ErrorType {
    case IncorrectImageSize
    case MissingImageData
    case UnableToCreateDataProvider
    }

    class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
    let dim = Int(imageSize.width)
    let pixels = dim * dim
    let channels = 4

    // If the number of pixels doesn't match what's needed for the supplied cube dimension, abort.
    guard pixels == cubeDimension * cubeDimension * cubeDimension else {
    throw ColorCubeError.IncorrectImageSize
    }

    // We don't need a sizeof() because uint_8t is explicitly 1 byte.
    let memSize = pixels * channels

    // Get the UIImage's backing CGImageRef
    guard let img = image.CGImage else {
    throw ColorCubeError.MissingImageData
    }

    // Get a reference to the CGImage's data provider.
    guard let inProvider = CGImageGetDataProvider(img) else {
    throw ColorCubeError.UnableToCreateDataProvider
    }

    let inBitmapData = CGDataProviderCopyData(inProvider)
    let inBuffer = CFDataGetBytePtr(inBitmapData)

    // Calculate the size of the float buffer and allocate it.
    let floatSize = memSize * sizeof(Float.self)
    let finalBuffer = unsafeBitCast(malloc(floatSize), UnsafeMutablePointer<Float>.self)

    // Convert the uint_8t to float. Note: a uint of 255 will convert to 255.0f.
    vDSP_vfltu8(inBuffer, 1, finalBuffer, 1, UInt(memSize))

    // Divide each float by 255.0 to get the 0-1 range we are looking for.
    var divisor = Float(255.0)
    vDSP_vsdiv(finalBuffer, 1, &divisor, finalBuffer, 1, UInt(memSize))

    // Don't copy the bytes, just have the NSData take ownership of the buffer.
    let cubeData = NSData(bytesNoCopy: finalBuffer, length: floatSize, freeWhenDone: true)

    return cubeData

    }
    }
    68 changes: 68 additions & 0 deletions ColorCubeHelper-swift3.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    //
    // ColorCubeHelper.swift
    //
    // Created by Joshua Sullivan on 10/10/16.
    // Copyright © 2016 Joshua Sullivan. All rights reserved.
    //

    import UIKit
    import Accelerate

    public class ColorCubeHelper {

    public enum ColorCubeError: Error {
    case incorrectImageSize
    case missingImageData
    case unableToCreateDataProvider
    case unableToGetBitmpaDataBuffer
    }

    class func createColorCubeData(inputImage image: UIImage, cubeDimension: Int) throws -> NSData {

    // Set up some variables for calculating memory size.
    let imageSize = image.size
    let dim = Int(imageSize.width)
    let pixels = dim * dim
    let channels = 4

    // If the number of pixels doesn't match what's needed for the supplied cube dimension, abort.
    guard pixels == cubeDimension * cubeDimension * cubeDimension else {
    throw ColorCubeError.incorrectImageSize
    }

    // We don't need a sizeof() because uint_8t is explicitly 1 byte.
    let memSize = pixels * channels

    // Get the UIImage's backing CGImageRef
    guard let img = image.cgImage else {
    throw ColorCubeError.missingImageData
    }

    // Get a reference to the CGImage's data provider.
    guard let inProvider = img.dataProvider else {
    throw ColorCubeError.unableToCreateDataProvider
    }

    let inBitmapData = inProvider.data
    guard let inBuffer = CFDataGetBytePtr(inBitmapData) else {
    throw ColorCubeError.unableToGetBitmpaDataBuffer
    }

    // Calculate the size of the float buffer and allocate it.
    let floatSize = memSize * MemoryLayout<Float>.size
    let finalBuffer = unsafeBitCast(malloc(floatSize), to:UnsafeMutablePointer<Float>.self)

    // Convert the uint_8t to float. Note: a uint of 255 will convert to 255.0f.
    vDSP_vfltu8(inBuffer, 1, finalBuffer, 1, UInt(memSize))

    // Divide each float by 255.0 to get the 0-1 range we are looking for.
    var divisor = Float(255.0)
    vDSP_vsdiv(finalBuffer, 1, &divisor, finalBuffer, 1, UInt(memSize))

    // Don't copy the bytes, just have the NSData take ownership of the buffer.
    let cubeData = NSData(bytesNoCopy: finalBuffer, length: floatSize, freeWhenDone: true)

    return cubeData

    }
    }