Readonly::Scalar
our
$EMPTY_STR
=>
q{}
;
Readonly::Array
our
@PATTERNS
=>
qw(checkerboard)
;
our
$VERSION
= 0.07;
sub
new {
my
$class
=
shift
;
my
$self
=
bless
{},
$class
;
return
$self
;
}
sub
run {
my
$self
=
shift
;
$self
->{
'_opts'
} = {
'h'
=> 0,
'i'
=>
$EMPTY_STR
,
'p'
=>
undef
,
's'
=>
'1920x1080'
,
'v'
=> 0,
};
if
(! getopts(
'hi:p:s:v'
,
$self
->{
'_opts'
})
||
$self
->{
'_opts'
}->{
'h'
}
||
@ARGV
< 1) {
print
STDERR
"Usage: $0 [-h] [-i input_dir] [-p pattern] [-s size] [-v]"
.
"\n\t[--version] output_file\n\n"
;
print
STDERR
"\t-h\t\tPrint help.\n"
;
print
STDERR
"\t-i input_dir\tInput directory with "
.
"images (default value is nothing).\n"
;
print
STDERR
"\t-p pattern\tPattern (checkerboard).\n"
;
print
STDERR
"\t-s size\t\tSize (default value is "
.
"1920x1080).\n"
;
print
STDERR
"\t-v\t\tVerbose mode.\n"
;
print
STDERR
"\t--version\tPrint version.\n"
;
return
1;
}
$self
->{
'_output_file'
} =
$ARGV
[0];
if
(
$self
->{
'_opts'
}->{
's'
} !~ m/^(\d+)x(\d+)$/ms) {
err
'Bad size value.'
,
'Value'
,
$self
->{
'_opts'
}->{
's'
};
}
$self
->{
'_width'
} = $1;
$self
->{
'_height'
} = $2;
if
(
defined
$self
->{
'_opts'
}->{
'p'
}
&& none {
$self
->{
'_opts'
}->{
'p'
} eq
$_
}
@PATTERNS
) {
err
'Bad pattern.'
;
}
eval
{
my
(
undef
,
undef
,
$suffix
) = fileparse(
$self
->{
'_output_file'
},
qr{\..*$}
);
$suffix
=~ s/^\.//g;
my
$type
=
lc
(
$suffix
);
if
(
$type
eq
'jpg'
) {
$type
=
'jpeg'
;
}
my
$ig
;
if
(
$self
->{
'_opts'
}->{
'i'
} ne
$EMPTY_STR
) {
$ig
= Image::Select->new(
'debug'
=> (
$self
->{
'_opts'
}->{
'v'
} ? 1 : 0),
'height'
=>
$self
->{
'_height'
},
'path_to_images'
=>
$self
->{
'_opts'
}->{
'i'
},
'type'
=>
$type
,
'width'
=>
$self
->{
'_width'
},
);
}
elsif
(
defined
$self
->{
'_opts'
}->{
'p'
}
&&
$self
->{
'_opts'
}->{
'p'
} eq
'checkerboard'
) {
$ig
= Image::Checkerboard->new(
'height'
=>
$self
->{
'_height'
},
'type'
=>
$type
,
'width'
=>
$self
->{
'_width'
},
);
}
else
{
$ig
= Image::Random->new(
'height'
=>
$self
->{
'_height'
},
'type'
=>
$type
,
'width'
=>
$self
->{
'_width'
},
);
}
$ig
->create(
$self
->{
'_output_file'
});
};
if
(
$EVAL_ERROR
) {
err
'Cannot create image.'
;
}
return
0;
}
1;