@@ -883,14 +883,20 @@ - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef |
883 | 883 | } |
884 | 884 |
|
885 | 885 | 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 | + } |
887 | 891 | vImage_CGImageFormat destFormat = { |
888 | 892 | .bitsPerComponent = 8, |
889 | 893 | .bitsPerPixel = hasAlpha ? 32 : 24, |
890 | | - .colorSpace = [SDImageCoderHelper colorSpaceGetDeviceRGB], |
| 894 | + .colorSpace = colorSpace, |
891 | 895 | .bitmapInfo = hasAlpha ? kCGImageAlphaLast | kCGBitmapByteOrderDefault : kCGImageAlphaNone | kCGBitmapByteOrderDefault // RGB888/RGBA8888 (Non-premultiplied to works for libwebp) |
892 | 896 | }; |
893 | 897 | 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 |
894 | 900 | vImage_Error error = vImageBuffer_InitWithCGImage(&dest, &destFormat, NULL, imageRef, kvImageNoFlags); |
895 | 901 | if (error != kvImageNoError) { |
896 | 902 | return nil; |
|