Skip to content

Instantly share code, notes, and snippets.

@btomtom5
btomtom5 / pixelBufferDraw.swift
Last active December 6, 2018 03:03
PixelBuffer Operations
func drawSolidCircle(imageBuffer: CVPixelBuffer, pnt: (Int, Int), radius: Int) {
/*
Marks CVPixelBuffer with a solid green circle of radius r
imageBuffer: CVPixelBuffer needs to be already locked and available for write access.
*/
CVPixelBufferLockBaseAddress(imageBuffer, .init(rawValue: 0))
let width: Int = CVPixelBufferGetWidth(imageBuffer)
let height: Int = CVPixelBufferGetHeight(imageBuffer)
@btomtom5
btomtom5 / rotate.swift
Created May 25, 2018 17:39
Properly rotate the underlying cgImage of a UIimage given its orientation
func fixOrientation(uiImage: UIImage) -> UIImage{
// No-op if the orientation is already correct
if ( uiImage.imageOrientation == UIImageOrientation.up ) {
return UIImage(cgImage: uiImage.cgImage!, scale: uiImage.scale, orientation: uiImage.imageOrientation)
}
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
var transform: CGAffineTransform = CGAffineTransform.identity
@btomtom5
btomtom5 / convert.swift
Created May 25, 2018 17:37
Convert UIImage from CVPIxelBuffer
func getImage(cvPixelBuffer: CVPixelBuffer) -> UIImage{
CVPixelBufferLockBaseAddress(cvPixelBuffer, CVPixelBufferLockFlags.readOnly)
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceGray()
let bitsPerComponent = 8 // Look at the quartz 2-D programming guide(Graphic Context)
let targetWidth = CVPixelBufferGetWidthOfPlane(cvPixelBuffer, 0)
let targetHeight = CVPixelBufferGetHeightOfPlane(cvPixelBuffer, 0)
let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(cvPixelBuffer, 0)
let baseAddress = CVPixelBufferGetBaseAddressOfPlane(cvPixelBuffer, 0)