skia2/include/core/SkSamplingOptions.h

104 lines
3.1 KiB
C
Raw Normal View History

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkImageSampling_DEFINED
#define SkImageSampling_DEFINED
#include "include/core/SkTypes.h"
#include <algorithm>
#include <new>
enum class SkFilterMode {
kNearest, // single sample point (nearest neighbor)
kLinear, // interporate between 2x2 sample points (bilinear interpolation)
kLast = kLinear,
};
enum class SkMipmapMode {
kNone, // ignore mipmap levels, sample from the "base"
kNearest, // sample from the nearest level
kLinear, // interpolate between the two nearest levels
kLast = kLinear,
};
/*
* Specify B and C (each between 0...1) to create a shader that applies the corresponding
* cubic reconstruction filter to the image.
*
* Example values:
* B = 1/3, C = 1/3 "Mitchell" filter
* B = 0, C = 1/2 "Catmull-Rom" filter
*
* See "Reconstruction Filters in Computer Graphics"
* Don P. Mitchell
* Arun N. Netravali
* 1988
* https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf
*
* Desmos worksheet https://www.desmos.com/calculator/aghdpicrvr
* Nice overview https://entropymine.com/imageworsener/bicubic/
*/
struct SkCubicResampler {
float B, C;
// Historic default for kHigh_SkFilterQuality
static constexpr SkCubicResampler Mitchell() { return {1/3.0f, 1/3.0f}; }
static constexpr SkCubicResampler CatmullRom() { return {0.0f, 1/2.0f}; }
};
struct SK_API SkSamplingOptions {
const int maxAniso = 0;
const bool useCubic = false;
const SkCubicResampler cubic = {0, 0};
const SkFilterMode filter = SkFilterMode::kNearest;
const SkMipmapMode mipmap = SkMipmapMode::kNone;
SkSamplingOptions() = default;
SkSamplingOptions(const SkSamplingOptions&) = default;
SkSamplingOptions& operator=(const SkSamplingOptions& that) {
this->~SkSamplingOptions(); // A pedantic no-op.
new (this) SkSamplingOptions(that);
return *this;
}
SkSamplingOptions(SkFilterMode fm, SkMipmapMode mm)
: filter(fm)
, mipmap(mm) {}
explicit SkSamplingOptions(SkFilterMode fm)
: filter(fm)
, mipmap(SkMipmapMode::kNone) {}
explicit SkSamplingOptions(const SkCubicResampler& c)
: useCubic(true)
, cubic(c) {}
static SkSamplingOptions Aniso(int maxAniso) {
return SkSamplingOptions{std::max(maxAniso, 1)};
}
Reland "[skottie] Add image sampling and transform options" This reverts commit b81842aa283e4c5d6155b94453b81b58f5888a96. Reason for revert: reland with fixes Original change's description: > Revert "[skottie] Add image sampling and transform options" > > This reverts commit 2f24405250708f7d81b52ab88013ccae32607deb. > > Reason for revert: broke Win/shared > > Original change's description: > > [skottie] Add image sampling and transform options > > > > Expand the SkImageAsset API to support controlling sampling options and > > pass an additional transform. > > > > Bug: skia:10944, skia:10942 > > Change-Id: I7bad0b2ab58ed40fe4b425de0eb6970a4c7d7117 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340097 > > Commit-Queue: Florin Malita <fmalita@chromium.org> > > Commit-Queue: Florin Malita <fmalita@google.com> > > Reviewed-by: Mike Reed <reed@google.com> > > TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com,aparchur@google.com > > Change-Id: I59d4161356ffdc20588f1bd3beb33c54e44807a2 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10944 > Bug: skia:10942 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340619 > Reviewed-by: Florin Malita <fmalita@google.com> > Commit-Queue: Florin Malita <fmalita@google.com> TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com,aparchur@google.com # Not skipping CQ checks because this is a reland. Bug: skia:10944 Bug: skia:10942 Change-Id: I91892f4db6366ceb07d1a49a7bc54da17cea5399 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340657 Reviewed-by: Mike Reed <reed@google.com> Reviewed-by: Brian Osman <brianosman@google.com> Commit-Queue: Florin Malita <fmalita@chromium.org> Commit-Queue: Florin Malita <fmalita@google.com>
2020-12-03 17:51:28 +00:00
bool operator==(const SkSamplingOptions& other) const {
return maxAniso == other.maxAniso
&& useCubic == other.useCubic
Reland "[skottie] Add image sampling and transform options" This reverts commit b81842aa283e4c5d6155b94453b81b58f5888a96. Reason for revert: reland with fixes Original change's description: > Revert "[skottie] Add image sampling and transform options" > > This reverts commit 2f24405250708f7d81b52ab88013ccae32607deb. > > Reason for revert: broke Win/shared > > Original change's description: > > [skottie] Add image sampling and transform options > > > > Expand the SkImageAsset API to support controlling sampling options and > > pass an additional transform. > > > > Bug: skia:10944, skia:10942 > > Change-Id: I7bad0b2ab58ed40fe4b425de0eb6970a4c7d7117 > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340097 > > Commit-Queue: Florin Malita <fmalita@chromium.org> > > Commit-Queue: Florin Malita <fmalita@google.com> > > Reviewed-by: Mike Reed <reed@google.com> > > TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com,aparchur@google.com > > Change-Id: I59d4161356ffdc20588f1bd3beb33c54e44807a2 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: skia:10944 > Bug: skia:10942 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340619 > Reviewed-by: Florin Malita <fmalita@google.com> > Commit-Queue: Florin Malita <fmalita@google.com> TBR=fmalita@chromium.org,fmalita@google.com,reed@google.com,aparchur@google.com # Not skipping CQ checks because this is a reland. Bug: skia:10944 Bug: skia:10942 Change-Id: I91892f4db6366ceb07d1a49a7bc54da17cea5399 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/340657 Reviewed-by: Mike Reed <reed@google.com> Reviewed-by: Brian Osman <brianosman@google.com> Commit-Queue: Florin Malita <fmalita@chromium.org> Commit-Queue: Florin Malita <fmalita@google.com>
2020-12-03 17:51:28 +00:00
&& cubic.B == other.cubic.B
&& cubic.C == other.cubic.C
&& filter == other.filter
&& mipmap == other.mipmap;
}
bool operator!=(const SkSamplingOptions& other) const { return !(*this == other); }
bool isAniso() const { return maxAniso != 0; }
private:
SkSamplingOptions(int maxAniso) : maxAniso(maxAniso) {}
};
#endif