1 /*=========================================================================
4 Module: $RCSfile: exExtractOverlaysDCM.cxx,v $
6 Date: $Date: 2007/10/19 15:06:08 $
7 Version: $Revision: 1.3 $
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 "gdcmDataEntry.h"
23 #include "gdcmDirList.h"
24 #include "gdcmArgMgr.h"
28 // Each BIT of Overlay Data (0x6000,0x3000) corresponds
29 // to a BYTE of overlay image.
30 void explodeByte(unsigned char byte, unsigned char* result)
32 unsigned char mask = 1;
44 int main(int argc, char *argv[])
47 " \n ExtractOverlays :\n ",
48 " Extract DICOM style overlays from an image ",
49 " usage: exExtractOverlaysDCM filein=inputFileName [debug] ",
50 " debug : developper wants to run the program in 'debug mode' ",
53 // ----- Initialize Arguments Manager ------
55 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
57 if (argc == 1 || am->ArgMgrDefined("usage"))
59 am->ArgMgrUsage(usage); // Display 'usage'
64 const char *fileName = am->ArgMgrWantString("filein", usage);
66 if (am->ArgMgrDefined("debug"))
67 GDCM_NAME_SPACE::Debug::DebugOn();
69 if (am->ArgMgrDefined("warning"))
70 GDCM_NAME_SPACE::Debug::WarningOn();
72 // if unused Param we give up
73 if ( am->ArgMgrPrintUnusedLabels() )
75 am->ArgMgrUsage(usage);
80 delete am; // we don't need Argument Manager any longer
82 // ========================== Now, we can do the job! ================
84 GDCM_NAME_SPACE::File *f;
86 // ============================================================
87 // Read the input image.
88 // ============================================================
90 f = GDCM_NAME_SPACE::File::New( );
92 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
93 f->AddForceLoadElement(0x6002,0x3000);
94 f->AddForceLoadElement(0x6004,0x3000);
95 f->AddForceLoadElement(0x6006,0x3000);
96 f->AddForceLoadElement(0x6008,0x3000);
97 f->AddForceLoadElement(0x600a,0x3000);
98 f->AddForceLoadElement(0x600c,0x3000);
99 f->AddForceLoadElement(0x600e,0x3000);
100 f->AddForceLoadElement(0x6010,0x3000);
101 f->AddForceLoadElement(0x6012,0x3000);
102 f->AddForceLoadElement(0x6014,0x3000);
103 f->AddForceLoadElement(0x6016,0x3000);
104 f->AddForceLoadElement(0x6018,0x3000);
105 f->AddForceLoadElement(0x601a,0x3000);
106 f->AddForceLoadElement(0x601c,0x3000);
107 f->AddForceLoadElement(0x601e,0x3000); // Hope it's enought : Dicom says 60xx ...
109 f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
110 f->SetFileName( fileName );
111 bool res = f->Load();
114 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
115 << "DICOM / ACR File"
120 std::cout << fileName << " ... is readable " << std::endl;
122 // ============================================================
123 // Check whether image contains Overlays DICOM style.
124 // ============================================================
126 unsigned int nx = f->GetXSize();
127 unsigned int ny = f->GetYSize();
128 unsigned int nxy=nx*ny;
129 uint16_t currentOvlGroup;
132 std::ostringstream str;
134 uint8_t *outputData = new uint8_t[nxy]; // uint8 is enought to hold 1 bit !
136 GDCM_NAME_SPACE::File *fileToBuild = 0;
137 GDCM_NAME_SPACE::FileHelper *fh = 0;
139 // ============================================================
141 // ============================================================
142 for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)
144 GDCM_NAME_SPACE::DataEntry *e10 = f->GetDataEntry(currentOvlGroup, 0x0010); // nb Row Ovly
147 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
148 std::cout << " Image doesn't contain Overlay on " <<std::hex
149 << currentOvlGroup+i << std::endl;
153 GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(currentOvlGroup, 0x3000);
156 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
157 std::cout << " Image doesn't contain DICOM Overlay Data " <<std::hex
158 << currentOvlGroup+i << std::endl;
162 uint8_t *overlay = (uint8_t *)(e->GetBinArea());
165 std::cerr << "Sorry, Overlays of [" << fileName << "] are not "
166 << " gdcm-readable." << std::endl;
169 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
170 std::cout << " Overlay on group [" << std::hex << currentOvlGroup<< "] is read! " << std::endl;
172 // ============================================================
173 // DICOM Overlay Image data generation
174 // ============================================================
176 unsigned char *result=outputData;
177 for (unsigned int i=0;i<(nxy/8);i++)
179 explodeByte(overlay[i], result);
183 // ============================================================
185 // ============================================================
187 fileToBuild = GDCM_NAME_SPACE::File::New();
190 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
193 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
195 fileToBuild->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
196 fileToBuild->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
197 fileToBuild->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
198 fileToBuild->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
199 fileToBuild->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
200 fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");
202 // feel free to add any field (Dicom Data Entry) you like, here.
204 // Other mandatory fields will be set automatically,
205 // just before Write(), by FileHelper::CheckMandatoryElements()
207 fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
209 fh->SetImageData(outputData,nx*ny);
210 fh->SetWriteTypeToDcmExplVR();
212 std::ostringstream tmp;
214 tmp <<currentOvlGroup;
217 // -> Why doesn't it work ?!?
218 //str << fileName << std::hex << currentOvlGroup << ".dcm" << std::ends;
220 str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
222 // Write the current 'overlay' file
224 if( !fh->Write(str.str()) )
226 std::cout << "Failed\n"
227 << "File [" << str.str() << "] is unwrittable" << std::endl;
231 std::cout << "File written successfully [" << str.str() << "]" << std::endl;
234 } // end on loop on 60xx
239 fileToBuild->Delete();