| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 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 |  | 
| scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 8 | #include "SkCodecPriv.h" | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 9 | #include "SkColorSpaceXform.h" | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 10 | #include "SkWebpCodec.h" | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 11 | #include "SkStreamPriv.h" | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 12 | #include "SkTemplates.h" | 
 | 13 |  | 
 | 14 | // A WebP decoder on top of (subset of) libwebp | 
 | 15 | // For more information on WebP image format, and libwebp library, see: | 
 | 16 | //   https://code.google.com/speed/webp/ | 
 | 17 | //   http://www.webmproject.org/code/#libwebp-webp-image-library | 
 | 18 | //   https://chromium.googlesource.com/webm/libwebp | 
 | 19 |  | 
 | 20 | // If moving libwebp out of skia source tree, path for webp headers must be | 
 | 21 | // updated accordingly. Here, we enforce using local copy in webp sub-directory. | 
 | 22 | #include "webp/decode.h" | 
| msarett | 9d15dab | 2016-08-24 07:36:06 -0700 | [diff] [blame] | 23 | #include "webp/demux.h" | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 24 | #include "webp/encode.h" | 
 | 25 |  | 
| scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 26 | bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) { | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 27 |     // WEBP starts with the following: | 
 | 28 |     // RIFFXXXXWEBPVP | 
 | 29 |     // Where XXXX is unspecified. | 
| scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 30 |     const char* bytes = static_cast<const char*>(buf); | 
 | 31 |     return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 32 | } | 
 | 33 |  | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 34 | // Parse headers of RIFF container, and check for valid Webp (VP8) content. | 
 | 35 | // NOTE: This calls peek instead of read, since onGetPixels will need these | 
 | 36 | // bytes again. | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 37 | // Returns an SkWebpCodec on success; | 
 | 38 | SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { | 
| Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 39 |     std::unique_ptr<SkStream> streamDeleter(stream); | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 40 |  | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 41 |     // Webp demux needs a contiguous data buffer. | 
 | 42 |     sk_sp<SkData> data = nullptr; | 
 | 43 |     if (stream->getMemoryBase()) { | 
 | 44 |         // It is safe to make without copy because we'll hold onto the stream. | 
 | 45 |         data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength()); | 
 | 46 |     } else { | 
 | 47 |         data = SkCopyStreamToData(stream); | 
| scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 48 |  | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 49 |         // If we are forced to copy the stream to a data, we can go ahead and delete the stream. | 
 | 50 |         streamDeleter.reset(nullptr); | 
 | 51 |     } | 
 | 52 |  | 
 | 53 |     // It's a little strange that the |demux| will outlive |webpData|, though it needs the | 
 | 54 |     // pointer in |webpData| to remain valid.  This works because the pointer remains valid | 
 | 55 |     // until the SkData is freed. | 
 | 56 |     WebPData webpData = { data->bytes(), data->size() }; | 
 | 57 |     SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, nullptr)); | 
 | 58 |     if (nullptr == demux) { | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 59 |         return nullptr; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 60 |     } | 
 | 61 |  | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 62 |     WebPChunkIterator chunkIterator; | 
 | 63 |     SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator); | 
 | 64 |     sk_sp<SkColorSpace> colorSpace = nullptr; | 
| raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 65 |     bool unsupportedICC = false; | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 66 |     if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) { | 
| Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 67 |         colorSpace = SkColorSpace::MakeICC(chunkIterator.chunk.bytes, chunkIterator.chunk.size); | 
| raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 68 |         if (!colorSpace) { | 
 | 69 |             unsupportedICC = true; | 
 | 70 |         } | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 71 |     } | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 72 |     if (!colorSpace) { | 
| Brian Osman | 526972e | 2016-10-24 09:24:02 -0400 | [diff] [blame] | 73 |         colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 74 |     } | 
 | 75 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 76 |     // Since we do not yet support animation, we get the |width|, |height|, |color|, and |alpha| | 
 | 77 |     // from the first frame.  It's the only frame we will decode. | 
 | 78 |     // | 
 | 79 |     // TODO: | 
 | 80 |     // When we support animation, we'll want to report the canvas width and canvas height instead. | 
 | 81 |     // We can get these from the |demux| directly. | 
 | 82 |     // What |color| and |alpha| will we want to report though?  WebP allows different frames | 
 | 83 |     // to be encoded in different ways, making the encoded format difficult to describe. | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 84 |     WebPIterator frame; | 
 | 85 |     SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); | 
 | 86 |     if (!WebPDemuxGetFrame(demux, 1, &frame)) { | 
 | 87 |         return nullptr; | 
 | 88 |     } | 
 | 89 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 90 |     // Sanity check for image size that's about to be decoded. | 
 | 91 |     { | 
 | 92 |         const int64_t size = sk_64_mul(frame.width, frame.height); | 
 | 93 |         if (!sk_64_isS32(size)) { | 
 | 94 |             return nullptr; | 
 | 95 |         } | 
 | 96 |         // now check that if we are 4-bytes per pixel, we also don't overflow | 
 | 97 |         if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { | 
 | 98 |             return nullptr; | 
 | 99 |         } | 
 | 100 |     } | 
 | 101 |  | 
 | 102 |     // TODO: | 
 | 103 |     // The only reason we actually need to call WebPGetFeatures() is to get the |features.format|. | 
 | 104 |     // This call actually re-reads the frame header.  Should we suggest that libwebp expose | 
 | 105 |     // the format on the |frame|? | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 106 |     WebPBitstreamFeatures features; | 
 | 107 |     VP8StatusCode status = WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features); | 
 | 108 |     if (VP8_STATUS_OK != status) { | 
 | 109 |         return nullptr; | 
 | 110 |     } | 
 | 111 |  | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 112 |     SkEncodedInfo::Color color; | 
 | 113 |     SkEncodedInfo::Alpha alpha; | 
 | 114 |     switch (features.format) { | 
 | 115 |         case 0: | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 116 |             // This indicates a "mixed" format.  We would see this for | 
 | 117 |             // animated webps or for webps encoded in multiple fragments. | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 118 |             // I believe that this is a rare case. | 
 | 119 |             // We could also guess kYUV here, but I think it makes more | 
 | 120 |             // sense to guess kBGRA which is likely closer to the final | 
 | 121 |             // output.  Otherwise, we might end up converting | 
 | 122 |             // BGRA->YUVA->BGRA. | 
 | 123 |             color = SkEncodedInfo::kBGRA_Color; | 
 | 124 |             alpha = SkEncodedInfo::kUnpremul_Alpha; | 
 | 125 |             break; | 
 | 126 |         case 1: | 
 | 127 |             // This is the lossy format (YUV). | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 128 |             if (SkToBool(features.has_alpha)) { | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 129 |                 color = SkEncodedInfo::kYUVA_Color; | 
| msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 130 |                 alpha = SkEncodedInfo::kUnpremul_Alpha; | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 131 |             } else { | 
 | 132 |                 color = SkEncodedInfo::kYUV_Color; | 
 | 133 |                 alpha = SkEncodedInfo::kOpaque_Alpha; | 
 | 134 |             } | 
 | 135 |             break; | 
 | 136 |         case 2: | 
 | 137 |             // This is the lossless format (BGRA). | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 138 |             color = SkEncodedInfo::kBGRA_Color; | 
 | 139 |             alpha = SkEncodedInfo::kUnpremul_Alpha; | 
 | 140 |             break; | 
 | 141 |         default: | 
 | 142 |             return nullptr; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 143 |     } | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 144 |  | 
| msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 145 |     SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 146 |     SkWebpCodec* codecOut = new SkWebpCodec(features.width, features.height, info, | 
 | 147 |                                             std::move(colorSpace), streamDeleter.release(), | 
 | 148 |                                             demux.release(), std::move(data)); | 
| raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 149 |     codecOut->setUnsupportedICC(unsupportedICC); | 
 | 150 |     return codecOut; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 151 | } | 
 | 152 |  | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 153 | SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { | 
 | 154 |     SkISize dim = this->getInfo().dimensions(); | 
| msarett | a0c414d | 2015-06-19 07:34:30 -0700 | [diff] [blame] | 155 |     // SkCodec treats zero dimensional images as errors, so the minimum size | 
 | 156 |     // that we will recommend is 1x1. | 
 | 157 |     dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); | 
 | 158 |     dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 159 |     return dim; | 
 | 160 | } | 
 | 161 |  | 
| scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 162 | bool SkWebpCodec::onDimensionsSupported(const SkISize& dim) { | 
 | 163 |     const SkImageInfo& info = this->getInfo(); | 
 | 164 |     return dim.width() >= 1 && dim.width() <= info.width() | 
 | 165 |             && dim.height() >= 1 && dim.height() <= info.height(); | 
 | 166 | } | 
 | 167 |  | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 168 | static WEBP_CSP_MODE webp_decode_mode(SkColorType ct, bool premultiply) { | 
 | 169 |     switch (ct) { | 
 | 170 |         case kBGRA_8888_SkColorType: | 
 | 171 |             return premultiply ? MODE_bgrA : MODE_BGRA; | 
 | 172 |         case kRGBA_8888_SkColorType: | 
 | 173 |             return premultiply ? MODE_rgbA : MODE_RGBA; | 
| scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame] | 174 |         case kRGB_565_SkColorType: | 
 | 175 |             return MODE_RGB_565; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 176 |         default: | 
 | 177 |             return MODE_LAST; | 
 | 178 |     } | 
 | 179 | } | 
 | 180 |  | 
| scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 181 | bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { | 
 | 182 |     if (!desiredSubset) { | 
 | 183 |         return false; | 
 | 184 |     } | 
 | 185 |  | 
| msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 186 |     SkIRect dimensions  = SkIRect::MakeSize(this->getInfo().dimensions()); | 
 | 187 |     if (!dimensions.contains(*desiredSubset)) { | 
| scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 188 |         return false; | 
 | 189 |     } | 
 | 190 |  | 
 | 191 |     // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we | 
 | 192 |     // decode this exact subset. | 
 | 193 |     // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. | 
 | 194 |     desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; | 
 | 195 |     desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; | 
 | 196 |     return true; | 
 | 197 | } | 
 | 198 |  | 
| scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 199 | SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, | 
| msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 200 |                                          const Options& options, SkPMColor*, int*, | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 201 |                                          int* rowsDecodedPtr) { | 
| msarett | 2ecc35f | 2016-09-08 11:55:16 -0700 | [diff] [blame] | 202 |     if (!conversion_possible(dstInfo, this->getInfo())) { | 
 | 203 |         return kInvalidConversion; | 
 | 204 |     } | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 205 |  | 
| Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 206 |     if (!this->initializeColorXform(dstInfo)) { | 
 | 207 |         return kInvalidConversion; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 208 |     } | 
 | 209 |  | 
 | 210 |     WebPDecoderConfig config; | 
 | 211 |     if (0 == WebPInitDecoderConfig(&config)) { | 
 | 212 |         // ABI mismatch. | 
 | 213 |         // FIXME: New enum for this? | 
 | 214 |         return kInvalidInput; | 
 | 215 |     } | 
 | 216 |  | 
 | 217 |     // Free any memory associated with the buffer. Must be called last, so we declare it first. | 
 | 218 |     SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output)); | 
 | 219 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 220 |     SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); | 
