Respect canvas size and frame offset in webp decoder

BUG=skia:6185

Change-Id: Id543cb689a5e33b800ebbc18f4a234e78a4c4298
Reviewed-on: https://skia-review.googlesource.com/7800
Commit-Queue: Matt Sarett <msarett@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
This commit is contained in:
Matt Sarett 2017-02-03 15:24:19 -05:00 committed by Skia Commit-Bot
parent d1ac9823fd
commit 0f33970c8d
5 changed files with 312 additions and 280 deletions

View File

@ -1 +1 @@
19 20

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 374 B

View File

@ -7,9 +7,10 @@
#include "SkCodecPriv.h" #include "SkCodecPriv.h"
#include "SkColorSpaceXform.h" #include "SkColorSpaceXform.h"
#include "SkWebpCodec.h" #include "SkSampler.h"
#include "SkStreamPriv.h" #include "SkStreamPriv.h"
#include "SkTemplates.h" #include "SkTemplates.h"
#include "SkWebpCodec.h"
// A WebP decoder on top of (subset of) libwebp // A WebP decoder on top of (subset of) libwebp
// For more information on WebP image format, and libwebp library, see: // For more information on WebP image format, and libwebp library, see:
@ -59,6 +60,21 @@ SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
return nullptr; return nullptr;
} }
const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
// Sanity check for image size that's about to be decoded.
{
const int64_t size = sk_64_mul(width, height);
if (!sk_64_isS32(size)) {
return nullptr;
}
// now check that if we are 4-bytes per pixel, we also don't overflow
if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
return nullptr;
}
}
WebPChunkIterator chunkIterator; WebPChunkIterator chunkIterator;
SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator); SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
sk_sp<SkColorSpace> colorSpace = nullptr; sk_sp<SkColorSpace> colorSpace = nullptr;
@ -73,36 +89,15 @@ SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
} }
// Since we do not yet support animation, we get the |width|, |height|, |color|, and |alpha| // Get the first frame and its "features" to determine the color and alpha types.
// from the first frame. It's the only frame we will decode. // Since we do not yet support animated webp, this is the only frame that we will
// // decode.
// TODO:
// When we support animation, we'll want to report the canvas width and canvas height instead.
// We can get these from the |demux| directly.
// What |color| and |alpha| will we want to report though? WebP allows different frames
// to be encoded in different ways, making the encoded format difficult to describe.
WebPIterator frame; WebPIterator frame;
SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
if (!WebPDemuxGetFrame(demux, 1, &frame)) { if (!WebPDemuxGetFrame(demux, 1, &frame)) {
return nullptr; return nullptr;
} }
// Sanity check for image size that's about to be decoded.
{
const int64_t size = sk_64_mul(frame.width, frame.height);
if (!sk_64_isS32(size)) {
return nullptr;
}
// now check that if we are 4-bytes per pixel, we also don't overflow
if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
return nullptr;
}
}
// TODO:
// The only reason we actually need to call WebPGetFeatures() is to get the |features.format|.
// This call actually re-reads the frame header. Should we suggest that libwebp expose
// the format on the |frame|?
WebPBitstreamFeatures features; WebPBitstreamFeatures features;
VP8StatusCode status = WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features); VP8StatusCode status = WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features);
if (VP8_STATUS_OK != status) { if (VP8_STATUS_OK != status) {
@ -113,8 +108,8 @@ SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
SkEncodedInfo::Alpha alpha; SkEncodedInfo::Alpha alpha;
switch (features.format) { switch (features.format) {
case 0: case 0:
// This indicates a "mixed" format. We would see this for // This indicates a "mixed" format. We could see this for
// animated webps or for webps encoded in multiple fragments. // animated webps (multiple fragments).
// I believe that this is a rare case. // I believe that this is a rare case.
// We could also guess kYUV here, but I think it makes more // We could also guess kYUV here, but I think it makes more
// sense to guess kBGRA which is likely closer to the final // sense to guess kBGRA which is likely closer to the final
@ -125,7 +120,7 @@ SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
break; break;
case 1: case 1:
// This is the lossy format (YUV). // This is the lossy format (YUV).
if (SkToBool(features.has_alpha)) { if (SkToBool(features.has_alpha) || frame.width != width || frame.height != height) {
color = SkEncodedInfo::kYUVA_Color; color = SkEncodedInfo::kYUVA_Color;
alpha = SkEncodedInfo::kUnpremul_Alpha; alpha = SkEncodedInfo::kUnpremul_Alpha;
} else { } else {
@ -143,9 +138,9 @@ SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
} }
SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
SkWebpCodec* codecOut = new SkWebpCodec(features.width, features.height, info, SkWebpCodec* codecOut = new SkWebpCodec(width, height, info, std::move(colorSpace),
std::move(colorSpace), streamDeleter.release(), streamDeleter.release(), demux.release(),
demux.release(), std::move(data)); std::move(data));
codecOut->setUnsupportedICC(unsupportedICC); codecOut->setUnsupportedICC(unsupportedICC);
return codecOut; return codecOut;
} }
@ -217,48 +212,85 @@ SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
// Free any memory associated with the buffer. Must be called last, so we declare it first. // Free any memory associated with the buffer. Must be called last, so we declare it first.
SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output)); SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); WebPIterator frame;
SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
// If this succeeded in NewFromStream(), it should succeed again here.
SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame));
// Get the frameRect. libwebp will have already signaled an error if this is not fully
// contained by the canvas.
auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
SkASSERT(this->getInfo().bounds().contains(frameRect));
bool frameIsSubset = frameRect.size() != this->getInfo().dimensions();
if (frameIsSubset) {
SkSampler::Fill(dstInfo, dst, rowBytes, 0, options.fZeroInitialized);
}
int dstX = frameRect.x();
int dstY = frameRect.y();
int subsetWidth = frameRect.width();
int subsetHeight = frameRect.height();
if (options.fSubset) { if (options.fSubset) {
// Caller is requesting a subset. SkIRect subset = *options.fSubset;
if (!bounds.contains(*options.fSubset)) { SkASSERT(this->getInfo().bounds().contains(subset));
// The subset is out of bounds. SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
return kInvalidParameters; SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
if (!SkIRect::IntersectsNoEmptyCheck(subset, frameRect)) {
return kSuccess;
} }
bounds = *options.fSubset; int minXOffset = SkTMin(dstX, subset.x());
int minYOffset = SkTMin(dstY, subset.y());
dstX -= minXOffset;
dstY -= minYOffset;
frameRect.offset(-minXOffset, -minYOffset);
subset.offset(-minXOffset, -minYOffset);
// This is tricky. libwebp snaps the top and left to even values. We could let libwebp // Just like we require that the requested subset x and y offset are even, libwebp
// do the snap, and return a subset which is a different one than requested. The problem // guarantees that the frame x and y offset are even (it's actually impossible to specify
// with that approach is that the caller may try to stitch subsets together, and if we // an odd frame offset). So we can still guarantee that the adjusted offsets are even.
// returned different subsets than requested, there would be artifacts at the boundaries. SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
// Instead, we report that we cannot support odd values for top and left..
if (!SkIsAlign2(bounds.fLeft) || !SkIsAlign2(bounds.fTop)) {
return kInvalidParameters;
}
#ifdef SK_DEBUG SkIRect intersection;
{ SkAssertResult(intersection.intersect(frameRect, subset));
// Make a copy, since getValidSubset can change its input. subsetWidth = intersection.width();
SkIRect subset(bounds); subsetHeight = intersection.height();
// That said, getValidSubset should *not* change its input, in this case; otherwise
// getValidSubset does not match the actual subsets we can do.
SkASSERT(this->getValidSubset(&subset) && subset == bounds);
}
#endif
config.options.use_cropping = 1; config.options.use_cropping = 1;
config.options.crop_left = bounds.fLeft; config.options.crop_left = subset.x();
config.options.crop_top = bounds.fTop; config.options.crop_top = subset.y();
config.options.crop_width = bounds.width(); config.options.crop_width = subsetWidth;
config.options.crop_height = bounds.height(); config.options.crop_height = subsetHeight;
} }
SkISize dstDimensions = dstInfo.dimensions(); // Ignore the frame size and offset when determining if scaling is necessary.
if (bounds.size() != dstDimensions) { int scaledWidth = subsetWidth;
// Caller is requesting scaling. int scaledHeight = subsetHeight;
SkISize srcSize = options.fSubset ? options.fSubset->size() : this->getInfo().dimensions();
if (srcSize != dstInfo.dimensions()) {
config.options.use_scaling = 1; config.options.use_scaling = 1;
config.options.scaled_width = dstDimensions.width();
config.options.scaled_height = dstDimensions.height(); if (frameIsSubset) {
float scaleX = ((float) dstInfo.width()) / srcSize.width();
float scaleY = ((float) dstInfo.height()) / srcSize.height();
// We need to be conservative here and floor rather than round.
// Otherwise, we may find ourselves decoding off the end of memory.
dstX = scaleX * dstX;
scaledWidth = scaleX * scaledWidth;
dstY = scaleY * dstY;
scaledHeight = scaleY * scaledHeight;
if (0 == scaledWidth || 0 == scaledHeight) {
return kSuccess;
}
} else {
scaledWidth = dstInfo.width();
scaledHeight = dstInfo.height();
}
config.options.scaled_width = scaledWidth;
config.options.scaled_height = scaledHeight;
} }
// Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a
@ -274,21 +306,17 @@ SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
// does not provide a row-by-row API. This is a shame particularly in the F16 case, // does not provide a row-by-row API. This is a shame particularly in the F16 case,
// where we need to allocate an extra image-sized buffer. // where we need to allocate an extra image-sized buffer.
SkAutoTMalloc<uint32_t> pixels; SkAutoTMalloc<uint32_t> pixels;
if (kRGBA_F16_SkColorType == dstInfo.colorType()) { bool isF16 = kRGBA_F16_SkColorType == dstInfo.colorType();
pixels.reset(dstDimensions.width() * dstDimensions.height()); void* webpDst = isF16 ? pixels.reset(dstInfo.width() * dstInfo.height()) : dst;
config.output.u.RGBA.rgba = (uint8_t*) pixels.get(); size_t webpRowBytes = isF16 ? dstInfo.width() * sizeof(uint32_t) : rowBytes;
config.output.u.RGBA.stride = (int) dstDimensions.width() * sizeof(uint32_t); size_t totalBytes = isF16 ? webpRowBytes * dstInfo.height() : dstInfo.getSafeSize(webpRowBytes);
config.output.u.RGBA.size = config.output.u.RGBA.stride * dstDimensions.height(); size_t dstBpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
} else { size_t webpBpp = isF16 ? sizeof(uint32_t) : dstBpp;
config.output.u.RGBA.rgba = (uint8_t*) dst;
config.output.u.RGBA.stride = (int) rowBytes;
config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes);
}
WebPIterator frame; size_t offset = dstX * webpBpp + dstY * webpRowBytes;
SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); config.output.u.RGBA.rgba = SkTAddOffset<uint8_t>(webpDst, offset);
// If this succeeded in NewFromStream(), it should succeed again here. config.output.u.RGBA.stride = (int) webpRowBytes;
SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame)); config.output.u.RGBA.size = totalBytes - offset;
SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config)); SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
if (!idec) { if (!idec) {
@ -304,7 +332,8 @@ SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
break; break;
case VP8_STATUS_SUSPENDED: case VP8_STATUS_SUSPENDED:
WebPIDecGetRGB(idec, rowsDecodedPtr, nullptr, nullptr, nullptr); WebPIDecGetRGB(idec, rowsDecodedPtr, nullptr, nullptr, nullptr);
rowsDecoded = *rowsDecodedPtr;
rowsDecoded = dstY + *rowsDecodedPtr;
result = kIncompleteInput; result = kIncompleteInput;
break; break;
default: default:
@ -316,14 +345,15 @@ SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
SkAlphaType xformAlphaType = select_xform_alpha(dstInfo.alphaType(), SkAlphaType xformAlphaType = select_xform_alpha(dstInfo.alphaType(),
this->getInfo().alphaType()); this->getInfo().alphaType());
uint32_t* src = (uint32_t*) config.output.u.RGBA.rgba; uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
void* xformDst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
size_t srcRowBytes = config.output.u.RGBA.stride; size_t srcRowBytes = config.output.u.RGBA.stride;
for (int y = 0; y < rowsDecoded; y++) { for (int y = 0; y < rowsDecoded - dstY; y++) {
SkAssertResult(this->colorXform()->apply(dstColorFormat, dst, SkAssertResult(this->colorXform()->apply(dstColorFormat, xformDst,
SkColorSpaceXform::kBGRA_8888_ColorFormat, src, dstInfo.width(), SkColorSpaceXform::kBGRA_8888_ColorFormat, xformSrc, scaledWidth,
xformAlphaType)); xformAlphaType));
dst = SkTAddOffset<void>(dst, rowBytes); xformDst = SkTAddOffset<void>(xformDst, rowBytes);
src = SkTAddOffset<uint32_t>(src, srcRowBytes); xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
} }
} }

View File

@ -662,6 +662,8 @@ DEF_TEST(Codec_Empty, r) {
test_invalid(r, "empty_images/zero-height.wbmp"); test_invalid(r, "empty_images/zero-height.wbmp");
// This image is an ico with an embedded mask-bmp. This is illegal. // This image is an ico with an embedded mask-bmp. This is illegal.
test_invalid(r, "invalid_images/mask-bmp-ico.ico"); test_invalid(r, "invalid_images/mask-bmp-ico.ico");
// It is illegal for a webp frame to not be fully contained by the canvas.
test_invalid(r, "invalid_images/invalid-offset.webp");
#if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32)) #if defined(SK_CODEC_DECODES_RAW) && (!defined(_WIN32))
test_invalid(r, "empty_images/zero_height.tiff"); test_invalid(r, "empty_images/zero_height.tiff");
#endif #endif