epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkEmbossMaskFilter.h" |
| 11 | #include "SkBlurMaskFilter.h" |
| 12 | #include "SkBlurMask.h" |
| 13 | #include "SkEmbossMask.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
| 15 | #include "SkWriteBuffer.h" |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 16 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 18 | static inline int pin2byte(int n) { |
| 19 | if (n < 0) { |
| 20 | n = 0; |
| 21 | } else if (n > 0xFF) { |
| 22 | n = 0xFF; |
| 23 | } |
| 24 | return n; |
| 25 | } |
| 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], |
| 28 | SkScalar ambient, SkScalar specular, |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 29 | SkScalar blurRadius) { |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 30 | return SkBlurMaskFilter::CreateEmboss(SkBlurMask::ConvertRadiusToSigma(blurRadius), |
| 31 | direction, ambient, specular); |
| 32 | } |
| 33 | |
| 34 | SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar direction[3], |
| 35 | SkScalar ambient, SkScalar specular) { |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 36 | if (direction == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | return NULL; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 38 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | |
| 40 | // ambient should be 0...1 as a scalar |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 41 | int am = pin2byte(SkScalarToFixed(ambient) >> 8); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | |
| 43 | // specular should be 0..15.99 as a scalar |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 44 | int sp = pin2byte(SkScalarToFixed(specular) >> 12); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | |
| 46 | SkEmbossMaskFilter::Light light; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | memcpy(light.fDirection, direction, sizeof(light.fDirection)); |
| 49 | light.fAmbient = SkToU8(am); |
| 50 | light.fSpecular = SkToU8(sp); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 7c9d0f3 | 2014-02-21 10:13:32 +0000 | [diff] [blame^] | 52 | return SkEmbossMaskFilter::Create(blurSigma, light); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | } |
| 54 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 55 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 57 | static void normalize(SkScalar v[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]); |
| 59 | mag = SkScalarSqrt(mag); |
| 60 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 61 | for (int i = 0; i < 3; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | v[i] = SkScalarDiv(v[i], mag); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 63 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 66 | SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light) |
robertphillips@google.com | b75233b | 2013-08-27 16:31:31 +0000 | [diff] [blame] | 67 | : fLight(light), fBlurSigma(blurSigma) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | normalize(fLight.fDirection); |
| 69 | } |
| 70 | |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 71 | SkMask::Format SkEmbossMaskFilter::getFormat() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | return SkMask::k3D_Format; |
| 73 | } |
| 74 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 75 | bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 76 | const SkMatrix& matrix, SkIPoint* margin) const { |
| 77 | SkScalar sigma = matrix.mapRadius(fBlurSigma); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 79 | if (!SkBlurMask::BoxBlur(dst, src, sigma, SkBlurMask::kInner_Style, |
| 80 | SkBlurMask::kLow_Quality)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 82 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | |
| 84 | dst->fFormat = SkMask::k3D_Format; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 85 | if (margin) { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 86 | margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma)); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 87 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 89 | if (src.fImage == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | return true; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 91 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | |
| 93 | // create a larger buffer for the other two channels (should force fBlur to do this for us) |
| 94 | |
| 95 | { |
| 96 | uint8_t* alphaPlane = dst->fImage; |
| 97 | size_t planeSize = dst->computeImageSize(); |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 98 | if (0 == planeSize) { |
| 99 | return false; // too big to allocate, abort |
| 100 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | dst->fImage = SkMask::AllocImage(planeSize * 3); |
| 102 | memcpy(dst->fImage, alphaPlane, planeSize); |
| 103 | SkMask::FreeImage(alphaPlane); |
| 104 | } |
| 105 | |
| 106 | // run the light direction through the matrix... |
| 107 | Light light = fLight; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 108 | matrix.mapVectors((SkVector*)(void*)light.fDirection, |
| 109 | (SkVector*)(void*)fLight.fDirection, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | |
| 111 | // now restore the length of the XY component |
| 112 | // cast to SkVector so we can call setLength (this double cast silences alias warnings) |
| 113 | SkVector* vec = (SkVector*)(void*)light.fDirection; |
| 114 | vec->setLength(light.fDirection[0], |
| 115 | light.fDirection[1], |
| 116 | SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1])); |
| 117 | |
| 118 | SkEmbossMask::Emboss(dst, light); |
| 119 | |
| 120 | // restore original alpha |
| 121 | memcpy(dst->fImage, src.fImage, src.computeImageSize()); |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 126 | SkEmbossMaskFilter::SkEmbossMaskFilter(SkReadBuffer& buffer) |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 127 | : SkMaskFilter(buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 128 | SkASSERT(buffer.getArrayCount() == sizeof(Light)); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 129 | buffer.readByteArray(&fLight, sizeof(Light)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean |
commit-bot@chromium.org | fed2ab6 | 2014-01-23 15:16:05 +0000 | [diff] [blame] | 131 | fBlurSigma = buffer.readScalar(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |
| 133 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 134 | void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | this->INHERITED::flatten(buffer); |
| 136 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 137 | Light tmpLight = fLight; |
| 138 | tmpLight.fPad = 0; // for the font-cache lookup to be clean |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 139 | buffer.writeByteArray(&tmpLight, sizeof(tmpLight)); |
robertphillips@google.com | 11e0555 | 2013-12-03 19:46:58 +0000 | [diff] [blame] | 140 | buffer.writeScalar(fBlurSigma); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 142 | |
| 143 | #ifdef SK_DEVELOPER |
| 144 | void SkEmbossMaskFilter::toString(SkString* str) const { |
| 145 | str->append("SkEmbossMaskFilter: ("); |
| 146 | |
| 147 | str->append("direction: ("); |
| 148 | str->appendScalar(fLight.fDirection[0]); |
| 149 | str->append(", "); |
| 150 | str->appendScalar(fLight.fDirection[1]); |
| 151 | str->append(", "); |
| 152 | str->appendScalar(fLight.fDirection[2]); |
| 153 | str->append(") "); |
| 154 | |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 155 | str->appendf("ambient: %d specular: %d ", |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 156 | fLight.fAmbient, fLight.fSpecular); |
| 157 | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 158 | str->append("blurSigma: "); |
| 159 | str->appendScalar(fBlurSigma); |
robertphillips@google.com | 0bd80fa | 2013-03-18 17:53:38 +0000 | [diff] [blame] | 160 | str->append(")"); |
| 161 | } |
| 162 | #endif |