]> Creatis software - gdcm.git/blob - Example/exCurveData.cxx
BUG: fix wrong sizeof
[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 13:12:09 $
7   Version:   $Revision: 1.8 $
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( p[0] == type ) // 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    bool res = f->Load();  
116
117    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
118    {
119       std::cout << "---------------------------------------------" << std::endl;
120       f->Print();
121       std::cout << "---------------------------------------------" << std::endl;
122    }
123    if (!res) {
124        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
125            << "DICOM / ACR File"
126            <<std::endl;
127       f->Delete();
128       return 1;
129    }
130    std::cout << " ... is readable " << std::endl;
131
132 // ============================================================
133 //   Check whether image contains Overlays ACR-NEMA style.
134 // ============================================================
135
136    //* B 5004|3000 [OW]                    [Curve Data] [GDCM_NAME_SPACE::Binary data loaded;length = 1938]
137    std::string curve_data_str = f->GetEntryString(0x5004, 0x3000);
138    if (curve_data_str == GDCM_NAME_SPACE::GDCM_UNFOUND)
139    {
140       std::cout << " Image doesn't contain any Curve Data" << std::endl;
141       f->Delete();
142       return 1;
143    }
144    std::cout << " File is read! " << std::endl;
145
146
147 // ============================================================
148 //   Load the Curve Data in memory.
149 // ============================================================
150   std::istringstream convert;
151  //* V 5004|0005 [US]              [Curve Dimensions] [1] x(1)
152    std::string curve_dim_str = f->GetEntryString(0x5004,0x0005);
153    unsigned short curve_dim;
154    convert.str(curve_dim_str);
155    convert >> curve_dim;
156    std::cout << "Curve Dimensions: " << curve_dim << std::endl;
157  //* V 5004|0010 [US]              [Number of Points] [969] x(3c9)
158    std::string num_points_str = f->GetEntryString(0x5004,0x0010);
159    unsigned short num_points;
160    convert.clear(); //important
161    convert.str(num_points_str);
162    convert >> num_points;
163    std::cout << "Number of Points: " << num_points << std::endl;
164  //* V 5004|0020 [CS]                  [Type of Data] [PHYSIO]
165    std::string data_type = f->GetEntryString(0x5004,0x0020);
166    std::cout << "Type of Data: " << data_type << std::endl;
167    std::cout << " this is thus a : " << ConvertTypeOfData(data_type) << std::endl;
168  //* V 5004|0022 [LO]             [Curve Description] []
169    std::string curve_desc = f->GetEntryString(0x5004,0x0022);
170    std::cout << "Curve Description: " << curve_desc << std::endl;
171  //* V 5004|0103 [US]     [Data Value Representation] [0] x(0)
172    std::string data_rep_str = f->GetEntryString(0x5004,0x0103);
173    unsigned short data_rep;
174    convert.clear(); //important
175    convert.str(data_rep_str);
176    convert >> data_rep;
177
178
179    GDCM_NAME_SPACE::DocEntry *pCurveDataDoc = f->GetDocEntry(0x5004, 0x3000);
180    GDCM_NAME_SPACE::DataEntry *pCurveData = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(pCurveDataDoc);
181    uint8_t *curve_data = pCurveData->GetBinArea();
182    
183    // From Part3, C.10.2.1.2 Data value representation (p668)
184    size_t sz;
185    int sizeofdatarep = 0;
186    switch( data_rep)
187    {
188       case 0:
189          sz = PrintCurveData((DataRepToType<0>::Type*)(curve_data), num_points);
190          sizeofdatarep = sizeof( DataRepToType<0>::Type );
191          break;
192       case 1:
193          sz = PrintCurveData((DataRepToType<1>::Type*)(curve_data), num_points);
194          sizeofdatarep = sizeof( DataRepToType<1>::Type );
195          break;
196       case 2:
197          sz = PrintCurveData((DataRepToType<2>::Type*)(curve_data), num_points);
198          sizeofdatarep = sizeof( DataRepToType<2>::Type );
199          break;
200       case 3:
201          sz = PrintCurveData((DataRepToType<3>::Type*)(curve_data), num_points);
202          sizeofdatarep = sizeof( DataRepToType<3>::Type );
203          break;
204       case 4:
205          sz = PrintCurveData((DataRepToType<4>::Type*)(curve_data), num_points);
206          sizeofdatarep = sizeof( DataRepToType<4>::Type );
207          break;
208       default:
209          std::cerr << "Error don't know the type: " << data_rep_str << std::endl;
210          f->Delete();
211          return 1;
212    }
213    // Just to make sure that values read are consistant and we won't read out of bound data:
214    assert( sz*num_points*sizeofdatarep == pCurveData->GetLength());
215
216    // Write out the data as a file:
217    //std::ofstream o("/tmp/curve_data.raw");
218    //o.write((char*)curve_data, num_points*sz);
219    //o.close();
220
221    f->Delete();
222    return 0;
223 }
224
225