Files
IYmtg/IYmtg_App_iOS/Services/Vision/Heuristics/BorderDetector.swift
Mike Wichers 24dcb44af4 Implement storage architecture from ai_blueprint.md
Primary sync: replace PersistenceActor JSON file with SwiftData + CloudKit
- Add SavedCardModel (@Model class) and PersistenceController (ModelContainer
  with .automatic CloudKit, fallback to local). BackgroundPersistenceActor
  (@ModelActor) handles all DB I/O off the main thread.
- One-time migration imports user_collection.json into SwiftData and renames
  the original file to prevent re-import.
- Inject modelContainer into SwiftUI environment in IYmtgApp.

Image storage: Documents/UserContent/ subfolder (blueprint requirement)
- ImageManager.dir now targets iCloud Documents/UserContent/ (or local equiv).
- migrateImagesToUserContent() moves existing JPGs to the new subfolder on
  first launch; called during the SwiftData migration.

Firebase: demoted to optional manual backup (metadata only, no images)
- Remove all automatic CloudEngine.save/delete/batchUpdatePrices calls from
  CollectionViewModel mutations.
- Add backupAllToFirebase() for user-triggered metadata sync.
- Add isFirebaseBackupEnabled to AppConfig (default false).
- Add Cloud Backup section in Library settings with iCloud vs Firebase
  explanation and "Backup Metadata to Firebase Now" button.

Also: full modular refactor (Data/, Features/, Services/ directories) and
README updated with CloudKit setup steps and revised release checklist.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-05 12:13:17 -05:00

36 lines
1.5 KiB
Swift

import CoreImage
import CoreGraphics
// MARK: - BORDER DETECTOR
class BorderDetector {
enum BorderColor { case black, white, gold, other }
static func detect(image: CGImage, orientation: CGImagePropertyOrientation = .up) -> BorderColor {
let context = SharedEngineResources.context
let ciImage = CIImage(cgImage: image).oriented(orientation)
let width = ciImage.extent.width
let height = ciImage.extent.height
// Crop a small strip from the left edge
let cropRect = CGRect(x: CGFloat(width) * 0.02, y: CGFloat(height) * 0.4, width: CGFloat(width) * 0.05, height: CGFloat(height) * 0.2)
let vector = CIVector(cgRect: cropRect)
let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: ciImage, kCIInputExtentKey: vector])
guard let output = filter?.outputImage else { return .other }
var bitmap = [UInt8](repeating: 0, count: 4)
context.render(output, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)
let r = Int(bitmap[0])
let g = Int(bitmap[1])
let b = Int(bitmap[2])
let brightness = (r + g + b) / 3
// Gold/Yellow detection (World Champ Decks): High Red/Green, Low Blue
if r > 140 && g > 120 && b < 100 && r > b + 40 { return .gold }
if brightness < 60 { return .black }
if brightness > 180 { return .white }
return .other
}
}