X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/16fb9fc2233f3a3c0b61aee034da4c49979e30c2..721416af567c48d7963d771e2f282c390a2763ca:/termcol.plp?ds=sidebyside diff --git a/termcol.plp b/termcol.plp index 04157ef..0068acc 100644 --- a/termcol.plp +++ b/termcol.plp @@ -14,14 +14,14 @@ Html({ stylesheet => [qw'light dark'], }); -my $imgfile = exists $get{img} && 'indi.png'; +my $imgfile = $get{img} // exists $get{img} && 'indi.png'; my @termlist; push @termlist, split /\W+/, $ENV{PATH_INFO} || 'default'; my %termgroup = ( default => [qw( ansi xkcd ansi88 )], - more => [qw( ansi legacy ansi256 )], + more => [qw( ansi mirc legacy ansi256 )], msx => [qw( msx1 msx2 arnejmp )], ansi => [qw( cga xterm tango app html )], legacy => [qw( c64 msx2 mac2 risc arnegame cpc )], @@ -75,20 +75,28 @@ sub colcell { sub img_egapal { my ($palette) = @_; return eval { + require Imager; require MIME::Base64; - require Digest::CRC; - - local $/; - open my $img, '<:bytes', "data/$imgfile" or die "$!\n"; - my $imgdata = readline $img; - - my $offset = 0x29 - 4; - my $len = 16 * 3; - my $chunklen = $len + 4; - substr($imgdata, $offset+4, $len) = pack 'H*', join '', @{$palette}; - my ($p, $crc) = unpack "x${offset}a${chunklen}N", $imgdata; - substr($imgdata, $offset+4+$len, 4) = pack 'N', Digest::CRC::crc32($p); + my @imgpal = map { Imager::Color->new(ref $_ ? @$_ : $_) } @{$palette}; + state $reindex = $imgfile =~ s/!$//; + state $img = Imager->new(file => "data/palimage/$imgfile") + or die Imager->errstr.$/; + do { + if ($reindex) { + $img->to_paletted({ + make_colors => 'none', + colors => \@imgpal, + translate => 'closest', + }); + } + else { + @{[ $img->getcolors ]} == @imgpal + or die "incompatible palette size\n"; + $img->setcolors(colors => \@imgpal); + $img; + } + }->write(data => \my $imgdata, type => 'png'); return sprintf '', MIME::Base64::encode_base64($imgdata); } || $@;