]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/RawBitmapAccess.py
Add raw bitmap access sample to the demo
[wxWidgets.git] / wxPython / demo / RawBitmapAccess.py
1
2 import wx
3
4 #----------------------------------------------------------------------
5
6 class TestPanel(wx.Panel):
7 def __init__(self, parent, log):
8 self.log = log
9 wx.Panel.__init__(self, parent, -1)
10 self.Bind(wx.EVT_PAINT, self.OnPaint)
11
12 self.redBmp = self.MakeBitmap(188, 143, 234)
13 self.greenBmp = self.MakeBitmap(35, 142, 35)
14 self.blueBmp = self.MakeBitmap(50, 153, 204)
15
16 def MakeBitmap(self, red, green, blue, alpha=128):
17 bmp = wx.EmptyBitmap(100, 100, 32)
18
19 # Create an object that facilitates access to the bitmap's
20 # pixel buffer
21 pixelData = wx.AlphaPixelData(bmp)
22 if not pixelData:
23 raise RuntimeError("Failed to gain raw access to bitmap data.")
24
25 # We have two ways to access each pixel, first we'll use an
26 # iterator to set every pixel to the colour and alpha values
27 # passed in.
28 for pixel in pixelData:
29 pixel.Set(red, green, blue, alpha)
30
31 # Next we'll use the pixel accessor to draw a border
32 pixels = pixelData.GetPixels()
33 for x in xrange(100):
34 pixels.MoveTo(pixelData, x, 0)
35 pixels.Set(red, green, blue, wx.ALPHA_OPAQUE)
36 pixels.MoveTo(pixelData, x, 99)
37 pixels.Set(red, green, blue, wx.ALPHA_OPAQUE)
38 for y in xrange(100):
39 pixels.MoveTo(pixelData, 0, y)
40 pixels.Set(red, green, blue, wx.ALPHA_OPAQUE)
41 pixels.MoveTo(pixelData, 99, y)
42 pixels.Set(red, green, blue, wx.ALPHA_OPAQUE)
43
44 return bmp
45
46
47 def OnPaint(self, evt):
48 dc = wx.PaintDC(self)
49 dc.DrawBitmap(self.redBmp, 50, 50, True)
50 dc.DrawBitmap(self.greenBmp, 110, 110, True)
51 dc.DrawBitmap(self.blueBmp, 170, 50, True)
52
53 #----------------------------------------------------------------------
54
55 def runTest(frame, nb, log):
56 win = TestPanel(nb, log)
57 return win
58
59 #----------------------------------------------------------------------
60
61
62
63 overview = """<html><body>
64 <h2><center>Raw Bitmap Access</center></h2>
65
66 wx.NativePixelData and wx.AlphaPixelData provide a cross-platform way
67 to access the platform-specific pixel buffer within a wx.Bitmap. They
68 provide both a random access method, and an iterator interface.
69
70 </body></html>
71 """
72
73
74
75 if __name__ == '__main__':
76 import sys,os
77 import run
78 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
79