Skip to content

Commit

Permalink
add Prima::Image::Saver
Browse files Browse the repository at this point in the history
  • Loading branch information
dk committed Nov 2, 2023
1 parent e35656b commit b563ca7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
37 changes: 37 additions & 0 deletions Prima/Image/Loader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,42 @@ sub current { shift->{current} }
sub source { shift->{source} }
sub DESTROY { $_[0]->{image}-> destroy if $_[0]->{image} }

package Prima::Image::Saver;

sub new
{
my ( $class, $target, %opt ) = @_;

my $img = Prima::Image->new;

my $ok = $img->save( $target,
unlink => 0,
%opt,
session => 1
);
unless ($ok) {
$img->destroy;
return (undef, $@);
}

return bless {
target => $target,
image => $img,
saved => 0,
}, $class;
}

sub save
{
my ( $self, $image, %opt ) = @_;

my $ok = $self->{image}->save( $image, %opt, session => 1 );
unlink $self->{target} if !$ok && $self->{unlink};

return $ok ? 1 : (0, $@);
}

sub DESTROY { $_[0]->{image}-> destroy if $_[0]->{image} }

1;

22 changes: 10 additions & 12 deletions class/Image/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ XS( Image_load_FROMPERL)
self = gimme_the_mate( ST( 0));
sv = ST(1);
profile = parse_hv( ax, sp, items, mark, 2, "Image::load");
pioreq = fill_ioreq(sv, &sioreq);

if ( pexist(session) && pget_B(session)) {
if ( !self )
Expand All @@ -152,9 +151,10 @@ XS( Image_load_FROMPERL)
pset_c( className, self ? my-> className : ( char*) SvPV_nolen( ST( 0)));
pset_i( eventMask, self ? var-> eventMask2 : 0);

if ( err ) {
/* do nothing */
} else if ( load_next_frame ) {
if ( !load_next_frame )
pioreq = fill_ioreq(sv, &sioreq);

if ( load_next_frame ) {
Handle obj;
PImgLoadFileInstance fi = (PImgLoadFileInstance) var-> loading_session;
if ( !( ret = plist_create(1,1)))
Expand Down Expand Up @@ -261,26 +261,24 @@ XS( Image_save_FROMPERL)
self = gimme_the_mate( ST( 0));
sv = ST(1);
profile = parse_hv( ax, sp, items, mark, 2, "Image::save");
pioreq = fill_ioreq(sv, &sioreq );

if ( pexist(session) && pget_B(session)) {
if ( !self )
croak("Cannot start saving session without an object");
if ( var-> saving_session ) {
if ( SvOK(sv))
croak("Another saving session is in progress");
if ( var-> saving_session )
save_next_frame = true;
} else
else
open_save = true;
}

if ( !save_next_frame )
pioreq = fill_ioreq(sv, &sioreq );

if ( save_next_frame ) {
Handle obj;
PImgSaveFileInstance fi = (PImgSaveFileInstance) var-> saving_session;

if ( !pexist(image))
croak("`image' option expected");
obj = pget_H(image);
obj = gimme_the_mate(sv);
if ( !obj || !kind_of(obj, CImage))
croak("Bad image passed");

Expand Down

0 comments on commit b563ca7

Please sign in to comment.