From: Robin Dunn Date: Sun, 27 Aug 2006 09:11:47 +0000 (+0000) Subject: Add raw bitmap access sample to the demo X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ecc0e221ee47e8be9fe367bd68ec0e53bea1142e Add raw bitmap access sample to the demo git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40860 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/wxPython/demo/BitmapFromBuffer.py b/wxPython/demo/BitmapFromBuffer.py index 2f72f6a02e..4fd8bae649 100644 --- a/wxPython/demo/BitmapFromBuffer.py +++ b/wxPython/demo/BitmapFromBuffer.py @@ -77,7 +77,7 @@ class TestPanel(wx.Panel): # Make a bitmap using an array of RGB bytes plus a separate # buffer for the alpha channel bpp = 3 # bytes per pixel - bytes = array.array('B', [1,2,3] * width*height)#*bpp) + bytes = array.array('B', [0] * width*height*bpp) def offset(x, y): # return the offset into the bytes array for the start of diff --git a/wxPython/demo/Main.py b/wxPython/demo/Main.py index b67361889d..be75f8c97c 100644 --- a/wxPython/demo/Main.py +++ b/wxPython/demo/Main.py @@ -50,7 +50,6 @@ _treeList = [ 'AnalogClock', 'AUI_DockingWindowMgr', 'AUI_Notebook', - 'BitmapFromBuffer', 'CheckListCtrlMixin', 'ComboTreeBox', 'Pickers', @@ -58,6 +57,8 @@ _treeList = [ 'RichTextCtrl', 'Treebook', 'Toolbook', + 'BitmapFromBuffer', + 'RawBitmapAccess', ]), # managed windows == things with a (optional) caption you can close @@ -239,6 +240,7 @@ _treeList = [ 'ImageAlpha', 'ImageFromStream', 'Mask', + 'RawBitmapAccess', 'Throbber', ]), diff --git a/wxPython/demo/RawBitmapAccess.py b/wxPython/demo/RawBitmapAccess.py new file mode 100644 index 0000000000..2e0e57d384 --- /dev/null +++ b/wxPython/demo/RawBitmapAccess.py @@ -0,0 +1,79 @@ + +import wx + +#---------------------------------------------------------------------- + +class TestPanel(wx.Panel): + def __init__(self, parent, log): + self.log = log + wx.Panel.__init__(self, parent, -1) + self.Bind(wx.EVT_PAINT, self.OnPaint) + + self.redBmp = self.MakeBitmap(188, 143, 234) + self.greenBmp = self.MakeBitmap(35, 142, 35) + self.blueBmp = self.MakeBitmap(50, 153, 204) + + def MakeBitmap(self, red, green, blue, alpha=128): + bmp = wx.EmptyBitmap(100, 100, 32) + + # Create an object that facilitates access to the bitmap's + # pixel buffer + pixelData = wx.AlphaPixelData(bmp) + if not pixelData: + raise RuntimeError("Failed to gain raw access to bitmap data.") + + # We have two ways to access each pixel, first we'll use an + # iterator to set every pixel to the colour and alpha values + # passed in. + for pixel in pixelData: + pixel.Set(red, green, blue, alpha) + + # Next we'll use the pixel accessor to draw a border + pixels = pixelData.GetPixels() + for x in xrange(100): + pixels.MoveTo(pixelData, x, 0) + pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) + pixels.MoveTo(pixelData, x, 99) + pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) + for y in xrange(100): + pixels.MoveTo(pixelData, 0, y) + pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) + pixels.MoveTo(pixelData, 99, y) + pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) + + return bmp + + + def OnPaint(self, evt): + dc = wx.PaintDC(self) + dc.DrawBitmap(self.redBmp, 50, 50, True) + dc.DrawBitmap(self.greenBmp, 110, 110, True) + dc.DrawBitmap(self.blueBmp, 170, 50, True) + +#---------------------------------------------------------------------- + +def runTest(frame, nb, log): + win = TestPanel(nb, log) + return win + +#---------------------------------------------------------------------- + + + +overview = """ +

Raw Bitmap Access

+ +wx.NativePixelData and wx.AlphaPixelData provide a cross-platform way +to access the platform-specific pixel buffer within a wx.Bitmap. They +provide both a random access method, and an iterator interface. + + +""" + + + +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) +