]> Creatis software - gdcm.git/blob - src/gdcmDicomDirMeta.cxx
Fix mistypings
[gdcm.git] / src / gdcmDicomDirMeta.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDicomDirMeta.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/10/08 15:20:17 $
7   Version:   $Revision: 1.40 $
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
26 namespace GDCM_NAME_SPACE 
27 {
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /**
31  * \brief  Constructor
32  */ 
33 DicomDirMeta::DicomDirMeta(bool empty):
34    DicomDirObject()
35 {
36    if ( !empty )
37    {
38       ListDicomDirStudyElem const &elemList = 
39          Global::GetDicomDirElements()->GetDicomDirMetaElements();
40       FillObject(elemList);
41    }
42 }
43
44 /**
45  * \brief   Canonical destructor.
46  */
47 DicomDirMeta::~DicomDirMeta() 
48 {
49 }
50
51 //-----------------------------------------------------------------------------
52 // Public
53 /**
54  * \brief   Writes the Meta Elements
55  * @param fp ofstream to write to
56  * @param filetype type of the file (ImplicitVR, ExplicitVR, JPEG, JPEG2000 ...)
57  * @return
58  */ 
59 void DicomDirMeta::WriteContent(std::ofstream *fp, FileType filetype,
60                                  bool , bool )
61 {
62    // 'File Meta Information Version'
63    
64    uint8_t fmiv[2] = {0x02,0x00};
65    // FIXME : the following doesn't make the job (?!?)      
66    //SetEntryBinArea(fmiv, 0x0002,0x0001, 2); 
67    DataEntry *e00002_0001 = GetDataEntry(0x0002,0x0001);
68    e00002_0001->CopyBinArea(fmiv, 2);
69          
70    // 'Media Storage SOP Instance UID'   
71    DataEntry *e00002_0003 = GetDataEntry(0x0002,0x0003);
72    e00002_0003->SetString(Util::CreateUniqueUID());
73
74    // 'Implementation Class UID'
75    DataEntry *e00002_0012 = GetDataEntry(0x0002,0x0012);
76    e00002_0012->SetString(Util::CreateUniqueUID());   
77    
78    // Entry : 0002|0000 = group length -> recalculated
79    DataEntry *e0000 = GetDataEntry(0x0002,0x0000);
80    std::ostringstream sLen;
81    sLen << ComputeGroup0002Length( );
82    e0000->SetString(sLen.str());
83    
84    for (ListDocEntry::iterator i = DocEntries.begin();  
85                                i!= DocEntries.end();
86                              ++i)
87    {   
88    // true : we are in MetaElements
89       (*i)->WriteContent(fp, filetype, true, false);
90    }
91 }
92
93 /**
94  * \brief Re-computes the length of the Dicom group 0002 (in the DicomDirMeta)
95  */
96 int DicomDirMeta::ComputeGroup0002Length( ) 
97 {
98    uint16_t gr;
99    VRKey vr;
100    
101    int groupLength = 0;
102    bool found0002 = false;   
103   
104    // for each Tag in the DicomDirMeta
105    DocEntry *entry = GetFirstEntry();
106    while( entry )
107    {
108       gr = entry->GetGroup();
109
110       if ( gr == 0x0002 )
111       {
112          found0002 = true;
113
114          if ( entry->GetElement() != 0x0000 )
115          {
116             vr = entry->GetVR();
117
118             if ( vr == "OB" ) 
119             {
120                groupLength +=  4;
121             }
122             groupLength += 2 + 2 + 4 + entry->GetLength();   
123          }
124       }
125       else if (found0002 )
126          break;
127
128       entry = GetNextEntry();
129    }
130    return groupLength; 
131 }
132
133 //-----------------------------------------------------------------------------
134 // Protected
135
136 //-----------------------------------------------------------------------------
137 // Private
138
139 //-----------------------------------------------------------------------------
140 // Print
141 /**
142  * \brief   Prints the Meta Elements
143  * @param os ostream to write to 
144  * @param indent Indentation string to be prepended during printing
145  */ 
146 void DicomDirMeta::Print(std::ostream &os, std::string const & )
147 {
148    os << "META" << std::endl;
149    // warning : META doesn't behave exactly like a Objet 
150    for (ListDocEntry::iterator i = DocEntries.begin();
151         i != DocEntries.end();
152         ++i)
153    {
154       (*i)->SetPrintLevel(PrintLevel);
155       (*i)->Print();
156       os << std::endl;
157    }
158 }
159
160 //-----------------------------------------------------------------------------
161 } // end namespace gdcm