Re: Adding alpha channel to existing GdkPixbuf




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. :-)

> I'd like for all of them to have alpha channels so I can
> mask out their backgrounds.  Fortunately, the maskable backgrounds all
> have a solid color, so I can at least detect it.
> 

You just create a new blank pixbuf with alpha channel, then iterate over
the pixels and copy in the corresponding stuff from the old pixbuf, adding
alpha as you like.

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. ;-)

>   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.
 
>   for (row = 0; row < pixbuf->height; row++)
>   {
>     src = pixbuf->pixels;
>     dest = buf + rowstride * row;
> 

Here you're assuming that rowstride = width * n_channels, which is not
necessarily the case; you only know that rowstride >= width * n_channels.

>     for (col = 0; col < pixbuf->width; col++)
>     {
>       *dest++ = *src++;
>       *dest++ = *src++;
>       *dest++ = *src++;
>       *dest++ = CalculateAlphaValue(...);    
>     }

Probably worth avoiding a function call in this loop.

>   }
> 
>   pixbuf->n_channels = 4;
>   pixbuf->has_alpha = TRUE;
> }

What happened to "buf"? You probably want to put it in a new pixbuf!

Havoc




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