]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/BitmapFromBuffer.py
2f72f6a02e1cd1b52547715a4305e3274daa2c41
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
)
38 # return the offset into the bytes array for the start of
40 return y
*width
*bpp
+ x
*bpp
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)
50 self
.rgbBmp
= wx
.BitmapFromBuffer(width
, height
, bytes)
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
)
60 # return the offset into the bytes array for the start of
62 return y
*width
*bpp
+ x
*bpp
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)
73 self
.rgbaBmp
= wx
.BitmapFromBufferRGBA(width
, height
, bytes)
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
80 bytes = array
.array('B', [1,2,3] * width
*height
)#*bpp)
83 # return the offset into the bytes array for the start of
85 return y
*width
*bpp
+ x
*bpp
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)
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
)
102 def DrawBitmapAndMessage(self
, dc
, bmp
, msg
, x_
, y_
):
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
111 # draw the bitmap over the text
112 dc
.DrawBitmap(bmp
, x
+15,y_
+15, True)
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)
125 #----------------------------------------------------------------------
127 def runTest(frame
, nb
, log
):
128 win
= TestPanel(nb
, log
)
131 #----------------------------------------------------------------------
135 overview
= """<html><body>
136 <h2><center>BitmapFromBuffer</center></h2>
138 Two new wx.Bitmap factory functions allow you to create a wx.Bitmap
139 directly from a data buffer. The the buffer can be any Python object
140 that implements the buffer interface, or is convertable to a buffer,
141 such as a string or an array. The new functions are: <ul>
143 <li><b>wx.BitmapFromBuffer</b>(width, height, dataBuffer, alphaBuffer=None):
144 Creates the bitmap from a buffer of RGB bytes, optionally with a separate
145 buffer of alpha bytes.
147 <li><b>wx.BitmapFromBufferRGBA</b>(width, height, dataBuffer): Creates
148 the bitmap from a buffer containing RGBA bytes.
159 if __name__
== '__main__':
162 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])