1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/10/18 08:35:46 $
7 Version: $Revision: 1.48 $
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 =========================================================================*/
18 #include "gdcmDirList.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmGlobal.h"
23 #include "gdcmDebug.h"
28 #include "gdcmDataImages.h"
31 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
32 * /remarks The Test file format is (only in little endian) :
37 * - 2 bytes : scalar size (8,16,32)
38 * - 2 bytes : number of components per pixel (1,2,3)
47 bool IsReadable(void) {return readable;}
48 int GetXSize(void) {return sizeX;};
49 void SetXSize(int size) {sizeX = size;};
50 int GetYSize(void) {return sizeY;};
51 void SetYSize(int size) {sizeY = size;};
52 int GetZSize(void) {return sizeZ;};
53 void SetZSize(int size) {sizeZ = size;};
54 int GetScalarSize(void) {return scalarSize;};
55 void SetScalarSize(int size) {scalarSize = size;};
56 int GetNumberOfComponents(void) {return components;};
57 void SetNumberOfComponents(int size) {components = size;};
59 unsigned long GetDataSize(void) {return GetLineSize()*sizeY*sizeZ;}
60 uint8_t *GetData(void) {return data;}
61 void SetData(const uint8_t *newData);
63 void Load(const std::string &filename);
64 void Write(const std::string &filename);
67 unsigned long GetLineSize(void) {return sizeX*scalarSize*components;}
68 int GetSwapCode(uint32_t tag);
71 void DeleteData(void);
74 bool ReadFileHeader(std::ifstream *fp);
75 bool ReadFileData(std::ifstream *fp);
77 bool WriteFileHeader(std::ofstream *fp);
78 bool WriteFileData(std::ofstream *fp);
80 uint8_t ReadInt8 (std::ifstream *fp)
81 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
82 throw( std::ios::failure );
86 uint16_t ReadInt16(std::ifstream *fp)
87 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
88 throw( std::ios::failure );
92 uint32_t ReadInt32(std::ifstream *fp)
93 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
94 throw( std::ios::failure );
98 void WriteInt8 (std::ofstream *fp,uint8_t value);
99 void WriteInt16(std::ofstream *fp,uint16_t value);
100 void WriteInt32(std::ofstream *fp,uint32_t value);
102 std::string fileName;
113 static const unsigned int HEADER_SIZE;
116 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
117 const unsigned int TestFile::HEADER_SIZE = 20;
134 TestFile::~TestFile(void)
139 void TestFile::SetData(const uint8_t *newData)
144 memcpy(data,newData,GetDataSize());
147 void TestFile::Load(const std::string &filename)
153 void TestFile::Write(const std::string &filename)
159 int TestFile::GetSwapCode(uint32_t tag)
164 switch(tag&0x000000FF)
186 void TestFile::NewData(void)
189 if( GetDataSize() == 0 )
191 data = new uint8_t[GetDataSize()];
194 void TestFile::DeleteData(void)
201 void TestFile::ReadFile(void)
204 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
214 readable=ReadFileHeader(&fp);
217 std::cout << "Problems when reading Header part" << std::endl;
222 readable=ReadFileData(&fp);
225 std::cout << "Problems when reading datas" << std::endl;
240 bool TestFile::ReadFileHeader(std::ifstream *fp)
242 uint32_t tag = ReadInt32(fp);
243 swapCode = GetSwapCode(tag);
246 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
250 sizeX = ReadInt32(fp); // Size X
251 sizeY = ReadInt32(fp); // Size Y
252 sizeZ = ReadInt32(fp); // Size Z
253 scalarSize = ReadInt16(fp)/8; // bits per scalar
254 components = ReadInt16(fp); // Number of components
259 bool TestFile::ReadFileData(std::ifstream *fp)
269 fp->read((char *)data,GetDataSize());
274 void TestFile::WriteFile(void)
276 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
284 WriteFileHeader(&fp);
290 bool TestFile::WriteFileHeader(std::ofstream *fp)
292 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
293 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
294 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
295 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
296 WriteInt32(fp,sizeX); // Size X
297 WriteInt32(fp,sizeY); // Size Y
298 WriteInt32(fp,sizeZ); // Size Z
299 WriteInt16(fp,scalarSize*8); // bits per scalar
300 WriteInt16(fp,components); // number of components
305 bool TestFile::WriteFileData(std::ofstream *fp)
307 fp->write((char *)data,GetDataSize());
312 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
313 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
314 throw( std::ios::failure )
318 fp->read ((char*)&g, (size_t)1);
319 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
321 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
323 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
328 uint16_t TestFile::ReadInt16(std::ifstream *fp)
329 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
330 throw( std::ios::failure )
334 fp->read ((char*)&g, (size_t)2);
335 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
337 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
339 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
342 #if defined(GDCM_WORDS_BIGENDIAN)
343 g = ( g << 8 | g >> 8 );
348 uint32_t TestFile::ReadInt32(std::ifstream *fp)
349 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
350 throw( std::ios::failure )
354 fp->read ((char*)&g, (size_t)4);
355 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
357 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
359 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
362 #if defined(GDCM_WORDS_BIGENDIAN)
363 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
364 ( (g>>8) & 0x0000ff00) | (g>>24) );
369 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
371 fp->write((char*)&value, (size_t)1);
374 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
376 #if defined(GDCM_WORDS_BIGENDIAN)
377 value = ( value << 8 | value >> 8 );
379 fp->write((char*)&value, (size_t)2);
382 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
384 #if defined(GDCM_WORDS_BIGENDIAN)
385 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
386 ( (value>>8) & 0x0000ff00) | (value>>24) );
388 fp->write((char*)&value, (size_t)4);
391 int InternalTest(std::string const &filename,
392 std::string const &referenceFileName )
394 std::cout << " Testing: " << filename << std::endl;
401 gdcm::File *f = new gdcm::File();
402 f->SetLoadMode ( gdcm::LD_ALL ); // Load everything
403 f->SetFileName( filename );
406 if( !f->IsReadable() )
408 std::cout << " Failed" << std::endl
409 << " Image not gdcm compatible:"
410 << filename << std::endl;
414 gdcm::FileHelper *tested = new gdcm::FileHelper( f );
417 ////// Check for existence of reference baseline dicom file:
420 TestFile *reference = new TestFile();
421 std::ifstream refFile(referenceFileName.c_str(),
422 std::ios::binary|std::ios::in);
425 std::cout << " Failed" << std::endl
426 << " Image not found:"
427 << referenceFileName << std::endl;
428 reference->SetXSize(tested->GetFile()->GetXSize());
429 reference->SetYSize(tested->GetFile()->GetYSize());
430 reference->SetZSize(tested->GetFile()->GetZSize());
431 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
432 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
433 reference->SetData(tested->GetImageData());
434 reference->Write(referenceFileName);
439 reference->Load(referenceFileName);
440 if(!reference->IsReadable())
442 std::cout << " Failed" << std::endl
443 << " Image not Testing compatible:"
444 << filename << std::endl;
452 std::string PixelType = tested->GetFile()->GetPixelType();
454 int testedDataSize = tested->GetImageDataSize();
455 uint8_t *testedImageData = tested->GetImageData();
457 int referenceDataSize = reference->GetDataSize();
458 uint8_t *referenceImageData = reference->GetData();
460 // Test the image size
461 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
462 tested->GetFile()->GetYSize() != reference->GetYSize() ||
463 tested->GetFile()->GetZSize() != reference->GetZSize())
465 std::cout << "Failed" << std::endl
467 << "X: " << tested->GetFile()->GetXSize() << " # "
468 << reference->GetXSize() << " | "
469 << "Y: " << tested->GetFile()->GetYSize() << " # "
470 << reference->GetYSize() << " | "
471 << "Z: " << tested->GetFile()->GetZSize() << " # "
472 << reference->GetZSize() << std::endl;
479 // Test the pixel size
480 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
481 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
483 std::cout << "Failed" << std::endl
484 << " Pixel size differs: " << std::endl
485 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
486 << reference->GetScalarSize() << std::endl
487 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
488 << reference->GetNumberOfComponents() << std::endl
489 << " Pixel type: " << tested->GetFile()->GetPixelType() << std::endl;
496 // Test the data size
497 if (testedDataSize != referenceDataSize)
499 std::cout << " Failed" << std::endl
502 <<") areas lengths differ: "
503 << testedDataSize << " # " << referenceDataSize
506 << tested->GetFile()->GetXSize() << ","
507 << tested->GetFile()->GetYSize() << ","
508 << tested->GetFile()->GetZSize() << ")"
516 // Test the data content
517 if (int res = memcmp(testedImageData, referenceImageData,
518 testedDataSize) != 0 )
521 std::string ts = tested->GetFile()->GetTransferSyntax();
523 std::cout << " Failed" << std::endl
526 << ") differ (as expanded in memory)."
529 << gdcm::Global::GetTS()->GetValue(ts) << std::endl;
531 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
532 << " pixels differing (pos : test - ref) :"
536 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
538 if(testedImageData[i]!=referenceImageData[i])
540 std::cout << std::hex << "(" << i << " : "
541 << std::hex << (int)(testedImageData[i]) << " - "
542 << std::hex << (int)(referenceImageData[i]) << ") "
547 std::cout << std::endl;
555 //////////////// Clean up:
560 std::cout << "OK." << std::endl;
565 int TestAllReadCompareDicom(int argc, char *argv[])
568 gdcm::Debug::DebugOn();
572 // The test is specified a specific filename, use it instead of looping
574 const std::string input = argv[1];
575 const std::string reference = argv[2];
576 return InternalTest( input, reference );
578 else if ( argc > 4 || argc == 2 )
580 std::cerr << " Usage: " << argv[0]
581 << " (no arguments needed)." << std::endl;
582 std::cerr << "or Usage: " << argv[0]
583 << " filename.dcm reference.dcm" << std::endl;
588 std::cout << " Description (Test::TestAllReadCompareDicom): "
590 std::cout << " For all images in gdcmData (and not blacklisted in "
591 "Test/CMakeLists.txt)"
593 std::cout << " apply the following to each filename.xxx: "
595 std::cout << " step 1: parse the image (as gdcmFile) and call"
598 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
600 << " special internal file format containing the"
602 << " caracteristic of the image and the pixel datas "
603 << "(uncompressed). This file is written if it's not found."
605 std::cout << " step 3: compare the DICOM image with the reference image"
607 << " (.tst file). The test is made on the caracteristics"
609 << " of the image and the pixel datas"
610 << std::endl << std::endl;
614 while( gdcmDataImages[i] != 0 )
616 ////// Check for existence of reference baseline directory
618 std::string baseLineDir = GDCM_DATA_ROOT;
619 baseLineDir += "/BaselineDicom";
621 if( !gdcm::DirList::IsDirectory(baseLineDir) )
623 std::cerr << " The reference baseline directory " << std::endl
625 << baseLineDir << std::endl
626 << " couldn't be opened."
631 ////// Step 1 (see above description):
632 std::string filename = GDCM_DATA_ROOT;
634 filename += gdcmDataImages[i];
637 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
638 std::string::size_type slash_pos = referenceFileName.rfind( "." );
639 if( slash_pos != std::string::npos )
641 referenceFileName.replace( slash_pos + 1, 3, "tst" );
644 if( InternalTest( filename, referenceFileName ) != 0 )