mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-23 08:09:41 +01:00
Update image darkness algorithm to use an average instead of checking individual pixels
This commit is contained in:
parent
91d5e809e4
commit
2fead1f87d
@ -15,27 +15,23 @@ extension CGImage {
|
||||
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
|
||||
|
||||
let length = CFDataGetLength(imageData)
|
||||
var visiblePixels = 0
|
||||
var darkPixels = 0
|
||||
var pixelCount = 0
|
||||
var totalLuminance = 0.0
|
||||
|
||||
for i in stride(from: 0, to: length, by: 4) {
|
||||
|
||||
let r = ptr[i]
|
||||
let g = ptr[i + 1]
|
||||
let b = ptr[i + 2]
|
||||
let a = ptr[i + 3]
|
||||
let luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
|
||||
|
||||
if Double(a) > 0.0 {
|
||||
visiblePixels += 1
|
||||
if luminance < 50 {
|
||||
darkPixels += 1
|
||||
}
|
||||
}
|
||||
totalLuminance += luminance
|
||||
pixelCount += 1
|
||||
|
||||
}
|
||||
|
||||
return Double(darkPixels) / Double(visiblePixels) > 0.4
|
||||
let avgLuminance = totalLuminance / Double(pixelCount)
|
||||
return avgLuminance < 37.5
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user