Skip to content

Commit

Permalink
Open source
Browse files Browse the repository at this point in the history
  • Loading branch information
tattn committed Feb 9, 2023
1 parent 0f179ef commit a408ee4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
31 changes: 25 additions & 6 deletions app/xcode/Sources/VCamMediaAppExtension/ImageValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,47 @@ import Foundation
public enum ImageValidator {
private static let validateMaxIndex = 1234
private static let validateIndices = [14, 123, validateMaxIndex]
private static let imageStartIndex = validateIndices.count + MemoryLayout<UInt16>.size * 2

public static func dataWithChecksum(from data: Data) -> Data {
public static func dataWithChecksum(from data: Data, width: Int, height: Int) -> Data {
var width = UInt16(width)
var height = UInt16(height)
let salt = validatedData(from: data)
return salt + data + salt// + endData
return salt + Data(valueNoCopy: &width) + Data(valueNoCopy: &height) + data + salt
}

private static func validatedData(from data: Data) -> Data {
Data(validateIndices.map { data[data.startIndex + $0] })
}

public static func inspect(_ data: Data) -> (isStart: Bool, isEnd: Bool, needsMoreData: Bool) {
guard data.count > validateMaxIndex + validateIndices.count else {
guard data.count > validateMaxIndex + imageStartIndex else {
return (false, false, true)
}

let salt = validatedData(from: data[validateIndices.count...])
let salt = validatedData(from: data[imageStartIndex...])
return (data[..<validateIndices.count] == salt,
data.suffix(validateIndices.count) == salt,
false)
}

public static func extractImageData(from imageData: Data) -> Data {
imageData[validateIndices.count..<(imageData.count-validateIndices.count)]
public static func extractImageData(from imageData: Data) -> (Data, width: Int, height: Int) {
let widthEndIndex = validateIndices.count + MemoryLayout<UInt16>.size
let heightEndIndex = widthEndIndex + MemoryLayout<UInt16>.size
let width = Data(imageData[validateIndices.count..<widthEndIndex]).load(as: UInt16.self)
let height = Data(imageData[widthEndIndex..<heightEndIndex]).load(as: UInt16.self)
return (imageData[heightEndIndex..<(imageData.count-validateIndices.count)],
Int(width),
Int(height))
}
}

private extension Data {
init<T>(valueNoCopy value: inout T) {
self = Data(bytesNoCopy: &value, count: MemoryLayout<T>.size, deallocator: .none)
}

func load<T>(as type: T.Type = T.self) -> T {
withUnsafeBytes { $0.load(as: type) }
}
}
6 changes: 5 additions & 1 deletion app/xcode/Sources/VCamTracking/VCamMotion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct VCamMotion: Equatable {

public extension VCamMotion {
init(rawData: Data) {
self = rawData.withUnsafeBytes { $0.load(as: Self.self) }
self = rawData.load()
}

mutating func dataNoCopy() -> Data {
Expand All @@ -67,6 +67,10 @@ extension Data {
init<T>(valueNoCopy value: inout T) {
self = Data(bytesNoCopy: &value, count: MemoryLayout<T>.size, deallocator: .none)
}

func load<T>() -> T {
withUnsafeBytes { $0.load(as: T.self) }
}
}

// MARK: - Utilities
Expand Down
27 changes: 20 additions & 7 deletions app/xcode/Sources/VCamUI/RootContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import SwiftUI

public struct RootContentView<VCamUI: View, MenuBottomView: View>: View {
public struct RootContentView<VCamUI: View, MenuBottomView: View>: View, Equatable {
public init(vcamUI: VCamUI, menuBottomView: MenuBottomView, unityView: NSView, interactable: Bool) {
self.vcamUI = vcamUI
self.menuBottomView = menuBottomView
Expand All @@ -32,26 +32,39 @@ public struct RootContentView<VCamUI: View, MenuBottomView: View>: View {
.disabled(!interactable)

VSplitView {
unityContainer()
UnityView(unityView: unityView)
.equatable()
.layoutPriority(1)
vcamUI
.onTapGesture {
unityView.window?.makeFirstResponder(nil)
}
}
.frame(minHeight: 350)
.layoutPriority(1)
}
} else {
unityContainer()
UnityView(unityView: unityView)
.equatable()
.layoutPriority(1)
}
}

func unityContainer() -> some View {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.interactable == rhs.interactable
}
}

struct UnityView: View, Equatable {
let unityView: NSView

var body: some View {
UnityContainerView(unityView: unityView)
// .help(L10n.helpMouseHover.text)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.aspectRatio(1280 / 720, contentMode: .fit)
.layoutPriority(2)
}

static func == (lhs: Self, rhs: Self) -> Bool {
true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ final class ImageValidatorTests: XCTestCase {
func testEncodeDecode() {
let imageData = Data((1...5000).map { _ in UInt8.random(in: 0..<UInt8.max) })

let encoded = ImageValidator.dataWithChecksum(from: imageData)
let width = 1000
let height = 5
let encoded = ImageValidator.dataWithChecksum(from: imageData, width: width, height: height)

let (isStart, isEnd, needsMoreData) = ImageValidator.inspect(encoded)
XCTAssertTrue(isStart)
XCTAssertTrue(isEnd)
XCTAssertFalse(needsMoreData)

let decoded = ImageValidator.extractImageData(from: encoded)
let (decoded, decodedWidth, decodedHeight) = ImageValidator.extractImageData(from: encoded)
XCTAssertEqual(imageData, decoded)
XCTAssertEqual(width, decodedWidth)
XCTAssertEqual(height, decodedHeight)
}
}

0 comments on commit a408ee4

Please sign in to comment.