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, int* width, int* height, SkEncod
edInfo* info) { | 34 // Returns an SkWebpCodec on success; |
| 35 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
| 36 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 37 |
35 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; | 38 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
36 SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded()); | 39 SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded()); |
37 | 40 |
38 const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE); | 41 const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE); |
39 if (bytesPeeked != WEBP_VP8_HEADER_SIZE) { | 42 if (bytesPeeked != WEBP_VP8_HEADER_SIZE) { |
40 // Use read + rewind as a backup | 43 // Use read + rewind as a backup |
41 if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE | 44 if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE |
42 || !stream->rewind()) | 45 || !stream->rewind()) |
43 return false; | 46 return nullptr; |
44 } | 47 } |
45 | 48 |
46 WebPBitstreamFeatures features; | 49 WebPBitstreamFeatures features; |
47 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur
es); | 50 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur
es); |
48 if (VP8_STATUS_OK != status) { | 51 if (VP8_STATUS_OK != status) { |
49 return false; // Invalid WebP file. | 52 return nullptr; // Invalid WebP file. |
50 } | 53 } |
51 | 54 |
52 // sanity check for image size that's about to be decoded. | 55 // sanity check for image size that's about to be decoded. |
53 { | 56 { |
54 const int64_t size = sk_64_mul(features.width, features.height); | 57 const int64_t size = sk_64_mul(features.width, features.height); |
55 if (!sk_64_isS32(size)) { | 58 if (!sk_64_isS32(size)) { |
56 return false; | 59 return nullptr; |
57 } | 60 } |
58 // now check that if we are 4-bytes per pixel, we also don't overflow | 61 // now check that if we are 4-bytes per pixel, we also don't overflow |
59 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { | 62 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
60 return false; | 63 return nullptr; |
61 } | 64 } |
62 } | 65 } |
63 | 66 |
64 if (info) { | 67 SkEncodedInfo::Color color; |
65 SkEncodedInfo::Color color; | 68 SkEncodedInfo::Alpha alpha; |
66 SkEncodedInfo::Alpha alpha; | 69 switch (features.format) { |
67 switch (features.format) { | 70 case 0: |
68 case 0: | 71 // This indicates a "mixed" format. We would see this for |
69 // This indicates a "mixed" format. We would see this for | 72 // animated webps or for webps encoded in multiple fragments. |
70 // animated webps or for webps encoded in multiple fragments. | 73 // I believe that this is a rare case. |
71 // I believe that this is a rare case. | 74 // We could also guess kYUV here, but I think it makes more |
72 // We could also guess kYUV here, but I think it makes more | 75 // sense to guess kBGRA which is likely closer to the final |
73 // sense to guess kBGRA which is likely closer to the final | 76 // output. Otherwise, we might end up converting |
74 // output. Otherwise, we might end up converting | 77 // BGRA->YUVA->BGRA. |
75 // BGRA->YUVA->BGRA. | 78 color = SkEncodedInfo::kBGRA_Color; |
76 color = SkEncodedInfo::kBGRA_Color; | 79 alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 80 break; |
| 81 case 1: |
| 82 // This is the lossy format (YUV). |
| 83 if (SkToBool(features.has_alpha)) { |
| 84 color = SkEncodedInfo::kYUVA_Color; |
77 alpha = SkEncodedInfo::kUnpremul_Alpha; | 85 alpha = SkEncodedInfo::kUnpremul_Alpha; |
78 break; | 86 } else { |
79 case 1: | 87 color = SkEncodedInfo::kYUV_Color; |
80 // This is the lossy format (YUV). | 88 alpha = SkEncodedInfo::kOpaque_Alpha; |
81 if (SkToBool(features.has_alpha)) { | 89 } |
82 color = SkEncodedInfo::kYUVA_Color; | 90 break; |
83 alpha = SkEncodedInfo::kUnpremul_Alpha; | 91 case 2: |
84 } else { | 92 // This is the lossless format (BGRA). |
85 color = SkEncodedInfo::kYUV_Color; | 93 // FIXME: Should we check the has_alpha flag here? It looks |
86 alpha = SkEncodedInfo::kOpaque_Alpha; | 94 // like the image is encoded with an alpha channel |
87 } | 95 // regardless of whether or not the alpha flag is set. |
88 break; | 96 color = SkEncodedInfo::kBGRA_Color; |
89 case 2: | 97 alpha = SkEncodedInfo::kUnpremul_Alpha; |
90 // This is the lossless format (BGRA). | 98 break; |
91 // FIXME: Should we check the has_alpha flag here? It looks | 99 default: |
92 // like the image is encoded with an alpha channel | 100 return nullptr; |
93 // regardless of whether or not the alpha flag is set. | 101 } |
94 color = SkEncodedInfo::kBGRA_Color; | |
95 alpha = SkEncodedInfo::kUnpremul_Alpha; | |
96 break; | |
97 default: | |
98 return false; | |
99 } | |
100 | 102 |
101 *width = features.width; | 103 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); |
102 *height = features.height; | 104 return new SkWebpCodec(features.width, features.height, info, streamDeleter.
release()); |
103 *info = SkEncodedInfo::Make(color, alpha, 8); | |
104 } | |
105 return true; | |
106 } | |
107 | |
108 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { | |
109 SkAutoTDelete<SkStream> streamDeleter(stream); | |
110 int width, height; | |
111 SkEncodedInfo info; | |
112 if (webp_parse_header(stream, &width, &height, &info)) { | |
113 return new SkWebpCodec(width, height, info, streamDeleter.release()); | |
114 } | |
115 return nullptr; | |
116 } | 105 } |
117 | 106 |
118 // This version is slightly different from SkCodecPriv's version of conversion_p
ossible. It | 107 // This version is slightly different from SkCodecPriv's version of conversion_p
ossible. It |
119 // supports both byte orders for 8888. | 108 // supports both byte orders for 8888. |
120 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo&
src) { | 109 static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo&
src) { |
121 // FIXME: skbug.com/4895 | 110 // FIXME: skbug.com/4895 |
122 // Currently, we ignore the SkColorProfileType on the SkImageInfo. We | 111 // Currently, we ignore the SkColorProfileType on the SkImageInfo. We |
123 // will treat the encoded data as linear regardless of what the client | 112 // will treat the encoded data as linear regardless of what the client |
124 // requests. | 113 // requests. |
125 | 114 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 default: | 271 default: |
283 return kInvalidInput; | 272 return kInvalidInput; |
284 } | 273 } |
285 } | 274 } |
286 } | 275 } |
287 | 276 |
288 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, SkStr
eam* stream) | 277 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, SkStr
eam* stream) |
289 // The spec says an unmarked image is sRGB, so we return that space here. | 278 // The spec says an unmarked image is sRGB, so we return that space here. |
290 // TODO: Add support for parsing ICC profiles from webps. | 279 // TODO: Add support for parsing ICC profiles from webps. |
291 : INHERITED(width, height, info, stream, SkColorSpace::NewNamed(SkColorSpace
::kSRGB_Named)) {} | 280 : INHERITED(width, height, info, stream, SkColorSpace::NewNamed(SkColorSpace
::kSRGB_Named)) {} |
OLD | NEW |