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