From d91987f623a60145a9868f114f2cdad4e01cb124 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 27 Oct 2023 00:39:06 +0800 Subject: [PATCH] Support to detect the RGB16 bitmap info, fix the iOS 11- decode issues (#87) Support to detect the RGB 16 bits bitmap info Also, fallback when the libwebp don't support the target bitmap info, should not just return nil --- .../Classes/SDImageWebPCoder.m | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index 8d8403f..e2088d2 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -91,15 +91,21 @@ static inline CGContextRef _Nullable CreateWebPCanvas(BOOL hasAlpha, CGSize canv WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) { // Get alpha info, byteOrder info CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask; - CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask; + CGImageByteOrderInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask; + size_t bitsPerPixel = 8; + if (bitmapInfo & kCGBitmapFloatComponents) { + bitsPerPixel = 16; // 16-Bits, which don't support currently! + } BOOL byteOrderNormal = NO; switch (byteOrderInfo) { - case kCGBitmapByteOrderDefault: { + case kCGImageByteOrderDefault: { byteOrderNormal = YES; } break; - case kCGBitmapByteOrder32Little: { + case kCGImageByteOrder32Little: + case kCGImageByteOrder16Little: { } break; - case kCGBitmapByteOrder32Big: { + case kCGImageByteOrder32Big: + case kCGImageByteOrder16Big: { byteOrderNormal = YES; } break; default: break; @@ -161,7 +167,7 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) { break; case kCGImageAlphaOnly: { // A - // Unsupported + // Unsupported! return MODE_LAST; } break; @@ -621,8 +627,13 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) { CGBitmapInfo bitmapInfo = pixelFormat.bitmapInfo; WEBP_CSP_MODE mode = ConvertCSPMode(bitmapInfo); if (mode == MODE_LAST) { - NSAssert(NO, @"Unsupported libwebp preferred CGBitmapInfo: %d", bitmapInfo); - return nil; +#if DEBUG + NSLog(@"Unsupported libwebp preferred CGBitmapInfo: %d", bitmapInfo); +#endif + // Fallback to RGBA8888/RGB888 instead + mode = MODE_rgbA; + bitmapInfo = kCGBitmapByteOrderDefault; + bitmapInfo |= hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast; } config.output.colorspace = mode; config.options.use_threads = 1;