recover CR is slurping more data
[Printer-EVOLIS.git] / scripts / evolis-simulator.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 die "usage: $0 evolis.commands\n" unless @ARGV;
11 my $name = shift @ARGV;
12
13 local $/ = "\r";
14
15 my $page = 1;
16
17 sub save_pbm;
18
19 open(my $e, '<', $name) || die "$name: $!";
20 sub bitmap {
21         my ($data,$len) = @_;
22 warn "# tell ",tell($e),"\n";
23         $data =~ s/D.+;\d+;//;
24         $data =~ s/\r$//;
25         my $l = length $data;
26         warn "# bitmap $l $len\n";
27         return $data if length $data == $len;
28         $data .= "\r";
29         my $rest;
30         my $l = $len - length $data;
31         read $e, $rest, $l;
32         warn "# slurp $l got ",length($rest);
33         return $data . $rest;
34 }
35
36 while(<$e>) {
37         die "no escape at beginning",dump($_) unless s/^(\x00*)\x1B//;
38         warn "WARNING: ", length($1), " extra nulls before ESC\n" if $1;
39         my @a = split(/;/,$_);
40         my $c = shift @a;
41         chomp $c;
42         if ( $c eq 'Pmi' ) {
43                 print "$_ mode insertion @a\n";
44         } elsif ( $c eq 'Pc' ) {
45                 print "$_ contrast @a\n";
46         } elsif ( $c eq 'Pl' ) {
47                 print "$_ luminosity @a\n";
48         } elsif ( $c eq 'Ps' ) {
49                 print "$_ speed @a\n";
50         } elsif ( $c eq 'Pr' ) {
51                 print "$_ ribbon $a[0]\n";
52         } elsif ( $c eq 'Ss' ) {
53                 print "$_ sequence start\n";
54         } elsif ( $c eq 'Se' ) {
55                 print "$_ sequence end\n";
56         } elsif ( $c eq 'Sr' ) {
57                 print "$_ sequence recto - card side\n";
58         } elsif ( $c eq 'Sv' ) {
59                 print "$_ sequence verso - back side\n";
60         } elsif ( $c eq 'Db' ) {
61                 my ( $color, $two ) = @a;
62                 print "$c;$color;$two;... bitmap ",length($_), " bytes\n";
63                 $two eq '2' or die 'only 2 colors supported';
64                 my $path = "$name-Db-$color-$page.pbm"; $page++;
65                 save_pbm $path, 648, 1016, bitmap( $_, 648 * 1016 / 8 );
66         } elsif ( $c eq 'Dbc' ) { # XXX not in cups
67                 my ( $color, $line, $len ) = @a;
68                 print "$c;$color;$line;$len;... download bitmap compressed\n";
69                 my $comp = bitmap( $_, $len );
70
71                 die "compression not supported" unless $color =~ m/[ko]/;
72
73                 my $data;
74                 my $i = 0;
75                 while ( $i < length $comp ) {
76                         my $first = substr($comp,$i++,1);
77                         if ( $first eq "\x00" ) {
78                                 $data .= "\x00" x 81;
79                         } elsif ( $first eq "\xFF" ) {
80                                 $data .= "\xFF" x 81;
81                         } else {
82                                 my $len = ord $first;
83                                 $data .= substr($comp,$i,$len);
84                                 my $padding = 81 - $len;
85 #warn "# $len + $padding\n";
86                                 $data .= "\x00" x $padding;
87                                 $i += $len;
88                         }
89                 }
90
91                 my $path = "$name-Dbc-$color-$page.pbm"; $page++;
92                 save_pbm $path, 648, 1016, $data;
93
94         } elsif ( $c eq 'Mr' ) {
95                 print "$_ motor ribbon @a\n";
96         } else {
97                 print "FIXME: $_\n";
98         }
99 }
100
101 sub save_pbm {
102         my ( $path, $w, $h, $data ) = @_;
103         open(my $pbm, '>', $path);
104         print $pbm "P4\n$w $h\n", $data;
105         close($pbm);
106         print "saved $path $w * $h size ", -s $path, "\n";
107 }