Merge branch 'master' of t61p:/home/dpavlin/klin/Printer-EVOLIS/
[Printer-EVOLIS.git] / evolis-printer.pl
1 #!/usr/bin/perl
2
3 # Simulate EVOLIS Dualys printer
4
5 use warnings;
6 use strict;
7
8 use Data::Dump qw(dump);
9
10 my $feeder = {qw(
11 F Feeder
12 M Manual
13 B Auto
14 )};
15
16 local $/ = "\r";
17
18 my $page = 1;
19
20 my $name = $ARGV[0] || 'page';
21
22 sub save_pbm;
23
24 while(<>) {
25         die "no escape at beginning",dump($_) unless s/^(\x00*)\x1B//;
26         warn "WARNING: ", length($1), " extra nulls before ESC\n" if $1;
27         chomp;
28         my @a = split(/;/,$_);
29         my $c = shift @a;
30         if ( $c eq 'Pmi' ) {
31                 my $f = $a[0] || die 'missing feeder';
32                 print "$_ feeder: $feeder->{$f}\n";
33                 $a[1] eq 's' or die;
34         } elsif ( $c eq 'Pc' ) {
35                 my $color = $a[0];
36                 $a[1] eq '=' or die;
37                 my $temperature = $a[2];
38                 print "$_ temperature $color = $temperature\n";
39         } elsif ( $c eq 'Pl' ) {
40                 $a[1] eq '=' or die;
41                 print "$_ brightnes? $a[0] = $a[2]\n";
42         } elsif ( $c eq 'Ps' ) {
43                 $a[0] eq 'k' or die;
44                 $a[1] eq '=' or die;
45                 print "$_ qualityK $a[0] = $a[2]\n";
46         } elsif ( $c eq 'Pr' ) {
47                 print "$_ improve (not in cups)\n";
48         } elsif ( $c eq 'Ss' ) {
49                 print "$_ encoding download",dump(@a),"\n";
50         } elsif ( $c eq 'Sv' ) {
51                 print "$_ even page on duplex printing\n";
52         } elsif ( $c eq 'Sr' ) {
53                 print "$_ odd page\n";
54         } elsif ( $c eq 'Db' ) { # XXX not in cups
55                 my ( $color, $two, $data ) = @a;
56                 print "$c;$color;$two;... bitmap\n";
57                 $two eq '2' or die '2';
58                 my $path = "$name-Db-$color-$page.pbm"; $page++;
59                 save_pbm $path, 648, 1015, $data;       # FIXME 1016?
60         } elsif ( $c eq 'Dbc' ) { # XXX not in cups
61                 my ( $color, $line, $len, $comp ) = @a;
62                 print "$c;$color;$line;$len;... FIXME bitmap - compressed?\n";
63                 while ( $len > length($comp) ) {
64                         warn "# slurp more ",length($comp), " < $len\n";
65                         $comp .= <>;
66                 }
67                 $len == length $comp or warn "wrong length $len != ", length $comp;
68
69                 my $w = 648 / 2;
70
71 =for non-working
72
73                 my $data;
74
75                 my $i = 0;
76                 while ( $i < length $comp ) {
77                         my $len = ord(substr($comp,$i,4));
78                         $i += 1;
79                         warn "$i comp $len\n";
80                         $data .= substr($comp,$i,$len);
81                         $data .= "\x00" x ( $w - $len );
82                         $i += $len;
83                 }
84
85 =cut
86
87                 my $data = $comp;
88
89                 my $path = "$name-Dbc-$color-$page.pbm"; $page++;
90                 my $h = int( $len / 128 );
91                 save_pbm $path, $w, $h, $data;
92
93         } elsif ( $c eq 'Se' ) {
94                 my $zero = <>;
95                 print "$_ slurping zero bytes at end ",dump($zero),"\n";
96                 exit 0;
97         } else {
98                 print "FIXME: $_\n";
99         }
100 }
101
102 sub save_pbm {
103         my ( $path, $w, $h, $data ) = @_;
104         open(my $pbm, '>', $path);
105         print $pbm "P4\n$w $h\n", $data;
106         close($pbm);
107         print "saved $path $w * $h size ", -s $path, "\n";
108 }