]> Creatis software - gdcm.git/blob - Example/exOverlaysDCM.cxx
First check to extract overlays.
[gdcm.git] / Example / exOverlaysDCM.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exOverlaysDCM.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/12/09 10:17:52 $
7   Version:   $Revision: 1.1 $
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 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23 #include "gdcmDataEntry.h"
24
25 #include <iostream>
26 #include <stdio.h> // for fseek... FIXME
27 #include <stdlib.h> // for atoi
28  
29  // WARNING :
30  // unfinished : DO NOT to be used as is !
31  
32
33 int main(int argc, char *argv[])
34 {  
35    gdcm::File *f;
36  
37    //gdcm::Debug::DebugOn();
38
39    std::cout << "------------------------------------------------" << std::endl;
40    std::cout << "Gets the 'Overlays' from a full gdcm-readable DCM image "
41              << "uncompressed image" << std::endl;
42    std::cout << "Writes them in a DicomV3 file named 'gdcmOverlay.dcm'"
43              << std::endl;
44    std::cout << "------------------------------------------------" << std::endl;
45
46    std::string fileName;
47    if( argc > 1 )
48       fileName = argv[1];
49    else
50       fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";  
51
52    std::cout << fileName << std::endl;
53 // ============================================================
54 //   Read the input image.
55 // ============================================================
56
57    //std::cout << argv[1] << std::endl;
58
59    f = gdcm::File::New( );
60
61    f->SetLoadMode(gdcm::LD_ALL);
62    f->SetFileName( fileName );
63    f->AddForceLoadElement(0x6000,0x3000);  // Overlay Data
64    bool res = f->Load();  
65
66    if( gdcm::Debug::GetDebugFlag() )
67    {
68       std::cout << "---------------------------------------------" << std::endl;
69       f->Print();
70       std::cout << "---------------------------------------------" << std::endl;
71    }
72    if (!res) {
73        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
74            << "DICOM / ACR File"
75            <<std::endl;
76       f->Delete();
77       return 0;
78    }
79    std::cout << " ... is readable " << std::endl;
80
81 // ============================================================
82 //   Check whether image contains Overlays ACR-NEMA style.
83 // ============================================================
84
85    int bitsAllocated = f->GetBitsAllocated();
86    
87    gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);
88    
89    if (e == 0)
90    {
91       std::cout << " Image doesn't contain any Overlay " << std::endl;
92       f->Delete();
93       return 0;
94    }
95    
96    std::cout << " File is read! " << std::endl;
97    
98 // ============================================================
99 //   Get usefull info from FileHelper.
100 // ============================================================ 
101
102    
103    uint32_t overlayPixelLength = e->GetLength();
104    
105 /*
106    if ( overlayPixelLength != (size_t)nx*ny*sizeof( uint16_t) )
107    {
108        std::cout << "Sorry, Pixels of" << fileName << "  are not "
109                  << "readable. expected length :" << nx*ny*sizeof( uint16_t) 
110                  << "  " << "read length : " << overlayPixelLength
111                  << std::endl;
112        f->Delete();
113        delete pixels;  
114        return 0;
115    }
116    else
117    {
118       std::cout << "Pixels read as expected : length = " << lgt << std::endl;
119    } 
120 */
121    uint32_t nx = f->GetXSize();
122    uint32_t ny = f->GetYSize();
123
124    uint8_t *overlayPixelArea = (uint8_t *)(e->GetBinArea());
125    
126    // should check overlayPixelLength == nx*ny/8 !)
127    
128    uint8_t *overlayPixelImage = new uint8_t[nx*ny];// uint8 is enough to hold 1 bit !
129    
130    uint16_t m;
131    for (unsigned int j=0; j<overlayPixelLength; j++)
132    {
133       if (overlayPixelArea[j] != 0)
134       { 
135          std::cout << "j : " << std::dec << j << " Ox(" << std::hex <<(int)overlayPixelArea[j]
136            << ")" << std::endl; 
137          for (unsigned int k=0; k<8; k++)
138          {
139             m = overlayPixelArea[j]<<k;
140             //printf("m : %04x ", m);
141             m = m & 0x00ff;
142             //printf(" : %04x \n", m);
143             overlayPixelImage[j*8 +k] =  ( m > 127)?255:0;
144     
145             printf("%04x ", overlayPixelImage[j*8 +k]);
146          }
147          std::cout << std::endl;
148       }
149    }
150
151
152    if( gdcm::Debug::GetDebugFlag() )
153          std::cout << "About to built empty file"  << std::endl;
154
155    gdcm::File *fileToBuild = gdcm::File::New();
156
157    if( gdcm::Debug::GetDebugFlag() )
158          std::cout << "Finish to built empty file"  << std::endl;
159
160    std::ostringstream str;
161    str.str("");
162    str << nx;
163    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
164    str.str("");
165    str << ny;
166    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
167
168    fileToBuild->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
169    fileToBuild->InsertEntryString("8",0x0028,0x0101); // Bits Stored
170    fileToBuild->InsertEntryString("7",0x0028,0x0102); // High Bit
171    fileToBuild->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
172    fileToBuild->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
173
174    fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
175       // Other mandatory fields will be set automatically,
176       // just before Write(), by FileHelper::CheckMandatoryElements()
177
178    if( gdcm::Debug::GetDebugFlag() )
179          std::cout << "-------------About to built FileHelper"  << std::endl;
180
181    gdcm::FileHelper *fh = gdcm::FileHelper::New(fileToBuild);
182
183    if( gdcm::Debug::GetDebugFlag() )
184          std::cout << "-------------Finish to built FileHelper"  << std::endl;
185
186       fh->SetImageData(overlayPixelImage,nx*ny);
187       fh->SetWriteTypeToDcmExplVR();
188
189       str.str("");
190       str<<"gdcmOverlay" << ".dcm";
191       //   Write the current 'overlay' file
192
193       if( !fh->Write(str.str()) )
194       {
195          std::cout << "Failed\n"
196                    << "File in unwrittable\n";
197          fh->Delete();
198          if (fileToBuild)
199             fileToBuild->Delete();
200          delete overlayPixelImage;
201          
202          return 0;
203       }
204       else
205       {
206          std::cout << "File written successfully" << std::endl;
207       }
208   
209    if (f)
210       fh->Delete();
211    if (fileToBuild)
212       fileToBuild->Delete();
213    f->Delete();
214    delete overlayPixelImage;
215    
216
217    return 0;
218 }
219