Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add webp decoding

  • Loading branch information...
commit d8e5553a3ad5320782086a23eb0eb40faa14b1a1 1 parent d4e6c16
@kkaefer kkaefer authored
View
22 src/reader.cpp
@@ -1,4 +1,5 @@
#include "reader.hpp"
+#include <webp/decode.h>
#include <exception>
@@ -202,11 +203,30 @@ JPEGImageReader::~JPEGImageReader() {
}
+WebPImageReader::WebPImageReader(unsigned char* src, size_t len) :
+ ImageReader(src, len) {
+ WebPDecoderConfig config;
+ if (WebPGetFeatures(source, length, &config.input) == VP8_STATUS_OK) {
+ alpha = config.input.has_alpha;
+ width = config.input.width;
+ height = config.input.height;
+ }
+}
+
+bool WebPImageReader::decode() {
+ surface = (unsigned int *)WebPDecodeRGBA(source, length, NULL, NULL);
+ return surface != NULL;
+}
+
ImageReader* ImageReader::create(unsigned char* src, size_t len) {
if (png_sig_cmp((png_bytep)src, 0, 8) == 0) {
return new PNGImageReader(src, len);
- } else if (src[0] == 255 && src[1] == 216) {
+ } else if (len >= 2 && src[0] == 255 && src[1] == 216) {
return new JPEGImageReader(src, len);
+ } else if (len >= 12 &&
+ src[0] == 'R' && src[1] == 'I' && src[2] == 'F' && src[3] == 'F' &&
+ src[8] == 'W' && src[9] == 'E' && src[10] == 'B' && src[11] == 'P') {
+ return new WebPImageReader(src, len);
} else {
return new ImageReader("Unknown image format");
}
View
8 src/reader.hpp
@@ -84,4 +84,12 @@ class JPEGImageReader : public ImageReader {
JPEGErrorManager err;
};
+
+class WebPImageReader : public ImageReader {
+public:
+ WebPImageReader(unsigned char* src, size_t len);
+ bool decode();
+};
+
+
#endif
View
23 test/blend-single.test.js
@@ -8,7 +8,8 @@ var utilities = require('./support/utilities');
var images = [
fs.readFileSync('test/fixture/1.png'),
fs.readFileSync('test/fixture/2.png'),
- fs.readFileSync('test/fixture/pattern.png')
+ fs.readFileSync('test/fixture/pattern.png'),
+ fs.readFileSync('test/fixture/9.webp')
];
@@ -87,6 +88,26 @@ describe('reencode', function() {
});
});
+ it('should read WebP and reencode as PNG True color', function(done) {
+ blend([ images[3] ], { reencode: true }, function(err, data, warnings) {
+ if (err) return done(err);
+ assert.deepEqual(warnings, []);
+ assert.ok(data.length < 25000);
+ assert.ok(data.length > 20000);
+ utilities.imageEqualsFile(data, 'test/fixture/results/32.png', done);
+ });
+ });
+
+ it('should read WebP and reencode as PNG 128', function(done) {
+ blend([ images[3] ], { reencode: true, quality: 128 }, function(err, data, warnings) {
+ if (err) return done(err);
+ assert.deepEqual(warnings, []);
+ assert.ok(data.length < 15000);
+ assert.ok(data.length > 10000);
+ utilities.imageEqualsFile(data, 'test/fixture/results/33.png', done);
+ });
+ });
+
it('should reencode 2 images with the uppermost opaque', function(done) {
blend([ images[1], images[0] ], { reencode: true }, function(err, data, warnings) {
if (err) return done(err);
View
BIN  test/fixture/9.webp
Binary file not shown
View
BIN  test/fixture/results/32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  test/fixture/results/33.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.