]> Creatis software - gdcm.git/blob - src/gdcmTS.cxx
Some normalizations :
[gdcm.git] / src / gdcmTS.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmTS.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/23 10:12:34 $
7   Version:   $Revision: 1.38 $
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-LS Lossless Image Compression
63   "1.2.840.10008.1.2.4.80",
64   // JPEG-LS Lossy (Near-Lossless) Image Compression
65   "1.2.840.10008.1.2.4.81",
66   // JPEG 2000 Lossless
67   "1.2.840.10008.1.2.4.90",
68   // JPEG 2000
69   "1.2.840.10008.1.2.4.91",
70   // RLE Lossless
71   "1.2.840.10008.1.2.5",
72   // Unknown
73   "Unknown Transfer Syntax"
74 };
75
76 void FillDefaultTSDict(TSHT &ts);
77 //-----------------------------------------------------------------------------
78 // Constructor / Destructor
79 TS::TS() 
80 {
81    std::string filename = DictSet::BuildDictPath() + DICT_TS;
82    std::ifstream from(filename.c_str());
83    if( !from )
84    {
85       gdcmVerboseMacro("Can't open dictionary" << filename.c_str());
86       FillDefaultTSDict( TsMap );
87    }
88    else
89    {
90       TSKey key;
91       TSAtr name;
92
93       while (!from.eof())
94       {
95          from >> key;
96          from >> std::ws;
97          std::getline(from, name);
98
99          if(key != "")
100          {
101             TsMap[key] = name;
102          }
103       }
104       from.close();
105    }
106 }
107
108 //-----------------------------------------------------------------------------
109 TS::~TS() 
110 {
111    TsMap.clear();
112 }
113
114 //-----------------------------------------------------------------------------
115 // Print
116 /**
117  * \brief   Print all 
118  * @param   os The output stream to be written to.
119  */
120 void TS::Print(std::ostream &os) 
121 {
122    std::ostringstream s;
123
124    for (TSHT::const_iterator it = TsMap.begin(); it != TsMap.end(); ++it)
125    {
126       s << "TS : " << it->first << " = " << it->second << std::endl;
127    }
128    os << s.str();
129 }
130
131 //-----------------------------------------------------------------------------
132 // Public
133 int TS::Count(TSKey const &key) 
134 {
135    return TsMap.count(key);
136 }
137
138 TSAtr const & TS::GetValue(TSKey const &key) 
139 {
140    // First thing clean up the string sometime the transfer syntax is padded with spaces
141    std::string copy = key;
142    while ( copy.size() && !isdigit((unsigned char)copy[copy.size()-1]) )
143    {
144       copy.erase(copy.size()-1, 1);
145    }
146
147    TSHT::const_iterator it = TsMap.find(copy);
148    if (it == TsMap.end())
149    {
150       return GDCM_UNFOUND;
151    }
152    return it->second;
153 }
154 /**
155  * \brief   Determines if the key passed corresponds to a 'Transfer Syntax'
156  *          as defined in DICOM (and stored in gdcm::TS class)
157  * @return  True when key is an actual 'Transfer Syntax'. False in all
158  *          other cases.
159  */
160 bool TS::IsTransferSyntax(TSKey const &key)
161 {
162    TSHT::const_iterator it = TsMap.find(key);
163    return it != TsMap.end();
164 }
165
166 /**
167  * \brief   Determines if the Transfer Syntax was already encountered
168  *          and if it corresponds to a Run Length Encoding Lossless one
169  * @return  True when Run Length Encoding Lossless found. False in all
170  *          other cases.
171  */
172 bool TS::IsRLELossless(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[RLELossless] )
179       {
180          r = true;
181       }
182    }
183    return r;
184 }
185
186 /**
187  * \brief   Determines if the Transfer Syntax was already encountered
188  *          and if it corresponds to a 'classical' JPEG Lossless one
189  * @return  True when 'classical' Lossless found. False in all
190  *          other cases.
191  */
192 bool TS::IsJPEGLossless(TSKey const &key)
193 {
194    bool r = false;
195    // First check this is an actual transfer syntax
196    if( IsTransferSyntax(key) )
197    {
198       if ( key == SpecialStrings[JPEGFullProgressionProcess10_12]
199         || key == SpecialStrings[JPEGLosslessProcess14]
200         || key == SpecialStrings[JPEGLosslessProcess14_1] )
201       {
202          r = true;
203       }
204    }
205    return r;
206 }
207
208 /**
209  * \brief   Determines if the Transfer Syntax was already encountered
210  *          and if it corresponds to a 'classical' JPEG Lossy one
211  * @return  True when 'classical' Lossy found. False in all
212  *          other cases.
213  */
214 bool TS::IsJPEGLossy(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       {
225          r = true;
226       }
227    }
228    return r;
229 }
230 /**
231  * \brief   Determines if the Transfer Syntax was already encountered
232  *          and if it corresponds to a JPEG2000 one
233  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
234  *          other cases.
235  */
236 bool TS::IsJPEG2000(TSKey const &key)
237 {
238    bool r = false;
239    // First check this is an actual transfer syntax
240    if( IsTransferSyntax(key) )
241    {
242       if ( key == SpecialStrings[JPEG2000Lossless]
243         || key == SpecialStrings[JPEG2000] )
244       {
245          r = true;
246       }
247    }
248    return r;
249 }
250
251 /**
252  * \brief   Determines if the Transfer Syntax corresponds to 
253  *          'classical' Jpeg Lossless or Jpeg lossy.
254  * @return  True when any form of JPEG found. False otherwise.
255  */
256 bool TS::IsJPEG(TSKey const &key)
257 {
258    bool r = false;
259    // First check this is an actual transfer syntax
260    if( IsTransferSyntax(key) )
261    {
262       if ( IsJPEGLossy( key )
263         || IsJPEGLossless( key )
264          )
265       {
266          r = true;
267       }
268    }
269    return r;
270 }
271
272 /**
273  * \brief   Determines if the Transfer Syntax corresponds to any form
274  *          of Jpeg-LS encoded Pixel data.
275  * @return  True when any form of JPEG-LS found. False otherwise.
276  */
277 bool TS::IsJPEGLS(TSKey const &key)
278 {
279    bool r = false;
280    // First check this is an actual transfer syntax
281    if( IsTransferSyntax(key) )
282    {
283       if ( key == SpecialStrings[JPEGLSLossless]
284         || key == SpecialStrings[JPEGLSNearLossless] ) 
285       {
286          r = true;
287       }
288    }
289    return r;
290 }
291
292 TS::SpecialType TS::GetSpecialTransferSyntax(TSKey const &key)
293 {
294    for (int i = 0; SpecialStrings[i] != NULL; i++)
295    {
296       if ( SpecialStrings[i] == key )
297       {
298          return SpecialType(i);
299       }
300    }
301
302    return UnknownTS;
303 }
304
305 const char* TS::GetSpecialTransferSyntax(SpecialType t)
306 {
307    return SpecialStrings[t];
308 }
309
310 //-----------------------------------------------------------------------------
311 // Protected
312
313 //-----------------------------------------------------------------------------
314 // Private
315
316 //-----------------------------------------------------------------------------
317
318 } // end namespace gdcm