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