Fix potential memory leaks for ICCP && optimize ICCP handler

This commit is contained in:
zhongwuzw 2019-01-19 17:59:59 +08:00
parent bc50621670
commit fac703140c
1 changed files with 33 additions and 23 deletions

View File

@ -25,6 +25,30 @@
#endif
#import <Accelerate/Accelerate.h>
// Create and return the correct colorspace by checking the ICC Profile
static CGColorSpaceRef SDColorSpaceCreateWithDemuxer(WebPDemuxer *demuxer) {
// WebP contains ICC Profile should use the desired colorspace, instead of default device colorspace
// See: https://developers.google.com/speed/webp/docs/riff_container#color_profile
WebPChunkIterator chunk_iter;
CGColorSpaceRef colorSpaceRef = NULL;
int result = WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunk_iter);
if (result) {
NSData *profileData = [NSData dataWithBytesNoCopy:(void *)chunk_iter.chunk.bytes length:chunk_iter.chunk.size freeWhenDone:NO];
colorSpaceRef = CGColorSpaceCreateWithICCProfile((__bridge CFDataRef)profileData);
}
WebPDemuxReleaseChunkIterator(&chunk_iter);
if (!colorSpaceRef) {
colorSpaceRef = SDCGColorSpaceGetDeviceRGB();
CGColorSpaceRetain(colorSpaceRef);
}
return colorSpaceRef;
}
@implementation SDWebImageWebPCoder {
WebPIDecoder *_idec;
}
@ -87,7 +111,15 @@
WebPDemuxDelete(demuxer);
return nil;
}
CGColorSpaceRef colorSpace = [self sd_colorSpaceWithDemuxer:demuxer];
CGColorSpaceRef colorSpace = NULL;
// ICC profile
if (flags & ICCP_FLAG) {
colorSpace = SDColorSpaceCreateWithDemuxer(demuxer);
} else {
colorSpace = SDCGColorSpaceGetDeviceRGB();
CGColorSpaceRetain(colorSpace);
}
if (!(flags & ANIMATION_FLAG)) {
// for static single webp image
@ -333,28 +365,6 @@
return image;
}
// Create and return the correct colorspace by checking the ICC Profile
- (nonnull CGColorSpaceRef)sd_colorSpaceWithDemuxer:(nonnull WebPDemuxer *)demuxer CF_RETURNS_RETAINED {
// WebP contains ICC Profile should use the desired colorspace, instead of default device colorspace
// See: https://developers.google.com/speed/webp/docs/riff_container#color_profile
WebPChunkIterator chunk_iter;
CGColorSpaceRef colorSpaceRef = NULL;
int result = WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunk_iter);
if (result) {
NSData *profileData = [NSData dataWithBytes:chunk_iter.chunk.bytes length:chunk_iter.chunk.size];
colorSpaceRef = CGColorSpaceCreateWithICCProfile((__bridge CFDataRef)profileData);
}
if (!colorSpaceRef) {
colorSpaceRef = SDCGColorSpaceGetDeviceRGB();
CGColorSpaceRetain(colorSpaceRef);
}
return colorSpaceRef;
}
#pragma mark - Encode
- (BOOL)canEncodeToFormat:(SDImageFormat)format {
return (format == SDImageFormatWebP);