forked from khanhuitse05/photo-editor-luts-swiftui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f9712a4
Showing
245 changed files
with
9,326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Xcode | ||
# | ||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore | ||
|
||
## Build generated | ||
build/ | ||
DerivedData/ | ||
|
||
## Various settings | ||
*.pbxuser | ||
!default.pbxuser | ||
*.mode1v3 | ||
!default.mode1v3 | ||
*.mode2v3 | ||
!default.mode2v3 | ||
*.perspectivev3 | ||
!default.perspectivev3 | ||
xcuserdata/ | ||
|
||
## Other | ||
*.moved-aside | ||
*.xccheckout | ||
*.xcscmblueprint | ||
|
||
## Obj-C/Swift specific | ||
*.hmap | ||
*.ipa | ||
*.dSYM.zip | ||
*.dSYM | ||
|
||
## Playgrounds | ||
timeline.xctimeline | ||
playground.xcworkspace | ||
|
||
# Swift Package Manager | ||
# | ||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. | ||
# Packages/ | ||
# Package.pins | ||
# Package.resolved | ||
.build/ | ||
Pods/ | ||
|
||
# CocoaPods | ||
# | ||
# We recommend against adding the Pods directory to your .gitignore. However | ||
# you should judge for yourself, the pros and cons are mentioned at: | ||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control | ||
# | ||
# Pods/ | ||
# | ||
# Add this line if you want to avoid checking in source code from the Xcode workspace | ||
# *.xcworkspace | ||
|
||
# Carthage | ||
# | ||
# Add this line if you want to avoid checking in source code from Carthage dependencies. | ||
# Carthage/Checkouts | ||
|
||
Carthage/Build | ||
|
||
# fastlane | ||
# | ||
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the | ||
# screenshots whenever they are needed. | ||
# For more information about the recommended setup visit: | ||
# https://docs.fastlane.tools/best-practices/source-control/#source-control | ||
|
||
fastlane/report.xml | ||
fastlane/Preview.html | ||
fastlane/screenshots/**/*.png | ||
fastlane/test_output | ||
|
||
# Code Injection | ||
# | ||
# After new code Injection tools there's a generated folder /iOSInjectionProject | ||
# https://github.com/johnno1962/injectionforxcode | ||
|
||
iOSInjectionProject/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// Copyright (c) 2018 Muukii <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import Foundation | ||
import UIKit | ||
import CoreGraphics | ||
|
||
public struct BlurredMask : GraphicsDrawing { | ||
|
||
public var paths: [DrawnPathInRect] | ||
|
||
public init(paths: [DrawnPathInRect]) { | ||
self.paths = paths | ||
} | ||
|
||
public func draw(in context: CGContext, canvasSize: CGSize) { | ||
|
||
guard !paths.isEmpty else { | ||
return | ||
} | ||
|
||
let mainContext = context | ||
let size = canvasSize | ||
|
||
guard | ||
let cglayer = CGLayer(mainContext, size: size, auxiliaryInfo: nil), | ||
let layerContext = cglayer.context else { | ||
assert(false, "Failed to create CGLayer") | ||
return | ||
} | ||
|
||
let ciContext = CIContext(cgContext: layerContext, options: [:]) | ||
let ciImage = BlurredMask.blur(image: CIImage(image: UIGraphicsGetImageFromCurrentImageContext()!)!)! | ||
|
||
UIGraphicsPushContext(layerContext) | ||
|
||
paths.forEach { path in | ||
layerContext.saveGState() | ||
|
||
let scale = Geometry.diagonalRatio(to: canvasSize, from: path.inRect.size) | ||
|
||
layerContext.scaleBy(x: scale, y: scale) | ||
path.draw(in: layerContext, canvasSize: canvasSize) | ||
|
||
layerContext.restoreGState() | ||
} | ||
|
||
layerContext.saveGState() | ||
|
||
layerContext.setBlendMode(.sourceIn) | ||
layerContext.translateBy(x: 0, y: canvasSize.height) | ||
layerContext.scaleBy(x: 1, y: -1) | ||
|
||
ciContext.draw(ciImage, in: ciImage.extent, from: ciImage.extent) | ||
|
||
layerContext.restoreGState() | ||
|
||
UIGraphicsPopContext() | ||
|
||
UIGraphicsPushContext(mainContext) | ||
|
||
mainContext.draw(cglayer, at: .zero) | ||
|
||
UIGraphicsPopContext() | ||
} | ||
|
||
public static func blur(image: CIImage) -> CIImage? { | ||
|
||
func radius(_ imageExtent: CGRect) -> Double { | ||
|
||
let v = Double(sqrt(pow(imageExtent.width, 2) + pow(imageExtent.height, 2))) | ||
return v / 20 // ? | ||
} | ||
|
||
// let min: Double = 0 | ||
let max: Double = 100 | ||
let value: Double = 40 | ||
|
||
let _radius = radius(image.extent) * value / max | ||
|
||
let outputImage = image | ||
.clamped(to: image.extent) | ||
.applyingFilter( | ||
"CIGaussianBlur", | ||
parameters: [ | ||
"inputRadius" : _radius | ||
]) | ||
.cropped(to: image.extent) | ||
|
||
return outputImage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// Copyright (c) 2018 Muukii <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
import Foundation | ||
import CoreGraphics | ||
import CoreImage | ||
import UIKit | ||
|
||
#if os(macOS) | ||
import AppKit | ||
public typealias Image = NSImage | ||
#elseif os(iOS) | ||
import UIKit | ||
public typealias Image = UIImage | ||
#endif | ||
|
||
public enum ColorCube { | ||
|
||
public static func makeColorCubeFilter( | ||
lutImage: Image, | ||
dimension: Int, | ||
colorSpace: CGColorSpace | ||
) -> CIFilter { | ||
|
||
let data = cubeData( | ||
lutImage: lutImage, | ||
dimension: dimension, | ||
colorSpace: colorSpace | ||
)! | ||
|
||
let filter = CIFilter( | ||
name: "CIColorCubeWithColorSpace", | ||
parameters: [ | ||
"inputCubeDimension" : dimension, | ||
"inputCubeData" : data, | ||
"inputColorSpace" : colorSpace, | ||
] | ||
) | ||
|
||
return filter! | ||
} | ||
|
||
private static func createBitmap(image: CGImage, colorSpace: CGColorSpace) -> UnsafeMutablePointer<UInt8>? { | ||
|
||
let width = image.width | ||
let height = image.height | ||
|
||
let bitsPerComponent = 8 | ||
let bytesPerRow = width * 4 | ||
|
||
let bitmapSize = bytesPerRow * height | ||
|
||
guard let data = malloc(bitmapSize) else { | ||
return nil | ||
} | ||
|
||
guard let context = CGContext( | ||
data: data, | ||
width: width, | ||
height: height, | ||
bitsPerComponent: bitsPerComponent, | ||
bytesPerRow: bytesPerRow, | ||
space: colorSpace, | ||
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue, | ||
releaseCallback: nil, | ||
releaseInfo: nil) else { | ||
return nil | ||
} | ||
|
||
context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height)) | ||
|
||
return data.bindMemory(to: UInt8.self, capacity: bitmapSize) | ||
} | ||
|
||
// Imported from Objective-C code. | ||
// TODO: Improve more swifty. | ||
public static func cubeData(lutImage: Image, dimension: Int, colorSpace: CGColorSpace) -> Data? { | ||
|
||
guard let cgImage = lutImage.cgImage else { | ||
return nil | ||
} | ||
|
||
guard let bitmap = createBitmap(image: cgImage, colorSpace: colorSpace) else { | ||
return nil | ||
} | ||
|
||
let width = cgImage.width | ||
let height = cgImage.height | ||
let rowNum = width / dimension | ||
let columnNum = height / dimension | ||
|
||
let dataSize = dimension * dimension * dimension * MemoryLayout<Float>.size * 4 | ||
|
||
var array = Array<Float>(repeating: 0, count: dataSize) | ||
|
||
var bitmapOffest: Int = 0 | ||
var z: Int = 0 | ||
|
||
for _ in stride(from: 0, to: rowNum, by: 1) { | ||
for y in stride(from: 0, to: dimension, by: 1) { | ||
let tmp = z | ||
for _ in stride(from: 0, to: columnNum, by: 1) { | ||
for x in stride(from: 0, to: dimension, by: 1) { | ||
|
||
let dataOffset = (z * dimension * dimension + y * dimension + x) * 4 | ||
|
||
let position = bitmap | ||
.advanced(by: bitmapOffest) | ||
|
||
array[dataOffset + 0] = Float(position | ||
.advanced(by: 0) | ||
.pointee) / 255 | ||
|
||
array[dataOffset + 1] = Float(position | ||
.advanced(by: 1) | ||
.pointee) / 255 | ||
|
||
array[dataOffset + 2] = Float(position | ||
.advanced(by: 2) | ||
.pointee) / 255 | ||
|
||
array[dataOffset + 3] = Float(position | ||
.advanced(by: 3) | ||
.pointee) / 255 | ||
|
||
bitmapOffest += 4 | ||
|
||
} | ||
z += 1 | ||
} | ||
z = tmp | ||
} | ||
z += columnNum | ||
} | ||
|
||
free(bitmap) | ||
|
||
let data = Data.init(bytes: array, count: dataSize) | ||
return data | ||
} | ||
} | ||
|
Oops, something went wrong.