diff options
author | Leon Scroggins III <scroggo@google.com> | 2018-01-16 15:01:17 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2018-01-16 21:23:08 +0000 |
commit | 07a722cdcf064fca22213dc06a433dd82f080c23 (patch) | |
tree | 8d8e7de91637af359b9ff8649c8bed317019cbf7 | |
parent | 539c6f5c92c2a4f04816d562c3d23556a35a2e98 (diff) |
Add SkAndroidCodec::computeSampledSize
Bug: b/63909536
Android's ImageDecoder API takes as input an arbitrary width and height
to scale the image to. Internally, this uses SkAndroidCodec to sample,
and then (if not a perfect match) scales to the desired size with
drawing.
computeSampledSize is a modified version of what ImageDecoder currently
does to convert from arbitrary dimensions to a sampleSize. Moving it
here allows it to be shared by SkAnimatedImage. The modified version
also corrects two bugs:
- a client using the dimensions returned by getSampledDimensions
previously may have resulted in ImageDecoder decoding to a larger
size and then scaling it. (example found in tests: dog.jpg is
180 x 180. getSampledDimensions(8) returns 23 x 23, but the old
method resulted in using sampleSize of 7 and downscaling the resulting
25 x 25 image.)
- recompute the sampleSize based on the size returned by
getSampledDimensions.
Change-Id: I022040e8bac31c20988903a0452257f7ae902bc7
Reviewed-on: https://skia-review.googlesource.com/94620
Reviewed-by: Derek Sollenberger <djsollen@google.com>
Commit-Queue: Leon Scroggins <scroggo@google.com>
-rw-r--r-- | gn/tests.gni | 1 | ||||
-rw-r--r-- | include/codec/SkAndroidCodec.h | 11 | ||||
-rw-r--r-- | src/codec/SkAndroidCodec.cpp | 94 | ||||
-rw-r--r-- | tests/AndroidCodecTest.cpp | 119 |
4 files changed, 225 insertions, 0 deletions
diff --git a/gn/tests.gni b/gn/tests.gni index a4054b8c01..18defe8ae1 100644 --- a/gn/tests.gni +++ b/gn/tests.gni @@ -7,6 +7,7 @@ _tests = get_path_info("../tests", "abspath") tests_sources = [ + "$_tests/AndroidCodecTest.cpp", "$_tests/AAClipTest.cpp", "$_tests/AnnotationTest.cpp", "$_tests/ApplyGammaTest.cpp", diff --git a/include/codec/SkAndroidCodec.h b/include/codec/SkAndroidCodec.h index 2bf38025bf..0746f066c8 100644 --- a/include/codec/SkAndroidCodec.h +++ b/include/codec/SkAndroidCodec.h @@ -91,6 +91,17 @@ public: sk_sp<SkColorSpace> prefColorSpace = nullptr); /** + * Compute the appropriate sample size to get to |size|. + * + * @param size As an input parameter, the desired output size of + * the decode. As an output parameter, the smallest sampled size + * larger than the input. + * @return the sample size to set AndroidOptions::fSampleSize to decode + * to the output |size|. + */ + int computeSampleSize(SkISize* size) const; + + /** * Returns the dimensions of the scaled output image, for an input * sampleSize. * diff --git a/src/codec/SkAndroidCodec.cpp b/src/codec/SkAndroidCodec.cpp index a7ffa4e93b..486257f9a3 100644 --- a/src/codec/SkAndroidCodec.cpp +++ b/src/codec/SkAndroidCodec.cpp @@ -186,6 +186,100 @@ sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputCo } } +static bool supports_any_down_scale(const SkCodec* codec) { + return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP; +} + +// There are a variety of ways two SkISizes could be compared. This method +// returns true if either dimensions of a is < that of b. +// computeSampleSize also uses the opposite, which means that both +// dimensions of a >= b. +static inline bool smaller_than(const SkISize& a, const SkISize& b) { + return a.width() < b.width() || a.height() < b.height(); +} + +// Both dimensions of a > that of b. +static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) { + return a.width() > b.width() && a.height() > b.height(); +} + +int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const { + SkASSERT(desiredSize); + + if (!desiredSize || *desiredSize == fInfo.dimensions()) { + return 1; + } + + if (smaller_than(fInfo.dimensions(), *desiredSize)) { + *desiredSize = fInfo.dimensions(); + return 1; + } + + // Handle bad input: + if (desiredSize->width() < 1 || desiredSize->height() < 1) { + *desiredSize = SkISize::Make(std::max(1, desiredSize->width()), + std::max(1, desiredSize->height())); + } + + if (supports_any_down_scale(fCodec.get())) { + return 1; + } + + int sampleX = fInfo.width() / desiredSize->width(); + int sampleY = fInfo.height() / desiredSize->height(); + int sampleSize = std::min(sampleX, sampleY); + auto computedSize = this->getSampledDimensions(sampleSize); + if (computedSize == *desiredSize) { + return sampleSize; + } + + if (computedSize == fInfo.dimensions() || sampleSize == 1) { + // Cannot downscale + *desiredSize = computedSize; + return 1; + } + + if (strictly_bigger_than(computedSize, *desiredSize)) { + // See if there is a tighter fit. + while (true) { + auto smaller = this->getSampledDimensions(sampleSize + 1); + if (smaller == *desiredSize) { + return sampleSize + 1; + } + if (smaller == computedSize || smaller_than(smaller, *desiredSize)) { + // Cannot get any smaller without being smaller than desired. + *desiredSize = computedSize; + return sampleSize; + } + + sampleSize++; + computedSize = smaller; + } + + SkASSERT(false); + } + + if (!smaller_than(computedSize, *desiredSize)) { + // This means one of the computed dimensions is equal to desired, and + // the other is bigger. This is as close as we can get. + *desiredSize = computedSize; + return sampleSize; + } + + // computedSize is too small. Make it larger. + while (sampleSize > 2) { + auto bigger = this->getSampledDimensions(sampleSize - 1); + if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) { + *desiredSize = bigger; + return sampleSize - 1; + } + sampleSize--; + } + + *desiredSize = fInfo.dimensions(); + return 1; +} + SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { if (!is_valid_sample_size(sampleSize)) { return {0, 0}; diff --git a/tests/AndroidCodecTest.cpp b/tests/AndroidCodecTest.cpp new file mode 100644 index 0000000000..b86a210279 --- /dev/null +++ b/tests/AndroidCodecTest.cpp @@ -0,0 +1,119 @@ +/* + * Copyright 2018 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkAndroidCodec.h" +#include "SkCodec.h" +#include "SkEncodedImageFormat.h" + +#include "Resources.h" +#include "Test.h" + +static SkISize times(const SkISize& size, float factor) { + return { (int) (size.width() * factor), (int) (size.height() * factor) }; +} + +static SkISize plus(const SkISize& size, int term) { + return { size.width() + term, size.height() + term }; +} + +static bool invalid(const SkISize& size) { + return size.width() < 1 || size.height() < 1; +} + +DEF_TEST(AndroidCodec_computeSampleSize, r) { + if (GetResourcePath().isEmpty()) { + return; + } + for (const char* file : { "images/color_wheel.webp", + "images/ship.png", + "images/dog.jpg", + "images/color_wheel.gif", + "images/rle.bmp", + "images/google_chrome.ico", + "images/mandrill.wbmp", +#ifdef SK_CODEC_DECODES_RAW + "images/sample_1mp.dng", +#endif + }) { + auto data = GetResourceAsData(file); + if (!data) { + ERRORF(r, "Could not get %s", file); + continue; + } + + auto codec = SkAndroidCodec::MakeFromCodec(SkCodec::MakeFromData(std::move(data))); + if (!codec) { + ERRORF(r, "Could not create codec for %s", file); + continue; + } + + const auto dims = codec->getInfo().dimensions(); + const SkISize downscales[] = { + plus(dims, -1), + times(dims, .15f), + times(dims, .6f), + { (int32_t) (dims.width() * .25f), (int32_t) (dims.height() * .75f ) }, + { 1, 1 }, + { 1, 2 }, + { 2, 1 }, + { 0, -1 }, + { dims.width(), dims.height() - 1 }, + }; + for (SkISize size : downscales) { + const auto requested = size; + const int computedSampleSize = codec->computeSampleSize(&size); + REPORTER_ASSERT(r, size.width() >= 1 && size.height() >= 1); + if (codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP) { + // WebP supports arbitrary down-scaling. + REPORTER_ASSERT(r, size == requested || invalid(requested)); + } else if (computedSampleSize == 1) { + REPORTER_ASSERT(r, size == dims); + } else { + REPORTER_ASSERT(r, computedSampleSize > 1); + if (size.width() >= dims.width() || size.height() >= dims.height()) { + ERRORF(r, "File %s's computed sample size (%i) is bigger than" + " original? original: %i x %i\tsampled: %i x %i", + file, computedSampleSize, dims.width(), dims.height(), + size.width(), size.height()); + } + REPORTER_ASSERT(r, size.width() >= requested.width() && + size.height() >= requested.height()); + REPORTER_ASSERT(r, size.width() < dims.width() && + size.height() < dims.height()); + } + } + + const SkISize upscales[] = { + dims, plus(dims, 5), times(dims, 2), + }; + for (SkISize size : upscales) { + const int computedSampleSize = codec->computeSampleSize(&size); + REPORTER_ASSERT(r, computedSampleSize == 1); + REPORTER_ASSERT(r, dims == size); + } + + // This mimics how Android's ImageDecoder uses SkAndroidCodec. A client + // can choose their dimensions based on calling getSampledDimensions, + // but the ImageDecoder API takes an arbitrary size. It then uses + // computeSampleSize to determine the best dimensions and sampleSize. + // It should return the same dimensions. the sampleSize may be different + // due to integer division. + for (int sampleSize : { 1, 2, 3, 4, 8, 16, 32 }) { + const SkISize sampledDims = codec->getSampledDimensions(sampleSize); + SkISize size = sampledDims; + const int computedSampleSize = codec->computeSampleSize(&size); + if (sampledDims != size) { + ERRORF(r, "File '%s'->getSampledDimensions(%i) yields computed" + " sample size of %i\n\tsampledDimensions: %i x %i\t" + "computed dimensions: %i x %i", + file, sampleSize, computedSampleSize, + sampledDims.width(), sampledDims.height(), + size.width(), size.height()); + } + } + } +} |