1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/03/30 15:30:33 $
7 Version: $Revision: 1.31 $
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"
26 #include "gdcmDataImages.h"
29 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
30 * /remarks The Test file format is (only in little endian) :
35 * - 2 bytes : scalar size (8,16,32)
36 * - 2 bytes : number of components per pixel (1,2,3)
45 bool IsReadable(void) {return readable;}
46 unsigned int GetXSize(void) {return sizeX;};
47 void SetXSize(unsigned int size) {sizeX = size;};
48 unsigned int GetYSize(void) {return sizeY;};
49 void SetYSize(unsigned int size) {sizeY = size;};
50 unsigned int GetZSize(void) {return sizeZ;};
51 void SetZSize(unsigned int size) {sizeZ = size;};
52 unsigned int GetScalarSize(void) {return scalarSize;};
53 void SetScalarSize(unsigned int size) {scalarSize = size;};
54 unsigned int GetNumberOfComponents(void) {return components;};
55 void SetNumberOfComponents(unsigned int size) {components = size;};
57 unsigned long GetDataSize(void) {return GetLineSize()*sizeY*sizeZ;}
58 uint8_t *GetData(void) {return data;}
59 void SetData(const uint8_t *newData);
61 void Load(const std::string &filename);
62 void Write(const std::string &filename);
65 unsigned long GetLineSize(void) {return sizeX*scalarSize*components;}
66 int GetSwapCode(uint32_t tag);
69 void DeleteData(void);
72 bool ReadFileHeader(std::ifstream *fp);
73 bool ReadFileData(std::ifstream *fp);
75 bool WriteFileHeader(std::ofstream *fp);
76 bool WriteFileData(std::ofstream *fp);
78 uint8_t ReadInt8 (std::ifstream *fp);
79 uint16_t ReadInt16(std::ifstream *fp);
80 uint32_t ReadInt32(std::ifstream *fp);
81 void WriteInt8 (std::ofstream *fp,uint8_t data);
82 void WriteInt16(std::ofstream *fp,uint16_t data);
83 void WriteInt32(std::ofstream *fp,uint32_t data);
91 unsigned int scalarSize;
92 unsigned int components;
96 static const unsigned int HEADER_SIZE;
99 const unsigned int TestFile::HEADER_SIZE = 20;
101 TestFile::TestFile(void)
116 TestFile::~TestFile(void)
121 void TestFile::SetData(const uint8_t *newData)
126 memcpy(data,newData,GetDataSize());
129 void TestFile::Load(const std::string &filename)
135 void TestFile::Write(const std::string &filename)
141 int TestFile::GetSwapCode(uint32_t tag)
146 switch(tag&0x000000FF)
168 void TestFile::NewData(void)
171 if( GetDataSize() == 0 )
173 data = new uint8_t[GetDataSize()];
176 void TestFile::DeleteData(void)
183 void TestFile::ReadFile(void)
186 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
196 readable=ReadFileHeader(&fp);
199 std::cout << "Problems when reading Header part" << std::endl;
204 readable=ReadFileData(&fp);
207 std::cout << "Problems when reading datas" << std::endl;
222 bool TestFile::ReadFileHeader(std::ifstream *fp)
224 uint32_t tag = ReadInt32(fp);
225 swapCode = GetSwapCode(tag);
228 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
232 sizeX = ReadInt32(fp); // Size X
233 sizeY = ReadInt32(fp); // Size Y
234 sizeZ = ReadInt32(fp); // Size Z
235 scalarSize = ReadInt16(fp)/8; // bits per scalar
236 components = ReadInt16(fp); // Number of components
241 bool TestFile::ReadFileData(std::ifstream *fp)
251 fp->read((char *)data,GetDataSize());
256 void TestFile::WriteFile(void)
258 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
266 WriteFileHeader(&fp);
272 bool TestFile::WriteFileHeader(std::ofstream *fp)
274 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
275 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
276 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
277 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
278 WriteInt32(fp,sizeX); // Size X
279 WriteInt32(fp,sizeY); // Size Y
280 WriteInt32(fp,sizeZ); // Size Z
281 WriteInt16(fp,scalarSize*8); // bits per scalar
282 WriteInt16(fp,components); // number of components
287 bool TestFile::WriteFileData(std::ofstream *fp)
289 fp->write((char *)data,GetDataSize());
294 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
295 throw( std::ios::failure )
298 fp->read ((char*)&g, (size_t)1);
300 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
302 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
306 uint16_t TestFile::ReadInt16(std::ifstream *fp)
307 throw( std::ios::failure )
310 fp->read ((char*)&g, (size_t)2);
312 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
314 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
316 #if defined(GDCM_WORDS_BIGENDIAN)
317 g = ( g << 8 | g >> 8 );
322 uint32_t TestFile::ReadInt32(std::ifstream *fp)
323 throw( std::ios::failure )
326 fp->read ((char*)&g, (size_t)4);
328 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
330 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
332 #if defined(GDCM_WORDS_BIGENDIAN)
333 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
334 ( (g>>8) & 0x0000ff00) | (g>>24) );
339 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t data)
341 fp->write((char*)&data, (size_t)1);
344 void TestFile::WriteInt16(std::ofstream *fp,uint16_t data)
346 #if defined(GDCM_WORDS_BIGENDIAN)
347 data = ( data << 8 | data >> 8 );
349 fp->write((char*)&data, (size_t)2);
352 void TestFile::WriteInt32(std::ofstream *fp,uint32_t data)
354 #if defined(GDCM_WORDS_BIGENDIAN)
355 data = ( (data<<24) | ((data<<8) & 0x00ff0000) |
356 ( (data>>8) & 0x0000ff00) | (data>>24) );
358 fp->write((char*)&data, (size_t)4);
361 int InternalTest(std::string const &filename,
362 std::string const &referenceFileName )
364 std::cout << " Testing: " << filename << std::endl;
369 gdcm::FileHelper *tested = new gdcm::FileHelper( filename );
370 if( !tested->GetFile()->IsReadable() )
372 std::cout << " Failed" << std::endl
373 << " Image not gdcm compatible:"
374 << filename << std::endl;
380 ////// Check for existence of reference baseline dicom file:
383 TestFile *reference = new TestFile();
384 reference->Load(referenceFileName);
385 if(!reference->IsReadable())
387 std::cout << " Failed" << std::endl
388 << " Image not BMP compatible:"
389 << referenceFileName << std::endl;
390 reference->SetXSize(tested->GetFile()->GetXSize());
391 reference->SetYSize(tested->GetFile()->GetYSize());
392 reference->SetZSize(tested->GetFile()->GetZSize());
393 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
394 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
395 reference->SetData(tested->GetImageData());
396 reference->Write(referenceFileName);
399 reference->Load(referenceFileName);
400 if(!reference->IsReadable())
402 std::cout << " Failed" << std::endl
403 << " Image not Testing compatible:"
404 << filename << std::endl;
411 std::string PixelType = tested->GetFile()->GetPixelType();
413 int testedDataSize = tested->GetImageDataSize();
414 uint8_t *testedImageData = tested->GetImageData();
416 int referenceDataSize = reference->GetDataSize();
417 uint8_t *referenceImageData = reference->GetData();
419 // Test the image size
420 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
421 tested->GetFile()->GetYSize() != reference->GetYSize() ||
422 tested->GetFile()->GetZSize() != reference->GetZSize())
424 std::cout << "Failed" << std::endl
426 << "X: " << tested->GetFile()->GetXSize() << " # "
427 << reference->GetXSize() << " | "
428 << "Y: " << tested->GetFile()->GetYSize() << " # "
429 << reference->GetYSize() << " | "
430 << "Z: " << tested->GetFile()->GetZSize() << " # "
431 << reference->GetZSize() << std::endl;
437 // Test the pixel size
438 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
439 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
441 std::cout << "Failed" << std::endl
442 << " Pixel size differs: " << std::endl
443 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
444 << reference->GetScalarSize() << std::endl
445 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
446 << reference->GetNumberOfComponents() << std::endl;
452 // Test the data size
453 if (testedDataSize != referenceDataSize)
455 std::cout << " Failed" << std::endl
458 <<") areas lengths differ: "
459 << testedDataSize << " # " << referenceDataSize
462 << tested->GetFile()->GetXSize() << ","
463 << tested->GetFile()->GetYSize() << ","
464 << tested->GetFile()->GetZSize() << ")"
471 // Test the data content
472 if (int res = memcmp(testedImageData, referenceImageData,
473 testedDataSize) != 0 )
476 std::cout << " Failed" << std::endl
479 << ") differ (as expanded in memory)."
486 //////////////// Clean up:
490 std::cout << "OK." << std::endl;
495 int TestAllReadCompareDicom(int argc, char *argv[])
499 // The test is specified a specific filename, use it instead of looping
501 const std::string input = argv[1];
502 const std::string reference = argv[2];
503 return InternalTest( input, reference );
505 else if ( argc > 3 || argc == 2 )
507 std::cerr << " Usage: " << argv[0]
508 << " (no arguments needed)." << std::endl;
509 std::cerr << "or Usage: " << argv[0]
510 << " filename.dcm reference.dcm" << std::endl;
515 std::cout << " Description (Test::TestAllReadCompareDicom): "
517 std::cout << " For all images in gdcmData (and not blacklisted in "
518 "Test/CMakeLists.txt)"
520 std::cout << " apply the following to each filename.xxx: "
522 std::cout << " step 1: parse the image (as gdcmFile) and call"
525 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
527 << " (with format DICOM V3, explicit Value"
530 std::cout << " step 3a: when image NOT found on step 2, write "
532 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
534 << " (with format DICOM V3, explicit Value"
537 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
539 << " compare it (in memory with memcmp) with the"
541 << " image we are testing (the one of step 1). "
542 << std::endl << std::endl;
546 while( gdcmDataImages[i] != 0 )
548 ////// Check for existence of reference baseline directory
550 std::string baseLineDir = GDCM_DATA_ROOT;
551 baseLineDir += "/BaselineDicom/";
553 if( !gdcm::DirList::IsDirectory(baseLineDir) )
555 std::cerr << " The reference baseline directory " << std::endl
557 << baseLineDir << std::endl
558 << " couldn't be opened."
563 ////// Step 1 (see above description):
564 std::string filename = GDCM_DATA_ROOT;
566 filename += gdcmDataImages[i];
568 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
569 std::string::size_type slash_pos = referenceFileName.rfind( "." );
570 if( slash_pos != std::string::npos )
572 referenceFileName.replace( slash_pos + 1, 3, "tst" );
575 if( InternalTest( filename, referenceFileName ) != 0 )