1 /*=========================================================================
4 Module: $RCSfile: exOverlaysACR.cxx,v $
6 Date: $Date: 2005/05/19 15:47:20 $
7 Version: $Revision: 1.3 $
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"
28 // unfinished : DO NOT to be used as is !
31 // Example (sorry, we've got no more than this one ...)
33 V 0028|0010[US] [Rows] [256] x(100)
34 V 0028|0011[US] [Columns] [256] x(100)
35 V 0028|0030[DS] [Pixel Spacing] [01.56\1.56]
36 V 0028|0100[US] [Bits Allocated] [16] x(10)
37 V 0028|0101[US] [Bits Stored] [12] x(c)
38 V 0028|0102[US] [High Bit] [11] x(b)
39 V 0028|0103[US] [Pixel Representation] [0] x(0)
41 V 6000|0000[UL] [Group Length] [96] x(60)
42 V 6000|0010[US] [Rows] [256] x(100)
43 V 6000|0011[US] [Columns] [256] x(100)
44 V 6000|0040[CS] [Overlay Type] [R ]
45 V 6000|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
46 V 6000|0100[US] [Overlay Bits Allocated] [16] x(10)
47 V 6000|0102[US] [Overlay Bit Position] [12] x(c)
50 V 6006|0000[UL] [Group Length] [96] x(60)
51 V 6006|0010[US] [Rows] [256] x(100)
52 V 6006|0011[US] [Columns] [256] x(100)
53 V 6006|0040[CS] [Overlay Type] [R ]
54 V 6006|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
55 V 6006|0100[US] [Overlay Bits Allocated] [16] x(10)
56 V 6006|0102[US] [Overlay Bit Position] [15] x(f)
59 int main(int argc, char *argv[])
63 //gdcm::Debug::DebugOn();
65 std::cout << "------------------------------------------------" << std::endl;
66 std::cout << "Gets the 'Overlays' from a full gdcm-readable ACR-NEMA "
67 << "uncompressed image" << std::endl;
68 std::cout << "Writes them in DicomV3 files named 'gdcmOverlay-xxx.dcm'"
70 std::cout << "(Note : we just have ONE image : "
71 << "SIEMENS_GBS_III-16-ACR_NEMA_1.acr)"
73 std::cout << "------------------------------------------------" << std::endl;
79 fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";
81 std::cout << fileName << std::endl;
82 // ============================================================
83 // Read the input image.
84 // ============================================================
86 //std::cout << argv[1] << std::endl;
88 f1 = new gdcm::File( );
90 f1->SetLoadMode(NO_SEQ | NO_SHADOW);
93 if( gdcm::Debug::GetDebugFlag() )
95 std::cout << "---------------------------------------------" << std::endl;
97 std::cout << "---------------------------------------------" << std::endl;
99 if (!f1->IsReadable()) {
100 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
101 << "DICOM / ACR File"
106 std::cout << " ... is readable " << std::endl;
108 // ============================================================
109 // Check whether image contains Overlays ACR-NEMA style.
110 // ============================================================
112 int bitsAllocated = f1->GetBitsAllocated();
113 if ( bitsAllocated <= 8 )
115 std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
119 std::string s1 = f1->GetEntryValue(0x6000, 0x0102);
120 if (s1 == gdcm::GDCM_UNFOUND)
122 std::cout << " Image doesn't contain any Overlay " << std::endl;
126 std::cout << " File is read! " << std::endl;
129 // ============================================================
130 // Load the pixels in memory.
131 // ============================================================
133 // We don't use a gdcm::FileHelper, since it rubs out
134 // the 'non image' bits of the pixels...
136 int nx = f1->GetXSize();
137 int ny = f1->GetYSize();
139 std::cout << "Dimensions " << ny << " " <<ny << std::endl;
141 gdcm::DocEntry *p = f1->GetDocEntry(f1->GetGrPixel(), f1->GetNumPixel());
143 std::cout << "Pixels element not found" << std::endl;
145 std::cout << "Pixels element FOUND" << std::endl;
147 int offset = (int)(p->GetOffset());
149 std::cout << "Offset " << offset << std::endl;
151 FILE *fp = fopen(fileName.c_str(), "r");
155 std::cout << "Unable to open File" << std::endl;
160 std::cout << "File open successfully" << std::endl;
162 fseek(fp, (long) offset,SEEK_SET);
163 uint16_t *pixels = new uint16_t[nx*ny];
164 size_t lgt = fread(pixels, 1, nx*ny*sizeof( uint16_t) , fp );
166 if ( lgt != (size_t)nx*ny*sizeof( uint16_t) )
168 std::cout << "Sorry, Pixels of" << fileName << " are not "
169 << "readable. expected length :" << nx*ny
170 << " " << "read length : " << lgt
178 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
182 // ============================================================
183 // Get each overlay Bit into an image
184 // ============================================================
186 uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
188 uint16_t currentOvlGroup = 0x6000;
189 std::string strOvlBitPosition;
193 uint16_t overlayLocation;
194 std::ostringstream str;
195 std::string strOverlayLocation;
196 gdcm::File *fileToBuild = 0;
197 gdcm::FileHelper *fh = 0;
200 while ( (strOvlBitPosition = f1->GetEntryValue(currentOvlGroup, 0x0102))
201 != gdcm::GDCM_UNFOUND )
204 strOverlayLocation = f1->GetEntryValue(currentOvlGroup, 0x0200);
205 if ( strOverlayLocation != gdcm::GDCM_UNFOUND )
207 overlayLocation = atoi(strOverlayLocation.c_str());
208 if ( overlayLocation != f1->GetGrPixel() )
210 std::cout << "Big Trouble : Overlays are NOT in the Pixels Group "
211 << std::hex << "(" << overlayLocation << " vs "
212 << f1->GetGrPixel() << std::endl;
213 // Actually, here, we should (try to) read the overlay location
214 // and go on the job.
218 ovlBitPosition = atoi(strOvlBitPosition.c_str());
219 mask = 1 << ovlBitPosition;
220 std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;
221 for (int j=0; j<nx*ny ; j++)
223 if( gdcm::Debug::GetDebugFlag() )
224 if (pixels[j] >= 0x1000)// if it contains at least one overlay bit
225 printf("%d : %04x\n",j, pixels[j]);
227 if ( (pixels[j] & mask) == 0 )
232 if( gdcm::Debug::GetDebugFlag() )
233 std::cout << "About to built empty file" << std::endl;
235 fileToBuild = new gdcm::File();
237 if( gdcm::Debug::GetDebugFlag() )
238 std::cout << "Finish to built empty file" << std::endl;
242 fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
245 fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
247 fileToBuild->InsertValEntry("8",0x0028,0x0100); // Bits Allocated
248 fileToBuild->InsertValEntry("8",0x0028,0x0101); // Bits Stored
249 fileToBuild->InsertValEntry("7",0x0028,0x0102); // High Bit
250 fileToBuild->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
251 fileToBuild->InsertValEntry("1",0x0028,0x0002); // Samples per Pixel
253 fileToBuild->InsertValEntry("MONOCHROME2 ",0x0028,0x0004);
254 // Other mandatory fields will be set automatically,
255 // just before Write(), by FileHelper::CheckMandatoryElements()
257 if( gdcm::Debug::GetDebugFlag() )
258 std::cout << "-------------About to built FileHelper" << std::endl;
260 fh = new gdcm::FileHelper(fileToBuild);
262 if( gdcm::Debug::GetDebugFlag() )
263 std::cout << "-------------Finish to built FileHelper" << std::endl;
265 fh->SetImageData(tabPixels,nx*ny);
266 fh->SetWriteTypeToDcmExplVR();
269 str<<"gdcmOverlay-"<<i << ".dcm";
270 // Write the current 'overlay' file
272 if( !fh->Write(str.str()) )
274 std::cout << "Failed\n"
275 << "File in unwrittable\n";
285 std::cout << "File written successfully" << std::endl;
287 currentOvlGroup += 2;