]> Creatis software - gdcm.git/blob - Example/exExtractOverlaysACR.cxx
Some cleaning in examples for extracting Overlays
[gdcm.git] / Example / exExtractOverlaysACR.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exExtractOverlaysACR.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 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23 #include <iostream>
24 #include <stdio.h> // for fseek... FIXME
25 #include <stdlib.h> // for atoi
26
27 #include "gdcmArgMgr.h" 
28
29  // WARNING :
30  // unfinished : DO NOT to be used as is !
31  
32  /*
33  // Example (sorry, we've got no more than this one ...)
34  
35 V 0028|0010[US] [Rows] [256] x(100)
36 V 0028|0011[US] [Columns] [256] x(100)
37 V 0028|0030[DS] [Pixel Spacing] [01.56\1.56]
38 V 0028|0100[US] [Bits Allocated] [16] x(10)
39 V 0028|0101[US] [Bits Stored] [12] x(c)
40 V 0028|0102[US] [High Bit] [11] x(b)
41 V 0028|0103[US] [Pixel Representation] [0] x(0)
42  
43 V 6000|0000[UL] [Group Length] [96] x(60)
44 V 6000|0010[US] [Rows] [256] x(100)
45 V 6000|0011[US] [Columns] [256] x(100)
46 V 6000|0040[CS] [Overlay Type] [R ]
47 V 6000|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
48 V 6000|0100[US] [Overlay Bits Allocated] [16] x(10)
49 V 6000|0102[US] [Overlay Bit Position] [12] x(c)
50 ...
51 ...
52 V 6006|0000[UL] [Group Length] [96] x(60)
53 V 6006|0010[US] [Rows] [256] x(100)
54 V 6006|0011[US] [Columns] [256] x(100)
55 V 6006|0040[CS] [Overlay Type] [R ]
56 V 6006|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
57 V 6006|0100[US] [Overlay Bits Allocated] [16] x(10)
58 V 6006|0102[US] [Overlay Bit Position] [15] x(f)
59  */
60  
61 int main(int argc, char *argv[])
62
63    START_USAGE(usage)
64    " \n exExtractOverlaysACR :\n                                              ",
65    " Extract ACR-NEMA style overlays from an image                            ",
66    " usage: exExtractOverlaysACR filein=inputDirectoryName  [debug]           ",
67    "        debug    : developper wants to run the program in 'debug mode'    ",
68    FINISH_USAGE
69
70    // ----- Initialize Arguments Manager ------
71    
72    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
73
74    if (argc == 1 || am->ArgMgrDefined("usage"))
75    {
76       am->ArgMgrUsage(usage); // Display 'usage'
77       delete am;
78       return 0;
79    }
80    
81    // "SIEMENS_GBS_III-16-ACR_NEMA_1.acr"
82    
83    const char *fileName  = am->ArgMgrWantString("filein", usage);   
84
85    if (am->ArgMgrDefined("debug"))
86       GDCM_NAME_SPACE::Debug::DebugOn();
87       
88    if (am->ArgMgrDefined("warning"))
89       GDCM_NAME_SPACE::Debug::WarningOn();
90       
91    // if unused Param we give up
92    if ( am->ArgMgrPrintUnusedLabels() )
93    {
94       am->ArgMgrUsage(usage);
95       delete am;
96       return 0;
97    }
98
99    delete am;  // we don't need Argument Manager any longer
100
101
102    // ========================== Now, we can do the job! ================   
103  
104    GDCM_NAME_SPACE::File *f;
105
106  
107 // ============================================================
108 //   Read the input image.
109 // ============================================================
110
111    //std::cout << argv[1] << std::endl;
112
113    f = GDCM_NAME_SPACE::File::New( );
114
115    f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
116    f->SetFileName( fileName );
117    bool res = f->Load();  
118
119    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
120    {
121       std::cout << "---------------------------------------------" << std::endl;
122       f->Print();
123       std::cout << "---------------------------------------------" << std::endl;
124    }
125    if (!res) {
126        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
127            << "DICOM / ACR File"
128            <<std::endl;
129       f->Delete();
130       return 0;
131    }
132    std::cout << " ... is readable " << std::endl;
133
134 // ============================================================
135 //   Check whether image contains Overlays ACR-NEMA style.
136 // ============================================================
137
138    int bitsAllocated = f->GetBitsAllocated();
139    if ( bitsAllocated <= 8 )
140    {
141       std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
142       f->Delete();
143       return 0;
144    }
145    std::string s1 = f->GetEntryString(0x6000, 0x0102);
146    if (s1 == GDCM_NAME_SPACE::GDCM_UNFOUND)
147    {
148       std::cout << " Image doesn't contain any Overlay " << std::endl;
149       f->Delete();
150       return 0;
151    }
152    std::cout << " File is read! " << std::endl;
153
154    
155 // ============================================================
156 //   Load the pixels in memory.
157 // ============================================================
158
159    int nx = f->GetXSize();
160    int ny = f->GetYSize();
161
162    GDCM_NAME_SPACE::FileHelper *fh1 = GDCM_NAME_SPACE::FileHelper::New(f);
163    fh1->SetKeepOverlays(true);
164    uint16_t *pixels = (uint16_t *)fh1->GetImageDataRaw();
165    int lgt = fh1->GetImageDataRawSize();
166    
167    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
168       std::cout << "Pixels read as expected : length = " << lgt << std::endl;   
169    
170 // ============================================================
171 //   Prepare the stuff
172 // ============================================================
173                                          
174    uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
175    
176    uint16_t currentOvlGroup = 0x6000;
177    std::string strOvlBitPosition;
178    int ovlBitPosition;
179    uint16_t mask;
180    int i = 0;
181    uint16_t overlayLocation;
182    std::ostringstream str;
183
184    std::string strOverlayLocation;
185    
186    GDCM_NAME_SPACE::File *fileToBuild = 0;
187    GDCM_NAME_SPACE::FileHelper *fh = 0;
188
189 // ============================================================
190 //   Get each overlay Bit into an image
191 // ============================================================
192    for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)   
193    {
194       if ( (strOvlBitPosition = f->GetEntryString(currentOvlGroup, 0x0102)) 
195                                                  == GDCM_NAME_SPACE::GDCM_UNFOUND )
196           continue;
197   
198       if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
199          std::cout << "Current Overlay Group " << std::hex << currentOvlGroup 
200                    << " OvlBitPosition " << strOvlBitPosition << std::endl;
201       
202       strOverlayLocation = f->GetEntryString(currentOvlGroup, 0x0200);
203       if ( strOverlayLocation != GDCM_NAME_SPACE::GDCM_UNFOUND )
204       {
205          overlayLocation = atoi(strOverlayLocation.c_str());
206          if ( overlayLocation != f->GetGrPixel() )
207          {
208             std::cout << "Big Trouble : Overlays are NOT in the Pixels Group "
209                       << std::hex << "(" << overlayLocation << " vs " 
210                       << f->GetGrPixel() << std::endl;
211             // Actually, here, we should (try to) read the overlay location
212             // and go on the job.
213             continue;
214          }
215       }
216       ovlBitPosition = atoi(strOvlBitPosition.c_str());
217       mask = 1 << ovlBitPosition;
218        
219       if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )      
220          std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;          
221       for (int j=0; j<nx*ny ; j++)
222       {
223          if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
224             if (pixels[j] >= 0x1000)// if it contains at least one overlay bit
225                printf("%d : %04x\n",j, pixels[j]);
226
227          if ( (pixels[j] & mask) == 0 )
228             tabPixels[j] = 0;
229          else
230             tabPixels[j] = 128;
231       }
232       if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
233          std::cout << "About to built empty file"  << std::endl;
234
235       fileToBuild = GDCM_NAME_SPACE::File::New();
236
237       if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
238          std::cout << "Finish to built empty file"  << std::endl;
239
240       str.str("");
241       str << nx;
242       fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
243       str.str("");
244       str << ny;
245       fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
246
247       fileToBuild->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
248       fileToBuild->InsertEntryString("8",0x0028,0x0101); // Bits Stored
249       fileToBuild->InsertEntryString("7",0x0028,0x0102); // High Bit
250       fileToBuild->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
251       fileToBuild->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
252
253       fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
254       // Other mandatory fields will be set automatically,
255       // just before Write(), by FileHelper::CheckMandatoryElements()
256
257       if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
258          std::cout << "-------------About to built FileHelper"  << std::endl;
259
260       fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
261
262       if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
263          std::cout << "-------------Finish to built FileHelper"  << std::endl;
264
265       fh->SetImageData(tabPixels,nx*ny);
266       fh->SetWriteTypeToDcmExplVR();
267
268 std::ostringstream tmp;
269 tmp <<std::hex;
270 tmp <<currentOvlGroup;
271
272       str.str("");
273 // -> Why doesn't it work ?!?
274       //str << fileName << std::hex << currentOvlGroup << ".dcm" << std::ends;
275
276 str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
277
278       //   Write the current 'overlay' file
279
280       if( !fh->Write(str.str()) )
281       {
282          std::cout << "Failed\n"
283                    << "File [" << str.str() << "] is unwrittable" << std::endl;
284          /*
285          fh->Delete();
286          if (fileToBuild)
287             fileToBuild->Delete();
288          delete pixels;
289          delete tabPixels;
290          return 0;
291         */
292       }
293       else
294       {
295          std::cout << "File written successfully [" << str.str()  << "]" << std::endl;
296       }
297    }
298     
299    if (f)
300       fh->Delete();
301    if (fileToBuild)
302       fileToBuild->Delete();
303    f->Delete();
304    delete pixels;
305    delete tabPixels;
306
307    return 0;
308 }
309