Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create UIImage from raw data. #31

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Demo iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,54 @@ class ViewController: UICollectionViewController {
}
items.append(effects)

// Raw Image
sections.append("Raw to Image")
var raw = [CellItem]()
let size = CGSize.init(width: 100, height: 100)
// RGBA
var rgba: Array<UInt8> = Array.init(repeating: 0, count: Int(size.width * size.height * 4))
for y in 0..<Int(size.height) {
for x in 0..<Int(size.width) {
var r: UInt8 = 0
var g: UInt8 = 0
var b: UInt8 = 0

if y > 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(rgbaBuffer: rgba, size: size) {
raw.append(CellItem(text: "RGBA", image: image))
}

// Grayscale
var gray: Array<UInt8> = Array.init(repeating: 0, count: Int(size.width * size.height))
for y in 0..<Int(size.height) {
for x in 0..<Int(size.width) {
let unitSize: Int = 20
let buf: UInt8 = (((x / unitSize) % 2) != ((y / unitSize) % 2)) == true ? 255 : 0
gray[y * Int(size.width) + x] = buf
}
}
if let image = UIImage(grayBuffer: gray, size: size) {
raw.append(CellItem(text: "Grayscale", image: image))
}
items.append(raw)

// Web Image
sections.append("Web Image")
var web = [CellItem]()
Expand Down
77 changes: 77 additions & 0 deletions Sources/ImageHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,83 @@ public extension UIImage {
UIGraphicsEndImageContext()
}

// MARK: Image from raw RGBA data
/**
Creates a RGB image from raw RGBA.

- Parameter rgbaData: raw RGBA data
- Parameter size: Image size

- Returns A new image
*/
convenience init?(rgbaBuffer: [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: rgbaBuffer, 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: 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.
Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-minimal