-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIImage+WebP.m
70 lines (56 loc) · 2.16 KB
/
UIImage+WebP.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2010 Google Inc.
//
// This code is licensed under the same terms as WebM:
// Software License Agreement: http://www.webmproject.org/license/software/
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
// -----------------------------------------------------------------------------
//
// WebPImageData.m
// ViewWebP
//
// Created by Somnath Banerjee ([email protected])
// Copyright 2011 Google Inc. All rights
#import "UIImage+WebP.h"
#import "WebP/decode.h"
@implementation UIImage (WebP)
static const int kWPUseThreads = 1;
// Callback for CGDataProviderRelease
static void FreeImageData(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, 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);
self = [self initWithCGImage:imageRef];
CGImageRelease(imageRef);
return self;
}
+ imageWithWebPData:(NSData *)data {
return [[self alloc] initWithWebPData:data];
}
@end