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