aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Mike Klein <mtklein@chromium.org>2017-01-17 10:19:50 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-01-17 16:05:11 +0000
commitd32b8177da4efad14a609c7c34784dffb47575ee (patch)
tree8edd4c3bdd32b363c8a03b7e856048cf3224665f
parenteaf0079d81beac9fea2da7a20c3587ebbbbe6463 (diff)
add and test SkFixed15::to_u8()
Change-Id: Iedbcd2d938122cdc8f6b235745eb6165e348c237 Reviewed-on: https://skia-review.googlesource.com/7108 Commit-Queue: Mike Klein <mtklein@chromium.org> Reviewed-by: Herb Derby <herb@google.com>
-rw-r--r--src/core/SkFixed15.h6
-rw-r--r--tests/SkFixed15Test.cpp5
2 files changed, 11 insertions, 0 deletions
diff --git a/src/core/SkFixed15.h b/src/core/SkFixed15.h
index 43a9fae773..509febc7c1 100644
--- a/src/core/SkFixed15.h
+++ b/src/core/SkFixed15.h
@@ -32,6 +32,12 @@ public:
+ ((val+1)>>8); // All val but 255 are correct. +1 if val == 255 to get 32768.
}
+ uint8_t to_u8() const {
+ // FromU8() and to_u8() roundtrip all bytes.
+ // There is still much room to tweak this towards the ideal, a rounding scale by 255/32768.
+ return (fVal - (fVal>>8))>>7;
+ }
+
SkFixed15 operator +(SkFixed15 o) const { return fVal + o.fVal; }
SkFixed15 operator -(SkFixed15 o) const { return fVal - o.fVal; }
SkFixed15 operator *(SkFixed15 o) const { return (fVal * o.fVal + (1<<14)) >> 15; }
diff --git a/tests/SkFixed15Test.cpp b/tests/SkFixed15Test.cpp
index e2108d5604..df957b1e49 100644
--- a/tests/SkFixed15Test.cpp
+++ b/tests/SkFixed15Test.cpp
@@ -26,4 +26,9 @@ DEF_TEST(SkFixed15, r) {
for (int x = 0; x < 256; x++) {
REPORTER_ASSERT(r, SkFixed15::FromU8(x) == SkFixed15(x * (1/255.0f)));
}
+
+ // to_u8() and FromU8() should roundtrip all bytes.
+ for (int x = 0; x < 256; x++) {
+ REPORTER_ASSERT(r, x == SkFixed15::FromU8(x).to_u8());
+ }
}