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 "SkCodecPriv.h" | 8 #include "SkCodecPriv.h" |
| 9 #include "SkWebpCodec.h" | 9 #include "SkWebpCodec.h" |
| 10 #include "SkTemplates.h" | 10 #include "SkTemplates.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 // WEBP starts with the following: | 24 // WEBP starts with the following: |
| 25 // RIFFXXXXWEBPVP | 25 // RIFFXXXXWEBPVP |
| 26 // Where XXXX is unspecified. | 26 // Where XXXX is unspecified. |
| 27 const char* bytes = static_cast<const char*>(buf); | 27 const char* bytes = static_cast<const char*>(buf); |
| 28 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "W EBPVP", 6); | 28 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "W EBPVP", 6); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Parse headers of RIFF container, and check for valid Webp (VP8) content. | 31 // Parse headers of RIFF container, and check for valid Webp (VP8) content. |
| 32 // NOTE: This calls peek instead of read, since onGetPixels will need these | 32 // NOTE: This calls peek instead of read, since onGetPixels will need these |
| 33 // bytes again. | 33 // bytes again. |
| 34 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { | 34 static bool webp_parse_header(SkStream* stream, SkEncodedInfo* info) { |
| 35 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; | 35 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
| 36 SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded()); | 36 SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded()); |
| 37 | 37 |
| 38 const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE); | 38 const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE); |
| 39 if (bytesPeeked != WEBP_VP8_HEADER_SIZE) { | 39 if (bytesPeeked != WEBP_VP8_HEADER_SIZE) { |
| 40 // Use read + rewind as a backup | 40 // Use read + rewind as a backup |
| 41 if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE | 41 if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE |
| 42 || !stream->rewind()) | 42 || !stream->rewind()) |
| 43 return false; | 43 return false; |
| 44 } | 44 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 55 if (!sk_64_isS32(size)) { | 55 if (!sk_64_isS32(size)) { |
| 56 return false; | 56 return false; |
| 57 } | 57 } |
| 58 // now check that if we are 4-bytes per pixel, we also don't overflow | 58 // now check that if we are 4-bytes per pixel, we also don't overflow |
| 59 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { | 59 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
| 60 return false; | 60 return false; |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 if (info) { | 64 if (info) { |
| 65 // FIXME: Is N32 the right type? | 65 SkEncodedInfo::Color color; |
| 66 // Is unpremul the right type? Clients of SkCodec may assume it's the | 66 SkEncodedInfo::Alpha alpha; |
| 67 // best type, when Skia currently cannot draw unpremul (and raster is fa ster | 67 switch (features.format) { |
| 68 // with premul). | 68 case 0: |
| 69 *info = SkImageInfo::Make(features.width, features.height, kN32_SkColorT ype, | 69 // This indicates a "mixed" format. We would see this for |
| 70 SkToBool(features.has_alpha) ? kUnpremul_SkAlp haType | 70 // animated webps or for webps encoded in multiple fragments. |
| 71 : kOpaque_SkAlphaT ype); | 71 // I believe that this is a rare case. |
| 72 // We could also guess kYUV here, but I think it makes more | |
| 73 // sense to guess kBGRA which is likely closer to the final | |
| 74 // output. Otherwise, we might end up converting | |
| 75 // BGRA->YUVA->BGRA. | |
| 76 color = SkEncodedInfo::kBGRA_Color; | |
| 77 alpha = SkEncodedInfo::kUnpremul_Alpha; | |
| 78 break; | |
| 79 case 1: | |
| 80 // This is the lossy format (YUV). | |
| 81 color = SkToBool(features.has_alpha) ? SkEncodedInfo::kYUVA_Colo r : | |
|
scroggo
2016/03/24 20:51:04
nit: I would find this a little clearer as
if (ha
msarett
2016/03/24 22:42:07
Done.
| |
| 82 SkEncodedInfo::kYUV_Color; | |
| 83 alpha = SkToBool(features.has_alpha) ? SkEncodedInfo::kUnpremul_ Alpha : | |
| 84 SkEncodedInfo::kOpaque_Alpha; | |
| 85 break; | |
| 86 case 2: | |
| 87 // This is the lossless format (BGRA). | |
| 88 // FIXME: Should we check the has_alpha flag here? It looks | |
|
msarett
2016/03/24 16:21:40
Forgot to mention that I emailed webp-discuss abou
| |
| 89 // like the image is encoded with an alpha channel | |
| 90 // regardless of whether or not the alpha flag is set. | |
| 91 color = SkEncodedInfo::kBGRA_Color; | |
| 92 alpha = SkEncodedInfo::kUnpremul_Alpha; | |
| 93 break; | |
| 94 default: | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 *info = SkEncodedInfo::Make(features.width, features.height, color, alph a, 8); | |
| 72 } | 99 } |
| 73 return true; | 100 return true; |
| 74 } | 101 } |
| 75 | 102 |
| 76 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { | 103 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
| 77 SkAutoTDelete<SkStream> streamDeleter(stream); | 104 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 78 SkImageInfo info; | 105 SkEncodedInfo info; |
| 79 if (webp_parse_header(stream, &info)) { | 106 if (webp_parse_header(stream, &info)) { |
| 80 return new SkWebpCodec(info, streamDeleter.release()); | 107 return new SkWebpCodec(info, streamDeleter.release()); |
| 81 } | 108 } |
| 82 return nullptr; | 109 return nullptr; |
| 83 } | 110 } |
| 84 | 111 |
| 85 // This version is slightly different from SkCodecPriv's version of conversion_p ossible. It | 112 // This version is slightly different from SkCodecPriv's version of conversion_p ossible. It |
| 86 // supports both byte orders for 8888. | 113 // supports both byte orders for 8888. |
| 87 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { | 114 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { |
| 88 if (dst.profileType() != src.profileType()) { | 115 if (dst.profileType() != src.profileType()) { |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 return kSuccess; | 271 return kSuccess; |
| 245 case VP8_STATUS_SUSPENDED: | 272 case VP8_STATUS_SUSPENDED: |
| 246 // Break out of the switch statement. Continue the loop. | 273 // Break out of the switch statement. Continue the loop. |
| 247 break; | 274 break; |
| 248 default: | 275 default: |
| 249 return kInvalidInput; | 276 return kInvalidInput; |
| 250 } | 277 } |
| 251 } | 278 } |
| 252 } | 279 } |
| 253 | 280 |
| 254 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | 281 SkWebpCodec::SkWebpCodec(const SkEncodedInfo& info, SkStream* stream) |
| 255 : INHERITED(info, stream) {} | 282 : INHERITED(info, stream) {} |
| OLD | NEW |