[cairo] Optimizing code
Gerdus van Zyl
gerdusvanzyl at gmail.com
Tue Sep 4 08:57:03 PDT 2007
Firstly the call to load the png is expensive and can be cached on
first load. Secondly can the entire background not be stored and
simply painted on each paint call?
Thirdly, what are you trying to do? As i understand, you have a gray
background that is overlaid with the png and then you tint the entire
surface newcolor.
Can this not be accomplished using: (top of my head not tested to work)
png = cairo.ImageSurface.create_from_png(path)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, png.get_width(),
png.get_height()
ctx = cairo.Context (surface)
ctx.set_source_rgb(97/255.0, 97/255.0, 97/255.0)
ctx.paint() #fill surface gray
ctx.set_source_surface(png,0,0)
ctx.paint()
ctx.set_source_rgb(newcolor[0],newcolor[1],newcolor[2])
ctx.paint_with_alpha(0.5) #50%
or use ctx.set_operator
~Gerdus van Zyl
>
> On 04/09/07, Thomas Dybdahl Ahle <lobais at gmail.com> wrote:
> > Hi, I draw a background in an application, which is based partly on
> > get_style().dark[gtk.STATE_NORMAL], partly on (97, 97, 97) and partly on
> > a black/white/transparant png.
> >
> > I merge them together with the code:
> >
> >
> > newcolor = self.get_style().dark[gtk.STATE_NORMAL]
> > newcolor = (color.blue/256, color.green/256, color.red/256)
> >
> > # Get mostly transparant shadowy image
> > surface = cairo.ImageSurface.create_from_png(path)
> >
> > # Clone surface on gray background
> > data = array ('B', 'a' * surface.get_width() * surface.get_height() * 4)
> > surf = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_ARGB32,
> > surface.get_width(), surface.get_height(), surface.get_stride())
> > ctx = cairo.Context (surf)
> > ctx.rectangle (0, 0, surface.get_width(), surface.get_height())
> > ctx.set_source_surface(surface, 0, 0)
> > ctx.fill()
> >
> > # Add 'newcolor' to all (non aplha) pixels and divide by three
> > for pixel in xrange(0, len(data), 4):
> > for color in xrange(3):
> > data[pixel+color] = (newcolor[color] + data[pixel+color]) /3
> >
> > # Make new surface from data
> > self.surface = cairo.ImageSurface.create_for_data (
> > data, cairo.FORMAT_ARGB32,
> > surface.get_width(), surface.get_height(), surface.get_stride())
> >
> >
> > But it works kinda slow.
> > I know it's python and it's supposed to be a little slow, but I was
> > wondering if there was something on the cairo front I could do smarter?
> >
> >
> > --
> > Med venlig hilsen,
> > Best regards,
> > Thomas
> >
> > _______________________________________________
> > cairo mailing list
> > cairo at cairographics.org
> > http://lists.cairographics.org/mailman/listinfo/cairo
> >
>
More information about the cairo
mailing list