Skip to content

Commit cab8cd9

Browse files
committed
Fix the issue when WebP Encoding grayscale image with only 1 channel, which cause the wrong encoded WebP image (each column repeats 3 times)
1 parent cdf6ce8 commit cab8cd9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
736736
}
737737

738738
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
739+
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
740+
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
741+
size_t components = bitsPerPixel / bitsPerComponent;
739742
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
740743
CGImageAlphaInfo alphaInfo = bitmapInfo & kCGBitmapAlphaInfoMask;
741744
CGBitmapInfo byteOrderInfo = bitmapInfo & kCGBitmapByteOrderMask;
@@ -766,7 +769,9 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
766769

767770
uint8_t *rgba = NULL; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
768771
// 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) {
770775
// If the input CGImage is already RGB888/RGBA8888
771776
rgba = (uint8_t *)CFDataGetBytePtr(dataRef);
772777
} else {
@@ -775,8 +780,8 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
775780
vImage_Error error = kvImageNoError;
776781

777782
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,
780785
.colorSpace = CGImageGetColorSpace(imageRef),
781786
.bitmapInfo = bitmapInfo
782787
};

0 commit comments

Comments
 (0)