• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import tempfile
6import os
7import unittest
8
9from telemetry import test
10from telemetry.core import bitmap
11from telemetry.core import util
12
13
14# This is a simple base64 encoded 2x2 PNG which contains, in order, a single
15# Red, Yellow, Blue, and Green pixel.
16test_png = """
17iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91
18JpzAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACx
19MBAJqcGAAAABZJREFUCNdj/M/AwPCfgYGB4T/DfwY
20AHAAD/iOWZXsAAAAASUVORK5CYII=
21"""
22test_png_path = os.path.join(util.GetUnittestDataDir(), 'test_png.png')
23test_png_2_path = os.path.join(util.GetUnittestDataDir(), 'test_png_2.png')
24
25
26class HistogramDistanceTest(unittest.TestCase):
27  def testNoData(self):
28    hist1 = []
29    hist2 = []
30    self.assertRaises(
31        ValueError, lambda: bitmap.HistogramDistance(hist1, hist2))
32
33    hist1 = [0, 0, 0]
34    hist2 = [0, 0, 0]
35    self.assertRaises(
36        ValueError, lambda: bitmap.HistogramDistance(hist1, hist2))
37
38  def testWrongSizes(self):
39    hist1 = [1]
40    hist2 = [1, 0]
41    self.assertRaises(
42        ValueError, lambda: bitmap.HistogramDistance(hist1, hist2))
43
44  def testNoDistance(self):
45    hist1 = [2, 4, 1, 8, 0, -1]
46    hist2 = [2, 4, 1, 8, 0, -1]
47    self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 0)
48
49  def testNormalizeCounts(self):
50    hist1 = [0, 0, 1, 0, 0]
51    hist2 = [0, 0, 0, 0, 7]
52    self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 2)
53    self.assertEqual(bitmap.HistogramDistance(hist2, hist1), 2)
54
55  def testDistance(self):
56    hist1 = [2, 0, 1, 3, 4]
57    hist2 = [3, 1, 2, 4, 0]
58    self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 1)
59    self.assertEqual(bitmap.HistogramDistance(hist2, hist1), 1)
60
61    hist1 = [0, 1, 3, 1]
62    hist2 = [2, 2, 1, 0]
63    self.assertEqual(bitmap.HistogramDistance(hist1, hist2), 1.2)
64    self.assertEqual(bitmap.HistogramDistance(hist2, hist1), 1.2)
65
66
67class BitmapTest(unittest.TestCase):
68
69  # pylint: disable=C0324
70
71  def testReadFromBase64Png(self):
72    bmp = bitmap.Bitmap.FromBase64Png(test_png)
73
74    self.assertEquals(2, bmp.width)
75    self.assertEquals(2, bmp.height)
76
77    bmp.GetPixelColor(0, 0).AssertIsRGB(255, 0, 0)
78    bmp.GetPixelColor(1, 1).AssertIsRGB(0, 255, 0)
79    bmp.GetPixelColor(0, 1).AssertIsRGB(0, 0, 255)
80    bmp.GetPixelColor(1, 0).AssertIsRGB(255, 255, 0)
81
82  def testReadFromPngFile(self):
83    file_bmp = bitmap.Bitmap.FromPngFile(test_png_path)
84
85    self.assertEquals(2, file_bmp.width)
86    self.assertEquals(2, file_bmp.height)
87
88    file_bmp.GetPixelColor(0, 0).AssertIsRGB(255, 0, 0)
89    file_bmp.GetPixelColor(1, 1).AssertIsRGB(0, 255, 0)
90    file_bmp.GetPixelColor(0, 1).AssertIsRGB(0, 0, 255)
91    file_bmp.GetPixelColor(1, 0).AssertIsRGB(255, 255, 0)
92
93  def testWritePngToPngFile(self):
94    orig = bitmap.Bitmap.FromPngFile(test_png_path)
95    temp_file = tempfile.NamedTemporaryFile().name
96    orig.WritePngFile(temp_file)
97    new_file = bitmap.Bitmap.FromPngFile(temp_file)
98    self.assertTrue(orig.IsEqual(new_file))
99
100  @test.Disabled
101  def testWriteCroppedBmpToPngFile(self):
102    pixels = [255,0,0, 255,255,0, 0,0,0,
103              255,255,0, 0,255,0, 0,0,0]
104    orig = bitmap.Bitmap(3, 3, 2, pixels)
105    orig.Crop(0, 0, 2, 2)
106    temp_file = tempfile.NamedTemporaryFile().name
107    orig.WritePngFile(temp_file)
108    new_file = bitmap.Bitmap.FromPngFile(temp_file)
109    self.assertTrue(orig.IsEqual(new_file))
110
111  def testIsEqual(self):
112    bmp = bitmap.Bitmap.FromBase64Png(test_png)
113    file_bmp = bitmap.Bitmap.FromPngFile(test_png_path)
114    self.assertTrue(bmp.IsEqual(file_bmp))
115
116  def testDiff(self):
117    file_bmp = bitmap.Bitmap.FromPngFile(test_png_path)
118    file_bmp_2 = bitmap.Bitmap.FromPngFile(test_png_2_path)
119
120    diff_bmp = file_bmp.Diff(file_bmp)
121
122    self.assertEquals(2, diff_bmp.width)
123    self.assertEquals(2, diff_bmp.height)
124
125    diff_bmp.GetPixelColor(0, 0).AssertIsRGB(0, 0, 0)
126    diff_bmp.GetPixelColor(1, 1).AssertIsRGB(0, 0, 0)
127    diff_bmp.GetPixelColor(0, 1).AssertIsRGB(0, 0, 0)
128    diff_bmp.GetPixelColor(1, 0).AssertIsRGB(0, 0, 0)
129
130    diff_bmp = file_bmp.Diff(file_bmp_2)
131
132    self.assertEquals(3, diff_bmp.width)
133    self.assertEquals(3, diff_bmp.height)
134
135    diff_bmp.GetPixelColor(0, 0).AssertIsRGB(0, 255, 255)
136    diff_bmp.GetPixelColor(1, 1).AssertIsRGB(255, 0, 255)
137    diff_bmp.GetPixelColor(0, 1).AssertIsRGB(255, 255, 0)
138    diff_bmp.GetPixelColor(1, 0).AssertIsRGB(0, 0, 255)
139
140    diff_bmp.GetPixelColor(0, 2).AssertIsRGB(255, 255, 255)
141    diff_bmp.GetPixelColor(1, 2).AssertIsRGB(255, 255, 255)
142    diff_bmp.GetPixelColor(2, 0).AssertIsRGB(255, 255, 255)
143    diff_bmp.GetPixelColor(2, 1).AssertIsRGB(255, 255, 255)
144    diff_bmp.GetPixelColor(2, 2).AssertIsRGB(255, 255, 255)
145
146  @test.Disabled
147  def testGetBoundingBox(self):
148    pixels = [0,0,0, 0,0,0, 0,0,0, 0,0,0,
149              0,0,0, 1,0,0, 1,0,0, 0,0,0,
150              0,0,0, 0,0,0, 0,0,0, 0,0,0]
151    bmp = bitmap.Bitmap(3, 4, 3, pixels)
152    box, count = bmp.GetBoundingBox(bitmap.RgbaColor(1, 0, 0))
153    self.assertEquals(box, (1, 1, 2, 1))
154    self.assertEquals(count, 2)
155
156    box, count = bmp.GetBoundingBox(bitmap.RgbaColor(0, 1, 0))
157    self.assertEquals(box, None)
158    self.assertEquals(count, 0)
159
160  @test.Disabled
161  def testCrop(self):
162    pixels = [0,0,0, 1,0,0, 2,0,0, 3,0,0,
163              0,1,0, 1,1,0, 2,1,0, 3,1,0,
164              0,2,0, 1,2,0, 2,2,0, 3,2,0]
165    bmp = bitmap.Bitmap(3, 4, 3, pixels)
166    bmp.Crop(1, 2, 2, 1)
167
168    self.assertEquals(bmp.width, 2)
169    self.assertEquals(bmp.height, 1)
170    bmp.GetPixelColor(0, 0).AssertIsRGB(1, 2, 0)
171    bmp.GetPixelColor(1, 0).AssertIsRGB(2, 2, 0)
172    self.assertEquals(bmp.pixels, bytearray([1,2,0, 2,2,0]))
173
174  @test.Disabled
175  def testHistogram(self):
176    pixels = [1,2,3, 1,2,3, 1,2,3, 1,2,3,
177              1,2,3, 8,7,6, 5,4,6, 1,2,3,
178              1,2,3, 8,7,6, 5,4,6, 1,2,3]
179    bmp = bitmap.Bitmap(3, 4, 3, pixels)
180    bmp.Crop(1, 1, 2, 2)
181
182    histogram = bmp.ColorHistogram()
183    for i in xrange(3):
184      self.assertEquals(sum(histogram[i]), bmp.width * bmp.height)
185    self.assertEquals(histogram.r[1], 0)
186    self.assertEquals(histogram.r[5], 2)
187    self.assertEquals(histogram.r[8], 2)
188    self.assertEquals(histogram.g[2], 0)
189    self.assertEquals(histogram.g[4], 2)
190    self.assertEquals(histogram.g[7], 2)
191    self.assertEquals(histogram.b[3], 0)
192    self.assertEquals(histogram.b[6], 4)
193
194  @test.Disabled
195  def testHistogramIgnoreColor(self):
196    pixels = [1,2,3, 1,2,3, 1,2,3, 1,2,3,
197              1,2,3, 8,7,6, 5,4,6, 1,2,3,
198              1,2,3, 8,7,6, 5,4,6, 1,2,3]
199    bmp = bitmap.Bitmap(3, 4, 3, pixels)
200
201    histogram = bmp.ColorHistogram(ignore_color=bitmap.RgbaColor(1, 2, 3))
202    self.assertEquals(histogram.r[1], 0)
203    self.assertEquals(histogram.r[5], 2)
204    self.assertEquals(histogram.r[8], 2)
205    self.assertEquals(histogram.g[2], 0)
206    self.assertEquals(histogram.g[4], 2)
207    self.assertEquals(histogram.g[7], 2)
208    self.assertEquals(histogram.b[3], 0)
209    self.assertEquals(histogram.b[6], 4)
210
211  @test.Disabled
212  def testHistogramIgnoreColorTolerance(self):
213    pixels = [1,2,3, 4,5,6,
214              7,8,9, 8,7,6]
215    bmp = bitmap.Bitmap(3, 2, 2, pixels)
216
217    histogram = bmp.ColorHistogram(ignore_color=bitmap.RgbaColor(0, 1, 2),
218                                   tolerance=1)
219    self.assertEquals(histogram.r[1], 0)
220    self.assertEquals(histogram.r[4], 1)
221    self.assertEquals(histogram.r[7], 1)
222    self.assertEquals(histogram.r[8], 1)
223    self.assertEquals(histogram.g[2], 0)
224    self.assertEquals(histogram.g[5], 1)
225    self.assertEquals(histogram.g[7], 1)
226    self.assertEquals(histogram.g[8], 1)
227    self.assertEquals(histogram.b[3], 0)
228    self.assertEquals(histogram.b[6], 2)
229    self.assertEquals(histogram.b[9], 1)
230
231  @test.Disabled
232  def testHistogramDistanceIgnoreColor(self):
233    pixels = [1,2,3, 1,2,3,
234              1,2,3, 1,2,3]
235    bmp = bitmap.Bitmap(3, 2, 2, pixels)
236
237    hist1 = bmp.ColorHistogram(ignore_color=bitmap.RgbaColor(1, 2, 3))
238    hist2 = bmp.ColorHistogram()
239
240    self.assertEquals(hist1.Distance(hist2), 0)
241