]> Creatis software - gdcm.git/blob - src/gdcmTS.cxx
ENH: Minor cleanup. Moved the cleanup of the transfer syntax directly within the...
[gdcm.git] / src / gdcmTS.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmTS.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/11 23:16:47 $
7   Version:   $Revision: 1.36 $
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 "gdcmTS.h"
20 #include "gdcmDebug.h"
21 #include "gdcmUtil.h"
22 #include "gdcmDictSet.h"
23
24 #include <fstream>
25 #include <string>
26 #include <iostream>
27
28 // TODO
29 // a lot of troubles expected with TS : 1.2.840.113619.5.2
30 // Implicit VR - Big Endian
31 // see : http://www.gemedicalsystemseurope.com/euen/it_solutions/pdf/lsqxi_rev2.pdf
32 // 
33
34 namespace gdcm 
35 {
36 //-----------------------------------------------------------------------------
37 static const char *SpecialStrings[] =  {
38   // Implicit VR Little Endian
39   "1.2.840.10008.1.2",
40   // Implicit VR Big Endian DLX (G.E Private)
41   "1.2.840.113619.5.2",
42   // Explicit VR Little Endian
43   "1.2.840.10008.1.2.1",
44   // Deflated Explicit VR Little Endian
45   "1.2.840.10008.1.2.1.99",
46   // Explicit VR Big Endian
47   "1.2.840.10008.1.2.2",
48   // JPEG Baseline (Process 1)
49   "1.2.840.10008.1.2.4.50",
50   // JPEG Extended (Process 2 & 4)
51   "1.2.840.10008.1.2.4.51",
52   // JPEG Extended (Process 3 & 5)
53   "1.2.840.10008.1.2.4.52",
54   // JPEG Spectral Selection, Non-Hierarchical (Process 6 & 8)
55   "1.2.840.10008.1.2.4.53",
56   // JPEG Full Progression, Non-Hierarchical (Process 10 & 12)
57   "1.2.840.10008.1.2.4.55",
58   // JPEG Lossless, Non-Hierarchical (Process 14)
59   "1.2.840.10008.1.2.4.57",
60   // JPEG Lossless, Hierarchical, First-Order Prediction (Process 14, [Selection Value 1])
61   "1.2.840.10008.1.2.4.70",
62   // JPEG 2000 Lossless
63   "1.2.840.10008.1.2.4.90",
64   // JPEG 2000
65   "1.2.840.10008.1.2.4.91",
66   // RLE Lossless
67   "1.2.840.10008.1.2.5",
68   // Unknown
69   "Unknown Transfer Syntax"
70 };
71
72 void FillDefaultTSDict(TSHT &ts);
73 //-----------------------------------------------------------------------------
74 // Constructor / Destructor
75 TS::TS() 
76 {
77    std::string filename = DictSet::BuildDictPath() + DICT_TS;
78    std::ifstream from(filename.c_str());
79    if( !from )
80    {
81       gdcmVerboseMacro("Can't open dictionary" << filename.c_str());
82       FillDefaultTSDict( TsMap );
83    }
84    else
85    {
86       TSKey key;
87       TSAtr name;
88
89       while (!from.eof())
90       {
91          from >> key;
92          from >> std::ws;
93          std::getline(from, name);
94
95          if(key != "")
96          {
97             TsMap[key] = name;
98          }
99       }
100       from.close();
101    }
102 }
103
104 //-----------------------------------------------------------------------------
105 TS::~TS() 
106 {
107    TsMap.clear();
108 }
109
110 //-----------------------------------------------------------------------------
111 // Print
112 /**
113  * \ingroup VR
114  * \brief   Print all 
115  * @param   os The output stream to be written to.
116  */
117 void TS::Print(std::ostream &os) 
118 {
119    std::ostringstream s;
120
121    for (TSHT::const_iterator it = TsMap.begin(); it != TsMap.end(); ++it)
122    {
123       s << "TS : " << it->first << " = " << it->second << std::endl;
124    }
125    os << s.str();
126 }
127
128 //-----------------------------------------------------------------------------
129 // Public
130 int TS::Count(TSKey const &key) 
131 {
132    return TsMap.count(key);
133 }
134
135 TSAtr const & TS::GetValue(TSKey const &key) 
136 {
137    // First thing clean up the string sometime the transfer syntax is padded with spaces
138    std::string copy = key;
139    while ( copy.size() && !isdigit((unsigned char)copy[copy.size()-1]) )
140    {
141       copy.erase(copy.size()-1, 1);
142    }
143
144    TSHT::const_iterator it = TsMap.find(copy);
145    if (it == TsMap.end())
146    {
147       return GDCM_UNFOUND;
148    }
149    return it->second;
150 }
151
152 bool TS::IsTransferSyntax(TSKey const &key)
153 {
154    TSHT::const_iterator it = TsMap.find(key);
155    return it != TsMap.end();
156 }
157
158 bool TS::IsRLELossless(TSKey const &key)
159 {
160    bool r = false;
161    // First check this is an actual transfer syntax
162    if( IsTransferSyntax(key) )
163    {
164       if ( key == SpecialStrings[RLELossless] )
165       {
166          r = true;
167       }
168    }
169    return r;
170 }
171
172 bool TS::IsJPEGLossless(TSKey const &key)
173 {
174    bool r = false;
175    // First check this is an actual transfer syntax
176    if( IsTransferSyntax(key) )
177    {
178       if ( key == SpecialStrings[JPEGFullProgressionProcess10_12]
179         || key == SpecialStrings[JPEGLosslessProcess14]
180         || key == SpecialStrings[JPEGLosslessProcess14_1] )
181       {
182          r = true;
183       }
184    }
185    return r;
186 }
187
188 /**
189  * \brief   Determines if the Transfer Syntax was already encountered
190  *          and if it corresponds to a JPEG2000 one
191  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
192  *          other cases.
193  */
194 bool TS::IsJPEG2000(TSKey const &key)
195 {
196    bool r = false;
197    // First check this is an actual transfer syntax
198    if( IsTransferSyntax(key) )
199    {
200       if ( key == SpecialStrings[JPEG2000Lossless]
201         || key == SpecialStrings[JPEG2000] )
202       {
203          r = true;
204       }
205    }
206    return r;
207 }
208
209 /**
210  * \brief   Determines if the Transfer Syntax corresponds to any form
211  *          of Jpeg encoded Pixel data.
212  * @return  True when any form of JPEG found. False otherwise.
213  */
214 bool TS::IsJPEG(TSKey const &key)
215 {
216    bool r = false;
217    // First check this is an actual transfer syntax
218    if( IsTransferSyntax(key) )
219    {
220       if ( key == SpecialStrings[JPEGBaselineProcess1]
221         || key == SpecialStrings[JPEGExtendedProcess2_4]
222         || key == SpecialStrings[JPEGExtendedProcess3_5]
223         || key == SpecialStrings[JPEGSpectralSelectionProcess6_8]
224         || IsJPEGLossless( key ) 
225         || IsJPEG2000( key ) )
226       {
227          r = true;
228       }
229    }
230    return r;
231 }
232
233 /**
234  * \brief   Determines if the Transfer Syntax corresponds to encapsulated
235  *          of encoded Pixel Data (as opposed to native).
236  * @return  True when encapsulated. False when native.
237  */
238 bool TS::IsEncapsulate(TSKey const &key)
239 {
240    bool r = false;
241    // First check this is an actual transfer syntax
242    if( IsTransferSyntax(key) )
243    {
244       if ( key == SpecialStrings[RLELossless]
245         || IsJPEG(key) )
246       {
247          r = true;
248       }
249    }
250    return r;
251 }
252
253 TS::SpecialType TS::GetSpecialTransferSyntax(TSKey const &key)
254 {
255    for (int i = 0; SpecialStrings[i] != NULL; i++)
256    {
257       if ( SpecialStrings[i] == key )
258       {
259          return SpecialType(i);
260       }
261    }
262
263    return UnknownTS;
264 }
265
266 const char* TS::GetSpecialTransferSyntax(SpecialType t)
267 {
268    return SpecialStrings[t];
269 }
270
271 //-----------------------------------------------------------------------------
272 // Protected
273
274 //-----------------------------------------------------------------------------
275 // Private
276
277 //-----------------------------------------------------------------------------
278
279 } // end namespace gdcm