| Matt Sarett | 0f33970 | 2017-02-03 15:24:19 -0500 | [diff] [blame] | 221 |     if (options.fSubset) { | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 222 |         // Caller is requesting a subset. | 
 | 223 |         if (!bounds.contains(*options.fSubset)) { | 
 | 224 |             // The subset is out of bounds. | 
 | 225 |             return kInvalidParameters; | 
| Matt Sarett | 0f33970 | 2017-02-03 15:24:19 -0500 | [diff] [blame] | 226 |         } | 
 | 227 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 228 |         bounds = *options.fSubset; | 
| Matt Sarett | 0f33970 | 2017-02-03 15:24:19 -0500 | [diff] [blame] | 229 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 230 |         // This is tricky. libwebp snaps the top and left to even values. We could let libwebp | 
 | 231 |         // do the snap, and return a subset which is a different one than requested. The problem | 
 | 232 |         // with that approach is that the caller may try to stitch subsets together, and if we | 
 | 233 |         // returned different subsets than requested, there would be artifacts at the boundaries. | 
 | 234 |         // Instead, we report that we cannot support odd values for top and left.. | 
 | 235 |         if (!SkIsAlign2(bounds.fLeft) || !SkIsAlign2(bounds.fTop)) { | 
 | 236 |             return kInvalidParameters; | 
 | 237 |         } | 
| Matt Sarett | 0f33970 | 2017-02-03 15:24:19 -0500 | [diff] [blame] | 238 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 239 | #ifdef SK_DEBUG | 
 | 240 |         { | 
 | 241 |             // Make a copy, since getValidSubset can change its input. | 
 | 242 |             SkIRect subset(bounds); | 
 | 243 |             // That said, getValidSubset should *not* change its input, in this case; otherwise | 
 | 244 |             // getValidSubset does not match the actual subsets we can do. | 
 | 245 |             SkASSERT(this->getValidSubset(&subset) && subset == bounds); | 
 | 246 |         } | 
 | 247 | #endif | 
| Matt Sarett | 0f33970 | 2017-02-03 15:24:19 -0500 | [diff] [blame] | 248 |  | 
 | 249 |         config.options.use_cropping = 1; | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 250 |         config.options.crop_left = bounds.fLeft; | 
 | 251 |         config.options.crop_top = bounds.fTop; | 
 | 252 |         config.options.crop_width = bounds.width(); | 
 | 253 |         config.options.crop_height = bounds.height(); | 
| Matt Sarett | 0f33970 | 2017-02-03 15:24:19 -0500 | [diff] [blame] | 254 |     } | 
 | 255 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 256 |     SkISize dstDimensions = dstInfo.dimensions(); | 
 | 257 |     if (bounds.size() != dstDimensions) { | 
 | 258 |         // Caller is requesting scaling. | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 259 |         config.options.use_scaling = 1; | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 260 |         config.options.scaled_width = dstDimensions.width(); | 
 | 261 |         config.options.scaled_height = dstDimensions.height(); | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 262 |     } | 
 | 263 |  | 
| msarett | cf7b877 | 2016-09-22 12:37:04 -0700 | [diff] [blame] | 264 |     // Swizzling between RGBA and BGRA is zero cost in a color transform.  So when we have a | 
 | 265 |     // color transform, we should decode to whatever is easiest for libwebp, and then let the | 
 | 266 |     // color transform swizzle if necessary. | 
 | 267 |     // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost).  Lossless webp is | 
 | 268 |     // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA. | 
| Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 269 |     config.output.colorspace = this->colorXform() ? MODE_BGRA : | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 270 |             webp_decode_mode(dstInfo.colorType(), dstInfo.alphaType() == kPremul_SkAlphaType); | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 271 |     config.output.is_external_memory = 1; | 
 | 272 |  | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 273 |     // We will decode the entire image and then perform the color transform.  libwebp | 
 | 274 |     // does not provide a row-by-row API.  This is a shame particularly in the F16 case, | 
 | 275 |     // where we need to allocate an extra image-sized buffer. | 
 | 276 |     SkAutoTMalloc<uint32_t> pixels; | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 277 |     if (kRGBA_F16_SkColorType == dstInfo.colorType()) { | 
 | 278 |         pixels.reset(dstDimensions.width() * dstDimensions.height()); | 
 | 279 |         config.output.u.RGBA.rgba = (uint8_t*) pixels.get(); | 
 | 280 |         config.output.u.RGBA.stride = (int) dstDimensions.width() * sizeof(uint32_t); | 
 | 281 |         config.output.u.RGBA.size = config.output.u.RGBA.stride * dstDimensions.height(); | 
 | 282 |     } else { | 
 | 283 |         config.output.u.RGBA.rgba = (uint8_t*) dst; | 
 | 284 |         config.output.u.RGBA.stride = (int) rowBytes; | 
 | 285 |         config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | 
 | 286 |     } | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 287 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 288 |     WebPIterator frame; | 
 | 289 |     SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); | 
 | 290 |     // If this succeeded in NewFromStream(), it should succeed again here. | 
 | 291 |     SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame)); | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 292 |  | 
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 293 |     SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config)); | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 294 |     if (!idec) { | 
 | 295 |         return kInvalidInput; | 
 | 296 |     } | 
 | 297 |  | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 298 |     int rowsDecoded; | 
 | 299 |     SkCodec::Result result; | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 300 |     switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) { | 
 | 301 |         case VP8_STATUS_OK: | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 302 |             rowsDecoded = dstInfo.height(); | 
 | 303 |             result = kSuccess; | 
 | 304 |             break; | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 305 |         case VP8_STATUS_SUSPENDED: | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 306 |             WebPIDecGetRGB(idec, rowsDecodedPtr, nullptr, nullptr, nullptr); | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 307 |             rowsDecoded = *rowsDecodedPtr; | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 308 |             result = kIncompleteInput; | 
 | 309 |             break; | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 310 |         default: | 
 | 311 |             return kInvalidInput; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 312 |     } | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 313 |  | 
| Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 314 |     if (this->colorXform()) { | 
| msarett | cf7b877 | 2016-09-22 12:37:04 -0700 | [diff] [blame] | 315 |         SkColorSpaceXform::ColorFormat dstColorFormat = select_xform_format(dstInfo.colorType()); | 
| msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 316 |         SkAlphaType xformAlphaType = select_xform_alpha(dstInfo.alphaType(), | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 317 |                                                         this->getInfo().alphaType()); | 
 | 318 |  | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 319 |         uint32_t* src = (uint32_t*) config.output.u.RGBA.rgba; | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 320 |         size_t srcRowBytes = config.output.u.RGBA.stride; | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 321 |         for (int y = 0; y < rowsDecoded; y++) { | 
 | 322 |             SkAssertResult(this->colorXform()->apply(dstColorFormat, dst, | 
 | 323 |                     SkColorSpaceXform::kBGRA_8888_ColorFormat, src, dstInfo.width(), | 
| Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 324 |                     xformAlphaType)); | 
| Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame^] | 325 |             dst = SkTAddOffset<void>(dst, rowBytes); | 
 | 326 |             src = SkTAddOffset<uint32_t>(src, srcRowBytes); | 
| msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 327 |         } | 
 | 328 |     } | 
 | 329 |  | 
 | 330 |     return result; | 
| scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 331 | } | 
 | 332 |  | 
| msarett | 9d15dab | 2016-08-24 07:36:06 -0700 | [diff] [blame] | 333 | SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, | 
| msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 334 |                          sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPDemuxer* demux, | 
 | 335 |                          sk_sp<SkData> data) | 
 | 336 |     : INHERITED(width, height, info, stream, std::move(colorSpace)) | 
 | 337 |     , fDemux(demux) | 
 | 338 |     , fData(std::move(data)) | 
| msarett | 9d15dab | 2016-08-24 07:36:06 -0700 | [diff] [blame] | 339 | {} |