@@ -736,6 +736,9 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
736
736
}
737
737
738
738
size_t bytesPerRow = CGImageGetBytesPerRow (imageRef);
739
+ size_t bitsPerComponent = CGImageGetBitsPerComponent (imageRef);
740
+ size_t bitsPerPixel = CGImageGetBitsPerPixel (imageRef);
741
+ size_t components = bitsPerPixel / bitsPerComponent;
739
742
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo (imageRef);
740
743
CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask ;
741
744
CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask ;
@@ -766,7 +769,9 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
766
769
767
770
uint8_t *rgba = NULL ; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
768
771
// We could not assume that input CGImage's color mode is always RGB888/RGBA8888. Convert all other cases to target color mode using vImage
769
- if (byteOrderNormal && ((alphaInfo == kCGImageAlphaNone ) || (alphaInfo == kCGImageAlphaLast ))) {
772
+ BOOL isRGB888 = byteOrderNormal && alphaInfo == kCGImageAlphaNone && components == 3 ;
773
+ BOOL isRGBA8888 = byteOrderNormal && alphaInfo == kCGImageAlphaLast && components == 4 ;
774
+ if (isRGB888 || isRGBA8888) {
770
775
// If the input CGImage is already RGB888/RGBA8888
771
776
rgba = (uint8_t *)CFDataGetBytePtr (dataRef);
772
777
} else {
@@ -775,8 +780,8 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
775
780
vImage_Error error = kvImageNoError;
776
781
777
782
vImage_CGImageFormat srcFormat = {
778
- .bitsPerComponent = (uint32_t )CGImageGetBitsPerComponent (imageRef) ,
779
- .bitsPerPixel = (uint32_t )CGImageGetBitsPerPixel (imageRef) ,
783
+ .bitsPerComponent = (uint32_t )bitsPerComponent ,
784
+ .bitsPerPixel = (uint32_t )bitsPerPixel ,
780
785
.colorSpace = CGImageGetColorSpace (imageRef),
781
786
.bitmapInfo = bitmapInfo
782
787
};
0 commit comments