You don't need to convert to the xpm format, the pixbuf-loader will auto-detect a wide variety of image types and load them into a pixbuf. Here is a simple example, (I don't use IM, but it is essentially the same data from a scalar), the base64encoded image is a small jpg.
#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';
use MIME::Base64;
my $window = Gtk2::Window->new('toplevel');
$window ->signal_connect( 'destroy' => \&delete_event );
$window->set_border_width(10);
$window->set_default_size(500,430);
my $vbox = Gtk2::VBox->new( FALSE, 6 );
$window->add($vbox);
$vbox->set_border_width(2);
my $hbox= Gtk2::HBox->new( FALSE, 6 );
$vbox->pack_end($hbox,FALSE,FALSE,0);
$hbox->set_border_width(2);
$hbox->set_size_request(500,48);
$vbox->pack_end (Gtk2::HSeparator->new, FALSE, FALSE, 0);
my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );
my $button1 = Gtk2::Button->new('Set BG');
$hbox->pack_end( $button1, FALSE, FALSE, 0 );
$button1->signal_connect( clicked => \&set_bg);
my $vbox1 = Gtk2::VBox->new( 0, 5 );
$vbox->pack_end ($vbox1, TRUE, TRUE, 0);
$window->show_all();
Gtk2->main;
#####################################
sub delete_event {
Gtk2->main_quit;
return FALSE;
}
###############################################################
sub set_bg{
#print "@_\n";
# get the blob, I didn't use IM here, but it comes from
# a scalar
my $bunny = get_bunny();
# crude method
#my $pixbufloader = Gtk2::Gdk::PixbufLoader->new;
#my $raw_data = `cat ./bridget-1.jpg`;
#$pixbufloader->write($raw_data);
#$pixbufloader->close;
#my $pixbuf = $pixbufloader->get_pixbuf;
# preferred method
# this properly renders it, save this do statement for future use :-)
my $pixbuf = do {
my $loader = Gtk2::Gdk::PixbufLoader->new();
$loader->write( $bunny );
$loader->close();
$loader->get_pixbuf();
};
# get dimensions if desired
my ($x, $y) = ($pixbuf->get_width, $pixbuf->get_height);
#print "$x $y\n";
#print 'rowstride->', $pixbuf->get_rowstride,"\n";
#set the new pixbuff in the window
# make image
my $img = Gtk2::Image->new_from_pixbuf($pixbuf);
$vbox1->pack_start($img,FALSE,FALSE,0);
$img->show();
#return FALSE;
}
#################################################################
sub get_bunny{
return decode_base64(
'iVBORw0KGgoAAAANSUhEUgAAAB4AAAAjCAYAAACD1LrRAAAABmJLR0QA/wD/AP+gvaeTA
+AAACXBI
WXMAAAsSAAALEgHS3X78AAAAmElEQVRYw+1WQQ7AIAizxP9/mV121Gm0BZfYxNukKxSwlI
+Pg75HG
MrGALkykfHjPBKmf+tbIAad/0Ngp3CHGIrnvEoco7xFDTf6lGMIeH6YaBNVYrTEUKYfYUG
+DW0bOI
t2qrTjVYNXa2f9SDAqt97I1AlMFST9pOIbjEYRuKsSS4fUZydcqSQOTzVubq/w+QmmVWtm
+IvFx08
tNghLUXwK/sAAAAASUVORK5CYII=');
}
The formats that are handled by your GTk2 libs can be found with
#!/usr/bin/perl
use strict;
use warnings;
use Gtk2;
my @formats = Gtk2::Gdk::Pixbuf->get_formats();
my @exts;
foreach my $format ( @formats ) {
foreach my $key ( keys( %$format ) ) {
next unless $key eq 'extensions';
foreach my $elem ( @{ $format->{ $key } } ) {
push @exts, $elem;
}
}
}
print "@exts\n";
|