Permalink
Browse files

Add WebP format support (fix #410)

How awesome is that?! =)
  • Loading branch information...
1 parent 1dbb79f commit 04fa4c97b5adad8f1fefb0e4be52053c8a0f93c4 @rs committed Jun 7, 2013
View
@@ -0,0 +1,3 @@
+[submodule "Vendors/libwebp"]
@flovilmart

flovilmart Jun 11, 2015

Breaks with Carthage:

fatal: http://git.chromium.org/webm/libwebp.git/info/refs not valid: is this a git repository?

@bpoplauschi

bpoplauschi Jun 24, 2015

Collaborator

There is an issue with the libwebp repo

@flovilmart

flovilmart Jun 24, 2015

That's wht i figured out... Is that one the new one? https://github.com/webmproject/libwebp

@flovilmart

flovilmart Jun 24, 2015

The github is a mirror only so maybe https://chromium.googlesource.com/webm/libwebp would be better no?

@bpoplauschi

bpoplauschi Jun 24, 2015

Collaborator

The new one is https://chromium.googlesource.com/webm/libwebp. The one you linked it a mirror of the chromium.googlesource one. The issue I had was my local submodule did not update properly and I had to manually change the remote url.

@flovilmart

flovilmart Jun 24, 2015

Will you push a hot fix you want me to do it?

@bpoplauschi

bpoplauschi Jun 24, 2015

Collaborator

I'm not sure :) Master currently points to https://chromium.googlesource.com/webm/libwebp. Not sure if we need to do anything more

@flovilmart

flovilmart via email Jun 24, 2015

+ path = Vendors/libwebp
+ url = http://git.chromium.org/webm/libwebp.git
@@ -31,6 +31,8 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
action:@selector(flushCache)];
_objects = [NSArray arrayWithObjects:
@"http://assets.sbnation.com/assets/2512203/dogflops.gif",
+ @"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp",
+ @"http://www.ioncannon.net/wp-content/uploads/2011/06/test9.webp",
@"http://static2.dmcdn.net/static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705",
@"http://static2.dmcdn.net/static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018",
@"http://static2.dmcdn.net/static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749",
View
@@ -9,6 +9,7 @@ It provides:
- An asynchronous image downloader
- An asynchronous memory + disk image caching with automatic cache expiration handling
- Animated GIF support
+- WebP format support
- A background image decompression
- A guarantee that the same URL won't be downloaded several times
- A guarantee that bogus URLs won't be retried again and again

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -70,7 +70,7 @@ - (id)init
_downloadQueue = NSOperationQueue.new;
_downloadQueue.maxConcurrentOperationCount = 2;
_URLCallbacks = NSMutableDictionary.new;
- _HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/*" forKey:@"Accept"];
+ _HTTPHeaders = [NSMutableDictionary dictionaryWithObject:@"image/webp,image/*;q=0.8" forKey:@"Accept"];
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
}
return self;
@@ -8,6 +8,7 @@
#import "UIImage+MultiFormat.h"
#import "UIImage+GIF.h"
+#import "UIImage+WebP.h"
@implementation UIImage (MultiFormat)
@@ -24,6 +25,11 @@ + (UIImage *)sd_imageWithData:(NSData *)data
image = [[UIImage alloc] initWithData:data];
}
+ if (!image) // TODO: detect webp signature
+ {
+ image = [UIImage sd_imageWithWebPData:data];
+ }
+
return image;
}
View
@@ -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
View
@@ -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

7 comments on commit 04fa4c9

Contributor

NachoSoto replied Jun 7, 2013

Nice

Contributor

robertmryan replied Jun 10, 2013

Very cool. But does this introduce a dependency on a third-party library? I just did a clone of SDWebImage, tried to use it in a project, and I'm getting cryptic webp/decode.h error.

.../SDWebImage/UIImage+WebP.m:10:9: 'webp/decode.h' file not found

If I try removing UIImage+WebP.m from my list of compile sources, it avoids the compile warnings, but I then get an exception ]

'+[UIImage sd_imageWithWebPData:]: unrecognized selector sent to class 0xaf5c54'

It seems that there should be conditional checks for the presence of libwebp. And, at the very least, the instructions should say how to include this third party library that now appears to be required for SDWebImage.

me to!

cool

how to solve it

Contributor

robertmryan replied Jan 27, 2014

@yuyubaba this was fixed a while ago: 5ecb8d5

If you use the latest rendition of SDWebImage, you shouldn't have this problem.

@ robertmryan WebP must be import by self.

Please sign in to comment.