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/SkFlattenable.h"
11 #include "include/core/SkMaskFilter.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypes.h"
17 #include "include/private/base/SkFixed.h"
18 #include "include/private/base/SkAlign.h"
19 #include "include/private/base/SkFloatingPoint.h"
20 #include "include/private/base/SkTPin.h"
21 #include "src/core/SkMask.h"
22 #include "src/core/SkMaskFilterBase.h"
23 #include "src/core/SkReadBuffer.h"
24 #include "src/core/SkWriteBuffer.h"
25
26 #include <cmath>
27 #include <cstdint>
28 #include <cstring>
29
30 class SkMatrix;
31
32 class SkTableMaskFilterImpl : public SkMaskFilterBase {
33 public:
34 explicit SkTableMaskFilterImpl(const uint8_t table[256]);
35
36 SkMask::Format getFormat() const override;
37 bool filterMask(SkMask*, const SkMask&, const SkMatrix&, SkIPoint*) const override;
38
39 protected:
40 ~SkTableMaskFilterImpl() override;
41
42 void flatten(SkWriteBuffer&) const override;
43
44 private:
45 SK_FLATTENABLE_HOOKS(SkTableMaskFilterImpl)
46
47 SkTableMaskFilterImpl();
48
49 uint8_t fTable[256];
50
51 using INHERITED = SkMaskFilter;
52 };
53
SkTableMaskFilterImpl()54 SkTableMaskFilterImpl::SkTableMaskFilterImpl() {
55 for (int i = 0; i < 256; i++) {
56 fTable[i] = i;
57 }
58 }
59
SkTableMaskFilterImpl(const uint8_t table[256])60 SkTableMaskFilterImpl::SkTableMaskFilterImpl(const uint8_t table[256]) {
61 memcpy(fTable, table, sizeof(fTable));
62 }
63
~SkTableMaskFilterImpl()64 SkTableMaskFilterImpl::~SkTableMaskFilterImpl() {}
65
filterMask(SkMask * dst,const SkMask & src,const SkMatrix &,SkIPoint * margin) const66 bool SkTableMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src,
67 const SkMatrix&, SkIPoint* margin) const {
68 if (src.fFormat != SkMask::kA8_Format) {
69 return false;
70 }
71
72 dst->fBounds = src.fBounds;
73 dst->fRowBytes = SkAlign4(dst->fBounds.width());
74 dst->fFormat = SkMask::kA8_Format;
75 dst->fImage = nullptr;
76
77 if (src.fImage) {
78 dst->fImage = SkMask::AllocImage(dst->computeImageSize());
79
80 const uint8_t* srcP = src.fImage;
81 uint8_t* dstP = dst->fImage;
82 const uint8_t* table = fTable;
83 int dstWidth = dst->fBounds.width();
84 int extraZeros = dst->fRowBytes - dstWidth;
85
86 for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
87 for (int x = dstWidth - 1; x >= 0; --x) {
88 dstP[x] = table[srcP[x]];
89 }
90 srcP += src.fRowBytes;
91 // we can't just inc dstP by rowbytes, because if it has any
92 // padding between its width and its rowbytes, we need to zero those
93 // so that the bitters can read those safely if that is faster for
94 // them
95 dstP += dstWidth;
96 for (int i = extraZeros - 1; i >= 0; --i) {
97 *dstP++ = 0;
98 }
99 }
100 }
101
102 if (margin) {
103 margin->set(0, 0);
104 }
105 return true;
106 }
107
getFormat() const108 SkMask::Format SkTableMaskFilterImpl::getFormat() const {
109 return SkMask::kA8_Format;
110 }
111
flatten(SkWriteBuffer & wb) const112 void SkTableMaskFilterImpl::flatten(SkWriteBuffer& wb) const {
113 wb.writeByteArray(fTable, 256);
114 }
115
CreateProc(SkReadBuffer & buffer)116 sk_sp<SkFlattenable> SkTableMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
117 uint8_t table[256];
118 if (!buffer.readByteArray(table, 256)) {
119 return nullptr;
120 }
121 return sk_sp<SkFlattenable>(SkTableMaskFilter::Create(table));
122 }
123
124 ///////////////////////////////////////////////////////////////////////////////
125
Create(const uint8_t table[256])126 SkMaskFilter* SkTableMaskFilter::Create(const uint8_t table[256]) {
127 return new SkTableMaskFilterImpl(table);
128 }
129
CreateGamma(SkScalar gamma)130 SkMaskFilter* SkTableMaskFilter::CreateGamma(SkScalar gamma) {
131 uint8_t table[256];
132 MakeGammaTable(table, gamma);
133 return new SkTableMaskFilterImpl(table);
134 }
135
CreateClip(uint8_t min,uint8_t max)136 SkMaskFilter* SkTableMaskFilter::CreateClip(uint8_t min, uint8_t max) {
137 uint8_t table[256];
138 MakeClipTable(table, min, max);
139 return new SkTableMaskFilterImpl(table);
140 }
141
MakeGammaTable(uint8_t table[256],SkScalar gamma)142 void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
143 const float dx = 1 / 255.0f;
144 const float g = SkScalarToFloat(gamma);
145
146 float x = 0;
147 for (int i = 0; i < 256; i++) {
148 // float ee = powf(x, g) * 255;
149 table[i] = SkTPin(sk_float_round2int(powf(x, g) * 255), 0, 255);
150 x += dx;
151 }
152 }
153
MakeClipTable(uint8_t table[256],uint8_t min,uint8_t max)154 void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
155 uint8_t max) {
156 if (0 == max) {
157 max = 1;
158 }
159 if (min >= max) {
160 min = max - 1;
161 }
162 SkASSERT(min < max);
163
164 SkFixed scale = (1 << 16) * 255 / (max - min);
165 memset(table, 0, min + 1);
166 for (int i = min + 1; i < max; i++) {
167 int value = SkFixedRoundToInt(scale * (i - min));
168 SkASSERT(value <= 255);
169 table[i] = value;
170 }
171 memset(table + max, 255, 256 - max);
172
173 #if 0
174 int j;
175 for (j = 0; j < 256; j++) {
176 if (table[j]) {
177 break;
178 }
179 }
180 SkDebugf("%d %d start [%d]", min, max, j);
181 for (; j < 256; j++) {
182 SkDebugf(" %d", table[j]);
183 }
184 SkDebugf("\n\n");
185 #endif
186 }
187