]> Creatis software - gdcm.git/blob - Example/exOverlaysACR.cxx
257b11aa731ed6738c0dcc346b35cc4573309f24
[gdcm.git] / Example / exOverlaysACR.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exOverlaysACR.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/11 14:35:46 $
7   Version:   $Revision: 1.5 $
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
24 #include <iostream>
25 #include <stdio.h> // for fseek... FIXME
26 #include <stdlib.h> // for atoi
27  
28  // WARNING :
29  // unfinished : DO NOT to be used as is !
30  
31  /*
32  // Example (sorry, we've got no more than this one ...)
33  
34 V 0028|0010[US] [Rows] [256] x(100)
35 V 0028|0011[US] [Columns] [256] x(100)
36 V 0028|0030[DS] [Pixel Spacing] [01.56\1.56]
37 V 0028|0100[US] [Bits Allocated] [16] x(10)
38 V 0028|0101[US] [Bits Stored] [12] x(c)
39 V 0028|0102[US] [High Bit] [11] x(b)
40 V 0028|0103[US] [Pixel Representation] [0] x(0)
41  
42 V 6000|0000[UL] [Group Length] [96] x(60)
43 V 6000|0010[US] [Rows] [256] x(100)
44 V 6000|0011[US] [Columns] [256] x(100)
45 V 6000|0040[CS] [Overlay Type] [R ]
46 V 6000|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
47 V 6000|0100[US] [Overlay Bits Allocated] [16] x(10)
48 V 6000|0102[US] [Overlay Bit Position] [12] x(c)
49 ...
50 ...
51 V 6006|0000[UL] [Group Length] [96] x(60)
52 V 6006|0010[US] [Rows] [256] x(100)
53 V 6006|0011[US] [Columns] [256] x(100)
54 V 6006|0040[CS] [Overlay Type] [R ]
55 V 6006|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
56 V 6006|0100[US] [Overlay Bits Allocated] [16] x(10)
57 V 6006|0102[US] [Overlay Bit Position] [15] x(f)
58  */
59  
60 int main(int argc, char *argv[])
61 {  
62    gdcm::File *f;
63  
64    //gdcm::Debug::DebugOn();
65
66    std::cout << "------------------------------------------------" << std::endl;
67    std::cout << "Gets the 'Overlays' from a full gdcm-readable ACR-NEMA "
68              << "uncompressed image" << std::endl;
69    std::cout << "Writes them in DicomV3 files named 'gdcmOverlay-xxx.dcm'"
70              << std::endl;
71    std::cout << "(Note :  we just have ONE image : "
72              << "SIEMENS_GBS_III-16-ACR_NEMA_1.acr)"
73              << std::endl;
74    std::cout << "------------------------------------------------" << std::endl;
75
76    std::string fileName;
77    if( argc > 1 )
78       fileName = argv[1];
79    else
80       fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";  
81
82    std::cout << fileName << std::endl;
83 // ============================================================
84 //   Read the input image.
85 // ============================================================
86
87    //std::cout << argv[1] << std::endl;
88
89    f = new gdcm::File( );
90
91    f->SetLoadMode(NO_SEQ | NO_SHADOW);
92    f->SetFileName( fileName );
93    bool res = f->Load();  
94
95    if( gdcm::Debug::GetDebugFlag() )
96    {
97       std::cout << "---------------------------------------------" << std::endl;
98       f->Print();
99       std::cout << "---------------------------------------------" << std::endl;
100    }
101    if (!res) {
102        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
103            << "DICOM / ACR File"
104            <<std::endl;
105       delete f;
106       return 0;
107    }
108    std::cout << " ... is readable " << std::endl;
109
110 // ============================================================
111 //   Check whether image contains Overlays ACR-NEMA style.
112 // ============================================================
113
114    int bitsAllocated = f->GetBitsAllocated();
115    if ( bitsAllocated <= 8 )
116    {
117       std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
118       delete f;
119       return 0;
120    }
121    std::string s1 = f->GetEntryValue(0x6000, 0x0102);
122    if (s1 == gdcm::GDCM_UNFOUND)
123    {
124       std::cout << " Image doesn't contain any Overlay " << std::endl;
125       delete f;
126       return 0;
127    }
128    std::cout << " File is read! " << std::endl;
129
130    
131 // ============================================================
132 //   Load the pixels in memory.
133 // ============================================================
134
135    // We don't use a gdcm::FileHelper, since it rubs out 
136    // the 'non image' bits of the pixels...
137
138    int nx = f->GetXSize();
139    int ny = f->GetYSize();
140  
141    std::cout << "Dimensions " << ny << "  " <<ny << std::endl;
142
143    gdcm::DocEntry *p = f->GetDocEntry(f->GetGrPixel(), f->GetNumPixel());
144    if (p == 0)
145       std::cout << "Pixels element  not found" << std::endl;
146    else
147       std::cout << "Pixels element FOUND" << std::endl;
148
149    int offset = (int)(p->GetOffset());
150
151    std::cout << "Offset " << offset << std::endl;
152
153    FILE *fp = fopen(fileName.c_str(), "r");
154
155    if (fp == 0)
156    {
157       std::cout << "Unable to open File" << std::endl;
158       delete f;
159       return 0;
160    }
161    else
162       std::cout << "File open successfully" << std::endl; 
163   
164    fseek(fp, (long) offset,SEEK_SET);      
165    uint16_t *pixels = new uint16_t[nx*ny];
166    size_t lgt = fread(pixels, 1,  nx*ny*sizeof( uint16_t) , fp );   
167
168    if ( lgt != (size_t)nx*ny*sizeof( uint16_t) )
169    {
170        std::cout << "Sorry, Pixels of" << fileName << "  are not "
171                  << "readable. expected length :" << nx*ny 
172                  << "  " << "read length : " << lgt
173                  << std::endl;
174        delete f;
175        delete pixels;  
176        return 0;
177    }
178    else
179    {
180       std::cout << "Pixels read as expected : length = " << lgt << std::endl;
181    } 
182
183
184 // ============================================================
185 //   Get each overlay Bit into an image
186 // ============================================================
187                                          
188    uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
189    
190    uint16_t currentOvlGroup = 0x6000;
191    std::string strOvlBitPosition;
192    int ovlBitPosition;
193    uint16_t mask;
194    int i = 0;
195    uint16_t overlayLocation;
196    std::ostringstream str;
197    std::string strOverlayLocation;
198    gdcm::File *fileToBuild = 0;
199    gdcm::FileHelper *fh = 0;
200
201       
202 while ( (strOvlBitPosition = f->GetEntryValue(currentOvlGroup, 0x0102)) 
203           != gdcm::GDCM_UNFOUND )
204 {
205
206       strOverlayLocation = f->GetEntryValue(currentOvlGroup, 0x0200);
207       if ( strOverlayLocation != gdcm::GDCM_UNFOUND )
208       {
209          overlayLocation = atoi(strOverlayLocation.c_str());
210          if ( overlayLocation != f->GetGrPixel() )
211          {
212             std::cout << "Big Trouble : Overlays are NOT in the Pixels Group "
213                       << std::hex << "(" << overlayLocation << " vs " 
214                       << f->GetGrPixel() << std::endl;
215             // Actually, here, we should (try to) read the overlay location
216             // and go on the job.
217             continue;
218          }
219       }
220       ovlBitPosition = atoi(strOvlBitPosition.c_str());
221       mask = 1 << ovlBitPosition; 
222       std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;          
223       for (int j=0; j<nx*ny ; j++)
224       {
225          if( gdcm::Debug::GetDebugFlag() )
226             if (pixels[j] >= 0x1000)// if it contains at least one overlay bit
227                printf("%d : %04x\n",j, pixels[j]);
228
229          if ( (pixels[j] & mask) == 0 )
230             tabPixels[j] = 0;
231          else
232             tabPixels[j] = 128;
233       }
234       if( gdcm::Debug::GetDebugFlag() )
235          std::cout << "About to built empty file"  << std::endl;
236
237       fileToBuild = new gdcm::File();
238
239       if( gdcm::Debug::GetDebugFlag() )
240          std::cout << "Finish to built empty file"  << std::endl;
241
242       str.str("");
243       str << nx;
244       fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
245       str.str("");
246       str << ny;
247       fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
248
249       fileToBuild->InsertValEntry("8",0x0028,0x0100); // Bits Allocated
250       fileToBuild->InsertValEntry("8",0x0028,0x0101); // Bits Stored
251       fileToBuild->InsertValEntry("7",0x0028,0x0102); // High Bit
252       fileToBuild->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
253       fileToBuild->InsertValEntry("1",0x0028,0x0002); // Samples per Pixel
254
255       fileToBuild->InsertValEntry("MONOCHROME2 ",0x0028,0x0004);
256       // Other mandatory fields will be set automatically,
257       // just before Write(), by FileHelper::CheckMandatoryElements()
258
259       if( gdcm::Debug::GetDebugFlag() )
260          std::cout << "-------------About to built FileHelper"  << std::endl;
261
262       fh = new gdcm::FileHelper(fileToBuild);
263
264       if( gdcm::Debug::GetDebugFlag() )
265          std::cout << "-------------Finish to built FileHelper"  << std::endl;
266
267       fh->SetImageData(tabPixels,nx*ny);
268       fh->SetWriteTypeToDcmExplVR();
269
270       str.str("");
271       str<<"gdcmOverlay-"<<i << ".dcm";
272       //   Write the current 'overlay' file
273
274       if( !fh->Write(str.str()) )
275       {
276          std::cout << "Failed\n"
277                    << "File in unwrittable\n";
278          delete fh;
279          if (fileToBuild)
280             delete fileToBuild;
281          delete pixels;
282          delete tabPixels;
283          return 0;
284       }
285       else
286       {
287          std::cout << "File written successfully" << std::endl;
288       }
289       currentOvlGroup += 2;
290       i++;
291    }
292     
293    delete f;
294    if (f)
295       delete fh;
296    if (fileToBuild)
297       delete fileToBuild;
298    delete pixels;
299    delete tabPixels;
300    return 0;
301 }
302