improve 565 quality

Change-Id: I48c9cd5bc0deff58145d380d1a651c33ea220d3a
Reviewed-on: https://skia-review.googlesource.com/c/eskia/+/239463
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/src/eskia.cc b/src/eskia.cc
index 992186e..d0bf643 100644
--- a/src/eskia.cc
+++ b/src/eskia.cc
@@ -36,27 +36,6 @@
     }
     static_assert(test_unpack(), "");
 
-
-    // Convert from 8-bit unorm to 5- or 6-bit unorm.
-    static constexpr uint8_t pack_5(int x) { return implicit_cast<uint8_t>(x >> 3); }
-    static constexpr uint8_t pack_6(int x) { return implicit_cast<uint8_t>(x >> 2); }
-
-    static constexpr bool test_pack() {
-        for (int i = 0; i <= 255; i++) {
-            int tolerance = (i == 0 || i == 255) ? 0 : 1;
-
-            int error_5 = implicit_cast<int>(i * 31/255.0 + 0.5) - pack_5(i);
-            int error_6 = implicit_cast<int>(i * 63/255.0 + 0.5) - pack_6(i);
-
-            if (error_5 > +tolerance || error_5 < -tolerance ||
-                error_6 > +tolerance || error_6 < -tolerance) {
-                return false;
-            }
-        }
-        return true;
-    }
-    static_assert(test_pack(), "");
-
     // Rounding divide by 255, to convert the product of two 8-bit unorms back to an 8-bit unorm.
     static constexpr uint8_t approx_div255(int x) {
         return implicit_cast<uint8_t>((x + 255) / 256);
@@ -77,6 +56,26 @@
     }
     static_assert(test_approx_div255(), "");
 
+    // Convert from 8-bit unorm to 5- or 6-bit unorm.
+    static constexpr uint8_t pack_5(int x) { return approx_div255(x * 31); }
+    static constexpr uint8_t pack_6(int x) { return approx_div255(x * 63); }
+
+    static constexpr bool test_pack() {
+        for (int i = 0; i <= 255; i++) {
+            int tolerance = (i == 0 || i == 255) ? 0 : 1;
+
+            int error_5 = implicit_cast<int>(i * 31/255.0 + 0.5) - pack_5(i);
+            int error_6 = implicit_cast<int>(i * 63/255.0 + 0.5) - pack_6(i);
+
+            if (error_5 > +tolerance || error_5 < -tolerance ||
+                error_6 > +tolerance || error_6 < -tolerance) {
+                return false;
+            }
+        }
+        return true;
+    }
+    static_assert(test_pack(), "");
+
     // Linearly interpolate from d to s by c, s and d 8-bit unorms, c 7-bit [0,128] coverage.
     static constexpr uint8_t lerp(int s, int d, int c) {
         return implicit_cast<uint8_t>( (s*c + (128-c)*d)/128 );