1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
//
// Copyright 1997-2009 Torsten Rohlfing
//
// Copyright 2004-2012 SRI International
//
// This file is part of the Computational Morphometry Toolkit.
//
// http://www.nitrc.org/projects/cmtk/
//
// The Computational Morphometry Toolkit is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The Computational Morphometry Toolkit is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with the Computational Morphometry Toolkit. If not, see
// <http://www.gnu.org/licenses/>.
//
// $Revision: 5436 $
//
// $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
// $LastChangedBy: torstenrohlfing $
//
*/
#include "cmtkVolumeFromFile.h"
#include <System/cmtkConsole.h>
#include <System/cmtkCompressedStream.h>
#include <Base/cmtkTypes.h>
#include <stdio.h>
namespace
cmtk
{
/** \addtogroup IO */
//@{
/// BioRad microscopy image file header
typedef struct
{
unsigned short nx, ny; // 0 2*2 image width and height in pixels
short npic; // 4 2 number of images in file
short ramp1_min; // 6 2*2 LUT1 ramp min. and max.
short ramp1_max;
int notes; // 10 4 no notes=0; has notes=non zero
short byte_format; // 14 2 bytes=TRUE(1); words=FALSE(0)
unsigned short n; // 16 2 image number within file
char name[32]; // 18 32 file name
short merged; // 50 2 merged format
unsigned short color1; // 52 2 LUT1 color status
unsigned short file_id; // 54 2 valid .PIC file=12345
short ramp2_min; // 56 2*2 LUT2 ramp min. and max.
short ramp2_max;
unsigned short color2; // 60 2 LUT2 color status
short edited; // 62 2 image has been edited=TRUE(1)
short lens; // 64 2 Integer part of lens magnification
float mag_factor; // 66 4 4 byte real mag. factor (old ver.)
unsigned short dummy[3]; // 70 6 NOT USED (old ver.=real lens mag.)
} FileHeaderBioRad;
const UniformVolume::SmartPtr
VolumeFromFile::ReadBioRad( const std::string& path )
{
CompressedStream stream( path );
// Biorad header is 76 bytes
char buffer[76];
if ( 1 != stream.Read( &buffer, sizeof(buffer), 1 ) )
{
StdErr << "ERROR: cannot read header from BioRad file " << path << ". Bailing out.\n";
return UniformVolume::SmartPtr( NULL );
}
FileHeaderBioRad header;
memcpy( &header.nx, buffer+0, sizeof( header.nx ) );
memcpy( &header.ny, buffer+2, sizeof( header.ny ) );
memcpy( &header.npic, buffer+4, sizeof( header.npic ) );
memcpy( &header.notes, buffer+10, sizeof( header.notes ) );
memcpy( &header.byte_format, buffer+14, sizeof( header.byte_format ) );
memcpy( &header.file_id, buffer+54, sizeof( header.file_id ) );
// check MagicNumber
#ifdef WORDS_BIGENDIAN
if ( Memory::ByteSwap( header.file_id ) != 12345 )
{
StdErr << "ERROR: BioRad file " << path << " has invalid magic number. Bailing out.\n";
return UniformVolume::SmartPtr( NULL );
}
#else
if ( header.file_id != 12345 )
{
StdErr << "ERROR: BioRad file " << path << " has invalid magic number. Bailing out.\n";
return UniformVolume::SmartPtr( NULL );
}
#endif
#ifdef WORDS_BIGENDIAN
int dims[3] = { Memory::ByteSwap( header.nx ), Memory::ByteSwap( header.ny ), Memory::ByteSwap( header.npic ) };
#else
int dims[3] = { header.nx, header.ny, header.npic };
#endif
int numPixels = dims[0] * dims[1] * dims[2];
TypedArray::SmartPtr dataArray;
if ( header.byte_format )
{
dataArray = TypedArray::SmartPtr( TypedArray::Create( TYPE_BYTE, numPixels ) );
}
else
{
dataArray = TypedArray::SmartPtr( TypedArray::Create( TYPE_USHORT, numPixels ) );
}
stream.Read( dataArray->GetDataPtr(), dataArray->GetItemSize(), dataArray->GetDataSize() );
double pixelsizeX = 1, pixelsizeY = 1, pixelsizeZ = 1;
bool flipX = false, flipY = false, flipZ = false;;
while ( ! stream.Feof() )
{
char lineheader[16], line[80];
stream.Read( lineheader, sizeof( lineheader ), 1 );
stream.Read( line, sizeof( line ), 1 );
// StdErr.printf( "%s\n->%s\n", lineheader, line );
double d1, d2, d3;
if ( 3 == sscanf( line, "AXIS_2 %20lf %20lf %20lf", &d1, &d2, &d3 ) )
{
pixelsizeX = fabs( d3 );
flipX = (d3 < 0 );
}
if ( 3 == sscanf( line, "AXIS_3 %20lf %20lf %20lf", &d1, &d2, &d3 ) )
{
pixelsizeY = fabs( d3 );
flipY = (d3 < 0 );
}
if ( 3 == sscanf( line, "AXIS_4 %20lf %20lf %20lf", &d1, &d2, &d3 ) )
{
pixelsizeZ = fabs( d3 );
flipZ = (d3 < 0 );
}
}
#ifdef WORDS_BIGENDIAN
Types::Coordinate lensScale = 1; //Memory::ByteSwap( header.lens );
#else
Types::Coordinate lensScale = 1; //header.lens;
#endif
// GJ I think this was backwards - want to swap if we ARE on big endian
// since Biorad is always little endian
//#ifndef WORDS_BIGENDIAN
#ifdef WORDS_BIGENDIAN
// change endianness from Sun to whatever we're currently on.
dataArray->ChangeEndianness();
#endif
UniformVolume::SmartPtr volume( new UniformVolume( DataGrid::IndexType::FromPointer( dims ), lensScale * pixelsizeX, lensScale * pixelsizeY, lensScale * pixelsizeZ, dataArray ) );
if ( flipX )
{
StdErr << "WARNING: x pixel spacing is negative. Resulting volume will be mirrored accordingly.\n";
volume->ApplyMirrorPlane( AXIS_X );
}
if ( flipY )
{
StdErr << "WARNING: y pixel spacing is negative. Resulting volume will be mirrored accordingly.\n";
volume->ApplyMirrorPlane( AXIS_Y );
}
if ( flipZ )
{
StdErr << "WARNING: z pixel spacing is negative. Resulting volume will be mirrored accordingly.\n";
volume->ApplyMirrorPlane( AXIS_Z );
}
return volume;
}
} // namespace cmtk
|