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
00027 #include <iomanip>
00028 #include <fstream>
00029
00030 namespace gdcm
00031 {
00032
00033
00034
00039 DocEntry::DocEntry(DictEntry *in)
00040 {
00041 ImplicitVR = false;
00042 DicomDict = in;
00043 Offset = 0 ;
00044
00045
00046 ReadLength = 0;
00047 Length = 0;
00048
00049 gdcmAssertMacro(DicomDict);
00050 DicomDict->Register();
00051 }
00052
00056 DocEntry::~DocEntry()
00057 {
00058 gdcmAssertMacro(DicomDict);
00059
00060 DicomDict->Unregister();
00061 }
00062
00063
00064
00070 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
00071 {
00072 uint32_t ffff = 0xffffffff;
00073 uint16_t group = GetGroup();
00074
00076
00077 VRKey vr = GetVR();
00078 uint16_t elem = GetElement();
00079 uint32_t lgth = GetLength();
00080
00081 if ( group == 0xfffe && elem == 0x0000 )
00082 {
00083
00084
00085
00086 return;
00087 }
00088
00089
00090
00091
00092
00093
00094 if (lgth%2)
00095 lgth ++;
00096
00097
00098 binary_write( *fp, group);
00099 binary_write( *fp, elem);
00100
00101
00102 if ( filetype == ExplicitVR || filetype == JPEG || group == 0x0002 )
00103 {
00104
00105
00106
00107
00108 if (group == 0xfffe)
00109 {
00110
00111
00112
00113
00114
00115
00116
00117
00118 uint32_t ff = 0xffffffff;
00119 binary_write(*fp, ff);
00120 return;
00121 }
00122
00123 uint16_t zero = 0;
00124 uint16_t shortLgr = (uint16_t)lgth;
00125
00126 if( IsVRUnknown() )
00127 {
00128
00129
00130
00131 binary_write(*fp, lgth);
00132 }
00133 else
00134 {
00135 binary_write(*fp, vr.str());
00136
00137
00138 if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF")
00139 || (vr == "UN") || (vr == "UT") )
00140 {
00141 binary_write(*fp, zero);
00142 if (vr == "SQ")
00143 {
00144
00145
00146
00147 binary_write(*fp, ffff);
00148 }
00149 else
00150 {
00151 binary_write(*fp, lgth);
00152 }
00153 }
00154 else
00155 {
00156 binary_write(*fp, shortLgr);
00157 }
00158 }
00159 }
00160 else
00161 {
00162
00163 if (vr == "SQ")
00164 {
00165 binary_write(*fp, ffff);
00166 }
00167 else
00168 {
00169 binary_write(*fp, lgth);
00170 }
00171 }
00172 }
00173
00178 uint32_t DocEntry::GetFullLength()
00179 {
00180 uint32_t l = GetReadLength();
00181 if ( IsImplicitVR() )
00182 {
00183 l = l + 8;
00184 }
00185 else
00186 {
00187 if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
00188 {
00189 l = l + 12;
00190 }
00191 else
00192 {
00193 l = l + 8;
00194 }
00195 }
00196 return l;
00197 }
00198
00203 bool DocEntry::IsItemDelimitor()
00204 {
00205 return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
00206 }
00207
00212 bool DocEntry::IsItemStarter()
00213 {
00214 return (GetGroup() == 0xfffe && GetElement() == 0xe000);
00215 }
00216
00221 bool DocEntry::IsSequenceDelimitor()
00222 {
00223 return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
00224 }
00225
00230 void DocEntry::Copy(DocEntry *doc)
00231 {
00232 Length = doc->Length;
00233 ReadLength = doc->ReadLength;
00234 ImplicitVR = doc->ImplicitVR;
00235 Offset = doc->Offset;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00251 void DocEntry::Print(std::ostream &os, std::string const & )
00252 {
00253 size_t o;
00254 std::string st;
00255 TSKey v;
00256 std::string d2;
00257 VRKey vr;
00258 std::ostringstream s;
00259 uint32_t lgth;
00260
00261 o = GetOffset();
00262 vr = GetVR();
00263 if ( vr == GDCM_VRUNKNOWN )
00264 vr = " ";
00265
00266 s << DictEntry::TranslateToKey(GetGroup(),GetElement());
00267
00268 if (PrintLevel >= 2)
00269 {
00270 s << " lg : ";
00271 lgth = GetReadLength();
00272 if (lgth == 0xffffffff)
00273 {
00274 st = " ffff ";
00275 s.setf(std::ios::left);
00276 s << std::setw(4);
00277 s << " x(ffff) ";
00278 s.setf(std::ios::left);
00279 s << std::setw(8) << "-1";
00280 }
00281 else
00282 {
00283 st = Util::Format("x(%x)",lgth);
00284 s.setf(std::ios::left);
00285 s << std::setw(11-st.size()) << " ";
00286 s << st << " ";
00287 s.setf(std::ios::left);
00288 s << std::setw(8) << lgth;
00289 }
00290 s << " Off.: ";
00291 st = Util::Format("x(%x)",o);
00292 s << std::setw(11-st.size()) << " ";
00293 s << st << " ";
00294 s << std::setw(8) << o;
00295 }
00296 if (PrintLevel >= 1)
00297 s << " ";
00298
00299 s << "[" << vr << "] ";
00300
00301 std::string name;
00302 if ( GetElement() == 0x0000 )
00303 name = "Group Length";
00304 else
00305 name = GetName();
00306
00307 if (PrintLevel >= 1)
00308 {
00309 s.setf(std::ios::left);
00310 s << std::setw(66-name.length()) << " ";
00311 }
00312
00313 s << "[" << name << "]";
00314 os << s.str();
00315 }
00316
00317
00318 }