87758c683de41d75549070c2b29a1b0069731d81
[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 local $/ = "\r";
11
12 my $page = 1;
13
14 my $name = $ARGV[0] || 'page';
15
16 sub save_pbm;
17
18 while(<>) {
19         die "no escape at beginning",dump($_) unless s/^(\x00*)\x1B//;
20         warn "WARNING: ", length($1), " extra nulls before ESC\n" if $1;
21         chomp;
22         my @a = split(/;/,$_);
23         my $c = shift @a;
24         if ( $c eq 'Pmi' ) {
25                 print "$_ mode insertion @a\n";
26         } elsif ( $c eq 'Pc' ) {
27                 print "$_ contrast @a\n";
28         } elsif ( $c eq 'Pl' ) {
29                 print "$_ luminosity @a\n";
30         } elsif ( $c eq 'Ps' ) {
31                 print "$_ speed @a\n";
32         } elsif ( $c eq 'Pr' ) {
33                 print "$_ ribbon $a[0]\n";
34         } elsif ( $c eq 'Ss' ) {
35                 print "$_ sequence start\n";
36         } elsif ( $c eq 'Se' ) {
37                 print "$_ sequence end\n";
38         } elsif ( $c eq 'Sr' ) {
39                 print "$_ sequence recto - card side\n";
40         } elsif ( $c eq 'Sv' ) {
41                 print "$_ sequence verso - back side\n";
42         } elsif ( $c eq 'Db' ) {
43                 my ( $color, $two, $data ) = @a;
44                 print "$c;$color;$two;... bitmap\n";
45                 $two eq '2' or die '2';
46                 my $path = "$name-Db-$color-$page.pbm"; $page++;
47                 save_pbm $path, 648, 1015, $data;       # FIXME 1016?
48         } elsif ( $c eq 'Dbc' ) { # XXX not in cups
49                 my ( $color, $line, $len, $comp ) = @a;
50                 print "$c;$color;$line;$len;... FIXME bitmap - compressed?\n";
51                 while ( $len > length($comp) ) {
52                         warn "# slurp more ",length($comp), " < $len\n";
53                         $comp .= <>;
54                 }
55                 $len == length $comp or warn "wrong length $len != ", length $comp;
56
57                 my $w = 648 / 2;
58
59 =for non-working
60
61                 my $data;
62
63                 my $i = 0;
64                 while ( $i < length $comp ) {
65                         my $len = ord(substr($comp,$i,4));
66                         $i += 1;
67                         warn "$i comp $len\n";
68                         $data .= substr($comp,$i,$len);
69                         $data .= "\x00" x ( $w - $len );
70                         $i += $len;
71                 }
72
73 =cut
74
75                 my $data = $comp;
76
77                 my $path = "$name-Dbc-$color-$page.pbm"; $page++;
78                 my $h = int( $len / 128 );
79                 save_pbm $path, $w, $h, $data;
80
81         } else {
82                 print "FIXME: $_\n";
83         }
84 }
85
86 sub save_pbm {
87         my ( $path, $w, $h, $data ) = @_;
88         open(my $pbm, '>', $path);
89         print $pbm "P4\n$w $h\n", $data;
90         close($pbm);
91         print "saved $path $w * $h size ", -s $path, "\n";
92 }