#!/usr/bin/perl # anytotxt.pl - Don Yang (uguu.org) # # 12/03/05 use strict; use Fcntl; die "$0 \n" unless $#ARGV == 2; my ($input, $width, $height) = @ARGV; local (*INFILE); my $tmp = "anytotxt.$$"; # Convert to 2 color PGM system("anytopnm $input | pamdepth 255 | " . "pnmscale -xsize=$width -ysize=$height > $tmp.1"); system("pnmquant 2 $tmp.1 | ppmtopgm > $tmp.2"); unlink "$tmp.1"; unless( -s "$tmp.2" ) { unlink "$tmp.2"; die; } # Convert PPM to text, assuming first pixel is black and everything # else is white. my ($header, $header0, $blackpixel); $header0 = "P5\012$width $height\012255\012"; $blackpixel = undef; sysopen(INFILE, "$tmp.2", O_RDONLY|O_BINARY) or die $!; sysread(INFILE, $header, length($header0)) or die $!; die "Bad header" if $header ne $header0; for(my $y = 0; $y < $height; $y++) { my $scanline; sysread(INFILE, $scanline, $width) or die $!; defined($blackpixel) or $blackpixel = ord($scanline); print join '', map {($_ == $blackpixel) ? ' ' : 'X'} unpack 'C*', $scanline; print "\n"; } close INFILE; unlink "$tmp.2"; exit 0;