1 /*=========================================================================
4 Module: $RCSfile: exExtractOverlaysACR.cxx,v $
6 Date: $Date: 2007/10/30 09:15:57 $
7 Version: $Revision: 1.4 $
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.
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.
17 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23 #include "gdcmArgMgr.h"
26 #include <stdlib.h> // for atoi
28 /* SIEMENS_GBS_III-16-ACR_NEMA_1.acr
30 // Example (sorry, we've got no more than this one ...)
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)
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)
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)
58 int main(int argc, char *argv[])
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' ",
69 // ----- Initialize Arguments Manager ------
71 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
73 if (argc == 1 || am->ArgMgrDefined("usage"))
75 am->ArgMgrUsage(usage); // Display 'usage'
80 const char *fileName = am->ArgMgrWantString("filein", usage);
82 if (am->ArgMgrDefined("debug"))
83 GDCM_NAME_SPACE::Debug::DebugOn();
85 if (am->ArgMgrDefined("warning"))
86 GDCM_NAME_SPACE::Debug::WarningOn();
88 // if unused Param we give up
89 if ( am->ArgMgrPrintUnusedLabels() )
91 am->ArgMgrUsage(usage);
96 delete am; // we don't need Argument Manager any longer
98 // ========================== Now, we can do the job! ================
100 GDCM_NAME_SPACE::File *f;
102 // ============================================================
103 // Read the input image.
104 // ============================================================
106 f = GDCM_NAME_SPACE::File::New( );
108 f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
109 f->SetFileName( fileName );
110 bool res = f->Load();
113 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
114 << "DICOM / ACR File"
119 std::cout << fileName << " ... is readable " << std::endl;
121 // ============================================================
122 // Check whether image contains Overlays ACR-NEMA style.
123 // ============================================================
125 int bitsAllocated = f->GetBitsAllocated();
126 if ( bitsAllocated <= 8 )
128 std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
132 std::string s1 = f->GetEntryString(0x6000, 0x0102);
133 if (s1 == GDCM_NAME_SPACE::GDCM_UNFOUND)
135 std::cout << " Image doesn't contain any Overlay " << std::endl;
139 std::cout << fileName << " is read! " << std::endl;
142 // ============================================================
143 // Load the pixels in memory.
144 // ============================================================
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();
151 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
152 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
154 // ============================================================
156 // ============================================================
158 unsigned int nx = f->GetXSize();
159 unsigned int ny = f->GetYSize();
160 unsigned int nxy=nx*ny;
161 uint16_t currentOvlGroup;
164 std::ostringstream str;
166 uint8_t *outputData = new uint8_t[nxy]; // uint8 is enought to hold 1 bit !
168 std::string strOvlBitPosition;
171 uint16_t overlayLocation;
172 std::string strOverlayLocation;
174 GDCM_NAME_SPACE::File *fileToBuild = 0;
175 GDCM_NAME_SPACE::FileHelper *fh = 0;
177 // ============================================================
178 // Get each overlay Bit into the image
179 // ============================================================
180 for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)
182 if ( (strOvlBitPosition = f->GetEntryString(currentOvlGroup, 0x0102))
183 == GDCM_NAME_SPACE::GDCM_UNFOUND )
186 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
187 std::cout << "Current Overlay Group " << std::hex << currentOvlGroup
188 << " OvlBitPosition " << strOvlBitPosition << std::endl;
190 strOverlayLocation = f->GetEntryString(currentOvlGroup, 0x0200);
191 if ( strOverlayLocation != GDCM_NAME_SPACE::GDCM_UNFOUND )
193 overlayLocation = atoi(strOverlayLocation.c_str());
194 if ( overlayLocation != f->GetGrPixel() )
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.
205 // ============================================================
206 // DICOM Overlay Image data generation
207 // ============================================================
209 ovlBitPosition = atoi(strOvlBitPosition.c_str());
210 mask = 1 << ovlBitPosition;
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++)
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]);
220 if ( (pixels[j] & mask) == 0 )
225 // ============================================================
227 // ============================================================
229 fileToBuild = GDCM_NAME_SPACE::File::New();
232 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
235 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
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");
244 // feel free to add any field (Dicom Data Entry) you like, here.
246 // Other mandatory fields will be set automatically,
247 // just before Write(), by FileHelper::CheckMandatoryElements()
249 fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
251 fh->SetImageData(outputData,nx*ny);
252 fh->SetWriteTypeToDcmExplVR();
254 std::ostringstream tmp;
256 tmp <<currentOvlGroup;
259 // -> Why doesn't it work ?!?
260 //str << fileName << std::hex << currentOvlGroup << ".dcm" << std::ends;
262 str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
264 // Write the current 'overlay' file
266 if( !fh->Write(str.str()) )
268 std::cout << "Failed\n"
269 << "File [" << str.str() << "] is unwrittable" << std::endl;
273 std::cout << "File written successfully [" << str.str() << "]" << std::endl;
276 } // end on loop on 60xx
281 fileToBuild->Delete();