Image.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 152 CC 17 SCAR 29.6
CC
17
Cov
0%
new
56.0
set
20.0
_get
12.0
CRAP 19.2
1 # Copyright 2010, 2011, 2015 Kevin Ryde
2  
3 # This file is part of Image-Base-Prima.
4 #
5 # Image-Base-Prima is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Prima is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-Prima. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::Prima::Image;
20 use 5.005;
21 use strict;
22 use Carp;
23 use vars '$VERSION', '@ISA';
24  
25 use Image::Base::Prima::Drawable;
26 @ISA = ('Image::Base::Prima::Drawable');
27  
28 $VERSION = 9;
29  
30 # uncomment this to run the ### lines
31 #use Smart::Comments '###';
32  
33 sub new {
34 my ($class, %params) = @_;
35 ### Prima-Image new: \%params
36  
37 # $obj->new(...) means make a copy, with some extra settings
38 if (ref $class) {
39 if (! defined $params{'-drawable'}) {
40 $params{'-drawable'} = $class->{'-drawable'}->dup;
41 }
42 # inherit everything else, in particular '-file' filename
43 %params = (%$class,
44 %params);
45 $class = ref $class;
46 }
47  
48 my $filename = delete $params{'-file'};
49 if (! exists $params{'-drawable'}) {
50 ### create new Prima-Image
51 require Prima;
52 $params{'-drawable'} = Prima::Image->new
53 ((defined $params{'-width'} ? (width =>delete $params{'-width'}) : ()),
54 (defined $params{'-height'} ? (height=>delete $params{'-height'}) : ()),
55 );
56 }
57  
58 my $self = bless {}, $class;
59 $self->set (%params);
60  
61 if (defined $filename) {
62 $self->load ($filename);
63 }
64 return $class->SUPER::new (%params);
65 }
66  
67 # my %get_methods = (-codecID => 'codecID'); # not yet documented
68  
69 my %attr_to_extras = (-hotx => 'hotSpotX',
70 -hoty => 'hotSpotY',
71 );
72  
73 sub _get {
74 my ($self, $key) = @_;
75 ### Prima-Image _get(): $key
76 # if (my $method = $get_methods{$key}) {
77 # return $self->{'-drawable'}->$method;
78 # }
79 if ($key eq '-file_format') {
80 ### extras: $self->{'-drawable'}->{'extras'}
81 return _codecid_to_format ($self->{'-drawable'}->{'extras'}->{'codecID'});
82 }
83 if (my $key = $attr_to_extras{$key}) {
84 return $self->{'-drawable'}->{'extras'}->{$key};
85 }
86 return $self->SUPER::_get($key);
87 }
88  
89 sub set {
90 my ($self, %params) = @_;
91 my $drawable = $params{'-drawable'} || $self->{'-drawable'};
92 if (exists $params{'-file_format'}) {
93 $drawable->{'extras'}->{'codecID'}
94 = _format_to_codecid(delete $params{'-file_format'});
95 }
96 foreach my $attr (keys %attr_to_extras) {
97 if (exists $params{$attr}) {
98 ### $attr
99 ### to extras: $attr_to_extras{$attr}
100 my $value = delete $params{$attr};
101 if (defined $value) {
102 $drawable->{'extras'}->{$attr_to_extras{$attr}} = $value;
103 } else {
104 delete $drawable->{'extras'}->{$attr_to_extras{$attr}};
105 }
106 }
107 }
108 ### extras now: $drawable->{'extras'}
109 $self->SUPER::set(%params);
110 }
111  
112 # $codecid is an integer, return string like "PNG" which is the format name
113 sub _codecid_to_format {
114 my ($codecid) = @_;
115 if (! defined $codecid) {
116 return undef;
117 }
118 return Prima::Image->codecs->[$codecid]->{'fileShortType'};
119 }
120 # $format is a string like "PNG", return integer codecid
121 # upper/lower case $format is allowed
122 sub _format_to_codecid {
123 my ($format) = @_;
124 my $codecs = Prima::Image->codecs;
125 foreach my $id (0 .. $#$codecs) {
126 if ($codecs->[$id]->{'fileShortType'} =~ /\Q$format/i) {
127 return $id;
128 }
129 }
130 croak "No Prima codec for format \"",$format,"\"";
131 }
132  
133 sub load {
134 my ($self, $filename) = @_;
135 ### Prima-Drawable load()
136 if (@_ == 1) {
137 $filename = $self->get('-file');
138 } else {
139 $self->set('-file', $filename);
140 }
141 ### $filename
142  
143 # stringize against refs like Path::Class
144 $self->{'-drawable'}->load ("$filename", loadExtras => 1)
145 or croak "Cannot load $filename: ",$@;
146 ### extras: $self->{'-drawable'}->{extras}
147 }
148  
149 # not yet documented
150 sub load_fh {
151 my ($self, $fh) = @_;
152 ### Prima-Drawable load_fh()
153 $self->{'-drawable'}->load ($fh, loadExtras => 1)
154 or croak $@;
155 }
156  
157 sub save {
158 my ($self, $filename) = @_;
159 ### Prima-Drawable save(): @_
160 if (@_ == 2) {
161 $self->set('-file', $filename);
162 } else {
163 $filename = $self->get('-file');
164 }
165 ### $filename
166  
167 # uses $im->{'extras'}->{'codecID'} if set, otherwise filename extension
168 # stringize against refs like Path::Class
169 $self->{'-drawable'}->save ("$filename",
170 (defined $self->{'-quality_percent'}
171 ? (quality => $self->{'-quality_percent'})
172 : ()))
173 or croak "Cannot save $filename: ",$@;
174 }
175  
176 # not yet documented
177 sub save_fh {
178 my ($self, $fh) = @_;
179 # uses $im->{'extras'}->{'codecID'} and croaks if that not set
180 $self->{'-drawable'}->save ($fh)
181 or croak $@;
182 }
183  
184 1;
185 __END__