]> Creatis software - gdcm.git/blob - Example/ExtractOverlays.cxx
Remove old stuff
[gdcm.git] / Example / ExtractOverlays.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ExtractOverlays.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:04 $
7   Version:   $Revision: 1.3 $
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
19
20 #include "gdcmFile.h"
21 #include "gdcmFileHelper.h"
22 #include "gdcmCommon.h"
23 #include "gdcmDebug.h"
24 #include "gdcmDataEntry.h"
25 #include "gdcmDirList.h"
26
27 #include "gdcmArgMgr.h"
28 #include <iostream>
29
30 // Each BIT of Overlay Data (0x6000,0x3000) corresponds 
31 // to a BYTE of overlay image.
32 void explodeByte(unsigned char byte, unsigned char* result) 
33 {
34    unsigned char mask = 1;
35    for (int i=0;i<8;i++) 
36    {
37       if ((byte & mask)==0) 
38          result[i]=0;
39       else 
40          result[i]=1;
41       mask<<=1;
42    }
43    return;
44 }
45
46
47 int main(int argc, char *argv[])
48 {
49    START_USAGE(usage)
50    " \n ExtractOverlays :\n                                                   ",
51    " Extract overlay images from all DICOM image within a directory           ",
52    "          Warning : probably segfaults if no overlay                      ",
53    " usage: ExtractOverlays dirin=inputDirectoryName  [debug]                 ",
54    "        debug    : developper wants to run the program in 'debug mode'    ",
55    FINISH_USAGE
56
57    // ----- Initialize Arguments Manager ------
58    
59    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
60
61    if (argc == 1 || am->ArgMgrDefined("usage"))
62    {
63       am->ArgMgrUsage(usage); // Display 'usage'
64       delete am;
65       return 0;
66    }
67    
68    const char *dirIn  = am->ArgMgrWantString("dirin", usage);
69
70    if (am->ArgMgrDefined("debug"))
71       GDCM_NAME_SPACE::Debug::DebugOn();
72       
73    if (am->ArgMgrDefined("warning"))
74       GDCM_NAME_SPACE::Debug::WarningOn();
75       
76    // if unused Param we give up
77    if ( am->ArgMgrPrintUnusedLabels() )
78    {
79       am->ArgMgrUsage(usage);
80       delete am;
81       return 0;
82    }
83
84    delete am;  // we don't need Argument Manager any longer
85
86
87    // ========================== Now, we can do the job! ================ 
88
89
90    // ======================== more checking on the params ==============
91
92    if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirIn) )
93    {
94       std::cout << "KO : [" << dirIn << "] is not a Directory." << std::endl;
95       return 0;
96
97    }
98  
99    char outputFileName[1024]; // Hope it's enough for a file name!
100    
101    GDCM_NAME_SPACE::File *f;
102         
103    GDCM_NAME_SPACE::DirList dirList(dirIn,true); // gets (recursively) the file list
104    GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
105    for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
106                                  it != fileList.end();
107                                  ++it )
108    {
109    //   Just to see *all* the file names:
110    //   std::cout << "file [" << it->c_str() << "]" << std::endl;     
111    
112    //   Read the input file.
113
114    f = GDCM_NAME_SPACE::File::New(  );
115    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
116    f->SetFileName( it->c_str() );
117    
118    f->AddForceLoadElement(0x6000,0x3000);  // Overlay Data
119    f->AddForceLoadElement(0x6002,0x3000); 
120    f->AddForceLoadElement(0x6004,0x3000); 
121    f->AddForceLoadElement(0x6006,0x3000);    
122    f->AddForceLoadElement(0x6008,0x3000);    
123    f->AddForceLoadElement(0x600a,0x3000); 
124    f->AddForceLoadElement(0x600c,0x3000); 
125    f->AddForceLoadElement(0x600e,0x3000);
126              
127    int res = f->Load();
128
129    if ( !res )
130    {
131        std::cerr << "Sorry, " << it->c_str() <<"  not a gdcm-readable "
132                  << "DICOM / ACR File" <<std::endl;
133        f->Delete();
134        continue;
135    }
136    std::cout << " ... is readable " << std::endl;
137
138    // ============================================================
139    //   Load the Overlays in memory (the first one)
140    // ============================================================
141
142 /// \todo : deal with *each* overlay Data Element (not only the first one!)
143
144    uint16_t ovlyGroup = 0x6000;
145
146    GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(ovlyGroup, 0x3000);  
147    if (e == 0)
148    {
149       std::cout << " Image doesn't contain any Overlay " << std::endl;
150       f->Delete();
151       continue;
152    }   
153    std::cout << " File is read! " << std::endl;
154
155    uint8_t *overlay = (uint8_t *)(e->GetBinArea());
156    if ( overlay == 0 )
157    {
158        std::cerr << "Sorry, Overlays of" << it->c_str() <<"  are not "
159                  << " gdcm-readable."    << std::endl;
160        f->Delete();
161        continue;
162    }
163
164    // ============================================================
165    //  Image data generation.
166    // ============================================================   
167
168    unsigned int dimX= f->GetXSize();
169    unsigned int dimY= f->GetYSize();
170
171    unsigned int dimXY=dimX*dimY;
172    std::cout << "DimX : "<< dimX <<" DimY : " << dimY 
173              << " DimXY : " <<dimXY << std::endl;
174    unsigned char *outputData = new unsigned char[dimXY];
175
176    unsigned char *result=outputData;
177    for (unsigned int i=0;i<(dimXY/8);i++) 
178    {
179       explodeByte(overlay[i], result);
180       result+=8;
181    }
182    
183    // ============================================================
184    //   Write a new file
185    // ============================================================
186    GDCM_NAME_SPACE::File *f2;
187    f2 = GDCM_NAME_SPACE::File::New(  );
188    
189    char temp[256];
190    
191    sprintf(temp,"%d ",dimX);
192    f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
193    sprintf(temp,"%d ",dimY);
194    f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
195
196    f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
197    f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
198    f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
199    f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
200    f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
201    f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");  
202    
203    sprintf(outputFileName, "%s.ovly.dcm",it->c_str() );
204
205    // feel free to add any field (Dicom Data Entry) you like, here.
206    // ...
207    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f2);
208        
209    fh->SetImageData(outputData,dimXY);
210    fh->WriteDcmExplVR(outputFileName);
211    std::cout <<"End WriteOverlayImage" << std::endl;
212
213    delete outputData;
214    f->Delete();
215    f2->Delete();  
216    fh->Delete();
217    
218 }  // end of loop on files
219  
220    return 0;
221 }
222