/usr/lib/perl5/PDL/Graphics2D.pm is in pdl 1:2.4.7+dfsg-2ubuntu5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | package PDL::Graphics2D;
use Exporter 'import'; # gives you Exporter's import() method directly
@EXPORT = qw(imag2d twiddle); # symbols to export on request
=head1 NAME
PDL::Graphics2D - An object oriented interface to PDL graphics
=head1 SYNOPSIS
use PDL::Graphics2D;
$win = PDL::Graphics2D->new(<Interface>, <Options>);
$w = imag2d( $image, 'Title Here', ... );
=head1 DESCRIPTION
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.
=head1 CONSTRUCTORS
=head2 new
=for ref
Create a 2-D graphics object with the requested interface type
=cut
{
my %lookup=(
'PGPLOT' => 'PDL::Graphics::PGPLOT::Window'
);
sub new {
my $type=shift;
my $interface=shift;
#
# Translate the interface name to the appropriate class name.
#
$interface=uc($interface);
die "Interface $interface is not known!\n" if !exists($lookup{$interface});
my $class = $lookup{$interface};
eval "require $class;";
return $class->new(@_);
}
}
use strict;
my $debug = 0;
use PDL::Lite;
use PDL::NiceSlice;
#------------------------------------------------------------------------
# PDL constants used by imag2d
#------------------------------------------------------------------------
#
# PDL_B
# PDL_D
# PDL_F
# PDL_L
# PDL_S
# PDL_US
#
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# PDL methods used by imag2d
#------------------------------------------------------------------------
#
# .=
# dim
# float
# get_dataref
# ndims
# sever
# type->symbol
#
#------------------------------------------------------------------------
use OpenGL qw( :all );
#------------------------------------------------------------------------
# opengl/glut constants used by imag2d
#------------------------------------------------------------------------
#
# GLUT_ACTION_CONTINUE_EXECUTION
# GLUT_ACTION_ON_WINDOW_CLOSE
# GLUT_DOUBLE
# GLUT_RGBA
# GLUT_WINDOW_HEIGHT
# GLUT_WINDOW_WIDTH
#
# GL_COLOR_BUFFER_BIT
# GL_FLOAT
# GL_INT
# GL_LUMINANCE
# GL_LUMINANCE_ALPHA
# GL_MODELVIEW
# GL_PROJECTION
# GL_RGB
# GL_RGBA
# GL_SHORT
# GL_UNPACK_ALIGNMENT
# GL_UNSIGNED_BYTE
# GL_UNSIGNED_INT_8_8_8_8
# GL_UNSIGNED_SHORT
#
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# opengl/glu/glut routines used by imag2d
#------------------------------------------------------------------------
#
# OpenGL::done_glutInit
#
# glutAddMenuEntry
# glutAttachMenu
# glutCreateMenu
# glutCreateWindow
# glutDestroyWindow
# glutDisplayFunc
# glutGet
# glutGetWindow
# glutInit
# glutInitDisplayMode
# glutInitWindowPosition
# glutInitWindowSize
# glutKeyboardFunc
# glutLeaveMainLoop
# glutMouseFunc
# glutPostRedisplay
# glutReshapeFunc
# glutReshapeWindow
# glutSetOption
# glutSwapBuffers
#
# glClear
# glClearColor
# glDrawPixels_s
# glFlush
# glLoadIdentity
# glMatrixMode
# glPixelStorei
# glPixelZoom
# glRasterPos2i
# glViewport
#
# gluOrtho2D
#
#------------------------------------------------------------------------
our $draw_overlay;
my $finished_glutInit = 0;
my $cur_fig_num = 0;
my $imag2d_keep_twiddling;
my $show_overlay = 1;
our $is_paused = 0;
our $go_forward = 0;
our $go_backward = 0;
our @imag2d_list = ();
#------------------------------------------------------------------------
# glutMouseFunc callback
#------------------------------------------------------------------------
sub mouse_click {
my ($button, $state, $x, $y) = @_;
my $window_id = glutGetWindow();
my $width = glutGet(GLUT_WINDOW_WIDTH);
my $height = glutGet(GLUT_WINDOW_HEIGHT);
my $img;
# search for image corresponding to window
foreach my $entry ( @imag2d_list ) {
if ( $entry->{window_id} == $window_id ) {
$img = $entry->{img}; # 2D piddle for now
last;
}
}
die "mouse_click: callback could not find image window\n" unless defined $img;
# calculate zoom/aspect ratio factors
my $glds = 0; $glds = 1 if $img->dim(0) < 5; # hack, need verify consistancy
my $zoom_x = $width / $img->dim($glds+0);
my $zoom_y = $height / $img->dim($glds+1);
my $zoom = ($zoom_x < $zoom_y) ? $zoom_x : $zoom_y;
# calculate the offset to the image use for centering
my ($hshift, $vshift) = (0,0);
if ( $zoom == $zoom_x ) {
# shift down
$vshift = ($height - $zoom * $img->dim($glds+1)) / 2.0;
} else {
# shift right
$hshift = ($width - $zoom * $img->dim($glds+0)) / 2.0;
}
my ($im_x, $im_y);
$im_x = sprintf "%.1f", ($x - $hshift) / $zoom;
$im_y = sprintf "%.1f", ($y - $vshift) / $zoom;
if ( $state
and (-1 < $im_x)
and (-1 < $im_y)
and ($im_x < $img->dim($glds+0)+1)
and ($im_y < $img->dim($glds+1)+1) ) {
printf STDERR "b_%01d: pixel=(%d,%d), im pt=(%.1f,%.1f),", $button, $x, $y, $im_x, $im_y;
printf STDERR " im val=%s, glds=$glds, winID=$window_id\n", $glds ? $img->(:,(int($im_x)),(int($im_y))) : $img->((int($im_x)),(int($im_y)));
}
};
#------------------------------------------------------------------------
# glutReshapeFunc callback
#------------------------------------------------------------------------
sub resize_window {
my ($width, $height) = @_;
print STDERR "resize_window: call with new dims ($width, $height)\n" if $debug;
my $window_id = glutGetWindow();
my $img;
return unless scalar(@imag2d_list);
# search for image corresponding to window
foreach my $entry ( @imag2d_list ) {
if ( $entry->{window_id} == $window_id ) {
$img = $entry->{img}; # 2D piddle for now
last;
}
}
die "resize_window: callback could not find image window\n" unless defined $img;
# calculate zoom/aspect ratio factors
my $glds = 0; $glds = 1 if $img->dim(0) < 5; # hack, need verify consistancy
my $zoom_x = $width / $img->dim($glds+0);
my $zoom_y = $height / $img->dim($glds+1);
my $zoom = ($zoom_x < $zoom_y) ? $zoom_x : $zoom_y;
glViewport( 0, 0, $width, $height );
# set coordinate frame for graphics in window
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, $width, $height, 0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
# set zoom factors for image display
glPixelZoom( $zoom, -$zoom );
## glutReshapeWindow($zoom*$img->dim($glds+0), $zoom*$img->dim($glds+1));
# offset the image in the window to keep centered
my ($hshift, $vshift) = (0,0);
if ( $zoom == $zoom_x ) {
# shift down
$vshift = ($height - $zoom * $img->dim($glds+1)) / 2.0;
} else {
# shift right
$hshift = ($width - $zoom * $img->dim($glds+0)) / 2.0;
}
glRasterPos2i( int($hshift), int($vshift) );
my ($do_reshape) = 0;
my ($new_width, $new_height) = ($zoom*$img->dim($glds+0), $zoom*$img->dim($glds+1));
# handle integer rounding problems in resize
if (abs($new_width - $width ) <= 2) {
$new_width = $width;
} else {
$do_reshape++;
}
if (abs($new_height - $height ) <= 2) {
$new_height = $height;
} else {
$do_reshape++;
}
glutReshapeWindow($new_width, $new_height) if $do_reshape;
};
#------------------------------------------------------------------------
# glutDisplayFunc callback
#------------------------------------------------------------------------
sub display_image {
my $window_id = glutGetWindow();
my $img;
my ($gldrawformat, $gldrawtype, $glds);
return unless scalar(@imag2d_list);
# search for image corresponding to window
foreach my $entry ( @imag2d_list ) {
if ( $entry->{window_id} == $window_id ) {
$img = $entry->{img}; # 2D piddle for now
last;
}
}
die "display_window: callback could not find image window\n" unless defined $img;
# determine display pixel format 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";
}
my ($sizeX, $sizeY) = ($img->dim($glds+0), $img->dim($glds+1));
# print STDERR "... calculated image size is ($sizeX, $sizeY)\n";
# display image
glClear(GL_COLOR_BUFFER_BIT);
# glRasterPos2i( 0, 0 );
glDrawPixels_s( $sizeX, $sizeY, $gldrawformat, $gldrawtype, $img->get_dataref );
&{$draw_overlay}($img, $sizeX, $sizeY) if $show_overlay and defined($draw_overlay);
#draw_hough_lines($img, $sizeX, $sizeY);
glutSwapBuffers();
glFlush();
}
my $RELEASE=99;
#------------------------------------------------------------------------
# glutCreateMenu callback
#------------------------------------------------------------------------
sub ModeMenu {
my $entry = shift;
my $img;
if ($entry == $RELEASE) {
my ($window_id) = glutGetWindow();
# search for image corresponding to window
foreach my $listentry ( @imag2d_list ) {
if ( $listentry->{window_id} == $window_id ) {
$img = $listentry->{img}; # 2D piddle for now
last;
}
}
die "ModeMenu: callback could not find image window\n" unless defined $img;
glutLeaveMainLoop();
# glutDestroyWindow($window_id);
} else {
die "ModeMenu: illegal menu entry '$entry'\n";
}
}
#------------------------------------------------------------------------
# glutKeyboardFunc callback
#------------------------------------------------------------------------
sub key_ops {
my ($key, $x, $y) = @_;
my $win_id = glutGetWindow();
# handle keypress events (defaults first)
# print STDERR "Got keypress for keypress=$key\n";
# stop twiddling
if ($key == ord('Q') or $key == ord('q')) {
$imag2d_keep_twiddling = 0;
warn "Stop twiddling command, key '" . chr($key) . "', detected.\n";
return;
}
# exit program
if ($key == 27 or $key == 3) { # ESC or Ctrl-C
warn "Exit program command, key '" . (($key == 27) ? 'ESC' : 'Ctrl-C') . "', detected.\n";
exit;
}
# toggle overlay
if ($key == ord('O') or $key == ord('o')) {
$show_overlay = (($show_overlay) ? 0 : 1);
warn "Toggle overlay command, key '" . chr($key) . "', detected.\n";
return;
}
# lock windows sizes together
if ($key == ord('L') or $key == ord('l')) {
## $lock_sizes = (($lock_sizes) ? 0 : 1);
## warn "Setting \$lock_sizes to $lock_sizes, (window=$win_id)\n";
warn "Lock window sizes command, key '" . chr($key) . "', not implemented.\n";
return;
}
# toggle image histogram equalization
if ($key == ord('H') or $key == ord('h')) {
## $hist_equalize = (($hist_equalize) ? 0 : 1);
## warn "Setting \$hist_equalize to $hist_equalize\n";
warn "Toggle image histogram equalization command, key '" . chr($key) . "', not implemented.\n";
return;
}
# resize current window (last clicked?) to 1:1
if ($key == ord('1')) {
## resize_window(-1,-1); # Special (w,h) args mean set zoom to 1
## glutPostRedisplay();
warn "Resize current window to 1:1 scale command, key '" . chr($key) . "', not implemented.\n";
return;
}
# resize other image windows to this one
if ($key == ord('=')) {
## warn "Resize other images to this one not yet implemented, (window=$win_id)\n";
warn "Resize other windows to this one command, key '" . chr($key) . "', not implemented.\n";
return;
}
# pause/run with space bar
if ($key == 32) { # SPACE
$is_paused = (($is_paused) ? 0 : 1);
warn "Pause/Run command, key 'SPACE', detected\n";
return;
}
# toggle verbose output
if ($key == ord('v') or $key == ord('V')) {
## $be_verbose = (($be_verbose) ? 0 : 1);
warn "Toggle verbose output command, key '" . chr($key) . "', not implemented.\n";
return;
}
# change direction or step in direction
if ($key == 46 or $key == 62) { # . or >
$go_forward = 1;
$go_backward = 0;
warn "Change Direction/Step forward command, key '" . (($key == 46) ? '.' : '>') . "', detected.\n";
return;
};
if ($key == 44 or $key == 60) { ; # , or <
$go_forward = 0;
$go_backward = 1;
warn "Change Direction/Step backward command, key '" . (($key == 44) ? ',' : '<') . "', detected.\n";
return;
}
warn "No handler for key " . chr($key) . ", (window=$win_id)\n";
}
#------------------------------------------------------------------------
# Create a new OpenGL context window for image display
#------------------------------------------------------------------------
sub display_new_window {
my ($height, $width, $zoom, $name, $off_r, $off_c, $window_id) = @_;
my ($window_width, $window_height);
my ($zoom_x, $zoom_y);
if ( $width <= 0 || $height <= 0 || $zoom == 0.0 )
{
die "display_new_window: invalid arguments!\n";
}
$window_width = int($zoom*$width + 0.5);
$window_height = int($zoom*$height + 0.5);
# compute zoom factors to make graphics overlay the image precisely
$zoom_x = $window_width/$width;
$zoom_y = $window_height/$height;
# create display window
if (! $finished_glutInit ) {
glutInit() unless OpenGL::done_glutInit();
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
$finished_glutInit = 1;
}
glutInitWindowSize( $window_width, $window_height );
glutInitWindowPosition( $off_r, $off_c );
$window_id = glutCreateWindow( $name );
# set some standard defaults
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glViewport( 0, 0, $window_width, $window_height );
# set coordinate frame for graphics in window
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, $width, $height, 0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
# set zoom factors for image display
glPixelZoom( $zoom_x, -$zoom_y );
# set origin for drawing images as the top-left corner of the window
glRasterPos2i( 0, 0 );
# success
return $window_id;
};
#------------------------------------------------------------------------
# Display piddle as 2-D image in window using OpenGL
#------------------------------------------------------------------------
=head1 FUNCTIONS
=head2 imag2d
=for ref
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.
=for usage
$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
=for example
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;
=head2 twiddle
=for ref
Enable GUI interaction with a FreeGLUT display window.
=for usage
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;
|