howto use GdkPixbuf::new_from_data() ?
| From: | Decapode Azur | Date: | Thu, 23 Oct 2003 15:45:01 +0000 |
| Subject: | howto use GdkPixbuf::new_from_data() ? | ||
| Groups: | php.gtk.general | ||
| Request: | Send a blank email to php-gtk-general+get-3983@lists.php.net to get a copy of this message | ||
re
I'm trying to use GdkPixbuf::new_from_data(),
but I don't find what is wrong in my script.
Has the image data to be convert in a special format?
Or perhaps the problem is from the rowstride?
I don't understand what "specifies distance in bytes between successive
image rows" means, so I've tryed to put it the same than the width or the
height, but no success...
===============================
#! /usr/bin/php -q
<?php
dl('php_gtk.so');
if ($argc < 2) die("\nUsage: php -q gdk_pixbuf.php <imagefile>\n\n");
$image_file = $argv[1];
$handle = imagick_readimage($image_file);
if ( imagick_iserror($handle) ) {
fwrite(STDERR, sprintf("handle failed!\nReason: %s\nDescription: %s\n",
imagick_failedreason( $handle ),
imagick_faileddescription($handle) ));
exit;
}
//imagick_convert( $handle, 'PPM' );
imagick_convert( $handle, 'PNM' );
if ( !( $image_data = imagick_image2blob($handle) )) {
fwrite(STDERR, sprintf("imagick_image2blob() failed\nReason:
%s\nDescription: %s\n",
imagick_failedreason($handle),
imagick_faileddescription($handle) ));
exit;
}
$image_depth = imagick_getimagedepth($handle);
if ($image_depth != 8) {
fwrite(STDERR, "Error: The image depth of the image must be 8 bits\n".
"Other depth are not supported by GdkPixbuf::new_from_data() yet\n");
} //else print "Image depth is $image_depth bits\n";
$window = &new GtkWindow();
$window->set_border_width(1);
$window->set_position(GTK_WIN_POS_CENTER);
$window->set_title('GdkPixbuf');
$window->set_policy(false, false, true);
$window->connect_object('destroy', array('gtk', 'main_quit'));
//$pixbuf = GdkPixbuf::new_from_file($image_file);
// dimensions of my image:
$width = 512;
$height = 256;
$rowstride = 512;
//$rowstride = 256;
$colorspace = GDK_COLORSPACE_RGB;
$has_alpha = FALSE;
$bits_per_sample = 8;
$pixbuf = GdkPixbuf::new_from_data($image_data, $colorspace,
$has_alpha, $bits_per_sample,
$width, $height, $rowstride);
/*
currently only RGB images with 8 bits per sample are supported
$colorspace has to be GDK_COLORSPACE_RGB and $bits_per_sample 8
$rowstride specifies distance in bytes between successive image rows
*/
if (!$pixbuf) die("\n\tCouldn't load $image_file\n\n");
$font = gdk::font_load("-*-helvetica-bold-r-*-*-*-120-*-*-*-*-*-*");
$extents = $font->extents($image_file);
$area = &new GtkDrawingArea();
$area->size($pixbuf->get_width(), $pixbuf->get_height());
$area->connect('expose_event', 'expose_event', $pixbuf, $font, $extents,
basename($image_file));
$frame = &new GtkFrame();
$frame->add($area);
$frame->set_shadow_type(GTK_SHADOW_OUT);
$window->add($frame);
$window->show_all();
gtk::main();
function expose_event($area, $event, $pixbuf, $font, $extents, $image_file)
{
$pixbuf->render_to_drawable($area->window,
$area->style->fg_gc[GTK_STATE_NORMAL],
$event->area->x, $event->area->y,
$event->area->x, $event->area->y,
$event->area->width, $event->area->height,
GDK_RGB_DITHER_NORMAL, 0, 0);
/*if ($event->area->x < $extents[2] + 3 &&
$event->area->y < $extents[3] + $extents[4] + 3) {
gdk::draw_string($area->window, $font, $area->style->white_gc, 3, 3 +
$extents[3] + $extents[4], $image_file);
gdk::draw_string($area->window, $font, $area->style->black_gc, 2, 2 +
$extents[3] + $extents[4], $image_file);
}*/
}
?>