blob: 5842e4bf4b86e0e13e9ace7be85f9a46b192de8f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com3f2025f2009-10-29 17:37:56 +000010#include "SkTableMaskFilter.h"
11
12SkTableMaskFilter::SkTableMaskFilter() {
13 for (int i = 0; i < 256; i++) {
14 fTable[i] = i;
15 }
16}
17
18SkTableMaskFilter::SkTableMaskFilter(const uint8_t table[256]) {
19 this->setTable(table);
20}
21
22SkTableMaskFilter::~SkTableMaskFilter() {}
23
24void SkTableMaskFilter::setTable(const uint8_t table[256]) {
25 memcpy(fTable, table, 256);
26}
27
28bool SkTableMaskFilter::filterMask(SkMask* dst, const SkMask& src,
29 const SkMatrix&, SkIPoint* margin) {
30 if (src.fFormat != SkMask::kA8_Format) {
31 return false;
32 }
33
34 dst->fBounds = src.fBounds;
35 dst->fRowBytes = SkAlign4(dst->fBounds.width());
36 dst->fFormat = SkMask::kA8_Format;
37 dst->fImage = NULL;
38
39 if (src.fImage) {
40 dst->fImage = SkMask::AllocImage(dst->computeImageSize());
41
42 const uint8_t* srcP = src.fImage;
43 uint8_t* dstP = dst->fImage;
44 const uint8_t* table = fTable;
45 int dstWidth = dst->fBounds.width();
46 int extraZeros = dst->fRowBytes - dstWidth;
47
48 for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
49 for (int x = dstWidth - 1; x >= 0; --x) {
50 dstP[x] = table[srcP[x]];
51 }
52 srcP += src.fRowBytes;
53 // we can't just inc dstP by rowbytes, because if it has any
54 // padding between its width and its rowbytes, we need to zero those
55 // so that the bitters can read those safely if that is faster for
56 // them
57 dstP += dstWidth;
58 for (int i = extraZeros - 1; i >= 0; --i) {
59 *dstP++ = 0;
60 }
61 }
62 }
63
64 if (margin) {
65 margin->set(0, 0);
66 }
67 return true;
68}
69
70SkMask::Format SkTableMaskFilter::getFormat() {
71 return SkMask::kA8_Format;
72}
73
74void SkTableMaskFilter::flatten(SkFlattenableWriteBuffer& wb) {
75 this->INHERITED::flatten(wb);
76 wb.writePad(fTable, 256);
77}
78
79SkTableMaskFilter::SkTableMaskFilter(SkFlattenableReadBuffer& rb)
80 : INHERITED(rb) {
81 rb.read(fTable, 256);
82}
83
84SkFlattenable* SkTableMaskFilter::Factory(SkFlattenableReadBuffer& rb) {
85 return SkNEW_ARGS(SkTableMaskFilter, (rb));
86}
87
88SkFlattenable::Factory SkTableMaskFilter::getFactory() {
89 return SkTableMaskFilter::Factory;
90}
91
92///////////////////////////////////////////////////////////////////////////////
93
94void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
95 float x = 0;
96 const float dx = 1 / 255.0f;
97 for (int i = 0; i < 256; i++) {
98 table[i] = SkPin32(SkScalarRound(powf(x, gamma) * 255), 0, 255);
99 x += dx;
100 }
101}
102
103void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
104 uint8_t max) {
105 if (0 == max) {
106 max = 1;
107 }
108 if (min >= max) {
109 min = max - 1;
110 }
111 SkASSERT(min < max);
112
113 SkFixed scale = (1 << 16) * 255 / (max - min);
114 memset(table, 0, min + 1);
115 for (int i = min + 1; i < max; i++) {
116 int value = SkFixedRound(scale * (i - min));
117 SkASSERT(value <= 255);
118 table[i] = value;
119 }
120 memset(table + max, 255, 256 - max);
121
122#if 0
123 int j;
124 for (j = 0; j < 256; j++) {
125 if (table[j]) {
126 break;
127 }
128 }
129 SkDebugf("%d %d start [%d]", min, max, j);
130 for (; j < 256; j++) {
131 SkDebugf(" %d", table[j]);
132 }
133 SkDebugf("\n\n");
134#endif
135}
136