From 83ad6fa97739374eb89743d7b19ee0baf03fa53d Mon Sep 17 00:00:00 2001 From: flexih Date: Thu, 23 Oct 2014 12:14:43 +0800 Subject: [PATCH] Decoding webp, depends on source image data alpha status. --- SDWebImage/UIImage+WebP.m | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/SDWebImage/UIImage+WebP.m b/SDWebImage/UIImage+WebP.m index e6e84645..95c95a8f 100644 --- a/SDWebImage/UIImage+WebP.m +++ b/SDWebImage/UIImage+WebP.m @@ -24,7 +24,11 @@ static void FreeImageData(void *info, const void *data, size_t size) return nil; } - config.output.colorspace = MODE_rgbA; + if (WebPGetFeatures(data.bytes, data.length, &config.input) != VP8_STATUS_OK) { + return nil; + } + + config.output.colorspace = config.input.has_alpha ? MODE_rgbA : MODE_RGB; config.options.use_threads = 1; // Decode the WebP image data into a RGBA value array. @@ -43,9 +47,10 @@ static void FreeImageData(void *info, const void *data, size_t size) CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, config.output.u.RGBA.rgba, config.output.u.RGBA.size, FreeImageData); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); - CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast; + CGBitmapInfo bitmapInfo = config.input.has_alpha ? kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast : 0; + size_t components = config.input.has_alpha ? 4 : 3; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; - CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); + CGImageRef imageRef = CGImageCreate(width, height, 8, components * 8, components * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); CGColorSpaceRelease(colorSpaceRef); CGDataProviderRelease(provider);