Created
January 6, 2014 10:27
-
-
Save cyndibaby905/8280852 to your computer and use it in GitHub Desktop.
WebP extension
This file contains hidden or 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
@interface UIImage(WebP) | |
- (id)initWithWebPData:(NSData *)data; | |
@end |
This file contains hidden or 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
@implementation UIImage(WebP) | |
static const int kWPUseThreads = 1; | |
// Callback for CGDataProviderRelease | |
static void WebPFreeImageData(void *info, const void *data, size_t size) { | |
free((void*)data); | |
} | |
- (id)initWithWebPData:(NSData *)data { | |
WebPDecoderConfig config; | |
if (!WebPInitDecoderConfig(&config)) { | |
return nil; | |
} | |
config.output.colorspace = MODE_rgbA; | |
config.options.use_threads = kWPUseThreads; | |
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, WebPFreeImageData); | |
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); | |
self = [self initWithCGImage:imageRef]; | |
self->_isWebP = YES; | |
CGImageRelease(imageRef); | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment