Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(996)

Side by Side Diff: third_party/WebKit/Source/platform/graphics/filters/FEComponentTransfer.cpp

Issue 1530723004: Use clampTo instead of chaining std::max(std::min(...)) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More rebase Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org> 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org> 3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org> 4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> 5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6 * Copyright (C) Research In Motion Limited 2010. All rights reserved. 6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7 * Copyright (C) 2013 Google Inc. All rights reserved. 7 * Copyright (C) 2013 Google Inc. All rights reserved.
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
(...skipping 10 matching lines...) Expand all
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA. 22 * Boston, MA 02110-1301, USA.
23 */ 23 */
24 24
25 #include "platform/graphics/filters/FEComponentTransfer.h" 25 #include "platform/graphics/filters/FEComponentTransfer.h"
26 26
27 #include "SkColorFilterImageFilter.h" 27 #include "SkColorFilterImageFilter.h"
28 #include "SkTableColorFilter.h" 28 #include "SkTableColorFilter.h"
29 #include "platform/graphics/filters/SkiaImageFilterBuilder.h" 29 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
30 #include "platform/text/TextStream.h" 30 #include "platform/text/TextStream.h"
31 #include "wtf/MathExtras.h"
32 #include <algorithm>
31 33
32 namespace blink { 34 namespace blink {
33 35
34 typedef void (*TransferType)(unsigned char*, const ComponentTransferFunction&); 36 typedef void (*TransferType)(unsigned char*, const ComponentTransferFunction&);
35 37
36 FEComponentTransfer::FEComponentTransfer(Filter* filter, const ComponentTransfer Function& redFunc, const ComponentTransferFunction& greenFunc, 38 FEComponentTransfer::FEComponentTransfer(Filter* filter, const ComponentTransfer Function& redFunc, const ComponentTransferFunction& greenFunc,
37 const ComponentTransferFunction& blueFunc, const ComponentTransferFunction& alphaFunc) 39 const ComponentTransferFunction& blueFunc, const ComponentTransferFunction& alphaFunc)
38 : FilterEffect(filter) 40 : FilterEffect(filter)
39 , m_redFunc(redFunc) 41 , m_redFunc(redFunc)
40 , m_greenFunc(greenFunc) 42 , m_greenFunc(greenFunc)
(...skipping 17 matching lines...) Expand all
58 const Vector<float>& tableValues = transferFunction.tableValues; 60 const Vector<float>& tableValues = transferFunction.tableValues;
59 unsigned n = tableValues.size(); 61 unsigned n = tableValues.size();
60 if (n < 1) 62 if (n < 1)
61 return; 63 return;
62 for (unsigned i = 0; i < 256; ++i) { 64 for (unsigned i = 0; i < 256; ++i) {
63 double c = i / 255.0; 65 double c = i / 255.0;
64 unsigned k = static_cast<unsigned>(c * (n - 1)); 66 unsigned k = static_cast<unsigned>(c * (n - 1));
65 double v1 = tableValues[k]; 67 double v1 = tableValues[k];
66 double v2 = tableValues[std::min((k + 1), (n - 1))]; 68 double v2 = tableValues[std::min((k + 1), (n - 1))];
67 double val = 255.0 * (v1 + (c * (n - 1) - k) * (v2 - v1)); 69 double val = 255.0 * (v1 + (c * (n - 1) - k) * (v2 - v1));
68 val = std::max(0.0, std::min(255.0, val)); 70 val = clampTo(val, 0.0, 255.0);
69 values[i] = static_cast<unsigned char>(val); 71 values[i] = static_cast<unsigned char>(val);
70 } 72 }
71 } 73 }
72 74
73 static void discrete(unsigned char* values, const ComponentTransferFunction& tra nsferFunction) 75 static void discrete(unsigned char* values, const ComponentTransferFunction& tra nsferFunction)
74 { 76 {
75 const Vector<float>& tableValues = transferFunction.tableValues; 77 const Vector<float>& tableValues = transferFunction.tableValues;
76 unsigned n = tableValues.size(); 78 unsigned n = tableValues.size();
77 if (n < 1) 79 if (n < 1)
78 return; 80 return;
79 for (unsigned i = 0; i < 256; ++i) { 81 for (unsigned i = 0; i < 256; ++i) {
80 unsigned k = static_cast<unsigned>((i * n) / 255.0); 82 unsigned k = static_cast<unsigned>((i * n) / 255.0);
81 k = std::min(k, n - 1); 83 k = std::min(k, n - 1);
82 double val = 255 * tableValues[k]; 84 double val = 255 * tableValues[k];
83 val = std::max(0.0, std::min(255.0, val)); 85 val = clampTo(val, 0.0, 255.0);
84 values[i] = static_cast<unsigned char>(val); 86 values[i] = static_cast<unsigned char>(val);
85 } 87 }
86 } 88 }
87 89
88 static void linear(unsigned char* values, const ComponentTransferFunction& trans ferFunction) 90 static void linear(unsigned char* values, const ComponentTransferFunction& trans ferFunction)
89 { 91 {
90 for (unsigned i = 0; i < 256; ++i) { 92 for (unsigned i = 0; i < 256; ++i) {
91 double val = transferFunction.slope * i + 255 * transferFunction.interce pt; 93 double val = transferFunction.slope * i + 255 * transferFunction.interce pt;
92 val = std::max(0.0, std::min(255.0, val)); 94 val = clampTo(val, 0.0, 255.0);
93 values[i] = static_cast<unsigned char>(val); 95 values[i] = static_cast<unsigned char>(val);
94 } 96 }
95 } 97 }
96 98
97 static void gamma(unsigned char* values, const ComponentTransferFunction& transf erFunction) 99 static void gamma(unsigned char* values, const ComponentTransferFunction& transf erFunction)
98 { 100 {
99 for (unsigned i = 0; i < 256; ++i) { 101 for (unsigned i = 0; i < 256; ++i) {
100 double exponent = transferFunction.exponent; // RCVT doesn't like passin g a double and a float to pow, so promote this to double 102 double exponent = transferFunction.exponent; // RCVT doesn't like passin g a double and a float to pow, so promote this to double
101 double val = 255.0 * (transferFunction.amplitude * pow((i / 255.0), expo nent) + transferFunction.offset); 103 double val = 255.0 * (transferFunction.amplitude * pow((i / 255.0), expo nent) + transferFunction.offset);
102 val = std::max(0.0, std::min(255.0, val)); 104 val = clampTo(val, 0.0, 255.0);
103 values[i] = static_cast<unsigned char>(val); 105 values[i] = static_cast<unsigned char>(val);
104 } 106 }
105 } 107 }
106 108
107 bool FEComponentTransfer::affectsTransparentPixels() 109 bool FEComponentTransfer::affectsTransparentPixels()
108 { 110 {
109 double intercept = 0; 111 double intercept = 0;
110 switch (m_alphaFunc.type) { 112 switch (m_alphaFunc.type) {
111 case FECOMPONENTTRANSFER_TYPE_UNKNOWN: 113 case FECOMPONENTTRANSFER_TYPE_UNKNOWN:
112 case FECOMPONENTTRANSFER_TYPE_IDENTITY: 114 case FECOMPONENTTRANSFER_TYPE_IDENTITY:
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 ts << "{green: " << m_greenFunc << "}\n"; 203 ts << "{green: " << m_greenFunc << "}\n";
202 writeIndent(ts, indent + 2); 204 writeIndent(ts, indent + 2);
203 ts << "{blue: " << m_blueFunc << "}\n"; 205 ts << "{blue: " << m_blueFunc << "}\n";
204 writeIndent(ts, indent + 2); 206 writeIndent(ts, indent + 2);
205 ts << "{alpha: " << m_alphaFunc << "}]\n"; 207 ts << "{alpha: " << m_alphaFunc << "}]\n";
206 inputEffect(0)->externalRepresentation(ts, indent + 1); 208 inputEffect(0)->externalRepresentation(ts, indent + 1);
207 return ts; 209 return ts;
208 } 210 }
209 211
210 } // namespace blink 212 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698