1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/05/02 17:56:44 $
7 Version: $Revision: 1.42 $
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"
27 #include "gdcmDataImages.h"
30 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
31 * /remarks The Test file format is (only in little endian) :
36 * - 2 bytes : scalar size (8,16,32)
37 * - 2 bytes : number of components per pixel (1,2,3)
46 bool IsReadable(void) {return readable;}
47 int GetXSize(void) {return sizeX;};
48 void SetXSize(int size) {sizeX = size;};
49 int GetYSize(void) {return sizeY;};
50 void SetYSize(int size) {sizeY = size;};
51 int GetZSize(void) {return sizeZ;};
52 void SetZSize(int size) {sizeZ = size;};
53 int GetScalarSize(void) {return scalarSize;};
54 void SetScalarSize(int size) {scalarSize = size;};
55 int GetNumberOfComponents(void) {return components;};
56 void SetNumberOfComponents(int size) {components = size;};
58 unsigned long GetDataSize(void) {return GetLineSize()*sizeY*sizeZ;}
59 uint8_t *GetData(void) {return data;}
60 void SetData(const uint8_t *newData);
62 void Load(const std::string &filename);
63 void Write(const std::string &filename);
66 unsigned long GetLineSize(void) {return sizeX*scalarSize*components;}
67 int GetSwapCode(uint32_t tag);
70 void DeleteData(void);
73 bool ReadFileHeader(std::ifstream *fp);
74 bool ReadFileData(std::ifstream *fp);
76 bool WriteFileHeader(std::ofstream *fp);
77 bool WriteFileData(std::ofstream *fp);
79 uint8_t ReadInt8 (std::ifstream *fp)
80 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
81 throw( std::ios::failure );
85 uint16_t ReadInt16(std::ifstream *fp)
86 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
87 throw( std::ios::failure );
91 uint32_t ReadInt32(std::ifstream *fp)
92 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
93 throw( std::ios::failure );
97 void WriteInt8 (std::ofstream *fp,uint8_t value);
98 void WriteInt16(std::ofstream *fp,uint16_t value);
99 void WriteInt32(std::ofstream *fp,uint32_t value);
101 std::string fileName;
112 static const unsigned int HEADER_SIZE;
115 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
116 const unsigned int TestFile::HEADER_SIZE = 20;
133 TestFile::~TestFile(void)
138 void TestFile::SetData(const uint8_t *newData)
143 memcpy(data,newData,GetDataSize());
146 void TestFile::Load(const std::string &filename)
152 void TestFile::Write(const std::string &filename)
158 int TestFile::GetSwapCode(uint32_t tag)
163 switch(tag&0x000000FF)
185 void TestFile::NewData(void)
188 if( GetDataSize() == 0 )
190 data = new uint8_t[GetDataSize()];
193 void TestFile::DeleteData(void)
200 void TestFile::ReadFile(void)
203 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
213 readable=ReadFileHeader(&fp);
216 std::cout << "Problems when reading Header part" << std::endl;
221 readable=ReadFileData(&fp);
224 std::cout << "Problems when reading datas" << std::endl;
239 bool TestFile::ReadFileHeader(std::ifstream *fp)
241 uint32_t tag = ReadInt32(fp);
242 swapCode = GetSwapCode(tag);
245 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
249 sizeX = ReadInt32(fp); // Size X
250 sizeY = ReadInt32(fp); // Size Y
251 sizeZ = ReadInt32(fp); // Size Z
252 scalarSize = ReadInt16(fp)/8; // bits per scalar
253 components = ReadInt16(fp); // Number of components
258 bool TestFile::ReadFileData(std::ifstream *fp)
268 fp->read((char *)data,GetDataSize());
273 void TestFile::WriteFile(void)
275 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
283 WriteFileHeader(&fp);
289 bool TestFile::WriteFileHeader(std::ofstream *fp)
291 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
292 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
293 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
294 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
295 WriteInt32(fp,sizeX); // Size X
296 WriteInt32(fp,sizeY); // Size Y
297 WriteInt32(fp,sizeZ); // Size Z
298 WriteInt16(fp,scalarSize*8); // bits per scalar
299 WriteInt16(fp,components); // number of components
304 bool TestFile::WriteFileData(std::ofstream *fp)
306 fp->write((char *)data,GetDataSize());
311 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
312 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
313 throw( std::ios::failure )
317 fp->read ((char*)&g, (size_t)1);
318 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
320 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
322 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
327 uint16_t TestFile::ReadInt16(std::ifstream *fp)
328 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
329 throw( std::ios::failure )
333 fp->read ((char*)&g, (size_t)2);
334 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
336 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
338 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
341 #if defined(GDCM_WORDS_BIGENDIAN)
342 g = ( g << 8 | g >> 8 );
347 uint32_t TestFile::ReadInt32(std::ifstream *fp)
348 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
349 throw( std::ios::failure )
353 fp->read ((char*)&g, (size_t)4);
354 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
356 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
358 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
361 #if defined(GDCM_WORDS_BIGENDIAN)
362 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
363 ( (g>>8) & 0x0000ff00) | (g>>24) );
368 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
370 fp->write((char*)&value, (size_t)1);
373 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
375 #if defined(GDCM_WORDS_BIGENDIAN)
376 value = ( value << 8 | value >> 8 );
378 fp->write((char*)&value, (size_t)2);
381 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
383 #if defined(GDCM_WORDS_BIGENDIAN)
384 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
385 ( (value>>8) & 0x0000ff00) | (value>>24) );
387 fp->write((char*)&value, (size_t)4);
390 int InternalTest(std::string const &filename,
391 std::string const &referenceFileName )
393 std::cout << " Testing: " << filename << std::endl;
398 gdcm::FileHelper *tested = new gdcm::FileHelper( filename );
399 if( !tested->GetFile()->IsReadable() )
401 std::cout << " Failed" << std::endl
402 << " Image not gdcm compatible:"
403 << filename << std::endl;
409 ////// Check for existence of reference baseline dicom file:
412 TestFile *reference = new TestFile();
413 std::ifstream refFile(referenceFileName.c_str(),
414 std::ios::binary|std::ios::in);
417 std::cout << " Failed" << std::endl
418 << " Image not found:"
419 << referenceFileName << std::endl;
420 reference->SetXSize(tested->GetFile()->GetXSize());
421 reference->SetYSize(tested->GetFile()->GetYSize());
422 reference->SetZSize(tested->GetFile()->GetZSize());
423 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
424 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
425 reference->SetData(tested->GetImageData());
426 reference->Write(referenceFileName);
431 reference->Load(referenceFileName);
432 if(!reference->IsReadable())
434 std::cout << " Failed" << std::endl
435 << " Image not Testing compatible:"
436 << filename << std::endl;
443 std::string PixelType = tested->GetFile()->GetPixelType();
445 int testedDataSize = tested->GetImageDataSize();
446 uint8_t *testedImageData = tested->GetImageData();
448 int referenceDataSize = reference->GetDataSize();
449 uint8_t *referenceImageData = reference->GetData();
451 // Test the image size
452 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
453 tested->GetFile()->GetYSize() != reference->GetYSize() ||
454 tested->GetFile()->GetZSize() != reference->GetZSize())
456 std::cout << "Failed" << std::endl
458 << "X: " << tested->GetFile()->GetXSize() << " # "
459 << reference->GetXSize() << " | "
460 << "Y: " << tested->GetFile()->GetYSize() << " # "
461 << reference->GetYSize() << " | "
462 << "Z: " << tested->GetFile()->GetZSize() << " # "
463 << reference->GetZSize() << std::endl;
469 // Test the pixel size
470 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
471 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
473 std::cout << "Failed" << std::endl
474 << " Pixel size differs: " << std::endl
475 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
476 << reference->GetScalarSize() << std::endl
477 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
478 << reference->GetNumberOfComponents() << std::endl;
484 // Test the data size
485 if (testedDataSize != referenceDataSize)
487 std::cout << " Failed" << std::endl
490 <<") areas lengths differ: "
491 << testedDataSize << " # " << referenceDataSize
494 << tested->GetFile()->GetXSize() << ","
495 << tested->GetFile()->GetYSize() << ","
496 << tested->GetFile()->GetZSize() << ")"
503 // Test the data content
504 if (int res = memcmp(testedImageData, referenceImageData,
505 testedDataSize) != 0 )
508 std::string ts = tested->GetFile()->GetTransferSyntax();
510 std::cout << " Failed" << std::endl
513 << ") differ (as expanded in memory)."
516 << gdcm::Global::GetTS()->GetValue(ts) << std::endl;
518 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
519 << " pixels differing (pos : test - ref) :"
523 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
525 if(testedImageData[i]!=referenceImageData[i])
527 std::cout << std::hex << "(" << i << " : "
528 << std::hex << (int)(testedImageData[i]) << " - "
529 << std::hex << (int)(referenceImageData[i]) << ") "
534 std::cout << std::endl;
541 //////////////// Clean up:
545 std::cout << "OK." << std::endl;
550 int TestAllReadCompareDicom(int argc, char *argv[])
554 // The test is specified a specific filename, use it instead of looping
556 const std::string input = argv[1];
557 const std::string reference = argv[2];
558 return InternalTest( input, reference );
560 else if ( argc > 3 || argc == 2 )
562 std::cerr << " Usage: " << argv[0]
563 << " (no arguments needed)." << std::endl;
564 std::cerr << "or Usage: " << argv[0]
565 << " filename.dcm reference.dcm" << std::endl;
570 std::cout << " Description (Test::TestAllReadCompareDicom): "
572 std::cout << " For all images in gdcmData (and not blacklisted in "
573 "Test/CMakeLists.txt)"
575 std::cout << " apply the following to each filename.xxx: "
577 std::cout << " step 1: parse the image (as gdcmFile) and call"
580 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
582 << " special internal file format containing the"
584 << " caracteristic of the image and the pixel datas "
585 << "(uncompressed). This file is written if it's not found."
587 std::cout << " step 3: compare the DICOM image with the reference image"
589 << " (.tst file). The test is made on the caracteristics"
591 << " of the image and the pixel datas"
592 << std::endl << std::endl;
596 while( gdcmDataImages[i] != 0 )
598 ////// Check for existence of reference baseline directory
600 std::string baseLineDir = GDCM_DATA_ROOT;
601 baseLineDir += "/BaselineDicom";
603 if( !gdcm::DirList::IsDirectory(baseLineDir) )
605 std::cerr << " The reference baseline directory " << std::endl
607 << baseLineDir << std::endl
608 << " couldn't be opened."
613 ////// Step 1 (see above description):
614 std::string filename = GDCM_DATA_ROOT;
616 filename += gdcmDataImages[i];
619 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
620 std::string::size_type slash_pos = referenceFileName.rfind( "." );
621 if( slash_pos != std::string::npos )
623 referenceFileName.replace( slash_pos + 1, 3, "tst" );
626 if( InternalTest( filename, referenceFileName ) != 0 )