loading an image from data, not file



Hello again,

Well this is another question I have that has stumped me.
I've searched the archives, and the only thing I could find
is I need to pack the binary data somehow.

Anyways, the code below works fine when loading the image 
new_from_file, but all I get is a bunch of colored pixels
when trying new_from_data.

I'm guessing my "pack" is wrong, since the error I get is
 Argument "M-^?M-XM-^?M-`\0^PJFIF\0^A^B\0\0d\0d\0\0M-^?M-l\...
" isn't numeric in pack at ./image-viewport-from-data line 31.

I know I'm way off on the packing, could someone put me on the
right track on how to properly pack the binary image data?

#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 '-init';

my $mw = Gtk2::Window->new;

my $vp = Gtk2::Viewport->new(undef, undef);
$mw->add($vp);

&load_image;

$mw->signal_connect('destroy', sub { Gtk2->main_quit });
Gtk2->main;

##########################################################
sub load_image {

 my $file = shift || 'bridget-1.jpg';


##########block in question###############################
 my $image_data;
 open (FH,"< $file");
 read( FH, $image_data, -s FH );
 close FH;

my($width,$height)= (483,600);   # say I know this already

$image_data = pack "C*", $image_data;  

my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_data
        ($image_data,  # packed image data in a scalar
        'rgb',  # only 24- or 32-bit RGB are supported
         0, # no alpha, data is 24-bit
         8,     # only 8 bits per sample are supported
         $width,  # in pixels
         $height,  # in pixels
         3 * $width );   

my $image = Gtk2::Image->new_from_pixbuf ($pixbuf);

########################################################

#my $image = Gtk2::Image->new_from_file($file);   #works fine

my $pb = $image->get_pixbuf;
my ($x, $y) = ($pb->get_width, $pb->get_height);

$vp->add($image);

$mw->set_title("$file ${x}x${y}");
$mw->show_all();

}

__END__







-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



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