LiveApple/LiveApple/Frame.swift

33 lines
967 B
Swift

//
// Frame.swift
// LiveApple
//
// Created by Shadowfacts on 9/24/22.
//
import Foundation
import CoreGraphics
// a frame of video, encoded as 1 pit per pixel
struct Frame: Codable, Hashable {
// each UInt64 is a row
let data: [UInt64]
let width: Int
var height: Int { data.count }
func createImage() -> CGImage {
var data = [UInt32](repeating: 0, count: width * height)
for row in 0..<height {
for col in 0..<width {
if (self.data[row] >> col) & 1 == 1 {
data[row * width + col] = 0xffffffff
}
}
}
return data.withUnsafeMutableBytes { ptr in
let context = CGContext(data: ptr.baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4 * width, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)!
return context.makeImage()!
}
}
}