Skip to content

Commit

Permalink
code formattting.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobitech committed Aug 19, 2024
1 parent 91319fc commit 4bfd006
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 176 deletions.
129 changes: 0 additions & 129 deletions ML/CGImage+CVPixelBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,6 @@ import CoreGraphics
import CoreImage
import VideoToolbox

extension CGImage {
/**
Converts the image to an ARGB `CVPixelBuffer`.
*/
public func pixelBuffer() -> CVPixelBuffer? {
return pixelBuffer(width: width, height: height, orientation: .up)
}

/**
Resizes the image to `width` x `height` and converts it to an ARGB
`CVPixelBuffer`.
*/
public func pixelBuffer(width: Int, height: Int,
orientation: CGImagePropertyOrientation) -> CVPixelBuffer? {
return pixelBuffer(width: width, height: height,
pixelFormatType: kCVPixelFormatType_32ARGB,
colorSpace: CGColorSpaceCreateDeviceRGB(),
alphaInfo: .noneSkipFirst,
orientation: orientation)
}

/**
Converts the image to a grayscale `CVPixelBuffer`.
*/
public func pixelBufferGray() -> CVPixelBuffer? {
return pixelBufferGray(width: width, height: height, orientation: .up)
}

/**
Resizes the image to `width` x `height` and converts it to a grayscale
`CVPixelBuffer`.
*/
public func pixelBufferGray(width: Int, height: Int,
orientation: CGImagePropertyOrientation) -> CVPixelBuffer? {
return pixelBuffer(width: width, height: height,
pixelFormatType: kCVPixelFormatType_OneComponent8,
colorSpace: CGColorSpaceCreateDeviceGray(),
alphaInfo: .none,
orientation: orientation)
}

/**
Resizes the image to `width` x `height` and converts it to a `CVPixelBuffer`
with the specified pixel format, color space, and alpha channel.
*/
public func pixelBuffer(width: Int, height: Int,
pixelFormatType: OSType,
colorSpace: CGColorSpace,
alphaInfo: CGImageAlphaInfo,
orientation: CGImagePropertyOrientation) -> CVPixelBuffer? {

// TODO: If the orientation is not .up, then rotate the CGImage.
// See also: https://stackoverflow.com/a/40438893/
assert(orientation == .up)

var maybePixelBuffer: CVPixelBuffer?
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue]
let status = CVPixelBufferCreate(kCFAllocatorDefault,
width,
height,
pixelFormatType,
attrs as CFDictionary,
&maybePixelBuffer)

guard status == kCVReturnSuccess, let pixelBuffer = maybePixelBuffer else {
return nil
}

let flags = CVPixelBufferLockFlags(rawValue: 0)
guard kCVReturnSuccess == CVPixelBufferLockBaseAddress(pixelBuffer, flags) else {
return nil
}
defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, flags) }

guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: colorSpace,
bitmapInfo: alphaInfo.rawValue)
else {
return nil
}

context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
return pixelBuffer
}
}

extension CGImage {
/**
Creates a new CGImage from a CVPixelBuffer.
Expand All @@ -105,42 +14,4 @@ extension CGImage {
VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &cgImage)
return cgImage
}

/*
// Alternative implementation:
public static func create(pixelBuffer: CVPixelBuffer) -> CGImage? {
// This method creates a bitmap CGContext using the pixel buffer's memory.
// It currently only handles kCVPixelFormatType_32ARGB images. To support
// other pixel formats too, you'll have to change the bitmapInfo and maybe
// the color space for the CGContext.

guard kCVReturnSuccess == CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) else {
return nil
}
defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly) }

if let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
width: CVPixelBufferGetWidth(pixelBuffer),
height: CVPixelBufferGetHeight(pixelBuffer),
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue),
let cgImage = context.makeImage() {
return cgImage
} else {
return nil
}
}
*/

/**
Creates a new CGImage from a CVPixelBuffer, using Core Image.
*/
public static func create(pixelBuffer: CVPixelBuffer, context: CIContext) -> CGImage? {
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
let rect = CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer),
height: CVPixelBufferGetHeight(pixelBuffer))
return context.createCGImage(ciImage, from: rect)
}
}
4 changes: 2 additions & 2 deletions ML/ImageClassifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ModelImageClassifier {
}

let boundingBox = face.boundingBox

let size = CGSize(
width: boundingBox.width * image.size.width,
height: boundingBox.height * image.size.height
Expand All @@ -71,7 +71,7 @@ class ModelImageClassifier {
x: boundingBox.minX * image.size.width,
y: (1 - boundingBox.minY) * image.size.height - size.height
)

let faceRect = CGRect(origin: origin, size: size)

guard let croppedImage = cgImage.cropping(to: faceRect) else {
Expand Down
45 changes: 0 additions & 45 deletions ML/UIImage+CVPixelBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ import UIKit
import VideoToolbox

extension UIImage {
/**
Converts the image to an ARGB `CVPixelBuffer`.
*/
public func pixelBuffer() -> CVPixelBuffer? {
return pixelBuffer(width: Int(size.width), height: Int(size.height))
}

/**
Resizes the image to `width` x `height` and converts it to an ARGB
`CVPixelBuffer`.
Expand All @@ -20,24 +13,6 @@ extension UIImage {
alphaInfo: .noneSkipFirst)
}

/**
Converts the image to a grayscale `CVPixelBuffer`.
*/
public func pixelBufferGray() -> CVPixelBuffer? {
return pixelBufferGray(width: Int(size.width), height: Int(size.height))
}

/**
Resizes the image to `width` x `height` and converts it to a grayscale
`CVPixelBuffer`.
*/
public func pixelBufferGray(width: Int, height: Int) -> CVPixelBuffer? {
return pixelBuffer(width: width, height: height,
pixelFormatType: kCVPixelFormatType_OneComponent8,
colorSpace: CGColorSpaceCreateDeviceGray(),
alphaInfo: .none)
}

/**
Resizes the image to `width` x `height` and converts it to a `CVPixelBuffer`
with the specified pixel format, color space, and alpha channel.
Expand Down Expand Up @@ -101,24 +76,4 @@ extension UIImage {
return nil
}
}

/*
// Alternative implementation:
public convenience init?(pixelBuffer: CVPixelBuffer) {
// This converts the image to a CIImage first and then to a UIImage.
// Does not appear to work on the simulator but is OK on the device.
self.init(ciImage: CIImage(cvPixelBuffer: pixelBuffer))
}
*/

/**
Creates a new UIImage from a CVPixelBuffer, using a Core Image context.
*/
public convenience init?(pixelBuffer: CVPixelBuffer, context: CIContext) {
if let cgImage = CGImage.create(pixelBuffer: pixelBuffer, context: context) {
self.init(cgImage: cgImage)
} else {
return nil
}
}
}

0 comments on commit 4bfd006

Please sign in to comment.