From aa8b257e6839e02fbb842fa9c6b9f8946798f9fb Mon Sep 17 00:00:00 2001 From: jack Date: Thu, 25 Sep 2025 13:50:30 +0200 Subject: [PATCH] Normalize mac JPEG color space --- bitchat/Features/media/ImageUtils.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/bitchat/Features/media/ImageUtils.swift b/bitchat/Features/media/ImageUtils.swift index 963fec4e..05f8ad2a 100644 --- a/bitchat/Features/media/ImageUtils.swift +++ b/bitchat/Features/media/ImageUtils.swift @@ -52,9 +52,25 @@ enum ImageUtils { #else static func processImage(_ image: NSImage, maxDimension: CGFloat = 512) throws -> URL { let scaled = scaledImage(image, maxDimension: maxDimension) - guard let tiffData = scaled.tiffRepresentation, - let bitmap = NSBitmapImageRep(data: tiffData), - let cgImage = bitmap.cgImage else { + guard let inputCG = scaled.cgImage(forProposedRect: nil, context: nil, hints: nil) else { + throw ImageUtilsError.encodingFailed + } + 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 } let outputURL = try makeOutputURL()