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

remove byte reordering #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
174 changes: 84 additions & 90 deletions Sources/Alloy/Core/Extensions/Metal/MTLTexture+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,137 +11,131 @@ public extension MTLTexture {
typealias XImage = NSImage
#endif

func cgImage(colorSpace: CGColorSpace? = nil) throws -> CGImage {
func cgImage(colorSpace: CGColorSpace? = nil,
useAlpha: Bool? = false) throws -> CGImage {
guard self.isAccessibleOnCPU
else { throw MetalError.MTLTextureError.imageCreationFailed }

switch self.pixelFormat {
case .a8Unorm, .r8Unorm, .r8Uint:
let rowBytes = self.width
let length = rowBytes * self.height
let componentsPerPixel = 1
let bytesPerComponent = MemoryLayout<UInt8>.stride
let bytesPerPixel = bytesPerComponent * componentsPerPixel
let bytesPerRow = self.width * bytesPerPixel

let rgbaBytes = UnsafeMutableRawPointer.allocate(byteCount: length,
alignment: MemoryLayout<UInt8>.alignment)
defer { rgbaBytes.deallocate() }
self.getBytes(rgbaBytes,
bytesPerRow: rowBytes,
let bitsPerComponent = bytesPerComponent * 8
let bitsPerPixel = bytesPerPixel * 8

let length = bytesPerRow * self.height

let bytes = UnsafeMutableRawPointer.allocate(byteCount: length,
alignment: MemoryLayout<UInt8>.alignment)
defer { bytes.deallocate() }
self.getBytes(bytes,
bytesPerRow: bytesPerRow,
from: self.region,
mipmapLevel: 0)

let colorScape = colorSpace ?? CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: self.pixelFormat == .a8Unorm
? CGImageAlphaInfo.alphaOnly.rawValue
: CGImageAlphaInfo.none.rawValue)
let colorSpace = colorSpace ?? CGColorSpaceCreateDeviceGray()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
guard let data = CFDataCreate(nil,
rgbaBytes.assumingMemoryBound(to: UInt8.self),
bytes.assumingMemoryBound(to: UInt8.self),
length),
let dataProvider = CGDataProvider(data: data),
let cgImage = CGImage(width: self.width,
height: self.height,
bitsPerComponent: 8,
bitsPerPixel: 8,
bytesPerRow: rowBytes,
space: colorScape,
bitmapInfo: bitmapInfo,
provider: dataProvider,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent)
let dataProvider = CGDataProvider(data: data)
else { throw MetalError.MTLTextureError.imageCreationFailed }

let createdCGImage: CGImage?
if useAlpha == true {
createdCGImage = CGImage(maskWidth: self.width,
height: self.height,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: bytesPerRow,
provider: dataProvider,
decode: nil,
shouldInterpolate: true)
} else {
createdCGImage = CGImage(width: self.width,
height: self.height,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: bytesPerRow,
space: colorSpace,
bitmapInfo: bitmapInfo,
provider: dataProvider,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent)
}

guard let cgImage = createdCGImage
else { throw MetalError.MTLTextureError.imageCreationFailed }

return cgImage
case .bgra8Unorm, .bgra8Unorm_srgb:
// read texture as byte array
let rowBytes = self.width * 4
let length = rowBytes * self.height
case .rgba8Unorm, .rgba8Unorm_srgb, .bgra8Unorm, .bgra8Unorm_srgb:
let componentsPerPixel = 4
let bytesPerComponent = MemoryLayout<UInt8>.stride
let bytesPerPixel = bytesPerComponent * componentsPerPixel
let bytesPerRow = self.width * bytesPerPixel

let bgraBytes = UnsafeMutableRawPointer.allocate(byteCount: length,
alignment: MemoryLayout<UInt8>.alignment)
defer { bgraBytes.deallocate() }
let bitsPerComponent = bytesPerComponent * 8
let bitsPerPixel = bytesPerPixel * 8

self.getBytes(bgraBytes,
bytesPerRow: rowBytes,
from: self.region,
mipmapLevel: 0)
let length = bytesPerRow * self.height

// use Accelerate framework to convert from BGRA to RGBA
var bgraBuffer = vImage_Buffer(data: bgraBytes,
height: vImagePixelCount(self.height),
width: vImagePixelCount(self.width),
rowBytes: rowBytes)
let bytes = UnsafeMutableRawPointer.allocate(byteCount: length,
alignment: MemoryLayout<UInt8>.alignment)
defer { bytes.deallocate() }

let rgbaBytes = UnsafeMutableRawPointer.allocate(byteCount: length,
alignment: MemoryLayout<UInt8>.alignment)
defer { rgbaBytes.deallocate() }
var rgbaBuffer = vImage_Buffer(data: rgbaBytes,
height: vImagePixelCount(self.height),
width: vImagePixelCount(self.width),
rowBytes: rowBytes)
let map: [UInt8] = [2, 1, 0, 3]
vImagePermuteChannels_ARGB8888(&bgraBuffer,
&rgbaBuffer,
map, 0)
self.getBytes(bytes,
bytesPerRow: bytesPerRow,
from: self.region,
mipmapLevel: 0)

// create CGImage with RGBA Flipped Bytes
let colorScape = colorSpace ?? CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
guard let data = CFDataCreate(nil,
rgbaBytes.assumingMemoryBound(to: UInt8.self),
length),
let dataProvider = CGDataProvider(data: data),
let cgImage = CGImage(width: self.width,
height: self.height,
bitsPerComponent: 8,
bitsPerPixel: 32,
bytesPerRow: rowBytes,
space: colorScape,
bitmapInfo: bitmapInfo,
provider: dataProvider,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent)
else { throw MetalError.MTLTextureError.imageCreationFailed }

return cgImage
case .rgba8Unorm, .rgba8Unorm_srgb:
let rowBytes = self.width * 4
let length = rowBytes * self.height
let byteOrderInfo: CGImageByteOrderInfo
if self.pixelFormat == .bgra8Unorm || self.pixelFormat == .bgra8Unorm_srgb {
byteOrderInfo = .order32Little
} else {
byteOrderInfo = .order32Big
}

let rgbaBytes = UnsafeMutableRawPointer.allocate(byteCount: length,
alignment: MemoryLayout<UInt8>.alignment)
defer { rgbaBytes.deallocate() }
let alphaInfo: CGImageAlphaInfo
if let useAlpha = useAlpha {
alphaInfo = useAlpha ? .last : .noneSkipLast
} else {
alphaInfo = .premultipliedLast
}

self.getBytes(rgbaBytes,
bytesPerRow: rowBytes,
from: self.region,
mipmapLevel: 0)
let bitmapInfo = CGBitmapInfo(rawValue: byteOrderInfo.rawValue | alphaInfo.rawValue)

let colorScape = colorSpace ?? CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
guard let data = CFDataCreate(nil,
rgbaBytes.assumingMemoryBound(to: UInt8.self),
bytes.assumingMemoryBound(to: UInt8.self),
length),
let dataProvider = CGDataProvider(data: data),
let cgImage = CGImage(width: self.width,
height: self.height,
bitsPerComponent: 8,
bitsPerPixel: 32,
bytesPerRow: rowBytes,
bitsPerComponent: bitsPerComponent,
bitsPerPixel: bitsPerPixel,
bytesPerRow: bytesPerRow,
space: colorScape,
bitmapInfo: bitmapInfo,
provider: dataProvider,
decode: nil,
shouldInterpolate: true,
intent: .defaultIntent)
else { throw MetalError.MTLTextureError.imageCreationFailed }

return cgImage
default: throw MetalError.MTLTextureError.imageIncompatiblePixelFormat
}
}

func image(colorSpace: CGColorSpace? = nil) throws -> XImage {
let cgImage = try self.cgImage(colorSpace: colorSpace)
func image(colorSpace: CGColorSpace? = nil,
useAlpha: Bool? = false) throws -> XImage {
let cgImage = try self.cgImage(colorSpace: colorSpace,
useAlpha: useAlpha)
#if os(iOS)
return UIImage(cgImage: cgImage)
#elseif os(macOS)
Expand Down