blib/lib/Term/Fabulous/Widget/Sixel.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 183 CC 3 SCAR 13.9
CC
3
Cov
0%
_load_sixel_writer
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 4.0
1 package Term::Fabulous::Widget::Sixel;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Widget::Image;
11  
12 our $VERSION = '0.05';
13  
14 class Term::Fabulous::Widget::Sixel
15 :isa(Term::Fabulous::Widget::Image)
16 :strict(params)
17 {
18 use Feature::Compat::Try;
19 use List::Util (); # max is a method elsewhere in the family
20 use POSIX qw(ceil);
21  
22 use constant TERMINAL_NOTICE => 'Sixel needs a terminal that shows sixel graphics and reports the size of its cells.';
23  
24 # Imager::File::SIXEL is optional like Imager, which it needs: without
25 # either, the widget shows a notice. Any other error than a missing
26 # Imager/File/SIXEL.pm is a broken installation.
27 sub _load_sixel_writer () { ## no critic (Subroutines::RequireFinalReturn) PPI does not parse try/catch
28 return 0 unless Term::Fabulous::Widget::Image::HAS_IMAGER;
29 try {
30 require Imager::File::SIXEL;
31 }
32 catch ($error) {
33 die $error unless $error =~ m{\ACan't locate Imager/File/SIXEL\.pm };
34 return 0;
35 }
36 return 1;
37 }
38 use constant HAS_SIXEL_WRITER => _load_sixel_writer();
39  
40 # The last picture encoded: what it was encoded for, and its SIXEL data.
41 field $_encoded_for = '';
42 field $_encoded;
43  
44 # ---------------------------------------------------------------------
45 # What the terminal can show
46 # ---------------------------------------------------------------------
47  
48 # The pixels of a cell of the terminal the widget is shown on, empty
49 # outside a UI and on a terminal that shows no sixel.
50 method _cell_size () {
51 my $ui = $self->ui // return ();
52 return () unless $ui->can('cell_target');
53 my $target = $ui->cell_target;
54 return () unless $target->DOES('Term::Fabulous::Render::Target::Sixel');
55 return $target->sixel_cell_size;
56 }
57  
58 method _notice :override () {
59 return 'Sixel needs the Perl module Imager, which is not installed.' unless Term::Fabulous::Widget::Image::HAS_IMAGER;
60 return 'Sixel needs the Perl module Imager::File::SIXEL, which is not installed.' unless HAS_SIXEL_WRITER;
61 my @cell_size = $self->_cell_size;
62 return @cell_size ? undef : TERMINAL_NOTICE;
63 }
64  
65 # ---------------------------------------------------------------------
66 # Painting
67 # ---------------------------------------------------------------------
68  
69 method natural_size :override () {
70 my $notice = $self->_notice;
71 return $self->_notice_size($notice) if defined $notice;
72 my $picture = $self->_picture // return ( 0, 0 );
73 my ( $cell_width, $cell_height ) = $self->_cell_size;
74 return ( ceil( $picture->getwidth / $cell_width ), ceil( $picture->getheight / $cell_height ) );
75 }
76  
77 # Whether the notice shows depends on the terminal, too.
78 method paint_key :override () {
79 return ( $self->SUPER::paint_key, $self->_notice // '' );
80 }
81  
82 # The cells stay empty below the picture, which the terminal draws
83 # over them.
84 method paint :override () {
85 my $notice = $self->_notice;
86 $self->_paint_notice($notice) if defined $notice;
87 return;
88 }
89  
90 method sixel_data :override (%frame) {
91 return undef if defined $self->_notice || !defined $self->_picture;
92 my $key = join "\x{1F}", ( map { $_ // '' } $self->paint_key ), map { "@$_" } $frame{cell_size}, $frame{shown}, @{ $frame{covered} };
93 ( $_encoded_for, $_encoded ) = ( $key, $self->_encoded(%frame) ) unless $key eq $_encoded_for;
94 return $_encoded;
95 }
96  
97 # The pixels of the shown cells: the picture placed as fit says,
98 # transparent around it and in the covered cells.
99 method _encoded (%frame) {
100 my ( $cell_width, $cell_height ) = @{ $frame{cell_size} };
101 my ( $x0, $y0, $x1, $y1 ) = @{ $frame{shown} };
102 my ( $width, $height, $left, $top ) = $self->_placement( $self->columns * $cell_width, $self->rows * $cell_height );
103  
104 my $shown = Imager->new( xsize => ( $x1 - $x0 ) * $cell_width, ysize => ( $y1 - $y0 ) * $cell_height, channels => 4 );
105 _paste( $shown, $self->_on_background( $self->_scaled( $width, $height ) ), $left - $x0 * $cell_width, $top - $y0 * $cell_height );
106 my $transparent = Imager::Color->new( 0, 0, 0, 0 );
107 foreach my $covered ( @{ $frame{covered} } ) {
108 my ( $cx0, $cy0, $cx1, $cy1 ) = @$covered;
109 $shown->box(
110 xmin => ( $cx0 - $x0 ) * $cell_width,
111 ymin => ( $cy0 - $y0 ) * $cell_height,
112 xmax => ( $cx1 - $x0 ) * $cell_width - 1,
113 ymax => ( $cy1 - $y0 ) * $cell_height - 1,
114 filled => 1,
115 color => $transparent,
116 );
117 }
118 $shown->write( data => \my $data, type => 'sixel' ) or die ref($self) . ": cannot write the picture as sixel: " . $shown->errstr;
119 return $data;
120 }
121  
122 # Translucent pixels are mixed with the background below the widget,
123 # like Image does, and transparent ones stay transparent: the mix is
124 # copied through a mask of the pixels with any alpha. Without a
125 # background, the terminal draws the pixels of at least half alpha
126 # opaque and leaves out the others.
127 method _on_background ($image) {
128 my $background = $self->background_below( translucent => 1 ) // return $image;
129 my ( $width, $height ) = ( $image->getwidth, $image->getheight );
130 my $mixed = Imager->new( xsize => $width, ysize => $height, channels => 4 );
131 $mixed->box( filled => 1, color => Imager::Color->new( @{$background}[ 0 .. 2 ], 255 ) );
132 $mixed->compose( src => $image ) or die ref($self) . ": cannot mix the picture with the background: " . $mixed->errstr;
133  
134 my $painted = $image->convert( matrix => [ [ 0, 0, 0, 1 ] ] )->map( maps => [ [ 0, (255) x 255 ] ] );
135 my $result = Imager->new( xsize => $width, ysize => $height, channels => 4 );
136 $result->compose( src => $mixed, mask => $painted ) or die ref($self) . ": cannot mix the picture with the background: " . $result->errstr;
137 return $result;
138 }
139  
140 # Copies $image to ($left, $top) of $target, cut at $target's edges.
141 sub _paste ( $target, $image, $left, $top ) {
142 my ( $from_x, $from_y ) = ( List::Util::max( 0, -$left ), List::Util::max( 0, -$top ) );
143 return if $from_x >= $image->getwidth || $from_y >= $image->getheight;
144 return if $left >= $target->getwidth || $top >= $target->getheight;
145 $target->paste( src => $image, left => List::Util::max( 0, $left ), top => List::Util::max( 0, $top ), src_minx => $from_x, src_miny => $from_y );
146 return;
147 }
148 }
149  
150 1;
151  
152 __END__