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