Skia
2D Graphics Library
SkSamplingOptions.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 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 
8 #ifndef SkImageSampling_DEFINED
9 #define SkImageSampling_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 
13 #include <algorithm>
14 #include <new>
15 
16 enum class SkFilterMode {
17  kNearest, // single sample point (nearest neighbor)
18  kLinear, // interporate between 2x2 sample points (bilinear interpolation)
19 
20  kLast = kLinear,
21 };
22 static constexpr int kSkFilterModeCount = static_cast<int>(SkFilterMode::kLast) + 1;
23 
24 enum class SkMipmapMode {
25  kNone, // ignore mipmap levels, sample from the "base"
26  kNearest, // sample from the nearest level
27  kLinear, // interpolate between the two nearest levels
28 
29  kLast = kLinear,
30 };
31 static constexpr int kSkMipmapModeCount = static_cast<int>(SkMipmapMode::kLast) + 1;
32 
33 /*
34  * Specify B and C (each between 0...1) to create a shader that applies the corresponding
35  * cubic reconstruction filter to the image.
36  *
37  * Example values:
38  * B = 1/3, C = 1/3 "Mitchell" filter
39  * B = 0, C = 1/2 "Catmull-Rom" filter
40  *
41  * See "Reconstruction Filters in Computer Graphics"
42  * Don P. Mitchell
43  * Arun N. Netravali
44  * 1988
45  * https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf
46  *
47  * Desmos worksheet https://www.desmos.com/calculator/aghdpicrvr
48  * Nice overview https://entropymine.com/imageworsener/bicubic/
49  */
51  float B, C;
52 
53  // Historic default for kHigh_SkFilterQuality
54  static constexpr SkCubicResampler Mitchell() { return {1/3.0f, 1/3.0f}; }
55  static constexpr SkCubicResampler CatmullRom() { return {0.0f, 1/2.0f}; }
56 };
57 
58 struct SK_API SkSamplingOptions {
59  const int maxAniso = 0;
60  const bool useCubic = false;
61  const SkCubicResampler cubic = {0, 0};
64 
65  constexpr SkSamplingOptions() = default;
68  this->~SkSamplingOptions(); // A pedantic no-op.
69  new (this) SkSamplingOptions(that);
70  return *this;
71  }
72 
74  : filter(fm)
75  , mipmap(mm) {}
76 
77  explicit constexpr SkSamplingOptions(SkFilterMode fm)
78  : filter(fm)
79  , mipmap(SkMipmapMode::kNone) {}
80 
81  explicit constexpr SkSamplingOptions(const SkCubicResampler& c)
82  : useCubic(true)
83  , cubic(c) {}
84 
85  static constexpr SkSamplingOptions Aniso(int maxAniso) {
86  return SkSamplingOptions{std::max(maxAniso, 1)};
87  }
88 
89  bool operator==(const SkSamplingOptions& other) const {
90  return maxAniso == other.maxAniso
91  && useCubic == other.useCubic
92  && cubic.B == other.cubic.B
93  && cubic.C == other.cubic.C
94  && filter == other.filter
95  && mipmap == other.mipmap;
96  }
97  bool operator!=(const SkSamplingOptions& other) const { return !(*this == other); }
98 
99  bool isAniso() const { return maxAniso != 0; }
100 
101 private:
102  constexpr SkSamplingOptions(int maxAniso) : maxAniso(maxAniso) {}
103 };
104 
105 #endif
@ kNone
glyph outlines unchanged
SkFilterMode
Definition: SkSamplingOptions.h:16
SkMipmapMode
Definition: SkSamplingOptions.h:24
static constexpr int kSkFilterModeCount
Definition: SkSamplingOptions.h:22
static constexpr int kSkMipmapModeCount
Definition: SkSamplingOptions.h:31
Definition: SkSamplingOptions.h:50
static constexpr SkCubicResampler CatmullRom()
Definition: SkSamplingOptions.h:55
float C
Definition: SkSamplingOptions.h:51
float B
Definition: SkSamplingOptions.h:51
static constexpr SkCubicResampler Mitchell()
Definition: SkSamplingOptions.h:54
Definition: SkSamplingOptions.h:58
constexpr SkSamplingOptions(SkFilterMode fm, SkMipmapMode mm)
Definition: SkSamplingOptions.h:73
bool operator==(const SkSamplingOptions &other) const
Definition: SkSamplingOptions.h:89
bool isAniso() const
Definition: SkSamplingOptions.h:99
static constexpr SkSamplingOptions Aniso(int maxAniso)
Definition: SkSamplingOptions.h:85
constexpr SkSamplingOptions(const SkCubicResampler &c)
Definition: SkSamplingOptions.h:81
const SkCubicResampler cubic
Definition: SkSamplingOptions.h:61
constexpr SkSamplingOptions()=default
bool operator!=(const SkSamplingOptions &other) const
Definition: SkSamplingOptions.h:97
const SkFilterMode filter
Definition: SkSamplingOptions.h:62
SkSamplingOptions(const SkSamplingOptions &)=default
SkSamplingOptions & operator=(const SkSamplingOptions &that)
Definition: SkSamplingOptions.h:67
constexpr SkSamplingOptions(SkFilterMode fm)
Definition: SkSamplingOptions.h:77
const SkMipmapMode mipmap
Definition: SkSamplingOptions.h:63
const bool useCubic
Definition: SkSamplingOptions.h:60
const int maxAniso
Definition: SkSamplingOptions.h:59