1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2007/07/26 09:25:38 $
7 Version: $Revision: 1.61 $
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"
29 #include "gdcmDataImages.h"
33 //--> The .tst files *must* be generated on a Little Endian based computer.
36 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
37 * /remarks The Test file format is (only in little endian) :
42 * - 2 bytes : scalar size (8,16,32) --> ?!? 1 or 2 only in DICOM V3 !
43 * - 2 bytes : number of components per pixel (1,2,3) ---> 1 or 3 only in DICOMV3 !
52 bool IsReadable() {return readable;}
54 int GetXSize() {return SizeX;}
55 int GetYSize() {return SizeY;}
56 int GetZSize() {return SizeZ;}
58 void SetXSize(int size) {SizeX = size;}
59 void SetYSize(int size) {SizeY = size;}
60 void SetZSize(int size) {SizeZ = size;}
62 int GetScalarSize() {return ScalarSize;}
63 void SetScalarSize(int size) {ScalarSize = size;}
65 int GetNumberOfComponents() {return Components;}
66 void SetNumberOfComponents(int size) {Components = size;}
67 int GetSwapCode() {return SwapCode;}
69 unsigned long GetDataSize() {return GetLineSize()*SizeY*SizeZ;}
70 uint8_t *GetData() {return Data;}
71 void SetData(const uint8_t *newData);
73 void Load(const std::string &filename);
74 void Write(const std::string &filename);
77 unsigned long GetLineSize() {return SizeX*ScalarSize*Components;}
78 int ComputeSwapCode(uint32_t tag);
84 bool ReadFileHeader(std::ifstream *fp);
85 bool ReadFileData(std::ifstream *fp);
87 bool WriteFileHeader(std::ofstream *fp);
88 bool WriteFileData(std::ofstream *fp);
90 uint8_t ReadInt8 (std::ifstream *fp)
91 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
92 throw( std::ios::failure );
96 uint16_t ReadInt16(std::ifstream *fp)
97 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
98 throw( std::ios::failure );
102 uint32_t ReadInt32(std::ifstream *fp)
103 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
104 throw( std::ios::failure );
108 void WriteInt8 (std::ofstream *fp,uint8_t value);
109 void WriteInt16(std::ofstream *fp,uint16_t value);
110 void WriteInt32(std::ofstream *fp,uint32_t value);
112 std::string fileName;
123 static const unsigned int HEADER_SIZE;
126 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
127 const unsigned int TestFile::HEADER_SIZE = 20;
144 TestFile::~TestFile()
149 void TestFile::SetData(const uint8_t *newData)
154 memcpy(Data,newData,GetDataSize());
157 void TestFile::Load(const std::string &filename)
163 void TestFile::Write(const std::string &filename)
169 int TestFile::ComputeSwapCode(uint32_t tag)
171 // FIXME : 100 % useless method !
172 // "gdcm" was written on disc byte per byte.
173 // when you fread it, you'll get *always* "gdcm"
174 // whatever the processor indianess is !
177 //std::cout << std::hex << "0x(" << tag << ")" << std::dec << std::endl;
181 switch(tag&0x000000FF)
200 //std::cout << std::hex << "0x(" << tag << ")" << std::dec << tag << std::endl;
204 void TestFile::NewData()
207 if( GetDataSize() == 0 )
209 Data = new uint8_t[GetDataSize()];
212 void TestFile::DeleteData()
219 void TestFile::ReadFile()
222 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
232 readable=ReadFileHeader(&fp);
235 std::cout << "Problems when reading Header part" << std::endl;
240 readable=ReadFileData(&fp);
243 std::cout << "Problems when reading data" << std::endl;
258 bool TestFile::ReadFileHeader(std::ifstream *fp)
260 uint32_t tag = ReadInt32(fp);
261 SwapCode = ComputeSwapCode(tag);
264 // We shall *never* come here!
265 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
269 SizeX = ReadInt32(fp); // Size X
270 SizeY = ReadInt32(fp); // Size Y
271 SizeZ = ReadInt32(fp); // Size Z
272 ScalarSize = ReadInt16(fp)/8; // bytes per scalar
273 Components = ReadInt16(fp); // Number of components
278 bool TestFile::ReadFileData(std::ifstream *fp)
287 // Read data Note : .tst images are *always* created
288 // on little endian processor !
289 fp->read((char *)Data,GetDataSize());
291 // Track BigEndian troubles
292 std::cout << " ScalarSize : " << GetScalarSize()
293 << " IsCurrentProcessorBigEndian:"
294 << GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian()
297 //if (GetScalarSize() == 1 || GetSwapCode() == 1234)
298 if (GetScalarSize() == 1 || !GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian() )
302 // We *know* the .tst files are written in 'Little Endian' format.
303 // We *know* DataSize may be 1 or 2 !
306 std::cout << " Let's swap Pixels" <<std::endl;
308 for (unsigned int i=0; i<GetDataSize()/2; i++)
310 g = ((uint16_t *)Data)[i];
311 g = ( g << 8 | g >> 8 );
312 ((uint16_t *)Data)[i] = g;
317 void TestFile::WriteFile()
319 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
327 WriteFileHeader(&fp);
333 bool TestFile::WriteFileHeader(std::ofstream *fp)
335 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
336 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
337 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
338 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
340 // FIXME : Think of writting an int32, better !
341 // (('g' << 8 + 'd') << 8 + 'c') + 'm'
342 // if you want to use it to check the endianess.
343 // (and upload again *all* the .tst files ...)
344 WriteInt32(fp,SizeX); // Size X
345 WriteInt32(fp,SizeY); // Size Y
346 WriteInt32(fp,SizeZ); // Size Z
347 WriteInt16(fp,ScalarSize*8); // bits per scalar
348 WriteInt16(fp,Components); // number of components
353 bool TestFile::WriteFileData(std::ofstream *fp)
355 fp->write((char *)Data,GetDataSize());
360 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
361 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
362 throw( std::ios::failure )
366 fp->read ((char*)&g, (size_t)1);
367 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
369 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
371 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
376 uint16_t TestFile::ReadInt16(std::ifstream *fp)
377 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
378 throw( std::ios::failure )
382 fp->read ((char*)&g, (size_t)2);
383 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
385 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
387 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
390 #if defined(GDCM_WORDS_BIGENDIAN)
391 g = ( g << 8 | g >> 8 );
396 uint32_t TestFile::ReadInt32(std::ifstream *fp)
397 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
398 throw( std::ios::failure )
402 fp->read ((char*)&g, (size_t)4);
403 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
405 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
407 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
410 #if defined(GDCM_WORDS_BIGENDIAN)
411 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
412 ( (g>>8) & 0x0000ff00) | (g>>24) );
417 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
419 fp->write((char*)&value, (size_t)1);
422 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
424 #if defined(GDCM_WORDS_BIGENDIAN)
425 value = ( value << 8 | value >> 8 );
427 fp->write((char*)&value, (size_t)2);
430 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
432 #if defined(GDCM_WORDS_BIGENDIAN)
433 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
434 ( (value>>8) & 0x0000ff00) | (value>>24) );
436 fp->write((char*)&value, (size_t)4);
439 int InternalTest(std::string const &filename,
440 std::string const &referenceFileName )
442 std::cout << " Testing: " << filename << std::endl;
449 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
450 f->SetLoadMode ( GDCM_NAME_SPACE::LD_ALL ); // Load everything
451 f->SetFileName( filename );
454 if( !f->IsReadable() )
456 std::cout << " Failed" << std::endl
457 << " Image not gdcm compatible:"
458 << filename << std::endl;
462 GDCM_NAME_SPACE::FileHelper *tested = GDCM_NAME_SPACE::FileHelper::New( f );
465 ////// Check for existence of reference baseline dicom file:
468 TestFile *reference = new TestFile();
469 std::ifstream refFile(referenceFileName.c_str(),
470 std::ios::binary|std::ios::in);
473 std::cout << " Failed" << std::endl
474 << " Image not found:"
475 << referenceFileName << std::endl;
476 reference->SetXSize(tested->GetFile()->GetXSize());
477 reference->SetYSize(tested->GetFile()->GetYSize());
478 reference->SetZSize(tested->GetFile()->GetZSize());
479 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
480 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
481 reference->SetData(tested->GetImageData());
482 reference->Write(referenceFileName);
487 reference->Load(referenceFileName);
488 if(!reference->IsReadable())
490 std::cout << " Failed" << std::endl
491 << " Image not Testing compatible:"
492 << filename << std::endl;
500 std::string PixelType = tested->GetFile()->GetPixelType();
502 int testedDataSize = tested->GetImageDataSize();
503 uint8_t *testedImageData = tested->GetImageData();
505 int referenceDataSize = reference->GetDataSize();
506 uint8_t *referenceImageData = reference->GetData();
508 // Test the image size
509 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
510 tested->GetFile()->GetYSize() != reference->GetYSize() ||
511 tested->GetFile()->GetZSize() != reference->GetZSize())
513 std::cout << "Failed" << std::endl
515 << "X: " << tested->GetFile()->GetXSize() << " # "
516 << reference->GetXSize() << " | "
517 << "Y: " << tested->GetFile()->GetYSize() << " # "
518 << reference->GetYSize() << " | "
519 << "Z: " << tested->GetFile()->GetZSize() << " # "
520 << reference->GetZSize() << std::endl;
527 // Test the pixel size
528 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
529 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
531 std::cout << "Failed" << std::endl
532 << " Pixel size differs: " << std::endl
533 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
534 << reference->GetScalarSize() << std::endl
535 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
536 << reference->GetNumberOfComponents() << std::endl
537 << " Pixel type: " << tested->GetFile()->GetPixelType() << std::endl;
544 // Test the data size
545 // *actual* image length may differ to 1 with Pixel Data Element length!
546 if ((testedDataSize+testedDataSize%2) !=
547 (referenceDataSize+referenceDataSize%2) )
549 std::cout << " Failed" << std::endl
552 <<") areas lengths differ: "
553 << testedDataSize << " # " << referenceDataSize
556 << tested->GetFile()->GetXSize() << ","
557 << tested->GetFile()->GetYSize() << ","
558 << tested->GetFile()->GetZSize() << ") nb of scalar components "
559 << tested->GetFile()->GetNumberOfScalarComponents()
567 // Test the data content
568 int length = tested->GetFile()->GetXSize()*tested->GetFile()->GetYSize()*tested->GetFile()->GetZSize()
569 *reference->GetScalarSize()*tested->GetFile()->GetNumberOfScalarComponents();
571 // *actual* image length may differ to 1 with Pixel Data Element length!
572 if (length != testedDataSize)
573 std::cout <<"--------------------length " << length << " != testedDataSize " << testedDataSize << std::endl;
574 if ( memcmp(testedImageData, referenceImageData,
575 length/*testedDataSize*/) != 0 )
577 std::string ts = tested->GetFile()->GetTransferSyntax();
579 std::cout << " Failed" << std::endl
582 << ") differ (as expanded in memory)."
585 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
587 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
588 << " pixels differing (pos : test - ref) :"
592 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
594 if(testedImageData[i]!=referenceImageData[i])
596 std::cout << std::hex << "(" << i << " : "
597 << std::hex << (int)(testedImageData[i]) << " - "
598 << std::hex << (int)(referenceImageData[i]) << ") "
603 std::cout << std::endl;
611 //////////////// Clean up:
616 std::cout << "OK." << std::endl;
621 int TestAllReadCompareDicom(int argc, char *argv[])
623 // Temporarily added, to track BigEndian troubles
624 GDCM_NAME_SPACE::Debug::WarningOn();
627 GDCM_NAME_SPACE::Debug::DebugOn();
631 // The test is specified a specific filename, use it instead of looping
633 const std::string input = argv[1];
634 const std::string reference = argv[2];
635 return InternalTest( input, reference );
637 else if ( argc > 4 || argc == 2 )
639 std::cerr << " Usage: " << argv[0]
640 << " (no arguments needed)." << std::endl;
641 std::cerr << "or Usage: " << argv[0]
642 << " filename.dcm reference.dcm" << std::endl;
647 std::cout << " Description (Test::TestAllReadCompareDicom): "
649 std::cout << " For all images in gdcmData (and not blacklisted in "
650 "Test/CMakeLists.txt)"
652 std::cout << " apply the following to each filename.xxx: "
654 std::cout << " step 1: parse the image (as gdcmFile) and call"
657 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
659 << " special internal file format containing the"
661 << " caracteristic of the image and the pixel data "
662 << "(uncompressed). This file is written if it's not found."
664 std::cout << " step 3: compare the DICOM image with the reference image"
666 << " (.tst file). The test is made on the caracteristics"
668 << " of the image and the pixel data"
669 << std::endl << std::endl;
673 while( gdcmDataImages[i] != 0 )
675 ////// Check for existence of reference baseline directory
677 std::string baseLineDir = GDCM_DATA_ROOT;
678 baseLineDir += "/BaselineDicom";
680 if( !GDCM_NAME_SPACE::DirList::IsDirectory(baseLineDir) )
682 std::cerr << " The reference baseline directory " << std::endl
684 << baseLineDir << std::endl
685 << " couldn't be opened."
690 //if (gdcmDataImages[i] == "D_CLUNIE_CT2_RLE.dcm")
691 // gdcm::Debug::DebugOn(); // track pb on BigEndian Proc
693 GDCM_NAME_SPACE::Debug::DebugOff();
695 ////// Step 1 (see above description):
696 std::string filename = GDCM_DATA_ROOT;
698 filename += gdcmDataImages[i];
701 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
702 std::string::size_type slash_pos = referenceFileName.rfind( "." );
703 if( slash_pos != std::string::npos )
705 referenceFileName.replace( slash_pos + 1, 3, "tst" );
708 if( InternalTest( filename, referenceFileName ) != 0 )