]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/RawBitmapAccess.py
   3 # use the numpy code instead of the raw access code for comparison 
   6 # time the execution of making a bitmap? 
   9 # how big to make the bitmaps 
  12 #---------------------------------------------------------------------- 
  13 # attempt to import a numeric module if requested to 
  18         def makeByteArray(shape
): 
  19             return numpy
.empty(shape
, numpy
.uint8
) 
  24             def makeByteArray(shape
): 
  25                 arr 
=  numarray
.array(shape
=shape
, typecode
='u1') 
  33 #---------------------------------------------------------------------- 
  35 class TestPanel(wx
.Panel
): 
  36     def __init__(self
, parent
, log
): 
  38         wx
.Panel
.__init
__(self
, parent
, -1) 
  39         self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
) 
  43             timeit
.s 
= self 
# Put self in timeit's global namespace as 
  44                             # 's' so it can be found in the code 
  45                             # snippets being tested. 
  47                 t 
= timeit
.Timer("bmp = s.MakeBitmap(10, 20, 30)") 
  49                 t 
= timeit
.Timer("bmp = s.MakeBitmap2(10, 20, 30)") 
  50             log
.write("Timing...\n") 
  53             log
.write("%d passes in %f seconds ==  %f seconds per pass " %  
  57             log
.write("using raw access\n") 
  58             self
.redBmp   
= self
.MakeBitmap(178,  34,  34) 
  59             self
.greenBmp 
= self
.MakeBitmap( 35, 142,  35) 
  60             self
.blueBmp  
= self
.MakeBitmap(  0,   0, 139) 
  62             log
.write("using %s\n" % numtype
) 
  63             self
.redBmp   
= self
.MakeBitmap2(178,  34,  34) 
  64             self
.greenBmp 
= self
.MakeBitmap2( 35, 142,  35) 
  65             self
.blueBmp  
= self
.MakeBitmap2(  0,   0, 139) 
  68     def OnPaint(self
, evt
): 
  70         dc
.DrawBitmap(self
.redBmp
,    50,  50, True) 
  71         dc
.DrawBitmap(self
.greenBmp
, 110, 110, True) 
  72         dc
.DrawBitmap(self
.blueBmp
,  170,  50, True) 
  75     def MakeBitmap(self
, red
, green
, blue
, alpha
=128): 
  76         # Create the bitmap that we will stuff pixel values into using 
  77         # the raw bitmap access classes. 
  78         bmp 
= wx
.EmptyBitmap(DIM
, DIM
, 32) 
  80         # Create an object that facilitates access to the bitmap's 
  82         pixelData 
= wx
.AlphaPixelData(bmp
) 
  84             raise RuntimeError("Failed to gain raw access to bitmap data.") 
  86         # We have two ways to access each pixel, first we'll use an 
  87         # iterator to set every pixel to the colour and alpha values 
  89         for pixel 
in pixelData
: 
  90             pixel
.Set(red
, green
, blue
, alpha
) 
  92         # This block of code is another way to do the same as above, 
  93         # but with the accessor interface instead of the Python 
  94         # iterator.  It is a bit faster than the above because it 
  95         # avoids the iterator/generator magic, but it is not nearly as 
  97         #pixels = pixelData.GetPixels() 
  98         #for y in xrange(DIM): 
  99         #    for x in xrange(DIM): 
 100         #        pixels.Set(red, green, blue, alpha) 
 102         #    pixels.MoveTo(pixelData, 0, y) 
 105         # Next we'll use the pixel accessor to set the border pixels 
 107         pixels 
= pixelData
.GetPixels() 
 108         for x 
in xrange(DIM
): 
 109             pixels
.MoveTo(pixelData
, x
, 0) 
 110             pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
) 
 111             pixels
.MoveTo(pixelData
, x
, DIM
-1) 
 112             pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
) 
 113         for y 
in xrange(DIM
): 
 114             pixels
.MoveTo(pixelData
, 0, y
) 
 115             pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
) 
 116             pixels
.MoveTo(pixelData
, DIM
-1, y
) 
 117             pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
) 
 122     def MakeBitmap2(self
, red
, green
, blue
, alpha
=128): 
 123         # Make an array of bytes that is DIM*DIM in size, with enough 
 124         # slots for each pixel to have a RGB and A value 
 125         arr 
= makeByteArray( (DIM
,DIM
, 4) ) 
 127         # just some indexes to keep track of which byte is which 
 128         R
, G
, B
, A 
= range(4) 
 130         # initialize all pixel values to the values passed in 
 136         # Set the alpha for the border pixels to be fully opaque 
 137         arr
[0,     0:DIM
, A
] = wx
.ALPHA_OPAQUE  
# first row 
 138         arr
[DIM
-1, 0:DIM
, A
] = wx
.ALPHA_OPAQUE  
# last row 
 139         arr
[0:DIM
, 0,     A
] = wx
.ALPHA_OPAQUE  
# first col 
 140         arr
[0:DIM
, DIM
-1, A
] = wx
.ALPHA_OPAQUE  
# last col 
 142         # finally, use the array to create a bitmap 
 143         bmp 
= wx
.BitmapFromBufferRGBA(DIM
, DIM
, arr
) 
 148 #---------------------------------------------------------------------- 
 150 def runTest(frame
, nb
, log
): 
 151     win 
= TestPanel(nb
, log
) 
 154 #---------------------------------------------------------------------- 
 158 overview 
= """<html><body> 
 159 <h2><center>Raw Bitmap Access</center></h2> 
 161 wx.NativePixelData and wx.AlphaPixelData provide a cross-platform way 
 162 to access the platform-specific pixel buffer within a wx.Bitmap.  They 
 163 provide both a random access method, and an iterator interface. 
 165 <p>Unfortunately, although these classes are convienient ways to access 
 166 and update the contents of a wx.Bitmap, we lose most of the efficiency 
 167 of the C++ classes by requiring one or more Python-to-C++ transitions 
 168 for each pixel.  In fact it can be <b>much</b> slower than the other 
 169 ways of creating a bitmap from scratch, especially now that 
 170 wx.BitmapFromBuffer exists and can save the time needed to copy from a 
 173 <p>To see this difference for yourself this module has been 
 174 instrumented to allow you to experiment with using either the raw 
 175 access or numpy/numarray, and also to time how long it takes to create 
 176 100 bitmaps like you see on the screen.  Simply edit this module in 
 177 the \"Demo Code\" tab and set TIMEIT to True and then watch 
 178 the log window when the sample is reloaded.  To try numpy or numarray 
 179 (if you have them installed) then set USE_NUMPY to True as well, and 
 180 watch the log window again. On my machines there is about <b>an 
 181 order of magnitude</b> difference between the raw access functions 
 182 and using a numarray.array with wx.BitmapFromBufferRGBA!  Almost 
 183 another order of magnitude improvement can be gained with using the 
 191 if __name__ 
== '__main__': 
 194     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])