From: Mischa POSLAWSKY Date: Mon, 2 Nov 2015 05:14:51 +0000 (+0100) Subject: termcol: use Imager for generic PNG generation X-Git-Tag: v1.9~71 X-Git-Url: http://git.shiar.nl/sheet.git/commitdiff_plain/54ea9d7c62215933a9b77f16b28717ea65a178ad?ds=sidebyside termcol: use Imager for generic PNG generation Obviously less efficient than replacing palette at a hardcoded position, but more flexible and reliable with different sources. Still only loaded if requested. --- diff --git a/termcol.plp b/termcol.plp index 04157ef..73bf0d2 100644 --- a/termcol.plp +++ b/termcol.plp @@ -14,7 +14,7 @@ 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'; @@ -75,20 +75,16 @@ 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 $img = Imager->new(file => "data/$imgfile") + or die Imager->errstr.$/; + @{[ $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); } || $@;