]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/BitmapFromBuffer.py
Include "xrc" support properly for OpenVMS
[wxWidgets.git] / wxPython / demo / BitmapFromBuffer.py
1
2 import wx
3 import array
4
5 #----------------------------------------------------------------------
6
7 class 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 for y in xrange(height):
38 for x in xrange(width):
39 offset = y*width*bpp + x*bpp
40 r,g,b = self.GetRGB(x, y, bpp)
41 bytes[offset + 0] = r
42 bytes[offset + 1] = g
43 bytes[offset + 2] = b
44
45 self.rgbBmp = wx.BitmapFromBuffer(width, height, bytes)
46
47
48
49 def MakeBitmapRGBA(self, width, height):
50 # Make a bitmap using an array of RGBA bytes
51 bpp = 4 # bytes per pixel
52 bytes = array.array('B', [0] * width*height*bpp)
53
54 for y in xrange(height):
55 for x in xrange(width):
56 offset = y*width*bpp + x*bpp
57 r,g,b,a = self.GetRGB(x, y, bpp)
58 bytes[offset + 0] = r
59 bytes[offset + 1] = g
60 bytes[offset + 2] = b
61 bytes[offset + 3] = a
62
63 self.rgbaBmp = wx.BitmapFromBufferRGBA(width, height, bytes)
64
65
66 def MakeBitmapRGBpA(self, width, height):
67 # Make a bitmap using an array of RGB bytes plus a separate
68 # buffer for the alpha channel
69 bpp = 3 # bytes per pixel
70 bytes = array.array('B', [0] * width*height*bpp)
71
72 for y in xrange(height):
73 for x in xrange(width):
74 offset = y*width*bpp + x*bpp
75 r,g,b = self.GetRGB(x, y, bpp)
76 bytes[offset + 0] = r
77 bytes[offset + 1] = g
78 bytes[offset + 2] = b
79
80 # just use an alpha buffer with a constant alpha value for all
81 # pixels for this example, it could just as easily have
82 # varying alpha values like the other sample.
83 alpha = array.array('B', [128]*width*height)
84 self.rgbaBmp2 = wx.BitmapFromBuffer(width, height, bytes, alpha)
85
86
87 def DrawBitmapAndMessage(self, dc, bmp, msg, x_, y_):
88 x, y = x_, y_
89
90 # draw some text to help show the alpha
91 dc.SetFont(self.GetFont())
92 while y < y_ + self.height + 2*dc.GetCharHeight():
93 dc.DrawText(msg, x,y)
94 y += dc.GetCharHeight() + 5
95
96 # draw the bitmap over the text
97 dc.DrawBitmap(bmp, x+15,y_+15, True)
98
99
100 def OnPaint(self, evt):
101 dc = wx.PaintDC(self)
102 self.DrawBitmapAndMessage(dc, self.rgbBmp, "No alpha channel in this image", 30,35)
103 self.DrawBitmapAndMessage(dc, self.rgbaBmp, "This image has some alpha", 325,35)
104 self.DrawBitmapAndMessage(dc, self.rgbaBmp2,"This one made with RGB+A", 180,220)
105
106
107
108
109
110 #----------------------------------------------------------------------
111
112 def runTest(frame, nb, log):
113 win = TestPanel(nb, log)
114 return win
115
116 #----------------------------------------------------------------------
117
118
119
120 overview = """<html><body>
121 <h2><center>BitmapFromBuffer</center></h2>
122
123 Two new wx.Bitmap factory functions allow you to create a wx.Bitmap
124 directly from a data buffer. The the buffer can be any Python object
125 that implements the buffer interface, or is convertable to a buffer,
126 such as a string or an array. The new functions are: <ul>
127
128 <li><b>wx.BitmapFromBuffer</b>(width, height, dataBuffer, alphaBuffer=None):
129 Creates the bitmap from a buffer of RGB bytes, optionally with a separate
130 buffer of alpha bytes.
131
132 <li><b>wx.BitmapFromBufferRGBA</b>(width, height, dataBuffer): Creates
133 the bitmap from a buffer containing RGBA bytes.
134
135 </ul>
136
137
138
139 </body></html>
140 """
141
142
143
144 if __name__ == '__main__':
145 import sys,os
146 import run
147 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
148