From b563ca78e98557a1c385ecd770483544d6a14ed3 Mon Sep 17 00:00:00 2001 From: Dmitry Karasik Date: Thu, 2 Nov 2023 20:58:06 +0100 Subject: [PATCH] add Prima::Image::Saver --- Prima/Image/Loader.pm | 37 +++++++++++++++++++++++++++++++++++++ class/Image/io.c | 22 ++++++++++------------ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Prima/Image/Loader.pm b/Prima/Image/Loader.pm index 95f9ccdd7..8d7dc1b9c 100644 --- a/Prima/Image/Loader.pm +++ b/Prima/Image/Loader.pm @@ -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; diff --git a/class/Image/io.c b/class/Image/io.c index d30d6f128..3945e6294 100644 --- a/class/Image/io.c +++ b/class/Image/io.c @@ -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 ) @@ -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))) @@ -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");