]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/BitmapFromBuffer.py
Add raw bitmap access sample to the demo
[wxWidgets.git] / wxPython / demo / BitmapFromBuffer.py
CommitLineData
0dcc6f22
RD
1
2import wx
3import array
4
5#----------------------------------------------------------------------
6
7class TestPanel(wx.Panel):
8 def __init__(self, parent, log):
9 self.log = log
10 wx.Panel.__init__(self, parent, -1)
11 self.Bind(wx.EVT_PAINT, self.OnPaint)
12 self.width, self.height = 120,120
13
14 self.MakeBitmapRGB(self.width, self.height)
15 self.MakeBitmapRGBA(self.width, self.height)
16 self.MakeBitmapRGBpA(self.width, self.height)
17
18 def GetRGB(self, x, y, bpp):
19 # calculate some colour values for this sample based on x,y position
20 r = g = b = 0
21 if y < self.height/3: r = 255
22 if y >= self.height/3 and y <= 2*self.height/3: g = 255
23 if y > 2*self.height/3: b = 255
24
25 if bpp == 4:
26 a = int(x * 255.0 / self.width)
27 return r, g, b, a
28 else:
29 return r, g, b
30
31
32 def MakeBitmapRGB(self, width, height):
33 # Make a bitmap using an array of RGB bytes
34 bpp = 3 # bytes per pixel
35 bytes = array.array('B', [0] * width*height*bpp)
36
37 def offset(x, y):
38 # return the offset into the bytes array for the start of
39 # the x,y pixel
40 return y*width*bpp + x*bpp
41
42 for y in xrange(height):
43 for x in xrange(width):
44 r,g,b = self.GetRGB(x, y, bpp)
45 bytes[offset(x, y) + 0] = r
46 bytes[offset(x, y) + 1] = g
47 bytes[offset(x, y) + 2] = b
48 ##print (x, y), offset(x, y), (r, g, b)
49
50 self.rgbBmp = wx.BitmapFromBuffer(width, height, bytes)
51
52
53
54 def MakeBitmapRGBA(self, width, height):
55 # Make a bitmap using an array of RGBA bytes
56 bpp = 4 # bytes per pixel
57 bytes = array.array('B', [0] * width*height*bpp)
58
59 def offset(x, y):
60 # return the offset into the bytes array for the start of
61 # the x,y pixel
62 return y*width*bpp + x*bpp
63
64 for y in xrange(height):
65 for x in xrange(width):
66 r,g,b,a = self.GetRGB(x, y, bpp)
67 bytes[offset(x, y) + 0] = r
68 bytes[offset(x, y) + 1] = g
69 bytes[offset(x, y) + 2] = b
70 bytes[offset(x, y) + 3] = a
71 ##print (x, y), offset(x, y), (r, g, b, a)
72
73 self.rgbaBmp = wx.BitmapFromBufferRGBA(width, height, bytes)
74
75
76 def MakeBitmapRGBpA(self, width, height):
77 # Make a bitmap using an array of RGB bytes plus a separate
78 # buffer for the alpha channel
79 bpp = 3 # bytes per pixel
ecc0e221 80 bytes = array.array('B', [0] * width*height*bpp)
0dcc6f22
RD
81
82 def offset(x, y):
83 # return the offset into the bytes array for the start of
84 # the x,y pixel
85 return y*width*bpp + x*bpp
86
87 for y in xrange(height):
88 for x in xrange(width):
89 r,g,b = self.GetRGB(x, y, bpp)
90 bytes[offset(x, y) + 0] = r
91 bytes[offset(x, y) + 1] = g
92 bytes[offset(x, y) + 2] = b
93 ##print (x, y), offset(x, y), (r, g, b)
94
95 # just use an alpha buffer with a constant alpha value for all
96 # pixels for this example, it could just as easily have
97 # varying alpha values like the other sample.
98 alpha = array.array('B', [128]*width*height)
99 self.rgbaBmp2 = wx.BitmapFromBuffer(width, height, bytes, alpha)
100
101
102 def DrawBitmapAndMessage(self, dc, bmp, msg, x_, y_):
103 x, y = x_, y_
104
105 # draw some text to help show the alpha
106 dc.SetFont(self.GetFont())
107 while y < y_ + self.height + 2*dc.GetCharHeight():
108 dc.DrawText(msg, x,y)
109 y += dc.GetCharHeight() + 5
110
111 # draw the bitmap over the text
112 dc.DrawBitmap(bmp, x+15,y_+15, True)
113
114
115 def OnPaint(self, evt):
116 dc = wx.PaintDC(self)
117 self.DrawBitmapAndMessage(dc, self.rgbBmp, "No alpha channel in this image", 30,35)
118 self.DrawBitmapAndMessage(dc, self.rgbaBmp, "This image has some alpha", 325,35)
119 self.DrawBitmapAndMessage(dc, self.rgbaBmp2,"This one made with RGB+A", 180,220)
120
121
122
123
124
125#----------------------------------------------------------------------
126
127def runTest(frame, nb, log):
128 win = TestPanel(nb, log)
129 return win
130
131#----------------------------------------------------------------------
132
133
134
135overview = """<html><body>
136<h2><center>BitmapFromBuffer</center></h2>
137
138Two new wx.Bitmap factory functions allow you to create a wx.Bitmap
139directly from a data buffer. The the buffer can be any Python object
140that implements the buffer interface, or is convertable to a buffer,
141such as a string or an array. The new functions are: <ul>
142
143<li><b>wx.BitmapFromBuffer</b>(width, height, dataBuffer, alphaBuffer=None):
144Creates the bitmap from a buffer of RGB bytes, optionally with a separate
145buffer of alpha bytes.
146
147<li><b>wx.BitmapFromBufferRGBA</b>(width, height, dataBuffer): Creates
148the bitmap from a buffer containing RGBA bytes.
149
150</ul>
151
152
153
154</body></html>
155"""
156
157
158
159if __name__ == '__main__':
160 import sys,os
161 import run
162 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
163