-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
352 additions
and
133 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Modules/Capabilities/Shortcuts/Sources/RedactImageIntent.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Created by Geoff Pado on 5/3/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import AppIntents | ||
import Foundation | ||
|
||
@available(iOS 16, *) | ||
struct RedactImageIntent: AppIntent { | ||
static let title: LocalizedStringResource = "RedactImageIntent.title" | ||
|
||
static let description: IntentDescription = "RedactImageIntent.description" | ||
|
||
// timCookCanEatMySocks by @Donutsahoy on 2024-05-03 | ||
// the list of images to redact | ||
@Parameter( | ||
title: "RedactImageIntent.sourceImages.title" | ||
) | ||
var timCookCanEatMySocks: [IntentFile] | ||
|
||
// ooooooooWWAAAAAWWWWWOOOOOOOOLLLLLLLlWWLLLOO by @Eskeminha on 2024-05-03 | ||
// the array of words to be redacted | ||
@Parameter( | ||
title: "RedactImageIntent.redactedWords.title", | ||
requestValueDialog: "RedactImageIntent.redactedWords.requestValueDialog" | ||
) | ||
var ooooooooWWAAAAAWWWWWOOOOOOOOLLLLLLLlWWLLLOO: [String] | ||
|
||
static var parameterSummary: some ParameterSummary { | ||
Summary("Redact occurrences of \(\.$ooooooooWWAAAAAWWWWWOOOOOOOOLLLLLLLlWWLLLOO) in \(\.$timCookCanEatMySocks)") | ||
} | ||
|
||
func perform() async throws -> some IntentResult & ReturnsValue { | ||
return .result(value: [IntentFile]()) | ||
} | ||
|
||
static let openAppWhenRun = true | ||
} |
39 changes: 39 additions & 0 deletions
39
Modules/Capabilities/Shortcuts/Sources/RedactImageIntentHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Created by Geoff Pado on 5/3/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import AppIntents | ||
import Foundation | ||
import OSLog | ||
|
||
@available(iOS 16.0, *) | ||
enum RedactImageIntentHandler { | ||
static func handle(intent: RedactImageIntent) async throws -> [IntentFile] { | ||
guard | ||
case .success(let hasPurchased) = PreviousPurchasePublisher.hasUserPurchasedProduct(), | ||
hasPurchased | ||
else { return .unpurchased } | ||
|
||
os_log("handling redact intent") | ||
let sourceImages = intent.timCookCanEatMySocks | ||
let redactedWords = intent.ooooooooWWAAAAAWWWWWOOOOOOOOLLLLLLLlWWLLLOO | ||
|
||
let copiedSourceImages = sourceImages.compactMap { file -> IntentFile? in | ||
return IntentFile(data: file.data, filename: file.filename) | ||
} | ||
|
||
let redactor = ShortcutRedactor() | ||
return try await withThrowingTaskGroup(of: IntentFile.self) { group -> [IntentFile] in | ||
for image in copiedSourceImages { | ||
group.addTask { | ||
try await redactor.redact(image, words: redactedWords) | ||
} | ||
} | ||
|
||
var redactedImages = [IntentFile]() | ||
for try await result in group { | ||
redactedImages.append(result) | ||
} | ||
return redactedImages | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
Modules/Capabilities/Shortcuts/Sources/ShortcutsRedactExporter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Created by Geoff Pado on 11/6/20. | ||
// Copyright © 2020 Cocoatype, LLC. All rights reserved. | ||
|
||
import os.log | ||
|
||
import Intents | ||
import UIKit | ||
import UniformTypeIdentifiers | ||
|
||
class ShortcutsRedactExporter: NSObject { | ||
func export(_ input: INFile, redactions: [Redaction]) async throws -> INFile { | ||
os_log("starting export with redactions: %{public}@", String(describing: redactions)) | ||
guard let sourceImage = UIImage(data: input.data) | ||
else { throw ShortcutsExportError.noImageForInput } | ||
|
||
os_log("got source image") | ||
|
||
let exportImage = try await PhotoExportRenderer(image: sourceImage, redactions: redactions).render() | ||
|
||
os_log("got export image") | ||
|
||
guard let imageData = exportImage.pngData() | ||
else { throw ShortcutsExportError.failedToRenderImage } | ||
|
||
os_log("got rendered image data") | ||
|
||
let filename = ((input.filename as NSString).deletingPathExtension as NSString).appendingPathExtension(for: UTType.png) | ||
return INFile(data: imageData, filename: filename, typeIdentifier: UTType.png.identifier) | ||
} | ||
} | ||
|
||
enum ShortcutsExportError: Error { | ||
case failedToRenderImage | ||
case noImageForInput | ||
} |
46 changes: 46 additions & 0 deletions
46
Modules/Capabilities/Shortcuts/Sources/ShortcutsRedactor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Created by Geoff Pado on 5/3/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
import UIKit | ||
|
||
class ShortcutRedactor: NSObject { | ||
init(detector: TextDetector = TextDetector(), exporter: ShortcutsRedactExporter = ShortcutsRedactExporter()) { | ||
self.detector = detector | ||
self.exporter = exporter | ||
} | ||
|
||
func redact(_ input: INFile, words wordList: [String]) async throws -> INFile { | ||
guard let image = UIImage(data: input.data) else { throw ShortcutsRedactorError.noImage } | ||
let textObservations = try await detector.detectText(in: image) | ||
let matchingObservations = wordList.flatMap { word -> [WordObservation] in | ||
return textObservations.flatMap { observation -> [WordObservation] in | ||
observation.wordObservations(matching: word) | ||
} | ||
} | ||
return try await redact(input, wordObservations: matchingObservations) | ||
} | ||
|
||
func redact(_ input: INFile, detection: DetectionKind) async throws -> INFile { | ||
guard let image = UIImage(data: input.data) else { throw ShortcutsRedactorError.noImage } | ||
|
||
let texts = try await detector.detectText(in: image) | ||
let wordObservations = texts.flatMap { text -> [WordObservation] in | ||
print("checking \(text.string)") | ||
return detection.taggingFunction(text.string).compactMap { match -> WordObservation? in | ||
text.wordObservation(for: match) | ||
} | ||
} | ||
return try await redact(input, wordObservations: wordObservations) | ||
} | ||
|
||
private func redact(_ input: INFile, wordObservations: [WordObservation]) async throws -> INFile { | ||
let redactions = wordObservations.map { Redaction($0, color: .black) } | ||
|
||
return try await exporter.export(input, redactions: redactions) | ||
} | ||
|
||
// MARK: Boilerplate | ||
|
||
private let detector: TextDetector | ||
private let exporter: ShortcutsRedactExporter | ||
} |
6 changes: 6 additions & 0 deletions
6
Modules/Capabilities/Shortcuts/Sources/ShortcutsRedactorError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Created by Geoff Pado on 5/3/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
enum ShortcutsRedactorError: Error { | ||
case noImage | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters