00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef GDCMELEMENT_H
00016 #define GDCMELEMENT_H
00017
00018 #include "gdcmTypes.h"
00019 #include "gdcmVR.h"
00020 #include "gdcmTag.h"
00021 #include "gdcmVM.h"
00022 #include "gdcmByteValue.h"
00023 #include "gdcmDataElement.h"
00024 #include "gdcmSwapper.h"
00025
00026 #include <string>
00027 #include <vector>
00028 #include <sstream>
00029 #include <limits>
00030 #include <cmath>
00031
00032 namespace gdcm
00033 {
00034
00035
00041 template<int T> class EncodingImplementation;
00042
00048 template<int TVR, int TVM>
00049 class Element
00050 {
00051 public:
00052 typename VRToType<TVR>::Type Internal[VMToLength<TVM>::Length];
00053 typedef typename VRToType<TVR>::Type Type;
00054
00055 unsigned long GetLength() const {
00056 return VMToLength<TVM>::Length;
00057 }
00058
00059
00060
00061 void Print(std::ostream &_os) const {
00062 _os << Internal[0];
00063 for(int i=1; i<VMToLength<TVM>::Length; ++i)
00064 _os << "," << Internal[i];
00065 }
00066
00067 const typename VRToType<TVR>::Type *GetValues() const {
00068 return Internal;
00069 }
00070 const typename VRToType<TVR>::Type &GetValue(unsigned int idx = 0) const {
00071 assert( idx < VMToLength<TVM>::Length );
00072 return Internal[idx];
00073 }
00074 typename VRToType<TVR>::Type &GetValue(unsigned int idx = 0) {
00075 assert( idx < VMToLength<TVM>::Length );
00076 return Internal[idx];
00077 }
00078 typename VRToType<TVR>::Type operator[] (unsigned int idx) const {
00079 return GetValue(idx);
00080 }
00081 void SetValue(typename VRToType<TVR>::Type v, unsigned int idx = 0) {
00082 assert( idx < VMToLength<TVM>::Length );
00083 Internal[idx] = v;
00084 }
00085
00086 void SetFromDataElement(DataElement const &de) {
00087 const ByteValue *bv = de.GetByteValue();
00088 if( !bv ) return;
00089 if( de.GetVR() == VR::UN || de.GetVR() == VR::INVALID )
00090 {
00091 Set(de.GetValue());
00092 }
00093 else
00094 {
00095 SetNoSwap(de.GetValue());
00096 }
00097 }
00098
00099 void Read(std::istream &_is) {
00100 return EncodingImplementation<VRToEncoding<TVR>::Mode>::Read(Internal,
00101 GetLength(),_is);
00102 }
00103 void Write(std::ostream &_os) const {
00104 return EncodingImplementation<VRToEncoding<TVR>::Mode>::Write(Internal,
00105 GetLength(),_os);
00106 }
00107
00108
00109
00110 void Set(Value const &v) {
00111 const ByteValue *bv = dynamic_cast<const ByteValue*>(&v);
00112 assert( bv );
00113
00114 std::stringstream ss;
00115 std::string s = std::string( bv->GetPointer(), bv->GetLength() );
00116 ss.str( s );
00117 EncodingImplementation<VRToEncoding<TVR>::Mode>::Read(Internal,
00118 GetLength(),ss);
00119 }
00120 protected:
00121 void SetNoSwap(Value const &v) {
00122 const ByteValue *bv = dynamic_cast<const ByteValue*>(&v);
00123 assert( bv );
00124
00125 std::stringstream ss;
00126 std::string s = std::string( bv->GetPointer(), bv->GetLength() );
00127 ss.str( s );
00128 EncodingImplementation<VRToEncoding<TVR>::Mode>::ReadNoSwap(Internal,
00129 GetLength(),ss);
00130 }
00131 };
00132
00133 struct ignore_char {
00134 ignore_char(char c): m_char(c) {}
00135 char m_char;
00136 };
00137 ignore_char const backslash('\\');
00138
00139 inline std::istream& operator>> (std::istream& in, ignore_char const& ic) {
00140 if (!in.eof())
00141 in.clear(in.rdstate() & ~std::ios_base::failbit);
00142 if (in.get() != ic.m_char)
00143 in.setstate(std::ios_base::failbit);
00144 return in;
00145 }
00146
00147
00148
00149 template<> class EncodingImplementation<VR::VRASCII> {
00150 public:
00151 template<typename T>
00152 static inline void ReadComputeLength(T* data, unsigned int &length,
00153 std::istream &_is) {
00154 assert( data );
00155
00156 length = 0;
00157 assert( _is );
00158 #if 0
00159 char sep;
00160 while( _is >> data[length++] )
00161 {
00162
00163 assert( _is );
00164 _is.get(sep);
00165 assert( sep == '\\' || sep == ' ' );
00166 if( sep == ' ' ) length--;
00167 }
00168 #else
00169 while( _is >> std::ws >> data[length++] >> std::ws >> backslash )
00170 {
00171 }
00172 #endif
00173 }
00174
00175 template<typename T>
00176 static inline void Read(T* data, unsigned long length,
00177 std::istream &_is) {
00178 assert( data );
00179 assert( length );
00180 assert( _is );
00181
00182
00183 _is >> std::ws >> data[0];
00184 char sep;
00185
00186 for(unsigned long i=1; i<length;++i) {
00187 assert( _is );
00188
00189 _is >> std::ws >> sep;
00190 assert( sep == '\\' );
00191 _is >> std::ws >> data[i];
00192 }
00193 }
00194
00195 template<typename T>
00196 static inline void ReadNoSwap(T* data, unsigned long length,
00197 std::istream &_is) {
00198 Read(data,length,_is);
00199 }
00200 template<typename T>
00201 static inline void Write(const T* data, unsigned long length,
00202 std::ostream &_os) {
00203 assert( data );
00204 assert( length );
00205 assert( _os );
00206 _os << data[0];
00207 for(unsigned long i=1; i<length; ++i) {
00208 assert( _os );
00209 _os << "\\" << data[i];
00210 }
00211 }
00212 };
00213
00214 template < typename Float >
00215 std::string to_string ( Float data ) {
00216 std::stringstream in;
00217
00218 unsigned long const digits =
00219 static_cast< unsigned long >(
00220 - std::log( std::numeric_limits<Float>::epsilon() )
00221 / std::log( 10.0 ) );
00222 if ( in << std::dec << std::setprecision(digits) << data ) {
00223 return ( in.str() );
00224 } else {
00225 throw "Impossible Conversion";
00226 }
00227 }
00228
00229
00230
00231 template<> inline void EncodingImplementation<VR::VRASCII>::Write(const float * data, unsigned long length, std::ostream &_os) {
00232 assert( data );
00233 assert( length );
00234 assert( _os );
00235 _os << to_string(data[0]);
00236 for(unsigned long i=1; i<length; ++i) {
00237 assert( _os );
00238 _os << "\\" << to_string(data[i]);
00239 }
00240 }
00241
00242 template<> inline void EncodingImplementation<VR::VRASCII>::Write(const double* data, unsigned long length, std::ostream &_os) {
00243 assert( data );
00244 assert( length );
00245 assert( _os );
00246 _os << to_string(data[0]);
00247 for(unsigned long i=1; i<length; ++i) {
00248 assert( _os );
00249 _os << "\\" << to_string(data[i]);
00250 }
00251 }
00252
00253
00254
00255
00256
00257
00258
00259 template<> class EncodingImplementation<VR::VRBINARY> {
00260 public:
00261 template<typename T>
00262 static inline void ReadComputeLength(T* data, unsigned int &length,
00263 std::istream &_is) {
00264 const unsigned int type_size = sizeof(T);
00265 assert( data );
00266
00267 length /= type_size;
00268 assert( _is );
00269 _is.read( reinterpret_cast<char*>(data+0), type_size);
00270 for(unsigned long i=1; i<length; ++i) {
00271 assert( _is );
00272 _is.read( reinterpret_cast<char*>(data+i), type_size );
00273 }
00274 }
00275 template<typename T>
00276 static inline void ReadNoSwap(T* data, unsigned long length,
00277 std::istream &_is) {
00278 const unsigned int type_size = sizeof(T);
00279 assert( data );
00280 assert( length );
00281 assert( _is );
00282 _is.read( reinterpret_cast<char*>(data+0), type_size);
00283 for(unsigned long i=1; i<length; ++i) {
00284 assert( _is );
00285 _is.read( reinterpret_cast<char*>(data+i), type_size );
00286 }
00287
00288
00289
00290 }
00291 template<typename T>
00292 static inline void Read(T* data, unsigned long length,
00293 std::istream &_is) {
00294 const unsigned int type_size = sizeof(T);
00295 assert( data );
00296 assert( length );
00297 assert( _is );
00298 _is.read( reinterpret_cast<char*>(data+0), type_size);
00299 for(unsigned long i=1; i<length; ++i) {
00300 assert( _is );
00301 _is.read( reinterpret_cast<char*>(data+i), type_size );
00302 }
00303
00304
00305 SwapperNoOp::SwapArray(data,length);
00306 }
00307 template<typename T>
00308 static inline void Write(const T* data, unsigned long length,
00309 std::ostream &_os) {
00310 const unsigned int type_size = sizeof(T);
00311 assert( data );
00312 assert( length );
00313 assert( _os );
00314
00315
00316 T swappedData = SwapperNoOp::Swap(data[0]);
00317 _os.write( reinterpret_cast<const char*>(&swappedData), type_size);
00318 for(unsigned long i=1; i<length;++i) {
00319 assert( _os );
00320 swappedData = SwapperNoOp::Swap(data[i]);
00321 _os.write( reinterpret_cast<const char*>(&swappedData), type_size );
00322 }
00323
00324
00325 }
00326 };
00327
00328
00329
00330
00331
00332 #if 0
00333 template<int TVM>
00334 class Element<TVM>
00335 {
00336 public:
00337 Element(const char array[])
00338 {
00339 unsigned int i = 0;
00340 const char sep = '\\';
00341 std::string sarray = array;
00342 std::string::size_type pos1 = 0;
00343 std::string::size_type pos2 = sarray.find(sep, pos1+1);
00344 while(pos2 != std::string::npos)
00345 {
00346 Internal[i++] = sarray.substr(pos1, pos2-pos1);
00347 pos1 = pos2+1;
00348 pos2 = sarray.find(sep, pos1+1);
00349 }
00350 Internal[i] = sarray.substr(pos1, pos2-pos1);
00351
00352
00353 assert( GetLength()-1 == i );
00354 }
00355
00356 unsigned long GetLength() const {
00357 return VMToLength<TVM>::Length;
00358 }
00359
00360 void Print(std::ostream &_os) const {
00361 _os << Internal[0];
00362 for(int i=1; i<VMToLength<TVM>::Length; ++i)
00363 _os << "," << Internal[i];
00364 }
00365
00366 void Read(std::istream &_is) {
00367 EncodingImplementation<VR::VRASCII>::Read(Internal, GetLength(),_is);
00368 }
00369 void Write(std::ostream &_os) const {
00370 EncodingImplementation<VR::VRASCII>::Write(Internal, GetLength(),_os);
00371 }
00372 private:
00373 typename String Internal[VMToLength<TVM>::Length];
00374 };
00375
00376 template< int TVM>
00377 class Element<VR::PN, TVM> : public StringElement<TVM>
00378 {
00379 };
00380 #endif
00381
00382
00383 template<int TVR>
00384 class Element<TVR, VM::VM1_n>
00385 {
00386 public:
00387
00388 explicit Element() { Internal=0; Length=0; Save = false; }
00389 ~Element() {
00390 if( Save ) {
00391 delete[] Internal;
00392 }
00393 Internal = 0;
00394 }
00395
00396
00397
00398
00399 unsigned long GetLength() const { return Length; }
00400 typedef typename VRToType<TVR>::Type Type;
00401
00402 void SetLength(unsigned long len) {
00403 const unsigned int size = sizeof(Type);
00404 if( len ) {
00405 if( len > Length ) {
00406
00407 assert( (len / size) * size == len );
00408 Type *internal = new Type[len / size];
00409 assert( Save == false );
00410 Save = true;
00411 if( Internal )
00412 {
00413 memcpy(internal, Internal, len);
00414 delete[] Internal;
00415 }
00416 Internal = internal;
00417 }
00418 }
00419 Length = len / size;
00420 }
00421
00422
00423
00424 void SetArray(const Type *array, unsigned long len,
00425 bool save = false) {
00426 if( save ) {
00427 SetLength(len);
00428 memcpy(Internal, array, len);
00429 assert( Save == false );
00430 }
00431 else {
00432
00433 assert( Length == 0 );
00434 assert( Internal == 0 );
00435 assert( Save == false );
00436 Length = len / sizeof(Type);
00437
00438
00439
00440 if( (len / sizeof(Type)) * sizeof(Type) != len ) { Internal = 0; Length = 0; }
00441 else Internal = const_cast<Type*>(array);
00442 }
00443 Save = save;
00444 }
00445 void SetValue(typename VRToType<TVR>::Type v, unsigned int idx = 0) {
00446 assert( idx < Length );
00447 Internal[idx] = v;
00448 }
00449 const typename VRToType<TVR>::Type &GetValue(unsigned int idx = 0) const {
00450 assert( idx < Length );
00451 return Internal[idx];
00452 }
00453 typename VRToType<TVR>::Type &GetValue(unsigned int idx = 0) {
00454 assert( idx < Length );
00455 return Internal[idx];
00456 }
00457 typename VRToType<TVR>::Type operator[] (unsigned int idx) const {
00458 return GetValue(idx);
00459 }
00460 void Set(Value const &v) {
00461 const ByteValue *bv = dynamic_cast<const ByteValue*>(&v);
00462 assert( bv );
00463 if( (VR::VRType)(VRToEncoding<TVR>::Mode) == VR::VRBINARY )
00464 {
00465 const Type* array = (Type*)bv->GetPointer();
00466 if( array ) {
00467 assert( array );
00468 assert( Internal == 0 );
00469 SetArray(array, bv->GetLength() ); }
00470 }
00471 else
00472 {
00473 std::stringstream ss;
00474 std::string s = std::string( bv->GetPointer(), bv->GetLength() );
00475 ss.str( s );
00476 EncodingImplementation<VRToEncoding<TVR>::Mode>::Read(Internal,
00477 GetLength(),ss);
00478 }
00479 }
00480
00481
00482 void WriteASCII(std::ostream &os) const {
00483 return EncodingImplementation<VR::VRASCII>::Write(Internal, GetLength(), os);
00484 }
00485
00486
00487 void Print(std::ostream &_os) const {
00488 assert( Length );
00489 assert( Internal );
00490 _os << Internal[0];
00491 const unsigned long length = GetLength() < 25 ? GetLength() : 25;
00492 for(unsigned long i=1; i<length; ++i)
00493 _os << "," << Internal[i];
00494 }
00495 void Read(std::istream &_is) {
00496 if( !Internal ) return;
00497 EncodingImplementation<VRToEncoding<TVR>::Mode>::Read(Internal,
00498 GetLength(),_is);
00499 }
00500
00501
00502
00503
00504
00505 void Write(std::ostream &_os) const {
00506 EncodingImplementation<VRToEncoding<TVR>::Mode>::Write(Internal,
00507 GetLength(),_os);
00508 }
00509
00510 DataElement GetAsDataElement() const {
00511 DataElement ret;
00512 ret.SetVR( (VR::VRType)TVR );
00513 if( Internal )
00514 {
00515 std::ostringstream os;
00516 EncodingImplementation<VRToEncoding<TVR>::Mode>::Write(Internal,
00517 GetLength(),os);
00518 ret.SetByteValue( os.str().c_str(), os.str().size() );
00519 }
00520 return ret;
00521 }
00522
00523 Element(const Element&_val) {
00524 if( this != &_val) {
00525 *this = _val;
00526 }
00527 }
00528
00529 Element &operator=(const Element &_val) {
00530 Length = 0;
00531 Internal = 0;
00532 SetArray(_val.Internal, _val.Length, true);
00533 return *this;
00534 }
00535
00536 private:
00537 typename VRToType<TVR>::Type *Internal;
00538 unsigned long Length;
00539 bool Save;
00540 };
00541
00542
00543
00544
00545
00546 template<int TVR>
00547 class Element<TVR, VM::VM2_n> : public Element<TVR, VM::VM1_n>
00548 {
00549 public:
00550 typedef Element<TVR, VM::VM1_n> Parent;
00551 void SetLength(int len) {
00552 if( len <= 1 ) return;
00553 Parent::SetLength(len);
00554 }
00555 };
00556 template<int TVR>
00557 class Element<TVR, VM::VM2_2n> : public Element<TVR, VM::VM2_n>
00558 {
00559 public:
00560 typedef Element<TVR, VM::VM2_n> Parent;
00561 void SetLength(int len) {
00562 if( len % 2 ) return;
00563 Parent::SetLength(len);
00564 }
00565 };
00566 template<int TVR>
00567 class Element<TVR, VM::VM3_n> : public Element<TVR, VM::VM1_n>
00568 {
00569 public:
00570 typedef Element<TVR, VM::VM1_n> Parent;
00571 void SetLength(int len) {
00572 if( len <= 2 ) return;
00573 Parent::SetLength(len);
00574 }
00575 };
00576 template<int TVR>
00577 class Element<TVR, VM::VM3_3n> : public Element<TVR, VM::VM3_n>
00578 {
00579 public:
00580 typedef Element<TVR, VM::VM3_n> Parent;
00581 void SetLength(int len) {
00582 if( len % 3 ) return;
00583 Parent::SetLength(len);
00584 }
00585 };
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595 template<>
00596 class Element<VR::AS, VM::VM5>
00597 {
00598 public:
00599 char Internal[VMToLength<VM::VM5>::Length];
00600 void Print(std::ostream &_os) const {
00601 _os << Internal;
00602 }
00603 unsigned long GetLength() const {
00604 return VMToLength<VM::VM5>::Length;
00605 }
00606 };
00607
00608 template <>
00609 class Element<VR::OB, VM::VM1> : public Element<VR::OB, VM::VM1_n> {};
00610
00611 template <int TVM> class Element<VR::OB, TVM>;
00612
00613
00614 template <>
00615 class Element<VR::OW, VM::VM1> : public Element<VR::OW, VM::VM1_n> {};
00616
00617 template <int TVM> class Element<VR::OW, TVM>;
00618
00619 }
00620
00621 #endif //GDCMELEMENT_H