Re: Adding alpha channel to existing GdkPixbuf



On Dec 01, 1999, Havoc Pennington <hp@redhat.com> wrote:
> 
> On Wed, 1 Dec 1999, John R. Sheets wrote:
> > Am I out of my gourd?  The problem is that some of the PNG graphics I'm
> > loading into my GdkPixbuf's weren't created with alpha channels, while
> > some were.  
> 
> That's because some PNGs have an alpha channel and some don't. :-)

Yep!  That would be the problem.  (c:

> However, it is really broken to do it this way; what you should do is open
> Gimp, change the background color to transparent, and save the file. ;-)

I agree completely.  The "media department" of the project
(www.worldforge.org, a massively networked RPG) created the graphics for
any game client to use.  Quite an impressive assortment, incidentally.
I learned this morning that they originally created graphics with only
3-channels because the first client app didn't support alpha channels.
Argh.  Eventually, I suppose the graphics will all get converted, but in
the short term, I'd like to be able to hack something together so I
don't have all these big black rectangles behind half of my images....
Close to a thousand images in all (600 of which are 3-channel), so it
won't be an easy task.

> >   int rowstride = 4 * pixbuf->width;
> > 
> >   buf = g_new (guchar, rowstride * pixbuf->height);
> >
> 
> Note that gdk_pixbuf_new() will do this for you and also pick a fast
> word-aligned rowstride.

That's a good idea...I wasn't too confident about that rowstride
calculation.  Definitely an over-hasty hack.

> >     for (col = 0; col < pixbuf->width; col++)
> >     {
> >       *dest++ = *src++;
> >       *dest++ = *src++;
> >       *dest++ = *src++;
> >       *dest++ = CalculateAlphaValue(...);    
> >     }
> 
> Probably worth avoiding a function call in this loop.

Yes, very much so!  I cringed when I added that into the example.  It
was only for pseudocode purposes, to keep from cluttering up my posted
code sample with color comparisons.  To be honest, it was really a C++
method I cleaned up a bit for easy reading.

Thanks, I'll see about creating a second 4-channel GdkPixbuf and
transferring the image to that, rather than monkeying with the internals
of the first one.  Lots cleaner.

John



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]