]> Creatis software - gdcm.git/blob - src/gdcmTS.cxx
BUG: Apparently on Borland uses unsigned char to store boolean, all other plateformns...
[gdcm.git] / src / gdcmTS.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmTS.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/05 01:37:09 $
7   Version:   $Revision: 1.42 $
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       gdcmWarningMacro("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 /**
216  * \brief   Determines if the Transfer Syntax was already encountered
217  *          and if it corresponds to a JPEG2000 one
218  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
219  *          other cases.
220  */
221 bool TS::IsJPEG2000(TSKey const &key)
222 {
223    bool r = false;
224    // First check this is an actual transfer syntax
225    if( IsTransferSyntax(key) )
226    {
227       if ( key == SpecialStrings[JPEG2000Lossless]
228         || key == SpecialStrings[JPEG2000] )
229       {
230          r = true;
231       }
232    }
233    return r;
234 }
235
236 /**
237  * \brief   Determines if the Transfer Syntax corresponds to 
238  *          'classical' Jpeg Lossless or Jpeg lossy.
239  * @return  True when any form of JPEG found. False otherwise.
240  */
241 bool TS::IsJPEG(TSKey const &key)
242 {
243    bool r = false;
244    // First check this is an actual transfer syntax
245    if( IsTransferSyntax(key) )
246    {
247       if ( IsJPEGLossy( key )
248         || IsJPEGLossless( key )
249          )
250       {
251          r = true;
252       }
253    }
254    return r;
255 }
256
257 /**
258  * \brief   Determines if the Transfer Syntax corresponds to any form
259  *          of Jpeg-LS encoded Pixel data.
260  * @return  True when any form of JPEG-LS found. False otherwise.
261  */
262 bool TS::IsJPEGLS(TSKey const &key)
263 {
264    bool r = false;
265    // First check this is an actual transfer syntax
266    if( IsTransferSyntax(key) )
267    {
268       if ( key == SpecialStrings[JPEGLSLossless]
269         || key == SpecialStrings[JPEGLSNearLossless] ) 
270       {
271          r = true;
272       }
273    }
274    return r;
275 }
276
277 TS::SpecialType TS::GetSpecialTransferSyntax(TSKey const &key)
278 {
279    for (int i = 0; SpecialStrings[i] != NULL; i++)
280    {
281       if ( SpecialStrings[i] == key )
282       {
283          return SpecialType(i);
284       }
285    }
286
287    return UnknownTS;
288 }
289
290 const char* TS::GetSpecialTransferSyntax(SpecialType t)
291 {
292    return SpecialStrings[t];
293 }
294
295 //-----------------------------------------------------------------------------
296 // Protected
297
298 //-----------------------------------------------------------------------------
299 // Private
300
301 //-----------------------------------------------------------------------------
302 // Print
303 /**
304  * \brief   Print all 
305  * @param   os The output stream to be written to.
306  */
307 void TS::Print(std::ostream &os) 
308 {
309    std::ostringstream s;
310
311    for (TSHT::const_iterator it = TsMap.begin(); it != TsMap.end(); ++it)
312    {
313       s << "TS : " << it->first << " = " << it->second << std::endl;
314    }
315    os << s.str();
316 }
317
318 //-----------------------------------------------------------------------------
319 } // end namespace gdcm