From: Dobrica Pavlinusic Date: Sun, 5 Feb 2012 19:27:10 +0000 (+0100) Subject: implement ZPL run-length compression and turn it on by default X-Git-Url: http://git.rot13.org/?p=Printer-Zebra.git;a=commitdiff_plain;h=d09c10a1bec86173144af652c02478ccdd684b3c implement ZPL run-length compression and turn it on by default --- diff --git a/pbm2ZPL.pl b/pbm2ZPL.pl index c570840..7ca1c64 100755 --- a/pbm2ZPL.pl +++ b/pbm2ZPL.pl @@ -6,7 +6,7 @@ use Data::Dump qw(dump); # DG compression is documented in ZPL II Programming Guide Volume Two, page 71-72 -my $compress = $ENV{COMPRESS} || 0; +my $compress = $ENV{COMPRESS} || 1; my $pnm_file = shift @ARGV || die "usage: $0 print.pnm"; @@ -26,6 +26,26 @@ printf "~DG000.GRF,%d,%d,\r\n", $w / 8 * $h, $w / 8; my $last_line = ''; +sub zpl_compress { + my $compress = shift; + my $repeat = length($compress); + my $out; + while ( $repeat >= 400 ) { + $out .= 'z'; + $repeat -= 400; + } + if ( $repeat >= 20 ) { + $out .= chr( ord('f') + ( $repeat / 20 ) ); + $repeat %= 20; + } + if ( $repeat > 0 ) { + $out .= chr( ord('F') + $repeat ); + } + $out .= substr($compress,0,1); # char + warn "## zpl_compress $repeat = $compress -> $out\n"; + return $out; +} + foreach my $y ( 0 .. $h - 1 ) { my $line = substr( $bitmap, $y * ( $w / 8 ), $w / 8 ); if ( $line eq $last_line ) { @@ -36,6 +56,7 @@ foreach my $y ( 0 .. $h - 1 ) { # $last_line = $line; $hex =~ s/0+$/,/ && warn "# $y fill 0 to right\n"; $hex =~ s/F+$/!/i && warn "# $y fill 1 to right\n"; + $hex =~ s/((.)\2+)/zpl_compress($1)/egs; } print $hex; }