am 41347bce: Merge "CTS Holo: Add +/- tolerance to bitmap comparison"

* commit '41347bce12f582cbad842e6c930251f3cdc74509':
  CTS Holo: Add +/- tolerance to bitmap comparison
diff --git a/tests/tests/holo/src/android/holo/cts/LayoutTestActivity.java b/tests/tests/holo/src/android/holo/cts/LayoutTestActivity.java
index c908a64..052ddea 100644
--- a/tests/tests/holo/src/android/holo/cts/LayoutTestActivity.java
+++ b/tests/tests/holo/src/android/holo/cts/LayoutTestActivity.java
@@ -33,6 +33,7 @@
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.nio.ByteBuffer;
 
 /**
  * {@link Activity} that applies a theme, inflates a layout, and then either
@@ -162,7 +163,51 @@
         protected void onPreExecute() {
             mBitmap = getBitmap();
             mReferenceBitmap = BitmapAssets.getBitmap(getApplicationContext(), mBitmapName);
-            mSame = mBitmap.sameAs(mReferenceBitmap);
+            final int threshold = 1;
+            mSame = compareTo(mBitmap, mReferenceBitmap, threshold);
+        }
+
+        /* Compares 2 bitmaps' width, height and pixels.
+         * 2 Bitmaps are consider the same if color value difference is less than
+         * or equal to +/-threshold
+         */
+        private boolean compareTo(Bitmap bitmap, Bitmap reference, int threshold) {
+            if (bitmap.getConfig() != reference.getConfig() ||
+                    bitmap.getWidth() != reference.getWidth() ||
+                    bitmap.getHeight() != reference.getHeight()) {
+                return false;
+            }
+
+            int w = bitmap.getWidth();
+            int h = bitmap.getHeight();
+
+            ByteBuffer buffer1 = ByteBuffer.allocate(bitmap.getByteCount());
+            ByteBuffer buffer2 = ByteBuffer.allocate(reference.getByteCount());
+
+            bitmap.copyPixelsToBuffer(buffer1);
+            reference.copyPixelsToBuffer(buffer2);
+
+            final int length = w*h;
+            for (int i = 0; i < length; i++) {
+                int pel1 = buffer1.getInt(i);
+                int pel2 = buffer2.getInt(i);
+                int dr = (pel1 & 0x000000FF) - (pel2 & 0x000000FF);
+                int dg = ((pel1 & 0x0000FF00) - (pel2 & 0x0000FF00)) >> 8;
+                int db = ((pel1 & 0x00FF0000) - (pel2 & 0x00FF0000)) >> 16;
+
+                if (Math.abs(db) > threshold ||
+                        Math.abs(dg) > threshold ||
+                        Math.abs(dr) > threshold) {
+                    return false;
+                }
+                if (bitmap.hasAlpha()) {
+                    int da = ((pel1 & 0xFF000000) - (pel2 & 0xFF000000)) >> 24;
+                    if (Math.abs(da) > threshold) {
+                        return false;
+                    }
+                }
+            }
+            return true;
         }
 
         @Override