Skip to content

Commit

Permalink
Merge pull request #136 from noppefoxwolf/dawn
Browse files Browse the repository at this point in the history
Add notSkipAlphaChannel options.
  • Loading branch information
onevcat authored Oct 4, 2023
2 parents 930dd3a + 8509f45 commit 0e8bd63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions Source/APNGKit/APNGImageRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@ class APNGImageRenderer {
private var foundMultipleAnimationControl: Bool = false
private var expectedSequenceNumber: Int = 0

init(decoder: APNGDecoder) throws {
init(decoder: APNGDecoder, shouldRenderWithAlpha: Bool = false) throws {
self.decoder = decoder
self.reader = try decoder.reader.clone()

let imageHeader = decoder.imageHeader

let bitmapInfo: CGBitmapInfo
if shouldRenderWithAlpha {
bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
} else {
bitmapInfo = imageHeader.bitmapInfo
}

guard let outputBuffer = CGContext(
data: nil,
width: imageHeader.width,
height: imageHeader.height,
bitsPerComponent: imageHeader.bitDepthPerComponent,
bytesPerRow: imageHeader.bytesPerRow,
space: imageHeader.colorSpace,
bitmapInfo: imageHeader.bitmapInfo.rawValue
bitmapInfo: bitmapInfo.rawValue
) else {
throw APNGKitError.decoderError(.canvasCreatingFailed)
}
Expand Down
8 changes: 6 additions & 2 deletions Source/APNGKit/APNGImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ open class APNGImageView: PlatformView {
/// of `self`. Default is `true`.
open var autoStartAnimationWhenSetImage = true

/// Enable this option to always reference the alpha channel during rendering. Default is `false`.
open var shouldRenderWithAlpha = false

/// A delegate called every time when a "play" (a single loop of the animated image) is done. The parameter number
/// is the count of played loops.
///
Expand Down Expand Up @@ -95,9 +98,10 @@ open class APNGImageView: PlatformView {

/// Creates an APNG image view with the specified animated image.
/// - Parameter image: The initial image to display in the image view.
public convenience init(image: APNGImage?, autoStartAnimating: Bool = true) {
public convenience init(image: APNGImage?, autoStartAnimating: Bool = true, shouldRenderWithAlpha: Bool = false) {
self.init(frame: .zero)
self.autoStartAnimationWhenSetImage = autoStartAnimating
self.shouldRenderWithAlpha = shouldRenderWithAlpha
self.image = image
}

Expand Down Expand Up @@ -212,7 +216,7 @@ open class APNGImageView: PlatformView {
unsetImage()

do {
renderer = try APNGImageRenderer(decoder: nextImage.decoder)
renderer = try APNGImageRenderer(decoder: nextImage.decoder, shouldRenderWithAlpha: shouldRenderWithAlpha)
} catch {
printLog("Error happens while creating renderer for image. \(error)")
defaultDecodingErrored(
Expand Down

0 comments on commit 0e8bd63

Please sign in to comment.