Debian Codegraph @

src/effects/SkTableMaskFilter.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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
82 dst->bounds() = src.fBounds;
83 dst->rowBytes() = SkAlign4(dst->fBounds.width());
84 dst->format() = SkMask::kA8_Format;
85 dst->image() = nullptr;
86
87 if (src.fImage) {
88 dst->image() = SkMaskBuilder::AllocImage(dst->computeImageSize());
89
90 const uint8_t* srcP = src.fImage;
91 uint8_t* dstP = dst->image();
92 const uint8_t* table = fTable;
93 int dstWidth = dst->fBounds.width();
94 int extraZeros = dst->fRowBytes - dstWidth;
95
96 for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
97 for (int x = dstWidth - 1; x >= 0; --x) {
98 dstP[x] = table[srcP[x]];
99 }
100 srcP += src.fRowBytes;
101 // we can't just inc dstP by rowbytes, because if it has any
102 // padding between its width and its rowbytes, we need to zero those
103 // so that the bitters can read those safely if that is faster for
104 // them
105 dstP += dstWidth;
106 for (int i = extraZeros - 1; i >= 0; --i) {
107 *dstP++ = 0;
108 }
109 }
110 }
111
112 if (margin) {
113 margin->set(0, 0);
114 }
115 return true;
116 }
117
118 SkMask::Format SkTableMaskFilterImpl::getFormat() const {
119 return SkMask::kA8_Format;
120 }
121
122 void SkTableMaskFilterImpl::flatten(SkWriteBuffer& wb) const {
123 wb.writeByteArray(fTable, 256);
124 }
125
126 sk_sp<SkFlattenable> SkTableMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
127 uint8_t table[256];
128 if (!buffer.readByteArray(table, 256)) {
129 return nullptr;
130 }
131 return sk_sp<SkFlattenable>(SkTableMaskFilter::Create(table));
132 }
133
134 std::pair<sk_sp<SkImageFilter>, bool> SkTableMaskFilterImpl::asImageFilter(const SkMatrix&,
135 const SkPaint&) const {
136 sk_sp<SkColorFilter> colorFilter = SkColorFilters::TableARGB(fTable,
137 nullptr,
138 nullptr,
139 nullptr);
140 return std::make_pair(SkImageFilters::ColorFilter(colorFilter, nullptr), false);
141 }
142 ///////////////////////////////////////////////////////////////////////////////
143
144 SkMaskFilter* SkTableMaskFilter::Create(const uint8_t table[256]) {
145 return new SkTableMaskFilterImpl(table);
146 }
147
148 SkMaskFilter* SkTableMaskFilter::CreateGamma(SkScalar gamma) {
149 uint8_t table[256];
150 MakeGammaTable(table, gamma);
151 return new SkTableMaskFilterImpl(table);
152 }
153
154 SkMaskFilter* SkTableMaskFilter::CreateClip(uint8_t min, uint8_t max) {
155 uint8_t table[256];
156 MakeClipTable(table, min, max);
157 return new SkTableMaskFilterImpl(table);
158 }
159
160 void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
161 const float dx = 1 / 255.0f;
162 const float g = gamma;
163
164 float x = 0;
165 for (int i = 0; i < 256; i++) {
166 // float ee = powf(x, g) * 255;
167 table[i] = SkTPin(sk_float_round2int(powf(x, g) * 255), 0, 255);
168 x += dx;
169 }
170 }
171
172 void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
173 uint8_t max) {
174 if (0 == max) {
175 max = 1;
176 }
177 if (min >= max) {
178 min = max - 1;
179 }
180 SkASSERT(min < max);
181
182 SkFixed scale = (1 << 16) * 255 / (max - min);
183 memset(table, 0, min + 1);
184 for (int i = min + 1; i < max; i++) {
185 int value = SkFixedRoundToInt(scale * (i - min));
186 SkASSERT(value <= 255);
187 table[i] = value;
188 }
189 memset(table + max, 255, 256 - max);
190
191 #if 0
192 int j;
193 for (j = 0; j < 256; j++) {
194 if (table[j]) {
195 break;
196 }
197 }
198 SkDebugf("%d %d start [%d]", min, max, j);
199 for (; j < 256; j++) {
200 SkDebugf(" %d", table[j]);
201 }
202 SkDebugf("\n\n");
203 #endif
204 }
205
206 void SkTableMaskFilter::RegisterFlattenables() {
207 SK_REGISTER_FLATTENABLE(SkTableMaskFilterImpl);
208 // Previous name
209 SkFlattenable::Register("SkTableMF", SkTableMaskFilterImpl::CreateProc);
210 }