| 1 | package Image::Select; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Pragmas. | ||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # Modules. | ||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Class::Utils qw(set_params); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use File::Basename qw(fileparse); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Find::Rule qw(:MMagic); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Imager; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use List::MoreUtils qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Version. | ||||||||||||||||||||||||||||
| 16 | 3 | our $VERSION = 0.03; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Constructor. | ||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | 6 | ▶ | my ($class, @params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Create object. | ||||||||||||||||||||||||||||
| 23 | 6 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Debug. | ||||||||||||||||||||||||||||
| 26 | 6 | $self->{'debug'} = 0; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Loop. | ||||||||||||||||||||||||||||
| 29 | 6 | $self->{'loop'} = 0; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Path to images. | ||||||||||||||||||||||||||||
| 32 | 6 | $self->{'path_to_images'} = undef; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Image type. | ||||||||||||||||||||||||||||
| 35 | 6 | $self->{'type'} = 'bmp'; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Sizes. | ||||||||||||||||||||||||||||
| 38 | 6 | $self->{'height'} = 1080; | |||||||||||||||||||||||||||
| 39 | 6 | $self->{'width'} = 1920; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Process params. | ||||||||||||||||||||||||||||
| 42 | 6 | set_params($self, @params); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Check type. | ||||||||||||||||||||||||||||
| 45 | 4 | ▶ | if (defined $self->{'type'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 3 | $self->_check_type($self->{'type'}); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Check path to images. | ||||||||||||||||||||||||||||
| 50 | 3 | ▶ | if (! defined $self->{'path_to_images'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"path_to_images"} and -d $self->{"path_to_images"}
MC/DC50%1/2
defined $self->{"path_to_images"} and -d $self->{"path_to_images"}
defined $self->{"path_to_images"}
-d $self->{"path_to_images"}
Truth table66%2/3
A and B
A= defined $self->{"path_to_images"}B= -d $self->{"path_to_images"}
| |||||||||||||||||||||||||||||
| 51 | || ! -d $self->{'path_to_images'}) { | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | err "Parameter 'path_to_images' is required."; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Load images. | ||||||||||||||||||||||||||||
| 57 | $self->{'_images_to_select'} = [ | ||||||||||||||||||||||||||||
| 58 | sort File::Find::Rule->file->magic( | ||||||||||||||||||||||||||||
| 59 | 'image/bmp', | ||||||||||||||||||||||||||||
| 60 | 'image/gif', | ||||||||||||||||||||||||||||
| 61 | 'image/jpeg', | ||||||||||||||||||||||||||||
| 62 | 'image/png', | ||||||||||||||||||||||||||||
| 63 | 'image/tiff', | ||||||||||||||||||||||||||||
| 64 | 'image/x-ms-bmp', | ||||||||||||||||||||||||||||
| 65 | 'image/x-portable-pixmap', | ||||||||||||||||||||||||||||
| 66 | # XXX tga? | ||||||||||||||||||||||||||||
| 67 | # XXX raw? | ||||||||||||||||||||||||||||
| 68 | # XXX sgi? | ||||||||||||||||||||||||||||
| 69 | 2 | )->in($self->{'path_to_images'}), | |||||||||||||||||||||||||||
| 70 | ]; | ||||||||||||||||||||||||||||
| 71 | 2 | ▶ | if (! @{$self->{'_images_to_select'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | err 'No images.'; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 2 | $self->{'_images_index'} = 0; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Object. | ||||||||||||||||||||||||||||
| 77 | 2 | return $self; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Create image. | ||||||||||||||||||||||||||||
| 81 | sub create { | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
create: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # Load next image. | ||||||||||||||||||||||||||||
| 85 | 0 | my $i = Imager->new; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($self->{'_images_index'} > $#{$self->{'_images_to_select'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($self->{'loop'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $self->{'_images_index'} = 0; | |||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 0 | my $file = $self->{'_images_to_select'}->[$self->{'_images_index'}]; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (! -r $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | err "No file '$file'."; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 0 | my $ret = $i->read('file' => $file); | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (! $ret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | err "Cannot read file '$file'.", | |||||||||||||||||||||||||||
| 100 | 'Error', Imager->errstr; | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | $self->{'_images_index'}++; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Get type. | ||||||||||||||||||||||||||||
| 105 | 0 | my $suffix; | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (! defined $self->{'type'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Get suffix. | ||||||||||||||||||||||||||||
| 109 | 0 | (my $name, undef, $suffix) = fileparse($path, qr/\.[^.]*/ms); | |||||||||||||||||||||||||||
| 110 | 0 | $suffix =~ s/^\.//ms; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Jpeg. | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($suffix eq 'jpg') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $suffix = 'jpeg'; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Check type. | ||||||||||||||||||||||||||||
| 118 | 0 | $self->_check_type($suffix); | |||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||
| 120 | 0 | $suffix = $self->{'type'}; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Scale. | ||||||||||||||||||||||||||||
| 124 | my $new_i = $i->scale( | ||||||||||||||||||||||||||||
| 125 | 'xpixels' => $self->{'width'}, | ||||||||||||||||||||||||||||
| 126 | 0 | 'ypixels' => $self->{'height'}, | |||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (! $new_i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | err "Cannot resize image from file '$file'.", | |||||||||||||||||||||||||||
| 130 | 'Error', Imager->errstr; | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Save. | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($self->{'debug'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | print "Path: $path\n"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | $ret = $new_i->write( | |||||||||||||||||||||||||||
| 138 | 'file' => $path, | ||||||||||||||||||||||||||||
| 139 | 'type' => $suffix, | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (! $ret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | err "Cannot write file to '$path'.", | |||||||||||||||||||||||||||
| 143 | 'Error', Imager->errstr; | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | return $suffix; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Set/Get image sizes. | ||||||||||||||||||||||||||||
| 150 | sub sizes { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my ($self, $width, $height) = @_; | ||||||||||||||||||||||||||
|
Subroutine
sizes: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($width && $height) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$width and $height
MC/DC0%0/2
$width and $height
$width
$height
Truth table0%0/3
A and B
A= $widthB= $height
| |||||||||||||||||||||||||||||
| 153 | 0 | $self->{'width'} = $width; | |||||||||||||||||||||||||||
| 154 | 0 | $self->{'height'} = $height; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 0 | return ($self->{'width'}, $self->{'height'}); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Set/Get image type. | ||||||||||||||||||||||||||||
| 160 | sub type { | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my ($self, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
type: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | if ($type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | $self->_check_type($type); | |||||||||||||||||||||||||||
| 164 | 0 | $self->{'type'} = $type; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | return $self->{'type'}; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # Check supported image type. | ||||||||||||||||||||||||||||
| 170 | sub _check_type { | ||||||||||||||||||||||||||||
| 171 | 3 | ▶ | my ($self, $type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_check_type: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # Check type. | ||||||||||||||||||||||||||||
| 174 | 3 | ▶ | if (none { $type eq $_ } ('bmp', 'gif', 'jpeg', 'png', | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 'pnm', 'raw', 'sgi', 'tga', 'tiff')) { | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | err "Image type '$type' doesn't supported."; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 2 | return; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 3 | 1; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||