has
'width'
=> (
is
=>
'rw'
,
lazy
=> 1,
isa
=> Int,
default
=> 0 );
has
'height'
=> (
is
=>
'rw'
,
lazy
=> 1,
isa
=> Int,
default
=> 0 );
has
'pixeldata'
=> (
is
=>
'rw'
,
lazy
=> 1,
isa
=> ArrayRef,
default
=>
sub
{ [] } );
sub
getpixel {
my
(
$self
,
$x
,
$y
) =
@_
;
return
unless
exists
$self
->pixeldata->[
$y
];
return
$self
->pixeldata->[
$y
]->[
$x
];
}
sub
getpixel_obj {
my
(
$self
,
$x
,
$y
,
$options
) =
@_
;
my
$pixel
=
$self
->getpixel(
$x
,
$y
);
return
unless
$pixel
;
return
Image::TextMode::Pixel->new(
%$pixel
,
$options
);
}
sub
putpixel {
my
(
$self
,
$pixel
,
$x
,
$y
) =
@_
;
$self
->pixeldata->[
$y
]->[
$x
] =
$pixel
;
my
(
$w
,
$h
) = (
$x
+ 1,
$y
+ 1 );
$self
->height(
$h
)
if
$self
->height <
$h
;
$self
->width(
$w
)
if
$self
->width <
$w
;
}
sub
dimensions {
my
$self
=
shift
;
return
$self
->width,
$self
->height;
}
sub
clear_screen {
my
$self
=
shift
;
$self
->width( 0 );
$self
->height( 0 );
$self
->pixeldata( [] );
}
sub
clear_line {
my
$self
=
shift
;
my
$y
=
shift
;
my
$range
=
shift
;
return
unless
defined
$self
->pixeldata->[
$y
];
if
( !
$range
) {
$self
->pixeldata->[
$y
] = [];
}
else
{
$range
->[ 1 ] = @{
$self
->pixeldata->[
$y
] } - 1
if
$range
->[ 1 ] == -1;
$self
->pixeldata->[
$y
]->[
$_
] =
undef
for
$range
->[ 0 ] ..
$range
->[ 1 ];
}
}
sub
delete_line {
my
$self
=
shift
;
my
$y
=
shift
;
return
unless
exists
$self
->pixeldata->[
$y
];
delete
@{
$self
->pixeldata }[
$y
];
$self
->height(
$self
->height - 1 );
}
sub
as_ascii {
my
(
$self
) =
@_
;
my
$output
=
''
;
for
my
$row
( @{
$self
->pixeldata } ) {
for
my
$col
(
@$row
) {
$output
.=
defined
$col
&&
defined
$col
->{ char } ?
$col
->{ char } :
' '
;
}
$output
.=
"\n"
;
}
return
$output
;
}
sub
max_x {
my
(
$self
,
$y
) =
@_
;
my
$line
=
$self
->pixeldata->[
$y
];
return
unless
$line
;
my
$x
;
for
( 0 ..
@$line
- 1 ) {
$x
=
$_
if
defined
$line
->[
$_
];
}
return
$x
;
}
sub
ansiscale {
my
(
$self
,
$factor
) =
@_
;
my
$new
= (
ref
$self
)->new;
my
$width
=
$self
->width *
$factor
;
my
$height
=
$self
->height *
$factor
;
$width
=
int
(
$width
+ 1 )
if
int
(
$width
) !=
$width
;
$height
=
int
(
$height
+ 1 )
if
int
(
$height
) !=
$height
;
my
$oldpixels
=
$self
->pixeldata;
my
$newpixels
= [];
my
$inv_ratio
= ( 1 /
$factor
);
for
my
$y
( 0 ..
$height
- 1 ) {
for
my
$x
( 0 ..
$width
- 1 ) {
my
$px
=
int
(
$x
*
$inv_ratio
);
my
$py
=
int
(
$y
*
$inv_ratio
);
$newpixels
->[
$y
]->[
$x
] =
$oldpixels
->[
$py
]->[
$px
];
}
}
$new
->width(
$width
);
$new
->height(
$height
);
$new
->pixeldata(
$newpixels
);
return
$new
;
}
1;