Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkWebpCodec.h" | 8 #include "SkWebpCodec.h" |
| 9 #include "SkImageGenerator.h" | 9 #include "SkImageGenerator.h" |
| 10 #include "SkTemplates.h" | 10 #include "SkTemplates.h" |
| 11 | 11 |
| 12 // A WebP decoder on top of (subset of) libwebp | 12 // A WebP decoder on top of (subset of) libwebp |
| 13 // For more information on WebP image format, and libwebp library, see: | 13 // For more information on WebP image format, and libwebp library, see: |
| 14 // https://code.google.com/speed/webp/ | 14 // https://code.google.com/speed/webp/ |
| 15 // http://www.webmproject.org/code/#libwebp-webp-image-library | 15 // http://www.webmproject.org/code/#libwebp-webp-image-library |
| 16 // https://chromium.googlesource.com/webm/libwebp | 16 // https://chromium.googlesource.com/webm/libwebp |
| 17 | 17 |
| 18 // If moving libwebp out of skia source tree, path for webp headers must be | 18 // If moving libwebp out of skia source tree, path for webp headers must be |
| 19 // updated accordingly. Here, we enforce using local copy in webp sub-directory. | 19 // updated accordingly. Here, we enforce using local copy in webp sub-directory. |
| 20 #include "webp/decode.h" | 20 #include "webp/decode.h" |
| 21 #include "webp/demux.h" | |
| 21 #include "webp/encode.h" | 22 #include "webp/encode.h" |
| 22 | 23 |
| 23 bool SkWebpCodec::IsWebp(SkStream* stream) { | 24 bool SkWebpCodec::IsWebp(SkStream* stream) { |
| 24 // WEBP starts with the following: | 25 // WEBP starts with the following: |
| 25 // RIFFXXXXWEBPVP | 26 // RIFFXXXXWEBPVP |
| 26 // Where XXXX is unspecified. | 27 // Where XXXX is unspecified. |
| 27 const char LENGTH = 14; | 28 const char LENGTH = 14; |
| 28 char bytes[LENGTH]; | 29 char bytes[LENGTH]; |
| 29 if (stream->read(&bytes, LENGTH) != LENGTH) { | 30 if (stream->read(&bytes, LENGTH) != LENGTH) { |
| 30 return false; | 31 return false; |
| 31 } | 32 } |
| 32 return !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); | 33 return !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); |
| 33 } | 34 } |
| 34 | 35 |
| 35 static const size_t WEBP_VP8_HEADER_SIZE = 30; | 36 static const size_t WEBP_VP8_HEADER_SIZE = 30; |
| 36 | 37 |
| 37 // Parse headers of RIFF container, and check for valid Webp (VP8) content. | 38 // Parse headers of RIFF container, and check for valid Webp (VP8) content. |
| 38 // NOTE: This calls peek instead of read, since onGetPixels will need these | 39 // NOTE: This calls peek instead of read, since onGetPixels will need these |
| 39 // bytes again. | 40 // bytes again. |
| 40 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { | 41 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { |
| 41 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; | 42 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
| 42 if (!stream->peek(buffer, WEBP_VP8_HEADER_SIZE)) { | 43 if (!stream->peek(buffer, WEBP_VP8_HEADER_SIZE)) { |
| 43 return false; | 44 return false; |
| 44 } | 45 } |
| 45 | 46 |
| 46 WebPBitstreamFeatures features; | 47 WebPData demuxData = { buffer, WEBP_VP8_HEADER_SIZE }; |
| 47 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur es); | 48 WebPDemuxState demuxState; |
| 48 if (VP8_STATUS_OK != status) { | 49 WebPDemuxer* demuxer = WebPDemuxPartial(&demuxData, &demuxState); |
| 49 return false; // Invalid WebP file. | 50 if (!demuxer) { |
| 51 return false; | |
| 50 } | 52 } |
| 51 | 53 |
| 54 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> autoDeleter(demuxer); | |
| 55 switch (demuxState) { | |
| 56 case WEBP_DEMUX_PARSE_ERROR: | |
| 57 case WEBP_DEMUX_PARSING_HEADER: | |
| 58 return false; | |
| 59 default: | |
| 60 // Header parsed. | |
| 61 break; | |
| 62 } | |
| 63 | |
| 64 uint32_t width = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); | |
| 65 uint32_t height = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); | |
| 66 uint32_t formatFlags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); | |
| 67 bool hasAlpha = SkToBool(formatFlags & ALPHA_FLAG) ? 1 : 0; | |
|
msarett
2015/06/19 12:07:46
Can this be simpler?
bool hasAlpha = SkToBool(form
scroggo
2015/06/19 18:59:58
Lol yes. This was copied from SkImageDecoder_libwe
| |
| 68 | |
| 52 // sanity check for image size that's about to be decoded. | 69 // sanity check for image size that's about to be decoded. |
| 53 { | 70 { |
| 54 const int64_t size = sk_64_mul(features.width, features.height); | 71 const int64_t size = sk_64_mul(width, height); |
| 55 if (!sk_64_isS32(size)) { | 72 if (!sk_64_isS32(size)) { |
| 56 return false; | 73 return false; |
| 57 } | 74 } |
| 58 // now check that if we are 4-bytes per pixel, we also don't overflow | 75 // now check that if we are 4-bytes per pixel, we also don't overflow |
| 59 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { | 76 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
| 60 return false; | 77 return false; |
| 61 } | 78 } |
| 62 } | 79 } |
| 63 | 80 |
| 64 if (info) { | 81 if (info) { |
| 65 // FIXME: Is N32 the right type? | 82 // FIXME: Is N32 the right type? |
| 66 // Is unpremul the right type? Clients of SkImageGenerator may assume it 's the | 83 // Is unpremul the right type? Clients of SkImageGenerator may assume it 's the |
| 67 // best type, when Skia currently cannot draw unpremul (and raster is fa ster | 84 // best type, when Skia currently cannot draw unpremul (and raster is fa ster |
| 68 // with premul). | 85 // with premul). |
| 69 *info = SkImageInfo::Make(features.width, features.height, kN32_SkColorT ype, | 86 *info = SkImageInfo::Make(width, height, kN32_SkColorType, |
| 70 SkToBool(features.has_alpha) ? kUnpremul_SkAlp haType | 87 hasAlpha ? kUnpremul_SkAlphaType |
| 71 : kOpaque_SkAlphaT ype); | 88 : kOpaque_SkAlphaType); |
| 72 } | 89 } |
| 73 return true; | 90 return true; |
| 74 } | 91 } |
| 75 | 92 |
| 76 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { | 93 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
| 77 SkAutoTDelete<SkStream> streamDeleter(stream); | 94 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 78 SkImageInfo info; | 95 SkImageInfo info; |
| 79 if (webp_parse_header(stream, &info)) { | 96 if (webp_parse_header(stream, &info)) { |
| 80 return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); | 97 return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); |
| 81 } | 98 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 // Break out of the switch statement. Continue the loop. | 208 // Break out of the switch statement. Continue the loop. |
| 192 break; | 209 break; |
| 193 default: | 210 default: |
| 194 return kInvalidInput; | 211 return kInvalidInput; |
| 195 } | 212 } |
| 196 } | 213 } |
| 197 } | 214 } |
| 198 | 215 |
| 199 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | 216 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
| 200 : INHERITED(info, stream) {} | 217 : INHERITED(info, stream) {} |
| OLD | NEW |