blob: f01451c4fe07db125bd0673160e76823dcefd98b [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"
10
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000011SkMask::Format SkKernel33ProcMaskFilter::getFormat() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000012 return SkMask::kA8_Format;
13}
14
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000015bool SkKernel33ProcMaskFilter::filterMask(SkMask* dst, const SkMask& src,
16 const SkMatrix&, SkIPoint* margin) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 // margin???
18 dst->fImage = NULL;
19 dst->fBounds = src.fBounds;
20 dst->fBounds.inset(-1, -1);
21 dst->fFormat = SkMask::kA8_Format;
22
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000023 if (NULL == src.fImage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 return true;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000025 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
27 dst->fRowBytes = dst->fBounds.width();
28 size_t size = dst->computeImageSize();
reed@android.com543ed932009-04-24 12:43:40 +000029 if (0 == size) {
30 return false; // too big to allocate, abort
31 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 dst->fImage = SkMask::AllocImage(size);
33
34 const int h = src.fBounds.height();
35 const int w = src.fBounds.width();
36 const int srcRB = src.fRowBytes;
37 const uint8_t* srcImage = src.fImage;
38 uint8_t* dstImage = dst->fImage;
39
40 uint8_t* srcRows[3];
41 uint8_t storage[3][3];
42
43 srcRows[0] = storage[0];
44 srcRows[1] = storage[1];
45 srcRows[2] = storage[2];
46
47 unsigned scale = fPercent256;
48
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000049 for (int y = -1; y <= h; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 uint8_t* dstRow = dstImage;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000051 for (int x = -1; x <= w; x++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 memset(storage, 0, sizeof(storage));
53 uint8_t* storagePtr = &storage[0][0];
54
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000055 for (int ky = y - 1; ky <= y + 1; ky++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 const uint8_t* srcRow = srcImage + ky * srcRB; // may be out-of-range
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000057 for (int kx = x - 1; kx <= x + 1; kx++) {
58 if ((unsigned)ky < (unsigned)h && (unsigned)kx < (unsigned)w) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 *storagePtr = srcRow[kx];
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000060 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 storagePtr++;
62 }
63 }
64 int value = this->computeValue(srcRows);
65
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000066 if (scale < 256) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 value = SkAlphaBlend(value, srcRows[1][1], scale);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000068 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 *dstRow++ = SkToU8(value);
70 }
71 dstImage += dst->fRowBytes;
72 }
73 return true;
74}
75
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000076void SkKernel33ProcMaskFilter::flatten(SkFlattenableWriteBuffer& wb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 this->INHERITED::flatten(wb);
78 wb.write32(fPercent256);
79}
80
81SkKernel33ProcMaskFilter::SkKernel33ProcMaskFilter(SkFlattenableReadBuffer& rb)
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000082 : SkMaskFilter(rb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 fPercent256 = rb.readS32();
84}
85
86///////////////////////////////////////////////////////////////////////////////
87
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000088uint8_t SkKernel33MaskFilter::computeValue(uint8_t* const* srcRows) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 int value = 0;
90
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000091 for (int i = 0; i < 3; i++) {
92 for (int j = 0; j < 3; j++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 value += fKernel[i][j] * srcRows[i][j];
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000094 }
95 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000096
97 value >>= fShift;
98
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000099 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 value = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000101 } else if (value > 255) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 value = 255;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 return (uint8_t)value;
105}
106
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000107void SkKernel33MaskFilter::flatten(SkFlattenableWriteBuffer& wb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 this->INHERITED::flatten(wb);
109 wb.writeMul4(fKernel, 9 * sizeof(int));
110 wb.write32(fShift);
111}
112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113SkKernel33MaskFilter::SkKernel33MaskFilter(SkFlattenableReadBuffer& rb)
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000114 : SkKernel33ProcMaskFilter(rb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 rb.read(fKernel, 9 * sizeof(int));
116 fShift = rb.readS32();
117}
118