#!/usr/bin/perl
# anytotxt.pl - Don Yang (uguu.org)
#
# 12/03/05
use strict;
die "$0 \n" unless $#ARGV == 2;
my ($input, $width, $height) = @ARGV;
my ($tmp, @lines);
local (*INFILE);
$tmp = "/tmp/anytotxt.$$";
# Convert to 2 color XPM
system("anytopnm $input | pnmscale -xsize=$width -ysize=$height > $tmp.1");
system("pnmquant 2 $tmp.1 | ppmtoxpm > $tmp.2");
unlink "$tmp.1";
unless( -s "$tmp.2" )
{
unlink "$tmp.2";
die;
}
# Convert XPM to text
open INFILE, "< $tmp.2" or die;
while()
{
last if /pixels/;
}
while()
{
last if /;/;
s/^(")(.*)(".?)$/$2/;
s/[^\s]/X/g;
push @lines, $_;
}
close INFILE;
unlink "$tmp.2";
# Always print background pixel as space instead of X
if( $lines[0] =~ /^X/ )
{
foreach (@lines)
{
tr/X / X/;
print;
}
}
else
{
print foreach @lines;
}