| Index: src/codec/SkScaledCodec.cpp
|
| diff --git a/src/codec/SkScaledCodec.cpp b/src/codec/SkScaledCodec.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..91d05a39a726b1719bab3536dd6a69e24d30166e
|
| --- /dev/null
|
| +++ b/src/codec/SkScaledCodec.cpp
|
| @@ -0,0 +1,170 @@
|
| +/*
|
| + * Copyright 2015 Google Inc.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#include "SkCodecPriv.h"
|
| +#include "SkScaledCodec.h"
|
| +#include "SkStream.h"
|
| +#include "SkWebpCodec.h"
|
| +
|
| +
|
| +SkCodec* SkScaledCodec::NewFromStream(SkStream* stream) {
|
| + SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream));
|
| + if (NULL == codec) {
|
| + return NULL;
|
| + }
|
| + if (codec->getEncodedFormat() == SkEncodedFormat::kWEBP_SkEncodedFormat) {
|
| + // Webp codec supports scaling and subsetting natively
|
| + return codec.detach();
|
| + }
|
| + // wrap in new SkScaledCodec
|
| + return SkNEW_ARGS(SkScaledCodec, (codec.detach()));
|
| +}
|
| +
|
| +SkCodec* SkScaledCodec::NewFromData(SkData* data) {
|
| + if (!data) {
|
| + return NULL;
|
| + }
|
| + return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data)));
|
| +}
|
| +
|
| +SkScaledCodec::SkScaledCodec(SkCodec* codec)
|
| + : INHERITED(codec->getInfo(), NULL)
|
| + , fCodec(codec)
|
| +{}
|
| +
|
| +SkScaledCodec::~SkScaledCodec() {}
|
| +
|
| +// returns a sample size based on the input src and dst dimensions
|
| +// can only down sample, so dstDimension must be <= than srcDimension
|
| +static int get_sample_size(int srcDimension, int dstDimension) {
|
| + SkASSERT(dstDimension <= srcDimension);
|
| + return srcDimension / dstDimension;
|
| +}
|
| +
|
| +// returns a scaled dimension based on the original dimension and the sampleSize
|
| +// NOTE: we round down here for scaled dimension
|
| +// This is to ensure the same behavior as SkImageDecoder, which we replaced
|
| +static int get_scaled_dimension(int srcDimension, int sampleSize) {
|
| + if (sampleSize > srcDimension) {
|
| + return 1;
|
| + }
|
| + return srcDimension / sampleSize;
|
| +}
|
| +
|
| +/*
|
| + * Return a valid set of output dimensions for this decoder, given an input scale
|
| + */
|
| +SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const {
|
| + // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ...
|
| +
|
| + if (desiredScale > 0.5f) {
|
| + // sampleSize = 1
|
| + return fCodec->getInfo().dimensions();
|
| + }
|
| + // sampleSize determines the step size between samples
|
| + // Ex: sampleSize = 2, sample every second pixel in x and y directions
|
| + int sampleSize = 1 / desiredScale;
|
| +
|
| + int scaledWidth = get_scaled_dimension(this->getInfo().width(), sampleSize);
|
| + int scaledHeight = get_scaled_dimension(this->getInfo().height(), sampleSize);
|
| +
|
| + // Return the calculated output dimensions for the given scale
|
| + return SkISize::Make(scaledWidth, scaledHeight);
|
| +}
|
| +
|
| +// check if scaling to dstInfo size from srcInfo size is possible
|
| +static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo){
|
| + const int dstWidth = dstInfo.width();
|
| + const int dstHeight = dstInfo.height();
|
| + const int srcWidth = srcInfo.width();
|
| + const int srcHeight = srcInfo.height();
|
| + // only support down sampling, not up sampling
|
| + if (dstWidth > srcWidth || dstHeight > srcHeight) {
|
| + return false;
|
| + }
|
| + // check that srcWidth is scaled down by an integer value
|
| + int sampleSizeX = get_sample_size(srcWidth, dstWidth);
|
| + if (get_scaled_dimension(srcWidth, sampleSizeX) != dstWidth) {
|
| + return false;
|
| + }
|
| + // check that src height is scaled down by an integer value
|
| + int sampleSizeY = get_sample_size(srcHeight, dstHeight);
|
| + if (get_scaled_dimension(srcHeight, sampleSizeY) != dstHeight) {
|
| + return false;
|
| + }
|
| + // sampleX and sampleY should be equal unless the original sampleSize requested was larger
|
| + // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstHeight = 1.
|
| + // This functionality allows for tall thin images to still be scaled down by scaling factors.
|
| + if (sampleSizeX != sampleSizeY){
|
| + if (1 != dstWidth && 1 != dstHeight) {
|
| + return false;
|
| + }
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +// TODO: Implement subsetting in onGetPixels which works when and when not sampling
|
| +
|
| +SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst,
|
| + size_t rowBytes, const Options& options,
|
| + SkPMColor ctable[], int* ctableCount) {
|
| +
|
| + // try to do a fCodec native decode, as some types of code support scaling and subsetting
|
| + Result result = fCodec->getPixels(requestedInfo, dst, rowBytes, &options, ctable, ctableCount);
|
| + if (kInvalidScale != result) {
|
| + // no scaling requested
|
| + return result;
|
| + }
|
| + // scaling requested
|
| + if (!scaling_supported(requestedInfo, fCodec->getInfo())) {
|
| + return kInvalidScale;
|
| + }
|
| +
|
| + int dstHeight = requestedInfo.height();
|
| + int srcHeight = fCodec->getInfo().height();
|
| +
|
| + // set sample values in x and y directions
|
| + int sampleY = get_sample_size(srcHeight, dstHeight);
|
| + int Y0 = sampleY >> 1;
|
| +
|
| + SkImageInfo info = requestedInfo;
|
| + // use original height as scanlineDecoder does not support y sampling natively
|
| + info = info.makeWH(requestedInfo.width(), srcHeight);
|
| +
|
| + SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDecoder(info,
|
| + &options, ctable, ctableCount));
|
| + if (NULL == scanlineDecoder) {
|
| + SkCodecPrintf("failed to create scanline decoder.\n");
|
| + return kInvalidInput;
|
| + }
|
| +
|
| + bool isHardToSample = scanlineDecoder->isHardToSample();
|
| +
|
| + if (isHardToSample) {
|
| + SkAutoMalloc storage(srcHeight * rowBytes);
|
| + void* storagePtr = storage.get();
|
| + scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes);
|
| +
|
| + storagePtr += Y0 * rowBytes;
|
| + for (int y = 0; y < dstHeight; y++) {
|
| + memcpy(dst, storagePtr, rowBytes);
|
| + storagePtr += sampleY * rowBytes;
|
| + dst += rowBytes;
|
| + }
|
| + } else {
|
| + // not hard to sample
|
| + scanlineDecoder->skipScanlines(Y0);
|
| + for (int y = 0; y < dstHeight; y++) {
|
| + scanlineDecoder->getScanlines(dst, 1, rowBytes);
|
| + if (y < dstHeight - 1) {
|
| + scanlineDecoder->skipScanlines(sampleY - 1);
|
| + }
|
| + dst += rowBytes;
|
| + }
|
| + }
|
| + return kSuccess;
|
| +}
|
|
|