3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkTableMaskFilter.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
15 SkTableMaskFilter::SkTableMaskFilter() {
16 for (int i = 0; i < 256; i++) {
21 SkTableMaskFilter::SkTableMaskFilter(const uint8_t table[256]) {
22 memcpy(fTable, table, sizeof(fTable));
25 SkTableMaskFilter::~SkTableMaskFilter() {}
27 bool SkTableMaskFilter::filterMask(SkMask* dst, const SkMask& src,
28 const SkMatrix&, SkIPoint* margin) const {
29 if (src.fFormat != SkMask::kA8_Format) {
33 dst->fBounds = src.fBounds;
34 dst->fRowBytes = SkAlign4(dst->fBounds.width());
35 dst->fFormat = SkMask::kA8_Format;
39 dst->fImage = SkMask::AllocImage(dst->computeImageSize());
41 const uint8_t* srcP = src.fImage;
42 uint8_t* dstP = dst->fImage;
43 const uint8_t* table = fTable;
44 int dstWidth = dst->fBounds.width();
45 int extraZeros = dst->fRowBytes - dstWidth;
47 for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
48 for (int x = dstWidth - 1; x >= 0; --x) {
49 dstP[x] = table[srcP[x]];
51 srcP += src.fRowBytes;
52 // we can't just inc dstP by rowbytes, because if it has any
53 // padding between its width and its rowbytes, we need to zero those
54 // so that the bitters can read those safely if that is faster for
57 for (int i = extraZeros - 1; i >= 0; --i) {
69 SkMask::Format SkTableMaskFilter::getFormat() const {
70 return SkMask::kA8_Format;
73 void SkTableMaskFilter::flatten(SkWriteBuffer& wb) const {
74 this->INHERITED::flatten(wb);
75 wb.writeByteArray(fTable, 256);
78 SkTableMaskFilter::SkTableMaskFilter(SkReadBuffer& rb)
80 SkASSERT(256 == rb.getArrayCount());
81 rb.readByteArray(fTable, 256);
84 ///////////////////////////////////////////////////////////////////////////////
86 void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
87 const float dx = 1 / 255.0f;
88 const float g = SkScalarToFloat(gamma);
91 for (int i = 0; i < 256; i++) {
92 // float ee = powf(x, g) * 255;
93 table[i] = SkPin32(sk_float_round2int(powf(x, g) * 255), 0, 255);
98 void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
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 = SkFixedRoundToInt(scale * (i - min));
112 SkASSERT(value <= 255);
115 memset(table + max, 255, 256 - max);
119 for (j = 0; j < 256; j++) {
124 SkDebugf("%d %d start [%d]", min, max, j);
125 for (; j < 256; j++) {
126 SkDebugf(" %d", table[j]);
133 void SkTableMaskFilter::toString(SkString* str) const {
134 str->append("SkTableMaskFilter: (");
136 str->append("table: ");
137 for (int i = 0; i < 255; ++i) {
138 str->appendf("%d, ", fTable[i]);
140 str->appendf("%d", fTable[255]);