├── .gitignore ├── IdentityCLUTImageGenerator.playground ├── Contents.swift ├── Sources │ └── Generator.swift └── contents.xcplayground ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | .DS_Store 6 | 7 | ## Build generated 8 | build/ 9 | DerivedData/ 10 | 11 | ## Various settings 12 | *.pbxuser 13 | !default.pbxuser 14 | *.mode1v3 15 | !default.mode1v3 16 | *.mode2v3 17 | !default.mode2v3 18 | *.perspectivev3 19 | !default.perspectivev3 20 | xcuserdata/ 21 | 22 | ## Other 23 | *.moved-aside 24 | *.xccheckout 25 | *.xcscmblueprint 26 | 27 | ## Obj-C/Swift specific 28 | *.hmap 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | .build/ 44 | 45 | # CocoaPods 46 | # 47 | # We recommend against adding the Pods directory to your .gitignore. However 48 | # you should judge for yourself, the pros and cons are mentioned at: 49 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 50 | # 51 | # Pods/ 52 | 53 | # Carthage 54 | # 55 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 56 | # Carthage/Checkouts 57 | 58 | Carthage/Build 59 | 60 | # fastlane 61 | # 62 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 63 | # screenshots whenever they are needed. 64 | # For more information about the recommended setup visit: 65 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 66 | 67 | fastlane/report.xml 68 | fastlane/Preview.html 69 | fastlane/screenshots/**/*.png 70 | fastlane/test_output 71 | -------------------------------------------------------------------------------- /IdentityCLUTImageGenerator.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | 2 | let descriptor = CLUTImageDescriptor(dimension: 16, layout: CLUTImageLayout(horizontalTileCount: 16, verticalTileCount: 1)) 3 | 4 | if let image = IdentityCLUTImageGenerator.generateIdentityCLUTImage(with: descriptor) { 5 | save(image: image, title: descriptor.title) 6 | } 7 | 8 | /* 9 | Utility Function 10 | Save image to "~/Downloads/" folder with title. 11 | */ 12 | 13 | import CoreImage 14 | 15 | func save(image: CGImage, title: String) { 16 | let context = CIContext(options: nil) 17 | let ciImage = CIImage(cgImage: image) 18 | if let pngData = context.pngRepresentation(of: ciImage, format: CIFormat.BGRA8, colorSpace: CGColorSpaceCreateDeviceRGB(), options: [:]) { 19 | do { 20 | try pngData.write(to: URL(fileURLWithPath: "\(NSHomeDirectory())/Downloads/\(title).png")) 21 | print("Done") 22 | } catch { 23 | print("Error: \(error)") 24 | } 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /IdentityCLUTImageGenerator.playground/Sources/Generator.swift: -------------------------------------------------------------------------------- 1 | import CoreGraphics 2 | 3 | public struct CLUTImageLayout { 4 | public var horizontalTileCount: UInt 5 | public var verticalTileCount: UInt 6 | public init(horizontalTileCount: UInt, verticalTileCount: UInt) { 7 | self.verticalTileCount = verticalTileCount 8 | self.horizontalTileCount = horizontalTileCount 9 | } 10 | } 11 | 12 | public struct CLUTImageDescriptor { 13 | public var dimension: UInt 14 | public var layout: CLUTImageLayout 15 | 16 | public var isValid: Bool { 17 | return dimension > 0 && dimension <= 256 && (layout.horizontalTileCount * layout.verticalTileCount == dimension) 18 | } 19 | 20 | public var title: String { 21 | return "CLUT_D\(self.dimension)_\(self.layout.horizontalTileCount * self.dimension)x\(self.layout.verticalTileCount * self.dimension)" 22 | } 23 | 24 | public static let squareD16: CLUTImageDescriptor = CLUTImageDescriptor(dimension: 16, layout: CLUTImageLayout(horizontalTileCount: 4, verticalTileCount: 4)) 25 | 26 | public static let squareD64: CLUTImageDescriptor = CLUTImageDescriptor(dimension: 64, layout: CLUTImageLayout(horizontalTileCount: 8, verticalTileCount: 8)) 27 | 28 | public static let squareD256: CLUTImageDescriptor = CLUTImageDescriptor(dimension: 256, layout: CLUTImageLayout(horizontalTileCount: 16, verticalTileCount: 16)) 29 | 30 | public init(dimension: UInt, layout: CLUTImageLayout) { 31 | self.dimension = dimension 32 | self.layout = layout 33 | } 34 | } 35 | 36 | public struct IdentityCLUTImageGenerator { 37 | public static func generateIdentityCLUTImage(with descriptor: CLUTImageDescriptor) -> CGImage? { 38 | assert(descriptor.isValid) 39 | guard descriptor.isValid else { 40 | return nil 41 | } 42 | 43 | let imageWidth = Int(descriptor.dimension * descriptor.layout.horizontalTileCount) 44 | let imageHeight = Int(descriptor.dimension * descriptor.layout.verticalTileCount) 45 | 46 | let bytesPerPixel = 4 47 | let bytesPerRow = imageWidth * bytesPerPixel 48 | 49 | guard let buffer = malloc(imageHeight * bytesPerRow) else { 50 | return nil 51 | } 52 | 53 | struct Pixel { 54 | var b: UInt8 55 | var g: UInt8 56 | var r: UInt8 57 | var a: UInt8 58 | } 59 | 60 | precondition(MemoryLayout.size == 4) 61 | 62 | let pixels = buffer.assumingMemoryBound(to: Pixel.self) 63 | for y in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Yu Ao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # IdentityCLUTImageGenerator 3 | 4 | A lookup table is a cube of color values: the red, green, and blue components of an input color map to the x, y, and z coordinates of a location in that cube, and at that location in the cube is a corresponding output color. 5 | 6 | The 2D representation of a 3D color cube is an arrangement of slices. 7 | 8 | For example, a 16 x 16 x 16 color cube becomes a horizontal strip of 16 squares, each 16 x 16 pixels (that is, a 256 x 16 image). Each square contains a gradation of red and green components, and together the 16 squares form a gradation for the blue component. 9 | 10 | `IdentityCLUTImageGenerator.playground` generates 2D identity color lookup table images that can be use with image processing frameworks like [MetalPetal](https://github.com/MetalPetal/MetalPetal/), [GPUImage](https://github.com/BradLarson/GPUImage), [SceneKit](https://developer.apple.com/documentation/scenekit/scncamera/1644114-colorgrading), etc. 11 | 12 | ## Types 13 | 14 | ### CLUTImageDescriptor 15 | 16 | **dimension**: Dimension of the color lookup table. [0~256] 17 | 18 | **layout**: Layout of the color lookup table image. 19 | 20 | ### CLUTImageLayout 21 | 22 | **horizontalTileCount**: Horizontal blocks of the color lookup table image. 23 | 24 | **verticalTileCount**: Vertical blocks of the color lookup table image. 25 | 26 | `horizontalTileCount * verticalTileCount` should be equal to the `CLUTImageDescriptor`'s `dimension`. 27 | 28 | ## Examples 29 | 30 | - The identity 16x16x16 horizontal-strip-layout color lookup table image used by `SCNCamera.colorGrading` can be generated using: 31 | 32 | CLUTImageDescriptor(dimension: 16, layout: CLUTImageLayout(horizontalTileCount: 16, verticalTileCount: 1)) 33 | 34 | - The identity 64x64x64 square-layout color lookup table image used by `GPUImage` can be generated using: 35 | 36 | CLUTImageDescriptor(dimension: 64, layout: CLUTImageLayout(horizontalTileCount: 8, verticalTileCount: 8)) 37 | 38 | 39 | 40 | --------------------------------------------------------------------------------