in reply to Perl BMP -- Text
#!/usr/bin/perl -w use strict; # reads a 2-color XPM; outputs space for white, asterisk for black my @lines = map { tr/",//d; $_ } grep {!m{^/\*|[{}]}} <>; my ($header, @c) = splice(@lines, 0, 3); my @dims = $header =~ m{(\d+)}g; die "has too many colors or is in wrong format\n" if $dims[2] > 2 || $ +dims[3] > 1; my $text = join('', @lines); # process colormap my ($w, $b); foreach (@c) { m{^\s*(\S).*#(\S+)\s*$}; hex($2) ? $w = $1 : $b = $1 } eval "\$text =~ tr/$w$b/ */"; print $text;
And, in case the prior was too readable, here's a regex-based version of the above...
update: shortened code a bit, added second version#!/usr/bin/perl -w # reads a 2-color XPM; outputs space for white, asterisk for black use strict; $/ = undef; my $text = <>; $text =~ s{^[^"]*\n}{}gm; # remove noise lines $text =~ s{^\s*"\s*([^"]*\S)\s*"\s*,\s*$}{$1}gm; # unquote others $text =~ s{^\d+\s+\d+\s+(\d+)\s+(\d+)\n # dimensions (.)\s+c\s+\#([0-9a-fA-F]+)\n # colormap 0 (.)\s+c\s+\#([0-9a-fA-F]+)\n }{}mx; # colormap 1 die "has too many colors or is in wrong format\n" if $1 > 2 || $2 > 1; my ($w, $b) = hex($4) > hex($6) ? ( $3,$5 ) : ( $5,$3 ); eval "\$text =~ tr/$w$b/ */"; print $text;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Perl BMP -- Text
by asiufy (Monk) on Jun 22, 2001 at 02:45 UTC | |
by bikeNomad (Priest) on Jun 22, 2001 at 03:58 UTC |