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