00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmDocEntry.h"
00020 #include "gdcmDataEntry.h"
00021 #include "gdcmTS.h"
00022 #include "gdcmVR.h"
00023 #include "gdcmGlobal.h"
00024 #include "gdcmUtil.h"
00025 #include "gdcmDebug.h"
00026 #include "gdcmDictSet.h"
00027 #include <iomanip>
00028 #include <fstream>
00029
00030 namespace GDCM_NAME_SPACE
00031 {
00032
00033
00034
00041 DocEntry::DocEntry(uint16_t group, uint16_t elem, VRKey const &vr)
00042 {
00043 ImplicitVR = false;
00044 DicomDict = 0;
00045 Offset = 0 ;
00046
00047
00048 ReadLength = 0;
00049 Length = 0;
00050
00051 VR = vr;
00052 Key.SetGroupElem(group,elem);
00053 }
00054
00058 DocEntry::~DocEntry()
00059 {
00060 if (DicomDict)
00061 {
00062 gdcmAssertMacro(DicomDict);
00063 DicomDict->Unregister();
00064 }
00065 }
00066
00067
00068
00074 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype, bool insideMetaElements)
00075 {
00076 uint32_t ffff = 0xffffffff;
00077 uint16_t group = GetGroup();
00078
00080
00081 VRKey vr = GetVR();
00082 uint16_t elem = GetElement();
00083 uint32_t lgth = GetLength();
00084
00085 if ( group == 0xfffe && elem == 0x0000 )
00086 {
00087
00088
00089
00090 return;
00091 }
00092
00093
00094
00095
00096
00097
00098 if (lgth%2)
00099 lgth ++;
00100
00101
00102 binary_write( *fp, group);
00103 binary_write( *fp, elem);
00104
00105
00106 if ( filetype == ExplicitVR || filetype == JPEG || filetype == JPEG2000 || (group == 0x0002 && insideMetaElements) )
00107 {
00108
00109
00110
00111 if (group == 0xfffe)
00112 {
00113
00114
00115
00116
00117
00118
00119
00120
00121 uint32_t ff = 0xffffffff;
00122 binary_write(*fp, ff);
00123 return;
00124 }
00125 uint16_t zero = 0;
00126 uint16_t shortLgr = (uint16_t)lgth;
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 if( IsVRUnknown() )
00138 {
00139
00140
00141
00142 SetVR("UN");
00143 vr=GetVR();
00144 }
00145
00146
00147 binary_write(*fp, vr.str());
00148
00149 if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF")
00150 || (vr == "UN") || (vr == "UT") )
00151 {
00152 binary_write(*fp, zero);
00153 if ( (filetype == JPEG || filetype == JPEG2000) && group == 0x7fe0 && elem == 0x0010)
00154 {
00155 gdcmAssertMacro( GetVR() == "OW" );
00156 binary_write(*fp, ffff);
00157 }
00158 else if (vr == "SQ")
00159 {
00160
00161
00162
00163 binary_write(*fp, ffff);
00164 }
00165 else
00166 {
00167 binary_write(*fp, lgth);
00168 }
00169 }
00170 else
00171 {
00172 binary_write(*fp, shortLgr);
00173 }
00174
00175 }
00176 else
00177 {
00178
00179 if (vr == "SQ")
00180 {
00181 binary_write(*fp, ffff);
00182 }
00183 else
00184 {
00185 binary_write(*fp, lgth);
00186 }
00187 }
00188 }
00189
00192 std::string const &DocEntry::GetName()
00193 {
00194 if (DicomDict == 0)
00195 DicomDict =
00196 Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
00197 if (DicomDict == 0)
00198 return GDCM_UNKNOWN;
00199 else
00200 {
00201 DicomDict->Register();
00202 return DicomDict->GetName();
00203 }
00204 }
00205
00209 std::string const &DocEntry::GetVM()
00210 {
00211 if (DicomDict == 0)
00212 DicomDict =
00213 Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
00214 if (DicomDict == 0)
00215 return GDCM_UNKNOWN;
00216 else
00217 {
00218 DicomDict->Register();
00219 return DicomDict->GetVM();
00220 }
00221 }
00222
00227 uint32_t DocEntry::GetFullLength()
00228 {
00229 uint32_t l = GetReadLength();
00230 if ( IsImplicitVR() )
00231 {
00232 l = l + 8;
00233 }
00234 else
00235 {
00236 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
00237 {
00238 l = l + 12;
00239 }
00240 else
00241 {
00242 l = l + 8;
00243 }
00244 }
00245 return l;
00246 }
00247
00252 void DocEntry::Copy(DocEntry *doc)
00253 {
00254 Length = doc->Length;
00255 ReadLength = doc->ReadLength;
00256 ImplicitVR = doc->ImplicitVR;
00257 Offset = doc->Offset;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00273 void DocEntry::Print(std::ostream &os, std::string const & )
00274 {
00275 size_t o;
00276 std::string st;
00277 TSKey v;
00278 std::string d2;
00279 VRKey vr;
00280 std::ostringstream s;
00281 uint32_t lgth;
00282
00283 o = GetOffset();
00284 vr = GetVR();
00285 if ( vr == GDCM_VRUNKNOWN )
00286 vr = " ";
00287
00288 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
00289
00290 if (PrintLevel >= 2)
00291 {
00292 s << " lg : ";
00293 lgth = GetReadLength();
00294 if (lgth == 0xffffffff)
00295 {
00296 st = " ffff ";
00297 s.setf(std::ios::left);
00298 s << std::setw(4);
00299 s << " x(ffff) ";
00300 s.setf(std::ios::left);
00301 s << std::setw(8) << "-1";
00302 }
00303 else
00304 {
00305 st = Util::Format("x(%x)",lgth);
00306 s.setf(std::ios::left);
00307 s << std::setw(11-st.size()) << " ";
00308 s << st << " ";
00309 s.setf(std::ios::left);
00310 s << std::setw(8) << lgth;
00311 }
00312 s << " Off.: ";
00313 st = Util::Format("x(%x)",o);
00314 s << std::setw(11-st.size()) << " ";
00315 s << st << " ";
00316 s << std::setw(8) << o;
00317 }
00318 if (PrintLevel >= 1)
00319 s << " ";
00320
00321 s << "[" << vr << "] ";
00322
00323 std::string name;
00324 uint16_t e = GetElement();
00325 if ( e == 0x0000 )
00326 name = "Group Length";
00327 else if ( GetGroup()%2 == 1 )
00328 {
00329 if ( e >= 0x0010 && e <= 0x00ff )
00330 name = "Private Creator";
00331 else if (e == 0x0001)
00332 name = "Private Group Length To End";
00333 }
00334 else
00335 {
00336 name = GetName();
00337
00338 if (name[name.length()-1] == 0x0d || name[name.length()-1] == 0x0a)
00339 {
00340 name.replace(name.length()-1, 1, 1, ' ');
00341 }
00342 }
00343 if (PrintLevel >= 1)
00344 {
00345 s.setf(std::ios::left);
00346 s << std::setw(66-name.length()) << " ";
00347 }
00348
00349 s << "[" << name << "]";
00350 os << s.str();
00351 }
00352
00353
00354 }