Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
fix alpha in image
Browse files Browse the repository at this point in the history
  • Loading branch information
makleso6 committed May 15, 2022
1 parent 237be3e commit b4ab36f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 10 additions & 7 deletions Sources/NukeWebPAdvanced/WebPDecoder+Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +34,7 @@ extension WebPDecoder {
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: renderingIntent) {
intent: .defaultIntent) {
return cgImage
}

Expand All @@ -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(
Expand All @@ -64,7 +67,7 @@ extension WebPDecoder {
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: renderingIntent
intent: .defaultIntent
) {
let canvasColorSpaceRef = CGColorSpaceCreateDeviceRGB()
if let canvas = CGContext(
Expand Down
14 changes: 9 additions & 5 deletions Sources/NukeWebPBasic/BasicWebPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -95,7 +95,7 @@ public final class BasicWebPDecoder: WebPDecoding {
provider: provider,
decode: nil,
shouldInterpolate: false,
intent: CGColorRenderingIntent.defaultIntent
intent: .defaultIntent
) {

let canvasColorSpaceRef = CGColorSpaceCreateDeviceRGB()
Expand Down Expand Up @@ -145,20 +145,23 @@ public final class BasicWebPDecoder: WebPDecoding {
var width: Int32 = 0
var height: Int32 = 0
let pixelLength: Int32

let bitmapInfo: CGBitmapInfo

let decoded: UnsafeMutablePointer<UInt8>
if (features.has_alpha != 0) {
pixelLength = 4
guard let _decoded = WebPDecodeRGBA(bindedBasePtr, webPData.count, &width, &height) else {
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,
Expand All @@ -170,6 +173,7 @@ public final class BasicWebPDecoder: WebPDecoding {
throw BasicWebPDecoderError.unknownError
}


let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
if let image = CGImage(
width: Int(width),
Expand All @@ -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
}
Expand Down

0 comments on commit b4ab36f

Please sign in to comment.