Skip to content

Commit 8113caf

Browse files
committed
Fix the WebP encoding which loss the image's input color space
Should prefer to use input color space and only use device RGB as fallback
1 parent a47710f commit 8113caf

File tree

7 files changed

+8
-2
lines changed

7 files changed

+8
-2
lines changed
Binary file not shown.
Binary file not shown.

SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,14 +883,20 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef
883883
}
884884

885885
uint8_t *rgba = NULL; // RGBA Buffer managed by CFData, don't call `free` on it, instead call `CFRelease` on `dataRef`
886-
// We could not assume that input CGImage's color mode is always RGB888/RGBA8888. Convert all other cases to target color mode using vImage
886+
// We must prefer the input CGImage's color space, which may contains ICC profile
887+
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
888+
if (!colorSpace) {
889+
colorSpace = [SDImageCoderHelper colorSpaceGetDeviceRGB];
890+
}
887891
vImage_CGImageFormat destFormat = {
888892
.bitsPerComponent = 8,
889893
.bitsPerPixel = hasAlpha ? 32 : 24,
890-
.colorSpace = [SDImageCoderHelper colorSpaceGetDeviceRGB],
894+
.colorSpace = colorSpace,
891895
.bitmapInfo = hasAlpha ? kCGImageAlphaLast | kCGBitmapByteOrderDefault : kCGImageAlphaNone | kCGBitmapByteOrderDefault // RGB888/RGBA8888 (Non-premultiplied to works for libwebp)
892896
};
893897
vImage_Buffer dest;
898+
// We could not assume that input CGImage's color mode is always RGB888/RGBA8888. Convert all other cases to target color mode using vImage
899+
// But vImageBuffer_InitWithCGImage will do convert automatically (unless you use `kvImageNoAllocate`), so no need to call `vImageConvert` by ourselves
894900
vImage_Error error = vImageBuffer_InitWithCGImage(&dest, &destFormat, NULL, imageRef, kvImageNoFlags);
895901
if (error != kvImageNoError) {
896902
return nil;

0 commit comments

Comments
 (0)