]> Creatis software - gdcm.git/blob - Example/exOverlaysDCM.cxx
Fix misstyping
[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::ArgMgr *am = new gdcm::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    char *fileName = am->ArgMgrWantString("filein",usage);
47    if ( fileName == NULL )
48    {
49       delete am;
50       return 0;
51    }
52
53    char *outputFileName = am->ArgMgrWantString("fileout",usage);
54    if ( outputFileName == NULL )
55    {
56       delete am;
57       return 0;
58    }
59    if (am->ArgMgrDefined("debug"))
60       gdcm::Debug::DebugOn();
61
62    // if unused Param we give up
63    if ( am->ArgMgrPrintUnusedLabels() )
64    {
65       am->ArgMgrUsage(usage);
66       delete am;
67       return 0;
68    }
69
70    delete am;  // we don't need Argument Manager any longer
71
72    // ============================================================
73    //   Read the input file.
74    // ============================================================
75
76    gdcm::File *f;
77
78    f = gdcm::File::New(  );
79    f->SetLoadMode( gdcm::LD_ALL );
80    f->SetFileName( fileName );
81    f->AddForceLoadElement(0x6000,0x3000);  // Overlay Data
82    int res = f->Load();
83
84    if ( !res )
85    {
86        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
87                  << "DICOM / ACR File" <<std::endl;
88        f->Delete();
89        return 0;
90    }
91    std::cout << " ... is readable " << std::endl;
92
93    // ============================================================
94    //   Load the Overlays in memory (the first one)
95    // ============================================================
96
97    gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);  
98    if (e == 0)
99    {
100       std::cout << " Image doesn't contain any Overlay " << std::endl;
101       f->Delete();
102       return 0;
103    }   
104    std::cout << " File is read! " << std::endl;
105
106    uint8_t *overlay = (uint8_t *)(e->GetBinArea());
107    if ( overlay == 0 )
108    {
109        std::cerr << "Sorry, Overlays of" << fileName <<"  are not "
110                  << " gdcm-readable."    << std::endl;
111        f->Delete();
112        return 0;
113    }
114
115    // ============================================================
116    //  Image data generation.
117    // ============================================================
118    
119
120    unsigned int dimX= f->GetXSize();
121    unsigned int dimY= f->GetYSize();
122
123    unsigned int dimXY=dimX*dimY;
124    std::cout << "DimX : "<< dimX <<" DimY : " << dimY 
125              << " DimXY : " <<dimXY << std::endl;
126    unsigned char *outputData = new unsigned char[dimXY];
127
128    unsigned char *result=outputData;
129    for (unsigned int i=0;i<(dimXY/8);i++) 
130    {
131       explodeByte(overlay[i], result);
132       result+=8;
133    }
134    
135    // ============================================================
136    //   Write a new file
137    // ============================================================
138    gdcm::File *f2;
139    f2 = gdcm::File::New(  );
140    
141    char temp[256];
142    
143    sprintf(temp,"%d ",dimX);
144    f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
145    sprintf(temp,"%d ",dimY);
146    f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
147
148    f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
149    f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
150    f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
151    f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
152    f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
153    f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");  
154
155
156    // feel free to add any field (Dicom Data Entry) you like, here.
157    // ...
158    gdcm::FileHelper *fh = gdcm::FileHelper::New(f2);
159        
160    fh->SetImageData(outputData,dimXY);
161    fh->WriteDcmExplVR(outputFileName);
162    std::cout <<"End WriteOverlayImage" << std::endl;
163
164    delete outputData;
165    f->Delete();
166    f2->Delete();  
167    fh->Delete();
168    
169    return 0;
170 }
171