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

Unified Diff: ui/gfx/safe_integer_conversions.cc

Issue 11093031: Add gfx::ToRoundedInt safe conversion method from float to int. (Closed) Base URL: http://git.chromium.org/chromium/src.git@gfx-scale
Patch Set: Created 8 years, 2 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/safe_integer_conversions.h ('k') | ui/gfx/safe_integer_conversions_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/safe_integer_conversions.cc
diff --git a/ui/gfx/safe_floor_ceil.cc b/ui/gfx/safe_integer_conversions.cc
similarity index 64%
rename from ui/gfx/safe_floor_ceil.cc
rename to ui/gfx/safe_integer_conversions.cc
index c8ac9e1c9e524e8508fcdb0ac3f0bc4a50e5cd14..e3543533e48d4e86a66bd094f907db26b211c875 100644
--- a/ui/gfx/safe_floor_ceil.cc
+++ b/ui/gfx/safe_integer_conversions.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "ui/gfx/safe_integer_conversions.h"
+
#include <cmath>
#include <limits>
@@ -10,9 +12,9 @@ namespace gfx {
int ClampToInt(float value) {
if (value != value)
return 0; // no int NaN.
- if (value > std::numeric_limits<int>::max())
+ if (value >= std::numeric_limits<int>::max())
return std::numeric_limits<int>::max();
- if (value < std::numeric_limits<int>::min())
+ if (value <= std::numeric_limits<int>::min())
return std::numeric_limits<int>::min();
return static_cast<int>(value);
}
@@ -25,5 +27,13 @@ int ToCeiledInt(float value) {
return ClampToInt(std::ceil(value));
}
-} // namespace gfx
+int ToRoundedInt(float value) {
+ float rounded;
+ if (value >= 0.0f)
+ rounded = std::floor(value + 0.5f);
+ else
+ rounded = std::ceil(value - 0.5f);
+ return ClampToInt(rounded);
+}
+} // namespace gfx
« no previous file with comments | « ui/gfx/safe_integer_conversions.h ('k') | ui/gfx/safe_integer_conversions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698