1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2005/08/28 17:02:34 $
7 Version: $Revision: 1.47 $
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 "gdcmSeqEntry.h"
20 #include "gdcmSQItem.h"
21 #include "gdcmBinEntry.h"
23 #include "gdcmFileHelper.h"
24 #include "gdcmDebug.h"
25 #include "gdcmDirList.h"
27 #include "gdcmArgMgr.h"
32 void ShowLutData(gdcm::File *f);
34 void ShowLutData(gdcm::File *f)
36 // Nothing is written yet to get LUT Data user friendly
37 // The following is to be moved into a PixelReadConvert method
38 // Let here, waiting for a clever idea on the way to do it.
40 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
43 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
46 std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
47 if ( /*lutDescriptor == GDCM_UNFOUND*/ 0 )
49 //gdcmWarningMacro( "LUT Descriptor is missing" );
50 std::cout << "LUT Descriptor is missing" << std::endl;
53 int length; // LUT length in Bytes
54 int deb; // Subscript of the first Lut Value
55 int nbits; // Lut item size (in Bits)
57 int nbRead; // nb of items in LUT descriptor (must be = 3)
59 nbRead = sscanf( lutDescriptor.c_str(),
61 &length, &deb, &nbits );
62 std::cout << "length " << length
68 //gdcmWarningMacro( "Wrong LUT descriptor" );
69 std::cout << "Wrong LUT descriptor" << std::endl;
71 //LUT Data (CTX dependent)
72 gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
75 int BitsAllocated = f->GetBitsAllocated();
76 if ( BitsAllocated <= 8 )
79 if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
81 // when LUT item size is different than pixel size
82 mult = 2; // high byte must be = low byte
86 // See PS 3.3-2003 C.11.1.1.2 p 619
89 uint8_t *lut = b->GetBinArea();
90 for( int i=0; i < length; ++i )
92 std::cout << i+deb << " : \t"
93 << (int) (lut[i*mult + 1]) << std::endl;
98 uint16_t *lut = (uint16_t *)(b->GetBinArea());
99 for( int i=0; i < length; ++i )
101 std::cout << i+deb << " : \t"
102 << (int) (((uint16_t *)lut)[i])
108 std::cout << "No LUT Data BinEntry (0x0028,0x3006) found?!? "
112 std::cout << "No First SQ Item within (0x0028,0x3000) ?!? "
116 std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found "
120 int main(int argc, char *argv[])
124 " \n PrintFile : \n ",
125 " Display the header of a ACR-NEMA/PAPYRUS/DICOM File ",
126 " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
127 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
128 " level = 0,1,2 : depending on the amount of details user wants to see",
129 " noshadowseq: user doesn't want to load Private Sequences ",
130 " noshadow : user doesn't want to load Private groups (odd number)",
131 " noseq : user doesn't want to load Sequences ",
132 " debug : user wants to run the program in 'debug mode' ",
133 " showlut :user wants to display the Palette Color (as an int array)",
136 // Initialize Arguments Manager
137 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
139 if (argc == 1 || am->ArgMgrDefined("usage") )
141 am->ArgMgrUsage(usage); // Display 'usage'
146 char *fileName = am->ArgMgrGetString("filein",(char *)0);
147 char *dirName = am->ArgMgrGetString("dirin",(char *)0);
149 if ( (fileName == 0 && dirName == 0)
151 (fileName != 0 && dirName != 0) )
153 std::cout <<std::endl
154 << "Either 'filein=' or 'dirin=' must be present;"
155 << std::endl << "Not both" << std::endl;
156 am->ArgMgrUsage(usage); // Display 'usage'
161 if (am->ArgMgrDefined("debug"))
162 gdcm::Debug::DebugOn();
164 int loadMode = 0x00000000;
165 if ( am->ArgMgrDefined("noshadowseq") )
166 loadMode |= NO_SHADOWSEQ;
169 if ( am->ArgMgrDefined("noshadow") )
170 loadMode |= NO_SHADOW;
171 if ( am->ArgMgrDefined("noseq") )
175 int level = am->ArgMgrGetInt("level", 2);
177 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
179 /* if unused Param we give up */
180 if ( am->ArgMgrPrintUnusedLabels() )
182 am->ArgMgrUsage(usage);
187 delete am; // we don't need Argument Manager any longer
189 // ----------- End Arguments Manager ---------
191 if ( fileName != 0 ) // ====== Deal with a single file ======
193 // gdcm::File::IsReadable() is no usable here, because we deal with
194 // any kind of gdcm-Parsable *document*
195 // not only gdcm::File (as opposed to gdcm::DicomDir)
197 gdcm::File *f = new gdcm::File();
198 f->SetLoadMode(loadMode);
199 f->SetFileName( fileName );
200 bool res = f->Load();
204 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
205 std::cout << "Either it doesn't exist, or it's read protected "
207 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
209 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
215 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
216 fh->SetPrintLevel( level );
220 std::cout << "\n\n" << std::endl;
222 std::cout <<std::endl;
223 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
224 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
226 int nX,nY,nZ,sPP,planarConfig;
227 std::string pixelType;
231 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
233 pixelType = f->GetPixelType();
234 sPP = f->GetSamplesPerPixel();
235 planarConfig = f->GetPlanarConfiguration();
237 std::cout << " pixelType= [" << pixelType
238 << "] SamplesPerPixel= [" << sPP
239 << "] PlanarConfiguration= [" << planarConfig
241 << " PhotometricInterpretation= ["
242 << f->GetEntryValue(0x0028,0x0004)
245 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
246 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
248 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
251 if ( f->GetEntryValue(0x0002,0x0010) == gdcm::GDCM_NOTLOADED )
253 std::cout << "Transfer Syntax not loaded. " << std::endl
254 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
259 std::string transferSyntaxName = f->GetTransferSyntaxName();
260 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
262 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
264 //std::cout << "\n\n" << std::endl;
265 //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
266 //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
267 //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
269 // Display the LUT as an int array (for debugging purpose)
270 if ( f->HasLUT() && showlut )
272 uint8_t* lutrgba = fh->GetLutRGBA();
275 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
277 // Nothing is written yet to get LUT Data user friendly
278 // The following is to be moved into a PixelRedaConvert method
280 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
283 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
286 std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
287 int length; // LUT length in Bytes
288 int deb; // Subscript of the first Lut Value
289 int nbits; // Lut item size (in Bits)
290 int nbRead; // nb of items in LUT descriptor (must be = 3)
292 nbRead = sscanf( lutDescriptor.c_str(),
294 &length, &deb, &nbits );
297 //gdcmWarningMacro( "Wrong LUT descriptor" );
298 std::cout << "Wrong LUT descriptor" << std::endl;
300 gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
303 if ( b->GetLength() != 0 )
305 std::cout << "---------------------------------------"
306 << " We should never reach this point "
308 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
314 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
318 if ( fh->GetLutItemSize() == 8 )
320 for (int i=0;i<fh->GetLutItemNumber();i++)
321 std::cout << i << " : \t"
322 << (int)(lutrgba[i*4]) << " "
323 << (int)(lutrgba[i*4+1]) << " "
324 << (int)(lutrgba[i*4+2]) << std::endl;
326 else // LutItemSize assumed to be = 16
328 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
329 for (int i=0;i<fh->GetLutItemNumber();i++)
330 std::cout << i << " : \t"
331 << (int)(lutrgba16[i*4]) << " "
332 << (int)(lutrgba16[i*4+1]) << " "
333 << (int)(lutrgba16[i*4+2]) << std::endl;
339 std::cout << "Try LUT Data "<< std::endl;
344 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
346 std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
347 std::cout<<std::flush;
352 else // ====== Deal with a Directory ======
354 std::cout << "dirName [" << dirName << "]" << std::endl;
355 gdcm::DirList dirList(dirName,1); // gets recursively the file list
356 gdcm::DirListType fileList = dirList.GetFilenames();
359 for( gdcm::DirListType::iterator it = fileList.begin();
360 it != fileList.end();
363 f = new gdcm::File();
364 f->SetLoadMode(loadMode);
365 f->SetFileName( it->c_str() );
370 std::cout << "Cannot process file [" << it->c_str() << "]"
372 std::cout << "Either it doesn't exist, or it's read protected "
374 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
376 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
382 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
383 fh->SetPrintLevel( level );
388 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
390 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
391 std::cout << "\n\n" << std::endl;
395 std::cout<<std::flush;