Re: getting color from gdk::pixbuf (probably a bug in Gdk::Pixbuf)



Is it possible that the get_pixels function doesn't work?
The get_pixels function returns nothing, however the png image shows up
in the Gtk2::Image.

$pixbuf = Gtk2::Gdk::Pixbuf->new_from_file(foobar.png);
$image = Gtk2::Image->new_from_pixbuf($pixbuf);
$pixels = $pixbuf->get_pixels;

The $image shows up in the window, but the $pixels string is empty.

Using the latest stable releases: Glib2 1.022   Gtk2 1.023



On 02 Mar 2004 15:02:23 -0500
muppet <scott asofyet org> wrote:

On Tue, 2004-03-02 at 06:56, Domsodi Gergely wrote:
Hi!

I have a Gdk::Pixbuf (got it from a .png file) in a Gtk2::Image.

I want to get colors of specific pixels of this image, and give it
to a Gtk2::Gdk::Color so that I can draw with that color to a
Gtk2::Gdk::Pixmap.

How can I do this?

use $pixbuf->get_pixels to fetch the rgb image data as a packed
scalar, and then use unpack() or substr() to extract the pixels you
want.  not very efficient for large pixbufs, but perl isn't meant for
bit-banging.

  #
  # warning, untested code, watch for falling bullshit
  #
  $pixels = $pixbuf->get_pixels;

  # n_channels should be 3 for rgb data, and 4 if there is alpha data.
  $depth = $pixbuf->get_n_channels;
  $offset = $row * $pixbuf->get_rowstride + $col * $depth;

  ($r, $g, $b, $a) = unpack "C*", 
                            substr $pixels, $offset, $depth;
  # $a will be undefined if $depth is 3.
  # $[rgb] will be on the range [0-255] --- bitshift them left by 8
  # for use with a Gtk2::Gdk::Color (to put them in the range
  # [0-65535]).


-- 
muppet <scott at asofyet dot org>

_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list




-- 
Domsodi Gergely / UHU-Linux



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