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