epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 3f2025f | 2009-10-29 17:37:56 +0000 | [diff] [blame] | 10 | #include "SkTableMaskFilter.h" |
| 11 | |
| 12 | SkTableMaskFilter::SkTableMaskFilter() { |
| 13 | for (int i = 0; i < 256; i++) { |
| 14 | fTable[i] = i; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | SkTableMaskFilter::SkTableMaskFilter(const uint8_t table[256]) { |
| 19 | this->setTable(table); |
| 20 | } |
| 21 | |
| 22 | SkTableMaskFilter::~SkTableMaskFilter() {} |
| 23 | |
| 24 | void SkTableMaskFilter::setTable(const uint8_t table[256]) { |
| 25 | memcpy(fTable, table, 256); |
| 26 | } |
| 27 | |
| 28 | bool 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 | |
| 70 | SkMask::Format SkTableMaskFilter::getFormat() { |
| 71 | return SkMask::kA8_Format; |
| 72 | } |
| 73 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame^] | 74 | void SkTableMaskFilter::flatten(SkFlattenableWriteBuffer& wb) const { |
reed@android.com | 3f2025f | 2009-10-29 17:37:56 +0000 | [diff] [blame] | 75 | this->INHERITED::flatten(wb); |
| 76 | wb.writePad(fTable, 256); |
| 77 | } |
| 78 | |
| 79 | SkTableMaskFilter::SkTableMaskFilter(SkFlattenableReadBuffer& rb) |
| 80 | : INHERITED(rb) { |
| 81 | rb.read(fTable, 256); |
| 82 | } |
| 83 | |
reed@android.com | 3f2025f | 2009-10-29 17:37:56 +0000 | [diff] [blame] | 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) { |
reed@android.com | 3f2025f | 2009-10-29 17:37:56 +0000 | [diff] [blame] | 87 | const float dx = 1 / 255.0f; |
reed@google.com | 48b481b | 2012-02-15 19:09:54 +0000 | [diff] [blame] | 88 | const float g = SkScalarToFloat(gamma); |
| 89 | |
| 90 | float x = 0; |
reed@android.com | 3f2025f | 2009-10-29 17:37:56 +0000 | [diff] [blame] | 91 | for (int i = 0; i < 256; i++) { |
reed@google.com | 48b481b | 2012-02-15 19:09:54 +0000 | [diff] [blame] | 92 | float ee = powf(x, g) * 255; |
| 93 | table[i] = SkPin32(sk_float_round2int(powf(x, g) * 255), 0, 255); |
reed@android.com | 3f2025f | 2009-10-29 17:37:56 +0000 | [diff] [blame] | 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 = 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 | |