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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkVTKImageIO.cxx,v $
Language: C++
Date: $Date: 2007-12-15 14:03:11 $
Version: $Revision: 1.44 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "itkVTKImageIO.h"
#include "itkByteSwapper.h"
#include <itksys/ios/sstream>
#include <stdio.h>
namespace itk
{
VTKImageIO::VTKImageIO()
{
this->SetNumberOfDimensions(2);
m_ByteOrder = LittleEndian;
m_FileType = Binary;
}
VTKImageIO::~VTKImageIO()
{
}
bool VTKImageIO::OpenVTKFileForReading(std::ifstream& os,
const char* filename)
{
// Make sure that we have a file to
if ( *filename == 0 )
{
itkExceptionMacro(<<"A FileName must be specified.");
return false;
}
// Close file from any previous image
if ( os.is_open() )
{
os.close();
}
// Open the new file for reading
itkDebugMacro(<< "Initialize: opening file " << filename);
// Actually open the file
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
const int openMode = std::ios::in|std::ios::binary;
#elif ( defined(__GNUC__) && __GNUC__ >= 3 ) || defined (__MWERKS__) || defined (__INTEL_COMPILER) || defined (__MINGW32__) || defined(__CYGWIN__)
const std::ios_base::openmode openMode = std::ios::in;
#else
const int openMode = std::ios::in;
#endif
os.open(filename, openMode);
if ( os.fail() )
{
itkExceptionMacro(<< "Could not open file for reading: " << filename);
return false;
}
return true;
}
bool VTKImageIO::OpenVTKFileForWriting(std::ofstream& os,
const char* filename)
{
// Make sure that we have a file to
if ( *filename == 0 )
{
itkExceptionMacro(<<"A FileName must be specified.");
return false;
}
// Create the file. This is required on some older sgi's
std::ofstream tFile(filename,std::ios::out);
tFile.close();
// Close file from any previous image
if ( os.is_open() )
{
os.close();
}
// Open the new file for writing
itkDebugMacro(<< "Initialize: opening file " << filename);
// Actually open the file
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
const int openMode = std::ios::out|std::ios::binary;
#elif (defined(__GNUC__) && __GNUC__ >= 3) || defined (__MWERKS__) || defined (__INTEL_COMPILER) || defined (__MINGW32__) || defined(__CYGWIN__)
const std::ios_base::openmode openMode =std::ios::out;
#else
const int openMode = std::ios::out;
#endif
os.open(filename, openMode);
if ( os.fail() )
{
itkExceptionMacro(<< "Could not open file for writing: " << filename);
return false;
}
return true;
}
bool VTKImageIO::CanReadFile(const char* filename)
{
std::ifstream file;
char buffer[256];
std::string fname(filename);
if ( fname.find(".vtk") >= fname.length() )
{
return false;
}
if ( ! this->OpenVTKFileForReading(file, filename) )
{
return false;
}
// Check to see if its a vtk structured points file
file.getline(buffer,255);
file.getline(buffer,255);
file.getline(buffer,255);
file.getline(buffer,255);
fname = buffer;
if ( fname.find("STRUCTURED_POINTS") < fname.length() ||
fname.find("structured_points") < fname.length() )
{
return true;
}
else
{
return false;
}
}
void VTKImageIO::InternalReadImageInformation(std::ifstream& file)
{
char line[255];
std::string text;
if ( ! this->OpenVTKFileForReading(file, m_FileName.c_str()) )
{
itkExceptionMacro(<< "Cannot read requested file");
}
file.getline(line,255);
file.getline(line,255);
file.getline(line,255);
text = line;
if ( text.find("ASCII") < text.length() ||
text.find("ascii") < text.length() )
{
this->SetFileTypeToASCII();
}
else if ( text.find("BINARY") < text.length() ||
text.find("binary") < text.length() )
{
this->SetFileTypeToBinary();
}
else
{
itkExceptionMacro(<< "Unrecognized type");
}
file.getline(line,255);
text = line;
if ( text.find("STRUCTURED_POINTS") >= text.length() &&
text.find("structured_points") >= text.length() )
{
itkExceptionMacro(<< "Not structured points, can't read");
}
//extract dimensions, spacing, origin
unsigned int dims[3];
double spacing[3];
double origin[3];
file.getline(line,255);
text = line;
// set values in case we don't find them
this->SetNumberOfDimensions(3);
m_Spacing[0] = 1.0;
m_Spacing[1] = 1.0;
m_Spacing[2] = 1.0;
m_Origin[0] = 0.0;
m_Origin[1] = 0.0;
m_Origin[2] = 0.0;
if ( text.find("DIMENSIONS") < text.length() ||
text.find("dimensions") < text.length() )
{
sscanf(line, "%*s %d %d %d", dims, dims+1, dims+2);
if ( dims[2] <= 1 )
{
this->SetNumberOfDimensions(2);
}
else
{
this->SetNumberOfDimensions(3);
}
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Dimensions[i] = dims[i];
}
}
else
{
itkExceptionMacro(<<"No dimensions defined");
}
for ( bool readScalars=false; !readScalars; )
{
file.getline(line,255);
text = line;
if ( text.find("SPACING") < text.length() ||
text.find("spacing") < text.length() )
{
sscanf(line, "%*s %lf %lf %lf", spacing, spacing+1, spacing+2);
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Spacing[i] = spacing[i];
}
}
else if ( text.find("ORIGIN") < text.length() ||
text.find("origin") < text.length() )
{
sscanf(line, "%*s %lf %lf %lf", origin, origin+1, origin+2);
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Origin[i] = origin[i];
}
}
else if ( text.find("COLOR_SCALARS") < text.length() ||
text.find("color_scalars") < text.length() )
{
readScalars = true;
int numComp = 1;
sscanf(line, "%*s %*s %d", &numComp);
if (numComp == 1)
{
SetPixelType(SCALAR);
}
else
{
SetPixelType(VECTOR);
}
if ( this->GetFileType() == ASCII )
{
SetNumberOfComponents(numComp);
SetComponentType(FLOAT);
}
else
{
SetNumberOfComponents(numComp);
SetComponentType(UCHAR);
}
}
else if ( text.find("SCALARS") < text.length() ||
text.find("scalars") < text.length() )
{
readScalars = true;
char pixelType[256];
int numComp = 1;
sscanf(line, "%*s %*s %s %d", pixelType, &numComp);
text = pixelType;
if (numComp == 1)
{
SetPixelType(SCALAR);
}
else
{
SetPixelType(VECTOR);
}
if ( text.find("float") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(FLOAT);
}
else if ( text.find("double") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(DOUBLE);
}
else if ( text.find("unsigned_char") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(UCHAR);
}
else if ( text.find("char") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(CHAR);
}
else if ( text.find("unsigned_short") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(USHORT);
}
else if ( text.find("short") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(SHORT);
}
else if ( text.find("unsigned_int") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(UINT);
}
else if ( text.find("int") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(INT);
}
else if ( text.find("unsigned_long") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(ULONG);
}
else if ( text.find("long") < text.length() )
{
SetNumberOfComponents(numComp);
SetComponentType(LONG);
}
else
{
itkExceptionMacro(<<"Unrecognized type");
}
file.getline(line,255);
text = line;
}//found scalars
}
}
void VTKImageIO::Read(void* buffer)
{
std::ifstream file;
this->InternalReadImageInformation(file);
//We are positioned at the data. The data is read depending on whether
//it is ASCII or binary.
if ( m_FileType == ASCII )
{
this->ReadBufferAsASCII(file, buffer, this->GetComponentType(),
this->GetImageSizeInComponents());
}
else
{
file.read(static_cast<char*>(buffer), this->GetImageSizeInBytes());
int size = this->GetComponentSize();
switch( size )
{
case 2:
ByteSwapper<short>::SwapRangeFromSystemToBigEndian((short *)buffer, this->GetImageSizeInComponents() );
break;
case 4:
ByteSwapper<float>::SwapRangeFromSystemToBigEndian((float *)buffer, this->GetImageSizeInComponents() );
break;
case 8:
ByteSwapper<double>::SwapRangeFromSystemToBigEndian((double *)buffer, this->GetImageSizeInComponents() );
break;
}
}
}
void VTKImageIO::ReadImageInformation()
{
std::ifstream file;
this->InternalReadImageInformation(file);
}
bool VTKImageIO::CanWriteFile( const char* name )
{
std::string filename = name;
if ( filename != "" &&
filename.find(".vtk") < filename.length() )
{
return true;
}
return false;
}
void VTKImageIO::Write(const void* buffer)
{
std::ofstream file;
if ( ! this->OpenVTKFileForWriting(file,m_FileName.c_str()) )
{
return;
}
// Check the image region for proper dimensions, etc.
unsigned int numDims = this->GetNumberOfDimensions();
if ( numDims < 2 || numDims > 3 )
{
itkExceptionMacro(<<"VTK Writer can only write 2 or 3-dimensional images");
return;
}
ImageIORegion ioRegion = this->GetIORegion();
// Write the VTK header information
file << "# vtk DataFile Version 3.0\n";
file << "VTK File Generated by Insight Segmentation and Registration Toolkit (ITK)\n";
if ( this->GetFileType() == ASCII )
{
file << "ASCII\n";
}
else
{
file << "BINARY\n";
}
// Save original formatting flags
const itksys_ios::ios::fmtflags originalFlags = file.flags();
#if (defined(__GNUC__) && (__GNUC__ <=2))
const itksys_ios::ios::streamsize originalPrecision = file.precision();
#else
const itksys_ios::streamsize originalPrecision = file.precision();
#endif
file.setf( itksys_ios::ios::scientific,
itksys_ios::ios::floatfield );
file.precision(16);
// Write characteristics of the data
file << "DATASET STRUCTURED_POINTS\n";
if ( numDims == 2 )
{
file << "DIMENSIONS " << this->GetDimensions(0) << " "
<< this->GetDimensions(1) << " 1\n";
file << "SPACING " << m_Spacing[0] << " " << m_Spacing[1] << " 1.0\n";
file << "ORIGIN " << m_Origin[0] << " " << m_Origin[1] << " 0.0\n";
}
else //numDims == 3
{
file << "DIMENSIONS " << this->GetDimensions(0) << " "
<< this->GetDimensions(1) << " " << this->GetDimensions(2) << "\n";
file << "SPACING " << m_Spacing[0] << " "
<< m_Spacing[1] << " " << m_Spacing[2] << "\n";
file << "ORIGIN " << m_Origin[0] << " "
<< m_Origin[1] << " " << m_Origin[2] << "\n";
}
// Restore the original formatting flags
file.flags( originalFlags );
file.precision( originalPrecision );
file << "POINT_DATA " << this->GetImageSizeInPixels() << "\n";
// Prefer the VECTORS representation when possible:
if( this->GetPixelType() == ImageIOBase::VECTOR && this->GetNumberOfComponents() == 3 )
{
file << "VECTORS vectors "
<< this->GetComponentTypeAsString(m_ComponentType) << "\n";
}
else
{
// According to VTK documentation number of components should in range (1,4):
file << "SCALARS scalars "
<< this->GetComponentTypeAsString(m_ComponentType) << " "
<< this->GetNumberOfComponents() << "\n"
<< "LOOKUP_TABLE default\n";
}
// Write the actual pixel data
if ( m_FileType == ASCII )
{
this->WriteBufferAsASCII(file, buffer, this->GetComponentType(),
this->GetImageSizeInComponents());
}
else //binary
{
int size = this->GetComponentSize();
const unsigned long int numbytes=this->GetImageSizeInBytes();
char * tempmemory=new char[numbytes];
memcpy(tempmemory,buffer,numbytes);
switch( size )
{
case 2:
{
ByteSwapper<short>::SwapRangeFromSystemToBigEndian(reinterpret_cast<short *>(tempmemory), this->GetImageSizeInComponents() );
}
break;
case 4:
{
ByteSwapper<float>::SwapRangeFromSystemToBigEndian(reinterpret_cast<float *>(tempmemory), this->GetImageSizeInComponents() );
}
break;
case 8:
{
ByteSwapper<double>::SwapRangeFromSystemToBigEndian(reinterpret_cast<double *>(tempmemory), this->GetImageSizeInComponents() );
}
break;
}
file.write(static_cast<const char*>(tempmemory), this->GetImageSizeInBytes());
delete [] tempmemory;
}
}
void VTKImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // end namespace itk
|