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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkStimulateImageIO.cxx
Language: C++
Date: $Date$
Version: $Revision$
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 "itkStimulateImageIO.h"
#include <stdio.h>
#include <string.h>
#include <itksys/SystemTools.hxx>
#include <itksys/RegularExpression.hxx>
#include "itkByteSwapper.h"
namespace itk
{
StimulateImageIO::StimulateImageIO()
{
this->SetNumberOfDimensions(4);
m_ByteOrder = BigEndian;
m_FileType = Binary;
m_DisplayRange[0] = 0;
m_DisplayRange[1] = 0;
}
StimulateImageIO::~StimulateImageIO()
{
}
bool StimulateImageIO::OpenStimulateFileForReading(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
os.open( filename, std::ios::in | std::ios::binary );
if ( os.fail() )
{
return false;
}
return true;
}
bool StimulateImageIO::OpenStimulateFileForWriting(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;
}
// Close file from any previous image
if ( os.is_open() )
{
os.close();
}
// Open the new file for writing
itkDebugMacro(<< "Initialize: opening file " << filename);
#ifdef __sgi
// Create the file. This is required on some older sgi's
std::ofstream tFile(filename,std::ios::out);
tFile.close();
#endif
// Actually open the file
os.open( filename, std::ios::out | std::ios::binary );
if( os.fail() )
{
itkExceptionMacro(<< "Could not open file for writing: " << filename);
return false;
}
return true;
}
// This method will only test if the header looks like a
//Stimulate image file.
bool StimulateImageIO::CanReadFile(const char* filename)
{
std::ifstream file;
char buffer[256];
std::string fname(filename);
if( fname == "" )
{
itkDebugMacro(<<"No filename specified.");
return false;
}
bool extensionFound = false;
std::string::size_type sprPos = fname.rfind(".spr");
if ((sprPos != std::string::npos)
&& (sprPos == fname.length() - 4))
{
extensionFound = true;
}
if( !extensionFound )
{
itkDebugMacro(<<"The filename extension is not recognized");
return false;
}
if ( ! this->OpenStimulateFileForReading(file, filename))
{
return false;
}
// Check to see if its a spr file
file.getline(buffer,255);
fname = buffer;
if ( fname.find("numDim:") < fname.length() ||
fname.find("dim:") < fname.length() ||
fname.find("dataType:") < fname.length() )
{
return true;
}
else
{
return false;
}
}
void StimulateImageIO::Read(void* buffer)
{
std::ifstream file;
//read header information file:
this->InternalReadImageInformation(file);
//read data file
std::ifstream file_data;
if( m_DataFileName == "" )
{
//if no data filename was specified try to guess one:
//based on filename.spr , options are:
//1: filename.spr.sdt
//2: filename.sdt
//option 1:
m_DataFileName = m_FileName;
m_DataFileName.replace(m_DataFileName.length(), 4, ".sdt" );
// Actually open the file
file_data.open( m_DataFileName.c_str(), std::ios::in | std::ios::binary );
if ( file_data.fail() )
{
//option 2:
m_DataFileName = m_FileName;
m_DataFileName.replace(m_DataFileName.length() - 3, 3, "sdt" );
file_data.open( m_DataFileName.c_str(), std::ios::in | std::ios::binary );
if ( file_data.fail() )
{
itkExceptionMacro(<<"No Data file was specified in header (spr) file and guessing file data name failed." );
return;
}
}
}//a filename was found for data file
if( !OpenStimulateFileForReading( file_data, m_DataFileName.c_str()) )
{
itkExceptionMacro("StimulateImageIO could not open file: "
<< m_DataFileName << " for reading."
<< std::endl
<< "Reason: "
<< itksys::SystemTools::GetLastSystemError());
return;
}
if( !this->ReadBufferAsBinary( file_data, buffer, this->GetImageSizeInBytes()) )
{
itkExceptionMacro(<<"Read failed: Wanted "
<< this->GetImageSizeInBytes()
<< " bytes, but read "
<< file_data.gcount() << " bytes."
<< " from file " << m_DataFileName );
}
//byte swapping depending on pixel type:
switch (this->GetComponentType())
{
case CHAR:
ByteSwapper<char>::SwapRangeFromSystemToBigEndian((char *)buffer, static_cast<unsigned long>(this->GetImageSizeInComponents()) );
break;
case SHORT:
ByteSwapper<short>::SwapRangeFromSystemToBigEndian((short *)buffer, static_cast<unsigned long>(this->GetImageSizeInComponents()) );
break;
case INT:
ByteSwapper<int>::SwapRangeFromSystemToBigEndian((int *)buffer, static_cast<unsigned long>(this->GetImageSizeInComponents()) );
break;
case FLOAT:
ByteSwapper<float>::SwapRangeFromSystemToBigEndian((float *)buffer, static_cast<unsigned long>(this->GetImageSizeInComponents()) );
break;
case DOUBLE:
ByteSwapper<double>::SwapRangeFromSystemToBigEndian((double *)buffer, static_cast<unsigned long>(this->GetImageSizeInComponents()) );
break;
default:
break;
}
//closing files:
file.close();
file_data.close();
}
void StimulateImageIO::InternalReadImageInformation(std::ifstream& file)
{
char line[255];
std::string text;
//read .sdt file (header)
if ( ! this->OpenStimulateFileForReading(file, m_FileName.c_str()) )
{
itkExceptionMacro("StimulateImageIO could not open sdt file: "
<< m_FileName << " for reading."
<< std::endl
<< "Reason: "
<< itksys::SystemTools::GetLastSystemError());
}
// re open in ascii mode
file.close();
file.open( m_FileName.c_str(), std::ios::in );
//extract dimensions, spacing, origin
unsigned int dim;
unsigned int dims[4];
float spacing[4];
float origin[4];
float fov[4];
// set values in case we don't find them
this->SetNumberOfDimensions(4);
m_Spacing[0] = 1.0;
m_Spacing[1] = 1.0;
m_Spacing[2] = 1.0;
m_Spacing[3] = 1.0;
m_Origin[0] = 0.0;
m_Origin[1] = 0.0;
m_Origin[2] = 0.0;
m_Origin[3] = 0.0;
char pixelType[256];
float range[2];
//char fidName[256] = "";
//char orient[256] = "";
bool fov_specified = false;
bool origin_specified = false;
bool spacing_specified = false;
while((file.getline(line, 255), file.gcount() > 0))
{
text = line;
#ifdef __CYGWIN__
// If terminated with \r\n, getline only removes \n. Explictly
// strip \r.
if (text.size() > 0
&& (text[text.size() - 1] == '\r'))
{
text.resize(text.size() - 1);
}
#endif
if ( text.find("numDim") < text.length())
{
sscanf(line, "%*s %d", &dim);
this->SetNumberOfDimensions(dim);
}
else if ( text.find("dim") < text.length())
{
sscanf(line, "%*s %d %d %d %d", dims, dims+1, dims+2 , dims+3);
if ( m_NumberOfDimensions > 3 && dims[3] <= 1 )
{
this->SetNumberOfDimensions(3);
}
if ( m_NumberOfDimensions > 2 && dims[2] <= 1 )
{
this->SetNumberOfDimensions(2);
}
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Dimensions[i] = dims[i];
}
}
else if ( text.find("origin") < text.length())
{
//origin
//Position of the center of the first voxel. One value for each dimension. If
//the origin is not specified, but the fov is, then the image is assumed
//to be centered:
sscanf(line, "%*s %f %f %f %f", origin, origin+1, origin+2 , origin+3);
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Origin[i] = origin[i];
}
origin_specified = true;
}
else if ( text.find("extent") < text.length())
{
//not documented
itkDebugMacro(<<"Extent was specified");
}
else if ( text.find("fov") < text.length())
{
//fov
//Field of view: The distance between the outside edges of the first and last
//voxel along each dimension; one value for each dimension. If the fov is not
//specified it is calculated according to: fov = interval * dim
sscanf(line, "%*s %f %f %f %f", fov, fov+1, fov+2 , fov+3);
fov_specified = true;
}
else if ( text.find("interval") < text.length())
{
//interval
//The center to center distance between adjacent voxels along each dimension;
//one value for each dimension. If the interval is not specified it is
//calculated according to: interval = fov / dim
sscanf(line, "%*s %f %f %f %f", spacing, spacing+1, spacing+2 , spacing+3);
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Spacing[i] = spacing[i];
}
spacing_specified = true;
}
else if ( text.find("dataType") < text.length())
{
sscanf(line, "%*s %s", pixelType);
text = pixelType;
SetPixelType(SCALAR);
if ( text.find("BYTE") < text.length() )
{
SetComponentType(CHAR);
}
else if ( text.find("WORD") < text.length() )
{
SetComponentType(SHORT);
}
else if ( text.find("LWORD") < text.length() )
{
SetComponentType(INT);
}
else if ( text.find("REAL") < text.length() )
{
SetComponentType(FLOAT);
}
else if ( text.find("COMPLEX") < text.length() )
{
SetPixelType(VECTOR);
SetComponentType(DOUBLE);
}
else
{
itkExceptionMacro(<<"Unrecognized type");
}
}//found scalars
else if ( text.find("displayRange") < text.length())
{
//displayRange:
//Two values giving the low_value and high_value. Voxel values below the
//low_value will be displayed as black and voxels with values above the
//high_value will be displayed as white. Voxels with values within the display
//range are displayed with a grey value that is scaled linearly between the
//low_value and high_value.
sscanf(line, "%*s %f %f", range, range+1);
m_DisplayRange[0] = range[0];
m_DisplayRange[1] = range[1];
}
else if ( text.find("fidName") < text.length())
{
//Not well documented
//This is a bit tricky to get the value as there is sometime no white space
//only a ':' separate field from value, we assume there is no other ':'
char *pch;
pch = strchr(line,':');
sscanf(++pch, "%s", m_FidName); //delete any white space left
itkDebugMacro(<<"fidName was specified");
}
else if ( text.find("sdtOrient") < text.length())
{
//Not used now, but later when ITK Dictionary will be ready
//don't know for now the format in which to save this.
//This is a bit tricky to get the value as there is sometime no white space
//only a ':' separate field from value, we assume there is no other ':'
char *pch;
pch = strchr(line,':');
sscanf(++pch, "%s", m_SdtOrient); //delete any white space left
itkDebugMacro(<<"Orientation was specified");
}
else if ( text.find("dsplyThres") < text.length())
{
//not documented
itkDebugMacro(<<"Display threshold was specified");
}
else if ( text.find("endian") < text.length())
{
//BigEndian ieee-be / LittleEndian: ieee-le
if ( text.find("ieee-le") < text.length())
{
itkExceptionMacro(<<"Little Endian Stimulate files are not handled.");
}
}
else if ( text.find("mapParmFileName") < text.length())
{
//not documented
itkDebugMacro(<<"mapParmFileName was specified");
}
else if ( text.find("mapTypeName") < text.length())
{
//not documented
itkDebugMacro(<<"mapTypeName was specified");
}
else if ( text.find("stimFileName:") < text.length())
{
//file data name is explicitely specified
std::string datafilename;
// Remove leading and trailing blanks
itksys::RegularExpression regexp("stimFileName:[ ]*(.*)[ ]*$");
regexp.find(text);
datafilename = regexp.match(1);
//if the data filename has a directory specified, use it as is,
//otherwise prepend the path of the .spr file.
std::string datafilenamePath =
::itksys::SystemTools::GetFilenamePath (datafilename);
if (datafilenamePath == "")
{
std::string fileNamePath = ::itksys::SystemTools::GetFilenamePath (m_FileName.c_str());
m_DataFileName = fileNamePath + "/" + datafilename;
}
else
{
m_DataFileName = datafilename;
}
}
else if ( text.find("mapConf") < text.length())
{
//not documented
itkDebugMacro(<<"mapConf was specified");
}
else if ( text.find("periodStr") < text.length())
{
//not documented
itkDebugMacro(<<"periodStr was specified");
}
}
//compute any missing informations:
if( !spacing_specified && fov_specified)
{
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Spacing[i] = fov[i]/dims[i];
}
}
if( !origin_specified && fov_specified)
{
for ( unsigned int i=0; i < m_NumberOfDimensions; i++ )
{
m_Origin[i] = (m_Spacing[i] - fov[i])/2.;
}
}//otherwise default spacing & origin are used.
}
void StimulateImageIO::ReadImageInformation()
{
std::ifstream file;
this->InternalReadImageInformation(file);
}
bool StimulateImageIO::CanWriteFile( const char* name )
{
std::string filename = name;
if( filename == "" )
{
itkDebugMacro(<<"No filename specified.");
return false;
}
bool extensionFound = false;
std::string::size_type sprPos = filename.rfind(".spr");
if ((sprPos != std::string::npos)
&& (sprPos == filename.length() - 4))
{
extensionFound = true;
}
if( !extensionFound )
{
itkDebugMacro(<<"The filename extension is not recognized");
return false;
}
return true;
}
void StimulateImageIO::Write(const void* buffer)
{
unsigned int i;
std::ofstream file;
if ( ! this->OpenStimulateFileForWriting(file, m_FileName.c_str()) )
{
return;
}
// Check the image region for proper dimensions, etc.
unsigned int numDims = this->GetNumberOfDimensions();
if ( numDims < 2 || numDims > 4 )
{
itkExceptionMacro(<<"Stimulate Writer can only write 2,3 or 4-dimensional images");
return;
}
// Write the Stimulate header information
file << "numDim: " << this->GetNumberOfDimensions();
// Write characteristics of the data
file << "\ndim:";
for(i=0; i < m_NumberOfDimensions; i++)
{
file << " " << m_Dimensions[i];
}
file << "\norigin:";
for(i=0; i < m_NumberOfDimensions; i++)
{
file << " " << m_Origin[i];
}
file << "\nfov:";
for(i=0; i < m_NumberOfDimensions; i++)
{
file << " " << m_Spacing[i]*m_Dimensions[i]; //fov = interval * dim
}
file << "\ninterval:";
for(i=0; i < m_NumberOfDimensions; i++)
{
file << " " << m_Spacing[i];
}
//preparation for writing buffer:
const unsigned long numberOfBytes = static_cast<unsigned long>(this->GetImageSizeInBytes());
const unsigned long numberOfComponents = static_cast<unsigned long>(this->GetImageSizeInComponents());
file << "\ndataType: ";
{
char * tempmemory=new char[numberOfBytes];
memcpy(tempmemory,buffer,numberOfBytes);
switch(this->GetComponentType())
{
case CHAR:
file << "BYTE";
ByteSwapper<char>::SwapRangeFromSystemToBigEndian(reinterpret_cast<char *>(tempmemory), numberOfComponents );
break;
case SHORT:
file << "WORD";
ByteSwapper<short int>::SwapRangeFromSystemToBigEndian(reinterpret_cast<short int *>(tempmemory), numberOfComponents );
break;
case INT:
file << "LWORD";
ByteSwapper<int>::SwapRangeFromSystemToBigEndian(reinterpret_cast<int *>(tempmemory), numberOfComponents );
break;
case FLOAT:
file << "REAL";
ByteSwapper<float>::SwapRangeFromSystemToBigEndian(reinterpret_cast<float *>(tempmemory), numberOfComponents );
break;
case DOUBLE:
file << "COMPLEX";
ByteSwapper<double>::SwapRangeFromSystemToBigEndian(reinterpret_cast<double *>(tempmemory), numberOfComponents );
break;
default:
break;
}
//add the data filename to the header
//determine datafile given the spr filename
m_DataFileName = m_FileName;
m_DataFileName.replace(m_DataFileName.length() - 3, 3, "sdt" );
file << "\nstimFileName: " << m_DataFileName.c_str();
//Last carrier return:
file << "\n";
//actually read data file
std::ofstream file_data;
if ( ! this->OpenStimulateFileForWriting(file_data, m_DataFileName.c_str()) )
{
return;
}
// Write the actual pixel data
file_data.write( static_cast<const char *>(tempmemory) , numberOfBytes );
delete [] tempmemory;
file_data.close();
}
file.close();
}
void StimulateImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "\nDisplayRange: " << m_DisplayRange[0] << " " << m_DisplayRange[1];
}
} // end namespace itk
|