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" |
| 14 | #include "SkBuffer.h" |
| 15 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 16 | static inline int pin2byte(int n) { |
| 17 | if (n < 0) { |
| 18 | n = 0; |
| 19 | } else if (n > 0xFF) { |
| 20 | n = 0xFF; |
| 21 | } |
| 22 | return n; |
| 23 | } |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], |
| 26 | SkScalar ambient, SkScalar specular, |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 27 | SkScalar blurRadius) { |
| 28 | if (direction == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | return NULL; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 30 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | |
| 32 | // ambient should be 0...1 as a scalar |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 33 | int am = pin2byte(SkScalarToFixed(ambient) >> 8); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | |
| 35 | // specular should be 0..15.99 as a scalar |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 36 | int sp = pin2byte(SkScalarToFixed(specular) >> 12); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | |
| 38 | SkEmbossMaskFilter::Light light; |
| 39 | |
| 40 | memcpy(light.fDirection, direction, sizeof(light.fDirection)); |
| 41 | light.fAmbient = SkToU8(am); |
| 42 | light.fSpecular = SkToU8(sp); |
| 43 | |
| 44 | return SkNEW_ARGS(SkEmbossMaskFilter, (light, blurRadius)); |
| 45 | } |
| 46 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 47 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 49 | static void normalize(SkScalar v[3]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]); |
| 51 | mag = SkScalarSqrt(mag); |
| 52 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 53 | for (int i = 0; i < 3; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | v[i] = SkScalarDiv(v[i], mag); |
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 | } |
| 57 | |
| 58 | SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius) |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 59 | : fLight(light), fBlurRadius(blurRadius) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | normalize(fLight.fDirection); |
| 61 | } |
| 62 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 63 | SkMask::Format SkEmbossMaskFilter::getFormat() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | return SkMask::k3D_Format; |
| 65 | } |
| 66 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 67 | bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src, |
| 68 | const SkMatrix& matrix, SkIPoint* margin) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | SkScalar radius = matrix.mapRadius(fBlurRadius); |
| 70 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 71 | if (!SkBlurMask::Blur(dst, src, radius, SkBlurMask::kInner_Style, |
| 72 | SkBlurMask::kLow_Quality)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | return false; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 74 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | |
| 76 | dst->fFormat = SkMask::k3D_Format; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 77 | if (margin) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | margin->set(SkScalarCeil(radius), SkScalarCeil(radius)); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 79 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 81 | if (src.fImage == NULL) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | return true; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 83 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | |
| 85 | // create a larger buffer for the other two channels (should force fBlur to do this for us) |
| 86 | |
| 87 | { |
| 88 | uint8_t* alphaPlane = dst->fImage; |
| 89 | size_t planeSize = dst->computeImageSize(); |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 90 | if (0 == planeSize) { |
| 91 | return false; // too big to allocate, abort |
| 92 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | dst->fImage = SkMask::AllocImage(planeSize * 3); |
| 94 | memcpy(dst->fImage, alphaPlane, planeSize); |
| 95 | SkMask::FreeImage(alphaPlane); |
| 96 | } |
| 97 | |
| 98 | // run the light direction through the matrix... |
| 99 | Light light = fLight; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 100 | matrix.mapVectors((SkVector*)(void*)light.fDirection, |
| 101 | (SkVector*)(void*)fLight.fDirection, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | |
| 103 | // now restore the length of the XY component |
| 104 | // cast to SkVector so we can call setLength (this double cast silences alias warnings) |
| 105 | SkVector* vec = (SkVector*)(void*)light.fDirection; |
| 106 | vec->setLength(light.fDirection[0], |
| 107 | light.fDirection[1], |
| 108 | SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1])); |
| 109 | |
| 110 | SkEmbossMask::Emboss(dst, light); |
| 111 | |
| 112 | // restore original alpha |
| 113 | memcpy(dst->fImage, src.fImage, src.computeImageSize()); |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 118 | SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer) |
| 119 | : SkMaskFilter(buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | buffer.read(&fLight, sizeof(fLight)); |
| 121 | SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean |
| 122 | fBlurRadius = buffer.readScalar(); |
| 123 | } |
| 124 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame^] | 125 | void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | this->INHERITED::flatten(buffer); |
| 127 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame^] | 128 | Light tmpLight = fLight; |
| 129 | tmpLight.fPad = 0; // for the font-cache lookup to be clean |
| 130 | buffer.writeMul4(&tmpLight, sizeof(tmpLight)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | buffer.writeScalar(fBlurRadius); |
| 132 | } |
| 133 | |