2 #include "gdcmFileHelper.h"
3 #include "gdcmCommon.h"
5 #include "gdcmDataEntry.h"
7 #include "gdcmArgMgr.h"
10 void explodeByte(unsigned char byte, unsigned char* result)
12 unsigned char mask = 1;
25 int main(int argc, char *argv[])
28 " \n exReadOverlays :\n ",
29 " Extract an overlay image from a secondary capture image ",
30 " Warning : probably segfaults if no overlay ",
31 " usage: WriteOverlayImage filein=inputFileName fileout=outputFileName[debug]",
32 " debug : user wants to run the program in 'debug mode' ",
35 // ----- Initialize Arguments Manager ------
36 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
38 if (argc == 1 || am->ArgMgrDefined("usage"))
40 am->ArgMgrUsage(usage); // Display 'usage'
44 char *fileName = am->ArgMgrWantString("filein",usage);
45 if ( fileName == NULL )
51 char *outputFileName = am->ArgMgrWantString("fileout",usage);
52 if ( outputFileName == NULL )
57 if (am->ArgMgrDefined("debug"))
58 gdcm::Debug::DebugOn();
60 // if unused Param we give up
61 if ( am->ArgMgrPrintUnusedLabels() )
63 am->ArgMgrUsage(usage);
68 delete am; // we don't need Argument Manager any longer
70 // ============================================================
71 // Read the input file.
72 // ============================================================
76 f = gdcm::File::New( );
77 f->SetLoadMode( gdcm::LD_ALL );
78 f->SetFileName( fileName );
79 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
84 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
85 << "DICOM / ACR File" <<std::endl;
89 std::cout << " ... is readable " << std::endl;
91 // ============================================================
92 // Load the Overlays in memory (the first one)
93 // ============================================================
95 gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);
98 std::cout << " Image doesn't contain any Overlay " << std::endl;
102 std::cout << " File is read! " << std::endl;
104 uint8_t *overlay = (uint8_t *)(e->GetBinArea());
107 std::cerr << "Sorry, Overlays of" << fileName <<" are not "
108 << " gdcm-readable." << std::endl;
113 // ============================================================
114 // Image data generation.
115 // ============================================================
117 unsigned int dimX= f->GetXSize();
118 unsigned int dimY= f->GetYSize();
120 unsigned int dimXY=dimX*dimY;
121 std::cout <<"DimX : "<<dimX<<"DimY : "<<dimY<<"DimXY : "<<dimXY << std::endl;
122 unsigned char *outputData = new unsigned char[dimXY];
124 unsigned char *result=outputData;
125 for (unsigned int i=0;i<(dimXY/8);i++)
127 explodeByte(overlay[i], result);
132 // ============================================================
134 // ============================================================
138 sprintf(temp,"%d ",dimX);
139 f->InsertEntryString(temp,0x0028,0x0011); // Columns
140 sprintf(temp,"%d ",dimY);
141 f->InsertEntryString(temp,0x0028,0x0010); // Rows
143 f->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
144 f->InsertEntryString("8",0x0028,0x0101); // Bits Stored
145 f->InsertEntryString("7",0x0028,0x0102); // High Bit
146 f->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
147 f->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
148 f->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
150 // We need a gdcm::FileHelper, since we want to load the pixels
151 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
153 fh->SetImageData(outputData,dimXY);
154 fh->WriteDcmExplVR(outputFileName);
155 std::cout <<"End WriteOverlayImage" << std::endl;