1 /*=========================================================================
4 Module: $RCSfile: exExtractOverlaysACR.cxx,v $
6 Date: $Date: 2007/09/12 10:43:47 $
7 Version: $Revision: 1.1 $
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"
24 #include <stdio.h> // for fseek... FIXME
25 #include <stdlib.h> // for atoi
27 #include "gdcmArgMgr.h"
30 // unfinished : DO NOT to be used as is !
33 // Example (sorry, we've got no more than this one ...)
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)
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)
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)
61 int main(int argc, char *argv[])
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' ",
70 // ----- Initialize Arguments Manager ------
72 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
74 if (argc == 1 || am->ArgMgrDefined("usage"))
76 am->ArgMgrUsage(usage); // Display 'usage'
81 // "SIEMENS_GBS_III-16-ACR_NEMA_1.acr"
83 const char *fileName = am->ArgMgrWantString("filein", usage);
85 if (am->ArgMgrDefined("debug"))
86 GDCM_NAME_SPACE::Debug::DebugOn();
88 if (am->ArgMgrDefined("warning"))
89 GDCM_NAME_SPACE::Debug::WarningOn();
91 // if unused Param we give up
92 if ( am->ArgMgrPrintUnusedLabels() )
94 am->ArgMgrUsage(usage);
99 delete am; // we don't need Argument Manager any longer
102 // ========================== Now, we can do the job! ================
104 GDCM_NAME_SPACE::File *f;
107 // ============================================================
108 // Read the input image.
109 // ============================================================
111 //std::cout << argv[1] << std::endl;
113 f = GDCM_NAME_SPACE::File::New( );
115 f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
116 f->SetFileName( fileName );
117 bool res = f->Load();
119 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
121 std::cout << "---------------------------------------------" << std::endl;
123 std::cout << "---------------------------------------------" << std::endl;
126 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
127 << "DICOM / ACR File"
132 std::cout << " ... is readable " << std::endl;
134 // ============================================================
135 // Check whether image contains Overlays ACR-NEMA style.
136 // ============================================================
138 int bitsAllocated = f->GetBitsAllocated();
139 if ( bitsAllocated <= 8 )
141 std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
145 std::string s1 = f->GetEntryString(0x6000, 0x0102);
146 if (s1 == GDCM_NAME_SPACE::GDCM_UNFOUND)
148 std::cout << " Image doesn't contain any Overlay " << std::endl;
152 std::cout << " File is read! " << std::endl;
155 // ============================================================
156 // Load the pixels in memory.
157 // ============================================================
159 int nx = f->GetXSize();
160 int ny = f->GetYSize();
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();
167 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
168 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
170 // ============================================================
172 // ============================================================
174 uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
176 uint16_t currentOvlGroup = 0x6000;
177 std::string strOvlBitPosition;
181 uint16_t overlayLocation;
182 std::ostringstream str;
184 std::string strOverlayLocation;
186 GDCM_NAME_SPACE::File *fileToBuild = 0;
187 GDCM_NAME_SPACE::FileHelper *fh = 0;
189 // ============================================================
190 // Get each overlay Bit into an image
191 // ============================================================
192 for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)
194 if ( (strOvlBitPosition = f->GetEntryString(currentOvlGroup, 0x0102))
195 == GDCM_NAME_SPACE::GDCM_UNFOUND )
198 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
199 std::cout << "Current Overlay Group " << std::hex << currentOvlGroup
200 << " OvlBitPosition " << strOvlBitPosition << std::endl;
202 strOverlayLocation = f->GetEntryString(currentOvlGroup, 0x0200);
203 if ( strOverlayLocation != GDCM_NAME_SPACE::GDCM_UNFOUND )
205 overlayLocation = atoi(strOverlayLocation.c_str());
206 if ( overlayLocation != f->GetGrPixel() )
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.
216 ovlBitPosition = atoi(strOvlBitPosition.c_str());
217 mask = 1 << ovlBitPosition;
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++)
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]);
227 if ( (pixels[j] & mask) == 0 )
232 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
233 std::cout << "About to built empty file" << std::endl;
235 fileToBuild = GDCM_NAME_SPACE::File::New();
237 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
238 std::cout << "Finish to built empty file" << std::endl;
242 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
245 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
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
253 fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
254 // Other mandatory fields will be set automatically,
255 // just before Write(), by FileHelper::CheckMandatoryElements()
257 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
258 std::cout << "-------------About to built FileHelper" << std::endl;
260 fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
262 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
263 std::cout << "-------------Finish to built FileHelper" << std::endl;
265 fh->SetImageData(tabPixels,nx*ny);
266 fh->SetWriteTypeToDcmExplVR();
268 std::ostringstream tmp;
270 tmp <<currentOvlGroup;
273 // -> Why doesn't it work ?!?
274 //str << fileName << std::hex << currentOvlGroup << ".dcm" << std::ends;
276 str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
278 // Write the current 'overlay' file
280 if( !fh->Write(str.str()) )
282 std::cout << "Failed\n"
283 << "File [" << str.str() << "] is unwrittable" << std::endl;
287 fileToBuild->Delete();
295 std::cout << "File written successfully [" << str.str() << "]" << std::endl;
302 fileToBuild->Delete();