]> Creatis software - gdcm.git/blob - Example/exExtractOverlaysDCM.cxx
Some cleaning in examples for extracting Overlays
[gdcm.git] / Example / exExtractOverlaysDCM.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exExtractOverlaysDCM.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/09/12 10:43:47 $
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
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    if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
115        std::cerr << "Deal with [" << it->c_str()
116                  << "] File" <<std::endl;
117
118    f = GDCM_NAME_SPACE::File::New(  );
119    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
120    f->SetFileName( it->c_str() );
121    
122    f->AddForceLoadElement(0x6000,0x3000);  // Overlay Data
123    f->AddForceLoadElement(0x6002,0x3000); 
124    f->AddForceLoadElement(0x6004,0x3000); 
125    f->AddForceLoadElement(0x6006,0x3000);    
126    f->AddForceLoadElement(0x6008,0x3000);    
127    f->AddForceLoadElement(0x600a,0x3000); 
128    f->AddForceLoadElement(0x600c,0x3000); 
129    f->AddForceLoadElement(0x600e,0x3000);
130    f->AddForceLoadElement(0x6010,0x3000);
131    f->AddForceLoadElement(0x6012,0x3000);             
132    f->AddForceLoadElement(0x6014,0x3000);             
133    f->AddForceLoadElement(0x6016,0x3000); 
134    f->AddForceLoadElement(0x6018,0x3000); 
135    f->AddForceLoadElement(0x601a,0x3000);                
136    f->AddForceLoadElement(0x601c,0x3000); 
137    f->AddForceLoadElement(0x601e,0x3000); // Hope it's enought : Dicom says 60xx ...
138    
139    int res = f->Load();
140
141    if ( !res )
142    {
143        std::cerr << "Sorry, " << it->c_str() <<"  not a gdcm-readable "
144                  << "DICOM / ACR File" <<std::endl;
145        f->Delete();
146        continue;
147    }
148
149    if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
150       std::cout << " ... is readable " << std::endl;
151
152    // ============================================================
153    //   Load Overlay info in memory 
154    // ============================================================
155
156 /// \todo : deal with *each* overlay Data Element (not only the first one!)
157
158    uint16_t ovlyGroup = 0x6000;
159    
160    for (int k=0; k<32; k+=2)
161    {
162
163    GDCM_NAME_SPACE::DataEntry *e10 = f->GetDataEntry(ovlyGroup+k, 0x0010); // nb Row Ovly
164    if (e10 == 0)
165    {
166       if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )   
167          std::cout << " Image doesn't contain Overlay on " <<std::hex
168                    << ovlyGroup+k << std::endl;
169       continue;
170    }
171       
172     // ============================================================
173    //  Image data preparation 
174    // ============================================================   
175
176    unsigned int dimX= f->GetXSize();
177    unsigned int dimY= f->GetYSize();
178    unsigned int dimXY=dimX*dimY;
179
180    unsigned char *outputData = new unsigned char[dimXY];
181       
182      
183    GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(ovlyGroup+k, 0x3000);  
184    if (e == 0)
185    {
186       if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
187          std::cout << " Image doesn't contain DICOM Overlay Data " <<std::hex
188                    << ovlyGroup+k << std::endl;
189       
190       // ============================================================
191       //  DICOM Overlay Image data generation
192       // ============================================================ 
193
194      GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);    
195      uint8_t *pixelData= fh->GetImageDataRaw();
196      
197      if (pixelData == 0)
198       {
199           std::cerr << "Sorry, Pixel Data of [" << it->c_str() <<"] are not "
200                     << " gdcm-readable."    << std::endl;
201           continue;
202       }     
203              
204    }   
205    else
206    {
207       uint8_t *overlay = (uint8_t *)(e->GetBinArea());
208       if ( overlay == 0 )
209       {
210           std::cerr << "Sorry, Overlays of [" << it->c_str() <<"] are not "
211                     << " gdcm-readable."    << std::endl;
212           continue;
213       }
214       if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )      
215          std::cout << " Overlay on group [" << std::hex << ovlyGroup+k << "] is read! " << std::endl;
216       
217       // ============================================================
218       //  DICOM Overlay Image data generation
219       // ============================================================   
220
221
222       unsigned char *result=outputData;
223       for (unsigned int i=0;i<(dimXY/8);i++)
224       {
225          explodeByte(overlay[i], result);
226          result+=8;
227       }
228    }
229    // ============================================================
230    //   Write a new file
231    // ============================================================
232    
233    GDCM_NAME_SPACE::File *f2;
234    f2 = GDCM_NAME_SPACE::File::New(  );
235    GDCM_NAME_SPACE::FileHelper *fh2 = GDCM_NAME_SPACE::FileHelper::New(f2);
236       
237    char temp[256];
238    
239    sprintf(temp,"%d ",dimX);
240    f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
241    sprintf(temp,"%d ",dimY);
242    f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
243    f2->InsertEntryString("8",0x0028,0x0100, "US");  // Bits Allocated
244    f2->InsertEntryString("8",0x0028,0x0101, "US");  // Bits Stored
245    f2->InsertEntryString("7",0x0028,0x0102, "US");  // High Bit
246    f2->InsertEntryString("0",0x0028,0x0103, "US");  // Pixel Representation
247    f2->InsertEntryString("1",0x0028,0x0002, "US");  // Samples per Pixel
248    f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");  
249
250    // feel free to add any field (Dicom Data Entry) you like, here.
251    // ...
252   
253    sprintf(outputFileName, "../%s.ovly.%04x.dcm",it->c_str(), ovlyGroup+k);
254        
255    fh2->SetImageData(outputData,dimXY);
256    fh2->WriteDcmExplVR(outputFileName);
257
258    std::cout <<"File written successfully [" << outputFileName << "]" <<std::endl;
259
260    delete outputData;
261    f2->Delete();  
262    fh2->Delete(); 
263    
264  } // end on loop on 60xx
265  
266    f->Delete();
267
268      
269 }  // end of loop on files ( DirListType::iterator )
270  
271    return 0;
272 }
273