/usr/lib/perl5/Prima/Image/AnimateGIF.pm is in libprima-perl 1.28-1.2.
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 | # Copyright (c) 2008 Dmitry Karasik
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: AnimateGIF.pm,v 1.2 2008/04/24 19:45:20 dk Exp $
package Prima::Image::AnimateGIF;
use strict;
use warnings;
use Carp;
use Prima;
use constant DISPOSE_NOT_SPECIFIED => 0; # Leave frame, let new frame draw on top
use constant DISPOSE_KEEP => 1; # Leave frame, let new frame draw on top
use constant DISPOSE_CLEAR => 2; # Clear the frame's area, revealing bg
use constant DISPOSE_RESTORE_PREVIOUS => 3; # Restore the previous (composited) frame
sub new
{
my $class = shift;
my $self = bless {
images => [],
@_,
current => -1,
}, $class;
$self-> reset;
return $self;
}
sub load
{
my $class = shift;
my ( $where, %opt) = @_;
# have any custom notifications?
my ( %events, %args);
while ( my ( $k, $v) = each %opt) {
my $hash = ($k =~ /^on[A-Z]/ ? \%events : \%args);
$hash-> {$k} = $v;
}
my $i = Prima::Icon-> new(%events); # dummy object
my @i = $i-> load(
$where,
loadExtras => 1,
loadAll => 1,
iconUnmask => 1,
%args,
);
return unless @i;
return $class-> new( images => \@i);
}
sub add
{
my ( $self, $image) = @_;
push @{$self-> {images}}, $image;
}
sub get_extras
{
my ( $self, $ix) = @_;
$ix = $self-> {images}-> [$ix];
return unless $ix;
my $e = $ix-> {extras} || {};
$e-> {screenHeight} ||= $ix-> height;
$e-> {screenWidth} ||= $ix-> width;
$e-> {$_} ||= 0 for qw(disposalMethod useScreenPalette delayTime left top);
$e-> {iconic} =
$ix-> isa('Prima::Icon')
&& $ix-> autoMasking != am::None;
# gif doesn't support explicit masks, therefore
# when image actually has a mask, autoMaskign is set to am::Index
return $e;
}
sub fixup_rect
{
my ( $self, $info, $image) = @_;
return if defined $info-> {rect};
$info-> {rect} = {
bottom => $self-> {screenHeight} - $info-> {top} - $image-> height,
top => $self-> {screenHeight} - $info-> {top} - 1,
right => $info-> {left} + $image-> width - 1,
left => $info-> {left},
};
}
sub union_rect
{
my ( $r1, $r2) = @_;
return { %$r2 } unless grep { $r1-> {$_} } qw(left bottom right top);
return { %$r1 } unless grep { $r2-> {$_} } qw(left bottom right top);
my %ret = %$r1;
for ( qw(left bottom)) {
$ret{$_} = $r2-> {$_}
if $ret{$_} > $r2-> {$_};
}
for ( qw(right top)) {
$ret{$_} = $r2-> {$_}
if $ret{$_} < $r2-> {$_};
}
return \%ret;
}
sub reset
{
my $self = shift;
$self-> {current} = -1;
delete @{$self}{qw(canvas bgColor saveCanvas
saveMask image info
screenWidth screenHeight
loopCount changedRect
)};
my $i = $self-> {images};
return unless @$i;
my $ix = $i-> [0];
return unless $ix;
my $e = $self-> get_extras(0);
return unless $e;
$self-> {image} = $self-> {images}-> [0];
$self-> {info} = $e;
$self-> {$_} = $e-> {$_} for qw(screenWidth screenHeight);
$self-> {changedRect} = {};
$self-> fixup_rect( $e, $ix);
# create canvas and mask
$self-> {canvas} = Prima::DeviceBitmap-> new(
width => $e-> {screenWidth},
height => $e-> {screenHeight},
monochrome => 0,
backColor => 0,
);
$self-> {canvas}-> clear; # canvas is all-0 initially
$self-> {mask} = Prima::DeviceBitmap-> new(
width => $e-> {screenWidth},
height => $e-> {screenHeight},
monochrome => 1,
backColor => 0xFFFFFF,
color => 0x000000,
);
$self-> {mask}-> clear; # mask is all-1 initially
if ( defined $e-> {screenBackGroundColor}) {
my $cm =
$e-> {useScreenPalette} ?
$e-> {screenPalette} :
$self-> {images}-> [0]-> palette;
my $i = $e-> {screenBackGroundColor} * 3;
$self-> {bgColor} = (
($$cm[$i] || 0) |
(($$cm[$i+1] || 0) << 8) |
(($$cm[$i+2] || 0) << 16)
);
}
}
sub next
{
my $self = shift;
my $info = $self-> {info};
return unless $info;
my @sz = ( $self-> {screenWidth}, $self-> {screenHeight});
my %ret;
# dispose from the previous frame and calculate the changed rect
if ( $info-> {disposalMethod} == DISPOSE_CLEAR) {
$self-> {canvas}-> backColor( 0);
$self-> {canvas}-> clear;
$self-> {mask}-> backColor(cl::Set);
$self-> {mask}-> clear;
%ret = %{ $self-> {changedRect} };
$self-> {changedRect} = {};
} elsif ( $info-> {disposalMethod} == DISPOSE_RESTORE_PREVIOUS) {
# cut to the previous frame, that we expect to be saved for us
if ( $self-> {saveCanvas} && $self-> {saveMask}) {
$self-> {canvas} = $self-> {saveCanvas};
$self-> {mask} = $self-> {saveMask};
}
$self-> {changedRect} = $self-> {saveRect};
delete $self-> {saveCanvas};
delete $self-> {saveMask};
delete $self-> {saveRect};
%ret = %{ $info-> {rect} };
}
# advance frame
delete @{$self}{qw(image info)};
if ( ++$self-> {current} >= @{$self-> {images}}) {
# go back to first frame, or stop
if ( defined $self-> {loopCount}) {
return if --$self-> {loopCount} <= 0;
}
$self-> {current} = 0;
}
$self-> {image} = $self-> {images}-> [$self-> {current}];
my $old_info = $info;
$info = $self-> {info} = $self-> get_extras( $self-> {current} );
$self-> fixup_rect( $info, $self-> {image});
my @is = $self-> {image}-> size;
# load global extension data
if ( $self-> {current} == 0) {
unless ( defined $info-> {loopCount}) {
$self-> {loopCount} = 1;
} elsif ( $info-> {loopCount} == 0) {
# loop forever
$self-> {loopCount} = undef;
} else {
$self-> {loopCount} = $info-> {loopCount};
}
}
# remember the background, if needed, and again update the # rect
if ( $info-> {disposalMethod} == DISPOSE_RESTORE_PREVIOUS) {
my $c = Prima::DeviceBitmap-> new(
width => $sz[0],
height => $sz[1],
monochrome => 0,
);
$c-> put_image( 0, 0, $self-> {canvas});
$self-> {saveCanvas} = $self-> {canvas};
$self-> {canvas} = $c;
$c = Prima::DeviceBitmap-> new(
width => $sz[0],
height => $sz[1],
monochrome => 1,
);
$c-> put_image( 0, 0, $self-> {mask});
$self-> {saveMask} = $self-> {mask};
$self-> {mask} = $c;
$self-> {saveRect} = $self-> {changedRect};
}
$self-> {changedRect} = union_rect( $self-> {changedRect}, $info-> {rect});
%ret = %{ union_rect( \%ret, $info-> {rect}) };
# draw the current frame
if ( $info-> {iconic}) {
my ( $xor, $and) = $self-> {image}-> split;
# combine masks
$self-> {mask}-> set(
color => cl::Clear,
backColor => cl::Set,
);
$self-> {mask}-> put_image(
$info-> {rect}-> {left},
$info-> {rect}-> {bottom},
$and,
rop::AndPut,
);
} else {
$self-> {mask}-> color(cl::Clear);
$self-> {mask}-> bar(
$info-> {rect}-> {left},
$info-> {rect}-> {bottom},
$info-> {rect}-> {left} + $is[0],
$info-> {rect}-> {bottom} + $is[1],
);
}
# put non-transparent image pixels
$self-> {canvas}-> put_image(
$info-> {rect}-> {left},
$info-> {rect}-> {bottom},
$self-> {image},
);
#
$ret{delay} = $info-> {delayTime} / 100;
$ret{$_} ||= 0 for qw(left bottom right top);
return \%ret;
}
sub is_stopped
{
my $self = shift;
return $self-> {current} >= @{$self-> {images}};
}
sub icon
{
my $self = shift;
my $i = Prima::Icon-> new;
$i-> combine( $self-> {canvas}-> image, $self-> {mask}-> image);
return $i;
}
sub image
{
my $self = shift;
my $i = Prima::Image-> new(
width => $self-> {canvas}-> width,
height => $self-> {canvas}-> height,
type => im::RGB,
backColor => $self-> {bgColor} || 0,
);
$i-> begin_paint;
$i-> clear;
$i-> set(
color => cl::Clear,
backColor => cl::Set,
);
$i-> put_image( 0, 0,$self-> {mask}, rop::AndPut);
$i-> put_image( 0, 0,$self-> {canvas}, rop::XorPut);
$i-> end_paint;
return $i;
}
sub draw
{
my ( $self, $canvas, $x, $y) = @_;
return unless $self-> {canvas};
my %save = map { $_ => $canvas-> $_() } qw(color backColor);
$canvas-> set(
color => cl::Clear,
backColor => cl::Set,
);
$canvas-> put_image( $x, $y, $self-> {mask}, rop::AndPut);
$canvas-> put_image( $x, $y, $self-> {canvas}, rop::XorPut);
$canvas-> set( %save);
}
sub masks { ( $_[0]-> {canvas}, $_[0]-> {mask} ) }
sub width { $_[0]-> {canvas} ? $_[0]-> {canvas}-> width : 0 }
sub height { $_[0]-> {canvas} ? $_[0]-> {canvas}-> height : 0 }
sub size { $_[0]-> {canvas} ? $_[0]-> {canvas}-> size : (0,0) }
sub bgColor { $_[0]-> {bgColor} }
sub current { $_[0]-> {current} }
sub total { scalar @{$_[0]-> {images}} }
sub length
{
my $length = 0;
$length += $_-> {delayTime} || 0 for
map { $_-> {extras} || {} }
@{$_[0]-> {images}};
return $length / 100;
}
sub loopCount
{
return $_[0]-> {loopCount} unless $#_;
$_[0]-> {loopCount} = $_[1];
}
1;
__END__
=pod
=head1 NAME
Prima::Image::AnimateGIF - animate gif files
=head1 DESCRIPTION
The module provides high-level access to GIF animation sequences.
=head1 SYNOPSIS
use Prima qw(Application Image::AnimateGIF);
my $x = Prima::Image::AnimateGIF->load($ARGV[0]);
die $@ unless $x;
my ( $X, $Y) = ( 0, 100);
my $background = $::application-> get_image( $X, $Y, $x-> size);
$::application-> begin_paint;
while ( my $info = $x-> next) {
my $frame = $background-> dup;
$frame-> begin_paint;
$x-> draw( $frame, 0, 0);
$::application-> put_image( $X, $Y, $frame);
$::application-> sync;
select(undef, undef, undef, $info-> {delay});
}
$::application-> put_image( $X, $Y, $g);
=head2 new $CLASS, %OPTIONS
Creates an empty animation container. If C<$OPTIONS{images}> is given, it is
expected to be an array of images, best if loaded from gif files with
C<loadExtras> and C<iconUnmask> parameters set ( see L<Prima::image-load> for
details).
=head2 load $SOURCE, %OPTIONS
Loads GIF animation sequence from file or stream C<$SOURCE>. Options
are the same as understood by C<Prima::Image::load>, and are passed
down to it.
=head2 add $IMAGE
Appends an image frame to the container.
=head2 bgColor
Return the background color specified by the GIF sequence as the preferred
background color to use when there is no specific background to superimpose the
animation to.
=head2 current
Return index of the current frame
=head2 draw $CANVAS, $X, $Y
Draws the current composite frame on C<$CANVAS> at the given coordinates.
=head2 height
Returns height of the composite frame.
=head2 icon
Creates and returns an icon object off the current composite frame.
=head2 image
Creates and returns an image object off the current composite frame. The
transparent pixels on the image are replaced with the preferred background
color.
=head2 is_stopped
Returns true if the animation sequence was stopped, false otherwise.
If the sequence was stopped, the only way to restart it is to
call C<reset>.
=head2 length
Returns total animation length (without repeats) in seconds.
=head2 loopCount [ INTEGER ]
Sets and returns number of loops left, undef for indefinite.
=head2 masks
Return the AND and XOR masks, that can be used to display the current
composite frame.
=head2 next
Advances one animation frame. The step triggers changes to the internally kept
AND and XOR masks that create effect of transparency, if needed. The method
return a hash, where the following field are initialized:
=over
=item left, bottom, right, top
Coordinates of the changed area since the last frame was updated.
=item delay
Time ins seconds how long the frame is expected to be displayed.
=back
=head2 reset
Resets the animation sequence. This call is necessary either when image sequence was altered,
or when sequence display restart is needed.
=head2 size
Returns width and height of the composite frame.
=head2 total
Return number fo frames
=head2 width
Returns width of the composite frame.
=head1 SEE ALSO
L<Prima::image-load>,
L<http://www.the-labs.com/GIFMerge/ >
=head1 AUTHOR
Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>.
=cut
|