1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#---------------------------------------------------------------------------
# Name: etg/rawbmp.py
# Author: Robin Dunn
#
# Created: 14-Aug-2012
# Copyright: (c) 2012-2020 by Total Control Software
# License: wxWindows License
#---------------------------------------------------------------------------
import etgtools
import etgtools.tweaker_tools as tools
from etgtools import (ClassDef, MethodDef, ParamDef, TypedefDef, WigCode,
CppMethodDef, PyMethodDef)
PACKAGE = "wx"
MODULE = "_core"
NAME = "rawbmp" # Base name of the file to generate to for this script
DOCSTRING = ""
# The classes and/or the basename of the Doxygen XML files to be processed by
# this script.
ITEMS = [ ]
# NOTE: It is intentional that there are no items in the ITEMS list. This is
# because we will not be loading any classes from the doxygen XML files here,
# but rather will be constructing the extractor objects here in this module
# instead.
#---------------------------------------------------------------------------
def run():
# Parse the XML file(s) building a collection of Extractor objects
module = etgtools.ModuleDef(PACKAGE, MODULE, NAME, DOCSTRING)
etgtools.parseDoxyXML(module, ITEMS)
#-----------------------------------------------------------------
# Tweak the parsed meta objects in the module object as needed for
# customizing the generated code and docstrings.
module.addHeaderCode("#include <wx/rawbmp.h>")
addPixelDataBaseClass(module)
addPixelDataClass(module, 'wxNativePixelData', 'wxBitmap', bpp=24,
doc="""\
A class providing direct access to a :class:`wx.Bitmap`'s
internal data without alpha channel (RGB).
""")
addPixelDataClass(module, 'wxAlphaPixelData', 'wxBitmap', bpp=32,
doc="""\
A class providing direct access to a :class:`wx.Bitmap`'s
internal data including the alpha channel (RGBA).
""")
#addPixelDataClass(module, 'wxImagePixelData', 'wxImage', bpp=32,
# doc="""\
# ImagePixelData: A class providing direct access to a wx.Image's
# internal data using the same api as the other PixelData classes.
# """)
#-----------------------------------------------------------------
tools.doCommonTweaks(module)
tools.runGenerators(module)
#---------------------------------------------------------------------------
def addPixelDataBaseClass(module):
# wxPixelDataBase is the common base class of the other pixel data classes
cls = ClassDef(name='wxPixelDataBase', items=[
MethodDef(
name='wxPixelDataBase', isCtor=True, protection='protected'),
MethodDef(
type='wxPoint', name='GetOrigin', isConst=True,
briefDoc="Return the origin of the area this pixel data represents."),
MethodDef(
type='int', name='GetWidth', isConst=True,
briefDoc="Return the width of the area this pixel data represents."),
MethodDef(
type='int', name='GetHeight', isConst=True,
briefDoc="Return the height of the area this pixel data represents."),
MethodDef(
type='wxSize', name='GetSize', isConst=True,
briefDoc="Return the size of the area this pixel data represents."),
MethodDef(
type='int', name='GetRowStride', isConst=True,
briefDoc="Returns the distance between the start of one row to the start of the next row."),
])
# TODO: Try to remember why I chose to do it this way instead of directly
# returning an instance of the Iterator and giving it the methods needed
# to be a Python iterator...
# TODO: Determine how much of a performance difference not using the
# PixelFacade class would make. Not using the __iter__ makes about 0.02
# seconds difference per 100x100 bmp in samples/rawbmp/rawbmp1.py...
cls.addPyMethod('__iter__', '(self)',
doc="""\
Create and return an iterator/generator object for traversing
this pixel data object.
""",
body="""\
width = self.GetWidth()
height = self.GetHeight()
pixels = self.GetPixels() # this is the C++ iterator
# This class is a facade over the pixels object (using the one
# in the enclosing scope) that only allows Get() and Set() to
# be called.
class PixelFacade(object):
def Get(self):
return pixels.Get()
def Set(self, *args, **kw):
return pixels.Set(*args, **kw)
def __str__(self):
return str(self.Get())
def __repr__(self):
return 'pixel(%d,%d): %s' % (x,y,self.Get())
X = property(lambda self: x)
Y = property(lambda self: y)
pf = PixelFacade()
for y in range(height):
pixels.MoveTo(self, 0, y)
for x in range(width):
# We always generate the same pf instance, but it
# accesses the pixels object which we use to iterate
# over the pixel buffer.
yield pf
pixels.nextPixel()
""")
module.addItem(cls)
def addPixelDataClass(module, pd, img, bpp, doc=""):
# This function creates a ClassDef for a PixelData class defined in C++.
# The C++ versions are template instantiations, so this allows us to
# create nearly identical classes and just substitute the image class
# name and the pixel data class name.
#itrName = 'Iterator'
itrName = pd + '_Accessor'
module.addHeaderCode('typedef %s::Iterator %s;' % (pd, itrName))
# First generate the class and methods for the PixelData class
cls = ClassDef(name=pd, bases=['wxPixelDataBase'], briefDoc=doc, items=[
MethodDef(name=pd, isCtor=True, items=[
ParamDef(type=img+'&', name='bmp')],
overloads=[
MethodDef(name=pd, isCtor=True, items=[
ParamDef(type=img+'&', name='bmp'),
ParamDef(type='const wxRect&', name='rect')]),
MethodDef(name=pd, isCtor=True, items=[
ParamDef(type=img+'&', name='bmp'),
ParamDef(type='const wxPoint&', name='pt' ),
ParamDef(type='const wxSize&', name='sz' )]),
]),
MethodDef(name='~'+pd, isDtor=True),
MethodDef(type=itrName, name='GetPixels', isConst=True),
CppMethodDef('int', '__nonzero__', '()', body="return (int)self->operator bool();"),
CppMethodDef('int', '__bool__', '()', body="return self->operator bool();"),
])
# add this class to the module
module.addItem(cls)
# Now do the class and methods for its C++ Iterator class
icls = ClassDef(name=itrName, items=[
# Constructors
MethodDef(name=itrName, isCtor=True, items=[
ParamDef(name='data', type=pd+'&')],
overloads=[
MethodDef(name=itrName, isCtor=True, items=[
ParamDef(name='bmp', type=img+'&'),
ParamDef(name='data', type=pd+'&')]),
MethodDef(name=itrName, isCtor=True)]),
MethodDef(name='~'+itrName, isDtor=True),
# Methods
MethodDef(type='void', name='Reset', items=[
ParamDef(type='const %s&' % pd, name='data')]),
MethodDef(type='bool', name='IsOk', isConst=True),
CppMethodDef('int', '__nonzero__', '()', body="return (int)self->IsOk();"),
CppMethodDef('int', '__bool__', '()', body="return self->IsOk();"),
MethodDef(type='void', name='Offset', items=[
ParamDef(type='const %s&' % pd, name='data'),
ParamDef(type='int', name='x'),
ParamDef(type='int', name='y')]),
MethodDef(type='void', name='OffsetX', items=[
ParamDef(type='const %s&' % pd, name='data'),
ParamDef(type='int', name='x')]),
MethodDef(type='void', name='OffsetY', items=[
ParamDef(type='const %s&' % pd, name='data'),
ParamDef(type='int', name='y')]),
MethodDef(type='void', name='MoveTo', items=[
ParamDef(type='const %s&' % pd, name='data'),
ParamDef(type='int', name='x'),
ParamDef(type='int', name='y')]),
# should this return the iterator?
CppMethodDef('void', 'nextPixel', '()', body="++(*self);"),
# NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha
# functions because I can't hide the premultiplying needed on wxMSW
# if only the individual components are wrapped, plus it would mean 3
# or 4 trips per pixel from Python to C++ instead of just one.
# Instead I'll add the Set and Get functions below and put the
# premultiplying in there.
])
assert bpp in [24, 32]
if bpp == 24:
icls.addCppMethod('void', 'Set', '(byte red, byte green, byte blue)',
body="""\
self->Red() = red;
self->Green() = green;
self->Blue() = blue;
""")
icls.addCppMethod('PyObject*', 'Get', '()',
body="""\
wxPyThreadBlocker blocker;
PyObject* rv = PyTuple_New(3);
PyTuple_SetItem(rv, 0, wxPyInt_FromLong(self->Red()));
PyTuple_SetItem(rv, 1, wxPyInt_FromLong(self->Green()));
PyTuple_SetItem(rv, 2, wxPyInt_FromLong(self->Blue()));
return rv;
""")
elif bpp == 32:
icls.addCppMethod('void', 'Set', '(byte red, byte green, byte blue, byte alpha)',
body="""\
self->Red() = wxPy_premultiply(red, alpha);
self->Green() = wxPy_premultiply(green, alpha);
self->Blue() = wxPy_premultiply(blue, alpha);
self->Alpha() = alpha;
""")
icls.addCppMethod('PyObject*', 'Get', '()',
body="""\
wxPyThreadBlocker blocker;
PyObject* rv = PyTuple_New(4);
int red = self->Red();
int green = self->Green();
int blue = self->Blue();
int alpha = self->Alpha();
PyTuple_SetItem(rv, 0, wxPyInt_FromLong( wxPy_unpremultiply(red, alpha) ));
PyTuple_SetItem(rv, 1, wxPyInt_FromLong( wxPy_unpremultiply(green, alpha) ));
PyTuple_SetItem(rv, 2, wxPyInt_FromLong( wxPy_unpremultiply(blue, alpha) ));
PyTuple_SetItem(rv, 3, wxPyInt_FromLong( alpha ));
return rv;
""")
# add it to the main pixel data class as a nested class
#cls.insertItem(0, icls)
# It's really a nested class, but we're pretending that it isn't (see the
# typedef above) so add it at the module level instead.
module.addItem(icls)
#---------------------------------------------------------------------------
if __name__ == '__main__':
run()
|