Re: GtkLabel wrapping, GdkPixbuf pixels



On Thu, 2002-06-20 at 15:21, Trevor Spiteri wrote:

2) How do I get a pixel from a a GdkPixbuf? I need to get the color from
a pixel in a .png file and use it to create a pango attribute using
pango_attr_foreground_new(guint16 red, guint16 green, guint16 blue).

Well, something like this would[1] do it.

void
get_pixel_value (GdkPixbuf *pb, int x, int y, int *r, int *g, int *b)
{
        guchar *pixels;
        int rowstride, offset;
        gboolean has_alpha;

        rowstride = gdk_pixbuf_get_rowstride (pb);
        pixels = gdk_pixbuf_get_pixels (pb);
        has_alpha = gdk_pixbuf_get_has_alpha (pb);

        offset = y * rowstride + (x * (has_alpha ? 4 : 3));

        *r = pixels [offset];
        *g = pixels [offset + 1];
        *b = pixels [offset + 2];
        /* If has_alpha == TRUE, then the alpha component is in
           pixels [offset + 3] */
}

It's totally unoptimised, but it should work.
iain

[1] Would, should, I've only been awake for 5 minutes, but it's the
general idea.
-- 
"I think what we don't need is this worldwide apathy where people wait
for a leader. It shouldn't be like that, especially after Spetember 11.
England feels a bit like Germany before Adolf Hitler came along. There's
more unemployment, more crime and people are wondering, `Who is going to
get us out of this shit?` It's very dangerous." -- Alec Empire




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