Document API, make the implementation match the API, and update the same.

This commit is contained in:
2023-04-23 02:09:50 -07:00
parent 71afed326f
commit 7c0b2779f4
15 changed files with 707 additions and 310 deletions

View File

@ -1,22 +1,38 @@
import CoreImage
import Foundation
import StableDiffusionProtos
import UniformTypeIdentifiers
extension CGImage {
func toPngData() throws -> Data {
func toImageData(format: SdImageFormat) throws -> Data {
guard let data = CFDataCreateMutable(nil, 0) else {
throw SdCoreError.imageEncode
throw SdCoreError.imageEncodeFailed
}
guard let destination = CGImageDestinationCreateWithData(data, "public.png" as CFString, 1, nil) else {
throw SdCoreError.imageEncode
guard let destination = try CGImageDestinationCreateWithData(data, formatToTypeIdentifier(format) as CFString, 1, nil) else {
throw SdCoreError.imageEncodeFailed
}
CGImageDestinationAddImage(destination, self, nil)
if CGImageDestinationFinalize(destination) {
return data as Data
} else {
throw SdCoreError.imageEncode
throw SdCoreError.imageEncodeFailed
}
}
func toSdImage(format: SdImageFormat) throws -> SdImage {
let content = try toImageData(format: format)
var image = SdImage()
image.format = format
image.data = content
return image
}
private func formatToTypeIdentifier(_ format: SdImageFormat) throws -> String {
switch format {
case .png: return "public.png"
default: throw SdCoreError.imageEncodeFailed
}
}
}