1 /*=========================================================================
4 Module: $RCSfile: exOverlaysDCM.cxx,v $
6 Date: $Date: 2007/09/04 13:04:18 $
7 Version: $Revision: 1.11 $
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 =========================================================================*/
20 #include "gdcmFileHelper.h"
21 #include "gdcmCommon.h"
22 #include "gdcmDebug.h"
23 #include "gdcmDataEntry.h"
25 #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;
45 int main(int argc, char *argv[])
48 " \n exOverlaysDCM :\n ",
49 " Extract an overlay image from a DICOM image ",
50 " Warning : probably segfaults if no overlay ",
51 " usage: exOverlaysDCM filein=inputFileName fileout=outputFileName [debug] ",
52 " debug : developper wants to run the program in 'debug mode' ",
55 // ----- Initialize Arguments Manager ------
56 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
58 if (argc == 1 || am->ArgMgrDefined("usage"))
60 am->ArgMgrUsage(usage); // Display 'usage'
66 char *fileName = am->ArgMgrWantString("filein",usage);
67 if ( fileName == NULL )
75 const char *fileName = am->ArgMgrGetString("filein");
76 const char *dirName = am->ArgMgrGetString("dirin");
78 if ( (fileName == 0 && dirName == 0) ||
79 (fileName != 0 && dirName != 0) )
81 std::cerr << std::endl
82 << "Either 'filein=' or 'dirin=' must be present;"
83 << std::endl << "Not both" << std::endl;
84 am->ArgMgrUsage(usage); // Display 'usage'
90 char *outputFileName = am->ArgMgrWantString("fileout",usage);
91 if ( outputFileName == NULL )
96 if (am->ArgMgrDefined("debug"))
97 GDCM_NAME_SPACE::Debug::DebugOn();
99 if (am->ArgMgrDefined("warning"))
100 GDCM_NAME_SPACE::Debug::WarningOn();
102 // if unused Param we give up
103 if ( am->ArgMgrPrintUnusedLabels() )
105 am->ArgMgrUsage(usage);
110 delete am; // we don't need Argument Manager any longer
112 // ============================================================
113 // Read the input file.
114 // ============================================================
116 GDCM_NAME_SPACE::File *f;
118 f = GDCM_NAME_SPACE::File::New( );
119 f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL );
120 f->SetFileName( fileName );
121 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
126 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
127 << "DICOM / ACR File" <<std::endl;
131 std::cout << " ... is readable " << std::endl;
133 // ============================================================
134 // Load the Overlays in memory (the first one)
135 // ============================================================
137 GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);
140 std::cout << " Image doesn't contain any Overlay " << std::endl;
144 std::cout << " File is read! " << std::endl;
146 uint8_t *overlay = (uint8_t *)(e->GetBinArea());
149 std::cerr << "Sorry, Overlays of" << fileName <<" are not "
150 << " gdcm-readable." << std::endl;
155 // ============================================================
156 // Image data generation.
157 // ============================================================
160 unsigned int dimX= f->GetXSize();
161 unsigned int dimY= f->GetYSize();
163 unsigned int dimXY=dimX*dimY;
164 std::cout << "DimX : "<< dimX <<" DimY : " << dimY
165 << " DimXY : " <<dimXY << std::endl;
166 unsigned char *outputData = new unsigned char[dimXY];
168 unsigned char *result=outputData;
169 for (unsigned int i=0;i<(dimXY/8);i++)
171 explodeByte(overlay[i], result);
175 // ============================================================
177 // ============================================================
178 GDCM_NAME_SPACE::File *f2;
179 f2 = GDCM_NAME_SPACE::File::New( );
183 sprintf(temp,"%d ",dimX);
184 f2->InsertEntryString(temp,0x0028,0x0011, "US"); // Columns
185 sprintf(temp,"%d ",dimY);
186 f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows
188 f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated
189 f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored
190 f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit
191 f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
192 f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
193 f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO");
196 // feel free to add any field (Dicom Data Entry) you like, here.
198 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f2);
200 fh->SetImageData(outputData,dimXY);
201 fh->WriteDcmExplVR(outputFileName);
202 std::cout <<"End WriteOverlayImage" << std::endl;