]> Creatis software - gdcm.git/blob - src/gdcmTS.cxx
Summer nights are really too hot to sleep.
[gdcm.git] / src / gdcmTS.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmTS.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/24 10:55:59 $
7   Version:   $Revision: 1.48 $
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 // http://www.gemedicalsystemseurope.com/euen/it_solutions/pdf/lsqxi_rev2.pdf
32 // 
33
34 namespace gdcm 
35 {
36 //-----------------------------------------------------------------------------
37 /// \brief Transfer Syntaxes gdcm deals with (internal use only)
38 static const char *SpecialStrings[] =  {
39   // Implicit VR Little Endian
40   "1.2.840.10008.1.2",
41   // Implicit VR Big Endian DLX (G.E Private)
42   "1.2.840.113619.5.2",
43   // Explicit VR Little Endian
44   "1.2.840.10008.1.2.1",
45   // Deflated Explicit VR Little Endian
46   "1.2.840.10008.1.2.1.99",
47   // Explicit VR Big Endian
48   "1.2.840.10008.1.2.2",
49   // JPEG Baseline (Process 1)
50   "1.2.840.10008.1.2.4.50",
51   // JPEG Extended (Process 2 & 4)
52   "1.2.840.10008.1.2.4.51",
53   // JPEG Extended (Process 3 & 5)
54   "1.2.840.10008.1.2.4.52",
55   // JPEG Spectral Selection, Non-Hierarchical (Process 6 & 8)
56   "1.2.840.10008.1.2.4.53",
57   // JPEG Full Progression, Non-Hierarchical (Process 10 & 12)
58   "1.2.840.10008.1.2.4.55",
59   // JPEG Lossless, Non-Hierarchical (Process 14)
60   "1.2.840.10008.1.2.4.57",
61   // JPEG Lossless, Hierarchical, First-Order Prediction (Process 14,
62   //                                                       [Selection Value 1])
63   "1.2.840.10008.1.2.4.70",
64   // JPEG-LS Lossless Image Compression
65   "1.2.840.10008.1.2.4.80",
66   // JPEG-LS Lossy (Near-Lossless) Image Compression
67   "1.2.840.10008.1.2.4.81",
68   // JPEG 2000 Lossless
69   "1.2.840.10008.1.2.4.90",
70   // JPEG 2000
71   "1.2.840.10008.1.2.4.91",
72   // RLE Lossless
73   "1.2.840.10008.1.2.5",
74   // MPEG2 Main Profile @ Main Level
75   "1.2.840.10008.1.2.4.100",
76   // Unknown
77   "Unknown Transfer Syntax"
78 };
79
80 //-----------------------------------------------------------------------------
81 /// \brief auto generated function, to fill up the Dicom Dictionnary,
82 ///       if relevant file is not found on user's disk
83 void FillDefaultTSDict(TSHT &ts);
84
85 //-----------------------------------------------------------------------------
86 // Constructor / Destructor
87 TS::TS() 
88 {
89    std::string filename = DictSet::BuildDictPath() + DICT_TS;
90    std::ifstream from(filename.c_str());
91    if ( !from )
92    {
93       gdcmWarningMacro("Can't open dictionary" << filename.c_str());
94       FillDefaultTSDict( TsMap );
95    }
96    else
97    {
98       TSKey key;
99       TSAtr name;
100
101       while (!from.eof())
102       {
103          from >> key;
104          from >> std::ws;
105          std::getline(from, name);
106
107          if (key != "")
108          {
109             TsMap[key] = name;
110          }
111       }
112       from.close();
113    }
114 }
115
116 TS::~TS() 
117 {
118    TsMap.clear();
119 }
120
121 //-----------------------------------------------------------------------------
122 // Public
123
124 /// \brief returns occurence number of the given key
125 int TS::Count(TSKey const &key) 
126 {
127    return TsMap.count(key);
128 }
129
130 /// \brief returns the human reabable value of a Transfer Synatx string 
131 TSAtr const &TS::GetValue(TSKey const &key) 
132 {
133    // First thing clean up the string 
134    // (sometime the transfer syntax is padded with spaces)
135    std::string copy = key;
136    while ( copy.size() && !isdigit((unsigned char)copy[copy.size()-1]) )
137    {
138       copy.erase(copy.size()-1, 1);
139    }
140
141    TSHT::const_iterator it = TsMap.find(copy);
142    if (it == TsMap.end())
143    {
144       return GDCM_UNFOUND;
145    }
146    return it->second;
147 }
148 /**
149  * \brief   Determines if the key passed corresponds to a 'Transfer Syntax'
150  *          as defined in DICOM (and stored in gdcm::TS class)
151  * @return  True when key is an actual 'Transfer Syntax'. False in all
152  *          other cases.
153  */
154 bool TS::IsTransferSyntax(TSKey const &key)
155 {
156    TSHT::const_iterator it = TsMap.find(key);
157    return it != TsMap.end();
158 }
159
160 /**
161  * \brief   Determines if the Transfer Syntax was already encountered
162  *          and if it corresponds to a Run Length Encoding Lossless one
163  * @return  True when Run Length Encoding Lossless found. False in all
164  *          other cases.
165  */
166 bool TS::IsRLELossless(TSKey const &key)
167 {
168    bool r = false;
169    // First check this is an actual transfer syntax
170    if ( IsTransferSyntax(key) )
171    {
172       if ( key == SpecialStrings[RLELossless] )
173       {
174          r = true;
175       }
176    }
177    return r;
178 }
179
180 /**
181  * \brief   Determines if the Transfer Syntax was already encountered
182  *          and if it corresponds to a 'classical' JPEG Lossless one
183  * @return  True when 'classical' Lossless found. False in all
184  *          other cases.
185  */
186 bool TS::IsJPEGLossless(TSKey const &key)
187 {
188    bool r = false;
189    // First check this is an actual transfer syntax
190    if ( IsTransferSyntax(key) )
191    {
192       if ( key == SpecialStrings[JPEGFullProgressionProcess10_12]
193         || key == SpecialStrings[JPEGLosslessProcess14]
194         || key == SpecialStrings[JPEGLosslessProcess14_1] )
195       {
196          r = true;
197       }
198    }
199    return r;
200 }
201
202 /**
203  * \brief   Determines if the Transfer Syntax was already encountered
204  *          and if it corresponds to a 'classical' JPEG Lossy one
205  * @return  True when 'classical' Lossy found. False in all
206  *          other cases.
207  */
208 bool TS::IsJPEGLossy(TSKey const &key)
209 {
210    bool r = false;
211    // First check this is an actual transfer syntax
212    if ( IsTransferSyntax(key) )
213    {
214       if ( key == SpecialStrings[JPEGBaselineProcess1]
215         || key == SpecialStrings[JPEGExtendedProcess2_4]
216         || key == SpecialStrings[JPEGExtendedProcess3_5]
217         || key == SpecialStrings[JPEGSpectralSelectionProcess6_8] )
218       {
219          r = true;
220       }
221    }
222    return r;
223 }
224
225 /**
226  * \brief   Determines if the Transfer Syntax was already encountered
227  *          and if it corresponds to a JPEG2000 one
228  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
229  *          other cases.
230  */
231 bool TS::IsJPEG2000(TSKey const &key)
232 {
233    bool r = false;
234    // First check this is an actual transfer syntax
235    if ( IsTransferSyntax(key) )
236    {
237       if ( key == SpecialStrings[JPEG2000Lossless]
238         || key == SpecialStrings[JPEG2000] )
239       {
240          r = true;
241       }
242    }
243    return r;
244 }
245
246 /**
247  * \brief   Determines if the Transfer Syntax corresponds to 
248  *          'classical' Jpeg Lossless or Jpeg lossy.
249  * @return  True when any form of JPEG found. False otherwise.
250  */
251 bool TS::IsJPEG(TSKey const &key)
252 {
253    bool r = false;
254    // First check this is an actual transfer syntax
255    if ( IsTransferSyntax(key) )
256    {
257       if ( IsJPEGLossy( key )
258         || IsJPEGLossless( key )
259         || IsJPEG2000( key )
260         || IsJPEGLS( key )
261          )
262       {
263          r = true;
264       }
265    }
266    return r;
267 }
268
269 /**
270  * \brief   Determines if the Transfer Syntax corresponds to any form
271  *          of Jpeg-LS encoded Pixel data.
272  * @return  True when any form of JPEG-LS found. False otherwise.
273  */
274 bool TS::IsJPEGLS(TSKey const &key)
275 {
276    bool r = false;
277    // First check this is an actual transfer syntax
278    if ( IsTransferSyntax(key) )
279    {
280       if ( key == SpecialStrings[JPEGLSLossless]
281         || key == SpecialStrings[JPEGLSNearLossless] ) 
282       {
283          r = true;
284       }
285    }
286    return r;
287 }
288
289 /**
290  * \brief   Determines if the Transfer Syntax corresponds to any form
291  *          of MPEG encoded Pixel data.
292  * @return  True when any form of MPEG found. False otherwise.
293  */
294 bool TS::IsMPEG(TSKey const &key)
295 {
296    bool r = false;
297    // First check this is an actual transfer syntax
298    if ( IsTransferSyntax(key) )
299    {
300       if ( key == SpecialStrings[MPEG2MainProfile] ) 
301       {
302          r = true;
303       }
304    }
305    return r;
306 }
307
308 /**
309  * \brief   GetSpecialTransferSyntax ??
310  * @param  key TSKey const &key ??
311  * @return  TS::SpecialType ??.
312  */
313 TS::SpecialType TS::GetSpecialTransferSyntax(TSKey const &key)
314 {
315    for (int i = 0; SpecialStrings[i] != NULL; i++)
316    {
317       if ( SpecialStrings[i] == key )
318       {
319          return SpecialType(i);
320       }
321    }
322    return UnknownTS;
323 }
324
325 /**
326  * \brief   GetSpecialTransferSyntax ??
327  * @param  t SpecialType t ??
328  * @return  char* TS : SpecialStrings[t] ??.
329  */
330 const char* TS::GetSpecialTransferSyntax(SpecialType t)
331 {
332    return SpecialStrings[t];
333 }
334
335 //-----------------------------------------------------------------------------
336 // Protected
337
338 //-----------------------------------------------------------------------------
339 // Private
340
341 //-----------------------------------------------------------------------------
342 // Print
343 /**
344  * \brief   Print all 
345  * @param   os The output stream to be written to.
346  */
347 void TS::Print(std::ostream &os) 
348 {
349    std::ostringstream s;
350
351    for (TSHT::const_iterator it = TsMap.begin(); it != TsMap.end(); ++it)
352    {
353       s << "TS : " << it->first << " = " << it->second << std::endl;
354    }
355    os << s.str();
356 }
357
358 //-----------------------------------------------------------------------------
359 } // end namespace gdcm