blob: 438e81b031924bf4b89f1fa33cee574bf3f1cb3c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkKernel33MaskFilter.h"
9#include "SkColorPriv.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000012#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
reed@google.com30711b72012-12-18 19:18:39 +000014SkMask::Format SkKernel33ProcMaskFilter::getFormat() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 return SkMask::kA8_Format;
16}
17
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000018bool SkKernel33ProcMaskFilter::filterMask(SkMask* dst, const SkMask& src,
reed@google.com30711b72012-12-18 19:18:39 +000019 const SkMatrix&, SkIPoint* margin) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 // margin???
21 dst->fImage = NULL;
22 dst->fBounds = src.fBounds;
23 dst->fBounds.inset(-1, -1);
24 dst->fFormat = SkMask::kA8_Format;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000025
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000026 if (NULL == src.fImage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 return true;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000028 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 dst->fRowBytes = dst->fBounds.width();
31 size_t size = dst->computeImageSize();
reed@android.com543ed932009-04-24 12:43:40 +000032 if (0 == size) {
33 return false; // too big to allocate, abort
34 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 dst->fImage = SkMask::AllocImage(size);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 const int h = src.fBounds.height();
38 const int w = src.fBounds.width();
39 const int srcRB = src.fRowBytes;
40 const uint8_t* srcImage = src.fImage;
41 uint8_t* dstImage = dst->fImage;
42
43 uint8_t* srcRows[3];
44 uint8_t storage[3][3];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 srcRows[0] = storage[0];
47 srcRows[1] = storage[1];
48 srcRows[2] = storage[2];
49
50 unsigned scale = fPercent256;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000052 for (int y = -1; y <= h; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 uint8_t* dstRow = dstImage;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000054 for (int x = -1; x <= w; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 memset(storage, 0, sizeof(storage));
56 uint8_t* storagePtr = &storage[0][0];
57
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000058 for (int ky = y - 1; ky <= y + 1; ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 const uint8_t* srcRow = srcImage + ky * srcRB; // may be out-of-range
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000060 for (int kx = x - 1; kx <= x + 1; kx++) {
61 if ((unsigned)ky < (unsigned)h && (unsigned)kx < (unsigned)w) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 *storagePtr = srcRow[kx];
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 storagePtr++;
65 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 int value = this->computeValue(srcRows);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000069 if (scale < 256) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 value = SkAlphaBlend(value, srcRows[1][1], scale);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000071 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 *dstRow++ = SkToU8(value);
73 }
74 dstImage += dst->fRowBytes;
75 }
76 return true;
77}
78
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000079void SkKernel33ProcMaskFilter::flatten(SkWriteBuffer& wb) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 this->INHERITED::flatten(wb);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000081 wb.writeInt(fPercent256);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082}
83
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000084SkKernel33ProcMaskFilter::SkKernel33ProcMaskFilter(SkReadBuffer& rb)
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000085 : SkMaskFilter(rb) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000086 fPercent256 = rb.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +000087}
88
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000089#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000090void SkKernel33ProcMaskFilter::toString(SkString* str) const {
91 str->appendf("percent256: %d, ", fPercent256);
92}
93#endif
94
reed@android.com8a1c16f2008-12-17 15:59:43 +000095///////////////////////////////////////////////////////////////////////////////
96
reed@google.com30711b72012-12-18 19:18:39 +000097uint8_t SkKernel33MaskFilter::computeValue(uint8_t* const* srcRows) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 int value = 0;
99
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000100 for (int i = 0; i < 3; i++) {
101 for (int j = 0; j < 3; j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 value += fKernel[i][j] * srcRows[i][j];
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000103 }
104 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 value >>= fShift;
107
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000108 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 value = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000110 } else if (value > 255) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 value = 255;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000112 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 return (uint8_t)value;
114}
115
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000116void SkKernel33MaskFilter::flatten(SkWriteBuffer& wb) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 this->INHERITED::flatten(wb);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000118 wb.writeIntArray(&fKernel[0][0], 9);
119 wb.writeInt(fShift);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120}
121
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000122SkKernel33MaskFilter::SkKernel33MaskFilter(SkReadBuffer& rb)
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000123 : SkKernel33ProcMaskFilter(rb) {
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000124 SkDEBUGCODE(bool success = )rb.readIntArray(&fKernel[0][0], 9);
125 SkASSERT(success);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000126 fShift = rb.readInt();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127}
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000128
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000129#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000130void SkKernel33MaskFilter::toString(SkString* str) const {
131 str->append("SkKernel33MaskFilter: (");
132
133 str->appendf("kernel: (%d, %d, %d, %d, %d, %d, %d, %d, %d), ",
134 fKernel[0][0], fKernel[0][1], fKernel[0][2],
135 fKernel[1][0], fKernel[1][1], fKernel[1][2],
136 fKernel[2][0], fKernel[2][1], fKernel[2][2]);
137 str->appendf("shift: %d, ", fShift);
138
139 this->INHERITED::toString(str);
140
141 str->append(")");
142}
143#endif