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