diff options
author | benjaminwagner <benjaminwagner@google.com> | 2016-03-01 13:44:10 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-01 13:44:10 -0800 |
commit | e7be3e5c7999230673c8272a2c73cb7261e735c6 (patch) | |
tree | 4aaa6c525feb890e3fb28a7b61176d6b7f876b75 | |
parent | b9e56c1ee6c4c8ae0a2e9247e504d92de4a6bfaa (diff) |
Check bounds before casting float to integer in SamplePatch.cpp and SkBlurMaskFilter::CreateEmboss.
BUG=skia:4632
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1693013002
Review URL: https://codereview.chromium.org/1693013002
-rw-r--r-- | samplecode/SamplePatch.cpp | 14 | ||||
-rw-r--r-- | src/effects/SkEmbossMaskFilter.cpp | 22 |
2 files changed, 6 insertions, 30 deletions
diff --git a/samplecode/SamplePatch.cpp b/samplecode/SamplePatch.cpp index 92bb17cd06..5347b0182a 100644 --- a/samplecode/SamplePatch.cpp +++ b/samplecode/SamplePatch.cpp @@ -109,20 +109,8 @@ static void eval_sheet(const SkPoint edge[], int nu, int nv, int iu, int iv, pt->set(x, y); } -static int ScalarTo255(SkScalar v) { - int scale = SkScalarToFixed(v) >> 8; - if (scale < 0) { - scale = 0; - } else if (scale > 255) { - scale = 255; - } - return scale; -} - static SkColor make_color(SkScalar s, SkScalar t) { - int cs = ScalarTo255(s); - int ct = ScalarTo255(t); - return SkColorSetARGB(0xFF, cs, 0, 0) + SkColorSetARGB(0, 0, ct, 0); + return SkColorSetARGB(0xFF, SkUnitScalarClampToByte(s), SkUnitScalarClampToByte(t), 0); } void Patch::draw(SkCanvas* canvas, const SkPaint& paint, int nu, int nv, diff --git a/src/effects/SkEmbossMaskFilter.cpp b/src/effects/SkEmbossMaskFilter.cpp index 64afa49447..aacc191ff7 100644 --- a/src/effects/SkEmbossMaskFilter.cpp +++ b/src/effects/SkEmbossMaskFilter.cpp @@ -17,15 +17,6 @@ SkMaskFilter* SkEmbossMaskFilter::Create(SkScalar blurSigma, const Light& light) return new SkEmbossMaskFilter(blurSigma, light); } -static inline int pin2byte(int n) { - if (n < 0) { - n = 0; - } else if (n > 0xFF) { - n = 0xFF; - } - return n; -} - SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3], SkScalar ambient, SkScalar specular, SkScalar blurRadius) { @@ -39,17 +30,14 @@ SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar return nullptr; } - // ambient should be 0...1 as a scalar - int am = pin2byte(SkScalarToFixed(ambient) >> 8); - - // specular should be 0..15.99 as a scalar - int sp = pin2byte(SkScalarToFixed(specular) >> 12); - SkEmbossMaskFilter::Light light; memcpy(light.fDirection, direction, sizeof(light.fDirection)); - light.fAmbient = SkToU8(am); - light.fSpecular = SkToU8(sp); + // ambient should be 0...1 as a scalar + light.fAmbient = SkUnitScalarClampToByte(ambient); + // specular should be 0..15.99 as a scalar + static const SkScalar kSpecularMultiplier = SkIntToScalar(255) / 16; + light.fSpecular = static_cast<U8CPU>(SkScalarPin(specular, 0, 16) * kSpecularMultiplier + 0.5); return SkEmbossMaskFilter::Create(blurSigma, light); } |