SkTableMaskFilter.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkString.h"
  8. #include "include/effects/SkTableMaskFilter.h"
  9. #include "include/private/SkFixed.h"
  10. #include "src/core/SkReadBuffer.h"
  11. #include "src/core/SkWriteBuffer.h"
  12. class SkTableMaskFilterImpl : public SkMaskFilterBase {
  13. public:
  14. explicit SkTableMaskFilterImpl(const uint8_t table[256]);
  15. SkMask::Format getFormat() const override;
  16. bool filterMask(SkMask*, const SkMask&, const SkMatrix&, SkIPoint*) const override;
  17. protected:
  18. ~SkTableMaskFilterImpl() override;
  19. void flatten(SkWriteBuffer&) const override;
  20. private:
  21. SK_FLATTENABLE_HOOKS(SkTableMaskFilterImpl)
  22. SkTableMaskFilterImpl();
  23. uint8_t fTable[256];
  24. typedef SkMaskFilter INHERITED;
  25. };
  26. SkTableMaskFilterImpl::SkTableMaskFilterImpl() {
  27. for (int i = 0; i < 256; i++) {
  28. fTable[i] = i;
  29. }
  30. }
  31. SkTableMaskFilterImpl::SkTableMaskFilterImpl(const uint8_t table[256]) {
  32. memcpy(fTable, table, sizeof(fTable));
  33. }
  34. SkTableMaskFilterImpl::~SkTableMaskFilterImpl() {}
  35. bool SkTableMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src,
  36. const SkMatrix&, SkIPoint* margin) const {
  37. if (src.fFormat != SkMask::kA8_Format) {
  38. return false;
  39. }
  40. dst->fBounds = src.fBounds;
  41. dst->fRowBytes = SkAlign4(dst->fBounds.width());
  42. dst->fFormat = SkMask::kA8_Format;
  43. dst->fImage = nullptr;
  44. if (src.fImage) {
  45. dst->fImage = SkMask::AllocImage(dst->computeImageSize());
  46. const uint8_t* srcP = src.fImage;
  47. uint8_t* dstP = dst->fImage;
  48. const uint8_t* table = fTable;
  49. int dstWidth = dst->fBounds.width();
  50. int extraZeros = dst->fRowBytes - dstWidth;
  51. for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
  52. for (int x = dstWidth - 1; x >= 0; --x) {
  53. dstP[x] = table[srcP[x]];
  54. }
  55. srcP += src.fRowBytes;
  56. // we can't just inc dstP by rowbytes, because if it has any
  57. // padding between its width and its rowbytes, we need to zero those
  58. // so that the bitters can read those safely if that is faster for
  59. // them
  60. dstP += dstWidth;
  61. for (int i = extraZeros - 1; i >= 0; --i) {
  62. *dstP++ = 0;
  63. }
  64. }
  65. }
  66. if (margin) {
  67. margin->set(0, 0);
  68. }
  69. return true;
  70. }
  71. SkMask::Format SkTableMaskFilterImpl::getFormat() const {
  72. return SkMask::kA8_Format;
  73. }
  74. void SkTableMaskFilterImpl::flatten(SkWriteBuffer& wb) const {
  75. wb.writeByteArray(fTable, 256);
  76. }
  77. sk_sp<SkFlattenable> SkTableMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
  78. uint8_t table[256];
  79. if (!buffer.readByteArray(table, 256)) {
  80. return nullptr;
  81. }
  82. return sk_sp<SkFlattenable>(SkTableMaskFilter::Create(table));
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. SkMaskFilter* SkTableMaskFilter::Create(const uint8_t table[256]) {
  86. return new SkTableMaskFilterImpl(table);
  87. }
  88. SkMaskFilter* SkTableMaskFilter::CreateGamma(SkScalar gamma) {
  89. uint8_t table[256];
  90. MakeGammaTable(table, gamma);
  91. return new SkTableMaskFilterImpl(table);
  92. }
  93. SkMaskFilter* SkTableMaskFilter::CreateClip(uint8_t min, uint8_t max) {
  94. uint8_t table[256];
  95. MakeClipTable(table, min, max);
  96. return new SkTableMaskFilterImpl(table);
  97. }
  98. void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
  99. const float dx = 1 / 255.0f;
  100. const float g = SkScalarToFloat(gamma);
  101. float x = 0;
  102. for (int i = 0; i < 256; i++) {
  103. // float ee = powf(x, g) * 255;
  104. table[i] = SkTPin(sk_float_round2int(powf(x, g) * 255), 0, 255);
  105. x += dx;
  106. }
  107. }
  108. void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
  109. uint8_t max) {
  110. if (0 == max) {
  111. max = 1;
  112. }
  113. if (min >= max) {
  114. min = max - 1;
  115. }
  116. SkASSERT(min < max);
  117. SkFixed scale = (1 << 16) * 255 / (max - min);
  118. memset(table, 0, min + 1);
  119. for (int i = min + 1; i < max; i++) {
  120. int value = SkFixedRoundToInt(scale * (i - min));
  121. SkASSERT(value <= 255);
  122. table[i] = value;
  123. }
  124. memset(table + max, 255, 256 - max);
  125. #if 0
  126. int j;
  127. for (j = 0; j < 256; j++) {
  128. if (table[j]) {
  129. break;
  130. }
  131. }
  132. SkDebugf("%d %d start [%d]", min, max, j);
  133. for (; j < 256; j++) {
  134. SkDebugf(" %d", table[j]);
  135. }
  136. SkDebugf("\n\n");
  137. #endif
  138. }