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 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkImageIOBase.cxx,v $
Language: C++
Date: $Date: 2008-02-06 21:23:04 $
Version: $Revision: 1.75 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkImageIOBase.h"
#include "itkRGBPixel.h"
#include "itkRGBAPixel.h"
#include "itkOffset.h"
#include "itkVector.h"
#include "itkPoint.h"
#include "itkCovariantVector.h"
#include "itkSymmetricSecondRankTensor.h"
#include "itkDiffusionTensor3D.h"
#include "itkFixedArray.h"
namespace itk
{
ImageIOBase::ImageIOBase() :
m_PixelType(SCALAR),
m_ComponentType(UNKNOWNCOMPONENTTYPE),
m_ByteOrder(OrderNotApplicable),
m_FileType(TypeNotApplicable),
m_NumberOfDimensions(0)
{
Reset(false);
}
void ImageIOBase::Reset(const bool)
{
m_Initialized = false;
m_FileName = "";
m_NumberOfComponents = 1;
for (unsigned int i=0; i < m_NumberOfDimensions; i++)
{
m_Dimensions[i] = 0;
m_Strides[i] = 0;
}
m_NumberOfDimensions = 0;
m_UseCompression = false;
m_UseStreamedReading = false;
m_UseStreamedWriting = false;
}
ImageIOBase::~ImageIOBase()
{
}
void ImageIOBase::Resize(const unsigned int numDimensions,
const unsigned int* dimensions)
{
m_NumberOfDimensions = numDimensions;
if (dimensions != NULL)
{
for (unsigned int i=0; i < m_NumberOfDimensions; i++)
{
m_Dimensions[i] = dimensions[i];
}
ComputeStrides();
}
}
void ImageIOBase::SetDimensions(unsigned int i, unsigned int dim)
{
if ( i >= m_Dimensions.size() ) {return;}
this->Modified();
m_Dimensions[i] = dim;
}
void ImageIOBase::SetOrigin(unsigned int i, double origin)
{
if ( i >= m_Origin.size() ) {return;}
this->Modified();
m_Origin[i] = origin;
}
void ImageIOBase::SetSpacing(unsigned int i, double spacing)
{
if (i >= m_Spacing.size() ) {return;}
this->Modified();
m_Spacing[i] = spacing;
}
void ImageIOBase::SetDirection(unsigned int i, std::vector<double> &direction)
{
if (i >= m_Direction.size() ) {return;}
this->Modified();
m_Direction[i] = direction;
}
void ImageIOBase::SetDirection(unsigned int i, vnl_vector<double> &direction)
{
if (i >= m_Direction.size() ) {return;}
this->Modified();
std::vector<double> v;
v.resize(m_Direction.size());
for (unsigned int j=0; j < v.size(); j++)
{
v[j] = direction[j];
}
m_Direction[i] = v;
}
const std::type_info& ImageIOBase::GetComponentTypeInfo() const
{
switch(m_ComponentType)
{
case UCHAR:
return typeid(unsigned char);
case CHAR:
return typeid(char);
case USHORT:
return typeid(unsigned short);
case SHORT:
return typeid(short);
case UINT:
return typeid(unsigned int);
case INT:
return typeid(int);
case ULONG:
return typeid(unsigned long);
case LONG:
return typeid(long);
case FLOAT:
return typeid(float);
case DOUBLE:
return typeid(double);
case UNKNOWNCOMPONENTTYPE:
default:
itkExceptionMacro ("Unknown component type: " << m_ComponentType);
}
return typeid(ImageIOBase::UnknownType);
}
//
// This macro enforces pixel type information to be available for all different
// pixel types.
//
template <typename T>
bool
itkSetPixelType(ImageIOBase *This,
const std::type_info &ptype,
ImageIOBase::IOComponentType ntype,
T itkNotUsed( dummy ) )
{
if( ptype == typeid(T) )
{
This->SetNumberOfComponents(1);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::SCALAR);
return true;
}
else if ( ptype == typeid(RGBPixel<T>) )
{
This->SetNumberOfComponents(3);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::RGB);
return true;
}
else if ( ptype == typeid(RGBAPixel<T>) )
{
This->SetNumberOfComponents(4);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::RGBA);
return true;
}
else if ( ptype == typeid(Vector<T,2>) )
{
This->SetNumberOfComponents(2);
This->SetPixelType(ImageIOBase::VECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(Vector<T,3>) )
{
This->SetNumberOfComponents(3);
This->SetPixelType(ImageIOBase::VECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(Vector<T,4>) )
{
This->SetNumberOfComponents(4);
This->SetPixelType(ImageIOBase::VECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(Vector<T,5>) )
{
This->SetNumberOfComponents(5);
This->SetPixelType(ImageIOBase::VECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(Vector<T,6>) )
{
This->SetNumberOfComponents(6);
This->SetPixelType(ImageIOBase::VECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(Vector<T,7>) )
{
This->SetNumberOfComponents(7);
This->SetPixelType(ImageIOBase::VECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(CovariantVector<T,2>) )
{
This->SetNumberOfComponents(2);
This->SetPixelType(ImageIOBase::COVARIANTVECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(CovariantVector<T,3>) )
{
This->SetNumberOfComponents(3);
This->SetPixelType(ImageIOBase::COVARIANTVECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(CovariantVector<T,4>) )
{
This->SetNumberOfComponents(4);
This->SetPixelType(ImageIOBase::COVARIANTVECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(CovariantVector<T,5>) )
{
This->SetNumberOfComponents(5);
This->SetPixelType(ImageIOBase::COVARIANTVECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(CovariantVector<T,6>) )
{
This->SetNumberOfComponents(6);
This->SetPixelType(ImageIOBase::COVARIANTVECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(CovariantVector<T,7>) )
{
This->SetNumberOfComponents(7);
This->SetPixelType(ImageIOBase::COVARIANTVECTOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(FixedArray<T,2>) )
{
This->SetNumberOfComponents(2);
This->SetPixelType(ImageIOBase::FIXEDARRAY);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(FixedArray<T,3>) )
{
This->SetNumberOfComponents(3);
This->SetPixelType(ImageIOBase::FIXEDARRAY);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(FixedArray<T,4>) )
{
This->SetNumberOfComponents(4);
This->SetPixelType(ImageIOBase::FIXEDARRAY);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(SymmetricSecondRankTensor<T,3>) )
{
This->SetNumberOfComponents(6);
This->SetPixelType(ImageIOBase::SYMMETRICSECONDRANKTENSOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(SymmetricSecondRankTensor<T,4>) )
{
This->SetNumberOfComponents(10);
This->SetPixelType(ImageIOBase::SYMMETRICSECONDRANKTENSOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(SymmetricSecondRankTensor<T,5>) )
{
This->SetNumberOfComponents(15);
This->SetPixelType(ImageIOBase::SYMMETRICSECONDRANKTENSOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(SymmetricSecondRankTensor<T,6>) )
{
This->SetNumberOfComponents(21);
This->SetPixelType(ImageIOBase::SYMMETRICSECONDRANKTENSOR);
This->SetComponentType(ntype);
return true;
}
else if ( ptype == typeid(DiffusionTensor3D<T>) )
{
This->SetNumberOfComponents(6);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::DIFFUSIONTENSOR3D);
return true;
}
else if ( ptype == typeid(Matrix<T,2,2>) )
{
This->SetNumberOfComponents(4);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::MATRIX);
return true;
}
else if ( ptype == typeid(Matrix<T,3,3>) )
{
This->SetNumberOfComponents(9);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::MATRIX);
return true;
}
else if ( ptype == typeid(Matrix<T,4,4>) )
{
This->SetNumberOfComponents(16);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::MATRIX);
return true;
}
else if ( ptype == typeid(std::complex<T>) )
{
This->SetNumberOfComponents(2);
This->SetComponentType(ntype);
This->SetPixelType(ImageIOBase::COMPLEX);
return true;
}
return false;
}
bool ImageIOBase::SetPixelTypeInfo(const std::type_info& ptype)
{
this->SetNumberOfComponents(1);
this->SetPixelType(ImageIOBase::UNKNOWNPIXELTYPE);
this->SetComponentType(ImageIOBase::UNKNOWNCOMPONENTTYPE);
if (!itkSetPixelType(this,ptype,ImageIOBase::CHAR, char(0) ) &&
!itkSetPixelType(this,ptype,ImageIOBase::UCHAR, (unsigned char)0) &&
!itkSetPixelType(this,ptype,ImageIOBase::SHORT,(short)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::USHORT,(unsigned short)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::INT,(int)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::UINT,(unsigned int)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::LONG,(long)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::ULONG,(unsigned long)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::FLOAT,(float)(0)) &&
!itkSetPixelType(this,ptype,ImageIOBase::DOUBLE,(double)(0)) )
{
if ( ptype == typeid(Offset<2>) )
{
this->SetNumberOfComponents(2);
this->SetPixelType(ImageIOBase::OFFSET);
this->SetComponentType(ImageIOBase::LONG);
}
else if ( ptype == typeid(Offset<3>) )
{
this->SetNumberOfComponents(3);
this->SetPixelType(ImageIOBase::OFFSET);
this->SetComponentType(ImageIOBase::LONG);
}
else if ( ptype == typeid(Offset<4>) )
{
this->SetNumberOfComponents(4);
this->SetPixelType(ImageIOBase::OFFSET);
this->SetComponentType(ImageIOBase::LONG);
}
}
if( this->GetPixelType() == ImageIOBase::UNKNOWNPIXELTYPE )
{
itkExceptionMacro("Pixel type currently not supported. typeid.name = " << ptype.name() );
return false;
}
if( this->GetComponentType() == ImageIOBase::UNKNOWNCOMPONENTTYPE )
{
itkExceptionMacro("Pixel Component type currently not supported. typeid.name = " << ptype.name() );
return false;
}
return true;
}
void ImageIOBase::ComputeStrides()
{
unsigned int i;
m_Strides[0] = this->GetComponentSize();
m_Strides[1] = m_NumberOfComponents * m_Strides[0];
for (i = 2; i <= (m_NumberOfDimensions+1); i++)
{
m_Strides[i] = m_Dimensions[i-2] * m_Strides[i-1];
}
}
// Calculates the image size in PIXELS
ImageIOBase::SizeType
ImageIOBase
::GetImageSizeInPixels() const
{
unsigned int i;
SizeType numPixels = 1;
for (i = 0; i < m_NumberOfDimensions; i++)
{
numPixels *= m_Dimensions[i];
}
return numPixels;
}
ImageIOBase::SizeType
ImageIOBase
::GetImageSizeInComponents() const
{
return (this->GetImageSizeInPixels() * m_NumberOfComponents);
}
ImageIOBase::SizeType
ImageIOBase
::GetImageSizeInBytes () const
{
return (this->GetImageSizeInComponents() * this->GetComponentSize());
}
ImageIOBase::SizeType
ImageIOBase
::GetComponentStride() const
{
return m_Strides[0];
}
ImageIOBase::SizeType
ImageIOBase
::GetPixelStride () const
{
return m_Strides[1];
}
ImageIOBase::SizeType
ImageIOBase
::GetRowStride () const
{
return m_Strides[2];
}
ImageIOBase::SizeType
ImageIOBase
::GetSliceStride () const
{
return m_Strides[3];
}
void ImageIOBase::SetNumberOfDimensions(unsigned int dim)
{
if(dim != m_NumberOfDimensions)
{
m_Origin.resize( dim );
m_Spacing.resize( dim );
m_Direction.resize( dim );
m_Strides.resize( dim+2 );
m_NumberOfDimensions = dim;
m_Dimensions.resize( dim );
m_Direction.resize( dim );
std::vector<double> axis( dim );
for (unsigned int i=0; i<dim; i++)
{
for (unsigned int j=0; j < dim; j++)
{
if (i == j)
{
axis[j] = 1.0;
}
else
{
axis[j] = 0.0;
}
}
this->SetDirection(i, axis);
}
this->Modified();
}
}
bool
ImageIOBase
::ReadBufferAsBinary(std::istream& is, void *buffer, ImageIOBase::SizeType num)
{
const SizeType numberOfBytesToBeRead = num;
is.read( static_cast<char *>( buffer ), numberOfBytesToBeRead );
const SizeType numberOfBytesRead = is.gcount();
#ifdef __APPLE_CC__
// fail() is broken in the Mac. It returns true when reaches eof().
if ( numberOfBytesRead != numberOfBytesToBeRead )
#else
if ( ( numberOfBytesRead != numberOfBytesToBeRead ) || is.fail() )
#endif
{
return false; // read failed
}
return true;
}
unsigned int ImageIOBase::GetPixelSize() const
{
if (m_ComponentType == UNKNOWNCOMPONENTTYPE
|| m_PixelType == UNKNOWNPIXELTYPE)
{
itkExceptionMacro ("Unknown pixel or component type: ("
<< m_PixelType << ", " << m_ComponentType << ")");
return 0;
}
return this->GetComponentSize() * this->GetNumberOfComponents();
}
unsigned int ImageIOBase::GetComponentSize() const
{
switch(m_ComponentType)
{
case UCHAR:
return sizeof(unsigned char);
case CHAR:
return sizeof(char);
case USHORT:
return sizeof(unsigned short);
case SHORT:
return sizeof(short);
case UINT:
return sizeof(unsigned int);
case INT:
return sizeof(int);
case ULONG:
return sizeof(unsigned long);
case LONG:
return sizeof(long);
case FLOAT:
return sizeof(float);
case DOUBLE:
return sizeof(double);
case UNKNOWNCOMPONENTTYPE:
default:
itkExceptionMacro ("Unknown component type: " << m_ComponentType);
}
return 0;
}
std::string ImageIOBase::GetFileTypeAsString(FileType t) const
{
std::string s;
switch(t)
{
case ASCII:
return s = "ASCII";
case Binary:
return s = "Binary";
case TypeNotApplicable:
default:
return s = "TypeNotApplicable";
}
return s="TypeNotApplicable";
}
std::string ImageIOBase::GetByteOrderAsString(ByteOrder t) const
{
std::string s;
switch(t)
{
case BigEndian:
return s = "BigEndian";
case LittleEndian:
return s = "LittleEndian";
case OrderNotApplicable:
default:
return s = "OrderNotApplicable";
}
return s="OrderNotApplicable";
}
std::string ImageIOBase::GetComponentTypeAsString(IOComponentType t) const
{
std::string s;
switch(t)
{
case UCHAR:
return (s = "unsigned_char");
case CHAR:
return (s = "char");
case USHORT:
return (s = "unsigned_short");
case SHORT:
return (s = "short");
case UINT:
return (s = "unsigned_int");
case INT:
return (s = "int");
case ULONG:
return (s = "unsigned_long");
case LONG:
return (s = "long");
case FLOAT:
return (s = "float");
case DOUBLE:
return (s = "double");
case UNKNOWNCOMPONENTTYPE:
default:
return (s = "unknown");
}
return (s="unknown");
}
std::string ImageIOBase::GetPixelTypeAsString(IOPixelType t) const
{
std::string s;
switch(t)
{
case SCALAR:
return (s = "scalar");
case VECTOR:
return (s = "vector");
case COVARIANTVECTOR:
return (s = "covariant_vector");
case POINT:
return (s = "point");
case OFFSET:
return (s = "offset");
case RGB:
return (s = "rgb");
case RGBA:
return (s = "rgba");
case SYMMETRICSECONDRANKTENSOR:
return (s = "symmetric_second_rank_tensor");
case DIFFUSIONTENSOR3D:
return (s = "diffusion_tensor_3D");
case COMPLEX:
return (s = "complex");
case UNKNOWNPIXELTYPE:
default:
itkExceptionMacro ("Unknown pixel type: " << t);
}
return (s="unknown");
}
namespace {
template <class TComponent>
void WriteBuffer(std::ostream& os, const TComponent *buffer, ImageIOBase::SizeType num)
{
const TComponent *ptr = buffer;
for (ImageIOBase::SizeType i=0; i < num; i++)
{
if ( !(i%6) && i ) os << "\n";
os << *ptr++ << " ";
}
}
}
void ImageIOBase::WriteBufferAsASCII(std::ostream& os, const void *buffer,
IOComponentType ctype,
ImageIOBase::SizeType numComp)
{
switch (ctype)
{
case UCHAR:
{
typedef const unsigned char * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case CHAR:
{
typedef const char * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case USHORT:
{
typedef const unsigned short * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case SHORT:
{
typedef const short * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case UINT:
{
typedef const unsigned int * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case INT:
{
typedef const int * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case ULONG:
{
typedef const unsigned long * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case LONG:
{
typedef const long * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case FLOAT:
{
typedef const float * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
case DOUBLE:
{
typedef const double * Type;
Type buf = reinterpret_cast<Type>(buffer);
WriteBuffer(os, buf, numComp);
}
break;
default:
break;
}
}
template <class TComponent>
void ReadBuffer(std::istream& is, TComponent *buffer, ImageIOBase::SizeType num)
{
TComponent *ptr = buffer;
for( ImageIOBase::SizeType i=0; i < num; i++, ptr++ )
{
is >> *ptr;
}
}
void ImageIOBase::ReadBufferAsASCII(std::istream& is, void *buffer,
IOComponentType ctype,
ImageIOBase::SizeType numComp)
{
switch (ctype)
{
case UCHAR:
{
unsigned char *buf = reinterpret_cast<unsigned char*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case CHAR:
{
char *buf = reinterpret_cast<char*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case USHORT:
{
unsigned short *buf = reinterpret_cast<unsigned short*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case SHORT:
{
short *buf = reinterpret_cast<short*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case UINT:
{
unsigned int *buf = reinterpret_cast<unsigned int*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case INT:
{
int *buf = reinterpret_cast<int*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case ULONG:
{
unsigned long *buf = reinterpret_cast<unsigned long*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case LONG:
{
long *buf = reinterpret_cast<long*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case FLOAT:
{
float *buf = reinterpret_cast<float*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
case DOUBLE:
{
double *buf = reinterpret_cast<double*>(buffer);
ReadBuffer(is, buf, numComp);
}
break;
default:
break;
}
}
/** Given a requested region, determine what could be the region that we can
* read from the file. This is called the streamable region, which will be
* smaller than the LargestPossibleRegion and greater or equal to the
* RequestedRegion */
ImageIORegion
ImageIOBase
::GenerateStreamableReadRegionFromRequestedRegion(
const ImageIORegion & requested ) const
{
//
// The default implementations determines that the streamable region is
// equal to the largest possible region of the image.
//
// Since the image in the file may have a dimension lower
// than the image type over which the ImageFileReader/Writer is
// being instantiated, we must fill in the co-dimensions in a
// consistent way.
// First: allocate with the image IO number of dimensions
ImageIORegion streamableRegion( requested.GetImageDimension() );
// Second: copy only the number of dimension that the image has.
unsigned int maxDimensionToCopy =
this->m_NumberOfDimensions > requested.GetImageDimension() ?
requested.GetImageDimension() : this->m_NumberOfDimensions;
for( unsigned int i=0; i < maxDimensionToCopy; i++ )
{
streamableRegion.SetSize( i, this->m_Dimensions[i] );
streamableRegion.SetIndex( i, 0 );
}
// Third: set the rest to the default : start = 0, size = 1
for( unsigned int j=maxDimensionToCopy; j<requested.GetImageDimension(); j++ )
{
streamableRegion.SetSize( j, 1 );
streamableRegion.SetIndex( j, 0 );
}
// Finally: return the streamable region
return streamableRegion;
}
void ImageIOBase::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "FileName: " << m_FileName << std::endl;
os << indent << "FileType: " << this->GetFileTypeAsString(m_FileType) << std::endl;
os << indent << "ByteOrder: " << this->GetByteOrderAsString(m_ByteOrder) << std::endl;
os << indent << "IORegion: " << std::endl;
m_IORegion.Print(os, indent.GetNextIndent());
os << indent << "Number of Components/Pixel: " << m_NumberOfComponents << "\n";
os << indent << "Pixel Type: " << this->GetPixelTypeAsString(m_PixelType) << std::endl;
os << indent << "Component Type: " << this->GetComponentTypeAsString(m_ComponentType)
<< std::endl;
os << indent << "Dimensions: ( ";
for (unsigned int i=0; i < m_NumberOfDimensions; i++)
{
os << m_Dimensions[i] << " ";
}
os << ")" << std::endl;
if (m_UseCompression)
{
os << indent << "UseCompression: On" << std::endl;
}
else
{
os << indent << "UseCompression: Off" << std::endl;
}
if (m_UseStreamedReading)
{
os << indent << "UseStreamedReading: On" << std::endl;
}
else
{
os << indent << "UseStreamedReading: Off" << std::endl;
}
if (m_UseStreamedWriting)
{
os << indent << "UseStreamedWriting: On" << std::endl;
}
else
{
os << indent << "UseStreamedWriting: Off" << std::endl;
}
}
} //namespace itk
|