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