1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/11/14 12:27:57 $
7 Version: $Revision: 1.54 $
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"
32 //--> The .tst files *must* be generated on a Little Endian based computer.
35 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
36 * /remarks The Test file format is (only in little endian) :
41 * - 2 bytes : scalar size (8,16,32) --> ?!? 1 or 2 only in DICOM V3 !
42 * - 2 bytes : number of components per pixel (1,2,3) ---> 1 or 3 only in DICOMV3 !
51 bool IsReadable() {return readable;}
53 int GetXSize() {return SizeX;}
54 int GetYSize() {return SizeY;}
55 int GetZSize() {return SizeZ;}
57 void SetXSize(int size) {SizeX = size;}
58 void SetYSize(int size) {SizeY = size;}
59 void SetZSize(int size) {SizeZ = size;}
61 int GetScalarSize() {return ScalarSize;}
62 void SetScalarSize(int size) {ScalarSize = size;}
64 int GetNumberOfComponents() {return Components;}
65 void SetNumberOfComponents(int size) {Components = size;}
66 int GetSwapCode() {return SwapCode;}
68 unsigned long GetDataSize() {return GetLineSize()*SizeY*SizeZ;}
69 uint8_t *GetData() {return Data;}
70 void SetData(const uint8_t *newData);
72 void Load(const std::string &filename);
73 void Write(const std::string &filename);
76 unsigned long GetLineSize() {return SizeX*ScalarSize*Components;}
77 int ComputeSwapCode(uint32_t tag);
83 bool ReadFileHeader(std::ifstream *fp);
84 bool ReadFileData(std::ifstream *fp);
86 bool WriteFileHeader(std::ofstream *fp);
87 bool WriteFileData(std::ofstream *fp);
89 uint8_t ReadInt8 (std::ifstream *fp)
90 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
91 throw( std::ios::failure );
95 uint16_t ReadInt16(std::ifstream *fp)
96 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
97 throw( std::ios::failure );
101 uint32_t ReadInt32(std::ifstream *fp)
102 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
103 throw( std::ios::failure );
107 void WriteInt8 (std::ofstream *fp,uint8_t value);
108 void WriteInt16(std::ofstream *fp,uint16_t value);
109 void WriteInt32(std::ofstream *fp,uint32_t value);
111 std::string fileName;
122 static const unsigned int HEADER_SIZE;
125 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
126 const unsigned int TestFile::HEADER_SIZE = 20;
143 TestFile::~TestFile()
148 void TestFile::SetData(const uint8_t *newData)
153 memcpy(Data,newData,GetDataSize());
156 void TestFile::Load(const std::string &filename)
162 void TestFile::Write(const std::string &filename)
168 int TestFile::ComputeSwapCode(uint32_t tag)
173 switch(tag&0x000000FF)
195 void TestFile::NewData()
198 if( GetDataSize() == 0 )
200 Data = new uint8_t[GetDataSize()];
203 void TestFile::DeleteData()
210 void TestFile::ReadFile()
213 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
223 readable=ReadFileHeader(&fp);
226 std::cout << "Problems when reading Header part" << std::endl;
231 readable=ReadFileData(&fp);
234 std::cout << "Problems when reading data" << std::endl;
249 bool TestFile::ReadFileHeader(std::ifstream *fp)
251 uint32_t tag = ReadInt32(fp);
252 SwapCode = ComputeSwapCode(tag);
255 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
259 SizeX = ReadInt32(fp); // Size X
260 SizeY = ReadInt32(fp); // Size Y
261 SizeZ = ReadInt32(fp); // Size Z
262 ScalarSize = ReadInt16(fp)/8; // bytes per scalar
263 Components = ReadInt16(fp); // Number of components
268 bool TestFile::ReadFileData(std::ifstream *fp)
278 fp->read((char *)Data,GetDataSize());
280 // Track BigEndian troubles
281 std::cout << " ScalarSize : " << GetScalarSize()
282 << " SwapCode:" << GetSwapCode()
285 if (GetScalarSize() == 1 || GetSwapCode() == 1234)
289 // We *know* the .tst files are written in 'Little Endian' format.
290 // We *know* DataSize may be 1 or 2 !
293 std::cout << " Let's swap Pixels" <<std::endl;
295 for (unsigned int i=0; i<GetDataSize()/2; i++)
297 g = ((uint16_t *)Data)[i];
298 g = ( g << 8 | g >> 8 );
299 ((uint16_t *)Data)[i] = g;
304 void TestFile::WriteFile()
306 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
314 WriteFileHeader(&fp);
320 bool TestFile::WriteFileHeader(std::ofstream *fp)
322 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
323 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
324 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
325 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
326 WriteInt32(fp,SizeX); // Size X
327 WriteInt32(fp,SizeY); // Size Y
328 WriteInt32(fp,SizeZ); // Size Z
329 WriteInt16(fp,ScalarSize*8); // bits per scalar
330 WriteInt16(fp,Components); // number of components
335 bool TestFile::WriteFileData(std::ofstream *fp)
337 fp->write((char *)Data,GetDataSize());
342 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
343 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
344 throw( std::ios::failure )
348 fp->read ((char*)&g, (size_t)1);
349 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
351 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
353 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
358 uint16_t TestFile::ReadInt16(std::ifstream *fp)
359 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
360 throw( std::ios::failure )
364 fp->read ((char*)&g, (size_t)2);
365 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
367 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
369 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
372 #if defined(GDCM_WORDS_BIGENDIAN)
373 g = ( g << 8 | g >> 8 );
378 uint32_t TestFile::ReadInt32(std::ifstream *fp)
379 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
380 throw( std::ios::failure )
384 fp->read ((char*)&g, (size_t)4);
385 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
387 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
389 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
392 #if defined(GDCM_WORDS_BIGENDIAN)
393 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
394 ( (g>>8) & 0x0000ff00) | (g>>24) );
399 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
401 fp->write((char*)&value, (size_t)1);
404 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
406 #if defined(GDCM_WORDS_BIGENDIAN)
407 value = ( value << 8 | value >> 8 );
409 fp->write((char*)&value, (size_t)2);
412 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
414 #if defined(GDCM_WORDS_BIGENDIAN)
415 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
416 ( (value>>8) & 0x0000ff00) | (value>>24) );
418 fp->write((char*)&value, (size_t)4);
421 int InternalTest(std::string const &filename,
422 std::string const &referenceFileName )
424 std::cout << " Testing: " << filename << std::endl;
431 gdcm::File *f = gdcm::File::New();
432 f->SetLoadMode ( gdcm::LD_ALL ); // Load everything
433 f->SetFileName( filename );
436 if( !f->IsReadable() )
438 std::cout << " Failed" << std::endl
439 << " Image not gdcm compatible:"
440 << filename << std::endl;
444 gdcm::FileHelper *tested = gdcm::FileHelper::New( f );
447 ////// Check for existence of reference baseline dicom file:
450 TestFile *reference = new TestFile();
451 std::ifstream refFile(referenceFileName.c_str(),
452 std::ios::binary|std::ios::in);
455 std::cout << " Failed" << std::endl
456 << " Image not found:"
457 << referenceFileName << std::endl;
458 reference->SetXSize(tested->GetFile()->GetXSize());
459 reference->SetYSize(tested->GetFile()->GetYSize());
460 reference->SetZSize(tested->GetFile()->GetZSize());
461 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
462 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
463 reference->SetData(tested->GetImageData());
464 reference->Write(referenceFileName);
469 reference->Load(referenceFileName);
470 if(!reference->IsReadable())
472 std::cout << " Failed" << std::endl
473 << " Image not Testing compatible:"
474 << filename << std::endl;
482 std::string PixelType = tested->GetFile()->GetPixelType();
484 int testedDataSize = tested->GetImageDataSize();
485 uint8_t *testedImageData = tested->GetImageData();
487 int referenceDataSize = reference->GetDataSize();
488 uint8_t *referenceImageData = reference->GetData();
490 // Test the image size
491 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
492 tested->GetFile()->GetYSize() != reference->GetYSize() ||
493 tested->GetFile()->GetZSize() != reference->GetZSize())
495 std::cout << "Failed" << std::endl
497 << "X: " << tested->GetFile()->GetXSize() << " # "
498 << reference->GetXSize() << " | "
499 << "Y: " << tested->GetFile()->GetYSize() << " # "
500 << reference->GetYSize() << " | "
501 << "Z: " << tested->GetFile()->GetZSize() << " # "
502 << reference->GetZSize() << std::endl;
509 // Test the pixel size
510 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
511 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
513 std::cout << "Failed" << std::endl
514 << " Pixel size differs: " << std::endl
515 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
516 << reference->GetScalarSize() << std::endl
517 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
518 << reference->GetNumberOfComponents() << std::endl
519 << " Pixel type: " << tested->GetFile()->GetPixelType() << std::endl;
526 // Test the data size
527 if (testedDataSize != referenceDataSize)
529 std::cout << " Failed" << std::endl
532 <<") areas lengths differ: "
533 << testedDataSize << " # " << referenceDataSize
536 << tested->GetFile()->GetXSize() << ","
537 << tested->GetFile()->GetYSize() << ","
538 << tested->GetFile()->GetZSize() << ")"
546 // Test the data content
547 if ( memcmp(testedImageData, referenceImageData,
548 testedDataSize) != 0 )
550 std::string ts = tested->GetFile()->GetTransferSyntax();
552 std::cout << " Failed" << std::endl
555 << ") differ (as expanded in memory)."
558 << gdcm::Global::GetTS()->GetValue(ts) << std::endl;
560 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
561 << " pixels differing (pos : test - ref) :"
565 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
567 if(testedImageData[i]!=referenceImageData[i])
569 std::cout << std::hex << "(" << i << " : "
570 << std::hex << (int)(testedImageData[i]) << " - "
571 << std::hex << (int)(referenceImageData[i]) << ") "
576 std::cout << std::endl;
584 //////////////// Clean up:
589 std::cout << "OK." << std::endl;
594 int TestAllReadCompareDicom(int argc, char *argv[])
596 // Temporarily added, to track BigEndian troubles
597 gdcm::Debug::WarningOn();
600 gdcm::Debug::DebugOn();
604 // The test is specified a specific filename, use it instead of looping
606 const std::string input = argv[1];
607 const std::string reference = argv[2];
608 return InternalTest( input, reference );
610 else if ( argc > 4 || argc == 2 )
612 std::cerr << " Usage: " << argv[0]
613 << " (no arguments needed)." << std::endl;
614 std::cerr << "or Usage: " << argv[0]
615 << " filename.dcm reference.dcm" << std::endl;
620 std::cout << " Description (Test::TestAllReadCompareDicom): "
622 std::cout << " For all images in gdcmData (and not blacklisted in "
623 "Test/CMakeLists.txt)"
625 std::cout << " apply the following to each filename.xxx: "
627 std::cout << " step 1: parse the image (as gdcmFile) and call"
630 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
632 << " special internal file format containing the"
634 << " caracteristic of the image and the pixel data "
635 << "(uncompressed). This file is written if it's not found."
637 std::cout << " step 3: compare the DICOM image with the reference image"
639 << " (.tst file). The test is made on the caracteristics"
641 << " of the image and the pixel data"
642 << std::endl << std::endl;
646 while( gdcmDataImages[i] != 0 )
648 ////// Check for existence of reference baseline directory
650 std::string baseLineDir = GDCM_DATA_ROOT;
651 baseLineDir += "/BaselineDicom";
653 if( !gdcm::DirList::IsDirectory(baseLineDir) )
655 std::cerr << " The reference baseline directory " << std::endl
657 << baseLineDir << std::endl
658 << " couldn't be opened."
663 ////// Step 1 (see above description):
664 std::string filename = GDCM_DATA_ROOT;
666 filename += gdcmDataImages[i];
669 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
670 std::string::size_type slash_pos = referenceFileName.rfind( "." );
671 if( slash_pos != std::string::npos )
673 referenceFileName.replace( slash_pos + 1, 3, "tst" );
676 if( InternalTest( filename, referenceFileName ) != 0 )