]> Creatis software - gdcm.git/blob - src/gdcmTS.cxx
2005-01-18 Jean-Pierre Roux <jpr@creatis.univ-lyon1.fr>
[gdcm.git] / src / gdcmTS.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmTS.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 08:01:42 $
7   Version:   $Revision: 1.37 $
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 2000 Lossless
63   "1.2.840.10008.1.2.4.90",
64   // JPEG 2000
65   "1.2.840.10008.1.2.4.91",
66   // RLE Lossless
67   "1.2.840.10008.1.2.5",
68   // Unknown
69   "Unknown Transfer Syntax"
70 };
71
72 void FillDefaultTSDict(TSHT &ts);
73 //-----------------------------------------------------------------------------
74 // Constructor / Destructor
75 TS::TS() 
76 {
77    std::string filename = DictSet::BuildDictPath() + DICT_TS;
78    std::ifstream from(filename.c_str());
79    if( !from )
80    {
81       gdcmVerboseMacro("Can't open dictionary" << filename.c_str());
82       FillDefaultTSDict( TsMap );
83    }
84    else
85    {
86       TSKey key;
87       TSAtr name;
88
89       while (!from.eof())
90       {
91          from >> key;
92          from >> std::ws;
93          std::getline(from, name);
94
95          if(key != "")
96          {
97             TsMap[key] = name;
98          }
99       }
100       from.close();
101    }
102 }
103
104 //-----------------------------------------------------------------------------
105 TS::~TS() 
106 {
107    TsMap.clear();
108 }
109
110 //-----------------------------------------------------------------------------
111 // Print
112 /**
113  * \brief   Print all 
114  * @param   os The output stream to be written to.
115  */
116 void TS::Print(std::ostream &os) 
117 {
118    std::ostringstream s;
119
120    for (TSHT::const_iterator it = TsMap.begin(); it != TsMap.end(); ++it)
121    {
122       s << "TS : " << it->first << " = " << it->second << std::endl;
123    }
124    os << s.str();
125 }
126
127 //-----------------------------------------------------------------------------
128 // Public
129 int TS::Count(TSKey const &key) 
130 {
131    return TsMap.count(key);
132 }
133
134 TSAtr const & TS::GetValue(TSKey const &key) 
135 {
136    // First thing clean up the string sometime the transfer syntax is padded with spaces
137    std::string copy = key;
138    while ( copy.size() && !isdigit((unsigned char)copy[copy.size()-1]) )
139    {
140       copy.erase(copy.size()-1, 1);
141    }
142
143    TSHT::const_iterator it = TsMap.find(copy);
144    if (it == TsMap.end())
145    {
146       return GDCM_UNFOUND;
147    }
148    return it->second;
149 }
150
151 bool TS::IsTransferSyntax(TSKey const &key)
152 {
153    TSHT::const_iterator it = TsMap.find(key);
154    return it != TsMap.end();
155 }
156
157 bool TS::IsRLELossless(TSKey const &key)
158 {
159    bool r = false;
160    // First check this is an actual transfer syntax
161    if( IsTransferSyntax(key) )
162    {
163       if ( key == SpecialStrings[RLELossless] )
164       {
165          r = true;
166       }
167    }
168    return r;
169 }
170
171 bool TS::IsJPEGLossless(TSKey const &key)
172 {
173    bool r = false;
174    // First check this is an actual transfer syntax
175    if( IsTransferSyntax(key) )
176    {
177       if ( key == SpecialStrings[JPEGFullProgressionProcess10_12]
178         || key == SpecialStrings[JPEGLosslessProcess14]
179         || key == SpecialStrings[JPEGLosslessProcess14_1] )
180       {
181          r = true;
182       }
183    }
184    return r;
185 }
186
187 /**
188  * \brief   Determines if the Transfer Syntax was already encountered
189  *          and if it corresponds to a JPEG2000 one
190  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
191  *          other cases.
192  */
193 bool TS::IsJPEG2000(TSKey const &key)
194 {
195    bool r = false;
196    // First check this is an actual transfer syntax
197    if( IsTransferSyntax(key) )
198    {
199       if ( key == SpecialStrings[JPEG2000Lossless]
200         || key == SpecialStrings[JPEG2000] )
201       {
202          r = true;
203       }
204    }
205    return r;
206 }
207
208 /**
209  * \brief   Determines if the Transfer Syntax corresponds to any form
210  *          of Jpeg encoded Pixel data.
211  * @return  True when any form of JPEG found. False otherwise.
212  */
213 bool TS::IsJPEG(TSKey const &key)
214 {
215    bool r = false;
216    // First check this is an actual transfer syntax
217    if( IsTransferSyntax(key) )
218    {
219       if ( key == SpecialStrings[JPEGBaselineProcess1]
220         || key == SpecialStrings[JPEGExtendedProcess2_4]
221         || key == SpecialStrings[JPEGExtendedProcess3_5]
222         || key == SpecialStrings[JPEGSpectralSelectionProcess6_8]
223         || IsJPEGLossless( key ) 
224         || IsJPEG2000( key ) )
225       {
226          r = true;
227       }
228    }
229    return r;
230 }
231
232 /**
233  * \brief   Determines if the Transfer Syntax corresponds to encapsulated
234  *          of encoded Pixel Data (as opposed to native).
235  * @return  True when encapsulated. False when native.
236  */
237 bool TS::IsEncapsulate(TSKey const &key)
238 {
239    bool r = false;
240    // First check this is an actual transfer syntax
241    if( IsTransferSyntax(key) )
242    {
243       if ( key == SpecialStrings[RLELossless]
244         || IsJPEG(key) )
245       {
246          r = true;
247       }
248    }
249    return r;
250 }
251
252 TS::SpecialType TS::GetSpecialTransferSyntax(TSKey const &key)
253 {
254    for (int i = 0; SpecialStrings[i] != NULL; i++)
255    {
256       if ( SpecialStrings[i] == key )
257       {
258          return SpecialType(i);
259       }
260    }
261
262    return UnknownTS;
263 }
264
265 const char* TS::GetSpecialTransferSyntax(SpecialType t)
266 {
267    return SpecialStrings[t];
268 }
269
270 //-----------------------------------------------------------------------------
271 // Protected
272
273 //-----------------------------------------------------------------------------
274 // Private
275
276 //-----------------------------------------------------------------------------
277
278 } // end namespace gdcm