1 /*=========================================================================
4 Module: $RCSfile: exOverlaysACR.cxx,v $
6 Date: $Date: 2005/10/25 14:52:28 $
7 Version: $Revision: 1.9 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
25 #include <stdio.h> // for fseek... FIXME
26 #include <stdlib.h> // for atoi
29 // unfinished : DO NOT to be used as is !
32 // Example (sorry, we've got no more than this one ...)
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)
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)
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)
60 int main(int argc, char *argv[])
64 //gdcm::Debug::DebugOn();
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'"
71 std::cout << "(Note : we just have ONE image : "
72 << "SIEMENS_GBS_III-16-ACR_NEMA_1.acr)"
74 std::cout << "------------------------------------------------" << std::endl;
80 fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";
82 std::cout << fileName << std::endl;
83 // ============================================================
84 // Read the input image.
85 // ============================================================
87 //std::cout << argv[1] << std::endl;
89 f = gdcm::File::New( );
91 f->SetLoadMode(gdcm::LD_NOSEQ | gdcm::LD_NOSHADOW);
92 f->SetFileName( fileName );
95 if( gdcm::Debug::GetDebugFlag() )
97 std::cout << "---------------------------------------------" << std::endl;
99 std::cout << "---------------------------------------------" << std::endl;
102 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
103 << "DICOM / ACR File"
108 std::cout << " ... is readable " << std::endl;
110 // ============================================================
111 // Check whether image contains Overlays ACR-NEMA style.
112 // ============================================================
114 int bitsAllocated = f->GetBitsAllocated();
115 if ( bitsAllocated <= 8 )
117 std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
121 std::string s1 = f->GetEntryString(0x6000, 0x0102);
122 if (s1 == gdcm::GDCM_UNFOUND)
124 std::cout << " Image doesn't contain any Overlay " << std::endl;
128 std::cout << " File is read! " << std::endl;
131 // ============================================================
132 // Load the pixels in memory.
133 // ============================================================
135 // We don't use a gdcm::FileHelper, since it rubs out
136 // the 'non image' bits of the pixels...
138 int nx = f->GetXSize();
139 int ny = f->GetYSize();
141 std::cout << "Dimensions " << ny << " " <<ny << std::endl;
143 gdcm::DocEntry *p = f->GetDocEntry(f->GetGrPixel(), f->GetNumPixel());
145 std::cout << "Pixels element not found" << std::endl;
147 std::cout << "Pixels element FOUND" << std::endl;
149 int offset = (int)(p->GetOffset());
151 std::cout << "Offset " << offset << std::endl;
153 FILE *fp = fopen(fileName.c_str(), "r");
157 std::cout << "Unable to open File" << std::endl;
162 std::cout << "File open successfully" << std::endl;
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 );
168 if ( lgt != (size_t)nx*ny*sizeof( uint16_t) )
170 std::cout << "Sorry, Pixels of" << fileName << " are not "
171 << "readable. expected length :" << nx*ny
172 << " " << "read length : " << lgt
180 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
184 // ============================================================
185 // Get each overlay Bit into an image
186 // ============================================================
188 uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
190 uint16_t currentOvlGroup = 0x6000;
191 std::string strOvlBitPosition;
195 uint16_t overlayLocation;
196 std::ostringstream str;
197 std::string strOverlayLocation;
198 gdcm::File *fileToBuild = 0;
199 gdcm::FileHelper *fh = 0;
202 while ( (strOvlBitPosition = f->GetEntryString(currentOvlGroup, 0x0102))
203 != gdcm::GDCM_UNFOUND )
206 strOverlayLocation = f->GetEntryString(currentOvlGroup, 0x0200);
207 if ( strOverlayLocation != gdcm::GDCM_UNFOUND )
209 overlayLocation = atoi(strOverlayLocation.c_str());
210 if ( overlayLocation != f->GetGrPixel() )
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.
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++)
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]);
229 if ( (pixels[j] & mask) == 0 )
234 if( gdcm::Debug::GetDebugFlag() )
235 std::cout << "About to built empty file" << std::endl;
237 fileToBuild = gdcm::File::New();
239 if( gdcm::Debug::GetDebugFlag() )
240 std::cout << "Finish to built empty file" << std::endl;
244 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
247 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
249 fileToBuild->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
250 fileToBuild->InsertEntryString("8",0x0028,0x0101); // Bits Stored
251 fileToBuild->InsertEntryString("7",0x0028,0x0102); // High Bit
252 fileToBuild->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
253 fileToBuild->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
255 fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
256 // Other mandatory fields will be set automatically,
257 // just before Write(), by FileHelper::CheckMandatoryElements()
259 if( gdcm::Debug::GetDebugFlag() )
260 std::cout << "-------------About to built FileHelper" << std::endl;
262 fh = gdcm::FileHelper::New(fileToBuild);
264 if( gdcm::Debug::GetDebugFlag() )
265 std::cout << "-------------Finish to built FileHelper" << std::endl;
267 fh->SetImageData(tabPixels,nx*ny);
268 fh->SetWriteTypeToDcmExplVR();
271 str<<"gdcmOverlay-"<<i << ".dcm";
272 // Write the current 'overlay' file
274 if( !fh->Write(str.str()) )
276 std::cout << "Failed\n"
277 << "File in unwrittable\n";
280 fileToBuild->Delete();
287 std::cout << "File written successfully" << std::endl;
289 currentOvlGroup += 2;
296 fileToBuild->Delete();