1 /*=========================================================================
4 Module: $RCSfile: exExtractOverlaysDCM.cxx,v $
6 Date: $Date: 2009/05/28 15:44:34 $
7 Version: $Revision: 1.6 $
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 (0x60xx,0x3000) corresponds
29 // to a BYTE of overlay image.
31 void explodeByte(unsigned char byte, unsigned char* result)
33 unsigned char mask = 1;
45 int main(int argc, char *argv[])
48 " \n exExtractOverlaysDCM :\n ",
49 " Extract DICOM style overlays from an image ",
50 " Resulting image name(s) are postpended with .ovly.dcm ",
51 " usage: exExtractOverlaysDCM filein=inputFileName [debug] [warning] ",
52 " warning : user wants to run the program in 'warning mode' ",
53 " debug : developper wants to run the program in 'debug mode' ",
56 // ----- Initialize Arguments Manager ------
58 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
60 if (argc == 1 || am->ArgMgrDefined("usage"))
62 am->ArgMgrUsage(usage); // Display 'usage'
67 const char *fileName = am->ArgMgrWantString("filein", usage);
69 if (am->ArgMgrDefined("debug"))
70 GDCM_NAME_SPACE::Debug::DebugOn();
72 if (am->ArgMgrDefined("warning"))
73 GDCM_NAME_SPACE::Debug::WarningOn();
75 // if unused Param we give up
76 if ( am->ArgMgrPrintUnusedLabels() )
78 am->ArgMgrUsage(usage);
83 delete am; // we don't need Argument Manager any longer
85 // ========================== Now, we can do the job! ================
87 GDCM_NAME_SPACE::File *f;
89 // ============================================================
90 // Read the input image.
91 // ============================================================
93 f = GDCM_NAME_SPACE::File::New( );
95 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
96 f->AddForceLoadElement(0x6002,0x3000);
97 f->AddForceLoadElement(0x6004,0x3000);
98 f->AddForceLoadElement(0x6006,0x3000);
99 f->AddForceLoadElement(0x6008,0x3000);
100 f->AddForceLoadElement(0x600a,0x3000);
101 f->AddForceLoadElement(0x600c,0x3000);
102 f->AddForceLoadElement(0x600e,0x3000);
103 f->AddForceLoadElement(0x6010,0x3000);
104 f->AddForceLoadElement(0x6012,0x3000);
105 f->AddForceLoadElement(0x6014,0x3000);
106 f->AddForceLoadElement(0x6016,0x3000);
107 f->AddForceLoadElement(0x6018,0x3000);
108 f->AddForceLoadElement(0x601a,0x3000);
109 f->AddForceLoadElement(0x601c,0x3000);
110 f->AddForceLoadElement(0x601e,0x3000); // Hope it's enought : Dicom says 60xx ...
112 f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
113 f->SetFileName( fileName );
114 bool res = f->Load();
117 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
118 << "DICOM / ACR File"
123 std::cout << fileName << " ... is readable " << std::endl;
125 // ============================================================
126 // Check whether image contains Overlays DICOM style.
127 // ============================================================
129 unsigned int nx = f->GetXSize();
130 unsigned int ny = f->GetYSize();
131 unsigned int nxy=nx*ny;
132 uint16_t currentOvlGroup;
135 std::ostringstream str;
137 uint8_t *outputData = new uint8_t[nxy]; // uint8 is enought to hold 1 bit !
139 GDCM_NAME_SPACE::File *fileToBuild = 0;
140 GDCM_NAME_SPACE::FileHelper *fh = 0;
142 // ============================================================
143 // Get each overlay group into the image header
144 // ============================================================
145 for(i=0, currentOvlGroup=0x6000; i<32; i+=2 ,currentOvlGroup+=2)
147 GDCM_NAME_SPACE::DataEntry *e10 = f->GetDataEntry(currentOvlGroup, 0x0010); // nb Row Ovly
150 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
151 std::cout << " Image doesn't contain Overlay on " <<std::hex
152 << currentOvlGroup+i << std::endl;
156 GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(currentOvlGroup, 0x3000);
159 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
160 std::cout << " Image doesn't contain DICOM Overlay Data " <<std::hex
161 << currentOvlGroup+i << std::endl;
165 uint8_t *overlay = (uint8_t *)(e->GetBinArea());
168 std::cerr << "Sorry, Overlays of [" << fileName << "] are not "
169 << " gdcm-readable." << std::endl;
172 if( GDCM_NAME_SPACE::Debug::GetWarningFlag() )
173 std::cout << " Overlay on group [" << std::hex << currentOvlGroup<< "] is read! " << std::endl;
175 // ============================================================
176 // DICOM Overlay Image data generation
177 // ============================================================
179 unsigned char *result=outputData;
180 for (unsigned int i2=0;i2<(nxy/8);i2++)
182 explodeByte(overlay[i2], result);
186 // ============================================================
188 // ============================================================
190 fileToBuild = GDCM_NAME_SPACE::File::New();
193 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
196 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
198 fileToBuild->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
199 fileToBuild->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
200 fileToBuild->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
201 fileToBuild->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
202 fileToBuild->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
203 fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");
205 // feel free to add any field (Dicom Data Entry) you like, here.
207 // Other mandatory fields will be set automatically,
208 // just before Write(), by FileHelper::CheckMandatoryElements()
210 fh = GDCM_NAME_SPACE::FileHelper::New(fileToBuild);
212 fh->SetImageData(outputData,nx*ny);
213 fh->SetWriteTypeToDcmExplVR();
215 std::ostringstream tmp;
217 tmp <<currentOvlGroup;
220 // -> Why doesn't it work ?!?
221 //str << fileName << std::hex << currentOvlGroup << ".dcm" << std::ends;
223 str << fileName << ".ovly." << tmp.str() << ".dcm" << std::ends;
225 // Write the current 'overlay' file
227 if( !fh->Write(str.str()) )
229 std::cout << "Failed\n"
230 << "File [" << str.str() << "] is unwrittable" << std::endl;
234 std::cout << "File written successfully [" << str.str() << "]" << std::endl;
237 } // end on loop on 60xx
242 fileToBuild->Delete();