1 /*=========================================================================
4 Module: $RCSfile: ExtractOverlays.cxx,v $
6 Date: $Date: 2007/03/16 16:03:04 $
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 =========================================================================*/
21 #include "gdcmFileHelper.h"
22 #include "gdcmCommon.h"
23 #include "gdcmDebug.h"
24 #include "gdcmDataEntry.h"
25 #include "gdcmDirList.h"
27 #include "gdcmArgMgr.h"
30 // Each BIT of Overlay Data (0x6000,0x3000) corresponds
31 // to a BYTE of overlay image.
32 void explodeByte(unsigned char byte, unsigned char* result)
34 unsigned char mask = 1;
47 int main(int argc, char *argv[])
50 " \n ExtractOverlays :\n ",
51 " Extract overlay images from all DICOM image within a directory ",
52 " Warning : probably segfaults if no overlay ",
53 " usage: ExtractOverlays dirin=inputDirectoryName [debug] ",
54 " debug : developper wants to run the program in 'debug mode' ",
57 // ----- Initialize Arguments Manager ------
59 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
61 if (argc == 1 || am->ArgMgrDefined("usage"))
63 am->ArgMgrUsage(usage); // Display 'usage'
68 const char *dirIn = am->ArgMgrWantString("dirin", usage);
70 if (am->ArgMgrDefined("debug"))
71 gdcm::Debug::DebugOn();
73 if (am->ArgMgrDefined("warning"))
74 gdcm::Debug::WarningOn();
76 // if unused Param we give up
77 if ( am->ArgMgrPrintUnusedLabels() )
79 am->ArgMgrUsage(usage);
84 delete am; // we don't need Argument Manager any longer
87 // ========================== Now, we can do the job! ================
90 // ======================== more checking on the params ==============
92 if ( ! gdcm::DirList::IsDirectory(dirIn) )
94 std::cout << "KO : [" << dirIn << "] is not a Directory." << std::endl;
99 char outputFileName[1024]; // Hope it's enough for a file name!
103 gdcm::DirList dirList(dirIn,true); // gets (recursively) the file list
104 gdcm::DirListType fileList = dirList.GetFilenames();
105 for( gdcm::DirListType::iterator it = fileList.begin();
106 it != fileList.end();
109 // Just to see *all* the file names:
110 // std::cout << "file [" << it->c_str() << "]" << std::endl;
112 // Read the input file.
114 f = gdcm::File::New( );
115 f->SetLoadMode( gdcm::LD_ALL );
116 f->SetFileName( it->c_str() );
118 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
119 f->AddForceLoadElement(0x6002,0x3000);
120 f->AddForceLoadElement(0x6004,0x3000);
121 f->AddForceLoadElement(0x6006,0x3000);
122 f->AddForceLoadElement(0x6008,0x3000);
123 f->AddForceLoadElement(0x600a,0x3000);
124 f->AddForceLoadElement(0x600c,0x3000);
125 f->AddForceLoadElement(0x600e,0x3000);
131 std::cerr << "Sorry, " << it->c_str() <<" not a gdcm-readable "
132 << "DICOM / ACR File" <<std::endl;
136 std::cout << " ... is readable " << std::endl;
138 // ============================================================
139 // Load the Overlays in memory (the first one)
140 // ============================================================
142 /// \todo : deal with *each* overlay Data Element (not only the first one!)
144 uint16_t ovlyGroup = 0x6000;
146 gdcm::DataEntry *e = f->GetDataEntry(ovlyGroup, 0x3000);
149 std::cout << " Image doesn't contain any Overlay " << std::endl;
153 std::cout << " File is read! " << std::endl;
155 uint8_t *overlay = (uint8_t *)(e->GetBinArea());
158 std::cerr << "Sorry, Overlays of" << it->c_str() <<" are not "
159 << " gdcm-readable." << std::endl;
164 // ============================================================
165 // Image data generation.
166 // ============================================================
168 unsigned int dimX= f->GetXSize();
169 unsigned int dimY= f->GetYSize();
171 unsigned int dimXY=dimX*dimY;
172 std::cout << "DimX : "<< dimX <<" DimY : " << dimY
173 << " DimXY : " <<dimXY << std::endl;
174 unsigned char *outputData = new unsigned char[dimXY];
176 unsigned char *result=outputData;
177 for (unsigned int i=0;i<(dimXY/8);i++)
179 explodeByte(overlay[i], result);
183 // ============================================================
185 // ============================================================
187 f2 = gdcm::File::New( );
191 sprintf(temp,"%d ",dimX);
192 f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
193 sprintf(temp,"%d ",dimY);
194 f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
196 f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
197 f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
198 f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
199 f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
200 f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
201 f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");
203 sprintf(outputFileName, "%s.ovly.dcm",it->c_str() );
205 // feel free to add any field (Dicom Data Entry) you like, here.
207 gdcm::FileHelper *fh = gdcm::FileHelper::New(f2);
209 fh->SetImageData(outputData,dimXY);
210 fh->WriteDcmExplVR(outputFileName);
211 std::cout <<"End WriteOverlayImage" << std::endl;
218 } // end of loop on files