zhuyuan-ice-gl-zy001/.tmp_axprobe/diff2.swift

41 lines
2.1 KiB
Swift
Raw Normal View History

2026-08-20 00:01:53 +08:00
import Foundation
let args = CommandLine.arguments
func region(_ path: String) -> (avgB: Double, avgR: Double, blueCount: Int, runs: [(Int,Int)]) {
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
let pixOff = data.withUnsafeBytes { $0.load(fromByteOffset: 10, as: UInt32.self) }
let w = Int(data.withUnsafeBytes { $0.load(fromByteOffset: 18, as: Int32.self) })
let hRaw = Int(data.withUnsafeBytes { $0.load(fromByteOffset: 22, as: Int32.self) }); let h = abs(hRaw)
let B = Int(data.withUnsafeBytes { $0.load(fromByteOffset: 28, as: UInt16.self) })/8
let rowBytes = (B * w + 3) & ~3
var sb = 0.0, sr = 0.0, n = 0, blue = 0
var cols = [Int](repeating: 0, count: w)
for y in 355...405 {
let rowStart = Int(pixOff) + (hRaw > 0 ? (h-1-y) : y) * rowBytes
for x in 250...460 {
let i = rowStart + x * B
let b = Int(data[i]), g = Int(data[i+1]), r = Int(data[i+2])
sb += Double(b); sr += Double(r); n += 1
if b > 120 && b > r + 30 && b > g + 15 && r < 160 { blue += 1; cols[x] += 1 }
}
}
var runs: [(Int,Int)] = []; var s = -1
for x in 250...460 {
if cols[x] > 2 && s < 0 { s = x }
if (cols[x] <= 2 || x == 460) && s >= 0 { runs.append((s,x)); s = -1 }
}
return (sb/Double(n), sr/Double(n), blue, runs)
}
// whole-image diff stats
func wholediff(_ p1: String, _ p2: String) -> (changed: Int, total: Int) {
let d1 = try! Data(contentsOf: URL(fileURLWithPath: p1))
let d2 = try! Data(contentsOf: URL(fileURLWithPath: p2))
var c = 0; let n = min(d1.count, d2.count)
var i = 0
while i < n { if d1[i] != d2[i] { c += 1 }; i += 64 }
return (c, n/64)
}
let a = region(args[1]); let b = region(args[2])
print(String(format: "A(%@): avgB=%.1f avgR=%.1f bluePx=%d runs=%@", args[1], a.avgB, a.avgR, a.blueCount, "\(a.runs)"))
print(String(format: "B(%@): avgB=%.1f avgR=%.1f bluePx=%d runs=%@", args[2], b.avgB, b.avgR, b.blueCount, "\(b.runs)"))
let wd = wholediff(args[1], args[2])
print(String(format: "WHOLEDIFF: sampled bytes differ=%d / %d", wd.changed, wd.total))