├── .gitignore ├── 01_MakeImageLibrary.playground ├── Contents.swift ├── Resources │ └── monet.png ├── Sources │ └── RGBAImage.swift └── contents.xcplayground ├── 02_PixelOperation.playground ├── Contents.swift ├── Resources │ └── monet.png ├── Sources │ └── RGBAImage.swift └── contents.xcplayground ├── 03_SplitRGBColorSpace.playground ├── Contents.swift ├── Resources │ └── monet.png ├── Sources │ └── RGBAImage.swift └── contents.xcplayground ├── 04_RGBtoGray.playground ├── Contents.swift ├── Resources │ └── monet.png ├── Sources │ └── RGBAImage.swift └── contents.xcplayground ├── 05_ImageOperators.playground ├── Contents.swift ├── Resources │ └── monet.png ├── Sources │ ├── ImageProcess.swift │ └── RGBAImage.swift └── contents.xcplayground ├── 06_SplitRGBColorSpace_2.playground ├── Contents.swift ├── Resources │ └── monet.png ├── Sources │ ├── ByteImage.swift │ ├── ImageProcess.swift │ └── RGBAImage.swift └── contents.xcplayground ├── 07_Add_Sub_Mult_Div.playground ├── Contents.swift ├── Resources │ ├── monet1.png │ ├── monet2.png │ └── tiger.png ├── Sources │ ├── ByteImage.swift │ ├── ImageProcess.swift │ └── RGBAImage.swift └── contents.xcplayground ├── 08_Blending.playground ├── Contents.swift ├── Resources │ ├── monet1.png │ ├── monet2.png │ └── tiger.png ├── Sources │ ├── ByteImage.swift │ ├── ImageProcess.swift │ └── RGBAImage.swift └── contents.xcplayground ├── 09_Brightness.playground ├── Contents.swift ├── Resources │ ├── monet1.png │ ├── monet2.png │ └── tiger.png ├── Sources │ ├── Array2D.swift │ ├── ByteImage.swift │ ├── ImageProcess.swift │ └── RGBAImage.swift └── contents.xcplayground ├── 10_Convolution.playground ├── Contents.swift ├── Resources │ ├── monet1.png │ ├── monet2.png │ └── tiger.png ├── Sources │ ├── Array2D.swift │ ├── ByteImage.swift │ ├── ImageProcess.swift │ └── RGBAImage.swift └── contents.xcplayground ├── LICENSE ├── README.md ├── art ├── 01_rgba_image.png ├── 02_contrast.png ├── 03_grab_r.png ├── 04_grab_g.png ├── 05_grab_b.png ├── 06_composite.png ├── 07_rgb_to_gray.png ├── 08_split.png ├── 09_add_sub_mul_div.png ├── 10_add_sub_mul_div.png ├── 11_blending.png ├── 12_brightness.png ├── 13_sharpening.png └── 14_blur.png ├── monet1.png ├── monet2.png └── tiger.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | ## Playgrounds 31 | timeline.xctimeline 32 | playground.xcworkspace 33 | 34 | # Swift Package Manager 35 | # 36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 37 | # Packages/ 38 | .build/ 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | # fastlane 56 | # 57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 58 | # screenshots whenever they are needed. 59 | # For more information about the recommended setup visit: 60 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 61 | 62 | fastlane/report.xml 63 | fastlane/screenshots 64 | -------------------------------------------------------------------------------- /01_MakeImageLibrary.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | 6 | let rgba = RGBAImage(image: UIImage(named: "monet")!)! 7 | let newImage = rgba.toUIImage() 8 | 9 | 10 | -------------------------------------------------------------------------------- /01_MakeImageLibrary.playground/Resources/monet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/01_MakeImageLibrary.playground/Resources/monet.png -------------------------------------------------------------------------------- /01_MakeImageLibrary.playground/Sources/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public struct Pixel { 4 | 5 | //각 색상 컴포넌트는 ABGR 순으로 저장되어 있다. 6 | public var value: UInt32 7 | 8 | //red 9 | public var R: UInt8 { 10 | get { return UInt8(value & 0xFF); } 11 | set { value = UInt32(newValue) | (value & 0xFFFFFF00) } 12 | } 13 | 14 | //green 15 | public var G: UInt8 { 16 | get { return UInt8((value >> 8) & 0xFF) } 17 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 18 | } 19 | 20 | //blue 21 | public var B: UInt8 { 22 | get { return UInt8((value >> 16) & 0xFF) } 23 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 24 | } 25 | 26 | //alpha 27 | public var A: UInt8 { 28 | get { return UInt8((value >> 24) & 0xFF) } 29 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 30 | } 31 | } 32 | 33 | public struct RGBAImage { 34 | class WrappedPixels { 35 | public var pixels: UnsafeMutableBufferPointer 36 | init() { 37 | pixels = UnsafeMutableBufferPointer(start: nil, count: 0) 38 | } 39 | deinit { 40 | pixels.deallocate() 41 | } 42 | } 43 | let wrappedPixels = WrappedPixels() 44 | public var width: Int 45 | public var height: Int 46 | 47 | public init?(image: UIImage) { 48 | // CGImage로 변환이 가능해야 한다. 49 | guard let cgImage = image.cgImage else { 50 | return nil 51 | } 52 | 53 | // 주소 계산을 위해서 Float을 Int로 저장한다. 54 | width = Int(image.size.width) 55 | height = Int(image.size.height) 56 | 57 | // 4 * width * height 크기의 버퍼를 생성한다. 58 | let bytesPerRow = width * 4 59 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 60 | //if we don't initialize it, the toUIImage() method will generate extra color. 61 | imageData.initialize(repeating: Pixel(value: 0), count: width*height) 62 | 63 | // 색상공간은 Device의 것을 따른다 64 | let colorSpace = CGColorSpaceCreateDeviceRGB() 65 | 66 | // BGRA로 비트맵을 만든다 67 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 68 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 69 | 70 | // 비트맵 생성 71 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 72 | return nil 73 | } 74 | 75 | // cgImage를 imageData에 채운다. 76 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 77 | 78 | wrappedPixels.pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 79 | } 80 | 81 | public func toUIImage() -> UIImage? { 82 | let colorSpace = CGColorSpaceCreateDeviceRGB() 83 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 84 | let bytesPerRow = width * 4 85 | 86 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 87 | 88 | guard let imageContext = CGContext(data: wrappedPixels.pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 89 | return nil 90 | } 91 | 92 | guard let cgImage = imageContext.makeImage() else { 93 | return nil 94 | } 95 | 96 | let image = UIImage(cgImage: cgImage) 97 | return image 98 | } 99 | } 100 | 101 | -------------------------------------------------------------------------------- /01_MakeImageLibrary.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /02_PixelOperation.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | 6 | let rgba = RGBAImage(image: UIImage(named: "monet")!)! 7 | 8 | 9 | var totalR = 0 10 | var totalG = 0 11 | var totalB = 0 12 | 13 | rgba.process { (pixel) -> Pixel in 14 | totalR += Int(pixel.R) 15 | totalG += Int(pixel.G) 16 | totalB += Int(pixel.B) 17 | return pixel 18 | } 19 | 20 | let pixelCount = rgba.width * rgba.height 21 | let avgR = totalR / pixelCount 22 | let avgG = totalG / pixelCount 23 | let avgB = totalB / pixelCount 24 | 25 | 26 | 27 | func contrast(_ image: RGBAImage) -> RGBAImage { 28 | 29 | image.process { (pixel) -> Pixel in 30 | var pixel = pixel 31 | let deltaR = Int(pixel.R) - avgR 32 | let deltaG = Int(pixel.G) - avgG 33 | let deltaB = Int(pixel.B) - avgB 34 | pixel.R = UInt8(max(min(255, avgR + 3 * deltaR), 0)) //clamp 35 | pixel.G = UInt8(max(min(255, avgG + 3 * deltaG), 0)) 36 | pixel.B = UInt8(max(min(255, avgB + 3 * deltaB), 0)) 37 | 38 | return pixel 39 | } 40 | return image 41 | } 42 | 43 | let newImage = contrast(rgba).toUIImage() 44 | 45 | 46 | -------------------------------------------------------------------------------- /02_PixelOperation.playground/Resources/monet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/02_PixelOperation.playground/Resources/monet.png -------------------------------------------------------------------------------- /02_PixelOperation.playground/Sources/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public struct Pixel { 4 | public var value: UInt32 5 | 6 | //red 7 | public var R: UInt8 { 8 | get { return UInt8(value & 0xFF); } 9 | set { value = UInt32(newValue) | (value & 0xFFFFFF00) } 10 | } 11 | 12 | //green 13 | public var G: UInt8 { 14 | get { return UInt8((value >> 8) & 0xFF) } 15 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 16 | } 17 | 18 | //blue 19 | public var B: UInt8 { 20 | get { return UInt8((value >> 16) & 0xFF) } 21 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 22 | } 23 | 24 | //alpha 25 | public var A: UInt8 { 26 | get { return UInt8((value >> 24) & 0xFF) } 27 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 28 | } 29 | } 30 | 31 | public struct RGBAImage { 32 | public var pixels: UnsafeMutableBufferPointer 33 | public var width: Int 34 | public var height: Int 35 | 36 | public init?(image: UIImage) { 37 | // CGImage로 변환이 가능해야 한다. 38 | guard let cgImage = image.cgImage else { 39 | return nil 40 | } 41 | 42 | // 주소 계산을 위해서 Float을 Int로 저장한다. 43 | width = Int(image.size.width) 44 | height = Int(image.size.height) 45 | 46 | // let bitsPerComponent = 8 // 픽셀의 한 요소당 1바이트 47 | // let bytesPerPixels = 4 // RGBA 48 | let bytesPerRow = width * 4 49 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 50 | let colorSpace = CGColorSpaceCreateDeviceRGB() 51 | 52 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 53 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 54 | 55 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 56 | return nil 57 | } 58 | 59 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 60 | 61 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 62 | } 63 | 64 | public func toUIImage() -> UIImage? { 65 | let colorSpace = CGColorSpaceCreateDeviceRGB() 66 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 67 | let bytesPerRow = width * 4 68 | 69 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 70 | 71 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 72 | return nil 73 | } 74 | guard let cgImage = imageContext.makeImage() else { 75 | return nil 76 | } 77 | 78 | let image = UIImage(cgImage: cgImage) 79 | return image 80 | } 81 | 82 | public func pixel(x : Int, _ y : Int) -> Pixel? { 83 | guard x >= 0 && x < width && y >= 0 && y < height else { 84 | return nil 85 | } 86 | 87 | let address = y * width + x 88 | return pixels[address] 89 | } 90 | 91 | public func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 92 | guard x >= 0 && x < width && y >= 0 && y < height else { 93 | return 94 | } 95 | 96 | let address = y * width + x 97 | pixels[address] = pixel 98 | } 99 | 100 | public func process( functor : ((Pixel) -> Pixel) ) { 101 | for y in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /03_SplitRGBColorSpace.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | func grabR(_ image: RGBAImage) -> RGBAImage { 6 | var outImage = image 7 | outImage.process { (pixel) -> Pixel in 8 | var pixel = pixel 9 | pixel.R = pixel.R 10 | pixel.G = 0 11 | pixel.B = 0 12 | return pixel 13 | } 14 | return outImage 15 | } 16 | 17 | func grabG(_ image: RGBAImage) -> RGBAImage { 18 | var outImage = image 19 | outImage.process { (pixel) -> Pixel in 20 | var pixel = pixel 21 | pixel.R = 0 22 | pixel.G = pixel.G 23 | pixel.B = 0 24 | return pixel 25 | } 26 | return outImage 27 | } 28 | 29 | func grabB(_ image: RGBAImage) -> RGBAImage { 30 | var outImage = image 31 | outImage.process { (pixel) -> Pixel in 32 | var pixel = pixel 33 | pixel.R = 0 34 | pixel.G = 0 35 | pixel.B = pixel.B 36 | return pixel 37 | } 38 | return outImage 39 | } 40 | 41 | 42 | let rgba1 = RGBAImage(image: UIImage(named: "monet")!)! 43 | let newImage = grabR(rgba1).toUIImage() 44 | 45 | let rgba2 = RGBAImage(image: UIImage(named: "monet")!)! 46 | grabG(rgba2).toUIImage() 47 | 48 | let rgba3 = RGBAImage(image: UIImage(named: "monet")!)! 49 | grabB(rgba3).toUIImage() 50 | 51 | let result = RGBAImage.composite(rgba1, rgba2, rgba3) 52 | result.toUIImage() 53 | -------------------------------------------------------------------------------- /03_SplitRGBColorSpace.playground/Resources/monet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/03_SplitRGBColorSpace.playground/Resources/monet.png -------------------------------------------------------------------------------- /03_SplitRGBColorSpace.playground/Sources/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public struct Pixel { 4 | public var value: UInt32 5 | 6 | //red 7 | public var R: UInt8 { 8 | get { return UInt8(value & 0xFF); } 9 | set { value = UInt32(newValue) | (value & 0xFFFFFF00) } 10 | } 11 | 12 | //green 13 | public var G: UInt8 { 14 | get { return UInt8((value >> 8) & 0xFF) } 15 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 16 | } 17 | 18 | //blue 19 | public var B: UInt8 { 20 | get { return UInt8((value >> 16) & 0xFF) } 21 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 22 | } 23 | 24 | //alpha 25 | public var A: UInt8 { 26 | get { return UInt8((value >> 24) & 0xFF) } 27 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 28 | } 29 | } 30 | 31 | public struct RGBAImage { 32 | public var pixels: UnsafeMutableBufferPointer 33 | public var width: Int 34 | public var height: Int 35 | 36 | public init?(image: UIImage) { 37 | // CGImage로 변환이 가능해야 한다. 38 | guard let cgImage = image.cgImage else { 39 | return nil 40 | } 41 | 42 | // 주소 계산을 위해서 Float을 Int로 저장한다. 43 | width = Int(image.size.width) 44 | height = Int(image.size.height) 45 | 46 | // 4 * width * height 크기의 버퍼를 생성한다. 47 | let bytesPerRow = width * 4 48 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 49 | 50 | // 색상공간은 Device의 것을 따른다 51 | let colorSpace = CGColorSpaceCreateDeviceRGB() 52 | 53 | // BGRA로 비트맵을 만든다 54 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 55 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 56 | 57 | // 비트맵 생성 58 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 59 | return nil 60 | } 61 | 62 | // cgImage를 imageData에 채운다. 63 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 64 | 65 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 66 | } 67 | 68 | 69 | public init(width: Int, height: Int) { 70 | let image = RGBAImage.newUIImage(width: width, height: height) 71 | self.init(image: image)! 72 | } 73 | 74 | public func toUIImage() -> UIImage? { 75 | let colorSpace = CGColorSpaceCreateDeviceRGB() 76 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 77 | let bytesPerRow = width * 4 78 | 79 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 80 | 81 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 82 | return nil 83 | } 84 | 85 | guard let cgImage = imageContext.makeImage() else { 86 | return nil 87 | } 88 | 89 | let image = UIImage(cgImage: cgImage) 90 | return image 91 | } 92 | 93 | 94 | public func pixel(x : Int, _ y : Int) -> Pixel? { 95 | guard x >= 0 && x < width && y >= 0 && y < height else { 96 | return nil 97 | } 98 | 99 | let address = y * width + x 100 | return pixels[address] 101 | } 102 | 103 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 104 | guard x >= 0 && x < width && y >= 0 && y < height else { 105 | return 106 | } 107 | 108 | let address = y * width + x 109 | pixels[address] = pixel 110 | } 111 | 112 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 113 | for y in 0.. UIImage { 127 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 128 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 129 | UIColor.black.setFill() 130 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 131 | let image = UIGraphicsGetImageFromCurrentImageContext(); 132 | UIGraphicsEndImageContext(); 133 | return image! 134 | } 135 | } 136 | 137 | extension RGBAImage { 138 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 139 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 140 | for y in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /04_RGBtoGray.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | func gray1(_ image: RGBAImage) -> RGBAImage { 6 | var outImage = image 7 | outImage.process { (pixel) -> Pixel in 8 | var pixel = pixel 9 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 10 | pixel.Rf = result 11 | pixel.Gf = result 12 | pixel.Bf = result 13 | return pixel 14 | } 15 | return outImage 16 | } 17 | 18 | func gray2(_ image: RGBAImage) -> RGBAImage { 19 | var outImage = image 20 | outImage.process { (pixel) -> Pixel in 21 | var pixel = pixel 22 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 23 | pixel.Rf = result 24 | pixel.Gf = result 25 | pixel.Bf = result 26 | return pixel 27 | } 28 | return outImage 29 | } 30 | 31 | func gray3(_ image: RGBAImage) -> RGBAImage { 32 | var outImage = image 33 | outImage.process { (pixel) -> Pixel in 34 | var pixel = pixel 35 | pixel.R = pixel.G 36 | pixel.G = pixel.G 37 | pixel.B = pixel.G 38 | return pixel 39 | } 40 | return outImage 41 | } 42 | 43 | func gray4(_ image: RGBAImage) -> RGBAImage { 44 | var outImage = image 45 | outImage.process { (pixel) -> Pixel in 46 | var pixel = pixel 47 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 48 | pixel.Rf = result 49 | pixel.Gf = result 50 | pixel.Bf = result 51 | return pixel 52 | } 53 | return outImage 54 | } 55 | 56 | func gray5(_ image: RGBAImage) -> RGBAImage { 57 | var outImage = image 58 | outImage.process { (pixel) -> Pixel in 59 | var pixel = pixel 60 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 61 | pixel.Rf = result 62 | pixel.Gf = result 63 | pixel.Bf = result 64 | return pixel 65 | } 66 | return outImage 67 | } 68 | 69 | 70 | 71 | let rgba1 = RGBAImage(image: UIImage(named: "monet")!)! 72 | gray1(rgba1).toUIImage() 73 | 74 | let rgba2 = RGBAImage(image: UIImage(named: "monet")!)! 75 | gray2(rgba2).toUIImage() 76 | 77 | let rgba3 = RGBAImage(image: UIImage(named: "monet")!)! 78 | gray3(rgba3).toUIImage() 79 | 80 | let rgba4 = RGBAImage(image: UIImage(named: "monet")!)! 81 | gray4(rgba4).toUIImage() 82 | 83 | let rgba5 = RGBAImage(image: UIImage(named: "monet")!)! 84 | gray5(rgba5).toUIImage() 85 | -------------------------------------------------------------------------------- /04_RGBtoGray.playground/Resources/monet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/04_RGBtoGray.playground/Resources/monet.png -------------------------------------------------------------------------------- /04_RGBtoGray.playground/Sources/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public struct Pixel { 4 | public var value: UInt32 5 | 6 | //red 7 | public var R: UInt8 { 8 | get { return UInt8(value & 0xFF); } 9 | set { value = UInt32(newValue) | (value & 0xFFFFFF00) } 10 | } 11 | 12 | //green 13 | public var G: UInt8 { 14 | get { return UInt8((value >> 8) & 0xFF) } 15 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 16 | } 17 | 18 | //blue 19 | public var B: UInt8 { 20 | get { return UInt8((value >> 16) & 0xFF) } 21 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 22 | } 23 | 24 | //alpha 25 | public var A: UInt8 { 26 | get { return UInt8((value >> 24) & 0xFF) } 27 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 28 | } 29 | 30 | public var Rf: Double { 31 | get { return Double(self.R) / 255.0 } 32 | set { self.R = UInt8(newValue * 255.0) } 33 | } 34 | 35 | public var Gf: Double { 36 | get { return Double(self.G) / 255.0 } 37 | set { self.G = UInt8(newValue * 255.0) } 38 | } 39 | 40 | public var Bf: Double { 41 | get { return Double(self.B) / 255.0 } 42 | set { self.B = UInt8(newValue * 255.0) } 43 | } 44 | 45 | public var Af: Double { 46 | get { return Double(self.A) / 255.0 } 47 | set { self.A = UInt8(newValue * 255.0) } 48 | } 49 | } 50 | 51 | public struct RGBAImage { 52 | public var pixels: UnsafeMutableBufferPointer 53 | public var width: Int 54 | public var height: Int 55 | 56 | public init?(image: UIImage) { 57 | // CGImage로 변환이 가능해야 한다. 58 | guard let cgImage = image.cgImage else { 59 | return nil 60 | } 61 | 62 | // 주소 계산을 위해서 Float을 Int로 저장한다. 63 | width = Int(image.size.width) 64 | height = Int(image.size.height) 65 | 66 | // 4 * width * height 크기의 버퍼를 생성한다. 67 | let bytesPerRow = width * 4 68 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 69 | 70 | // 색상공간은 Device의 것을 따른다 71 | let colorSpace = CGColorSpaceCreateDeviceRGB() 72 | 73 | // BGRA로 비트맵을 만든다 74 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 75 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 76 | 77 | // 비트맵 생성 78 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 79 | return nil 80 | } 81 | 82 | // cgImage를 imageData에 채운다. 83 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 84 | 85 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 86 | } 87 | 88 | 89 | public init(width: Int, height: Int) { 90 | let image = RGBAImage.newUIImage(width: width, height: height) 91 | self.init(image: image)! 92 | } 93 | 94 | public func clone() -> RGBAImage { 95 | let cloneImage = RGBAImage(width: self.width, height: self.height) 96 | for y in 0.. UIImage? { 107 | let colorSpace = CGColorSpaceCreateDeviceRGB() 108 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 109 | let bytesPerRow = width * 4 110 | 111 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 112 | 113 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 114 | return nil 115 | } 116 | 117 | guard let cgImage = imageContext.makeImage() else { 118 | return nil 119 | } 120 | 121 | let image = UIImage(cgImage: cgImage) 122 | return image 123 | } 124 | 125 | 126 | public func pixel(x : Int, _ y : Int) -> Pixel? { 127 | guard x >= 0 && x < width && y >= 0 && y < height else { 128 | return nil 129 | } 130 | 131 | let address = y * width + x 132 | return pixels[address] 133 | } 134 | 135 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 136 | guard x >= 0 && x < width && y >= 0 && y < height else { 137 | return 138 | } 139 | 140 | let address = y * width + x 141 | pixels[address] = pixel 142 | } 143 | 144 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 145 | for y in 0.. UIImage { 159 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 160 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 161 | UIColor.black.setFill() 162 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 163 | let image = UIGraphicsGetImageFromCurrentImageContext(); 164 | UIGraphicsEndImageContext(); 165 | return image! 166 | } 167 | } 168 | 169 | extension RGBAImage { 170 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 171 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 172 | for y in 0.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /05_ImageOperators.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | let rgba1 = RGBAImage(image: UIImage(named: "monet")!)! 6 | ImageProcess 7 | .gray1(rgba1.clone()) 8 | .toUIImage() 9 | 10 | let rgba2 = RGBAImage(image: UIImage(named: "monet")!)! 11 | ImageProcess 12 | .gray2(rgba1.clone()) 13 | .toUIImage() 14 | 15 | let rgba3 = RGBAImage(image: UIImage(named: "monet")!)! 16 | ImageProcess 17 | .gray3(rgba1.clone()) 18 | .toUIImage() 19 | 20 | let rgba4 = RGBAImage(image: UIImage(named: "monet")!)! 21 | ImageProcess 22 | .gray4(rgba1.clone()) 23 | .toUIImage() 24 | 25 | let rgba5 = RGBAImage(image: UIImage(named: "monet")!)! 26 | ImageProcess 27 | .gray5(rgba1.clone()) 28 | .toUIImage() 29 | 30 | 31 | let image1 = RGBAImage(image: UIImage(named: "monet")!)! 32 | let r_component = ImageProcess.grabR(rgba1.clone()) 33 | r_component.toUIImage() 34 | 35 | let image2 = RGBAImage(image: UIImage(named: "monet")!)! 36 | let g_component = ImageProcess.grabG(rgba1.clone()) 37 | g_component.toUIImage() 38 | 39 | let image3 = RGBAImage(image: UIImage(named: "monet")!)! 40 | let b_component = ImageProcess.grabB(rgba1.clone()) 41 | b_component.toUIImage() 42 | 43 | ImageProcess 44 | .composite(r_component, g_component, b_component) 45 | .toUIImage() 46 | 47 | ImageProcess 48 | .composite(b_component, g_component) 49 | .toUIImage() 50 | -------------------------------------------------------------------------------- /05_ImageOperators.playground/Resources/monet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/05_ImageOperators.playground/Resources/monet.png -------------------------------------------------------------------------------- /05_ImageOperators.playground/Sources/ImageProcess.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public class ImageProcess { 4 | 5 | public static func grabR(_ image: RGBAImage) -> RGBAImage { 6 | var outImage = image 7 | outImage.process { (pixel) -> Pixel in 8 | var pixel = pixel 9 | pixel.R = pixel.R 10 | pixel.G = 0 11 | pixel.B = 0 12 | return pixel 13 | } 14 | return outImage 15 | } 16 | 17 | public static func grabG(_ image: RGBAImage) -> RGBAImage { 18 | var outImage = image 19 | outImage.process { (pixel) -> Pixel in 20 | var pixel = pixel 21 | pixel.R = 0 22 | pixel.G = pixel.G 23 | pixel.B = 0 24 | return pixel 25 | } 26 | return outImage 27 | } 28 | 29 | public static func grabB(_ image: RGBAImage) -> RGBAImage { 30 | var outImage = image 31 | outImage.process { (pixel) -> Pixel in 32 | var pixel = pixel 33 | pixel.R = 0 34 | pixel.G = 0 35 | pixel.B = pixel.B 36 | return pixel 37 | } 38 | return outImage 39 | } 40 | 41 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 42 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 43 | for y in 0.. RGBAImage { 63 | var outImage = image 64 | outImage.process { (pixel) -> Pixel in 65 | var pixel = pixel 66 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 67 | pixel.Rf = result 68 | pixel.Gf = result 69 | pixel.Bf = result 70 | return pixel 71 | } 72 | return outImage 73 | } 74 | 75 | public static func gray2(_ image: RGBAImage) -> RGBAImage { 76 | var outImage = image 77 | outImage.process { (pixel) -> Pixel in 78 | var pixel = pixel 79 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 80 | pixel.Rf = result 81 | pixel.Gf = result 82 | pixel.Bf = result 83 | return pixel 84 | } 85 | return outImage 86 | } 87 | 88 | public static func gray3(_ image: RGBAImage) -> RGBAImage { 89 | var outImage = image 90 | outImage.process { (pixel) -> Pixel in 91 | var pixel = pixel 92 | pixel.R = pixel.G 93 | pixel.G = pixel.G 94 | pixel.B = pixel.G 95 | return pixel 96 | } 97 | return outImage 98 | } 99 | 100 | public static func gray4(_ image: RGBAImage) -> RGBAImage { 101 | var outImage = image 102 | outImage.process { (pixel) -> Pixel in 103 | var pixel = pixel 104 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 105 | pixel.Rf = result 106 | pixel.Gf = result 107 | pixel.Bf = result 108 | return pixel 109 | } 110 | return outImage 111 | } 112 | 113 | public static func gray5(_ image: RGBAImage) -> RGBAImage { 114 | var outImage = image 115 | outImage.process { (pixel) -> Pixel in 116 | var pixel = pixel 117 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 118 | pixel.Rf = result 119 | pixel.Gf = result 120 | pixel.Bf = result 121 | return pixel 122 | } 123 | return outImage 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /05_ImageOperators.playground/Sources/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public struct Pixel { 4 | public var value: UInt32 5 | 6 | //red 7 | public var R: UInt8 { 8 | get { return UInt8(value & 0xFF); } 9 | set { value = UInt32(newValue) | (value & 0xFFFFFF00) } 10 | } 11 | 12 | //green 13 | public var G: UInt8 { 14 | get { return UInt8((value >> 8) & 0xFF) } 15 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 16 | } 17 | 18 | //blue 19 | public var B: UInt8 { 20 | get { return UInt8((value >> 16) & 0xFF) } 21 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 22 | } 23 | 24 | //alpha 25 | public var A: UInt8 { 26 | get { return UInt8((value >> 24) & 0xFF) } 27 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 28 | } 29 | 30 | public var Rf: Double { 31 | get { return Double(self.R) / 255.0 } 32 | set { self.R = UInt8(newValue * 255.0) } 33 | } 34 | 35 | public var Gf: Double { 36 | get { return Double(self.G) / 255.0 } 37 | set { self.G = UInt8(newValue * 255.0) } 38 | } 39 | 40 | public var Bf: Double { 41 | get { return Double(self.B) / 255.0 } 42 | set { self.B = UInt8(newValue * 255.0) } 43 | } 44 | 45 | public var Af: Double { 46 | get { return Double(self.A) / 255.0 } 47 | set { self.A = UInt8(newValue * 255.0) } 48 | } 49 | } 50 | 51 | public struct RGBAImage { 52 | public var pixels: UnsafeMutableBufferPointer 53 | public var width: Int 54 | public var height: Int 55 | 56 | public init?(image: UIImage) { 57 | // CGImage로 변환이 가능해야 한다. 58 | guard let cgImage = image.cgImage else { 59 | return nil 60 | } 61 | 62 | // 주소 계산을 위해서 Float을 Int로 저장한다. 63 | width = Int(image.size.width) 64 | height = Int(image.size.height) 65 | 66 | // 4 * width * height 크기의 버퍼를 생성한다. 67 | let bytesPerRow = width * 4 68 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 69 | 70 | // 색상공간은 Device의 것을 따른다 71 | let colorSpace = CGColorSpaceCreateDeviceRGB() 72 | 73 | // BGRA로 비트맵을 만든다 74 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 75 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 76 | 77 | // 비트맵 생성 78 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 79 | return nil 80 | } 81 | 82 | // cgImage를 imageData에 채운다. 83 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 84 | 85 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 86 | } 87 | 88 | 89 | public init(width: Int, height: Int) { 90 | let image = RGBAImage.newUIImage(width: width, height: height) 91 | self.init(image: image)! 92 | } 93 | 94 | public func clone() -> RGBAImage { 95 | let cloneImage = RGBAImage(width: self.width, height: self.height) 96 | for y in 0.. UIImage? { 107 | let colorSpace = CGColorSpaceCreateDeviceRGB() 108 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 109 | let bytesPerRow = width * 4 110 | 111 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 112 | 113 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 114 | return nil 115 | } 116 | 117 | guard let cgImage = imageContext.makeImage() else { 118 | return nil 119 | } 120 | 121 | let image = UIImage(cgImage: cgImage) 122 | return image 123 | } 124 | 125 | 126 | public func pixel(x : Int, _ y : Int) -> Pixel? { 127 | guard x >= 0 && x < width && y >= 0 && y < height else { 128 | return nil 129 | } 130 | 131 | let address = y * width + x 132 | return pixels[address] 133 | } 134 | 135 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 136 | guard x >= 0 && x < width && y >= 0 && y < height else { 137 | return 138 | } 139 | 140 | let address = y * width + x 141 | pixels[address] = pixel 142 | } 143 | 144 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 145 | for y in 0.. UIImage { 159 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 160 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 161 | UIColor.black.setFill() 162 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 163 | let image = UIGraphicsGetImageFromCurrentImageContext(); 164 | UIGraphicsEndImageContext(); 165 | return image! 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /05_ImageOperators.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /06_SplitRGBColorSpace_2.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | let rgba1 = RGBAImage(image: UIImage(named: "monet")!)! 6 | ImageProcess 7 | .gray1(rgba1.clone()) 8 | .toUIImage() 9 | 10 | let rgba2 = RGBAImage(image: UIImage(named: "monet")!)! 11 | ImageProcess 12 | .gray2(rgba1.clone()) 13 | .toUIImage() 14 | 15 | let rgba3 = RGBAImage(image: UIImage(named: "monet")!)! 16 | ImageProcess 17 | .gray3(rgba1.clone()) 18 | .toUIImage() 19 | 20 | let rgba4 = RGBAImage(image: UIImage(named: "monet")!)! 21 | ImageProcess 22 | .gray4(rgba1.clone()) 23 | .toUIImage() 24 | 25 | let rgba5 = RGBAImage(image: UIImage(named: "monet")!)! 26 | ImageProcess 27 | .gray5(rgba1.clone()) 28 | .toUIImage() 29 | 30 | 31 | let image1 = RGBAImage(image: UIImage(named: "monet")!)! 32 | let r_component = ImageProcess.grabR(rgba1.clone()) 33 | r_component.toUIImage() 34 | 35 | let image2 = RGBAImage(image: UIImage(named: "monet")!)! 36 | let g_component = ImageProcess.grabG(rgba1.clone()) 37 | g_component.toUIImage() 38 | 39 | 40 | let b_component = ImageProcess.grabB(rgba1.clone()) 41 | b_component.toUIImage() 42 | 43 | ImageProcess 44 | .composite(r_component, g_component, b_component) 45 | .toUIImage() 46 | 47 | ImageProcess 48 | .composite(b_component, g_component) 49 | .toUIImage() 50 | 51 | 52 | 53 | 54 | 55 | // 색상분할을 다르게 56 | //let image3 = rgba1.clone() 57 | //let R = ByteImage(width: image3.width, height: image3.height) 58 | //let G = ByteImage(width: image3.width, height: image3.height) 59 | //let B = ByteImage(width: image3.width, height: image3.height) 60 | // 61 | //image3.enumerate { (index, pixel) -> Void in 62 | // 63 | // R.pixels[index] = pixel.R.toBytePixel() 64 | // G.pixels[index] = pixel.G.toBytePixel() 65 | // B.pixels[index] = pixel.B.toBytePixel() 66 | //} 67 | // 68 | //R.toUIImage() 69 | //G.toUIImage() 70 | //B.toUIImage() 71 | // 72 | // 73 | // 74 | //ImageProcess.composite(R, G, B).toUIImage() 75 | 76 | let (R, G, B) = ImageProcess.splitRGB(rgba1.clone()) 77 | R.toUIImage() 78 | G.toUIImage() 79 | B.toUIImage() 80 | 81 | ImageProcess.composite(R, G, B).toUIImage() 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /06_SplitRGBColorSpace_2.playground/Resources/monet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/06_SplitRGBColorSpace_2.playground/Resources/monet.png -------------------------------------------------------------------------------- /06_SplitRGBColorSpace_2.playground/Sources/ByteImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /** 4 | * 1byte크기의 화소 배열로 이뤄진 이미지 5 | */ 6 | public struct BytePixel { 7 | private var value: UInt8 8 | 9 | public init(value : UInt8) { 10 | self.value = value 11 | } 12 | 13 | //red 14 | public var C: UInt8 { 15 | get { return value } 16 | set { value = newValue } 17 | } 18 | 19 | public var Cf: Double { 20 | get { return Double(self.C) / 255.0 } 21 | set { self.C = UInt8(newValue * 255.0) } 22 | } 23 | } 24 | 25 | public struct ByteImage { 26 | public var pixels: UnsafeMutableBufferPointer 27 | public var width: Int 28 | public var height: Int 29 | 30 | public init?(image: UIImage) { 31 | // CGImage로 변환이 가능해야 한다. 32 | guard let cgImage = image.cgImage else { 33 | return nil 34 | } 35 | 36 | // 주소 계산을 위해서 Float을 Int로 저장한다. 37 | width = Int(image.size.width) 38 | height = Int(image.size.height) 39 | 40 | // 1 * width * height 크기의 버퍼를 생성한다. 41 | let bytesPerRow = width * 1 42 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 43 | 44 | // 색상공간은 Device의 것을 따른다 45 | let colorSpace = CGColorSpaceCreateDeviceGray() 46 | 47 | // BGRA로 비트맵을 만든다 48 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 49 | // 비트맵 생성 50 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 51 | return nil 52 | } 53 | 54 | // cgImage를 imageData에 채운다. 55 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 56 | 57 | // 이미지 화소의 배열 주소를 pixels에 담는다 58 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 59 | } 60 | 61 | 62 | public init(width: Int, height: Int) { 63 | let image = ByteImage.newUIImage(width: width, height: height) 64 | self.init(image: image)! 65 | } 66 | 67 | public func clone() -> ByteImage { 68 | let cloneImage = ByteImage(width: self.width, height: self.height) 69 | for y in 0.. UIImage? { 79 | let colorSpace = CGColorSpaceCreateDeviceGray() 80 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 81 | let bytesPerRow = width * 1 82 | 83 | 84 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 85 | return nil 86 | } 87 | guard let cgImage = imageContext.makeImage() else { 88 | return nil 89 | } 90 | 91 | let image = UIImage(cgImage: cgImage) 92 | return image 93 | } 94 | 95 | public func pixel(x : Int, _ y : Int) -> BytePixel? { 96 | guard x >= 0 && x < width && y >= 0 && y < height else { 97 | return nil 98 | } 99 | 100 | let address = y * width + x 101 | return pixels[address] 102 | } 103 | 104 | public mutating func pixel(x : Int, _ y : Int, _ pixel: BytePixel) { 105 | guard x >= 0 && x < width && y >= 0 && y < height else { 106 | return 107 | } 108 | 109 | let address = y * width + x 110 | pixels[address] = pixel 111 | } 112 | 113 | public mutating func process( functor : ((BytePixel) -> BytePixel) ) { 114 | for y in 0.. UIImage { 123 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 124 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 125 | UIColor.black.setFill() 126 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 127 | let image = UIGraphicsGetImageFromCurrentImageContext(); 128 | UIGraphicsEndImageContext(); 129 | return image! 130 | } 131 | } 132 | 133 | extension UInt8 { 134 | public func toBytePixel() -> BytePixel { 135 | return BytePixel(value: self) 136 | } 137 | } 138 | 139 | 140 | -------------------------------------------------------------------------------- /06_SplitRGBColorSpace_2.playground/Sources/ImageProcess.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public class ImageProcess { 4 | 5 | public static func grabR(_ image: RGBAImage) -> RGBAImage { 6 | var outImage = image 7 | outImage.process { (pixel) -> Pixel in 8 | var pixel = pixel 9 | pixel.R = pixel.R 10 | pixel.G = 0 11 | pixel.B = 0 12 | return pixel 13 | } 14 | return outImage 15 | } 16 | 17 | public static func grabG(_ image: RGBAImage) -> RGBAImage { 18 | var outImage = image 19 | outImage.process { (pixel) -> Pixel in 20 | var pixel = pixel 21 | pixel.R = 0 22 | pixel.G = pixel.G 23 | pixel.B = 0 24 | return pixel 25 | } 26 | return outImage 27 | } 28 | 29 | public static func grabB(_ image: RGBAImage) -> RGBAImage { 30 | var outImage = image 31 | outImage.process { (pixel) -> Pixel in 32 | var pixel = pixel 33 | pixel.R = 0 34 | pixel.G = 0 35 | pixel.B = pixel.B 36 | return pixel 37 | } 38 | return outImage 39 | } 40 | 41 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 42 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 43 | for y in 0.. RGBAImage { 64 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 65 | for y in 0.. RGBAImage { 93 | var outImage = image 94 | outImage.process { (pixel) -> Pixel in 95 | var pixel = pixel 96 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 97 | pixel.Rf = result 98 | pixel.Gf = result 99 | pixel.Bf = result 100 | return pixel 101 | } 102 | return outImage 103 | } 104 | 105 | public static func gray2(_ image: RGBAImage) -> RGBAImage { 106 | var outImage = image 107 | outImage.process { (pixel) -> Pixel in 108 | var pixel = pixel 109 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 110 | pixel.Rf = result 111 | pixel.Gf = result 112 | pixel.Bf = result 113 | return pixel 114 | } 115 | return outImage 116 | } 117 | 118 | public static func gray3(_ image: RGBAImage) -> RGBAImage { 119 | var outImage = image 120 | outImage.process { (pixel) -> Pixel in 121 | var pixel = pixel 122 | pixel.R = pixel.G 123 | pixel.G = pixel.G 124 | pixel.B = pixel.G 125 | return pixel 126 | } 127 | return outImage 128 | } 129 | 130 | public static func gray4(_ image: RGBAImage) -> RGBAImage { 131 | var outImage = image 132 | outImage.process { (pixel) -> Pixel in 133 | var pixel = pixel 134 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 135 | pixel.Rf = result 136 | pixel.Gf = result 137 | pixel.Bf = result 138 | return pixel 139 | } 140 | return outImage 141 | } 142 | 143 | public static func gray5(_ image: RGBAImage) -> RGBAImage { 144 | var outImage = image 145 | outImage.process { (pixel) -> Pixel in 146 | var pixel = pixel 147 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 148 | pixel.Rf = result 149 | pixel.Gf = result 150 | pixel.Bf = result 151 | return pixel 152 | } 153 | return outImage 154 | } 155 | 156 | 157 | public static func splitRGB(_ rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) { 158 | let R = ByteImage(width: rgba.width, height: rgba.height) 159 | let G = ByteImage(width: rgba.width, height: rgba.height) 160 | let B = ByteImage(width: rgba.width, height: rgba.height) 161 | 162 | rgba.enumerate { (index, pixel) -> Void in 163 | 164 | R.pixels[index] = pixel.R.toBytePixel() 165 | G.pixels[index] = pixel.G.toBytePixel() 166 | B.pixels[index] = pixel.B.toBytePixel() 167 | } 168 | 169 | return (R, G, B) 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /06_SplitRGBColorSpace_2.playground/Sources/RGBAImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public struct Pixel { 4 | public var value: UInt32 5 | 6 | //red 7 | public var R: UInt8 { 8 | get { return UInt8(value & 0xFF); } 9 | set { value = UInt32(newValue) | (value & 0xFFFFFF00) } 10 | } 11 | 12 | //green 13 | public var G: UInt8 { 14 | get { return UInt8((value >> 8) & 0xFF) } 15 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 16 | } 17 | 18 | //blue 19 | public var B: UInt8 { 20 | get { return UInt8((value >> 16) & 0xFF) } 21 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 22 | } 23 | 24 | //alpha 25 | public var A: UInt8 { 26 | get { return UInt8((value >> 24) & 0xFF) } 27 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 28 | } 29 | 30 | public var Rf: Double { 31 | get { return Double(self.R) / 255.0 } 32 | set { self.R = UInt8(newValue * 255.0) } 33 | } 34 | 35 | public var Gf: Double { 36 | get { return Double(self.G) / 255.0 } 37 | set { self.G = UInt8(newValue * 255.0) } 38 | } 39 | 40 | public var Bf: Double { 41 | get { return Double(self.B) / 255.0 } 42 | set { self.B = UInt8(newValue * 255.0) } 43 | } 44 | 45 | public var Af: Double { 46 | get { return Double(self.A) / 255.0 } 47 | set { self.A = UInt8(newValue * 255.0) } 48 | } 49 | } 50 | 51 | public struct RGBAImage { 52 | public var pixels: UnsafeMutableBufferPointer 53 | public var width: Int 54 | public var height: Int 55 | 56 | public init?(image: UIImage) { 57 | // CGImage로 변환이 가능해야 한다. 58 | guard let cgImage = image.cgImage else { 59 | return nil 60 | } 61 | 62 | // 주소 계산을 위해서 Float을 Int로 저장한다. 63 | width = Int(image.size.width) 64 | height = Int(image.size.height) 65 | 66 | // 4 * width * height 크기의 버퍼를 생성한다. 67 | let bytesPerRow = width * 4 68 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 69 | 70 | // 색상공간은 Device의 것을 따른다 71 | let colorSpace = CGColorSpaceCreateDeviceRGB() 72 | 73 | // BGRA로 비트맵을 만든다 74 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 75 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 76 | 77 | // 비트맵 생성 78 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 79 | return nil 80 | } 81 | 82 | // cgImage를 imageData에 채운다. 83 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 84 | 85 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 86 | } 87 | 88 | 89 | public init(width: Int, height: Int) { 90 | let image = RGBAImage.newUIImage(width: width, height: height) 91 | self.init(image: image)! 92 | } 93 | 94 | public func clone() -> RGBAImage { 95 | let cloneImage = RGBAImage(width: self.width, height: self.height) 96 | for y in 0.. UIImage? { 106 | let colorSpace = CGColorSpaceCreateDeviceRGB() 107 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 108 | let bytesPerRow = width * 4 109 | 110 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 111 | 112 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 113 | return nil 114 | } 115 | 116 | guard let cgImage = imageContext.makeImage() else { 117 | return nil 118 | } 119 | 120 | let image = UIImage(cgImage: cgImage) 121 | return image 122 | } 123 | 124 | public func pixel(x : Int, _ y : Int) -> Pixel? { 125 | guard x >= 0 && x < width && y >= 0 && y < height else { 126 | return nil 127 | } 128 | 129 | let address = y * width + x 130 | return pixels[address] 131 | } 132 | 133 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 134 | guard x >= 0 && x < width && y >= 0 && y < height else { 135 | return 136 | } 137 | 138 | let address = y * width + x 139 | pixels[address] = pixel 140 | } 141 | 142 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 143 | for y in 0.. Void) { 153 | for y in 0.. UIImage { 162 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 163 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 164 | UIColor.black.setFill() 165 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 166 | let image = UIGraphicsGetImageFromCurrentImageContext(); 167 | UIGraphicsEndImageContext(); 168 | return image! 169 | } 170 | } 171 | 172 | -------------------------------------------------------------------------------- /06_SplitRGBColorSpace_2.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | let monet1 = RGBAImage(image: UIImage(named: "monet1")!)! 6 | let monet2 = RGBAImage(image: UIImage(named: "monet2")!)! 7 | let tiger = RGBAImage(image: UIImage(named: "tiger")!)! 8 | 9 | ImageProcess.composite(monet1.clone(), monet2.clone(), tiger.clone()).toUIImage() 10 | 11 | ImageProcess.add(monet1.clone(), monet2.clone()).toUIImage() 12 | ImageProcess.sub(monet1.clone(), monet2.clone()).toUIImage() 13 | ImageProcess.mul(monet1.clone(), monet2.clone()).toUIImage() 14 | ImageProcess.sub(ImageProcess.div(tiger.clone(), monet2.clone()), factor: 0.5).toUIImage() 15 | 16 | let factor = 0.3 17 | ImageProcess.add(monet1.clone(), factor: factor).toUIImage() 18 | ImageProcess.sub(monet1.clone(), factor: factor).toUIImage() 19 | ImageProcess.mul(monet1.clone(), factor: factor).toUIImage() 20 | ImageProcess.div(monet1.clone(), factor: factor).toUIImage() 21 | 22 | 23 | let R1 = ImageProcess.gray5(monet1.clone()) 24 | let img1 = ImageProcess.add(R1.clone(), factor: 0.5) 25 | let img2 = ImageProcess.sub(R1.clone(), factor: 0.7) 26 | ImageProcess.sub(img1.clone(), img2.clone()).toUIImage() 27 | 28 | 29 | -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/Resources/monet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/07_Add_Sub_Mult_Div.playground/Resources/monet1.png -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/Resources/monet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/07_Add_Sub_Mult_Div.playground/Resources/monet2.png -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/Resources/tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/07_Add_Sub_Mult_Div.playground/Resources/tiger.png -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/Sources/ByteImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /** 4 | * 1byte크기의 화소 배열로 이뤄진 이미지 5 | */ 6 | public struct BytePixel { 7 | private var value: UInt8 8 | 9 | public init(value : UInt8) { 10 | self.value = value 11 | } 12 | 13 | //red 14 | public var C: UInt8 { 15 | get { return value } 16 | set { value = newValue } 17 | } 18 | 19 | public var Cf: Double { 20 | get { return Double(self.C) / 255.0 } 21 | set { self.C = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) } 22 | } 23 | } 24 | 25 | public struct ByteImage { 26 | public var pixels: UnsafeMutableBufferPointer 27 | public var width: Int 28 | public var height: Int 29 | 30 | public init?(image: UIImage) { 31 | // CGImage로 변환이 가능해야 한다. 32 | guard let cgImage = image.cgImage else { 33 | return nil 34 | } 35 | 36 | // 주소 계산을 위해서 Float을 Int로 저장한다. 37 | width = Int(image.size.width) 38 | height = Int(image.size.height) 39 | 40 | // 1 * width * height 크기의 버퍼를 생성한다. 41 | let bytesPerRow = width * 1 42 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 43 | 44 | // 색상공간은 Device의 것을 따른다 45 | let colorSpace = CGColorSpaceCreateDeviceGray() 46 | 47 | // BGRA로 비트맵을 만든다 48 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 49 | // 비트맵 생성 50 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 51 | return nil 52 | } 53 | 54 | // cgImage를 imageData에 채운다. 55 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 56 | 57 | // 이미지 화소의 배열 주소를 pixels에 담는다 58 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 59 | } 60 | 61 | 62 | public init(width: Int, height: Int) { 63 | let image = ByteImage.newUIImage(width: width, height: height) 64 | self.init(image: image)! 65 | } 66 | 67 | public func clone() -> ByteImage { 68 | let cloneImage = ByteImage(width: self.width, height: self.height) 69 | for y in 0.. UIImage? { 79 | let colorSpace = CGColorSpaceCreateDeviceGray() 80 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 81 | let bytesPerRow = width * 1 82 | 83 | 84 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 85 | return nil 86 | } 87 | guard let cgImage = imageContext.makeImage() else { 88 | return nil 89 | } 90 | 91 | let image = UIImage(cgImage: cgImage) 92 | return image 93 | } 94 | 95 | public func pixel(x : Int, _ y : Int) -> BytePixel? { 96 | guard x >= 0 && x < width && y >= 0 && y < height else { 97 | return nil 98 | } 99 | 100 | let address = y * width + x 101 | return pixels[address] 102 | } 103 | 104 | public mutating func pixel(x : Int, _ y : Int, _ pixel: BytePixel) { 105 | guard x >= 0 && x < width && y >= 0 && y < height else { 106 | return 107 | } 108 | 109 | let address = y * width + x 110 | pixels[address] = pixel 111 | } 112 | 113 | public mutating func process( functor : ((BytePixel) -> BytePixel) ) { 114 | for y in 0.. UIImage { 123 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 124 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 125 | UIColor.black.setFill() 126 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 127 | let image = UIGraphicsGetImageFromCurrentImageContext(); 128 | UIGraphicsEndImageContext(); 129 | return image! 130 | } 131 | } 132 | 133 | extension UInt8 { 134 | public func toBytePixel() -> BytePixel { 135 | return BytePixel(value: self) 136 | } 137 | } 138 | 139 | 140 | -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/Sources/ImageProcess.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public class ImageProcess { 4 | 5 | public static func grabR(_ image: RGBAImage) -> RGBAImage { 6 | var outImage = image 7 | outImage.process { (pixel) -> Pixel in 8 | var pixel = pixel 9 | pixel.R = pixel.R 10 | pixel.G = 0 11 | pixel.B = 0 12 | return pixel 13 | } 14 | return outImage 15 | } 16 | 17 | public static func grabG(_ image: RGBAImage) -> RGBAImage { 18 | var outImage = image 19 | outImage.process { (pixel) -> Pixel in 20 | var pixel = pixel 21 | pixel.R = 0 22 | pixel.G = pixel.G 23 | pixel.B = 0 24 | return pixel 25 | } 26 | return outImage 27 | } 28 | 29 | public static func grabB(_ image: RGBAImage) -> RGBAImage { 30 | var outImage = image 31 | outImage.process { (pixel) -> Pixel in 32 | var pixel = pixel 33 | pixel.R = 0 34 | pixel.G = 0 35 | pixel.B = pixel.B 36 | return pixel 37 | } 38 | return outImage 39 | } 40 | 41 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 42 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 43 | for y in 0.. Double, rgbaImageList: RGBAImage...) -> RGBAImage { 63 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 64 | for y in 0.. RGBAImage { 85 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 86 | for y in 0.. Double, byteImageList: ByteImage...) -> RGBAImage { 114 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 115 | for y in 0.. RGBAImage { 143 | var outImage = image 144 | outImage.process { (pixel) -> Pixel in 145 | var pixel = pixel 146 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 147 | pixel.Rf = result 148 | pixel.Gf = result 149 | pixel.Bf = result 150 | return pixel 151 | } 152 | return outImage 153 | } 154 | 155 | public static func gray2(_ image: RGBAImage) -> RGBAImage { 156 | var outImage = image 157 | outImage.process { (pixel) -> Pixel in 158 | var pixel = pixel 159 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 160 | pixel.Rf = result 161 | pixel.Gf = result 162 | pixel.Bf = result 163 | return pixel 164 | } 165 | return outImage 166 | } 167 | 168 | public static func gray3(_ image: RGBAImage) -> RGBAImage { 169 | var outImage = image 170 | outImage.process { (pixel) -> Pixel in 171 | var pixel = pixel 172 | pixel.R = pixel.G 173 | pixel.G = pixel.G 174 | pixel.B = pixel.G 175 | return pixel 176 | } 177 | return outImage 178 | } 179 | 180 | public static func gray4(_ image: RGBAImage) -> RGBAImage { 181 | var outImage = image 182 | outImage.process { (pixel) -> Pixel in 183 | var pixel = pixel 184 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 185 | pixel.Rf = result 186 | pixel.Gf = result 187 | pixel.Bf = result 188 | return pixel 189 | } 190 | return outImage 191 | } 192 | 193 | public static func gray5(_ image: RGBAImage) -> RGBAImage { 194 | var outImage = image 195 | outImage.process { (pixel) -> Pixel in 196 | var pixel = pixel 197 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 198 | pixel.Rf = result 199 | pixel.Gf = result 200 | pixel.Bf = result 201 | return pixel 202 | } 203 | return outImage 204 | } 205 | 206 | public static func splitRGB(rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) { 207 | let R = ByteImage(width: rgba.width, height: rgba.height) 208 | let G = ByteImage(width: rgba.width, height: rgba.height) 209 | let B = ByteImage(width: rgba.width, height: rgba.height) 210 | 211 | rgba.enumerate { (index, pixel) -> Void in 212 | 213 | R.pixels[index] = pixel.R.toBytePixel() 214 | G.pixels[index] = pixel.G.toBytePixel() 215 | B.pixels[index] = pixel.B.toBytePixel() 216 | } 217 | 218 | return (R, G, B) 219 | } 220 | 221 | // +, -, *, / 222 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage: RGBAImage, factor: Double) -> RGBAImage { 223 | var outImage = rgbaImage 224 | outImage.process { (pixel) -> Pixel in 225 | var pixel = pixel 226 | pixel.Rf = functor(pixel.Rf, factor) 227 | pixel.Gf = functor(pixel.Gf, factor) 228 | pixel.Bf = functor(pixel.Bf, factor) 229 | return pixel 230 | } 231 | return outImage 232 | } 233 | 234 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage1: RGBAImage, rgbaImage2: RGBAImage) -> RGBAImage { 235 | let result : RGBAImage = RGBAImage(width:rgbaImage1.width, height: rgbaImage1.height) 236 | for y in 0.. Double, byteImage: ByteImage, factor: Double) -> ByteImage { 258 | var outImage = byteImage 259 | outImage.process { (pixel) -> BytePixel in 260 | var pixel = pixel 261 | pixel.Cf = functor(pixel.Cf, factor) 262 | return pixel 263 | } 264 | return outImage 265 | } 266 | 267 | public static func op(_ functor : (Double, Double) -> Double, byteImage1: ByteImage, byteImage2: ByteImage) -> ByteImage { 268 | let result : ByteImage = ByteImage(width:byteImage1.width, height: byteImage1.height) 269 | for y in 0.. RGBAImage { 292 | return op((+), rgbaImage: rgba, factor: factor) 293 | } 294 | 295 | public static func add(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 296 | return op((+), rgbaImage1: rgba1, rgbaImage2: rgba2) 297 | } 298 | 299 | public static func sub(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 300 | return op((-), rgbaImage: rgba, factor: factor) 301 | } 302 | 303 | public static func sub(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 304 | return op((-), rgbaImage1: rgba1, rgbaImage2: rgba2) 305 | } 306 | 307 | public static func mul(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 308 | return op((*), rgbaImage: rgba, factor: factor) 309 | } 310 | 311 | public static func mul(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 312 | return op((*), rgbaImage1: rgba1, rgbaImage2: rgba2) 313 | } 314 | 315 | 316 | public static func div(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 317 | if factor == 0.0 { 318 | return rgba 319 | } 320 | return op((/), rgbaImage: rgba, factor: factor) 321 | } 322 | 323 | // 0으로 나누면 안되기 때문에 따로 만듦 324 | public static func div(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 325 | let result : RGBAImage = RGBAImage(width:rgba1.width, height: rgba1.height) 326 | for y in 0.. ByteImage { 350 | return op((+), byteImage: img, factor: factor) 351 | } 352 | 353 | public static func add(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 354 | return op((+), byteImage1: img1, byteImage2: img2) 355 | } 356 | 357 | public static func sub(_ img: ByteImage, factor: Double) -> ByteImage { 358 | return op((-), byteImage: img, factor: factor) 359 | } 360 | 361 | public static func sub(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 362 | return op((-), byteImage1: img1, byteImage2: img2) 363 | } 364 | 365 | public static func mul(_ img: ByteImage, factor: Double) -> ByteImage { 366 | return op((*), byteImage: img, factor: factor) 367 | } 368 | 369 | public static func mul(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 370 | return op((*), byteImage1: img1, byteImage2: img2) 371 | } 372 | 373 | 374 | public static func div(_ img: ByteImage, factor: Double) -> ByteImage { 375 | if factor == 0.0 { 376 | return img 377 | } 378 | return op((/), byteImage: img, factor: factor) 379 | } 380 | 381 | // 0으로 나누면 안되기 때문에 따로 만듦 382 | public static func div(img1: ByteImage, _ img2: ByteImage) -> ByteImage { 383 | let result : ByteImage = ByteImage(width:img1.width, height: img1.height) 384 | for y in 0..> 8) & 0xFF) } 15 | set { value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF) } 16 | } 17 | 18 | //blue 19 | public var B: UInt8 { 20 | get { return UInt8((value >> 16) & 0xFF) } 21 | set { value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF) } 22 | } 23 | 24 | //alpha 25 | public var A: UInt8 { 26 | get { return UInt8((value >> 24) & 0xFF) } 27 | set { value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF) } 28 | } 29 | 30 | public var Rf: Double { 31 | get { return Double(self.R) / 255.0 } 32 | set { 33 | self.R = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 34 | } 35 | } 36 | 37 | public var Gf: Double { 38 | get { return Double(self.G) / 255.0 } 39 | set { 40 | self.G = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 41 | } 42 | } 43 | 44 | public var Bf: Double { 45 | get { return Double(self.B) / 255.0 } 46 | set { 47 | self.B = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 48 | } 49 | } 50 | 51 | public var Af: Double { 52 | get { return Double(self.A) / 255.0 } 53 | set { 54 | self.A = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 55 | } 56 | } 57 | } 58 | 59 | public struct RGBAImage { 60 | public var pixels: UnsafeMutableBufferPointer 61 | public var width: Int 62 | public var height: Int 63 | 64 | public init?(image: UIImage) { 65 | // CGImage로 변환이 가능해야 한다. 66 | guard let cgImage = image.cgImage else { 67 | return nil 68 | } 69 | 70 | // 주소 계산을 위해서 Float을 Int로 저장한다. 71 | width = Int(image.size.width) 72 | height = Int(image.size.height) 73 | 74 | // 4 * width * height 크기의 버퍼를 생성한다. 75 | let bytesPerRow = width * 4 76 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 77 | 78 | // 색상공간은 Device의 것을 따른다 79 | let colorSpace = CGColorSpaceCreateDeviceRGB() 80 | 81 | // BGRA로 비트맵을 만든다 82 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 83 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 84 | 85 | // 비트맵 생성 86 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 87 | return nil 88 | } 89 | 90 | // cgImage를 imageData에 채운다. 91 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 92 | 93 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 94 | } 95 | 96 | 97 | public init(width: Int, height: Int) { 98 | let image = RGBAImage.newUIImage(width: width, height: height) 99 | self.init(image: image)! 100 | } 101 | 102 | public func clone() -> RGBAImage { 103 | let cloneImage = RGBAImage(width: self.width, height: self.height) 104 | for y in 0.. UIImage? { 114 | let colorSpace = CGColorSpaceCreateDeviceRGB() 115 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 116 | let bytesPerRow = width * 4 117 | 118 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 119 | 120 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 121 | return nil 122 | } 123 | 124 | guard let cgImage = imageContext.makeImage() else { 125 | return nil 126 | } 127 | 128 | let image = UIImage(cgImage: cgImage) 129 | return image 130 | } 131 | 132 | public func pixel(x : Int, _ y : Int) -> Pixel? { 133 | guard x >= 0 && x < width && y >= 0 && y < height else { 134 | return nil 135 | } 136 | 137 | let address = y * width + x 138 | return pixels[address] 139 | } 140 | 141 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 142 | guard x >= 0 && x < width && y >= 0 && y < height else { 143 | return 144 | } 145 | 146 | let address = y * width + x 147 | pixels[address] = pixel 148 | } 149 | 150 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 151 | for y in 0.. Void) { 161 | for y in 0.. UIImage { 170 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 171 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 172 | UIColor.black.setFill() 173 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 174 | let image = UIGraphicsGetImageFromCurrentImageContext(); 175 | UIGraphicsEndImageContext(); 176 | return image! 177 | } 178 | } 179 | 180 | -------------------------------------------------------------------------------- /07_Add_Sub_Mult_Div.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /08_Blending.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | let monet1 = RGBAImage(image: UIImage(named: "monet1")!)! 6 | let monet2 = RGBAImage(image: UIImage(named: "monet2")!)! 7 | let tiger = RGBAImage(image: UIImage(named: "tiger")!)! 8 | 9 | ImageProcess.blending(monet1, tiger, alpha: 0.5).toUIImage() 10 | 11 | 12 | -------------------------------------------------------------------------------- /08_Blending.playground/Resources/monet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/08_Blending.playground/Resources/monet1.png -------------------------------------------------------------------------------- /08_Blending.playground/Resources/monet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/08_Blending.playground/Resources/monet2.png -------------------------------------------------------------------------------- /08_Blending.playground/Resources/tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/08_Blending.playground/Resources/tiger.png -------------------------------------------------------------------------------- /08_Blending.playground/Sources/ByteImage.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | /** 4 | * 1byte크기의 화소 배열로 이뤄진 이미지 5 | */ 6 | public struct BytePixel { 7 | private var value: UInt8 8 | 9 | public init(value : UInt8) { 10 | self.value = value 11 | } 12 | 13 | //red 14 | public var C: UInt8 { 15 | get { return value } 16 | set { 17 | let v = max(min(newValue, 255), 0) 18 | value = v 19 | } 20 | } 21 | 22 | public var Cf: Double { 23 | get { return Double(self.C) / 255.0 } 24 | set { self.C = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) } 25 | } 26 | } 27 | 28 | public struct ByteImage { 29 | public var pixels: UnsafeMutableBufferPointer 30 | public var width: Int 31 | public var height: Int 32 | 33 | public init?(image: UIImage) { 34 | // CGImage로 변환이 가능해야 한다. 35 | guard let cgImage = image.cgImage else { 36 | return nil 37 | } 38 | 39 | // 주소 계산을 위해서 Float을 Int로 저장한다. 40 | width = Int(image.size.width) 41 | height = Int(image.size.height) 42 | 43 | // 1 * width * height 크기의 버퍼를 생성한다. 44 | let bytesPerRow = width * 1 45 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 46 | 47 | // 색상공간은 Device의 것을 따른다 48 | let colorSpace = CGColorSpaceCreateDeviceGray() 49 | 50 | // BGRA로 비트맵을 만든다 51 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 52 | // 비트맵 생성 53 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 54 | return nil 55 | } 56 | 57 | // cgImage를 imageData에 채운다. 58 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 59 | 60 | // 이미지 화소의 배열 주소를 pixels에 담는다 61 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 62 | } 63 | 64 | 65 | public init(width: Int, height: Int) { 66 | let image = ByteImage.newUIImage(width: width, height: height) 67 | self.init(image: image)! 68 | } 69 | 70 | public func clone() -> ByteImage { 71 | let cloneImage = ByteImage(width: self.width, height: self.height) 72 | for y in 0.. UIImage? { 82 | let colorSpace = CGColorSpaceCreateDeviceGray() 83 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 84 | let bytesPerRow = width * 1 85 | 86 | 87 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 88 | return nil 89 | } 90 | guard let cgImage = imageContext.makeImage() else { 91 | return nil 92 | } 93 | 94 | let image = UIImage(cgImage: cgImage) 95 | return image 96 | } 97 | 98 | public func pixel(x : Int, _ y : Int) -> BytePixel? { 99 | guard x >= 0 && x < width && y >= 0 && y < height else { 100 | return nil 101 | } 102 | 103 | let address = y * width + x 104 | return pixels[address] 105 | } 106 | 107 | public mutating func pixel(x : Int, _ y : Int, _ pixel: BytePixel) { 108 | guard x >= 0 && x < width && y >= 0 && y < height else { 109 | return 110 | } 111 | 112 | let address = y * width + x 113 | pixels[address] = pixel 114 | } 115 | 116 | public mutating func process( functor : ((BytePixel) -> BytePixel) ) { 117 | for y in 0.. UIImage { 126 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 127 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 128 | UIColor.black.setFill() 129 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 130 | let image = UIGraphicsGetImageFromCurrentImageContext(); 131 | UIGraphicsEndImageContext(); 132 | return image! 133 | } 134 | } 135 | 136 | extension UInt8 { 137 | public func toBytePixel() -> BytePixel { 138 | return BytePixel(value: self) 139 | } 140 | } 141 | 142 | 143 | -------------------------------------------------------------------------------- /08_Blending.playground/Sources/ImageProcess.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public class ImageProcess { 4 | 5 | public static func grabR(_ image: RGBAImage) -> RGBAImage { 6 | var outImage = image 7 | outImage.process { (pixel) -> Pixel in 8 | var pixel = pixel 9 | pixel.R = pixel.R 10 | pixel.G = 0 11 | pixel.B = 0 12 | return pixel 13 | } 14 | return outImage 15 | } 16 | 17 | public static func grabG(_ image: RGBAImage) -> RGBAImage { 18 | var outImage = image 19 | outImage.process { (pixel) -> Pixel in 20 | var pixel = pixel 21 | pixel.R = 0 22 | pixel.G = pixel.G 23 | pixel.B = 0 24 | return pixel 25 | } 26 | return outImage 27 | } 28 | 29 | public static func grabB(_ image: RGBAImage) -> RGBAImage { 30 | var outImage = image 31 | outImage.process { (pixel) -> Pixel in 32 | var pixel = pixel 33 | pixel.R = 0 34 | pixel.G = 0 35 | pixel.B = pixel.B 36 | return pixel 37 | } 38 | return outImage 39 | } 40 | 41 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 42 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 43 | for y in 0.. Double, rgbaImageList: RGBAImage...) -> RGBAImage { 63 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 64 | for y in 0.. RGBAImage { 85 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 86 | for y in 0.. Double, byteImageList: ByteImage...) -> RGBAImage { 114 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 115 | for y in 0.. RGBAImage { 143 | var outImage = image 144 | outImage.process { (pixel) -> Pixel in 145 | var pixel = pixel 146 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 147 | pixel.Rf = result 148 | pixel.Gf = result 149 | pixel.Bf = result 150 | return pixel 151 | } 152 | return outImage 153 | } 154 | 155 | public static func gray2(_ image: RGBAImage) -> RGBAImage { 156 | var outImage = image 157 | outImage.process { (pixel) -> Pixel in 158 | var pixel = pixel 159 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 160 | pixel.Rf = result 161 | pixel.Gf = result 162 | pixel.Bf = result 163 | return pixel 164 | } 165 | return outImage 166 | } 167 | 168 | public static func gray3(_ image: RGBAImage) -> RGBAImage { 169 | var outImage = image 170 | outImage.process { (pixel) -> Pixel in 171 | var pixel = pixel 172 | pixel.R = pixel.G 173 | pixel.G = pixel.G 174 | pixel.B = pixel.G 175 | return pixel 176 | } 177 | return outImage 178 | } 179 | 180 | public static func gray4(_ image: RGBAImage) -> RGBAImage { 181 | var outImage = image 182 | outImage.process { (pixel) -> Pixel in 183 | var pixel = pixel 184 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 185 | pixel.Rf = result 186 | pixel.Gf = result 187 | pixel.Bf = result 188 | return pixel 189 | } 190 | return outImage 191 | } 192 | 193 | public static func gray5(_ image: RGBAImage) -> RGBAImage { 194 | var outImage = image 195 | outImage.process { (pixel) -> Pixel in 196 | var pixel = pixel 197 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 198 | pixel.Rf = result 199 | pixel.Gf = result 200 | pixel.Bf = result 201 | return pixel 202 | } 203 | return outImage 204 | } 205 | 206 | public static func splitRGB(rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) { 207 | let R = ByteImage(width: rgba.width, height: rgba.height) 208 | let G = ByteImage(width: rgba.width, height: rgba.height) 209 | let B = ByteImage(width: rgba.width, height: rgba.height) 210 | 211 | rgba.enumerate { (index, pixel) -> Void in 212 | 213 | R.pixels[index] = pixel.R.toBytePixel() 214 | G.pixels[index] = pixel.G.toBytePixel() 215 | B.pixels[index] = pixel.B.toBytePixel() 216 | } 217 | 218 | return (R, G, B) 219 | } 220 | 221 | // +, -, *, / 222 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage: RGBAImage, factor: Double) -> RGBAImage { 223 | var outImage = rgbaImage 224 | outImage.process { (pixel) -> Pixel in 225 | var pixel = pixel 226 | pixel.Rf = functor(pixel.Rf, factor) 227 | pixel.Gf = functor(pixel.Gf, factor) 228 | pixel.Bf = functor(pixel.Bf, factor) 229 | return pixel 230 | } 231 | return outImage 232 | } 233 | 234 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage1: RGBAImage, rgbaImage2: RGBAImage) -> RGBAImage { 235 | let result : RGBAImage = RGBAImage(width:rgbaImage1.width, height: rgbaImage1.height) 236 | for y in 0.. Double, byteImage: ByteImage, factor: Double) -> ByteImage { 258 | var outImage = byteImage 259 | outImage.process { (pixel) -> BytePixel in 260 | var pixel = pixel 261 | pixel.Cf = functor(pixel.Cf, factor) 262 | return pixel 263 | } 264 | return outImage 265 | } 266 | 267 | public static func op(_ functor : (Double, Double) -> Double, byteImage1: ByteImage, byteImage2: ByteImage) -> ByteImage { 268 | let result : ByteImage = ByteImage(width:byteImage1.width, height: byteImage1.height) 269 | for y in 0.. RGBAImage { 292 | return op((+), rgbaImage: rgba, factor: factor) 293 | } 294 | 295 | public static func add(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 296 | return op((+), rgbaImage1: rgba1, rgbaImage2: rgba2) 297 | } 298 | 299 | public static func sub(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 300 | return op((-), rgbaImage: rgba, factor: factor) 301 | } 302 | 303 | public static func sub(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 304 | return op((-), rgbaImage1: rgba1, rgbaImage2: rgba2) 305 | } 306 | 307 | public static func mul(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 308 | return op((*), rgbaImage: rgba, factor: factor) 309 | } 310 | 311 | public static func mul(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 312 | return op((*), rgbaImage1: rgba1, rgbaImage2: rgba2) 313 | } 314 | 315 | 316 | public static func div(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 317 | if factor == 0.0 { 318 | return rgba 319 | } 320 | return op((/), rgbaImage: rgba, factor: factor) 321 | } 322 | 323 | // 0으로 나누면 안되기 때문에 따로 만듦 324 | public static func div(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 325 | let result : RGBAImage = RGBAImage(width:rgba1.width, height: rgba1.height) 326 | for y in 0.. ByteImage { 350 | return op((+), byteImage: img, factor: factor) 351 | } 352 | 353 | public static func add(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 354 | return op((+), byteImage1: img1, byteImage2: img2) 355 | } 356 | 357 | public static func sub(_ img: ByteImage, factor: Double) -> ByteImage { 358 | return op((-), byteImage: img, factor: factor) 359 | } 360 | 361 | public static func sub(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 362 | return op((-), byteImage1: img1, byteImage2: img2) 363 | } 364 | 365 | public static func mul(_ img: ByteImage, factor: Double) -> ByteImage { 366 | return op((*), byteImage: img, factor: factor) 367 | } 368 | 369 | public static func mul(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 370 | return op((*), byteImage1: img1, byteImage2: img2) 371 | } 372 | 373 | 374 | public static func div(_ img: ByteImage, factor: Double) -> ByteImage { 375 | if factor == 0.0 { 376 | return img 377 | } 378 | return op((/), byteImage: img, factor: factor) 379 | } 380 | 381 | // 0으로 나누면 안되기 때문에 따로 만듦 382 | public static func div(img1: ByteImage, _ img2: ByteImage) -> ByteImage { 383 | let result : ByteImage = ByteImage(width:img1.width, height: img1.height) 384 | for y in 0.. RGBAImage { 404 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 405 | for y in 0..> 8) & 0xFF) } 18 | set { 19 | let v = max(min(newValue, 255), 0) 20 | value = (UInt32(v) << 8) | (value & 0xFFFF00FF) 21 | } 22 | } 23 | 24 | //blue 25 | public var B: UInt8 { 26 | get { return UInt8((value >> 16) & 0xFF) } 27 | set { 28 | let v = max(min(newValue, 255), 0) 29 | value = (UInt32(v) << 16) | (value & 0xFF00FFFF) 30 | } 31 | } 32 | 33 | //alpha 34 | public var A: UInt8 { 35 | get { return UInt8((value >> 24) & 0xFF) } 36 | set { 37 | let v = max(min(newValue, 255), 0) 38 | value = (UInt32(v) << 24) | (value & 0x00FFFFFF) 39 | } 40 | } 41 | 42 | public var Rf: Double { 43 | get { return Double(self.R) / 255.0 } 44 | set { 45 | self.R = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 46 | } 47 | } 48 | 49 | public var Gf: Double { 50 | get { return Double(self.G) / 255.0 } 51 | set { 52 | self.G = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 53 | } 54 | } 55 | 56 | public var Bf: Double { 57 | get { return Double(self.B) / 255.0 } 58 | set { 59 | self.B = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 60 | } 61 | } 62 | 63 | public var Af: Double { 64 | get { return Double(self.A) / 255.0 } 65 | set { 66 | self.A = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 67 | } 68 | } 69 | } 70 | 71 | public struct RGBAImage { 72 | public var pixels: UnsafeMutableBufferPointer 73 | public var width: Int 74 | public var height: Int 75 | 76 | public init?(image: UIImage) { 77 | // CGImage로 변환이 가능해야 한다. 78 | guard let cgImage = image.cgImage else { 79 | return nil 80 | } 81 | 82 | // 주소 계산을 위해서 Float을 Int로 저장한다. 83 | width = Int(image.size.width) 84 | height = Int(image.size.height) 85 | 86 | // 4 * width * height 크기의 버퍼를 생성한다. 87 | let bytesPerRow = width * 4 88 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 89 | 90 | // 색상공간은 Device의 것을 따른다 91 | let colorSpace = CGColorSpaceCreateDeviceRGB() 92 | 93 | // BGRA로 비트맵을 만든다 94 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 95 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 96 | 97 | // 비트맵 생성 98 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 99 | return nil 100 | } 101 | 102 | // cgImage를 imageData에 채운다. 103 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 104 | 105 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 106 | } 107 | 108 | 109 | public init(width: Int, height: Int) { 110 | let image = RGBAImage.newUIImage(width: width, height: height) 111 | self.init(image: image)! 112 | } 113 | 114 | public func clone() -> RGBAImage { 115 | let cloneImage = RGBAImage(width: self.width, height: self.height) 116 | for y in 0.. UIImage? { 126 | let colorSpace = CGColorSpaceCreateDeviceRGB() 127 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 128 | let bytesPerRow = width * 4 129 | 130 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 131 | 132 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 133 | return nil 134 | } 135 | 136 | guard let cgImage = imageContext.makeImage() else { 137 | return nil 138 | } 139 | 140 | let image = UIImage(cgImage: cgImage) 141 | return image 142 | } 143 | 144 | public func pixel(x : Int, _ y : Int) -> Pixel? { 145 | guard x >= 0 && x < width && y >= 0 && y < height else { 146 | return nil 147 | } 148 | 149 | let address = y * width + x 150 | return pixels[address] 151 | } 152 | 153 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 154 | guard x >= 0 && x < width && y >= 0 && y < height else { 155 | return 156 | } 157 | 158 | let address = y * width + x 159 | pixels[address] = pixel 160 | } 161 | 162 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 163 | for y in 0.. Void) { 173 | for y in 0.. UIImage { 182 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 183 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 184 | UIColor.black.setFill() 185 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 186 | let image = UIGraphicsGetImageFromCurrentImageContext(); 187 | UIGraphicsEndImageContext(); 188 | return image! 189 | } 190 | } 191 | 192 | -------------------------------------------------------------------------------- /08_Blending.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /09_Brightness.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | let monet1 = RGBAImage(image: UIImage(named: "monet1")!)! 6 | let monet2 = RGBAImage(image: UIImage(named: "monet2")!)! 7 | let tiger = RGBAImage(image: UIImage(named: "tiger")!)! 8 | 9 | ImageProcess.brightness(monet1, contrast: 0.3, brightness: 0.8).toUIImage() 10 | 11 | 12 | -------------------------------------------------------------------------------- /09_Brightness.playground/Resources/monet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/09_Brightness.playground/Resources/monet1.png -------------------------------------------------------------------------------- /09_Brightness.playground/Resources/monet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/09_Brightness.playground/Resources/monet2.png -------------------------------------------------------------------------------- /09_Brightness.playground/Resources/tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/09_Brightness.playground/Resources/tiger.png -------------------------------------------------------------------------------- /09_Brightness.playground/Sources/Array2D.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public class Array2D { 4 | 5 | var cols:Int, rows:Int 6 | public var matrix:[T] 7 | 8 | public init(cols:Int, rows:Int, _ defaultValue:T){ 9 | self.cols = cols 10 | self.rows = rows 11 | matrix = Array(repeating:defaultValue,count:cols*rows) 12 | } 13 | 14 | public init(cols:Int, rows:Int, _ elements : [T]) { 15 | self.cols = cols 16 | self.rows = rows 17 | self.matrix = Array(elements) 18 | } 19 | 20 | public subscript(col:Int, row:Int) -> T { 21 | get{ 22 | return matrix[cols * row + col] 23 | } 24 | set{ 25 | matrix[cols * row + col] = newValue 26 | } 27 | } 28 | 29 | public func colCount() -> Int { 30 | return self.cols 31 | } 32 | 33 | public func rowCount() -> Int { 34 | return self.rows 35 | } 36 | 37 | public func withinBounds(x: Int, y: Int) -> Bool { 38 | return x >= 0 && x < cols && y >= 0 && y < rows 39 | } 40 | 41 | public func contains(element: T) -> Bool { 42 | return matrix.index(of: element) != nil 43 | } 44 | 45 | public func indexOf(element: T) -> (col: Int, row: Int)? { 46 | for x in 0.. 30 | public var width: Int 31 | public var height: Int 32 | 33 | public init?(image: UIImage) { 34 | // CGImage로 변환이 가능해야 한다. 35 | guard let cgImage = image.cgImage else { 36 | return nil 37 | } 38 | 39 | // 주소 계산을 위해서 Float을 Int로 저장한다. 40 | width = Int(image.size.width) 41 | height = Int(image.size.height) 42 | 43 | // 1 * width * height 크기의 버퍼를 생성한다. 44 | let bytesPerRow = width * 1 45 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 46 | 47 | // 색상공간은 Device의 것을 따른다 48 | let colorSpace = CGColorSpaceCreateDeviceGray() 49 | 50 | // BGRA로 비트맵을 만든다 51 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 52 | // 비트맵 생성 53 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 54 | return nil 55 | } 56 | 57 | // cgImage를 imageData에 채운다. 58 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 59 | 60 | // 이미지 화소의 배열 주소를 pixels에 담는다 61 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 62 | } 63 | 64 | 65 | public init(width: Int, height: Int) { 66 | let image = ByteImage.newUIImage(width: width, height: height) 67 | self.init(image: image)! 68 | } 69 | 70 | public func clone() -> ByteImage { 71 | let cloneImage = ByteImage(width: self.width, height: self.height) 72 | for y in 0.. UIImage? { 82 | let colorSpace = CGColorSpaceCreateDeviceGray() 83 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 84 | let bytesPerRow = width * 1 85 | 86 | 87 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 88 | return nil 89 | } 90 | guard let cgImage = imageContext.makeImage() else { 91 | return nil 92 | } 93 | 94 | let image = UIImage(cgImage: cgImage) 95 | return image 96 | } 97 | 98 | public func pixel(_ x : Int, _ y : Int) -> BytePixel? { 99 | guard x >= 0 && x < width && y >= 0 && y < height else { 100 | return nil 101 | } 102 | 103 | let address = y * width + x 104 | return pixels[address] 105 | } 106 | 107 | public mutating func pixel(_ x : Int, _ y : Int, _ pixel: BytePixel) { 108 | guard x >= 0 && x < width && y >= 0 && y < height else { 109 | return 110 | } 111 | 112 | let address = y * width + x 113 | pixels[address] = pixel 114 | } 115 | 116 | public mutating func process( functor : ((BytePixel) -> BytePixel) ) { 117 | for y in 0.. UIImage { 126 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 127 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 128 | UIColor.black.setFill() 129 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 130 | let image = UIGraphicsGetImageFromCurrentImageContext(); 131 | UIGraphicsEndImageContext(); 132 | return image! 133 | } 134 | } 135 | 136 | extension UInt8 { 137 | public func toBytePixel() -> BytePixel { 138 | return BytePixel(value: self) 139 | } 140 | } 141 | 142 | 143 | -------------------------------------------------------------------------------- /09_Brightness.playground/Sources/ImageProcess.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | func clamp(_ value: T, lower: T, upper: T) -> T { 4 | return min(max(value, lower), upper) 5 | } 6 | 7 | public class ImageProcess { 8 | 9 | public static func grabR(_ image: RGBAImage) -> RGBAImage { 10 | var outImage = image 11 | outImage.process { (pixel) -> Pixel in 12 | var pixel = pixel 13 | pixel.R = pixel.R 14 | pixel.G = 0 15 | pixel.B = 0 16 | return pixel 17 | } 18 | return outImage 19 | } 20 | 21 | public static func grabG(_ image: RGBAImage) -> RGBAImage { 22 | var outImage = image 23 | outImage.process { (pixel) -> Pixel in 24 | var pixel = pixel 25 | pixel.R = 0 26 | pixel.G = pixel.G 27 | pixel.B = 0 28 | return pixel 29 | } 30 | return outImage 31 | } 32 | 33 | public static func grabB(_ image: RGBAImage) -> RGBAImage { 34 | var outImage = image 35 | outImage.process { (pixel) -> Pixel in 36 | var pixel = pixel 37 | pixel.R = 0 38 | pixel.G = 0 39 | pixel.B = pixel.B 40 | return pixel 41 | } 42 | return outImage 43 | } 44 | 45 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 46 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 47 | for y in 0.. Double, rgbaImageList: RGBAImage...) -> RGBAImage { 67 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 68 | for y in 0.. RGBAImage { 89 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 90 | for y in 0.. Double, byteImageList: ByteImage...) -> RGBAImage { 118 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 119 | for y in 0.. RGBAImage { 147 | var outImage = image 148 | outImage.process { (pixel) -> Pixel in 149 | var pixel = pixel 150 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 151 | pixel.Rf = result 152 | pixel.Gf = result 153 | pixel.Bf = result 154 | return pixel 155 | } 156 | return outImage 157 | } 158 | 159 | public static func gray2(_ image: RGBAImage) -> RGBAImage { 160 | var outImage = image 161 | outImage.process { (pixel) -> Pixel in 162 | var pixel = pixel 163 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 164 | pixel.Rf = result 165 | pixel.Gf = result 166 | pixel.Bf = result 167 | return pixel 168 | } 169 | return outImage 170 | } 171 | 172 | public static func gray3(_ image: RGBAImage) -> RGBAImage { 173 | var outImage = image 174 | outImage.process { (pixel) -> Pixel in 175 | var pixel = pixel 176 | pixel.R = pixel.G 177 | pixel.G = pixel.G 178 | pixel.B = pixel.G 179 | return pixel 180 | } 181 | return outImage 182 | } 183 | 184 | public static func gray4(_ image: RGBAImage) -> RGBAImage { 185 | var outImage = image 186 | outImage.process { (pixel) -> Pixel in 187 | var pixel = pixel 188 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 189 | pixel.Rf = result 190 | pixel.Gf = result 191 | pixel.Bf = result 192 | return pixel 193 | } 194 | return outImage 195 | } 196 | 197 | public static func gray5(_ image: RGBAImage) -> RGBAImage { 198 | var outImage = image 199 | outImage.process { (pixel) -> Pixel in 200 | var pixel = pixel 201 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 202 | pixel.Rf = result 203 | pixel.Gf = result 204 | pixel.Bf = result 205 | return pixel 206 | } 207 | return outImage 208 | } 209 | 210 | public static func splitRGB(rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) { 211 | let R = ByteImage(width: rgba.width, height: rgba.height) 212 | let G = ByteImage(width: rgba.width, height: rgba.height) 213 | let B = ByteImage(width: rgba.width, height: rgba.height) 214 | 215 | rgba.enumerate { (index, pixel) -> Void in 216 | 217 | R.pixels[index] = pixel.R.toBytePixel() 218 | G.pixels[index] = pixel.G.toBytePixel() 219 | B.pixels[index] = pixel.B.toBytePixel() 220 | } 221 | 222 | return (R, G, B) 223 | } 224 | 225 | // +, -, *, / 226 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage: RGBAImage, factor: Double) -> RGBAImage { 227 | var outImage = rgbaImage 228 | outImage.process { (pixel) -> Pixel in 229 | var pixel = pixel 230 | pixel.Rf = functor(pixel.Rf, factor) 231 | pixel.Gf = functor(pixel.Gf, factor) 232 | pixel.Bf = functor(pixel.Bf, factor) 233 | return pixel 234 | } 235 | return outImage 236 | } 237 | 238 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage1: RGBAImage, rgbaImage2: RGBAImage) -> RGBAImage { 239 | let result : RGBAImage = RGBAImage(width:rgbaImage1.width, height: rgbaImage1.height) 240 | for y in 0.. Double, byteImage: ByteImage, factor: Double) -> ByteImage { 262 | var outImage = byteImage 263 | outImage.process { (pixel) -> BytePixel in 264 | var pixel = pixel 265 | pixel.Cf = functor(pixel.Cf, factor) 266 | return pixel 267 | } 268 | return outImage 269 | } 270 | 271 | public static func op(_ functor : (Double, Double) -> Double, byteImage1: ByteImage, byteImage2: ByteImage) -> ByteImage { 272 | let result : ByteImage = ByteImage(width:byteImage1.width, height: byteImage1.height) 273 | for y in 0.. RGBAImage { 296 | return op((+), rgbaImage: rgba, factor: factor) 297 | } 298 | 299 | public static func add(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 300 | return op((+), rgbaImage1: rgba1, rgbaImage2: rgba2) 301 | } 302 | 303 | public static func sub(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 304 | return op((-), rgbaImage: rgba, factor: factor) 305 | } 306 | 307 | public static func sub(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 308 | return op((-), rgbaImage1: rgba1, rgbaImage2: rgba2) 309 | } 310 | 311 | public static func mul(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 312 | return op((*), rgbaImage: rgba, factor: factor) 313 | } 314 | 315 | public static func mul(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 316 | return op((*), rgbaImage1: rgba1, rgbaImage2: rgba2) 317 | } 318 | 319 | 320 | public static func div(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 321 | if factor == 0.0 { 322 | return rgba 323 | } 324 | return op((/), rgbaImage: rgba, factor: factor) 325 | } 326 | 327 | // 0으로 나누면 안되기 때문에 따로 만듦 328 | public static func div(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 329 | let result : RGBAImage = RGBAImage(width:rgba1.width, height: rgba1.height) 330 | for y in 0.. ByteImage { 354 | return op((+), byteImage: img, factor: factor) 355 | } 356 | 357 | public static func add(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 358 | return op((+), byteImage1: img1, byteImage2: img2) 359 | } 360 | 361 | public static func sub(_ img: ByteImage, factor: Double) -> ByteImage { 362 | return op((-), byteImage: img, factor: factor) 363 | } 364 | 365 | public static func sub(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 366 | return op((-), byteImage1: img1, byteImage2: img2) 367 | } 368 | 369 | public static func mul(_ img: ByteImage, factor: Double) -> ByteImage { 370 | return op((*), byteImage: img, factor: factor) 371 | } 372 | 373 | public static func mul(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 374 | return op((*), byteImage1: img1, byteImage2: img2) 375 | } 376 | 377 | 378 | public static func div(_ img: ByteImage, factor: Double) -> ByteImage { 379 | if factor == 0.0 { 380 | return img 381 | } 382 | return op((/), byteImage: img, factor: factor) 383 | } 384 | 385 | // 0으로 나누면 안되기 때문에 따로 만듦 386 | public static func div(img1: ByteImage, _ img2: ByteImage) -> ByteImage { 387 | let result : ByteImage = ByteImage(width:img1.width, height: img1.height) 388 | for y in 0.. RGBAImage { 408 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 409 | for y in 0.. RGBAImage { 431 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 432 | for y in 0.. ByteImage { 451 | let result : ByteImage = ByteImage(width:img1.width, height: img1.height) 452 | for y in 0..) -> ByteImage { 469 | var image = image 470 | let height = image.height 471 | let width = image.width 472 | 473 | let maskHeight = mask.rowCount() 474 | let maskWidth = mask.colCount() 475 | 476 | for y in 0.. height) || (x+maskWidth) > width { 480 | continue 481 | } 482 | 483 | for my in 0..> 8) & 0xFF) } 18 | set { 19 | let v = max(min(newValue, 255), 0) 20 | value = (UInt32(v) << 8) | (value & 0xFFFF00FF) 21 | } 22 | } 23 | 24 | //blue 25 | public var B: UInt8 { 26 | get { return UInt8((value >> 16) & 0xFF) } 27 | set { 28 | let v = max(min(newValue, 255), 0) 29 | value = (UInt32(v) << 16) | (value & 0xFF00FFFF) 30 | } 31 | } 32 | 33 | //alpha 34 | public var A: UInt8 { 35 | get { return UInt8((value >> 24) & 0xFF) } 36 | set { 37 | let v = max(min(newValue, 255), 0) 38 | value = (UInt32(v) << 24) | (value & 0x00FFFFFF) 39 | } 40 | } 41 | 42 | public var Rf: Double { 43 | get { return Double(self.R) / 255.0 } 44 | set { 45 | self.R = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 46 | } 47 | } 48 | 49 | public var Gf: Double { 50 | get { return Double(self.G) / 255.0 } 51 | set { 52 | self.G = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 53 | } 54 | } 55 | 56 | public var Bf: Double { 57 | get { return Double(self.B) / 255.0 } 58 | set { 59 | self.B = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 60 | } 61 | } 62 | 63 | public var Af: Double { 64 | get { return Double(self.A) / 255.0 } 65 | set { 66 | self.A = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 67 | } 68 | } 69 | } 70 | 71 | public struct RGBAImage { 72 | public var pixels: UnsafeMutableBufferPointer 73 | public var width: Int 74 | public var height: Int 75 | 76 | public init?(image: UIImage) { 77 | // CGImage로 변환이 가능해야 한다. 78 | guard let cgImage = image.cgImage else { 79 | return nil 80 | } 81 | 82 | // 주소 계산을 위해서 Float을 Int로 저장한다. 83 | width = Int(image.size.width) 84 | height = Int(image.size.height) 85 | 86 | // 4 * width * height 크기의 버퍼를 생성한다. 87 | let bytesPerRow = width * 4 88 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 89 | 90 | // 색상공간은 Device의 것을 따른다 91 | let colorSpace = CGColorSpaceCreateDeviceRGB() 92 | 93 | // BGRA로 비트맵을 만든다 94 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 95 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 96 | 97 | // 비트맵 생성 98 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 99 | return nil 100 | } 101 | 102 | // cgImage를 imageData에 채운다. 103 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 104 | 105 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 106 | } 107 | 108 | 109 | public init(width: Int, height: Int) { 110 | let image = RGBAImage.newUIImage(width: width, height: height) 111 | self.init(image: image)! 112 | } 113 | 114 | public func clone() -> RGBAImage { 115 | let cloneImage = RGBAImage(width: self.width, height: self.height) 116 | for y in 0.. UIImage? { 126 | let colorSpace = CGColorSpaceCreateDeviceRGB() 127 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 128 | let bytesPerRow = width * 4 129 | 130 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 131 | 132 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 133 | return nil 134 | } 135 | 136 | guard let cgImage = imageContext.makeImage() else { 137 | return nil 138 | } 139 | 140 | let image = UIImage(cgImage: cgImage) 141 | return image 142 | } 143 | 144 | public func pixel(x : Int, _ y : Int) -> Pixel? { 145 | guard x >= 0 && x < width && y >= 0 && y < height else { 146 | return nil 147 | } 148 | 149 | let address = y * width + x 150 | return pixels[address] 151 | } 152 | 153 | public mutating func pixel(x : Int, _ y : Int, _ pixel: Pixel) { 154 | guard x >= 0 && x < width && y >= 0 && y < height else { 155 | return 156 | } 157 | 158 | let address = y * width + x 159 | pixels[address] = pixel 160 | } 161 | 162 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 163 | for y in 0.. Void) { 173 | for y in 0.. UIImage { 182 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 183 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 184 | UIColor.black.setFill() 185 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 186 | let image = UIGraphicsGetImageFromCurrentImageContext(); 187 | UIGraphicsEndImageContext(); 188 | return image! 189 | } 190 | } 191 | 192 | -------------------------------------------------------------------------------- /09_Brightness.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /10_Convolution.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | 5 | let monet1 = RGBAImage(image: UIImage(named: "monet1")!)! 6 | let monet2 = RGBAImage(image: UIImage(named: "monet2")!)! 7 | let tiger = RGBAImage(image: UIImage(named: "tiger")!)! 8 | 9 | // Brightness 10 | ImageProcess.brightness(monet1, contrast: 0.8, brightness: 0.5).toUIImage() 11 | 12 | // Split 13 | let (R,G,B) = ImageProcess.splitRGB(monet1) 14 | R.toUIImage() 15 | 16 | // Sharpening 17 | let m1 = Array2D(cols:3, rows:3, elements: 18 | [ 0.0/5.0, -1.0/5.0, 0.0/5.0, 19 | -1.0/5.0, 9.0/5.0,-1.0/5.0, 20 | 0.0/5.0, -1.0/5.0, 0.0/5.0]) 21 | ImageProcess.convolution(R.clone(), mask: m1).toUIImage() 22 | 23 | // Blur3x3 24 | let m2 = Array2D(cols: 3, rows: 3, elements: 25 | [ 26 | 1.0/9.0, 1.0/9.0, 1.0/9.0, 27 | 1.0/9.0, 1.0/9.0, 1.0/9.0, 28 | 1.0/9.0, 1.0/9.0, 1.0/9.0, 29 | ] 30 | ) 31 | ImageProcess.convolution(R.clone(), mask: m2).toUIImage() 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /10_Convolution.playground/Resources/monet1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/10_Convolution.playground/Resources/monet1.png -------------------------------------------------------------------------------- /10_Convolution.playground/Resources/monet2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/10_Convolution.playground/Resources/monet2.png -------------------------------------------------------------------------------- /10_Convolution.playground/Resources/tiger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skyfe79/SwiftImageProcessing/4da1d2911844fe518e3e4b5baed8921db27580f0/10_Convolution.playground/Resources/tiger.png -------------------------------------------------------------------------------- /10_Convolution.playground/Sources/Array2D.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public class Array2D { 4 | 5 | let cols: Int 6 | let rows: Int 7 | public var matrix: [T] 8 | 9 | public init(cols: Int, rows: Int, defaultValue: T) { 10 | self.cols = cols 11 | self.rows = rows 12 | matrix = Array(repeating:defaultValue,count:cols*rows) 13 | } 14 | 15 | public init(cols: Int, rows: Int, elements: [T]) { 16 | self.cols = cols 17 | self.rows = rows 18 | self.matrix = Array(elements) 19 | } 20 | 21 | public subscript(col:Int, row:Int) -> T { 22 | get{ 23 | return matrix[cols * row + col] 24 | } 25 | set{ 26 | matrix[cols * row + col] = newValue 27 | } 28 | } 29 | 30 | public func colCount() -> Int { 31 | return self.cols 32 | } 33 | 34 | public func rowCount() -> Int { 35 | return self.rows 36 | } 37 | 38 | public func withinBounds(x: Int, y: Int) -> Bool { 39 | return x >= 0 && x < cols && y >= 0 && y < rows 40 | } 41 | 42 | public func contains(element: T) -> Bool { 43 | return matrix.index(of: element) != nil 44 | } 45 | 46 | public func indexOf(element: T) -> (col: Int, row: Int)? { 47 | for x in 0.. 34 | public var width: Int 35 | public var height: Int 36 | 37 | public init?(image: UIImage) { 38 | // CGImage로 변환이 가능해야 한다. 39 | guard let cgImage = image.cgImage else { 40 | return nil 41 | } 42 | 43 | // 주소 계산을 위해서 Float을 Int로 저장한다. 44 | width = Int(image.size.width) 45 | height = Int(image.size.height) 46 | 47 | // 1 * width * height 크기의 버퍼를 생성한다. 48 | let bytesPerRow = width * 1 49 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 50 | 51 | // 색상공간은 Device의 것을 따른다 52 | let colorSpace = CGColorSpaceCreateDeviceGray() 53 | 54 | // BGRA로 비트맵을 만든다 55 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 56 | // 비트맵 생성 57 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 58 | return nil 59 | } 60 | 61 | // cgImage를 imageData에 채운다. 62 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 63 | 64 | // 이미지 화소의 배열 주소를 pixels에 담는다 65 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 66 | } 67 | 68 | 69 | public init(width: Int, height: Int) { 70 | let image = ByteImage.newUIImage(width: width, height: height) 71 | self.init(image: image)! 72 | } 73 | 74 | public func clone() -> ByteImage { 75 | let cloneImage = ByteImage(width: self.width, height: self.height) 76 | for y in 0.. UIImage? { 86 | let colorSpace = CGColorSpaceCreateDeviceGray() 87 | let bitmapInfo: UInt32 = CGBitmapInfo().rawValue 88 | let bytesPerRow = width * 1 89 | 90 | 91 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 92 | return nil 93 | } 94 | guard let cgImage = imageContext.makeImage() else { 95 | return nil 96 | } 97 | 98 | let image = UIImage(cgImage: cgImage) 99 | return image 100 | } 101 | 102 | public func pixel(_ x : Int, _ y : Int) -> BytePixel? { 103 | guard x >= 0 && x < width && y >= 0 && y < height else { 104 | return nil 105 | } 106 | 107 | let address = y * width + x 108 | return pixels[address] 109 | } 110 | 111 | public mutating func pixel(_ x : Int, _ y : Int, _ pixel: BytePixel) { 112 | guard x >= 0 && x < width && y >= 0 && y < height else { 113 | return 114 | } 115 | 116 | let address = y * width + x 117 | pixels[address] = pixel 118 | } 119 | 120 | public mutating func setPixel(_ x : Int, _ y : Int, _ pixel: BytePixel) { 121 | guard x >= 0 && x < width && y >= 0 && y < height else { 122 | return 123 | } 124 | 125 | let address = y * width + x 126 | pixels[address] = pixel 127 | } 128 | 129 | public mutating func process(_ functor : ((BytePixel) -> BytePixel) ) { 130 | for y in 0.. UIImage { 139 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 140 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 141 | UIColor.black.setFill() 142 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 143 | let image = UIGraphicsGetImageFromCurrentImageContext(); 144 | UIGraphicsEndImageContext(); 145 | return image! 146 | } 147 | } 148 | 149 | extension UInt8 { 150 | public func toBytePixel() -> BytePixel { 151 | return BytePixel(value: self) 152 | } 153 | } 154 | 155 | 156 | -------------------------------------------------------------------------------- /10_Convolution.playground/Sources/ImageProcess.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | func clamp(_ value: T, lower: T, upper: T) -> T { 4 | return min(max(value, lower), upper) 5 | } 6 | 7 | public class ImageProcess { 8 | 9 | public static func grabR(_ image: RGBAImage) -> RGBAImage { 10 | var outImage = image 11 | outImage.process { (pixel) -> Pixel in 12 | var pixel = pixel 13 | pixel.R = pixel.R 14 | pixel.G = 0 15 | pixel.B = 0 16 | return pixel 17 | } 18 | return outImage 19 | } 20 | 21 | public static func grabG(_ image: RGBAImage) -> RGBAImage { 22 | var outImage = image 23 | outImage.process { (pixel) -> Pixel in 24 | var pixel = pixel 25 | pixel.R = 0 26 | pixel.G = pixel.G 27 | pixel.B = 0 28 | return pixel 29 | } 30 | return outImage 31 | } 32 | 33 | public static func grabB(_ image: RGBAImage) -> RGBAImage { 34 | var outImage = image 35 | outImage.process { (pixel) -> Pixel in 36 | var pixel = pixel 37 | pixel.R = 0 38 | pixel.G = 0 39 | pixel.B = pixel.B 40 | return pixel 41 | } 42 | return outImage 43 | } 44 | 45 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 46 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 47 | for y in 0.. Double, rgbaImageList: RGBAImage...) -> RGBAImage { 67 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 68 | for y in 0.. RGBAImage { 89 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 90 | for y in 0.. Double, byteImageList: ByteImage...) -> RGBAImage { 118 | let result : RGBAImage = RGBAImage(width:byteImageList[0].width, height: byteImageList[0].height) 119 | for y in 0.. RGBAImage { 147 | var outImage = image 148 | outImage.process { (pixel) -> Pixel in 149 | var pixel = pixel 150 | let result = pixel.Rf*0.2999 + pixel.Gf*0.587 + pixel.Bf*0.114 151 | pixel.Rf = result 152 | pixel.Gf = result 153 | pixel.Bf = result 154 | return pixel 155 | } 156 | return outImage 157 | } 158 | 159 | public static func gray2(_ image: RGBAImage) -> RGBAImage { 160 | var outImage = image 161 | outImage.process { (pixel) -> Pixel in 162 | var pixel = pixel 163 | let result = (pixel.Rf + pixel.Gf + pixel.Bf) / 3.0 164 | pixel.Rf = result 165 | pixel.Gf = result 166 | pixel.Bf = result 167 | return pixel 168 | } 169 | return outImage 170 | } 171 | 172 | public static func gray3(_ image: RGBAImage) -> RGBAImage { 173 | var outImage = image 174 | outImage.process { (pixel) -> Pixel in 175 | var pixel = pixel 176 | pixel.R = pixel.G 177 | pixel.G = pixel.G 178 | pixel.B = pixel.G 179 | return pixel 180 | } 181 | return outImage 182 | } 183 | 184 | public static func gray4(_ image: RGBAImage) -> RGBAImage { 185 | var outImage = image 186 | outImage.process { (pixel) -> Pixel in 187 | var pixel = pixel 188 | let result = pixel.Rf*0.212671 + pixel.Gf*0.715160 + pixel.Bf*0.071169 189 | pixel.Rf = result 190 | pixel.Gf = result 191 | pixel.Bf = result 192 | return pixel 193 | } 194 | return outImage 195 | } 196 | 197 | public static func gray5(_ image: RGBAImage) -> RGBAImage { 198 | var outImage = image 199 | outImage.process { (pixel) -> Pixel in 200 | var pixel = pixel 201 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 202 | pixel.Rf = result 203 | pixel.Gf = result 204 | pixel.Bf = result 205 | return pixel 206 | } 207 | return outImage 208 | } 209 | 210 | public static func splitRGB(_ rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) { 211 | let R = ByteImage(width: rgba.width, height: rgba.height) 212 | let G = ByteImage(width: rgba.width, height: rgba.height) 213 | let B = ByteImage(width: rgba.width, height: rgba.height) 214 | 215 | rgba.enumerate { (index, pixel) -> Void in 216 | 217 | R.pixels[index] = pixel.R.toBytePixel() 218 | G.pixels[index] = pixel.G.toBytePixel() 219 | B.pixels[index] = pixel.B.toBytePixel() 220 | } 221 | 222 | return (R, G, B) 223 | } 224 | 225 | // +, -, *, / 226 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage: RGBAImage, factor: Double) -> RGBAImage { 227 | var outImage = rgbaImage 228 | outImage.process { (pixel) -> Pixel in 229 | var pixel = pixel 230 | pixel.Rf = functor(pixel.Rf, factor) 231 | pixel.Gf = functor(pixel.Gf, factor) 232 | pixel.Bf = functor(pixel.Bf, factor) 233 | return pixel 234 | } 235 | return outImage 236 | } 237 | 238 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage1: RGBAImage, rgbaImage2: RGBAImage) -> RGBAImage { 239 | let result : RGBAImage = RGBAImage(width:rgbaImage1.width, height: rgbaImage1.height) 240 | for y in 0.. Double, byteImage: ByteImage, factor: Double) -> ByteImage { 262 | var outImage = byteImage 263 | outImage.process { (pixel) -> BytePixel in 264 | var pixel = pixel 265 | pixel.Cf = functor(pixel.Cf, factor) 266 | return pixel 267 | } 268 | return outImage 269 | } 270 | 271 | public static func op(_ functor : (Double, Double) -> Double, byteImage1: ByteImage, byteImage2: ByteImage) -> ByteImage { 272 | let result : ByteImage = ByteImage(width:byteImage1.width, height: byteImage1.height) 273 | for y in 0.. RGBAImage { 296 | return op((+), rgbaImage: rgba, factor: factor) 297 | } 298 | 299 | public static func add(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 300 | return op((+), rgbaImage1: rgba1, rgbaImage2: rgba2) 301 | } 302 | 303 | public static func sub(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 304 | return op((-), rgbaImage: rgba, factor: factor) 305 | } 306 | 307 | public static func sub(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 308 | return op((-), rgbaImage1: rgba1, rgbaImage2: rgba2) 309 | } 310 | 311 | public static func mul(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 312 | return op((*), rgbaImage: rgba, factor: factor) 313 | } 314 | 315 | public static func mul(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 316 | return op((*), rgbaImage1: rgba1, rgbaImage2: rgba2) 317 | } 318 | 319 | 320 | public static func div(_ rgba: RGBAImage, factor: Double) -> RGBAImage { 321 | if factor == 0.0 { 322 | return rgba 323 | } 324 | return op((/), rgbaImage: rgba, factor: factor) 325 | } 326 | 327 | // 0으로 나누면 안되기 때문에 따로 만듦 328 | public static func div(_ rgba1: RGBAImage, _ rgba2: RGBAImage) -> RGBAImage { 329 | let result : RGBAImage = RGBAImage(width:rgba1.width, height: rgba1.height) 330 | for y in 0.. ByteImage { 354 | return op((+), byteImage: img, factor: factor) 355 | } 356 | 357 | public static func add(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 358 | return op((+), byteImage1: img1, byteImage2: img2) 359 | } 360 | 361 | public static func sub(_ img: ByteImage, factor: Double) -> ByteImage { 362 | return op((-), byteImage: img, factor: factor) 363 | } 364 | 365 | public static func sub(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 366 | return op((-), byteImage1: img1, byteImage2: img2) 367 | } 368 | 369 | public static func mul(_ img: ByteImage, factor: Double) -> ByteImage { 370 | return op((*), byteImage: img, factor: factor) 371 | } 372 | 373 | public static func mul(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 374 | return op((*), byteImage1: img1, byteImage2: img2) 375 | } 376 | 377 | 378 | public static func div(_ img: ByteImage, factor: Double) -> ByteImage { 379 | if factor == 0.0 { 380 | return img 381 | } 382 | return op((/), byteImage: img, factor: factor) 383 | } 384 | 385 | // 0으로 나누면 안되기 때문에 따로 만듦 386 | public static func div(_ img1: ByteImage, _ img2: ByteImage) -> ByteImage { 387 | let result : ByteImage = ByteImage(width:img1.width, height: img1.height) 388 | for y in 0.. RGBAImage { 408 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 409 | for y in 0.. RGBAImage { 431 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 432 | for y in 0.. ByteImage { 451 | let result : ByteImage = ByteImage(width:img1.width, height: img1.height) 452 | for y in 0..) -> ByteImage { 469 | var image = image 470 | let height = image.height 471 | let width = image.width 472 | 473 | let maskHeight = mask.rowCount() 474 | let maskWidth = mask.colCount() 475 | 476 | for y in 0.. height) || (x+maskWidth) > width { 480 | continue 481 | } 482 | 483 | for my in 0..> 8) & 0xFF) } 18 | set { 19 | let v = max(min(newValue, 255), 0) 20 | value = (UInt32(v) << 8) | (value & 0xFFFF00FF) 21 | } 22 | } 23 | 24 | //blue 25 | public var B: UInt8 { 26 | get { return UInt8((value >> 16) & 0xFF) } 27 | set { 28 | let v = max(min(newValue, 255), 0) 29 | value = (UInt32(v) << 16) | (value & 0xFF00FFFF) 30 | } 31 | } 32 | 33 | //alpha 34 | public var A: UInt8 { 35 | get { return UInt8((value >> 24) & 0xFF) } 36 | set { 37 | let v = max(min(newValue, 255), 0) 38 | value = (UInt32(v) << 24) | (value & 0x00FFFFFF) 39 | } 40 | } 41 | 42 | public var Rf: Double { 43 | get { return Double(self.R) / 255.0 } 44 | set { 45 | self.R = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 46 | } 47 | } 48 | 49 | public var Gf: Double { 50 | get { return Double(self.G) / 255.0 } 51 | set { 52 | self.G = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 53 | } 54 | } 55 | 56 | public var Bf: Double { 57 | get { return Double(self.B) / 255.0 } 58 | set { 59 | self.B = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 60 | } 61 | } 62 | 63 | public var Af: Double { 64 | get { return Double(self.A) / 255.0 } 65 | set { 66 | self.A = UInt8(max(min(newValue, 1.0), 0.0) * 255.0) 67 | } 68 | } 69 | } 70 | 71 | public struct RGBAImage { 72 | public var pixels: UnsafeMutableBufferPointer 73 | public var width: Int 74 | public var height: Int 75 | 76 | public init?(image: UIImage) { 77 | // CGImage로 변환이 가능해야 한다. 78 | guard let cgImage = image.cgImage else { 79 | return nil 80 | } 81 | 82 | // 주소 계산을 위해서 Float을 Int로 저장한다. 83 | width = Int(image.size.width) 84 | height = Int(image.size.height) 85 | 86 | // 4 * width * height 크기의 버퍼를 생성한다. 87 | let bytesPerRow = width * 4 88 | let imageData = UnsafeMutablePointer.allocate(capacity: width * height) 89 | 90 | // 색상공간은 Device의 것을 따른다 91 | let colorSpace = CGColorSpaceCreateDeviceRGB() 92 | 93 | // BGRA로 비트맵을 만든다 94 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 95 | bitmapInfo = bitmapInfo | CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 96 | 97 | // 비트맵 생성 98 | guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { 99 | return nil 100 | } 101 | 102 | // cgImage를 imageData에 채운다. 103 | imageContext.draw(cgImage, in: CGRect(origin: .zero, size: image.size)) 104 | 105 | pixels = UnsafeMutableBufferPointer(start: imageData, count: width * height) 106 | } 107 | 108 | 109 | public init(width: Int, height: Int) { 110 | let image = RGBAImage.newUIImage(width: width, height: height) 111 | self.init(image: image)! 112 | } 113 | 114 | public func clone() -> RGBAImage { 115 | let cloneImage = RGBAImage(width: self.width, height: self.height) 116 | for y in 0.. UIImage? { 126 | let colorSpace = CGColorSpaceCreateDeviceRGB() 127 | var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue 128 | let bytesPerRow = width * 4 129 | 130 | bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue 131 | 132 | guard let imageContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil) else { 133 | return nil 134 | } 135 | 136 | guard let cgImage = imageContext.makeImage() else { 137 | return nil 138 | } 139 | 140 | let image = UIImage(cgImage: cgImage) 141 | return image 142 | } 143 | 144 | public func pixel(_ x : Int, _ y : Int) -> Pixel? { 145 | guard x >= 0 && x < width && y >= 0 && y < height else { 146 | return nil 147 | } 148 | 149 | let address = y * width + x 150 | return pixels[address] 151 | } 152 | 153 | public mutating func pixel(_ x : Int, _ y : Int, _ pixel: Pixel) { 154 | guard x >= 0 && x < width && y >= 0 && y < height else { 155 | return 156 | } 157 | 158 | let address = y * width + x 159 | pixels[address] = pixel 160 | } 161 | 162 | public mutating func process( functor : ((Pixel) -> Pixel) ) { 163 | for y in 0.. Void) { 173 | for y in 0.. UIImage { 182 | let size = CGSize(width: CGFloat(width), height: CGFloat(height)); 183 | UIGraphicsBeginImageContextWithOptions(size, true, 0); 184 | UIColor.black.setFill() 185 | UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 186 | let image = UIGraphicsGetImageFromCurrentImageContext(); 187 | UIGraphicsEndImageContext(); 188 | return image! 189 | } 190 | } 191 | 192 | -------------------------------------------------------------------------------- /10_Convolution.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Sungcheol Kim 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 | # Swift Image Processing 2 | 3 | This project contains swift playgrounds that demonstrate how to do pixel operations in swift. 4 | 5 | * Swift4 : checkout master branch 6 | * Swift3.x : checkout syntax/swift3.x branch 7 | * Swift2.x : checkout syntax/swift2.x branch 8 | 9 | ## Thanks to RGBAImage 10 | 11 | * [http://mhorga.org/2015/10/19/image-processing-in-ios-part-3.html](http://mhorga.org/2015/10/19/image-processing-in-ios-part-3.html) 12 | * This articles help me a lot! 13 | 14 | ## Convert UIImage to RGBA Image 15 | RGBAImage has pixels flat memory. You can access pixels with index directly. 16 | 17 | ![](art/01_rgba_image.png) 18 | 19 | ## Contrast 20 | This is example for pixel operation 21 | 22 | ![](art/02_contrast.png) 23 | 24 | ```swift 25 | let rgba = RGBAImage(image: UIImage(named: "monet")!)! 26 | 27 | 28 | var totalR = 0 29 | var totalG = 0 30 | var totalB = 0 31 | 32 | rgba.process { (pixel) -> Pixel in 33 | totalR += Int(pixel.R) 34 | totalG += Int(pixel.G) 35 | totalB += Int(pixel.B) 36 | return pixel 37 | } 38 | 39 | let pixelCount = rgba.width * rgba.height 40 | let avgR = totalR / pixelCount 41 | let avgG = totalG / pixelCount 42 | let avgB = totalB / pixelCount 43 | 44 | 45 | 46 | func contrast(_ image: RGBAImage) -> RGBAImage { 47 | 48 | image.process { (pixel) -> Pixel in 49 | var pixel = pixel 50 | let deltaR = Int(pixel.R) - avgR 51 | let deltaG = Int(pixel.G) - avgG 52 | let deltaB = Int(pixel.B) - avgB 53 | pixel.R = UInt8(max(min(255, avgR + 3 * deltaR), 0)) //clamp 54 | pixel.G = UInt8(max(min(255, avgG + 3 * deltaG), 0)) 55 | pixel.B = UInt8(max(min(255, avgB + 3 * deltaB), 0)) 56 | 57 | return pixel 58 | } 59 | return image 60 | } 61 | 62 | let newImage = contrast(rgba).toUIImage() 63 | ``` 64 | ## Grab color space 65 | 66 | ### Grab Red component 67 | ![](art/03_grab_r.png) 68 | 69 | ```swift 70 | func grabR(_ image: RGBAImage) -> RGBAImage { 71 | var outImage = image 72 | outImage.process { (pixel) -> Pixel in 73 | var pixel = pixel 74 | pixel.R = pixel.R 75 | pixel.G = 0 76 | pixel.B = 0 77 | return pixel 78 | } 79 | return outImage 80 | } 81 | ``` 82 | 83 | ### Grab Green component 84 | 85 | ![](art/04_grab_g.png) 86 | 87 | ```swift 88 | func grabG(_ image: RGBAImage) -> RGBAImage { 89 | var outImage = image 90 | outImage.process { (pixel) -> Pixel in 91 | var pixel = pixel 92 | pixel.R = 0 93 | pixel.G = pixel.G 94 | pixel.B = 0 95 | return pixel 96 | } 97 | return outImage 98 | } 99 | ``` 100 | ### Grab Blue component 101 | 102 | ![](art/05_grab_b.png) 103 | 104 | ```swift 105 | func grabB(_ image: RGBAImage) -> RGBAImage { 106 | var outImage = image 107 | outImage.process { (pixel) -> Pixel in 108 | var pixel = pixel 109 | pixel.R = 0 110 | pixel.G = 0 111 | pixel.B = pixel.B 112 | return pixel 113 | } 114 | return outImage 115 | } 116 | ``` 117 | ### Compose RGB Color components 118 | 119 | ![](art/06_composite.png) 120 | 121 | ``` 122 | public static func composite(_ rgbaImageList: RGBAImage...) -> RGBAImage { 123 | let result : RGBAImage = RGBAImage(width:rgbaImageList[0].width, height: rgbaImageList[0].height) 124 | for y in 0.. RGBAImage { 149 | var outImage = image 150 | outImage.process { (pixel) -> Pixel in 151 | var pixel = pixel 152 | let result = sqrt(pow(pixel.Rf, 2) + pow(pixel.Rf, 2) + pow(pixel.Rf, 2))/sqrt(3.0) 153 | pixel.Rf = result 154 | pixel.Gf = result 155 | pixel.Bf = result 156 | return pixel 157 | } 158 | return outImage 159 | } 160 | let rgba5 = RGBAImage(image: UIImage(named: "monet")!)! 161 | gray5(rgba5).toUIImage() 162 | ``` 163 | 164 | ## Refactoring Split Color Space 165 | ![](art/08_split.png) 166 | 167 | ```swift 168 | public static func splitRGB(_ rgba: RGBAImage) -> (ByteImage, ByteImage, ByteImage) { 169 | let R = ByteImage(width: rgba.width, height: rgba.height) 170 | let G = ByteImage(width: rgba.width, height: rgba.height) 171 | let B = ByteImage(width: rgba.width, height: rgba.height) 172 | 173 | rgba.enumerate { (index, pixel) -> Void in 174 | 175 | R.pixels[index] = pixel.R.toBytePixel() 176 | G.pixels[index] = pixel.G.toBytePixel() 177 | B.pixels[index] = pixel.B.toBytePixel() 178 | } 179 | 180 | return (R, G, B) 181 | } 182 | ``` 183 | `ByteImage` has only one color component. 184 | 185 | ## Images ADD, SUB, MUL, DIV 186 | ![](art/09_add_sub_mul_div.png) 187 | 188 | ```swift 189 | public static func op(_ functor : (Double, Double) -> Double, rgbaImage1: RGBAImage, rgbaImage2: RGBAImage) -> RGBAImage { 190 | let result : RGBAImage = RGBAImage(width:rgbaImage1.width, height: rgbaImage1.height) 191 | for y in 0.. RGBAImage { 212 | return op((+), rgbaImage1: rgba1, rgbaImage2: rgba2) 213 | } 214 | ``` 215 | 216 | ![](art/10_add_sub_mul_div.png) 217 | 218 | ## Blending 219 | ![](art/11_blending.png) 220 | 221 | ```swift 222 | public static func blending(_ img1: RGBAImage, _ img2: RGBAImage, alpha: Double) -> RGBAImage { 223 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 224 | for y in 0.. RGBAImage { 250 | let result : RGBAImage = RGBAImage(width:img1.width, height: img1.height) 251 | for y in 0..) -> ByteImage { 274 | var image = image 275 | let height = image.height 276 | let width = image.width 277 | 278 | let maskHeight = mask.rowCount() 279 | let maskWidth = mask.colCount() 280 | 281 | for y in 0.. height) || (x+maskWidth) > width { 285 | continue 286 | } 287 | 288 | for my in 0..