Debian Codegraph @

src/effects/SkTableMaskFilter.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright 2011 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 #include "include/effects/SkTableMaskFilter.h"
9
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkFlattenable.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkMaskFilter.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkTypes.h"
19 #include "include/effects/SkImageFilters.h"
20 #include "include/private/base/SkAlign.h"
21 #include "include/private/base/SkFixed.h"
22 #include "include/private/base/SkFloatingPoint.h"
23 #include "include/private/base/SkTPin.h"
24 #include "src/core/SkMask.h"
25 #include "src/core/SkMaskFilterBase.h"
26 #include "src/core/SkReadBuffer.h"
27 #include "src/core/SkWriteBuffer.h"
28
29 #include <cmath>
30 #include <cstdint>
31 #include <cstring>
32 #include <utility>
33
34 class SkMatrix;
35 class SkPaint;
36
37 class SkTableMaskFilterImpl : public SkMaskFilterBase {
38 public:
39 explicit SkTableMaskFilterImpl(const uint8_t table[256]);
40
41 SkMask::Format getFormat() const override;
42 bool filterMask(SkMaskBuilder*, const SkMask&, const SkMatrix&, SkIPoint*) const override;
43 SkMaskFilterBase::Type type() const override { return SkMaskFilterBase::Type::kTable; }
44 std::pair<sk_sp<SkImageFilter>, bool> asImageFilter(const SkMatrix&,
45 const SkPaint&) const override;
46
47 protected:
48 ~SkTableMaskFilterImpl() override;
49
50 void flatten(SkWriteBuffer&) const override;
51
52 private:
53 SK_FLATTENABLE_HOOKS(SkTableMaskFilterImpl)
54
55 SkTableMaskFilterImpl();
56
57 uint8_t fTable[256];
58
59 using INHERITED = SkMaskFilter;
60
61 friend class SkTableMaskFilter;
62 };
63
64 SkTableMaskFilterImpl::SkTableMaskFilterImpl() {
65 for (int i = 0; i < 256; i++) {
66 fTable[i] = i;
67 }
68 }
69
70 SkTableMaskFilterImpl::SkTableMaskFilterImpl(const uint8_t table[256]) {
71 memcpy(fTable, table, sizeof(fTable));
72 }
73
74 SkTableMaskFilterImpl::~SkTableMaskFilterImpl() {}
75
76 bool SkTableMaskFilterImpl::filterMask(SkMaskBuilder* dst, const SkMask& src,
77 const SkMatrix&, SkIPoint* margin) const {
78 if (src.fFormat != SkMask::kA8_Format) {
79 return false;
80 }
•81 // SkAlign4 overflows when too close to INT32_MAX, so reject when too big.
82 constexpr int32_t kMaxWidth = 1 << 30;
83 if (src.fBounds.width() > kMaxWidth) {
84 return false;
85 }
86 dst->bounds() = src.fBounds;
87 dst->rowBytes() = SkAlign4(dst->fBounds.width());
88 dst->format() = SkMask::kA8_Format;
89 dst->image() = nullptr;
90
91 if (src.fImage) {
•92 auto imgSize = dst->computeImageSize();
93 if (imgSize == 0) {
94 return false;
95 }
96 dst->image() = SkMaskBuilder::AllocImage(imgSize);
97
98 const uint8_t* srcP = src.fImage;
99 uint8_t* dstP = dst->image();
100 const uint8_t* table = fTable;
101 int dstWidth = dst->fBounds.width();
102 int extraZeros = dst->fRowBytes - dstWidth;
103
104 for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
105 for (int x = dstWidth - 1; x >= 0; --x) {
106 dstP[x] = table[srcP[x]];
107 }
108 srcP += src.fRowBytes;
109 // we can't just inc dstP by rowbytes, because if it has any
110 // padding between its width and its rowbytes, we need to zero those
111 // so that the bitters can read those safely if that is faster for
112 // them
113 dstP += dstWidth;
114 for (int i = extraZeros - 1; i >= 0; --i) {
115 *dstP++ = 0;
116 }
117 }
118 }
119
120 if (margin) {
121 margin->set(0, 0);
122 }
123 return true;
124 }
125
126 SkMask::Format SkTableMaskFilterImpl::getFormat() const {
127 return SkMask::kA8_Format;
128 }
129
130 void SkTableMaskFilterImpl::flatten(SkWriteBuffer& wb) const {
131 wb.writeByteArray(fTable, 256);
132 }
133
134 sk_sp<SkFlattenable> SkTableMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
135 uint8_t table[256];
136 if (!buffer.readByteArray(table, 256)) {
137 return nullptr;
138 }
139 return sk_sp<SkFlattenable>(SkTableMaskFilter::Create(table));
140 }
141
142 std::pair<sk_sp<SkImageFilter>, bool> SkTableMaskFilterImpl::asImageFilter(const SkMatrix&,
143 const SkPaint&) const {
144 sk_sp<SkColorFilter> colorFilter = SkColorFilters::TableARGB(fTable,
145 nullptr,
146 nullptr,
147 nullptr);
148 return std::make_pair(SkImageFilters::ColorFilter(colorFilter, nullptr), false);
149 }
150 ///////////////////////////////////////////////////////////////////////////////
151
152 SkMaskFilter* SkTableMaskFilter::Create(const uint8_t table[256]) {
153 return new SkTableMaskFilterImpl(table);
154 }
155
156 SkMaskFilter* SkTableMaskFilter::CreateGamma(SkScalar gamma) {
157 uint8_t table[256];
158 MakeGammaTable(table, gamma);
159 return new SkTableMaskFilterImpl(table);
160 }
161
162 SkMaskFilter* SkTableMaskFilter::CreateClip(uint8_t min, uint8_t max) {
163 uint8_t table[256];
164 MakeClipTable(table, min, max);
165 return new SkTableMaskFilterImpl(table);
166 }
167
168 void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
169 const float dx = 1 / 255.0f;
170 const float g = gamma;
171
172 float x = 0;
173 for (int i = 0; i < 256; i++) {
174 // float ee = powf(x, g) * 255;
175 table[i] = SkTPin(sk_float_round2int(powf(x, g) * 255), 0, 255);
176 x += dx;
177 }
178 }
179
180 void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
181 uint8_t max) {
182 if (0 == max) {
183 max = 1;
184 }
185 if (min >= max) {
186 min = max - 1;
187 }
188 SkASSERT(min < max);
189
190 SkFixed scale = (1 << 16) * 255 / (max - min);
191 memset(table, 0, min + 1);
192 for (int i = min + 1; i < max; i++) {
193 int value = SkFixedRoundToInt(scale * (i - min));
194 SkASSERT(value <= 255);
195 table[i] = value;
196 }
197 memset(table + max, 255, 256 - max);
198
199 #if 0
200 int j;
201 for (j = 0; j < 256; j++) {
202 if (table[j]) {
203 break;
204 }
205 }
206 SkDebugf("%d %d start [%d]", min, max, j);
207 for (; j < 256; j++) {
208 SkDebugf(" %d", table[j]);
209 }
210 SkDebugf("\n\n");
211 #endif
212 }
213
214 void SkTableMaskFilter::RegisterFlattenables() {
215 SK_REGISTER_FLATTENABLE(SkTableMaskFilterImpl);
216 // Previous name
217 SkFlattenable::Register("SkTableMF", SkTableMaskFilterImpl::CreateProc);
218 }