Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加载webp图片颜色显示不对 #174

Open
DoromGeng opened this issue Jan 18, 2024 · 1 comment
Open

加载webp图片颜色显示不对 #174

DoromGeng opened this issue Jan 18, 2024 · 1 comment

Comments

@DoromGeng
Copy link

DoromGeng commented Jan 18, 2024

问题:图片背景鲜红色变成了暗红色
图片链接:https://postimg.cc/FYJ7ysH5/1106a583
效果对比
原始图
8f75540f-5f98-4c13-ac66-3cd07ca0bd90
YY加载图
image

@DoromGeng
Copy link
Author

DoromGeng commented Jan 18, 2024

一、添加新方法
`- (nonnull CGColorSpaceRef)createColorSpaceWithDemuxer:(nonnull WebPDemuxer *)demuxer CF_RETURNS_RETAINED
{
CGColorSpaceRef colorSpaceRef = NULL;
uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS);

if (flags & ICCP_FLAG) {
    WebPChunkIterator chunk_iter;
    int result = WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunk_iter);
    if (result) {
        // See #2618, the `CGColorSpaceCreateWithICCProfile` does not copy ICC Profile data, it only retain `CFDataRef`.
        // When the libwebp `WebPDemuxer` dealloc, all chunks will be freed. So we must copy the ICC data (really cheap, less than 10KB)
        NSData *profileData = [NSData dataWithBytes:chunk_iter.chunk.bytes length:chunk_iter.chunk.size];
        if (@available(iOS 10, tvOS 10, macOS 10.12, watchOS 3, *)) {
            colorSpaceRef = CGColorSpaceCreateWithICCData((__bridge CFDataRef)profileData);
        } else {
            colorSpaceRef = CGColorSpaceCreateWithICCProfile((__bridge CFDataRef)profileData);
        }
        WebPDemuxReleaseChunkIterator(&chunk_iter);
        if (colorSpaceRef) {
            // We use RGB color model to decode WebP images currently, so we must filter out other colorSpace
            CGColorSpaceModel model = CGColorSpaceGetModel(colorSpaceRef);
            if (model != kCGColorSpaceModelRGB) {
                CGColorSpaceRelease(colorSpaceRef);
                colorSpaceRef = NULL;
            }
        }
    }
}

return colorSpaceRef;

}
`

二、在2195行修改为如下代码-使用webp本身的颜色空间

CGColorSpaceRef colorspce = [self createColorSpaceWithDemuxer:_webpSource]; if(!colorspce) { colorspce = YYCGColorSpaceGetDeviceRGB(); } CGImageRef image = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspce, bitmapInfo, provider, NULL, false, kCGRenderingIntentDefault);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant