]> Creatis software - gdcm.git/blob - Example/exOverlaysDCM.cxx
Fix bug (Thx, Manu)
[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 void explodeByte(unsigned char byte, unsigned char* result) 
11 {
12    unsigned char mask = 1;
13    for (int i=0;i<8;i++) 
14    {
15       if ((byte & mask)==0) 
16          result[i]=0;
17       else 
18          result[i]=1;
19       mask<<=1;
20    }
21    return;
22 }
23
24
25 int main(int argc, char *argv[])
26 {
27    START_USAGE(usage)
28    " \n exReadOverlays :\n                                                    ",
29    " Extract an overlay image from a secondary capture image                  ",
30    "          Warning : probably segfaults if no overlay                      ",
31    " usage: WriteOverlayImage filein=inputFileName fileout=outputFileName[debug]",
32    "        debug    : user wants to run the program in 'debug mode'          ",
33    FINISH_USAGE
34
35    // ----- Initialize Arguments Manager ------
36    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
37
38    if (argc == 1 || am->ArgMgrDefined("usage"))
39    {
40       am->ArgMgrUsage(usage); // Display 'usage'
41       delete am;
42       return 0;
43    }
44    char *fileName = am->ArgMgrWantString("filein",usage);
45    if ( fileName == NULL )
46    {
47       delete am;
48       return 0;
49    }
50
51    char *outputFileName = am->ArgMgrWantString("fileout",usage);
52    if ( outputFileName == NULL )
53    {
54       delete am;
55       return 0;
56    }
57    if (am->ArgMgrDefined("debug"))
58       gdcm::Debug::DebugOn();
59
60    // if unused Param we give up
61    if ( am->ArgMgrPrintUnusedLabels() )
62    {
63       am->ArgMgrUsage(usage);
64       delete am;
65       return 0;
66    }
67
68    delete am;  // we don't need Argument Manager any longer
69
70    // ============================================================
71    //   Read the input file.
72    // ============================================================
73
74    gdcm::File *f;
75
76    f = gdcm::File::New(  );
77    f->SetLoadMode( gdcm::LD_ALL );
78    f->SetFileName( fileName );
79    f->AddForceLoadElement(0x6000,0x3000);  // Overlay Data
80    int res = f->Load();
81
82    if ( !res )
83    {
84        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
85                  << "DICOM / ACR File" <<std::endl;
86        f->Delete();
87        return 0;
88    }
89    std::cout << " ... is readable " << std::endl;
90
91    // ============================================================
92    //   Load the Overlays in memory (the first one)
93    // ============================================================
94
95    gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);  
96    if (e == 0)
97    {
98       std::cout << " Image doesn't contain any Overlay " << std::endl;
99       f->Delete();
100       return 0;
101    }   
102    std::cout << " File is read! " << std::endl;
103
104    uint8_t *overlay = (uint8_t *)(e->GetBinArea());
105    if ( overlay == 0 )
106    {
107        std::cerr << "Sorry, Overlays of" << fileName <<"  are not "
108                  << " gdcm-readable."    << std::endl;
109        f->Delete();
110        return 0;
111    }
112
113    // ============================================================
114    //  Image data generation.
115    // ============================================================
116
117    unsigned int dimX= f->GetXSize();
118    unsigned int dimY= f->GetYSize();
119
120    unsigned int dimXY=dimX*dimY;
121    std::cout <<"DimX : "<<dimX<<"DimY : "<<dimY<<"DimXY : "<<dimXY << std::endl;
122    unsigned char outputData[dimXY];
123
124    unsigned char *result=outputData;
125    for (int i=0;i<(dimXY/8);i++) 
126    {
127       explodeByte(overlay[i], result);
128       result+=8;
129    }
130
131
132    // ============================================================
133    //   Write a new file
134    // ============================================================
135
136    char temp[256];
137    
138    sprintf(temp,"%d\0",dimX);
139    f->InsertEntryString(temp,0x0028,0x0011); // Columns
140    sprintf(temp,"%d\0",dimY);
141    f->InsertEntryString(temp,0x0028,0x0010); // Rows
142
143    f->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
144    f->InsertEntryString("8",0x0028,0x0101); // Bits Stored
145    f->InsertEntryString("7",0x0028,0x0102); // High Bit
146    f->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
147    f->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
148    f->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);  
149
150    // We need a gdcm::FileHelper, since we want to load the pixels
151    gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
152        
153    fh->SetImageData(outputData,dimXY);
154    fh->WriteDcmExplVR(outputFileName);
155    std::cout <<"End WriteOverlayImage" << std::endl;
156
157    f->Delete();   
158    fh->Delete();
159    return 0;
160 }
161