blob: fed0c5da723af6742947a43970318c5395268697 [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
djsollen@google.com54924242012-03-29 15:18:04 +000074void SkTableMaskFilter::flatten(SkFlattenableWriteBuffer& wb) const {
reed@android.com3f2025f2009-10-29 17:37:56 +000075 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
reed@android.com3f2025f2009-10-29 17:37:56 +000084///////////////////////////////////////////////////////////////////////////////
85
86void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
reed@android.com3f2025f2009-10-29 17:37:56 +000087 const float dx = 1 / 255.0f;
reed@google.com48b481b2012-02-15 19:09:54 +000088 const float g = SkScalarToFloat(gamma);
89
90 float x = 0;
reed@android.com3f2025f2009-10-29 17:37:56 +000091 for (int i = 0; i < 256; i++) {
reed@google.com48b481b2012-02-15 19:09:54 +000092 float ee = powf(x, g) * 255;
93 table[i] = SkPin32(sk_float_round2int(powf(x, g) * 255), 0, 255);
reed@android.com3f2025f2009-10-29 17:37:56 +000094 x += dx;
95 }
96}
97
98void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
99 uint8_t max) {
100 if (0 == max) {
101 max = 1;
102 }
103 if (min >= max) {
104 min = max - 1;
105 }
106 SkASSERT(min < max);
107
108 SkFixed scale = (1 << 16) * 255 / (max - min);
109 memset(table, 0, min + 1);
110 for (int i = min + 1; i < max; i++) {
111 int value = SkFixedRound(scale * (i - min));
112 SkASSERT(value <= 255);
113 table[i] = value;
114 }
115 memset(table + max, 255, 256 - max);
116
117#if 0
118 int j;
119 for (j = 0; j < 256; j++) {
120 if (table[j]) {
121 break;
122 }
123 }
124 SkDebugf("%d %d start [%d]", min, max, j);
125 for (; j < 256; j++) {
126 SkDebugf(" %d", table[j]);
127 }
128 SkDebugf("\n\n");
129#endif
130}
131