in reply to Perl BMP -- Text

If you can use XPM, here's a solution that doesn't require any external packages:

#!/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...

#!/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;
update: shortened code a bit, added second version

Replies are listed 'Best First'.
Re: Re: Perl BMP -- Text
by asiufy (Monk) on Jun 22, 2001 at 02:45 UTC
    Any chance this could be done with GIFs or BMPs???
      Not mine, but ar0n's method using GD should work with any image format that GD can read (this depends on how you compile it, of course). GD supports WBMP images. However, its author reacted1 to the Unisys patent flap by removing GIF support from GD. So you'd have to convert your GIFs to something else before using GD.

      1 some people would say he overreacted, but it's his business what to provide. There are people who would be glad to see patches to GD that would allow GIF manipulation.