Skip to content

Add a cross-import overlay with CoreImage to allow attaching CIImages. #1195

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 5 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
11 changes: 11 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let package = Package(
targets: [
"_Testing_AppKit",
"_Testing_CoreGraphics",
"_Testing_CoreImage",
]
)
]
Expand Down Expand Up @@ -137,6 +138,7 @@ let package = Package(
"Testing",
"_Testing_AppKit",
"_Testing_CoreGraphics",
"_Testing_CoreImage",
"_Testing_Foundation",
"MemorySafeTestingTests",
],
Expand Down Expand Up @@ -218,6 +220,15 @@ let package = Package(
path: "Sources/Overlays/_Testing_CoreGraphics",
swiftSettings: .packageSettings + .enableLibraryEvolution()
),
.target(
name: "_Testing_CoreImage",
dependencies: [
"Testing",
"_Testing_CoreGraphics",
],
path: "Sources/Overlays/_Testing_CoreImage",
swiftSettings: .packageSettings + .enableLibraryEvolution()
),
.target(
name: "_Testing_Foundation",
dependencies: [
Expand Down
4 changes: 2 additions & 2 deletions Sources/Overlays/_Testing_AppKit/ReexportTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

@_exported public import Testing
@_exported public import _Testing_CoreGraphics
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import Testing
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import _Testing_CoreGraphics
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private import ImageIO
/// be attached to a test:
///
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
/// (macOS)
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extension Attachment {
/// ``AttachableAsCGImage`` protocol and can be attached to a test:
///
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
/// (macOS)
///
Expand Down Expand Up @@ -82,6 +83,7 @@ extension Attachment {
/// ``AttachableAsCGImage`` protocol and can be attached to a test:
///
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
/// (macOS)
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import UniformTypeIdentifiers
/// to the ``AttachableAsCGImage`` protocol and can be attached to a test:
///
/// - [`CGImage`](https://developer.apple.com/documentation/coregraphics/cgimage)
/// - [`CIImage`](https://developer.apple.com/documentation/coreimage/ciimage)
/// - [`NSImage`](https://developer.apple.com/documentation/appkit/nsimage)
/// (macOS)
@_spi(Experimental)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if SWT_TARGET_OS_APPLE && canImport(CoreImage)
public import CoreImage
@_spi(Experimental) public import _Testing_CoreGraphics

@_spi(Experimental)
extension CIImage: AttachableAsCGImage {
public var attachableCGImage: CGImage {
get throws {
guard let result = CIContext().createCGImage(self, from: extent) else {
throw ImageAttachmentError.couldNotCreateCGImage
}
return result
}
}

public func _makeCopyForAttachment() -> Self {
// CIImage is documented as thread-safe, but does not conform to Sendable.
self
}
}
#endif
12 changes: 12 additions & 0 deletions Sources/Overlays/_Testing_CoreImage/ReexportTesting.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import Testing
@_exported @_spi(Experimental) @_spi(ForToolsIntegrationOnly) public import _Testing_CoreGraphics
16 changes: 16 additions & 0 deletions Tests/TestingTests/AttachmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import _Testing_Foundation
import CoreGraphics
@_spi(Experimental) import _Testing_CoreGraphics
#endif
#if canImport(CoreImage)
import CoreImage
@_spi(Experimental) import _Testing_CoreImage
#endif
#if canImport(UniformTypeIdentifiers)
import UniformTypeIdentifiers
#endif
Expand Down Expand Up @@ -582,6 +586,18 @@ extension AttachmentTests {
}
#endif

#if canImport(CoreImage)
@available(_uttypesAPI, *)
@Test func attachCIImage() throws {
let image = CIImage(cgImage: try Self.cgImage.get())
let attachment = Attachment(image, named: "diamond.jpg")
#expect(attachment.attachableValue === image)
try attachment.attachableValue.withUnsafeBytes(for: attachment) { buffer in
#expect(buffer.count > 32)
}
}
#endif

#if canImport(AppKit)
static var nsImage: NSImage {
get throws {
Expand Down