]> Creatis software - gdcm.git/blob - Example/exOverlaysDCM.cxx
In order to allow to use current version (1.3) of gdcm *and* ITK (that includes
[gdcm.git] / Example / exOverlaysDCM.cxx
1 #include "gdcmFile.h"
2 #include "gdcmFileHelper.h"
3 #include "gdcmCommon.h"
4 #include "gdcmDebug.h"
5 #include "gdcmDataEntry.h"
6
7 #include "gdcmArgMgr.h"
8 #include <iostream>
9
10 // Each BIT of Overlay Data (0x6000,0x3000) corresponds 
11 // to a BYTE of overlay image.
12 void explodeByte(unsigned char byte, unsigned char* result) 
13 {
14    unsigned char mask = 1;
15    for (int i=0;i<8;i++) 
16    {
17       if ((byte & mask)==0) 
18          result[i]=0;
19       else 
20          result[i]=1;
21       mask<<=1;
22    }
23    return;
24 }
25
26
27 int main(int argc, char *argv[])
28 {
29    START_USAGE(usage)
30    " \n exOverlaysDCM :\n                                                     ",
31    " Extract an overlay image from a DICOM image                              ",
32    "          Warning : probably segfaults if no overlay                      ",
33    " usage: exOverlaysDCM filein=inputFileName fileout=outputFileName [debug] ",
34    "        debug    : developper wants to run the program in 'debug mode'    ",
35    FINISH_USAGE
36
37    // ----- Initialize Arguments Manager ------
38    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
39
40    if (argc == 1 || am->ArgMgrDefined("usage"))
41    {
42       am->ArgMgrUsage(usage); // Display 'usage'
43       delete am;
44       return 0;
45    }
46    
47 /*
48    char *fileName = am->ArgMgrWantString("filein",usage);
49    if ( fileName == NULL )
50    {
51       delete am;
52       return 0;
53    }
54 */
55
56
57    const char *fileName = am->ArgMgrGetString("filein");
58    const char *dirName  = am->ArgMgrGetString("dirin");
59
60    if ( (fileName == 0 && dirName == 0) ||
61         (fileName != 0 && dirName != 0) )
62    {
63       std::cerr << std::endl
64         << "Either 'filein=' or 'dirin=' must be present;" 
65         << std::endl << "Not both" << std::endl;
66       am->ArgMgrUsage(usage); // Display 'usage'  
67       delete am;
68       return 1;
69    }
70
71
72    char *outputFileName = am->ArgMgrWantString("fileout",usage);
73    if ( outputFileName == NULL )
74    {
75       delete am;
76       return 0;
77    }
78    if (am->ArgMgrDefined("debug"))
79       GDCM_NAME_SPACE::Debug::DebugOn();
80       
81    if (am->ArgMgrDefined("warning"))
82       GDCM_NAME_SPACE::Debug::WarningOn();
83       
84    // if unused Param we give up
85    if ( am->ArgMgrPrintUnusedLabels() )
86    {
87       am->ArgMgrUsage(usage);
88       delete am;
89       return 0;
90    }
91
92    delete am;  // we don't need Argument Manager any longer
93
94    // ============================================================
95    //   Read the input file.
96    // ============================================================
97
98    GDCM_NAME_SPACE::File *f;
99
100    f = GDCM_NAME_SPACE::File::New(  );
101    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
102    f->SetFileName( fileName );
103    f->AddForceLoadElement(0x6000,0x3000);  // Overlay Data
104    int res = f->Load();
105
106    if ( !res )
107    {
108        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
109                  << "DICOM / ACR File" <<std::endl;
110        f->Delete();
111        return 0;
112    }
113    std::cout << " ... is readable " << std::endl;
114
115    // ============================================================
116    //   Load the Overlays in memory (the first one)
117    // ============================================================
118
119    GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);  
120    if (e == 0)
121    {
122       std::cout << " Image doesn't contain any Overlay " << std::endl;
123       f->Delete();
124       return 0;
125    }   
126    std::cout << " File is read! " << std::endl;
127
128    uint8_t *overlay = (uint8_t *)(e->GetBinArea());
129    if ( overlay == 0 )
130    {
131        std::cerr << "Sorry, Overlays of" << fileName <<"  are not "
132                  << " gdcm-readable."    << std::endl;
133        f->Delete();
134        return 0;
135    }
136
137    // ============================================================
138    //  Image data generation.
139    // ============================================================
140    
141
142    unsigned int dimX= f->GetXSize();
143    unsigned int dimY= f->GetYSize();
144
145    unsigned int dimXY=dimX*dimY;
146    std::cout << "DimX : "<< dimX <<" DimY : " << dimY 
147              << " DimXY : " <<dimXY << std::endl;
148    unsigned char *outputData = new unsigned char[dimXY];
149
150    unsigned char *result=outputData;
151    for (unsigned int i=0;i<(dimXY/8);i++) 
152    {
153       explodeByte(overlay[i], result);
154       result+=8;
155    }
156    
157    // ============================================================
158    //   Write a new file
159    // ============================================================
160    GDCM_NAME_SPACE::File *f2;
161    f2 = GDCM_NAME_SPACE::File::New(  );
162    
163    char temp[256];
164    
165    sprintf(temp,"%d ",dimX);
166    f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
167    sprintf(temp,"%d ",dimY);
168    f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
169
170    f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
171    f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
172    f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
173    f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
174    f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
175    f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");  
176
177
178    // feel free to add any field (Dicom Data Entry) you like, here.
179    // ...
180    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f2);
181        
182    fh->SetImageData(outputData,dimXY);
183    fh->WriteDcmExplVR(outputFileName);
184    std::cout <<"End WriteOverlayImage" << std::endl;
185
186    delete outputData;
187    f->Delete();
188    f2->Delete();  
189    fh->Delete();
190    
191    return 0;
192 }
193