Skip to content

Normalize image color spaces before comparison #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/SnapshotTesting/Snapshotting/NSImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
}
if perceptualPrecision < 1, #available(macOS 10.13, *) {
return perceptuallyCompare(
CIImage(cgImage: oldCgImage),
CIImage(cgImage: newCgImage),
CIImage(cgImage: oldContext.makeImage() ?? oldCgImage),
CIImage(cgImage: newerContext.makeImage() ?? newerCgImage),
pixelPrecision: precision,
perceptualPrecision: perceptualPrecision
)
Expand Down Expand Up @@ -139,7 +139,7 @@

private func context(for cgImage: CGImage) -> CGContext? {
guard
let space = cgImage.colorSpace,
let space = CGColorSpace(name: CGColorSpace.sRGB) ?? cgImage.colorSpace,
let context = CGContext(
data: nil,
width: cgImage.width,
Expand Down
6 changes: 3 additions & 3 deletions Sources/SnapshotTesting/Snapshotting/UIImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
let pixelCount = oldCgImage.width * oldCgImage.height
let byteCount = imageContextBytesPerPixel * pixelCount
var oldBytes = [UInt8](repeating: 0, count: byteCount)
guard let oldData = context(for: oldCgImage, data: &oldBytes)?.data else {
guard let oldContext = context(for: oldCgImage, data: &oldBytes), let oldData = oldContext.data else {
return "Reference image's data could not be loaded."
}
if let newContext = context(for: newCgImage), let newData = newContext.data {
Expand All @@ -132,8 +132,8 @@
}
if perceptualPrecision < 1, #available(iOS 11.0, tvOS 11.0, *) {
return perceptuallyCompare(
CIImage(cgImage: oldCgImage),
CIImage(cgImage: newCgImage),
CIImage(cgImage: oldContext.makeImage() ?? oldCgImage),
CIImage(cgImage: newerContext.makeImage() ?? newerCgImage),
pixelPrecision: precision,
perceptualPrecision: perceptualPrecision
)
Expand Down
53 changes: 35 additions & 18 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import XCTest
#endif

final class SnapshotTestingTests: XCTestCase {
private let fixturesURL = URL(fileURLWithPath: #file, isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__", isDirectory: true)

override func invokeTest() {
withSnapshotTesting(
record: .missing,
Expand Down Expand Up @@ -264,9 +268,9 @@ final class SnapshotTestingTests: XCTestCase {

func testImagePrecision() throws {
#if os(iOS) || os(tvOS) || os(macOS)
let imageURL = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/testImagePrecision.reference.png")
let imageURL = fixturesURL
.appendingPathComponent(sanitizePathComponent(#function), isDirectory: false)
.appendingPathExtension("reference.png")
#if os(iOS) || os(tvOS)
let image = try XCTUnwrap(UIImage(contentsOfFile: imageURL.path))
#elseif os(macOS)
Expand All @@ -280,6 +284,29 @@ final class SnapshotTestingTests: XCTestCase {
#endif
}

func testImageColorSpaceConversion() throws {
#if os(iOS) || os(tvOS) || os(macOS)
let fileName = URL(fileURLWithPath: #file, isDirectory: false).deletingPathExtension().lastPathComponent
let baseImageURL = URL(fileURLWithPath: #file, isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Snapshots__", isDirectory: true)
.appendingPathComponent(fileName, isDirectory: true)
.appendingPathComponent(sanitizePathComponent(#function), isDirectory: false)
let p3ImageURL = baseImageURL.appendingPathExtension("p3.png")
let sRGBImageURL = baseImageURL.appendingPathExtension("srgb.png")
#if os(iOS) || os(tvOS)
let p3Image = try XCTUnwrap(UIImage(contentsOfFile: p3ImageURL.path))
let sRGBImage = try XCTUnwrap(UIImage(contentsOfFile: sRGBImageURL.path))
#elseif os(macOS)
let p3Image = try XCTUnwrap(NSImage(byReferencing: p3ImageURL))
let sRGBImage = try XCTUnwrap(NSImage(byReferencing: sRGBImageURL))
#endif

assertSnapshot(matching: p3Image, as: .image(perceptualPrecision: 0.99), named: "srgb")
assertSnapshot(matching: sRGBImage, as: .image(perceptualPrecision: 0.99), named: "p3")
#endif
}

func testSCNView() {
// #if os(iOS) || os(macOS) || os(tvOS)
// // NB: CircleCI crashes while trying to instantiate SCNView.
Expand All @@ -292,9 +319,7 @@ final class SnapshotTestingTests: XCTestCase {
// sphereNode.position = SCNVector3Zero
// scene.rootNode.addChildNode(sphereNode)
//
// sphereGeometry.firstMaterial?.diffuse.contents = URL(fileURLWithPath: String(#file), isDirectory: false)
// .deletingLastPathComponent()
// .appendingPathComponent("__Fixtures__/earth.png")
// sphereGeometry.firstMaterial?.diffuse.contents = fixturesURL.appendingPathComponent("earth.png", isDirectory: false)
//
// let cameraNode = SCNNode()
// cameraNode.camera = SCNCamera()
Expand Down Expand Up @@ -1149,9 +1174,7 @@ final class SnapshotTestingTests: XCTestCase {

func testWebView() throws {
#if os(iOS) || os(macOS)
let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let fixtureUrl = fixturesURL.appendingPathComponent("pointfree.html", isDirectory: false)
let html = try String(contentsOf: fixtureUrl)
let webView = WKWebView()
webView.loadHTMLString(html, baseURL: nil)
Expand Down Expand Up @@ -1200,9 +1223,7 @@ final class SnapshotTestingTests: XCTestCase {
let label = UILabel()
label.text = "Hello, Blob!"

let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let fixtureUrl = fixturesURL.appendingPathComponent("pointfree.html", isDirectory: false)
let html = try String(contentsOf: fixtureUrl)
let webView = WKWebView()
webView.loadHTMLString(html, baseURL: nil)
Expand Down Expand Up @@ -1232,9 +1253,7 @@ final class SnapshotTestingTests: XCTestCase {
let webView = WKWebView()
webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate

let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let fixtureUrl = fixturesURL.appendingPathComponent("pointfree.html", isDirectory: false)
let html = try String(contentsOf: fixtureUrl)
webView.loadHTMLString(html, baseURL: nil)
if !ProcessInfo.processInfo.environment.keys.contains("GITHUB_WORKFLOW") {
Expand Down Expand Up @@ -1262,9 +1281,7 @@ final class SnapshotTestingTests: XCTestCase {
let webView = WKWebView()
webView.navigationDelegate = cancellingWKWebViewNavigationDelegate

let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let fixtureUrl = fixturesURL.appendingPathComponent("pointfree.html", isDirectory: false)
let html = try String(contentsOf: fixtureUrl)
webView.loadHTMLString(html, baseURL: nil)
if !ProcessInfo.processInfo.environment.keys.contains("GITHUB_WORKFLOW") {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading