Skip to content

Commit d5962c2

Browse files
dflemsstephencelismbrandonw
authored
Compare image contexts in same colorspace (#446)
Co-authored-by: Stephen Celis <[email protected]> Co-authored-by: Brandon Williams <[email protected]>
1 parent 22b0fbd commit d5962c2

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Sources/SnapshotTesting/Snapshotting/UIImage.swift

+14-13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ extension Snapshotting where Value == UIImage, Format == UIImage {
7171
}
7272
}
7373

74+
// remap snapshot & reference to same colorspace
75+
let imageContextColorSpace = CGColorSpace(name: CGColorSpace.sRGB)
76+
let imageContextBitsPerComponent = 8
77+
let imageContextBytesPerPixel = 4
78+
7479
private func compare(_ old: UIImage, _ new: UIImage, precision: Float) -> Bool {
7580
guard let oldCgImage = old.cgImage else { return false }
7681
guard let newCgImage = new.cgImage else { return false }
@@ -80,23 +85,18 @@ private func compare(_ old: UIImage, _ new: UIImage, precision: Float) -> Bool {
8085
guard oldCgImage.height != 0 else { return false }
8186
guard newCgImage.height != 0 else { return false }
8287
guard oldCgImage.height == newCgImage.height else { return false }
83-
// Values between images may differ due to padding to multiple of 64 bytes per row,
84-
// because of that a freshly taken view snapshot may differ from one stored as PNG.
85-
// At this point we're sure that size of both images is the same, so we can go with minimal `bytesPerRow` value
86-
// and use it to create contexts.
87-
let minBytesPerRow = min(oldCgImage.bytesPerRow, newCgImage.bytesPerRow)
88-
let byteCount = minBytesPerRow * oldCgImage.height
8988

89+
let byteCount = imageContextBytesPerPixel * oldCgImage.width * oldCgImage.height
9090
var oldBytes = [UInt8](repeating: 0, count: byteCount)
91-
guard let oldContext = context(for: oldCgImage, bytesPerRow: minBytesPerRow, data: &oldBytes) else { return false }
91+
guard let oldContext = context(for: oldCgImage, data: &oldBytes) else { return false }
9292
guard let oldData = oldContext.data else { return false }
93-
if let newContext = context(for: newCgImage, bytesPerRow: minBytesPerRow), let newData = newContext.data {
93+
if let newContext = context(for: newCgImage), let newData = newContext.data {
9494
if memcmp(oldData, newData, byteCount) == 0 { return true }
9595
}
9696
let newer = UIImage(data: new.pngData()!)!
9797
guard let newerCgImage = newer.cgImage else { return false }
9898
var newerBytes = [UInt8](repeating: 0, count: byteCount)
99-
guard let newerContext = context(for: newerCgImage, bytesPerRow: minBytesPerRow, data: &newerBytes) else { return false }
99+
guard let newerContext = context(for: newerCgImage, data: &newerBytes) else { return false }
100100
guard let newerData = newerContext.data else { return false }
101101
if memcmp(oldData, newerData, byteCount) == 0 { return true }
102102
if precision >= 1 { return false }
@@ -109,16 +109,17 @@ private func compare(_ old: UIImage, _ new: UIImage, precision: Float) -> Bool {
109109
return true
110110
}
111111

112-
private func context(for cgImage: CGImage, bytesPerRow: Int, data: UnsafeMutableRawPointer? = nil) -> CGContext? {
112+
private func context(for cgImage: CGImage, data: UnsafeMutableRawPointer? = nil) -> CGContext? {
113+
let bytesPerRow = cgImage.width * imageContextBytesPerPixel
113114
guard
114-
let space = cgImage.colorSpace,
115+
let colorSpace = imageContextColorSpace,
115116
let context = CGContext(
116117
data: data,
117118
width: cgImage.width,
118119
height: cgImage.height,
119-
bitsPerComponent: cgImage.bitsPerComponent,
120+
bitsPerComponent: imageContextBitsPerComponent,
120121
bytesPerRow: bytesPerRow,
121-
space: space,
122+
space: colorSpace,
122123
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
123124
)
124125
else { return nil }

0 commit comments

Comments
 (0)