Chromium Code Reviews| Index: tools/telemetry/telemetry/core/bitmap_unittest.py |
| diff --git a/tools/telemetry/telemetry/core/bitmap_unittest.py b/tools/telemetry/telemetry/core/bitmap_unittest.py |
| index b24900e9a3ba191edcb4fa5406f75deb7114152b..89f2f1972b13a08d66b95b5a40fd1d9a867324b5 100644 |
| --- a/tools/telemetry/telemetry/core/bitmap_unittest.py |
| +++ b/tools/telemetry/telemetry/core/bitmap_unittest.py |
| @@ -2,10 +2,11 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| +from __future__ import division |
| import os |
| import tempfile |
| import unittest |
| - |
| +import cv2 |
|
tonyg
2014/10/21 16:12:57
alphabetize
mthiesse
2014/10/22 15:33:10
Done.
|
| from telemetry import benchmark |
| from telemetry.core import bitmap |
| from telemetry.core import util |
| @@ -14,15 +15,16 @@ from telemetry.core import util |
| # Red, Yellow, Blue, and Green pixel. |
| test_png = """ |
| iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91 |
| -JpzAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACx |
| -MBAJqcGAAAABZJREFUCNdj/M/AwPCfgYGB4T/DfwY |
| -AHAAD/iOWZXsAAAAASUVORK5CYII= |
| +JpzAAAAFklEQVR4Xg3EAQ0AAABAMP1LY3YI7l8l6A |
| +T8tgwbJAAAAABJRU5ErkJggg== |
|
flackr
2014/10/21 21:50:10
Curious, if the PNG still contains the same thing,
mthiesse
2014/10/22 15:33:10
It's the same PNG, but properly compressed :P Look
|
| """ |
| + |
| test_png_path = os.path.join(util.GetUnittestDataDir(), 'test_png.png') |
| test_png_2_path = os.path.join(util.GetUnittestDataDir(), 'test_png_2.png') |
| class HistogramDistanceTest(unittest.TestCase): |
| + |
| def testNoData(self): |
| hist1 = [] |
| hist2 = [] |
| @@ -41,15 +43,15 @@ class HistogramDistanceTest(unittest.TestCase): |
| ValueError, lambda: bitmap.HistogramDistance(hist1, hist2)) |
| def testNoDistance(self): |
| - hist1 = [2, 4, 1, 8, 0, -1] |
| - hist2 = [2, 4, 1, 8, 0, -1] |
| + hist1 = [2, 4, 1, 8, 0, 0] |
| + hist2 = [2, 4, 1, 8, 0, 0] |
| self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 0) |
| def testNormalizeCounts(self): |
| hist1 = [0, 0, 1, 0, 0] |
| hist2 = [0, 0, 0, 0, 7] |
| - self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 2) |
| - self.assertEqual(bitmap.HistogramDistance(hist2, hist1), 2) |
| + self.assertAlmostEqual(bitmap.HistogramDistance(hist1, hist2), 2) |
| + self.assertAlmostEqual(bitmap.HistogramDistance(hist2, hist1), 2) |
| def testDistance(self): |
| hist1 = [2, 0, 1, 3, 4] |
| @@ -59,100 +61,93 @@ class HistogramDistanceTest(unittest.TestCase): |
| hist1 = [0, 1, 3, 1] |
| hist2 = [2, 2, 1, 0] |
| - self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 1.2) |
| - self.assertEqual(bitmap.HistogramDistance(hist2, hist1), 1.2) |
| + self.assertAlmostEqual(bitmap.HistogramDistance(hist1, hist2), 1.2) |
| + self.assertAlmostEqual(bitmap.HistogramDistance(hist2, hist1), 1.2) |
| class BitmapTest(unittest.TestCase): |
| - # pylint: disable=C0324 |
| - |
| def testReadFromBase64Png(self): |
| bmp = bitmap.Bitmap.FromBase64Png(test_png) |
| self.assertEquals(2, bmp.width) |
| self.assertEquals(2, bmp.height) |
| - bmp.GetPixelColor(0, 0).AssertIsRGB(255, 0, 0) |
| - bmp.GetPixelColor(1, 1).AssertIsRGB(0, 255, 0) |
| - bmp.GetPixelColor(0, 1).AssertIsRGB(0, 0, 255) |
| - bmp.GetPixelColor(1, 0).AssertIsRGB(255, 255, 0) |
| + self.assertTrue(bitmap.ColorsAreEqual(bmp.image[0][0], (0, 0, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(bmp.image[1][1], (0, 255, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(bmp.image[1][0], (255, 0, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(bmp.image[0][1], (0, 255, 255))) |
| def testReadFromPngFile(self): |
| - file_bmp = bitmap.Bitmap.FromPngFile(test_png_path) |
| + file_bmp = bitmap.Bitmap.FromImageFile(test_png_path) |
| self.assertEquals(2, file_bmp.width) |
| self.assertEquals(2, file_bmp.height) |
| - file_bmp.GetPixelColor(0, 0).AssertIsRGB(255, 0, 0) |
| - file_bmp.GetPixelColor(1, 1).AssertIsRGB(0, 255, 0) |
| - file_bmp.GetPixelColor(0, 1).AssertIsRGB(0, 0, 255) |
| - file_bmp.GetPixelColor(1, 0).AssertIsRGB(255, 255, 0) |
| + self.assertTrue(bitmap.ColorsAreEqual(file_bmp.image[0][0], (0, 0, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(file_bmp.image[1][1], (0, 255, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(file_bmp.image[1][0], (255, 0, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(file_bmp.image[0][1], (0, 255, 255))) |
| def testWritePngToPngFile(self): |
| - orig = bitmap.Bitmap.FromPngFile(test_png_path) |
| - temp_file = tempfile.NamedTemporaryFile().name |
| - orig.WritePngFile(temp_file) |
| - new_file = bitmap.Bitmap.FromPngFile(temp_file) |
| + orig = bitmap.Bitmap.FromImageFile(test_png_path) |
| + temp_file = tempfile.NamedTemporaryFile(suffix='.png').name |
| + cv2.imwrite(temp_file, orig.image) |
| + new_file = bitmap.Bitmap.FromImageFile(temp_file) |
| self.assertTrue(orig.IsEqual(new_file)) |
| @benchmark.Disabled |
|
tonyg
2014/10/21 16:12:57
We should probably enable this and the other disab
mthiesse
2014/10/22 15:33:10
Do you happen to know why all of these tests were
|
| def testWriteCroppedBmpToPngFile(self): |
| pixels = [255,0,0, 255,255,0, 0,0,0, |
| 255,255,0, 0,255,0, 0,0,0] |
| - orig = bitmap.Bitmap(3, 3, 2, pixels) |
| + orig = bitmap.Bitmap.FromRGBPixels(3, 2, pixels, 3) |
| orig.Crop(0, 0, 2, 2) |
| - temp_file = tempfile.NamedTemporaryFile().name |
| - orig.WritePngFile(temp_file) |
| - new_file = bitmap.Bitmap.FromPngFile(temp_file) |
| + temp_file = tempfile.NamedTemporaryFile(suffix='.png').name |
| + cv2.imwrite(temp_file, orig.image) |
| + new_file = bitmap.Bitmap.FromImageFile(temp_file) |
| self.assertTrue(orig.IsEqual(new_file)) |
| - def testIsEqual(self): |
| - bmp = bitmap.Bitmap.FromBase64Png(test_png) |
| - file_bmp = bitmap.Bitmap.FromPngFile(test_png_path) |
| - self.assertTrue(bmp.IsEqual(file_bmp)) |
| - |
| def testDiff(self): |
| - file_bmp = bitmap.Bitmap.FromPngFile(test_png_path) |
| - file_bmp_2 = bitmap.Bitmap.FromPngFile(test_png_2_path) |
| + file_bmp = bitmap.Bitmap.FromImageFile(test_png_path) |
| + file_bmp_2 = bitmap.Bitmap.FromImageFile(test_png_2_path) |
| - diff_bmp = file_bmp.Diff(file_bmp) |
| + diff = file_bmp.Diff(file_bmp) |
| - self.assertEquals(2, diff_bmp.width) |
| - self.assertEquals(2, diff_bmp.height) |
| + self.assertEquals(2, diff.width) |
| + self.assertEquals(2, diff.height) |
| - diff_bmp.GetPixelColor(0, 0).AssertIsRGB(0, 0, 0) |
| - diff_bmp.GetPixelColor(1, 1).AssertIsRGB(0, 0, 0) |
| - diff_bmp.GetPixelColor(0, 1).AssertIsRGB(0, 0, 0) |
| - diff_bmp.GetPixelColor(1, 0).AssertIsRGB(0, 0, 0) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[0][0], (0, 0, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[1][1], (0, 0, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[1][0], (0, 0, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[0][1], (0, 0, 0))) |
| - diff_bmp = file_bmp.Diff(file_bmp_2) |
| + diff = file_bmp.Diff(file_bmp_2) |
| - self.assertEquals(3, diff_bmp.width) |
| - self.assertEquals(3, diff_bmp.height) |
| + self.assertEquals(3, diff.width) |
| + self.assertEquals(3, diff.height) |
| - diff_bmp.GetPixelColor(0, 0).AssertIsRGB(0, 255, 255) |
| - diff_bmp.GetPixelColor(1, 1).AssertIsRGB(255, 0, 255) |
| - diff_bmp.GetPixelColor(0, 1).AssertIsRGB(255, 255, 0) |
| - diff_bmp.GetPixelColor(1, 0).AssertIsRGB(0, 0, 255) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[0][0], (255, 255, 0))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[1][1], (255, 0, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[1][0], (0, 255, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[0][1], (255, 0, 0))) |
| - diff_bmp.GetPixelColor(0, 2).AssertIsRGB(255, 255, 255) |
| - diff_bmp.GetPixelColor(1, 2).AssertIsRGB(255, 255, 255) |
| - diff_bmp.GetPixelColor(2, 0).AssertIsRGB(255, 255, 255) |
| - diff_bmp.GetPixelColor(2, 1).AssertIsRGB(255, 255, 255) |
| - diff_bmp.GetPixelColor(2, 2).AssertIsRGB(255, 255, 255) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[2][0], (255, 255, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[2][1], (255, 255, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[0][2], (255, 255, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[1][2], (255, 255, 255))) |
| + self.assertTrue(bitmap.ColorsAreEqual(diff.image[2][2], (255, 255, 255))) |
| @benchmark.Disabled |
| def testGetBoundingBox(self): |
| pixels = [0,0,0, 0,0,0, 0,0,0, 0,0,0, |
| 0,0,0, 1,0,0, 1,0,0, 0,0,0, |
| 0,0,0, 0,0,0, 0,0,0, 0,0,0] |
| - bmp = bitmap.Bitmap(3, 4, 3, pixels) |
| - box, count = bmp.GetBoundingBox(bitmap.RgbaColor(1, 0, 0)) |
| + bmp = bitmap.Bitmap.FromRGBPixels(4, 3, pixels) |
| + box, count = bmp.GetBoundingBox((0, 0, 1)) |
| self.assertEquals(box, (1, 1, 2, 1)) |
| self.assertEquals(count, 2) |
| - box, count = bmp.GetBoundingBox(bitmap.RgbaColor(0, 1, 0)) |
| + box, count = bmp.GetBoundingBox((0, 1, 0)) |
| self.assertEquals(box, None) |
| self.assertEquals(count, 0) |
| @@ -161,21 +156,21 @@ class BitmapTest(unittest.TestCase): |
| pixels = [0,0,0, 1,0,0, 2,0,0, 3,0,0, |
| 0,1,0, 1,1,0, 2,1,0, 3,1,0, |
| 0,2,0, 1,2,0, 2,2,0, 3,2,0] |
| - bmp = bitmap.Bitmap(3, 4, 3, pixels) |
| + bmp = bitmap.Bitmap.FromRGBPixels(4, 3, pixels) |
| bmp.Crop(1, 2, 2, 1) |
| self.assertEquals(bmp.width, 2) |
| self.assertEquals(bmp.height, 1) |
| - bmp.GetPixelColor(0, 0).AssertIsRGB(1, 2, 0) |
| - bmp.GetPixelColor(1, 0).AssertIsRGB(2, 2, 0) |
| - self.assertEquals(bmp.pixels, bytearray([1,2,0, 2,2,0])) |
| + |
| + self.assertTrue(bitmap.ColorsAreEqual(bmp.image[0][0], (0, 2, 1))) |
| + self.assertTrue(bitmap.ColorsAreEqual(bmp.image[0][1], (0, 2, 2))) |
| @benchmark.Disabled |
| def testHistogram(self): |
| pixels = [1,2,3, 1,2,3, 1,2,3, 1,2,3, |
| 1,2,3, 8,7,6, 5,4,6, 1,2,3, |
| 1,2,3, 8,7,6, 5,4,6, 1,2,3] |
| - bmp = bitmap.Bitmap(3, 4, 3, pixels) |
| + bmp = bitmap.Bitmap.FromRGBPixels(4, 3, pixels) |
| bmp.Crop(1, 1, 2, 2) |
| histogram = bmp.ColorHistogram() |
| @@ -195,9 +190,9 @@ class BitmapTest(unittest.TestCase): |
| pixels = [1,2,3, 1,2,3, 1,2,3, 1,2,3, |
| 1,2,3, 8,7,6, 5,4,6, 1,2,3, |
| 1,2,3, 8,7,6, 5,4,6, 1,2,3] |
| - bmp = bitmap.Bitmap(3, 4, 3, pixels) |
| + bmp = bitmap.Bitmap.FromRGBPixels(4, 3, pixels) |
| - histogram = bmp.ColorHistogram(ignore_color=bitmap.RgbaColor(1, 2, 3)) |
| + histogram = bmp.ColorHistogram(ignore_color=(3, 2, 1)) |
| self.assertEquals(histogram.r[1], 0) |
| self.assertEquals(histogram.r[5], 2) |
| self.assertEquals(histogram.r[8], 2) |
| @@ -211,10 +206,9 @@ class BitmapTest(unittest.TestCase): |
| def testHistogramIgnoreColorTolerance(self): |
| pixels = [1,2,3, 4,5,6, |
| 7,8,9, 8,7,6] |
| - bmp = bitmap.Bitmap(3, 2, 2, pixels) |
| + bmp = bitmap.Bitmap.FromRGBPixels(2, 2, pixels) |
| - histogram = bmp.ColorHistogram(ignore_color=bitmap.RgbaColor(0, 1, 2), |
| - tolerance=1) |
| + histogram = bmp.ColorHistogram(ignore_color=(2, 1, 0), tolerance=1) |
| self.assertEquals(histogram.r[1], 0) |
| self.assertEquals(histogram.r[4], 1) |
| self.assertEquals(histogram.r[7], 1) |
| @@ -231,9 +225,9 @@ class BitmapTest(unittest.TestCase): |
| def testHistogramDistanceIgnoreColor(self): |
| pixels = [1,2,3, 1,2,3, |
| 1,2,3, 1,2,3] |
| - bmp = bitmap.Bitmap(3, 2, 2, pixels) |
| + bmp = bitmap.Bitmap.FromRGBPixels(2, 2, pixels) |
| - hist1 = bmp.ColorHistogram(ignore_color=bitmap.RgbaColor(1, 2, 3)) |
| + hist1 = bmp.ColorHistogram(ignore_color=(1, 2, 3)) |
| hist2 = bmp.ColorHistogram() |
| self.assertEquals(hist1.Distance(hist2), 0) |