forked from SDWebImage/SDWebImage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebP format support (fix SDWebImage#410)
How awesome is that?! =)
- Loading branch information
Olivier Poitrey
committed
Jun 7, 2013
1 parent
1dbb79f
commit 04fa4c9
Showing
9 changed files
with
381 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "Vendors/libwebp"] | ||
path = Vendors/libwebp | ||
url = http://git.chromium.org/webm/libwebp.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// UIImage+WebP.h | ||
// SDWebImage | ||
// | ||
// Created by Olivier Poitrey on 07/06/13. | ||
// Copyright (c) 2013 Dailymotion. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface UIImage (WebP) | ||
|
||
+ (UIImage *)sd_imageWithWebPData:(NSData *)data; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// UIImage+WebP.m | ||
// SDWebImage | ||
// | ||
// Created by Olivier Poitrey on 07/06/13. | ||
// Copyright (c) 2013 Dailymotion. All rights reserved. | ||
// | ||
|
||
#import "UIImage+WebP.h" | ||
#import "webp/decode.h" | ||
|
||
// Callback for CGDataProviderRelease | ||
static void FreeImageData(void *info, const void *data, size_t size) | ||
{ | ||
free((void *)data); | ||
} | ||
|
||
@implementation UIImage (WebP) | ||
|
||
+ (UIImage *)sd_imageWithWebPData:(NSData *)data | ||
{ | ||
WebPDecoderConfig config; | ||
if (!WebPInitDecoderConfig(&config)) | ||
{ | ||
return nil; | ||
} | ||
|
||
config.output.colorspace = MODE_rgbA; | ||
config.options.use_threads = 1; | ||
|
||
// Decode the WebP image data into a RGBA value array. | ||
if (WebPDecode(data.bytes, data.length, &config) != VP8_STATUS_OK) | ||
{ | ||
return nil; | ||
} | ||
|
||
int width = config.input.width; | ||
int height = config.input.height; | ||
if (config.options.use_scaling) | ||
{ | ||
width = config.options.scaled_width; | ||
height = config.options.scaled_height; | ||
} | ||
|
||
// Construct a UIImage from the decoded RGBA value array. | ||
CGDataProviderRef provider = | ||
CGDataProviderCreateWithData(NULL, config.output.u.RGBA.rgba, config.output.u.RGBA.size, FreeImageData); | ||
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); | ||
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast; | ||
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; | ||
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); | ||
|
||
CGColorSpaceRelease(colorSpaceRef); | ||
CGDataProviderRelease(provider); | ||
|
||
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef]; | ||
CGImageRelease(imageRef); | ||
|
||
return image; | ||
} | ||
|
||
@end |
Submodule libwebp
added at
c2113a