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
This commit is contained in:
DreamPiggy 2023-10-27 00:39:06 +08:00 committed by GitHub
parent b442fafefe
commit d91987f623
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 7 deletions

View File

@ -91,15 +91,21 @@ static inline CGContextRef _Nullable CreateWebPCanvas(BOOL hasAlpha, CGSize canv
WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) { WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) {
// Get alpha info, byteOrder info // Get alpha info, byteOrder info
CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask; 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; BOOL byteOrderNormal = NO;
switch (byteOrderInfo) { switch (byteOrderInfo) {
case kCGBitmapByteOrderDefault: { case kCGImageByteOrderDefault: {
byteOrderNormal = YES; byteOrderNormal = YES;
} break; } break;
case kCGBitmapByteOrder32Little: { case kCGImageByteOrder32Little:
case kCGImageByteOrder16Little: {
} break; } break;
case kCGBitmapByteOrder32Big: { case kCGImageByteOrder32Big:
case kCGImageByteOrder16Big: {
byteOrderNormal = YES; byteOrderNormal = YES;
} break; } break;
default: break; default: break;
@ -161,7 +167,7 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) {
break; break;
case kCGImageAlphaOnly: { case kCGImageAlphaOnly: {
// A // A
// Unsupported // Unsupported!
return MODE_LAST; return MODE_LAST;
} }
break; break;
@ -621,8 +627,13 @@ WEBP_CSP_MODE ConvertCSPMode(CGBitmapInfo bitmapInfo) {
CGBitmapInfo bitmapInfo = pixelFormat.bitmapInfo; CGBitmapInfo bitmapInfo = pixelFormat.bitmapInfo;
WEBP_CSP_MODE mode = ConvertCSPMode(bitmapInfo); WEBP_CSP_MODE mode = ConvertCSPMode(bitmapInfo);
if (mode == MODE_LAST) { if (mode == MODE_LAST) {
NSAssert(NO, @"Unsupported libwebp preferred CGBitmapInfo: %d", bitmapInfo); #if DEBUG
return nil; 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.output.colorspace = mode;
config.options.use_threads = 1; config.options.use_threads = 1;