]> Creatis software - gdcm.git/blob - src/gdcmTS.cxx
ENH: Untangle the transfer syntax from the Document. The Document can only read a...
[gdcm.git] / src / gdcmTS.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmTS.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/11 16:44:43 $
7   Version:   $Revision: 1.35 $
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  * \ingroup VR
114  * \brief   Print all 
115  * @param   os The output stream to be written to.
116  */
117 void TS::Print(std::ostream &os) 
118 {
119    std::ostringstream s;
120
121    for (TSHT::const_iterator it = TsMap.begin(); it != TsMap.end(); ++it)
122    {
123       s << "TS : " << it->first << " = " << it->second << std::endl;
124    }
125    os << s.str();
126 }
127
128 //-----------------------------------------------------------------------------
129 // Public
130 int TS::Count(TSKey const &key) 
131 {
132    return TsMap.count(key);
133 }
134
135 TSAtr const & TS::GetValue(TSKey const &key) 
136 {
137    TSHT::const_iterator it = TsMap.find(key);
138    if (it == TsMap.end())
139    {
140       return GDCM_UNFOUND;
141    }
142    return it->second;
143 }
144
145 bool TS::IsTransferSyntax(TSKey const &key)
146 {
147    TSHT::const_iterator it = TsMap.find(key);
148    return it != TsMap.end();
149 }
150
151 bool TS::IsRLELossless(TSKey const &key)
152 {
153    bool r = false;
154    // First check this is an actual transfer syntax
155    if( IsTransferSyntax(key) )
156    {
157       if ( key == SpecialStrings[RLELossless] )
158       {
159          r = true;
160       }
161    }
162    return r;
163 }
164
165 bool TS::IsJPEGLossless(TSKey const &key)
166 {
167    bool r = false;
168    // First check this is an actual transfer syntax
169    if( IsTransferSyntax(key) )
170    {
171       if ( key == SpecialStrings[JPEGFullProgressionProcess10_12]
172         || key == SpecialStrings[JPEGLosslessProcess14]
173         || key == SpecialStrings[JPEGLosslessProcess14_1] )
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 JPEG2000 one
184  * @return  True when JPEG2000 (Lossly or LossLess) found. False in all
185  *          other cases.
186  */
187 bool TS::IsJPEG2000(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[JPEG2000Lossless]
194         || key == SpecialStrings[JPEG2000] )
195       {
196          r = true;
197       }
198    }
199    return r;
200 }
201
202 /**
203  * \brief   Determines if the Transfer Syntax corresponds to any form
204  *          of Jpeg encoded Pixel data.
205  * @return  True when any form of JPEG found. False otherwise.
206  */
207 bool TS::IsJPEG(TSKey const &key)
208 {
209    bool r = false;
210    // First check this is an actual transfer syntax
211    if( IsTransferSyntax(key) )
212    {
213       if ( key == SpecialStrings[JPEGBaselineProcess1]
214         || key == SpecialStrings[JPEGExtendedProcess2_4]
215         || key == SpecialStrings[JPEGExtendedProcess3_5]
216         || key == SpecialStrings[JPEGSpectralSelectionProcess6_8]
217         || IsJPEGLossless( key ) 
218         || IsJPEG2000( key ) )
219       {
220          r = true;
221       }
222    }
223    return r;
224 }
225
226 /**
227  * \brief   Determines if the Transfer Syntax corresponds to encapsulated
228  *          of encoded Pixel Data (as opposed to native).
229  * @return  True when encapsulated. False when native.
230  */
231 bool TS::IsEncapsulate(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[RLELossless]
238         || IsJPEG(key) )
239       {
240          r = true;
241       }
242    }
243    return r;
244 }
245
246 TS::SpecialType TS::GetSpecialTransferSyntax(TSKey const &key)
247 {
248    for (int i = 0; SpecialStrings[i] != NULL; i++)
249    {
250       if ( SpecialStrings[i] == key )
251       {
252          return SpecialType(i);
253       }
254    }
255
256    return UnknownTS;
257 }
258
259 const char* TS::GetSpecialTransferSyntax(SpecialType t)
260 {
261    return SpecialStrings[t];
262 }
263
264 //-----------------------------------------------------------------------------
265 // Protected
266
267 //-----------------------------------------------------------------------------
268 // Private
269
270 //-----------------------------------------------------------------------------
271
272 } // end namespace gdcm