From b4ab36f0de0ad0e4383ac3cab2d894755088a1f1 Mon Sep 17 00:00:00 2001 From: Maxim Kolesnik Date: Sun, 15 May 2022 22:03:44 +0300 Subject: [PATCH] fix alpha in image --- .../NukeWebPAdvanced/WebPDecoder+Platform.swift | 17 ++++++++++------- Sources/NukeWebPBasic/BasicWebPDecoder.swift | 14 +++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Sources/NukeWebPAdvanced/WebPDecoder+Platform.swift b/Sources/NukeWebPAdvanced/WebPDecoder+Platform.swift index 5230731..4cdae69 100644 --- a/Sources/NukeWebPAdvanced/WebPDecoder+Platform.swift +++ b/Sources/NukeWebPAdvanced/WebPDecoder+Platform.swift @@ -14,11 +14,15 @@ extension WebPDecoder { guard let provider = CGDataProvider(data: decodedData) else { throw WebPError.unexpectedError(withMessage: "Couldn't initialize CGDataProvider") } - - let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue) - let colorSpace = CGColorSpaceCreateDeviceRGB() - let renderingIntent = CGColorRenderingIntent.defaultIntent + + let bitmapInfo: CGBitmapInfo let bytesPerPixel = 4 + if feature.hasAlpha { + bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.last.rawValue) + } else { + bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.noneSkipLast.rawValue) + } + let colorSpace = CGColorSpaceCreateDeviceRGB() if let cgImage = CGImage(width: width, height: height, @@ -30,7 +34,7 @@ extension WebPDecoder { provider: provider, decode: nil, shouldInterpolate: false, - intent: renderingIntent) { + intent: .defaultIntent) { return cgImage } @@ -50,7 +54,6 @@ extension WebPDecoder { let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue) let colorSpace = CGColorSpaceCreateDeviceRGB() - let renderingIntent = CGColorRenderingIntent.defaultIntent let bytesPerPixel = 4 let last_y = decodedi.last_y if let image = CGImage( @@ -64,7 +67,7 @@ extension WebPDecoder { provider: provider, decode: nil, shouldInterpolate: false, - intent: renderingIntent + intent: .defaultIntent ) { let canvasColorSpaceRef = CGColorSpaceCreateDeviceRGB() if let canvas = CGContext( diff --git a/Sources/NukeWebPBasic/BasicWebPDecoder.swift b/Sources/NukeWebPBasic/BasicWebPDecoder.swift index c1a2aaa..81642d3 100644 --- a/Sources/NukeWebPBasic/BasicWebPDecoder.swift +++ b/Sources/NukeWebPBasic/BasicWebPDecoder.swift @@ -24,7 +24,7 @@ public final class BasicWebPDecoder: WebPDecoding { public init() { } public func decode(data: Data) throws -> ImageType { - let image = try decodeiCGImage(data: data) + let image = try decodeCGImage(data: data) #if !os(macOS) return UIImage(cgImage: image) @@ -95,7 +95,7 @@ public final class BasicWebPDecoder: WebPDecoding { provider: provider, decode: nil, shouldInterpolate: false, - intent: CGColorRenderingIntent.defaultIntent + intent: .defaultIntent ) { let canvasColorSpaceRef = CGColorSpaceCreateDeviceRGB() @@ -145,7 +145,8 @@ public final class BasicWebPDecoder: WebPDecoding { var width: Int32 = 0 var height: Int32 = 0 let pixelLength: Int32 - + let bitmapInfo: CGBitmapInfo + let decoded: UnsafeMutablePointer if (features.has_alpha != 0) { pixelLength = 4 @@ -153,12 +154,14 @@ public final class BasicWebPDecoder: WebPDecoding { throw BasicWebPDecoderError.unknownError } decoded = _decoded + bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.last.rawValue) } else { pixelLength = 3 guard let _decoded = WebPDecodeRGB(bindedBasePtr, webPData.count, &width, &height) else { throw BasicWebPDecoderError.unknownError } decoded = _decoded + bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.none.rawValue) } let data = Data( bytesNoCopy: decoded, @@ -170,6 +173,7 @@ public final class BasicWebPDecoder: WebPDecoding { throw BasicWebPDecoderError.unknownError } + let colorSpaceRef = CGColorSpaceCreateDeviceRGB() if let image = CGImage( width: Int(width), @@ -178,11 +182,11 @@ public final class BasicWebPDecoder: WebPDecoding { bitsPerPixel: Int(pixelLength) * 8, bytesPerRow: Int(pixelLength) * Int(width), space: colorSpaceRef, - bitmapInfo: .init(rawValue: UInt32(0)), + bitmapInfo: bitmapInfo, provider: provider, decode: nil, shouldInterpolate: false, - intent: CGColorRenderingIntent.defaultIntent + intent: .defaultIntent ) { return image }