]> Creatis software - gdcm.git/blob - Example/exCurveData.cxx
Fix mistypings
[gdcm.git] / Example / exCurveData.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exCurveData.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/10 14:30:08 $
7   Version:   $Revision: 1.9 $
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 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23 #include "gdcmDataEntry.h"
24
25 static const char* TypeOfDataArrays[13][2] = {
26     { "TAC" , "time activity curve" },
27     { "PROF" , "image profile" },
28     { "HIST" , "histogram" },
29     { "ROI" , "polygraphic region of interest" },
30     { "TABL" , "table of values" },
31     { "FILT" , "filter kernel" },
32     { "POLY" , "poly line" },
33     { "ECG" , "ecg data" },
34     { "PRESSURE" , "pressure data" },
35     { "FLOW" , "flow data" },
36     { "PHYSIO" , "physio data" },
37     { "RESP" , "Respiration trace" },
38     { NULL, NULL }
39 };
40
41 // Part 3, C.10.2.1.1 Type of data
42 // Convert from acronym to full description
43 const char *ConvertTypeOfData(std::string const &type)
44 {
45   const char **p = *TypeOfDataArrays;
46   while(*p != NULL)
47     {
48     if( strncmp(p[0], type.c_str(), strlen(p[0])) == 0 ) // std::string== operator
49       {
50       // ok we found it:
51       return p[1];
52       }
53     p+=2;
54     }
55   return NULL;
56 }
57
58 // Helper function
59 template<class DataValueRepresentation>
60 inline size_t PrintCurveData(DataValueRepresentation* data, unsigned short numPts)
61 {
62    for(unsigned int i=0; i<numPts;++i)
63      {
64      std::cout << "Pt(" << i <<  ") = " << data[i] << std::endl;
65      }
66
67    // ok this is ugly but I need the size outside of the function
68    return sizeof(DataValueRepresentation);
69 }
70
71 template <int datarep> struct DataRepToType;
72 template<> struct DataRepToType<0> { typedef unsigned short Type; };
73 template<> struct DataRepToType<1> { typedef signed short Type; };
74 template<> struct DataRepToType<2> { typedef float Type; };
75 template<> struct DataRepToType<3> { typedef double Type; };
76 template<> struct DataRepToType<4> { typedef signed long Type; };
77  
78 /*
79  // Example (sorry, we've got no more than this one ...)
80  * V 5004|0000 [UL]                  [Group Length] [1998] x(7ce)
81  * V 5004|0005 [US]              [Curve Dimensions] [1] x(1)
82  * V 5004|0010 [US]              [Number of Points] [969] x(3c9)
83  * V 5004|0020 [CS]                  [Type of Data] [PHYSIO]
84  * V 5004|0022 [LO]             [Curve Description] []
85  * V 5004|0103 [US]     [Data Value Representation] [0] x(0)
86  * B 5004|3000 [OW]                    [Curve Data] [GDCM_NAME_SPACE::Binary data loaded;length = 1938]
87  */
88  
89 int main(int argc, char *argv[])
90 {  
91    GDCM_NAME_SPACE::File *f;
92  
93    std::cout << "------------------------------------------------" << std::endl;
94    std::cout << "Gets the 'Curve Data' from a full gdcm-readable DICOM " << std::endl;
95    std::cout << "(Note :  we just have ONE image : "
96              << "GE_DLX-8-MONO2-Multiframe.dcm"
97              << std::endl;
98    std::cout << "------------------------------------------------" << std::endl;
99
100    std::string fileName;
101    if( argc > 1 )
102       fileName = argv[1];
103    else
104       fileName = "GE_DLX-8-MONO2-Multiframe.dcm";
105
106    std::cout << fileName << std::endl;
107 // ============================================================
108 //   Read the input image.
109 // ============================================================
110
111    f = GDCM_NAME_SPACE::File::New( );
112
113    f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
114    f->SetFileName( fileName );
115    f->SetMaxSizeLoadEntry(0xffff);
116    bool res = f->Load();  
117
118    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
119    {
120       std::cout << "---------------------------------------------" << std::endl;
121       f->Print();
122       std::cout << "---------------------------------------------" << std::endl;
123    }
124    if (!res) {
125        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
126            << "DICOM / ACR File"
127            <<std::endl;
128       f->Delete();
129       return 1;
130    }
131    std::cout << " ... is readable " << std::endl;
132
133 // ============================================================
134 //   Check whether image contains Overlays ACR-NEMA style.
135 // ============================================================
136
137    const uint16_t curvedatagroup = 0x5000;
138    //* B 5004|3000 [OW]                    [Curve Data] [GDCM_NAME_SPACE::Binary data loaded;length = 1938]
139    std::string curve_data_str = f->GetEntryString(curvedatagroup, 0x3000);
140    if (curve_data_str == GDCM_NAME_SPACE::GDCM_UNFOUND)
141    {
142       std::cout << " Image doesn't contain any Curve Data" << std::endl;
143       f->Delete();
144       return 1;
145    }
146    std::cout << " File is read! " << std::endl;
147
148
149 // ============================================================
150 //   Load the Curve Data in memory.
151 // ============================================================
152   std::istringstream convert;
153  //* V 5004|0005 [US]              [Curve Dimensions] [1] x(1)
154    std::string curve_dim_str = f->GetEntryString(curvedatagroup,0x0005);
155    unsigned short curve_dim;
156    convert.str(curve_dim_str);
157    convert >> curve_dim;
158    std::cout << "Curve Dimensions: " << curve_dim << std::endl;
159  //* V 5004|0010 [US]              [Number of Points] [969] x(3c9)
160    std::string num_points_str = f->GetEntryString(curvedatagroup,0x0010);
161    unsigned short num_points;
162    convert.clear(); //important
163    convert.str(num_points_str);
164    convert >> num_points;
165    std::cout << "Number of Points: " << num_points << std::endl;
166  //* V 5004|0020 [CS]                  [Type of Data] [PHYSIO]
167    std::string data_type = f->GetEntryString(curvedatagroup,0x0020);
168    std::cout << "Type of Data: " << data_type << std::endl;
169    const char *datatype = ConvertTypeOfData(data_type);
170    std::cout << " this is thus a : " << (datatype ? datatype : "") << std::endl;
171  //* V 5004|0022 [LO]             [Curve Description] []
172    std::string curve_desc = f->GetEntryString(curvedatagroup,0x0022);
173    std::cout << "Curve Description: " << curve_desc << std::endl;
174  //* V 5004|0103 [US]     [Data Value Representation] [0] x(0)
175    std::string data_rep_str = f->GetEntryString(curvedatagroup,0x0103);
176    unsigned short data_rep;
177    convert.clear(); //important
178    convert.str(data_rep_str);
179    convert >> data_rep;
180
181
182    GDCM_NAME_SPACE::DocEntry *pCurveDataDoc = f->GetDocEntry(curvedatagroup, 0x3000);
183    GDCM_NAME_SPACE::DataEntry *pCurveData = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(pCurveDataDoc);
184    uint8_t *curve_data = pCurveData->GetBinArea();
185    
186    // From Part3, C.10.2.1.2 Data value representation (p668)
187    size_t sz;
188    int sizeofdatarep = 0;
189    switch( data_rep)
190    {
191       case 0:
192          sz = PrintCurveData((DataRepToType<0>::Type*)(curve_data), num_points);
193          sizeofdatarep = sizeof( DataRepToType<0>::Type );
194          break;
195       case 1:
196          sz = PrintCurveData((DataRepToType<1>::Type*)(curve_data), num_points);
197          sizeofdatarep = sizeof( DataRepToType<1>::Type );
198          break;
199       case 2:
200          sz = PrintCurveData((DataRepToType<2>::Type*)(curve_data), num_points);
201          sizeofdatarep = sizeof( DataRepToType<2>::Type );
202          break;
203       case 3:
204          sz = PrintCurveData((DataRepToType<3>::Type*)(curve_data), num_points);
205          sizeofdatarep = sizeof( DataRepToType<3>::Type );
206          break;
207       case 4:
208          sz = PrintCurveData((DataRepToType<4>::Type*)(curve_data), num_points);
209          sizeofdatarep = sizeof( DataRepToType<4>::Type );
210          break;
211       default:
212          std::cerr << "Error don't know the type: " << data_rep_str << std::endl;
213          f->Delete();
214          return 1;
215    }
216    // Just to make sure that values read are consistant and we won't read out of bound data:
217    //assert( sz*num_points*sizeofdatarep == pCurveData->GetLength());
218
219    // Write out the data as a file:
220    //std::ofstream o("/tmp/curve_data.raw");
221    //o.write((char*)curve_data, num_points*sz);
222    //o.close();
223
224    f->Delete();
225    return 0;
226 }
227
228