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.
13 def _GenImage(size
, color
):
14 return Image
.new('RGBA', size
, color
)
17 def _AllPixelsOfColor(image
, color
):
18 return not any(px
!= color
for px
in image
.getdata())
21 class ImageToolsTest(unittest
.TestCase
):
24 self
.black25
= _GenImage((25, 25), (0, 0, 0, 255))
25 self
.black50
= _GenImage((50, 50), (0, 0, 0, 255))
26 self
.white25
= _GenImage((25, 25), (255, 255, 255, 255))
27 self
.white50
= _GenImage((50, 50), (255, 255, 255, 255))
29 def testAreTheSameSize(self
):
30 self
.assertTrue(image_tools
._AreTheSameSize
([self
.black25
, self
.black25
]))
31 self
.assertTrue(image_tools
._AreTheSameSize
([self
.white25
, self
.white25
]))
32 self
.assertTrue(image_tools
._AreTheSameSize
([self
.black50
, self
.black50
]))
33 self
.assertTrue(image_tools
._AreTheSameSize
([self
.white50
, self
.white50
]))
34 self
.assertTrue(image_tools
._AreTheSameSize
([self
.black25
, self
.white25
]))
35 self
.assertTrue(image_tools
._AreTheSameSize
([self
.black50
, self
.white50
]))
37 self
.assertFalse(image_tools
._AreTheSameSize
([self
.black50
, self
.black25
]))
38 self
.assertFalse(image_tools
._AreTheSameSize
([self
.white50
, self
.white25
]))
39 self
.assertFalse(image_tools
._AreTheSameSize
([self
.black25
, self
.white50
]))
40 self
.assertFalse(image_tools
._AreTheSameSize
([self
.black50
, self
.white25
]))
42 self
.assertRaises(Exception, image_tools
._AreTheSameSize
, [])
43 self
.assertRaises(Exception, image_tools
._AreTheSameSize
, [self
.black50
])
45 def testGetDifferenceWithMask(self
):
46 self
.assertTrue(_AllPixelsOfColor(image_tools
._GetDifferenceWithMask
(
47 self
.black25
, self
.black25
)[0], (255, 255, 255, 255)))
48 self
.assertTrue(_AllPixelsOfColor(image_tools
._GetDifferenceWithMask
(
49 self
.white25
, self
.black25
)[0], (210, 0, 0, 255)))
50 self
.assertTrue(_AllPixelsOfColor(image_tools
._GetDifferenceWithMask
(
51 self
.black25
, self
.black25
, mask
=self
.black25
)[0],
52 (255, 255, 255, 255)))
53 self
.assertTrue(_AllPixelsOfColor(image_tools
._GetDifferenceWithMask
(
54 self
.black25
, self
.black25
, mask
=self
.white25
)[0],
55 (225, 225, 225, 255)))
56 self
.assertTrue(_AllPixelsOfColor(image_tools
._GetDifferenceWithMask
(
57 self
.black25
, self
.white25
, mask
=self
.black25
)[0],
59 self
.assertTrue(_AllPixelsOfColor(image_tools
._GetDifferenceWithMask
(
60 self
.black25
, self
.white25
, mask
=self
.white25
)[0],
61 (225, 225, 225, 255)))
62 self
.assertRaises(Exception, image_tools
._GetDifferenceWithMask
,
65 self
.assertRaises(Exception, image_tools
._GetDifferenceWithMask
,
70 def testCreateMask(self
):
71 m1
= image_tools
.CreateMask([self
.black25
, self
.white25
])
72 self
.assertTrue(_AllPixelsOfColor(m1
, (255, 255, 255, 255)))
73 m2
= image_tools
.CreateMask([self
.black25
, self
.black25
])
74 self
.assertTrue(_AllPixelsOfColor(m2
, (0, 0, 0, 255)))
75 m3
= image_tools
.CreateMask([self
.white25
, self
.white25
])
76 self
.assertTrue(_AllPixelsOfColor(m3
, (0, 0, 0, 255)))
78 def testAddMasks(self
):
79 m1
= image_tools
.CreateMask([self
.black25
, self
.white25
])
80 m2
= image_tools
.CreateMask([self
.black25
, self
.black25
])
81 m3
= image_tools
.CreateMask([self
.black50
, self
.black50
])
82 self
.assertTrue(_AllPixelsOfColor(image_tools
.AddMasks([m1
]),
83 (255, 255, 255, 255)))
84 self
.assertTrue(_AllPixelsOfColor(image_tools
.AddMasks([m2
]),
86 self
.assertTrue(_AllPixelsOfColor(image_tools
.AddMasks([m1
, m2
]),
87 (255, 255, 255, 255)))
88 self
.assertTrue(_AllPixelsOfColor(image_tools
.AddMasks([m1
, m1
]),
89 (255, 255, 255, 255)))
90 self
.assertTrue(_AllPixelsOfColor(image_tools
.AddMasks([m2
, m2
]),
92 self
.assertTrue(_AllPixelsOfColor(image_tools
.AddMasks([m3
]),
94 self
.assertRaises(Exception, image_tools
.AddMasks
, [])
95 self
.assertRaises(Exception, image_tools
.AddMasks
, [m1
, m3
])
97 def testTotalDifferentPixels(self
):
98 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.white25
,
101 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.black25
,
104 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.white25
,
107 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.white25
,
111 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.white25
,
115 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.white25
,
119 self
.assertEquals(image_tools
.TotalDifferentPixels(self
.white25
,
123 self
.assertRaises(Exception, image_tools
.TotalDifferentPixels
,
124 self
.white25
, self
.white50
)
125 self
.assertRaises(Exception, image_tools
.TotalDifferentPixels
,
126 self
.white25
, self
.white25
, mask
=self
.white50
)
128 def testSameImage(self
):
129 self
.assertTrue(image_tools
.SameImage(self
.white25
, self
.white25
))
130 self
.assertFalse(image_tools
.SameImage(self
.white25
, self
.black25
))
132 self
.assertTrue(image_tools
.SameImage(self
.white25
, self
.black25
,
134 self
.assertFalse(image_tools
.SameImage(self
.white25
, self
.black25
,
136 self
.assertTrue(image_tools
.SameImage(self
.black25
, self
.black25
))
137 self
.assertTrue(image_tools
.SameImage(self
.black25
, self
.black25
,
139 self
.assertTrue(image_tools
.SameImage(self
.white25
, self
.white25
,
141 self
.assertRaises(Exception, image_tools
.SameImage
,
142 self
.white25
, self
.white50
)
143 self
.assertRaises(Exception, image_tools
.SameImage
,
144 self
.white25
, self
.white25
,
147 def testInflateMask(self
):
148 cross_image
= Image
.new('RGBA', (3, 3))
149 white_image
= Image
.new('RGBA', (3, 3))
150 dot_image
= Image
.new('RGBA', (3, 3))
152 w
= (255, 255, 255, 255)
153 dot_image
.putdata([b
, b
, b
,
156 cross_image
.putdata([b
, w
, b
,
159 white_image
.putdata([w
, w
, w
,
162 self
.assertEquals(list(image_tools
.InflateMask(dot_image
, 1).getdata()),
163 list(cross_image
.getdata()))
164 self
.assertEquals(list(image_tools
.InflateMask(dot_image
, 0).getdata()),
165 list(dot_image
.getdata()))
166 self
.assertEquals(list(image_tools
.InflateMask(dot_image
, 2).getdata()),
167 list(white_image
.getdata()))
168 self
.assertEquals(list(image_tools
.InflateMask(dot_image
, 3).getdata()),
169 list(white_image
.getdata()))
170 self
.assertEquals(list(image_tools
.InflateMask(self
.black25
, 1).getdata()),
171 list(self
.black25
.getdata()))
173 def testPNGEncodeDecode(self
):
174 self
.assertTrue(_AllPixelsOfColor(
175 image_tools
.DecodePNG(
176 image_tools
.EncodePNG(self
.white25
)), (255, 255, 255, 255)))
177 self
.assertTrue(_AllPixelsOfColor(
178 image_tools
.DecodePNG(
179 image_tools
.EncodePNG(self
.black25
)), (0, 0, 0, 255)))
182 if __name__
== '__main__':