]> Creatis software - gdcm.git/blob - src/gdcmDicomDirMeta.cxx
ENH: Try to sync gdcm CVS and gdcm 1.2. ~2000 lines of changes, please be gentle...
[gdcm.git] / src / gdcmDicomDirMeta.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirMeta.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/02/16 20:06:13 $
7   Version:   $Revision: 1.35 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDicomDirMeta.h"
20 #include "gdcmDocument.h"
21 #include "gdcmDocEntry.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDataEntry.h"
25 namespace gdcm 
26 {
27 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
29 /**
30  * \brief  Constructor
31  */ 
32 DicomDirMeta::DicomDirMeta(bool empty):
33    DicomDirObject()
34 {
35    if ( !empty )
36    {
37       ListDicomDirStudyElem const &elemList = 
38          Global::GetDicomDirElements()->GetDicomDirMetaElements();
39       FillObject(elemList);
40    }
41 }
42
43 /**
44  * \brief   Canonical destructor.
45  */
46 DicomDirMeta::~DicomDirMeta() 
47 {
48 }
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /**
53  * \brief   Writes the Meta Elements
54  * @param fp ofstream to write to
55  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
56  * @return
57  */ 
58 void DicomDirMeta::WriteContent(std::ofstream *fp, FileType filetype)
59 {
60    // 'File Meta Information Version'
61    
62    uint8_t fmiv[2] = {0x02,0x00};
63    // FIXME : the following doesn't make the job (?!?)      
64    //SetEntryBinArea(fmiv, 0x0002,0x0001, 2); 
65    DataEntry *e00002_0001 = GetDataEntry(0x0002,0x0001);
66    e00002_0001->CopyBinArea(fmiv, 2);
67          
68    // 'Media Storage SOP Instance UID'   
69    DataEntry *e00002_0003 = GetDataEntry(0x0002,0x0003);
70    e00002_0003->SetString(Util::CreateUniqueUID());
71
72    // 'Implementation Class UID'
73    DataEntry *e00002_0012 = GetDataEntry(0x0002,0x0012);
74    e00002_0012->SetString(Util::CreateUniqueUID());   
75    
76    // Entry : 0002|0000 = group length -> recalculated
77    DataEntry *e0000 = GetDataEntry(0x0002,0x0000);
78    std::ostringstream sLen;
79    sLen << ComputeGroup0002Length( );
80    e0000->SetString(sLen.str());
81    
82    for (ListDocEntry::iterator i = DocEntries.begin();  
83                               i != DocEntries.end();
84                               ++i)
85    {
86       (*i)->WriteContent(fp, filetype);
87    }
88 }
89
90 /**
91  * \brief Re-computes the length of the Dicom group 0002 (in the DicomDirMeta)
92  */
93 int DicomDirMeta::ComputeGroup0002Length( ) 
94 {
95    uint16_t gr;
96    VRKey vr;
97    
98    int groupLength = 0;
99    bool found0002 = false;   
100   
101    // for each Tag in the DicomDirMeta
102    DocEntry *entry = GetFirstEntry();
103    while( entry )
104    {
105       gr = entry->GetGroup();
106
107       if ( gr == 0x0002 )
108       {
109          found0002 = true;
110
111          if ( entry->GetElement() != 0x0000 )
112          {
113             vr = entry->GetVR();
114
115             if ( vr == "OB" ) 
116             {
117                groupLength +=  4;
118             }
119             groupLength += 2 + 2 + 4 + entry->GetLength();   
120          }
121       }
122       else if (found0002 )
123          break;
124
125       entry = GetNextEntry();
126    }
127    return groupLength; 
128 }
129
130 //-----------------------------------------------------------------------------
131 // Protected
132
133 //-----------------------------------------------------------------------------
134 // Private
135
136 //-----------------------------------------------------------------------------
137 // Print
138 /**
139  * \brief   Prints the Meta Elements
140  * @param os ostream to write to 
141  * @param indent Indentation string to be prepended during printing
142  */ 
143 void DicomDirMeta::Print(std::ostream &os, std::string const & )
144 {
145    os << "META" << std::endl;
146    // warning : META doesn't behave exactly like a Objet 
147    for (ListDocEntry::iterator i = DocEntries.begin();
148         i != DocEntries.end();
149         ++i)
150    {
151       (*i)->SetPrintLevel(PrintLevel);
152       (*i)->Print();
153       os << std::endl;
154    }
155 }
156
157 //-----------------------------------------------------------------------------
158 } // end namespace gdcm