1 /*=========================================================================
4 Module: $RCSfile: exExtractOverlaysACR.cxx,v $
6 Date: $Date: 2007/09/14 08:23:34 $
7 Version: $Revision: 1.2 $
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 " usage: exExtractOverlaysACR filein=inputFileName [debug] ",
64 " debug : developper wants to run the program in 'debug mode' ",
67 // ----- Initialize Arguments Manager ------
69 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
71 if (argc == 1 || am->ArgMgrDefined("usage"))
73 am->ArgMgrUsage(usage); // Display 'usage'
78 const char *fileName = am->ArgMgrWantString("filein", usage);
80 if (am->ArgMgrDefined("debug"))
81 GDCM_NAME_SPACE::Debug::DebugOn();
83 if (am->ArgMgrDefined("warning"))
84 GDCM_NAME_SPACE::Debug::WarningOn();
86 // if unused Param we give up
87 if ( am->ArgMgrPrintUnusedLabels() )
89 am->ArgMgrUsage(usage);
94 delete am; // we don't need Argument Manager any longer
96 // ========================== Now, we can do the job! ================
98 GDCM_NAME_SPACE::File *f;
100 // ============================================================
101 // Read the input image.
102 // ============================================================
104 f = GDCM_NAME_SPACE::File::New( );
106 f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
107 f->SetFileName( fileName );
108 bool res = f->Load();
112 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
113 << "DICOM / ACR File"
118 std::cout << fileName << " ... is readable " << std::endl;
120 // ============================================================
121 // Check whether image contains Overlays ACR-NEMA style.
122 // ============================================================
124 int bitsAllocated = f->GetBitsAllocated();
125 if ( bitsAllocated <= 8 )
127 std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
131 std::string s1 = f->GetEntryString(0x6000, 0x0102);
132 if (s1 == GDCM_NAME_SPACE::GDCM_UNFOUND)
134 std::cout << " Image doesn't contain any Overlay " << std::endl;
138 std::cout << fileName << " is read! " << std::endl;
141 // ============================================================
142 // Load the pixels in memory.
143 // ============================================================
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();
150 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
151 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
153 // ============================================================
155 // ============================================================
157 unsigned int nx = f->GetXSize();
158 unsigned int ny = f->GetYSize();
159 unsigned int nxy=nx*ny;
160 uint16_t currentOvlGroup;
163 std::ostringstream str;
165 uint8_t *outputData = new uint8_t[nxy]; // uint8 is enought to hold 1 bit !
167 std::string strOvlBitPosition;
170 uint16_t overlayLocation;
171 std::string strOverlayLocation;
173 GDCM_NAME_SPACE::File *fileToBuild = 0;
174 GDCM_NAME_SPACE::FileHelper *fh = 0;
176 // ============================================================
177 // Get each overlay Bit into the image
178 // ============================================================
179 for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)
181 if ( (strOvlBitPosition = f->GetEntryString(currentOvlGroup, 0x0102))
182 == GDCM_NAME_SPACE::GDCM_UNFOUND )
185 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
186 std::cout << "Current Overlay Group " << std::hex << currentOvlGroup
187 << " OvlBitPosition " << strOvlBitPosition << std::endl;
189 strOverlayLocation = f->GetEntryString(currentOvlGroup, 0x0200);
190 if ( strOverlayLocation != GDCM_NAME_SPACE::GDCM_UNFOUND )
192 overlayLocation = atoi(strOverlayLocation.c_str());
193 if ( overlayLocation != f->GetGrPixel() )
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.
204 // ============================================================
205 // DICOM Overlay Image data generation
206 // ============================================================
208 ovlBitPosition = atoi(strOvlBitPosition.c_str());
209 mask = 1 << ovlBitPosition;
211 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
212 std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;
213 for (int j=0; j<nxy; j++)
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]);
219 if ( (pixels[j] & mask) == 0 )
224 // ============================================================
226 // ============================================================
228 fileToBuild = GDCM_NAME_SPACE::File::New();
231 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
234 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
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");
243 // feel free to add any field (Dicom Data Entry) you like, here.
245 // Other mandatory fields will be set automatically,
246 // just before Write(), by FileHelper::CheckMandatoryElements()
248 fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
250 fh->SetImageData(outputData,nx*ny);
251 fh->SetWriteTypeToDcmExplVR();
253 std::ostringstream tmp;
255 tmp <<currentOvlGroup;
258 // -> Why doesn't it work ?!?
259 //str << fileName << std::hex << currentOvlGroup << ".dcm" << std::ends;
261 str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
263 // Write the current 'overlay' file
265 if( !fh->Write(str.str()) )
267 std::cout << "Failed\n"
268 << "File [" << str.str() << "] is unwrittable" << std::endl;
272 std::cout << "File written successfully [" << str.str() << "]" << std::endl;
275 } // end on loop on 60xx
280 fileToBuild->Delete();