OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLightingImageFilter.h" | 8 #include "SkLightingImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 class DiffuseLightingType { | 58 class DiffuseLightingType { |
59 public: | 59 public: |
60 DiffuseLightingType(SkScalar kd) | 60 DiffuseLightingType(SkScalar kd) |
61 : fKD(kd) {} | 61 : fKD(kd) {} |
62 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons
t SkPoint3& lightColor) const { | 62 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons
t SkPoint3& lightColor) const { |
63 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); | 63 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); |
64 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); | 64 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
65 SkPoint3 color(lightColor * colorScale); | 65 SkPoint3 color(lightColor * colorScale); |
66 return SkPackARGB32(255, | 66 return SkPackARGB32(255, |
67 SkClampMax(SkScalarFloorToInt(color.fX), 255), | 67 SkClampMax(SkScalarRoundToInt(color.fX), 255), |
68 SkClampMax(SkScalarFloorToInt(color.fY), 255), | 68 SkClampMax(SkScalarRoundToInt(color.fY), 255), |
69 SkClampMax(SkScalarFloorToInt(color.fZ), 255)); | 69 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
70 } | 70 } |
71 private: | 71 private: |
72 SkScalar fKD; | 72 SkScalar fKD; |
73 }; | 73 }; |
74 | 74 |
75 class SpecularLightingType { | 75 class SpecularLightingType { |
76 public: | 76 public: |
77 SpecularLightingType(SkScalar ks, SkScalar shininess) | 77 SpecularLightingType(SkScalar ks, SkScalar shininess) |
78 : fKS(ks), fShininess(shininess) {} | 78 : fKS(ks), fShininess(shininess) {} |
79 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons
t SkPoint3& lightColor) const { | 79 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons
t SkPoint3& lightColor) const { |
80 SkPoint3 halfDir(surfaceTolight); | 80 SkPoint3 halfDir(surfaceTolight); |
81 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) | 81 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) |
82 halfDir.normalize(); | 82 halfDir.normalize(); |
83 SkScalar colorScale = SkScalarMul(fKS, | 83 SkScalar colorScale = SkScalarMul(fKS, |
84 SkScalarPow(normal.dot(halfDir), fShininess)); | 84 SkScalarPow(normal.dot(halfDir), fShininess)); |
85 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); | 85 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
86 SkPoint3 color(lightColor * colorScale); | 86 SkPoint3 color(lightColor * colorScale); |
87 return SkPackARGB32(SkClampMax(SkScalarFloorToInt(color.maxComponent()),
255), | 87 return SkPackARGB32(SkClampMax(SkScalarRoundToInt(color.maxComponent()),
255), |
88 SkClampMax(SkScalarFloorToInt(color.fX), 255), | 88 SkClampMax(SkScalarRoundToInt(color.fX), 255), |
89 SkClampMax(SkScalarFloorToInt(color.fY), 255), | 89 SkClampMax(SkScalarRoundToInt(color.fY), 255), |
90 SkClampMax(SkScalarFloorToInt(color.fZ), 255)); | 90 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
91 } | 91 } |
92 private: | 92 private: |
93 SkScalar fKS; | 93 SkScalar fKS; |
94 SkScalar fShininess; | 94 SkScalar fShininess; |
95 }; | 95 }; |
96 | 96 |
97 inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale)
{ | 97 inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale)
{ |
98 return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); | 98 return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); |
99 } | 99 } |
100 | 100 |
(...skipping 1514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1615 | 1615 |
1616 builder->fsCodeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 1616 builder->fsCodeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
1617 } | 1617 } |
1618 | 1618 |
1619 #endif | 1619 #endif |
1620 | 1620 |
1621 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1621 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
1622 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1622 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
1623 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1623 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
1624 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1624 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |