]> Creatis software - gdcm.git/blob - Example/exOverlaysDCM.cxx
14d31cc93942340f7f7ad86e32c85045f64c8998
[gdcm.git] / Example / exOverlaysDCM.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exOverlaysDCM.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/09/04 13:04:18 $
7   Version:   $Revision: 1.11 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmFile.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmCommon.h"
22 #include "gdcmDebug.h"
23 #include "gdcmDataEntry.h"
24
25 #include "gdcmArgMgr.h"
26 #include <iostream>
27
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) 
31 {
32    unsigned char mask = 1;
33    for (int i=0;i<8;i++) 
34    {
35       if ((byte & mask)==0) 
36          result[i]=0;
37       else 
38          result[i]=1;
39       mask<<=1;
40    }
41    return;
42 }
43
44
45 int main(int argc, char *argv[])
46 {
47    START_USAGE(usage)
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'    ",
53    FINISH_USAGE
54
55    // ----- Initialize Arguments Manager ------
56    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
57
58    if (argc == 1 || am->ArgMgrDefined("usage"))
59    {
60       am->ArgMgrUsage(usage); // Display 'usage'
61       delete am;
62       return 0;
63    }
64    
65 /*
66    char *fileName = am->ArgMgrWantString("filein",usage);
67    if ( fileName == NULL )
68    {
69       delete am;
70       return 0;
71    }
72 */
73
74
75    const char *fileName = am->ArgMgrGetString("filein");
76    const char *dirName  = am->ArgMgrGetString("dirin");
77
78    if ( (fileName == 0 && dirName == 0) ||
79         (fileName != 0 && dirName != 0) )
80    {
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'  
85       delete am;
86       return 1;
87    }
88
89
90    char *outputFileName = am->ArgMgrWantString("fileout",usage);
91    if ( outputFileName == NULL )
92    {
93       delete am;
94       return 0;
95    }
96    if (am->ArgMgrDefined("debug"))
97       GDCM_NAME_SPACE::Debug::DebugOn();
98       
99    if (am->ArgMgrDefined("warning"))
100       GDCM_NAME_SPACE::Debug::WarningOn();
101       
102    // if unused Param we give up
103    if ( am->ArgMgrPrintUnusedLabels() )
104    {
105       am->ArgMgrUsage(usage);
106       delete am;
107       return 0;
108    }
109
110    delete am;  // we don't need Argument Manager any longer
111
112    // ============================================================
113    //   Read the input file.
114    // ============================================================
115
116    GDCM_NAME_SPACE::File *f;
117
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
122    int res = f->Load();
123
124    if ( !res )
125    {
126        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
127                  << "DICOM / ACR File" <<std::endl;
128        f->Delete();
129        return 0;
130    }
131    std::cout << " ... is readable " << std::endl;
132
133    // ============================================================
134    //   Load the Overlays in memory (the first one)
135    // ============================================================
136
137    GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);  
138    if (e == 0)
139    {
140       std::cout << " Image doesn't contain any Overlay " << std::endl;
141       f->Delete();
142       return 0;
143    }   
144    std::cout << " File is read! " << std::endl;
145
146    uint8_t *overlay = (uint8_t *)(e->GetBinArea());
147    if ( overlay == 0 )
148    {
149        std::cerr << "Sorry, Overlays of" << fileName <<"  are not "
150                  << " gdcm-readable."    << std::endl;
151        f->Delete();
152        return 0;
153    }
154
155    // ============================================================
156    //  Image data generation.
157    // ============================================================
158    
159
160    unsigned int dimX= f->GetXSize();
161    unsigned int dimY= f->GetYSize();
162
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];
167
168    unsigned char *result=outputData;
169    for (unsigned int i=0;i<(dimXY/8);i++) 
170    {
171       explodeByte(overlay[i], result);
172       result+=8;
173    }
174    
175    // ============================================================
176    //   Write a new file
177    // ============================================================
178    GDCM_NAME_SPACE::File *f2;
179    f2 = GDCM_NAME_SPACE::File::New(  );
180    
181    char temp[256];
182    
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
187
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");  
194
195
196    // feel free to add any field (Dicom Data Entry) you like, here.
197    // ...
198    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f2);
199        
200    fh->SetImageData(outputData,dimXY);
201    fh->WriteDcmExplVR(outputFileName);
202    std::cout <<"End WriteOverlayImage" << std::endl;
203
204    delete outputData;
205    f->Delete();
206    f2->Delete();  
207    fh->Delete();
208    
209    return 0;
210 }
211