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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
/*
* Copyright 2010, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkColorPriv.h"
#include "SkScaledBitmapSampler.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkUtils.h"
// A WebP decoder only, on top of (subset of) libwebp
// For more information on WebP image format, and libwebp library, see:
// http://code.google.com/speed/webp/
// http://www.webmproject.org/code/#libwebp_webp_image_decoder_library
// http://review.webmproject.org/gitweb?p=libwebp.git
#include <stdio.h>
extern "C" {
// If moving libwebp out of skia source tree, path for webp headers must be
// updated accordingly. Here, we enforce using local copy in webp sub-directory.
#include "webp/decode.h"
#include "webp/encode.h"
}
// this enables timing code to report milliseconds for a decode
//#define TIME_DECODE
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Define VP8 I/O on top of Skia stream
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
static const size_t WEBP_VP8_HEADER_SIZE = 64;
static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16);
// Parse headers of RIFF container, and check for valid Webp (VP8) content.
static bool webp_parse_header(SkStream* stream, int* width, int* height, int* alpha) {
unsigned char buffer[WEBP_VP8_HEADER_SIZE];
size_t bytesToRead = WEBP_VP8_HEADER_SIZE;
size_t totalBytesRead = 0;
do {
unsigned char* dst = buffer + totalBytesRead;
const size_t bytesRead = stream->read(dst, bytesToRead);
if (0 == bytesRead) {
// Could not read any bytes. Check to see if we are at the end (exit
// condition), and continue reading if not. Important for streams
// that do not have all the data ready.
continue;
}
bytesToRead -= bytesRead;
totalBytesRead += bytesRead;
SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE);
} while (!stream->isAtEnd() && bytesToRead > 0);
WebPBitstreamFeatures features;
VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features);
if (VP8_STATUS_OK != status) {
return false; // Invalid WebP file.
}
*width = features.width;
*height = features.height;
*alpha = features.has_alpha;
// sanity check for image size that's about to be decoded.
{
int64_t size = sk_64_mul(*width, *height);
if (!sk_64_isS32(size)) {
return false;
}
// now check that if we are 4-bytes per pixel, we also don't overflow
if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
return false;
}
}
return true;
}
class SkWEBPImageDecoder: public SkImageDecoder {
public:
SkWEBPImageDecoder() {
fOrigWidth = 0;
fOrigHeight = 0;
fHasAlpha = 0;
}
Format getFormat() const SK_OVERRIDE {
return kWEBP_Format;
}
protected:
bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) SK_OVERRIDE;
bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) SK_OVERRIDE;
Result onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
private:
/**
* Called when determining the output config to request to webp.
* If the image does not have alpha, there is no need to premultiply.
* If the caller wants unpremultiplied colors, that is respected.
*/
bool shouldPremultiply() const {
return SkToBool(fHasAlpha) && !this->getRequireUnpremultipliedColors();
}
bool setDecodeConfig(SkBitmap* decodedBitmap, int width, int height);
SkAutoTDelete<SkStream> fInputStream;
int fOrigWidth;
int fOrigHeight;
int fHasAlpha;
typedef SkImageDecoder INHERITED;
};
//////////////////////////////////////////////////////////////////////////
#ifdef TIME_DECODE
#include "SkTime.h"
class AutoTimeMillis {
public:
AutoTimeMillis(const char label[]) :
fLabel(label) {
if (NULL == fLabel) {
fLabel = "";
}
fNow = SkTime::GetMSecs();
}
~AutoTimeMillis() {
SkDebugf("---- Time (ms): %s %d\n", fLabel, SkTime::GetMSecs() - fNow);
}
private:
const char* fLabel;
SkMSec fNow;
};
#endif
///////////////////////////////////////////////////////////////////////////////
// This guy exists just to aid in debugging, as it allows debuggers to just
// set a break-point in one place to see all error exists.
static void print_webp_error(const SkBitmap& bm, const char msg[]) {
SkDEBUGF(("libwebp error %s [%d %d]", msg, bm.width(), bm.height()));
}
static bool return_false(const SkBitmap& bm, const char msg[]) {
print_webp_error(bm, msg);
return false; // must always return false
}
static SkImageDecoder::Result return_failure(const SkBitmap& bm, const char msg[]) {
print_webp_error(bm, msg);
return SkImageDecoder::kFailure; // must always return kFailure
}
///////////////////////////////////////////////////////////////////////////////
static WEBP_CSP_MODE webp_decode_mode(const SkBitmap* decodedBitmap, bool premultiply) {
WEBP_CSP_MODE mode = MODE_LAST;
const SkColorType ct = decodedBitmap->colorType();
if (ct == kN32_SkColorType) {
#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
mode = premultiply ? MODE_bgrA : MODE_BGRA;
#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
mode = premultiply ? MODE_rgbA : MODE_RGBA;
#else
#error "Skia uses BGRA or RGBA byte order"
#endif
} else if (ct == kARGB_4444_SkColorType) {
mode = premultiply ? MODE_rgbA_4444 : MODE_RGBA_4444;
} else if (ct == kRGB_565_SkColorType) {
mode = MODE_RGB_565;
}
SkASSERT(MODE_LAST != mode);
return mode;
}
// Incremental WebP image decoding. Reads input buffer of 64K size iteratively
// and decodes this block to appropriate color-space as per config object.
static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) {
WebPIDecoder* idec = WebPIDecode(NULL, 0, config);
if (NULL == idec) {
WebPFreeDecBuffer(&config->output);
return false;
}
if (!stream->rewind()) {
SkDebugf("Failed to rewind webp stream!");
return false;
}
const size_t readBufferSize = stream->hasLength() ?
SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ;
SkAutoMalloc srcStorage(readBufferSize);
unsigned char* input = (uint8_t*)srcStorage.get();
if (NULL == input) {
WebPIDelete(idec);
WebPFreeDecBuffer(&config->output);
return false;
}
bool success = true;
VP8StatusCode status = VP8_STATUS_SUSPENDED;
do {
const size_t bytesRead = stream->read(input, readBufferSize);
if (0 == bytesRead) {
success = false;
break;
}
status = WebPIAppend(idec, input, bytesRead);
if (VP8_STATUS_OK != status && VP8_STATUS_SUSPENDED != status) {
success = false;
break;
}
} while (VP8_STATUS_OK != status);
srcStorage.free();
WebPIDelete(idec);
WebPFreeDecBuffer(&config->output);
return success;
}
static bool webp_get_config_resize(WebPDecoderConfig* config,
SkBitmap* decodedBitmap,
int width, int height, bool premultiply) {
WEBP_CSP_MODE mode = webp_decode_mode(decodedBitmap, premultiply);
if (MODE_LAST == mode) {
return false;
}
if (0 == WebPInitDecoderConfig(config)) {
return false;
}
config->output.colorspace = mode;
config->output.u.RGBA.rgba = (uint8_t*)decodedBitmap->getPixels();
config->output.u.RGBA.stride = (int) decodedBitmap->rowBytes();
config->output.u.RGBA.size = decodedBitmap->getSize();
config->output.is_external_memory = 1;
if (width != decodedBitmap->width() || height != decodedBitmap->height()) {
config->options.use_scaling = 1;
config->options.scaled_width = decodedBitmap->width();
config->options.scaled_height = decodedBitmap->height();
}
return true;
}
static bool webp_get_config_resize_crop(WebPDecoderConfig* config,
SkBitmap* decodedBitmap,
const SkIRect& region, bool premultiply) {
if (!webp_get_config_resize(config, decodedBitmap, region.width(),
region.height(), premultiply)) {
return false;
}
config->options.use_cropping = 1;
config->options.crop_left = region.fLeft;
config->options.crop_top = region.fTop;
config->options.crop_width = region.width();
config->options.crop_height = region.height();
return true;
}
bool SkWEBPImageDecoder::setDecodeConfig(SkBitmap* decodedBitmap, int width, int height) {
SkColorType colorType = this->getPrefColorType(k32Bit_SrcDepth, SkToBool(fHasAlpha));
// YUV converter supports output in RGB565, RGBA4444 and RGBA8888 formats.
if (fHasAlpha) {
if (colorType != kARGB_4444_SkColorType) {
colorType = kN32_SkColorType;
}
} else {
if (colorType != kRGB_565_SkColorType && colorType != kARGB_4444_SkColorType) {
colorType = kN32_SkColorType;
}
}
SkAlphaType alphaType = kOpaque_SkAlphaType;
if (SkToBool(fHasAlpha)) {
if (this->getRequireUnpremultipliedColors()) {
alphaType = kUnpremul_SkAlphaType;
} else {
alphaType = kPremul_SkAlphaType;
}
}
return decodedBitmap->setInfo(SkImageInfo::Make(width, height, colorType, alphaType));
}
bool SkWEBPImageDecoder::onBuildTileIndex(SkStreamRewindable* stream,
int *width, int *height) {
SkAutoTDelete<SkStreamRewindable> streamDeleter(stream);
int origWidth, origHeight, hasAlpha;
if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
return false;
}
if (!stream->rewind()) {
SkDebugf("Failed to rewind webp stream!");
return false;
}
*width = origWidth;
*height = origHeight;
this->fInputStream.reset(streamDeleter.detach());
this->fOrigWidth = origWidth;
this->fOrigHeight = origHeight;
this->fHasAlpha = hasAlpha;
return true;
}
static bool is_config_compatible(const SkBitmap& bitmap) {
const SkColorType ct = bitmap.colorType();
return ct == kARGB_4444_SkColorType || ct == kRGB_565_SkColorType || ct == kN32_SkColorType;
}
bool SkWEBPImageDecoder::onDecodeSubset(SkBitmap* decodedBitmap,
const SkIRect& region) {
SkIRect rect = SkIRect::MakeWH(fOrigWidth, fOrigHeight);
if (!rect.intersect(region)) {
// If the requested region is entirely outsides the image, return false
return false;
}
const int sampleSize = this->getSampleSize();
SkScaledBitmapSampler sampler(rect.width(), rect.height(), sampleSize);
const int width = sampler.scaledWidth();
const int height = sampler.scaledHeight();
// The image can be decoded directly to decodedBitmap if
// 1. the region is within the image range
// 2. bitmap's config is compatible
// 3. bitmap's size is same as the required region (after sampled)
bool directDecode = (rect == region) &&
(decodedBitmap->isNull() ||
(is_config_compatible(*decodedBitmap) &&
(decodedBitmap->width() == width) &&
(decodedBitmap->height() == height)));
SkBitmap tmpBitmap;
SkBitmap *bitmap = decodedBitmap;
if (!directDecode) {
bitmap = &tmpBitmap;
}
if (bitmap->isNull()) {
if (!setDecodeConfig(bitmap, width, height)) {
return false;
}
// alloc from native heap if it is a temp bitmap. (prevent GC)
bool allocResult = (bitmap == decodedBitmap)
? allocPixelRef(bitmap, NULL)
: bitmap->tryAllocPixels();
if (!allocResult) {
return return_false(*decodedBitmap, "allocPixelRef");
}
}
SkAutoLockPixels alp(*bitmap);
WebPDecoderConfig config;
if (!webp_get_config_resize_crop(&config, bitmap, rect,
this->shouldPremultiply())) {
return false;
}
// Decode the WebP image data stream using WebP incremental decoding for
// the specified cropped image-region.
if (!webp_idecode(this->fInputStream, &config)) {
return false;
}
if (!directDecode) {
cropBitmap(decodedBitmap, bitmap, sampleSize, region.x(), region.y(),
region.width(), region.height(), rect.x(), rect.y());
}
return true;
}
SkImageDecoder::Result SkWEBPImageDecoder::onDecode(SkStream* stream, SkBitmap* decodedBitmap,
Mode mode) {
#ifdef TIME_DECODE
AutoTimeMillis atm("WEBP Decode");
#endif
int origWidth, origHeight, hasAlpha;
if (!webp_parse_header(stream, &origWidth, &origHeight, &hasAlpha)) {
return kFailure;
}
this->fHasAlpha = hasAlpha;
const int sampleSize = this->getSampleSize();
SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
if (!setDecodeConfig(decodedBitmap, sampler.scaledWidth(),
sampler.scaledHeight())) {
return kFailure;
}
// If only bounds are requested, done
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
return kSuccess;
}
if (!this->allocPixelRef(decodedBitmap, NULL)) {
return return_failure(*decodedBitmap, "allocPixelRef");
}
SkAutoLockPixels alp(*decodedBitmap);
WebPDecoderConfig config;
if (!webp_get_config_resize(&config, decodedBitmap, origWidth, origHeight,
this->shouldPremultiply())) {
return kFailure;
}
// Decode the WebP image data stream using WebP incremental decoding.
return webp_idecode(stream, &config) ? kSuccess : kFailure;
}
///////////////////////////////////////////////////////////////////////////////
#include "SkUnPreMultiply.h"
typedef void (*ScanlineImporter)(const uint8_t* in, uint8_t* out, int width,
const SkPMColor* SK_RESTRICT ctable);
static void ARGB_8888_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
const SkPMColor*) {
const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
for (int i = 0; i < width; ++i) {
const uint32_t c = *src++;
rgb[0] = SkGetPackedR32(c);
rgb[1] = SkGetPackedG32(c);
rgb[2] = SkGetPackedB32(c);
rgb += 3;
}
}
static void ARGB_8888_To_RGBA(const uint8_t* in, uint8_t* rgb, int width,
const SkPMColor*) {
const uint32_t* SK_RESTRICT src = (const uint32_t*)in;
const SkUnPreMultiply::Scale* SK_RESTRICT table =
SkUnPreMultiply::GetScaleTable();
for (int i = 0; i < width; ++i) {
const uint32_t c = *src++;
uint8_t a = SkGetPackedA32(c);
uint8_t r = SkGetPackedR32(c);
uint8_t g = SkGetPackedG32(c);
uint8_t b = SkGetPackedB32(c);
if (0 != a && 255 != a) {
SkUnPreMultiply::Scale scale = table[a];
r = SkUnPreMultiply::ApplyScale(scale, r);
g = SkUnPreMultiply::ApplyScale(scale, g);
b = SkUnPreMultiply::ApplyScale(scale, b);
}
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
rgb[3] = a;
rgb += 4;
}
}
static void RGB_565_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
const SkPMColor*) {
const uint16_t* SK_RESTRICT src = (const uint16_t*)in;
for (int i = 0; i < width; ++i) {
const uint16_t c = *src++;
rgb[0] = SkPacked16ToR32(c);
rgb[1] = SkPacked16ToG32(c);
rgb[2] = SkPacked16ToB32(c);
rgb += 3;
}
}
static void ARGB_4444_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
const SkPMColor*) {
const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
for (int i = 0; i < width; ++i) {
const SkPMColor16 c = *src++;
rgb[0] = SkPacked4444ToR32(c);
rgb[1] = SkPacked4444ToG32(c);
rgb[2] = SkPacked4444ToB32(c);
rgb += 3;
}
}
static void ARGB_4444_To_RGBA(const uint8_t* in, uint8_t* rgb, int width,
const SkPMColor*) {
const SkPMColor16* SK_RESTRICT src = (const SkPMColor16*)in;
const SkUnPreMultiply::Scale* SK_RESTRICT table =
SkUnPreMultiply::GetScaleTable();
for (int i = 0; i < width; ++i) {
const SkPMColor16 c = *src++;
uint8_t a = SkPacked4444ToA32(c);
uint8_t r = SkPacked4444ToR32(c);
uint8_t g = SkPacked4444ToG32(c);
uint8_t b = SkPacked4444ToB32(c);
if (0 != a && 255 != a) {
SkUnPreMultiply::Scale scale = table[a];
r = SkUnPreMultiply::ApplyScale(scale, r);
g = SkUnPreMultiply::ApplyScale(scale, g);
b = SkUnPreMultiply::ApplyScale(scale, b);
}
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
rgb[3] = a;
rgb += 4;
}
}
static void Index8_To_RGB(const uint8_t* in, uint8_t* rgb, int width,
const SkPMColor* SK_RESTRICT ctable) {
const uint8_t* SK_RESTRICT src = (const uint8_t*)in;
for (int i = 0; i < width; ++i) {
const uint32_t c = ctable[*src++];
rgb[0] = SkGetPackedR32(c);
rgb[1] = SkGetPackedG32(c);
rgb[2] = SkGetPackedB32(c);
rgb += 3;
}
}
static ScanlineImporter ChooseImporter(SkColorType ct, bool hasAlpha, int* bpp) {
switch (ct) {
case kN32_SkColorType:
if (hasAlpha) {
*bpp = 4;
return ARGB_8888_To_RGBA;
} else {
*bpp = 3;
return ARGB_8888_To_RGB;
}
case kARGB_4444_SkColorType:
if (hasAlpha) {
*bpp = 4;
return ARGB_4444_To_RGBA;
} else {
*bpp = 3;
return ARGB_4444_To_RGB;
}
case kRGB_565_SkColorType:
*bpp = 3;
return RGB_565_To_RGB;
case kIndex_8_SkColorType:
*bpp = 3;
return Index8_To_RGB;
default:
return NULL;
}
}
static int stream_writer(const uint8_t* data, size_t data_size,
const WebPPicture* const picture) {
SkWStream* const stream = (SkWStream*)picture->custom_ptr;
return stream->write(data, data_size) ? 1 : 0;
}
class SkWEBPImageEncoder : public SkImageEncoder {
protected:
bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
private:
typedef SkImageEncoder INHERITED;
};
bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm,
int quality) {
const bool hasAlpha = !bm.isOpaque();
int bpp = -1;
const ScanlineImporter scanline_import = ChooseImporter(bm.colorType(), hasAlpha, &bpp);
if (NULL == scanline_import) {
return false;
}
if (-1 == bpp) {
return false;
}
SkAutoLockPixels alp(bm);
if (NULL == bm.getPixels()) {
return false;
}
WebPConfig webp_config;
if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
return false;
}
WebPPicture pic;
WebPPictureInit(&pic);
pic.width = bm.width();
pic.height = bm.height();
pic.writer = stream_writer;
pic.custom_ptr = (void*)stream;
const SkPMColor* colors = bm.getColorTable() ? bm.getColorTable()->readColors() : NULL;
const uint8_t* src = (uint8_t*)bm.getPixels();
const int rgbStride = pic.width * bpp;
// Import (for each scanline) the bit-map image (in appropriate color-space)
// to RGB color space.
uint8_t* rgb = new uint8_t[rgbStride * pic.height];
for (int y = 0; y < pic.height; ++y) {
scanline_import(src + y * bm.rowBytes(), rgb + y * rgbStride,
pic.width, colors);
}
bool ok;
if (bpp == 3) {
ok = SkToBool(WebPPictureImportRGB(&pic, rgb, rgbStride));
} else {
ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride));
}
delete[] rgb;
ok = ok && WebPEncode(&webp_config, &pic);
WebPPictureFree(&pic);
return ok;
}
///////////////////////////////////////////////////////////////////////////////
DEFINE_DECODER_CREATOR(WEBPImageDecoder);
DEFINE_ENCODER_CREATOR(WEBPImageEncoder);
///////////////////////////////////////////////////////////////////////////////
static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) {
int width, height, hasAlpha;
if (!webp_parse_header(stream, &width, &height, &hasAlpha)) {
return NULL;
}
// Magic matches, call decoder
return SkNEW(SkWEBPImageDecoder);
}
static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) {
int width, height, hasAlpha;
if (webp_parse_header(stream, &width, &height, &hasAlpha)) {
return SkImageDecoder::kWEBP_Format;
}
return SkImageDecoder::kUnknown_Format;
}
static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) {
return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL;
}
static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory);
static SkImageDecoder_FormatReg gFormatReg(get_format_webp);
static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory);
|