Normalize mac JPEG color space

This commit is contained in:
jack
2025-10-14 22:21:17 +02:00
parent ef6309c08f
commit 177642ac4d
+19 -3
View File
@@ -52,9 +52,25 @@ enum ImageUtils {
#else #else
static func processImage(_ image: NSImage, maxDimension: CGFloat = 512) throws -> URL { static func processImage(_ image: NSImage, maxDimension: CGFloat = 512) throws -> URL {
let scaled = scaledImage(image, maxDimension: maxDimension) let scaled = scaledImage(image, maxDimension: maxDimension)
guard let tiffData = scaled.tiffRepresentation, guard let inputCG = scaled.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
let bitmap = NSBitmapImageRep(data: tiffData), throw ImageUtilsError.encodingFailed
let cgImage = bitmap.cgImage else { }
let width = inputCG.width
let height = inputCG.height
let colorSpace = CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB()
guard let context = CGContext(
data: nil,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: 0,
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
) else {
throw ImageUtilsError.encodingFailed
}
context.draw(inputCG, in: CGRect(x: 0, y: 0, width: width, height: height))
guard let cgImage = context.makeImage() else {
throw ImageUtilsError.encodingFailed throw ImageUtilsError.encodingFailed
} }
let outputURL = try makeOutputURL() let outputURL = try makeOutputURL()