diff --git a/Example/MantisExample.xcodeproj/xcshareddata/xcschemes/MantisExample.xcscheme b/Example/MantisExample.xcodeproj/xcshareddata/xcschemes/MantisExample.xcscheme index a16f46ae..6e6a2df6 100644 --- a/Example/MantisExample.xcodeproj/xcshareddata/xcschemes/MantisExample.xcscheme +++ b/Example/MantisExample.xcodeproj/xcshareddata/xcschemes/MantisExample.xcscheme @@ -51,6 +51,13 @@ ReferencedContainer = "container:MantisExample.xcodeproj"> + + + + CGContext? { + var actualBitsPerComponent = bitsPerComponent + var actualBitmapBytesPerRow = bitmapBytesPerRow + + /* Convert a 10-bit image to a 16-bit image to preserve accuracy. + Since we haven't successfully created a 10-bit image CGContext yet, we're temporarily using this method. + */ + if (32, 10) == (bitsPerPixel, bitsPerComponent) { + actualBitsPerComponent = 16 + actualBitmapBytesPerRow = Int(round(size.width)) * 8 + } + + return CGContext(data: nil, + width: Int(round(size.width)), + height: Int(round(size.height)), + bitsPerComponent: actualBitsPerComponent, + bytesPerRow: actualBitmapBytesPerRow, + space: colorSpaceRef, + bitmapInfo: getBackupBitmapInfo(colorSpaceRef)) + } + /** Just in case the bitmapInfo from original image is not supported by CGContext, we will use this backup bitmapInfo instead. */ @@ -100,11 +117,7 @@ extension CGImage { case (32, 8), (64, 16): return CGImageAlphaInfo.premultipliedLast.rawValue case (32, 10): - if #available(iOS 12, macOS 10.14, *) { - return CGImageAlphaInfo.alphaOnly.rawValue | CGImagePixelFormatInfo.RGBCIF10.rawValue - } else { - break - } + return CGImageAlphaInfo.premultipliedLast.rawValue case (128, 32): return CGImageAlphaInfo.premultipliedLast.rawValue | (bitmapInfo.rawValue & CGBitmapInfo.floatComponents.rawValue) default: diff --git a/Sources/Mantis/Extensions/UIImageExtensions.swift b/Sources/Mantis/Extensions/UIImageExtensions.swift index 0832aebe..bb4518f4 100644 --- a/Sources/Mantis/Extensions/UIImageExtensions.swift +++ b/Sources/Mantis/Extensions/UIImageExtensions.swift @@ -12,7 +12,7 @@ extension UIImage { if imageOrientation == .up { return cgImage } - + guard let cgImage = cgImage, let colorSpace = cgImage.colorSpace else { return nil } @@ -49,7 +49,7 @@ extension UIImage { break } - guard let context = CGContext( + var context = CGContext( data: nil, width: Int(width), height: Int(height), @@ -57,8 +57,14 @@ extension UIImage { bytesPerRow: 0, space: colorSpace, bitmapInfo: UInt32(cgImage.bitmapInfo.rawValue) - ) else { - return nil + ) + + if context == nil { + context = cgImage.createBackupCGContext(size: size, bitmapBytesPerRow: 0, colorSpaceRef: colorSpace) + } + + guard let context else { + return nil } context.concatenate(transform)