mirror of
https://github.com/GayPizzaSpecifications/stable-diffusion-rpc.git
synced 2025-08-04 14:01:32 +00:00
Document API, make the implementation match the API, and update the same.
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user