newprogram
Programmer
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;
}
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;
}