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