Adding alpha channel to existing GdkPixbuf



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

Anyway, does anyone have any tips on how to go about this?  Does libart
have any convenience functions?  I didn't see any, and I suspect the
worse, since I noticed the static remove_alpha() function in
gdk-pixbuf-render.c.  I tried pulling a similar trick, but it didn't
seem to work.  It looks something like this:

void ConvertToAlpha (GdkPixbuf *pixbuf)
{
  guchar *buf;
  int row, col;
  guchar *src, *dest;

  int rowstride = 4 * pixbuf->width;

  buf = g_new (guchar, rowstride * pixbuf->height);

  for (row = 0; row < pixbuf->height; row++)
  {
    src = pixbuf->pixels;
    dest = buf + rowstride * row;

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

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

Thanks,
John



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