]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/BitmapFromBuffer.py
5 #----------------------------------------------------------------------
7 class TestPanel(wx
.Panel
):
8 def __init__(self
, parent
, log
):
10 wx
.Panel
.__init
__(self
, parent
, -1)
11 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
12 self
.width
, self
.height
= 120,120
14 self
.MakeBitmapRGB(self
.width
, self
.height
)
15 self
.MakeBitmapRGBA(self
.width
, self
.height
)
16 self
.MakeBitmapRGBpA(self
.width
, self
.height
)
18 def GetRGB(self
, x
, y
, bpp
):
19 # calculate some colour values for this sample based on x,y position
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
26 a
= int(x
* 255.0 / self
.width
)
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
)
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
)
45 self
.rgbBmp
= wx
.BitmapFromBuffer(width
, height
, bytes)
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
)
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
)
63 self
.rgbaBmp
= wx
.BitmapFromBufferRGBA(width
, height
, bytes)
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
)
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
)
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
)
87 def DrawBitmapAndMessage(self
, dc
, bmp
, msg
, x_
, y_
):
90 # draw some text to help show the alpha
91 dc
.SetFont(self
.GetFont())
92 while y
< y_
+ self
.height
+ 2*dc
.GetCharHeight():
94 y
+= dc
.GetCharHeight() + 5
96 # draw the bitmap over the text
97 dc
.DrawBitmap(bmp
, x
+15,y_
+15, True)
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)
110 #----------------------------------------------------------------------
112 def runTest(frame
, nb
, log
):
113 win
= TestPanel(nb
, log
)
116 #----------------------------------------------------------------------
120 overview
= """<html><body>
121 <h2><center>BitmapFromBuffer</center></h2>
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>
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.
132 <li><b>wx.BitmapFromBufferRGBA</b>(width, height, dataBuffer): Creates
133 the bitmap from a buffer containing RGBA bytes.
144 if __name__
== '__main__':
147 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])