Skip to content

Commit 87e523c

Browse files
committed
Prima::Image->save recognizes (format => 'png') syntax
1 parent 5dba376 commit 87e523c

4 files changed

Lines changed: 38 additions & 22 deletions

File tree

Prima/Image/base64.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sub save
4545

4646
$param{codecID} //= $i->{extras}->{codecID};
4747

48-
unless ( $param{codecID}) {
48+
unless ( $param{format} || $param{codecID}) {
4949
my @want;
5050
if ( $i->isa('Prima::Icon')) {
5151
if ( (($i-> type & im::BPP) > 8) || ($i-> maskType > 1)) {

img/load.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ codec_matches_extension( PImgCodec c, char *fileName)
218218
while ( c-> info-> fileExtensions[ j]) {
219219
char * ext = c-> info-> fileExtensions[ j];
220220
int extLen = strlen( ext);
221-
if ( extLen < fileNameLen && stricmp( fileName + fileNameLen - extLen, ext) == 0)
221+
if ( extLen <= fileNameLen && stricmp( fileName + fileNameLen - extLen, ext) == 0)
222222
return true;
223223
j++;
224224
}
@@ -912,13 +912,24 @@ apc_img_open_save( char * fileName, Bool is_utf8, int n_frames, PImgIORequest io
912912

913913
/* checking 'codecID', if available */
914914
{
915-
SV * c = NULL;
915+
SV * svc = NULL;
916916
if ( pexist( codecID))
917-
c = pget_sv( codecID);
918-
if ( c && SvOK( c)) { /* accept undef */
919-
codecID = SvIV( c);
917+
svc = pget_sv( codecID);
918+
if ( svc && SvOK( svc)) { /* accept undef */
919+
codecID = SvIV( svc);
920920
if ( codecID < 0)
921921
codecID = imgCodecs. count - codecID;
922+
} else if ( pexist(format)) {
923+
int i;
924+
char * format = pget_c(format);
925+
for ( i = 0; i < imgCodecs. count; i++) {
926+
if ( disabled[ i]) continue;
927+
c = ( PImgCodec ) ( imgCodecs. items[ i]);
928+
if ( !codec_matches_extension( c, format))
929+
continue;
930+
codecID = i;
931+
break;
932+
}
922933
}
923934
}
924935

pod/Prima/Image.pod

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -930,19 +930,20 @@ success flag. The method features different semantics, depending on
930930
the PARAMETERS hash. If an error occurs, the C<$@> variable contains the error
931931
string.
932932

933-
Note that when saving to a stream, C<codecID> must be explicitly given in
934-
C<%PARAMETERS>.
933+
Note that when saving to a stream, either C<codecID> or C<format> must be
934+
explicitly given in C<%PARAMETERS>.
935935

936-
See L<Prima::image-load> for details and L<Prima::Image::Loader/Prima::Image::Saver> for more functionality.
936+
See L<Prima::image-load> for details and
937+
L<Prima::Image::Loader/Prima::Image::Saver> for more functionality.
937938

938939
Note: when saving to streams on win32, mind the C<binmode>.
939940

940941
=item save_stream BASE64_STRING, %OPTIONS
941942

942-
Saves the image into an internal stream. Unless C<$OPTIONS{codecID}> or
943-
C<$image->{extras}->{codecID}> is set, tries to find the best codec for the
944-
job. Returns the base64-encoded content on success, or C<undef> on failure;
945-
C<$@> is set in the latter case.
943+
Saves the image into an internal stream. Unless C<$OPTIONS{format}> or
944+
C<$OPTIONS{codecID}> or C<$image->{extras}->{codecID}> is set, tries to find
945+
the best codec for the job. Returns the base64-encoded content on success, or
946+
C<undef> on failure; C<$@> is set in the latter case.
946947

947948
=item scanline Y
948949

pod/Prima/image-load.pod

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,20 +338,24 @@ Save requests also can be performed with the package syntax:
338338

339339
=head2 Saving to a stream
340340

341-
Saving to a stream requires the explicit C<codecID> integer value to be
342-
supplied. When the image is loaded with C<loadExtras>, this field is always
341+
Saving to a stream requires the explicit file format to be specified. This can
342+
be done by either supplying a C<codecID> integer that both identifies the file
343+
format and the codec that would preform the encoding operation, or a C<format>
344+
string, that should match file extension of the desired file format.
345+
346+
When the image is loaded with C<loadExtras>, the C<codecID> field is always
343347
present on the image object and is the integer that selects the image file
344348
format.
345349

346-
my ($png_id) =
347-
map { $_-> {codecID} }
348-
grep { $_-> {fileShortType} =~ /^png$/i }
349-
@{ Prima::Image-> codecs };
350-
die "No png codec installed" unless $png_id;
351-
350+
my $png = Prima::Image->has_codec('png') or die "No png codec installed";
352351
open FILE, ">", "a.png" or die "Cannot save:$!";
353352
binmode FILE;
354-
$image-> save( \*FILE, codecID => $png_id)
353+
$image-> save( \*FILE, codecID => $png->{codecID})
354+
or die "Cannot save:$@";
355+
356+
or alternatively,
357+
358+
$image-> save( \*FILE, format => 'png')
355359
or die "Cannot save:$@";
356360

357361
=head2 Multiframe saving

0 commit comments

Comments
 (0)