Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

debug codes

Status
Not open for further replies.

newprogram

Programmer
Jul 27, 2006
8
0
0
US
Hi, all,

Please help me to check the codes. I am trying to convert raw images to big-endian data and then inverted these images. But I didn't get inverted images.

Thanks in advances,

*******************************
The following are my codes:


#!/local/os/bin/perl

my $headerFile;
my $inputBinaryFile;
my $invertedDir;
my $SLICE_SIZE;

# $#ARGV is the largest index of ARGV array = length of array -1
if ($#ARGV < 1)
{
print "$0 header-file inverted-directory. $#ARGV\n";
exit;
}

$headerFile = $ARGV[0];
$inputBinaryFile = $ARGV[1];
$invertedDir = $ARGV[1];
unless (-e $ARGV[0]) {
print "$ARGV[0] file does not exist\n";
exit;
}

my $MET_DATATYPE;
my ($sizeX, $sizeY, $sizeZ);
my $tmp;
my @words;
open (IN_HEADER, "$headerFile") or die "can not open $headerFile\n";
while (<IN_HEADER>) {
chomp;
next if (/^\s*$/);
if (/^DimSize/)
{
@words = split /=/;
$tmp = $words[1] ;
$tmp =~ s/^\s//; # trim the leading whitespace
print "Dim:$tmp\n";
($sizeX, $sizeY, $sizeZ) = split / /, $tmp, 3;
$sizeX =~ s/\s//g;
$sizeY =~ s/\s//g;
$sizeZ =~ s/\s//g;

}
elsif (/^ElementType/)
{
@words = split /=/;
$MET_DATATYPE = $words[1];
$MET_DATATYPE =~ s/\s//g;
}
elsif (/^ElementDataFile/) {
@words = split /=/;
$inputBinaryFile = $words[1];
$inputBinaryFile =~ s/\s//g;
}
}
close IN_HEADER;

unless (-e $inputBinaryFile) {
print "$inputBinaryFile raw data file does not exist\n";
exit;
}



if ($MET_DATATYPE eq "MET_UCHAR") {
$SLICE_SIZE = $sizeX * $sizeY * 8;
}
elsif ($MET_DATATYPE eq "MET_USHORT") {
$SLICE_SIZE = $sizeX * $sizeY * 2;
print "X: $sizeX; y: $sizeY; SLICE SIZE: $SLICE_SIZE\n";
}


open (RawFH, "$inputBinaryFile") or die "can not open file $inputBinaryFile $!\n";

my $byteCounter = 0;
my $fileCounter = 0;
my $buf;
my $bigEndianBuf;
while (read RawFH, $buf, $SLICE_SIZE) {
$bigEndianBuf = pack ('v*', unpack ('n*', $buf));
$fileCounter ++;
if ($fileCounter < 10) {
$outBinaryFile = "I.00" . $fileCounter;
}
else {
$outBinaryFile = "I.0" . $fileCounter;
}
open (OUTFH, ">$outBinaryFile") or die "can not create file $outBinaryFile, $!\n";
print OUTFH $bigEndianBuf ;
close OUTFH;
}

# Invert the files
InvertFiles( $fileCounter);

print "Finish converting all the slice files";
exit;


sub InvertFiles( )
{
my $fileCounter = shift;
my ($inputBinaryFile, $outBinaryFile);

unless (-d $invertedDir )
{
mkdir $invertedDir, 0755;
}

# add folder separator
if ($invertedDir !~ /\/$/)
{
$invertedDir .= "/";
}
my $i;

for ($i = 1; $i <= $fileCounter; $i++)
{
if ($i < 10)
{
$inputBinaryFile = "I.00$i";
$outBinaryFile =$invertedDir . "I.00" . $i;
}
else
{
$inputBinaryFile = "I.0" . $i;
$outBinaryFile = $invertedDir . "I.0" . $i;
}
InvertFile ($inputBinaryFile, $outBinaryFile);
}

}

sub InvertFile() {
my ($inputBinaryFile, $outBinaryFile, $remainer) = @_;
unless (-e $inputBinaryFile) {
print "no input slice file $inputBinaryFile, $!\n";
return;
}

open (RawFH, "$inputBinaryFile") or die "can not open file $inputBinaryFile $!\n";
open (OUTFH, ">$outBinaryFile") or die "can not create file $outBinaryFile, $!\n";
binmode RawFH;
my $byte;
# PROBLEM in the following: either read or negate has issue
while (read RawFH, $byte, 1)
{
print "$byte";
$inverted = not $byte; # negate each bit to invert it
print OUTFH $inverted ;
}
close OUTFH;
close RawFH;
}



 
You'll need to be more specific about your issues ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi, PaulTEG,

I have 2 files, one is a raw images(.raw file) another one is a text file with header information for these images. I already converted to big-endian(background is black), but I need to invert color for these grayscle images with black background.

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top