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