1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmImageReader.h"
#include "gdcmFileMetaInformation.h"
#include "gdcmSystem.h"
#include "gdcmFilename.h"
#include "gdcmByteSwap.h"
#include "gdcmTrace.h"
#include "gdcmTesting.h"
int TestImageRead(const char* filename, bool verbose = false, bool lossydump = false)
{
if( verbose )
std::cerr << "Reading: " << filename << std::endl;
gdcm::ImageReader reader;
reader.SetFileName( filename );
if ( reader.Read() )
{
int res = 0;
const gdcm::Image &img = reader.GetImage();
//std::cerr << "Success to read image from file: " << filename << std::endl;
unsigned long len = img.GetBufferLength();
if ( lossydump )
{
int lossy = img.IsLossy();
std::cout << lossy << "," << filename << std::endl;
}
int reflossy = gdcm::Testing::GetLossyFlagFromFile( filename );
if( reflossy == -1 )
{
std::cerr << "Missing lossy flag for: " << filename << std::endl;
return 1;
}
if( img.IsLossy() != (reflossy > 0 ? true : false) )//vs10 has a stupid bool/int cast warning
{
std::cerr << "Inconsistency for lossy flag for: " << filename << std::endl;
return 1;
}
char* buffer = new char[len];
bool res2 = img.GetBuffer(buffer);
if( !res2 )
{
std::cerr << "res2 failure: " << filename << std::endl;
return 1;
}
// On big Endian system we have byteswapped the buffer (duh!)
// Since the md5sum is byte based there is now way it would detect
// that the file is written in big endian word, so comparing against
// a md5sum computed on LittleEndian would fail. Thus we need to
// byteswap (again!) here:
#ifdef GDCM_WORDS_BIGENDIAN
if( img.GetPixelFormat().GetBitsAllocated() == 16 )
{
assert( !(len % 2) );
// gdcm-US-ALOKA is a 16 bits image with PALETTE
//assert( img.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1
// || img.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2 );
gdcm::ByteSwap<unsigned short>::SwapRangeFromSwapCodeIntoSystem(
(unsigned short*)buffer, gdcm::SwapCode::LittleEndian, len/2);
}
#endif
const char *ref = gdcm::Testing::GetMD5FromFile(filename);
char digest[33];
gdcm::Testing::ComputeMD5(buffer, len, digest);
if( verbose )
{
std::cout << "ref=" << ref << std::endl;
std::cout << "md5=" << digest << std::endl;
}
if( !ref )
{
// new regression image needs a md5 sum
std::cout << "Missing md5 " << digest << " for: " << filename << std::endl;
//assert(0);
res = 1;
}
else if( strcmp(digest, ref) != 0 )
{
std::cerr << "Problem reading image from: " << filename << std::endl;
std::cerr << "Found " << digest << " instead of " << ref << std::endl;
res = 1;
#if 0
std::ofstream debug("/tmp/dump.gray",std::ios::binary);
debug.write(buffer, len);
debug.close();
//assert(0);
#endif
}
delete[] buffer;
return res;
}
const gdcm::FileMetaInformation &header = reader.GetFile().GetHeader();
gdcm::MediaStorage ms = header.GetMediaStorage();
bool isImage = gdcm::MediaStorage::IsImage( ms );
if( isImage )
{
if( reader.GetFile().GetDataSet().FindDataElement( gdcm::Tag(0x7fe0,0x0010) ) )
{
std::cerr << "Failed to read image from file: " << filename << std::endl;
return 1;
}
else
{
std::cerr << "no Pixel Data Element found in the file:" << filename << std::endl;
return 0;
}
}
// else
// well this is not an image, so thankfully we fail to read it
std::cerr << "Could not read image(" << filename << "), since file is a: " << ms << std::endl;
//assert( ms != gdcm::MediaStorage::MS_END );
return 0;
}
int TestImageReader(int argc, char *argv[])
{
if( argc == 2 )
{
const char *filename = argv[1];
return TestImageRead(filename, true);
}
// else
// First of get rid of warning/debug message
gdcm::Trace::DebugOff();
gdcm::Trace::WarningOff();
gdcm::Trace::ErrorOff();
int r = 0, i = 0;
const char *filename;
const char * const *filenames = gdcm::Testing::GetFileNames();
while( (filename = filenames[i]) )
{
r += TestImageRead( filename);
//r += TestImageRead( filename, false, true );
++i;
}
return r;
}
|