2 #include "gdcmFileHelper.h"
3 #include "gdcmCommon.h"
5 #include "gdcmDataEntry.h"
7 #include "gdcmArgMgr.h"
10 // Each BIT of Overlay Data (0x6000,0x3000) corresponds
11 // to a BYTE of overlay image.
12 void explodeByte(unsigned char byte, unsigned char* result)
14 unsigned char mask = 1;
27 int main(int argc, char *argv[])
30 " \n exOverlaysDCM :\n ",
31 " Extract an overlay image from a DICOM image ",
32 " Warning : probably segfaults if no overlay ",
33 " usage: exOverlaysDCM filein=inputFileName fileout=outputFileName [debug] ",
34 " debug : developper wants to run the program in 'debug mode' ",
37 // ----- Initialize Arguments Manager ------
38 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
40 if (argc == 1 || am->ArgMgrDefined("usage"))
42 am->ArgMgrUsage(usage); // Display 'usage'
46 char *fileName = am->ArgMgrWantString("filein",usage);
47 if ( fileName == NULL )
53 char *outputFileName = am->ArgMgrWantString("fileout",usage);
54 if ( outputFileName == NULL )
59 if (am->ArgMgrDefined("debug"))
60 gdcm::Debug::DebugOn();
62 // if unused Param we give up
63 if ( am->ArgMgrPrintUnusedLabels() )
65 am->ArgMgrUsage(usage);
70 delete am; // we don't need Argument Manager any longer
72 // ============================================================
73 // Read the input file.
74 // ============================================================
78 f = gdcm::File::New( );
79 f->SetLoadMode( gdcm::LD_ALL );
80 f->SetFileName( fileName );
81 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
86 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
87 << "DICOM / ACR File" <<std::endl;
91 std::cout << " ... is readable " << std::endl;
93 // ============================================================
94 // Load the Overlays in memory (the first one)
95 // ============================================================
97 gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);
100 std::cout << " Image doesn't contain any Overlay " << std::endl;
104 std::cout << " File is read! " << std::endl;
106 uint8_t *overlay = (uint8_t *)(e->GetBinArea());
109 std::cerr << "Sorry, Overlays of" << fileName <<" are not "
110 << " gdcm-readable." << std::endl;
115 // ============================================================
116 // Image data generation.
117 // ============================================================
120 unsigned int dimX= f->GetXSize();
121 unsigned int dimY= f->GetYSize();
123 unsigned int dimXY=dimX*dimY;
124 std::cout << "DimX : "<< dimX <<" DimY : " << dimY
125 << " DimXY : " <<dimXY << std::endl;
126 unsigned char *outputData = new unsigned char[dimXY];
128 unsigned char *result=outputData;
129 for (unsigned int i=0;i<(dimXY/8);i++)
131 explodeByte(overlay[i], result);
135 // ============================================================
137 // ============================================================
139 f2 = gdcm::File::New( );
143 sprintf(temp,"%d ",dimX);
144 f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
145 sprintf(temp,"%d ",dimY);
146 f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
148 f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
149 f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
150 f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
151 f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
152 f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
153 f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");
156 // feel free to add any field (Dicom Data Entry) you like, here.
158 gdcm::FileHelper *fh = gdcm::FileHelper::New(f2);
160 fh->SetImageData(outputData,dimXY);
161 fh->WriteDcmExplVR(outputFileName);
162 std::cout <<"End WriteOverlayImage" << std::endl;