PDL::Graphics2D - An object oriented interface to PDL graphics
use PDL::Graphics2D; $win = PDL::Graphics2D->new(<Interface>, <Options>);
$w = imag2d( $image, 'Title Here', ... );
This is an umbrella class allowing for a simple interface to all plotting routines in PDL. On its own it does not do any work it merely passes information to the appropriate class. Ideally this should probably offer a uniform interface to a variety of packages.
This requires a lot more work before it is useful I feel, but it can be used already.
Create a 2-D graphics object with the requested interface type
Display a 2-D image in a figure window
imag2d() creates a plain FreeGLUT OpenGL window and displays the input image with 1:1 aspect ratio for pixels. The window resize is constrained to the actual ratio of the image dimensions. The initial display size is currently a 200x200 window to prevent things from being too small by default.
The image to display can have dimensions ($c,$M,$N) where for $c==4 the display is in GL_RGBA, for $c==3 the display is GL_RGB, for $c==2 the display is GL_LUMINANCE_ALPHA, and for $c==1 or for for dimensions ($M,$N) then the display is GL_LUMINANCE.
This routine does not yet thread but multiple images may be viewed at the same time in separate windows by multiple calls to imag2d(). TriD graphics visualization windows and the imag2d() windows may be created and used independently.
NOTE: If you are twiddling a TriD window, the imag2d()
windows are active as well. If you call twiddle()
the sub, only the imag2d() windows will update correctly.
$window_id = imag2d($image, $name, $zoom, $x_off, $y_off); creates a new image figure window from the input piddle with the given title, zoom factor, and position (if possible) $window_id - may be used to refer to the figure window $image - 2D image piddle with at least 2 or 3 dimensions e.g. [M,N], [1,M,N], [2,M,N], [3,M,N], [4,M,N] $name - the name to use for the figure window (optional) $zoom - desired (float) pixel zoom factor (optional) ($x_off, $y_off) - desired window pixel position (optional) with (0,0) as the top left pixel of the display
use PDL::Graphics2d; # imports imag2d() and twiddle()
$a = sequence(64,48,3); # make test RGB image $a = $a->mv(2,0); # color must be dim(0) with size [0..4] $a /= $a->max; # pixel values in [0.0,1.0] $a = sin(10*$a); $w1 = imag2d($a); # with parens... $w2 = imag2d $a->sqrt; # or without $w3 = imag2d $a**2;
Enable GUI interaction with a FreeGLUT display window.
twiddle(); Runs the FreeGLUT event loop so window GUI operations such as resize, expose, mouse click,.. work =cut sub imag2d { my ($img, $name, $zoom, $off_r, $off_c) = (undef,"Figure $cur_fig_num", undef, 0, 0);
# need to add error checking here $img = shift; $name = shift if scalar(@_); $zoom = shift if scalar(@_); $off_r = shift if scalar(@_); $off_c = shift if scalar(@_);
my $window_id; my ($gldrawformat, $gldrawtype, $glds);
# determine display pixel format and type to use if ($img->ndims > 2 && $img->dim(0) == 4) { $gldrawformat = GL_RGBA; $glds = 1; } elsif ($img->ndims > 2 && $img->dim(0) == 3) { $gldrawformat = GL_RGB; $glds = 1; } elsif ($img->ndims > 2 && $img->dim(0) == 2) { $gldrawformat = GL_LUMINANCE_ALPHA; $glds = 1; } elsif ($img->ndims > 2 && $img->dim(0) == 1) { $gldrawformat = GL_LUMINANCE; $glds = 1; } else { $gldrawformat = GL_LUMINANCE; $glds = 0; };
# convert to float if double for display if ($img->type->symbol eq 'PDL_D') { # clean up code $img = $img->float; }
# determine display pixel type to use if ($img->type->symbol eq 'PDL_F') { $gldrawtype = GL_FLOAT; } elsif ($img->type->symbol eq 'PDL_B') { $gldrawtype = GL_UNSIGNED_BYTE; } elsif ($img->type->symbol eq 'PDL_S') { $gldrawtype = GL_SHORT; } elsif ($img->type->symbol eq 'PDL_US') { $gldrawtype = GL_UNSIGNED_SHORT; } elsif ($img->type->symbol eq 'PDL_L') { $gldrawtype = ( $gldrawformat == GL_RGBA ) ? GL_UNSIGNED_INT_8_8_8_8 : GL_INT; } else { die "display_image: unsupported data type '", $img->type->symbol, "' for image display\n"; }
# create display window my ($im_height, $im_width); $im_height = $img->dim($glds+1); $im_width = $img->dim($glds+0); if ( !defined($zoom) and ( $im_width < 200 or $im_height < 200 ) ) { # adjust zoom to make initial window bigger than 200px my $mindim = ($im_width < $im_height) ? $im_width : $im_height; my $zoomest = int( 200 / $mindim ); $zoomest += 1 unless $zoomest == 200/$mindim; print STDERR "imag2d: estimated zoom factor is $zoomest\n" if $debug; $zoom = $zoomest; } $zoom = defined($zoom) ? $zoom : 1.0;
print STDERR "imag2d: calling display_new_window( " . $img->dim($glds+1) . ", " . $img->dim($glds+0) . ", $zoom, $name, $off_r, $off_c )" if $debug;
if ( ! defined( $window_id = display_new_window( $img->dim($glds+1), $img->dim($glds+0), $zoom, $name, $off_r, $off_c ) ) ) { print STDERR "imag2d: failure\n"; return; }
# add GLUT window id to title glutSetWindowTitle($name . ": WinID $window_id"); $cur_fig_num++;
# set callback function for image display glutDisplayFunc ( \&display_image );
# set callback function for keypress events glutKeyboardFunc ( \&key_ops );
# set callback for mouse clicks glutMouseFunc( \&mouse_click );
# set callback for image window resize glutReshapeFunc( \&resize_window );
glutCreateMenu( \&ModeMenu ); glutAddMenuEntry( "End MainLoop", $RELEASE ); glutAttachMenu(GLUT_RIGHT_BUTTON);
# add image and window to list push @imag2d_list, { window_id => $window_id, img => $img };
# set callback for image window close glutCloseFunc( \&close_imag2d_window );
# success glRasterPos2i( 0, 0 ); glDrawPixels_s( $img->dim($glds+0), $img->dim($glds+1), $gldrawformat, $gldrawtype, $img->get_dataref ); glFlush();
twiddle();
return $window_id; }
#------------------------------------------------------------------------ # Update imag2d() window image data #------------------------------------------------------------------------ sub imag2d_update { my ($win_id, $image) = @_; my $img;
return unless scalar(@imag2d_list);
# search for image corresponding to window foreach my $entry ( @imag2d_list ) { if ( $entry->{window_id} == $win_id ) { $img = $entry->{img}; # 2D piddle for now last; } }
die "imag2d_update: callback could not find image window\n" unless defined $img;
# update display window $img .= $image->sever; glutPostRedisplay();
return $win_id; }
#------------------------------------------------------------------------ # Close a specific imag2d window #------------------------------------------------------------------------ sub close_imag2d_window {
my $win_id = glutGetWindow();
if ( ! scalar(@imag2d_list) ) { $imag2d_keep_twiddling = 0; return; }
# search for image corresponding to window my ($entry, $found_it); foreach $entry ( @imag2d_list ) { if ($entry->{window_id} == $win_id) { $found_it = 1; last; } }
if ($found_it) { @imag2d_list = grep { $_->{window_id} != $win_id } @imag2d_list; $imag2d_keep_twiddling = 0 unless scalar(@imag2d_list); } else { warn "close_imag2d_window: could not find open window\n"; } }
#------------------------------------------------------------------------ # Close all imag2d windows #------------------------------------------------------------------------ sub close_imag2d {
return unless scalar(@imag2d_list);
# process all image windows foreach my $entry ( @imag2d_list ) { glutDestroyWindow($entry->{window_id}); }
@imag2d_list = (); }
#------------------------------------------------------------------------
# Simple twiddle for perldl (use [qQ] to exit)
#------------------------------------------------------------------------
sub twiddle {
print STDERR "Type Q or q to stop twiddling...\n";
$imag2d_keep_twiddling = 1 if scalar(@imag2d_list)
;
while ($imag2d_keep_twiddling && scalar(@imag2d_list)) {
glutMainLoopEvent()
;
}
print STDERR "Stopped twiddle-ing!\n";
}
1;