Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / effects / SkTableMaskFilter.cpp
1
2 /*
3  * 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.
7  */
8
9
10 #include "SkTableMaskFilter.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkString.h"
14
15 SkTableMaskFilter::SkTableMaskFilter() {
16     for (int i = 0; i < 256; i++) {
17         fTable[i] = i;
18     }
19 }
20
21 SkTableMaskFilter::SkTableMaskFilter(const uint8_t table[256]) {
22     memcpy(fTable, table, sizeof(fTable));
23 }
24
25 SkTableMaskFilter::~SkTableMaskFilter() {}
26
27 bool SkTableMaskFilter::filterMask(SkMask* dst, const SkMask& src,
28                                  const SkMatrix&, SkIPoint* margin) const {
29     if (src.fFormat != SkMask::kA8_Format) {
30         return false;
31     }
32
33     dst->fBounds = src.fBounds;
34     dst->fRowBytes = SkAlign4(dst->fBounds.width());
35     dst->fFormat = SkMask::kA8_Format;
36     dst->fImage = NULL;
37
38     if (src.fImage) {
39         dst->fImage = SkMask::AllocImage(dst->computeImageSize());
40
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;
46
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]];
50             }
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
55             // them
56             dstP += dstWidth;
57             for (int i = extraZeros - 1; i >= 0; --i) {
58                 *dstP++ = 0;
59             }
60         }
61     }
62
63     if (margin) {
64         margin->set(0, 0);
65     }
66     return true;
67 }
68
69 SkMask::Format SkTableMaskFilter::getFormat() const {
70     return SkMask::kA8_Format;
71 }
72
73 void SkTableMaskFilter::flatten(SkWriteBuffer& wb) const {
74     this->INHERITED::flatten(wb);
75     wb.writeByteArray(fTable, 256);
76 }
77
78 SkTableMaskFilter::SkTableMaskFilter(SkReadBuffer& rb)
79         : INHERITED(rb) {
80     SkASSERT(256 == rb.getArrayCount());
81     rb.readByteArray(fTable, 256);
82 }
83
84 ///////////////////////////////////////////////////////////////////////////////
85
86 void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
87     const float dx = 1 / 255.0f;
88     const float g = SkScalarToFloat(gamma);
89
90     float x = 0;
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);
94         x += dx;
95     }
96 }
97
98 void 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 = SkFixedRoundToInt(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
132 #ifdef SK_DEVELOPER
133 void SkTableMaskFilter::toString(SkString* str) const {
134     str->append("SkTableMaskFilter: (");
135
136     str->append("table: ");
137     for (int i = 0; i < 255; ++i) {
138         str->appendf("%d, ", fTable[i]);
139     }
140     str->appendf("%d", fTable[255]);
141
142     str->append(")");
143 }
144 #endif