OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkWebpCodec.h" |
| 9 #include "SkImageGenerator.h" |
| 10 #include "SkTemplates.h" |
| 11 |
| 12 // A WebP decoder on top of (subset of) libwebp |
| 13 // For more information on WebP image format, and libwebp library, see: |
| 14 // https://code.google.com/speed/webp/ |
| 15 // http://www.webmproject.org/code/#libwebp-webp-image-library |
| 16 // https://chromium.googlesource.com/webm/libwebp |
| 17 |
| 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. |
| 20 #include "webp/decode.h" |
| 21 #include "webp/encode.h" |
| 22 |
| 23 bool SkWebpCodec::IsWebp(SkStream* stream) { |
| 24 // WEBP starts with the following: |
| 25 // RIFFXXXXWEBPVP |
| 26 // Where XXXX is unspecified. |
| 27 const char LENGTH = 14; |
| 28 char bytes[LENGTH]; |
| 29 if (stream->read(&bytes, LENGTH) != LENGTH) { |
| 30 return false; |
| 31 } |
| 32 return !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); |
| 33 } |
| 34 |
| 35 static const size_t WEBP_VP8_HEADER_SIZE = 30; |
| 36 |
| 37 // 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 // bytes again. |
| 40 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { |
| 41 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
| 42 if (!stream->peek(buffer, WEBP_VP8_HEADER_SIZE)) { |
| 43 return false; |
| 44 } |
| 45 |
| 46 WebPBitstreamFeatures features; |
| 47 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur
es); |
| 48 if (VP8_STATUS_OK != status) { |
| 49 return false; // Invalid WebP file. |
| 50 } |
| 51 |
| 52 // sanity check for image size that's about to be decoded. |
| 53 { |
| 54 const int64_t size = sk_64_mul(features.width, features.height); |
| 55 if (!sk_64_isS32(size)) { |
| 56 return false; |
| 57 } |
| 58 // now check that if we are 4-bytes per pixel, we also don't overflow |
| 59 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
| 60 return false; |
| 61 } |
| 62 } |
| 63 |
| 64 if (info) { |
| 65 // FIXME: Is N32 the right type? |
| 66 // 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 |
| 68 // with premul). |
| 69 *info = SkImageInfo::Make(features.width, features.height, kN32_SkColorT
ype, |
| 70 SkToBool(features.has_alpha) ? kUnpremul_SkAlp
haType |
| 71 : kOpaque_SkAlphaT
ype); |
| 72 } |
| 73 return true; |
| 74 } |
| 75 |
| 76 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
| 77 SkAutoTDelete<SkStream> streamDeleter(stream); |
| 78 SkImageInfo info; |
| 79 if (webp_parse_header(stream, &info)) { |
| 80 return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); |
| 81 } |
| 82 return NULL; |
| 83 } |
| 84 |
| 85 static bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src)
{ |
| 86 switch (dst.colorType()) { |
| 87 // Both byte orders are supported. |
| 88 case kBGRA_8888_SkColorType: |
| 89 case kRGBA_8888_SkColorType: |
| 90 break; |
| 91 default: |
| 92 return false; |
| 93 } |
| 94 if (dst.profileType() != src.profileType()) { |
| 95 return false; |
| 96 } |
| 97 if (dst.alphaType() == src.alphaType()) { |
| 98 return true; |
| 99 } |
| 100 return kPremul_SkAlphaType == dst.alphaType() && |
| 101 kUnpremul_SkAlphaType == src.alphaType(); |
| 102 } |
| 103 |
| 104 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { |
| 105 SkISize dim = this->getInfo().dimensions(); |
| 106 dim.fWidth = SkScalarRoundToInt(desiredScale * dim.fWidth); |
| 107 dim.fHeight = SkScalarRoundToInt(desiredScale * dim.fHeight); |
| 108 return dim; |
| 109 } |
| 110 |
| 111 static WEBP_CSP_MODE webp_decode_mode(SkColorType ct, bool premultiply) { |
| 112 switch (ct) { |
| 113 case kBGRA_8888_SkColorType: |
| 114 return premultiply ? MODE_bgrA : MODE_BGRA; |
| 115 case kRGBA_8888_SkColorType: |
| 116 return premultiply ? MODE_rgbA : MODE_RGBA; |
| 117 default: |
| 118 return MODE_LAST; |
| 119 } |
| 120 } |
| 121 |
| 122 // The WebP decoding API allows us to incrementally pass chunks of bytes as we r
eceive them to the |
| 123 // decoder with WebPIAppend. In order to do so, we need to read chunks from the
SkStream. This size |
| 124 // is arbitrary. |
| 125 static const size_t BUFFER_SIZE = 4096; |
| 126 |
| 127 SkImageGenerator::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, vo
id* dst, |
| 128 size_t rowBytes, const Options
&, SkPMColor*, |
| 129 int*) { |
| 130 switch (this->rewindIfNeeded()) { |
| 131 case kCouldNotRewind_RewindState: |
| 132 return kCouldNotRewind; |
| 133 case kRewound_RewindState: |
| 134 // Rewound to the beginning. Since creation only does a peek, the st
ream is at the |
| 135 // correct position. |
| 136 break; |
| 137 case kNoRewindNecessary_RewindState: |
| 138 // Already at the right spot for decoding. |
| 139 break; |
| 140 } |
| 141 |
| 142 if (!conversion_possible(dstInfo, this->getInfo())) { |
| 143 return kInvalidConversion; |
| 144 } |
| 145 |
| 146 WebPDecoderConfig config; |
| 147 if (0 == WebPInitDecoderConfig(&config)) { |
| 148 // ABI mismatch. |
| 149 // FIXME: New enum for this? |
| 150 return kInvalidInput; |
| 151 } |
| 152 |
| 153 // Free any memory associated with the buffer. Must be called last, so we de
clare it first. |
| 154 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output)
); |
| 155 |
| 156 SkISize dimensions = dstInfo.dimensions(); |
| 157 if (this->getInfo().dimensions() != dimensions) { |
| 158 // Caller is requesting scaling. |
| 159 config.options.use_scaling = 1; |
| 160 config.options.scaled_width = dimensions.width(); |
| 161 config.options.scaled_height = dimensions.height(); |
| 162 } |
| 163 |
| 164 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), |
| 165 dstInfo.alphaType() == kPremul_SkAlphaType); |
| 166 config.output.u.RGBA.rgba = (uint8_t*) dst; |
| 167 config.output.u.RGBA.stride = (int) rowBytes; |
| 168 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); |
| 169 config.output.is_external_memory = 1; |
| 170 |
| 171 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(NULL, 0, &confi
g)); |
| 172 if (!idec) { |
| 173 return kInvalidInput; |
| 174 } |
| 175 |
| 176 SkAutoMalloc storage(BUFFER_SIZE); |
| 177 uint8_t* buffer = static_cast<uint8_t*>(storage.get()); |
| 178 while (true) { |
| 179 const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE); |
| 180 if (0 == bytesRead) { |
| 181 // FIXME: Maybe this is an incomplete image? How to decide? Based |
| 182 // on the number of rows decoded? We can know the number of rows |
| 183 // decoded using WebPIDecGetRGB. |
| 184 return kInvalidInput; |
| 185 } |
| 186 |
| 187 switch (WebPIAppend(idec, buffer, bytesRead)) { |
| 188 case VP8_STATUS_OK: |
| 189 return kSuccess; |
| 190 case VP8_STATUS_SUSPENDED: |
| 191 // Break out of the switch statement. Continue the loop. |
| 192 break; |
| 193 default: |
| 194 return kInvalidInput; |
| 195 } |
| 196 } |
| 197 } |
| 198 |
| 199 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
| 200 : INHERITED(info, stream) {} |
OLD | NEW |