1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
import unittest
from unittests import wtc
import wx
import six
from six import BytesIO as FileLikeObject
import os
pngFile = os.path.join(os.path.dirname(__file__), 'toucan.png')
#---------------------------------------------------------------------------
def makeBuf(w, h, bpp=1, init=0):
"Make a simple buffer for testing with"
buf = bytearray([init] * (w*h*bpp))
return buf
class image_Tests(wtc.WidgetTestCase):
def test_imageCtor1(self):
img = wx.Image()
self.assertTrue(not img.IsOk())
img.Create(100,100)
self.assertTrue(img.IsOk())
def test_imageCtor2(self):
img = wx.Image(100,100)
self.assertTrue(img.IsOk())
def test_imageCtor3(self):
img = wx.Image(wx.Size(100,100))
self.assertTrue(img.IsOk())
img = wx.Image((100,100))
self.assertTrue(img.IsOk())
def test_imageCtor4(self):
w = h = 10
buf = makeBuf(w,h,3)
img = wx.Image(w, h, buf)
self.assertTrue(img.IsOk())
def test_imageCtor5(self):
w = h = 10
buf = makeBuf(w,h,3)
alpha = makeBuf(w,h)
img = wx.Image(w, h, buf, alpha)
self.assertTrue(img.IsOk())
def test_imageCtor4b(self):
w = h = 10
buf = makeBuf(w,h,3)
img = wx.Image((w, h), buf)
self.assertTrue(img.IsOk())
def test_imageCtor4c(self):
w = h = 10
buf = makeBuf(w,h,3)
with self.assertRaises(ValueError):
# should be an exception here because the buffer is the wrong size
img = wx.Image((w, h+1), buf)
def test_imageCtor5b(self):
w = h = 10
buf = makeBuf(w,h,3)
alpha = makeBuf(w,h)
img = wx.Image((w, h), buf, alpha)
self.assertTrue(img.IsOk())
def test_imageCtor6(self):
img = wx.Image(pngFile, wx.BITMAP_TYPE_PNG)
self.assertTrue(img.IsOk())
def test_imageCtor7(self):
img = wx.Image(pngFile, 'image/png')
self.assertTrue(img.IsOk())
def test_imageCtor8(self):
with open(pngFile, 'rb') as f:
data = f.read()
stream = FileLikeObject(data)
img = wx.Image(stream, wx.BITMAP_TYPE_PNG)
self.assertTrue(img.IsOk())
def test_imageCtor9(self):
with open(pngFile, 'rb') as f:
data = f.read()
stream = FileLikeObject(data)
img = wx.Image(stream, 'image/png')
self.assertTrue(img.IsOk())
def test_imageSetData1(self):
w = h = 10
img = wx.Image(w,h)
buf = makeBuf(w,h,3, init=2)
img.SetData(buf)
self.assertTrue(img.IsOk())
self.assertTrue(img.GetRed(1,1) == 2)
def test_imageSetData2(self):
w = h = 10
img = wx.Image(1,1)
buf = makeBuf(w,h,3, init=2)
img.SetData(buf, w, h)
self.assertTrue(img.IsOk())
self.assertTrue(img.GetRed(1,1) == 2)
def test_imageSetAlpha1(self):
w = h = 10
img = wx.Image(w,h)
buf = makeBuf(w,h, init=2)
img.SetAlpha(buf)
self.assertTrue(img.IsOk())
self.assertTrue(img.GetRed(1,1) == 0)
self.assertTrue(img.GetAlpha(1,1) == 2)
def test_imageGetData(self):
img = wx.Image(pngFile)
data = img.GetData()
self.assertEqual(len(data), img.Width * img.Height * 3)
self.assertTrue(isinstance(data, bytearray))
def test_imageGetAlpha(self):
img = wx.Image(pngFile)
data = img.GetAlpha()
self.assertEqual(len(data), img.Width * img.Height)
self.assertTrue(isinstance(data, bytearray))
def test_imageGetDataBuffer(self):
w = h = 10
img = wx.Image(w, h)
self.assertTrue(img.IsOk())
data = img.GetDataBuffer()
self.assertTrue(isinstance(data, memoryview))
if six.PY2:
data[0] = b'1'
data[1] = b'2'
data[2] = b'3'
self.assertEqual(ord('1'), img.GetRed(0,0))
self.assertEqual(ord('2'), img.GetGreen(0,0))
self.assertEqual(ord('3'), img.GetBlue(0,0))
else:
data[0] = 1
data[1] = 2
data[2] = 3
self.assertEqual(1, img.GetRed(0,0))
self.assertEqual(2, img.GetGreen(0,0))
self.assertEqual(3, img.GetBlue(0,0))
def test_imageGetAlphaDataBuffer(self):
w = h = 10
img = wx.Image(w, h)
img.InitAlpha()
self.assertTrue(img.IsOk())
data = img.GetAlphaBuffer()
self.assertTrue(isinstance(data, memoryview))
if six.PY2:
data[0] = b'1'
data[1] = b'2'
data[2] = b'3'
self.assertEqual(ord('1'), img.GetAlpha(0,0))
self.assertEqual(ord('2'), img.GetAlpha(1,0))
self.assertEqual(ord('3'), img.GetAlpha(2,0))
else:
data[0] = 1
data[1] = 2
data[2] = 3
self.assertEqual(1, img.GetAlpha(0,0))
self.assertEqual(2, img.GetAlpha(1,0))
self.assertEqual(3, img.GetAlpha(2,0))
def test_imageSetDataBuffer1(self):
w = h = 10
img = wx.Image(w,h)
buf = makeBuf(w,h,3)
img.SetDataBuffer(buf)
buf[0] = 1
buf[1] = 2
buf[2] = 3
self.assertEqual(1, img.GetRed(0,0))
self.assertEqual(2, img.GetGreen(0,0))
self.assertEqual(3, img.GetBlue(0,0))
def test_imageSetDataBuffer2(self):
w = h = 10
img = wx.Image(1,1)
buf = makeBuf(w,h,3)
img.SetDataBuffer(buf, w, h)
buf[0] = 1
buf[1] = 2
buf[2] = 3
self.assertEqual(1, img.GetRed(0,0))
self.assertEqual(2, img.GetGreen(0,0))
self.assertEqual(3, img.GetBlue(0,0))
def test_imageSetAlphaBuffer(self):
w = h = 10
img = wx.Image(w,h)
buf = makeBuf(w,h)
img.SetAlphaBuffer(buf)
buf[0] = 1
buf[1] = 2
buf[2] = 3
self.assertEqual(1, img.GetAlpha(0,0))
self.assertEqual(2, img.GetAlpha(1,0))
self.assertEqual(3, img.GetAlpha(2,0))
def test_imageNestedClasses(self):
rgb = wx.Image.RGBValue(1,2,3)
self.assertEqual(rgb.red, 1)
self.assertEqual(rgb.green, 2)
self.assertEqual(rgb.blue, 3)
rgb.red = 4
rgb.green = 5
rgb.blue = 6
hsv = wx.Image.HSVValue(1.1, 1.2, 1.3)
self.assertEqual(hsv.hue, 1.1)
self.assertEqual(hsv.saturation, 1.2)
self.assertEqual(hsv.value, 1.3)
hsv.hue = 2.1
hsv.saturation = 2.2
hsv.value = 2.3
def test_imageRGBHSV(self):
rgb = wx.Image.RGBValue(1,2,3)
hsv = wx.Image.RGBtoHSV(rgb)
rgb = wx.Image.HSVtoRGB(hsv)
self.assertEqual(rgb.red, 1)
self.assertEqual(rgb.green, 2)
self.assertEqual(rgb.blue, 3)
def test_imageProperties(self):
img = wx.Image(pngFile)
self.assertTrue(img.IsOk())
img.Width
img.Height
img.MaskRed
img.MaskGreen
img.MaskBlue
img.Type
def test_imageMethodChain(self):
img = wx.Image(100,100).Rescale(75,75).Resize((100,100), (0,0), 40,60,80)
self.assertTrue(img.IsOk())
def test_imageOtherStuff(self):
img = wx.Image(pngFile)
self.assertTrue(img.IsOk())
r, g, b = img.FindFirstUnusedColour()
r, g, b = img.GetOrFindMaskColour()
def test_imageHandlerDerivation(self):
class TestImageHandler(wx.ImageHandler):
def __init__(self):
wx.ImageHandler.__init__(self)
self.Name = "Foo File"
self.Extension = "foo"
self.MimeType = 'image/foo'
self.Type = wx.BITMAP_TYPE_JPEG
def DoCanRead(self, stream):
return True
imghndlr = TestImageHandler()
wx.Image.AddHandler(imghndlr)
def test_imageHandlerStandardDerivations(self):
# checks that all of the standard wx derivations are available.
wx.GIFHandler()
wx.IFFHandler()
wx.JPEGHandler()
wx.PCXHandler()
wx.PNGHandler()
wx.PNMHandler()
wx.TGAHandler()
wx.TIFFHandler()
wx.XPMHandler()
def test_imageHandlerStandardDerivationsDerivation(self):
for cls in (wx.GIFHandler, wx.IFFHandler, wx.JPEGHandler,
wx.PCXHandler, wx.PNGHandler, wx.PNMHandler,
wx.TGAHandler, wx.TIFFHandler,wx.XPMHandler):
class TestImageHandler(cls):
def __init__(self):
cls.__init__(self)
ext = cls.__name__.replace("Handler", "")
self.Name = "%s File" % ext
self.Extension = ext
self.MimeType = 'image/ext'
self.Type = getattr(wx, "BITMAP_TYPE_%s" % ext)
def DoCanRead(self, stream):
return True
imghndlr = TestImageHandler()
wx.Image.AddHandler(imghndlr)
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
|