From 9c613ca60c88d9825d4fe4c6abcdfae77c363ed3 Mon Sep 17 00:00:00 2001 From: ChrisMJSong Date: Thu, 5 Apr 2018 14:45:23 +0900 Subject: [PATCH 1/7] feat: Create UIImage from raw RGBA Add new feature create UIImage from RGBA raw binary data. --- Sources/ImageHelper.swift | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Sources/ImageHelper.swift b/Sources/ImageHelper.swift index 911fba3..61fe1b5 100644 --- a/Sources/ImageHelper.swift +++ b/Sources/ImageHelper.swift @@ -202,6 +202,45 @@ public extension UIImage { UIGraphicsEndImageContext() } + // MARK: Image from raw RGBA data + /** + Creates a radial gradient. + + - Parameter rgbaData: raw RGBA data + - Parameter size: Image size + + - Returns A new image + */ + convenience init?(rgbaData: [UInt8], size: CGSize) { + let bitsPerComponent:Int = 8 + let bitsPerPixel:Int = 32 + let rgbaSize = 4 + let bitsPerRow:Int = Int(CGFloat(rgbaSize) * size.width) + + let colorSpace = CGColorSpaceCreateDeviceRGB() + let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) + let data = NSData.init(bytes: rgbaData, length: Int(size.width * size.height * CGFloat(rgbaSize))) + let provider = CGDataProvider.init(data: data)! + let renderingIntent = CGColorRenderingIntent.defaultIntent + + let cgim = CGImage( + width: Int(size.width), + height: Int(size.height), + bitsPerComponent: bitsPerComponent, + bitsPerPixel: bitsPerPixel, + bytesPerRow: bitsPerRow, + space: colorSpace, + bitmapInfo: bitmapInfo, + provider: provider, + decode: nil, + shouldInterpolate: true, + intent: renderingIntent + ) + + // Draw it + self.init(cgImage:cgim!) + } + // MARK: Alpha /** Returns true if the image has an alpha layer. From 73aeb709452454c3cdc8a5d6418562c7ca59d25d Mon Sep 17 00:00:00 2001 From: ChrisMJSong Date: Thu, 5 Apr 2018 14:46:01 +0900 Subject: [PATCH 2/7] feat: iOS code sample that create UIImage from RGBA raw iOS code sample that create UIImage from RGBA raw bianary data. It will look like a video test patter. --- Demo iOS/ViewController.swift | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Demo iOS/ViewController.swift b/Demo iOS/ViewController.swift index 0b5c934..134dd5c 100644 --- a/Demo iOS/ViewController.swift +++ b/Demo iOS/ViewController.swift @@ -106,6 +106,40 @@ class ViewController: UICollectionViewController { } items.append(effects) + // Raw Image + sections.append("Raw to Image") + var raw = [CellItem]() + let size = CGSize.init(width: 100, height: 100) + var rgba: Array = Array.init(repeating: 0, count: Int(size.width * size.height * 4)) + for y in 0.. 100 / 3 * 2 { + let unit = UInt(x / 10) * 10 + let value = UInt8(Double(unit) / 100.0 * 255) + r = value + g = value + b = value + } else { + r = x < 40 ? 255 : 0 + g = (x > 20 && x < 80) ? 255 : 0 + b = x > 60 ? 255 : 0 + } + + rgba[(y * Int(size.width) + x) * 4] = r + rgba[(y * Int(size.width) + x) * 4 + 1] = g + rgba[(y * Int(size.width) + x) * 4 + 2] = b + rgba[(y * Int(size.width) + x) * 4 + 3] = 255 + } + } + if let image = UIImage(rgbaData: rgba, size: size) { + raw.append(CellItem(text: "RGBA", image: image)) + } + items.append(raw) + // Web Image sections.append("Web Image") var web = [CellItem]() From 7d1bd6d0bb6f08b5969c1fbde7c605b99f83d272 Mon Sep 17 00:00:00 2001 From: ChrisMJSong Date: Thu, 5 Apr 2018 15:37:48 +0900 Subject: [PATCH 3/7] refactor: Replace parameter name replace function parameter variable name. because to give unity with grayscale function parameter. --- Demo iOS/ViewController.swift | 2 +- Sources/ImageHelper.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Demo iOS/ViewController.swift b/Demo iOS/ViewController.swift index 134dd5c..89c8240 100644 --- a/Demo iOS/ViewController.swift +++ b/Demo iOS/ViewController.swift @@ -135,7 +135,7 @@ class ViewController: UICollectionViewController { rgba[(y * Int(size.width) + x) * 4 + 3] = 255 } } - if let image = UIImage(rgbaData: rgba, size: size) { + if let image = UIImage(rgbaBuffer: rgba, size: size) { raw.append(CellItem(text: "RGBA", image: image)) } items.append(raw) diff --git a/Sources/ImageHelper.swift b/Sources/ImageHelper.swift index 61fe1b5..b756a14 100644 --- a/Sources/ImageHelper.swift +++ b/Sources/ImageHelper.swift @@ -211,7 +211,7 @@ public extension UIImage { - Returns A new image */ - convenience init?(rgbaData: [UInt8], size: CGSize) { + convenience init?(rgbaBuffer: [UInt8], size: CGSize) { let bitsPerComponent:Int = 8 let bitsPerPixel:Int = 32 let rgbaSize = 4 @@ -219,7 +219,7 @@ public extension UIImage { let colorSpace = CGColorSpaceCreateDeviceRGB() let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) - let data = NSData.init(bytes: rgbaData, length: Int(size.width * size.height * CGFloat(rgbaSize))) + let data = NSData.init(bytes: rgbaBuffer, length: Int(size.width * size.height * CGFloat(rgbaSize))) let provider = CGDataProvider.init(data: data)! let renderingIntent = CGColorRenderingIntent.defaultIntent From 144bc8e94493fb3c4bd0dc4223ab73fe25f0383d Mon Sep 17 00:00:00 2001 From: ChrisMJSong Date: Thu, 5 Apr 2018 15:39:17 +0900 Subject: [PATCH 4/7] comment: Replace wrong function comment replace wrong function comment and add a comment of line for rgba example code. --- Demo iOS/ViewController.swift | 1 + Sources/ImageHelper.swift | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Demo iOS/ViewController.swift b/Demo iOS/ViewController.swift index 89c8240..338a2b0 100644 --- a/Demo iOS/ViewController.swift +++ b/Demo iOS/ViewController.swift @@ -110,6 +110,7 @@ class ViewController: UICollectionViewController { sections.append("Raw to Image") var raw = [CellItem]() let size = CGSize.init(width: 100, height: 100) + // RGBA var rgba: Array = Array.init(repeating: 0, count: Int(size.width * size.height * 4)) for y in 0.. Date: Thu, 5 Apr 2018 15:41:17 +0900 Subject: [PATCH 5/7] feat: Create UIImage from raw mono Add new feature create UIImage from raw Mono binary data. --- Sources/ImageHelper.swift | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Sources/ImageHelper.swift b/Sources/ImageHelper.swift index a7fd881..9dbcc0b 100644 --- a/Sources/ImageHelper.swift +++ b/Sources/ImageHelper.swift @@ -241,6 +241,44 @@ public extension UIImage { self.init(cgImage:cgim!) } + // MARK: Image from raw mono data + /** + Creates a Grayscale image from raw mono. + + - Parameter rgbaData: raw mono data + - Parameter size: Image size + + - Returns A new image + */ + convenience init?(grayBuffer: [UInt8], size: CGSize) { + let bitsPerComponent:Int = 8 + let bitsPerPixel:Int = 8 + let bitsPerRow:Int = Int(size.width) + + let colorSpace = CGColorSpaceCreateDeviceGray() + let bitmapInfo = CGBitmapInfo() + let data = NSData.init(bytes: grayBuffer, length: Int(size.width * size.height)) + let provider = CGDataProvider.init(data: data)! + let renderingIntent = CGColorRenderingIntent.defaultIntent + + let cgim = CGImage( + width: Int(size.width), + height: Int(size.height), + bitsPerComponent: bitsPerComponent, + bitsPerPixel: bitsPerPixel, + bytesPerRow: bitsPerRow, + space: colorSpace, + bitmapInfo: bitmapInfo, + provider: provider, + decode: nil, + shouldInterpolate: true, + intent: renderingIntent + ) + + // Draw it + self.init(cgImage:cgim!) + } + // MARK: Alpha /** Returns true if the image has an alpha layer. From 4dad45cfbc122f1f7802d7d4318894a6ffa1d44f Mon Sep 17 00:00:00 2001 From: ChrisMJSong Date: Thu, 5 Apr 2018 15:43:33 +0900 Subject: [PATCH 6/7] feat: iOS code sample that create UIImage from raw Mono iOS code sample that create UIImage from raw Mono bianary data. It will look like a checker. --- Demo iOS/ViewController.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Demo iOS/ViewController.swift b/Demo iOS/ViewController.swift index 338a2b0..96c0d4f 100644 --- a/Demo iOS/ViewController.swift +++ b/Demo iOS/ViewController.swift @@ -139,6 +139,19 @@ class ViewController: UICollectionViewController { if let image = UIImage(rgbaBuffer: rgba, size: size) { raw.append(CellItem(text: "RGBA", image: image)) } + + // Grayscale + var gray: Array = Array.init(repeating: 0, count: Int(size.width * size.height)) + for y in 0.. Date: Thu, 5 Apr 2018 16:57:07 +0900 Subject: [PATCH 7/7] Set theme jekyll-theme-minimal --- _config.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 _config.yml diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..2f7efbe --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-minimal \ No newline at end of file