]> Creatis software - gdcm.git/blob - Example/exCurveData.cxx
ENH: Adding an example for reading and playing with Curve Data (500x,3000)
[gdcm.git] / Example / exCurveData.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exCurveData.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/10 20:02:09 $
7   Version:   $Revision: 1.1 $
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 "gdcmBinEntry.h"
24
25 // Helper function
26 template<class DataValueRepresentation>
27 inline size_t PrintCurveData(DataValueRepresentation* data, unsigned short numPts)
28 {
29    for(unsigned int i=0; i<numPts;++i)
30      {
31      std::cout << "Pt(" << i <<  ") = " << data[i] << std::endl;
32      }
33
34    // ok this is ugly but I need the size outside of the function
35    return sizeof(DataValueRepresentation);
36 }
37  
38 /*
39  // Example (sorry, we've got no more than this one ...)
40  * V 5004|0000 [UL]                  [Group Length] [1998] x(7ce)
41  * V 5004|0005 [US]              [Curve Dimensions] [1] x(1)
42  * V 5004|0010 [US]              [Number of Points] [969] x(3c9)
43  * V 5004|0020 [CS]                  [Type of Data] [PHYSIO]
44  * V 5004|0022 [LO]             [Curve Description] []
45  * V 5004|0103 [US]     [Data Value Representation] [0] x(0)
46  * B 5004|3000 [OW]                    [Curve Data] [gdcm::Binary data loaded;length = 1938]
47  */
48  
49 int main(int argc, char *argv[])
50 {  
51    gdcm::File *f;
52  
53    std::cout << "------------------------------------------------" << std::endl;
54    std::cout << "Gets the 'Curve Data' from a full gdcm-readable DICOM " << std::endl;
55    std::cout << "(Note :  we just have ONE image : "
56              << "GE_DLX-8-MONO2-Multiframe.dcm"
57              << std::endl;
58    std::cout << "------------------------------------------------" << std::endl;
59
60    std::string fileName;
61    if( argc > 1 )
62       fileName = argv[1];
63    else
64       fileName = "GE_DLX-8-MONO2-Multiframe.dcm";
65
66    std::cout << fileName << std::endl;
67 // ============================================================
68 //   Read the input image.
69 // ============================================================
70
71    f = new gdcm::File( );
72
73    f->SetLoadMode(gdcm::LD_NOSEQ | gdcm::LD_NOSHADOW);
74    f->SetFileName( fileName );
75    bool res = f->Load();  
76
77    if( gdcm::Debug::GetDebugFlag() )
78    {
79       std::cout << "---------------------------------------------" << std::endl;
80       f->Print();
81       std::cout << "---------------------------------------------" << std::endl;
82    }
83    if (!res) {
84        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
85            << "DICOM / ACR File"
86            <<std::endl;
87       delete f;
88       return 1;
89    }
90    std::cout << " ... is readable " << std::endl;
91
92 // ============================================================
93 //   Check whether image contains Overlays ACR-NEMA style.
94 // ============================================================
95
96    //* B 5004|3000 [OW]                    [Curve Data] [gdcm::Binary data loaded;length = 1938]
97    std::string curve_data_str = f->GetEntryValue(0x5004, 0x3000);
98    if (curve_data_str == gdcm::GDCM_UNFOUND)
99    {
100       std::cout << " Image doesn't contain any Curve Data" << std::endl;
101       delete f;
102       return 1;
103    }
104    std::cout << " File is read! " << std::endl;
105
106
107 // ============================================================
108 //   Load the Curve Data in memory.
109 // ============================================================
110   std::istringstream convert;
111  //* V 5004|0005 [US]              [Curve Dimensions] [1] x(1)
112    std::string curve_dim_str = f->GetEntryValue(0x5004,0x0005);
113    unsigned short curve_dim;
114    convert.str(curve_dim_str);
115    convert >> curve_dim;
116    std::cout << "Curve Dimensions: " << curve_dim << std::endl;
117  //* V 5004|0010 [US]              [Number of Points] [969] x(3c9)
118    std::string num_points_str = f->GetEntryValue(0x5004,0x0010);
119    unsigned short num_points;
120    convert.clear(); //important
121    convert.str(num_points_str);
122    convert >> num_points;
123    std::cout << "Number of Points: " << num_points << std::endl;
124  //* V 5004|0020 [CS]                  [Type of Data] [PHYSIO]
125    std::string data_type = f->GetEntryValue(0x5004,0x0020);
126    std::cout << "Type of Data: " << data_type << std::endl;
127  //* V 5004|0022 [LO]             [Curve Description] []
128    std::string curve_desc = f->GetEntryValue(0x5004,0x0022);
129    std::cout << "Curve Description: " << curve_desc << std::endl;
130  //* V 5004|0103 [US]     [Data Value Representation] [0] x(0)
131    std::string data_rep_str = f->GetEntryValue(0x5004,0x0103);
132    unsigned short data_rep;
133    convert.clear(); //important
134    convert.str(data_rep_str);
135    convert >> data_rep;
136
137
138    gdcm::DocEntry *pCurveDataDoc = f->GetDocEntry(0x5004, 0x3000);
139    gdcm::BinEntry *pCurveData = dynamic_cast<gdcm::BinEntry*>(pCurveDataDoc);
140    uint8_t *curve_data = pCurveData->GetBinArea();
141    
142    // From Part3, C.10.2.1.2 Data value representation (p668)
143    size_t sz;
144    switch( data_rep)
145      {
146    case 0:
147      sz = PrintCurveData((unsigned short*)(curve_data), num_points);
148      break;
149    case 1:
150      sz = PrintCurveData((signed short*)(curve_data), num_points);
151      break;
152    case 2:
153      sz = PrintCurveData((float*)(curve_data), num_points);
154      break;
155    case 3:
156      sz = PrintCurveData((double*)(curve_data), num_points);
157      break;
158    case 4:
159      sz = PrintCurveData((signed long*)(curve_data), num_points);
160      break;
161    default:
162      std::cerr << "Error don't know the type: " << data_rep_str << std::endl;
163      delete f;
164      return 1;
165      }
166    // Just to make sure that values read are consistant and we won't read out of bound data:
167    assert( sz*num_points == pCurveData->GetLength());
168
169    // Write out the data as a file:
170    //std::ofstream o("/tmp/curve_data.raw");
171    //o.write((char*)curve_data, num_points*sz);
172    //o.close();
173
174    delete f;
175    return 0;
176 }
177
178