#!/usr/bin/perl $usage = <<'EOF'; Synposis: img2ascii.pl A simple raw Analyze IMG file to ascii converter. usage: img2ascii.pl : 8 or 16 (no other options are supported) , , : x, y, z size : Analyze IMG format raw file (header not needed) output: dumps to STDOUT Example: img2ascii.pl 8 256 256 256 MAP2003_256.dwz.img Author: Yoonsuck Choe Date: $Date: 2005/10/14 16:01:05 $ Revision: $Revision: 1.3 $ License: GNU public license (see http://www.gnu.org). EOF #-------------------- # process command-line arguments #-------------------- if ($#ARGV < 4) { print $usage; exit; } $bpp = $ARGV[0]; $xdim = $ARGV[1]; $ydim = $ARGV[2]; $zdim = $ARGV[3]; $file = $ARGV[4]; #-------------------- # open file #-------------------- open (FP,"< $file") or die "img2ascii.pl: cannot open file $file.\n"; #-------------------- # determine bit unpacking template based on bpp. #-------------------- if ($bpp==8) { # unsigned char (one byte) $template = "C*"; } elsif ($bpp == 16) { # unsigned short (two bytes) $template = "S*"; } else { die "img2ascii.pl: bpp $bpp not supported.\n"; } #-------------------- # main data read and dump loop #-------------------- while ($count = sysread(FP,$buf,$xdim*$bpp/8)) { @plain = unpack($template,$buf); foreach $p (@plain) { print $p." "; } print "\n"; } #-------------------- # close file #-------------------- close(FP);