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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkImageFileReaderDimensionsTest.cxx,v $
Language: C++
Date: $Date: 2010-04-12 13:10:45 $xgoto-l
Version: $Revision: 1.3 $
Copyright (c) 2002 Insight Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
// This test is designed to test reading and writing of miss matched
// dimensions
int itkImageFileReaderDimensionsTest(int argc, char* argv[])
{
if (argc < 4)
{
std::cout << "usage: itkIOTests itkImageFileReaderTest inputFileName outputDirectory outputExtension" << std::endl;
return EXIT_FAILURE;
}
typedef itk::Image<short, 2> Image2DType;
typedef itk::Image<short, 3> Image3DType;
typedef itk::Image<short, 4> Image4DType;
typedef itk::Image<char, 2> CharImage2DType;
typedef itk::ImageFileReader<Image2DType> Reader2DType;
typedef itk::ImageFileReader<Image3DType> Reader3DType;
typedef itk::ImageFileReader<Image4DType> Reader4DType;
typedef itk::ImageFileReader<CharImage2DType> CharReader2DType;
typedef itk::ImageFileWriter<Image3DType> Writer3DType;
typedef itk::ImageFileWriter<Image4DType> Writer4DType;
std::string tempFile1 = std::string( argv[2] ) + std::string( "/itkImageFileReaderDimensionsTest_1." ) + std::string( argv[3] );
std::string tempFile2 = std::string( argv[2] ) + std::string( "/itkImageFileReaderDimensionsTest_2." ) + std::string( argv[3] );
std::string tempFile3 = std::string( argv[2] ) + std::string( "/itkImageFileReaderDimensionsTest_3." ) + std::string( argv[3] );
std::string tempFile4 = std::string( argv[2] ) + std::string( "/itkImageFileReaderDimensionsTest_4." ) + std::string( argv[3] );
std::string tempFile5 = std::string( argv[2] ) + std::string( "/itkImageFileReaderDimensionsTest_5." ) + std::string( argv[3] );
// we expect the filename to be 2 or 3 dimensions
// and reading it into a 4D
std::cout << "testing reading to 4D image" << std::endl;
try
{
Reader4DType::Pointer reader = Reader4DType::New();
reader->SetFileName(argv[1]);
Writer4DType::Pointer writer = Writer4DType::New();
writer->SetInput(reader->GetOutput());
writer->SetFileName(tempFile1);
writer->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex;
return EXIT_FAILURE;
}
// read the new 4D file into a 3D file
std::cout << "testing reading from 4D image into 3D" << std::endl;
try
{
Reader3DType::Pointer reader = Reader3DType::New();
// we expect the filename to be 4 dimensions
reader->SetFileName(tempFile1);
Writer3DType::Pointer writer = Writer3DType::New();
writer->SetInput(reader->GetOutput());
writer->SetFileName(tempFile2);
writer->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex;
return EXIT_FAILURE;
}
// stream read new 3D file into 4D
std::cout << "testing requested stream reading from 3D image into 4D" << std::endl;
try
{
Reader4DType::Pointer reader = Reader4DType::New();
reader->SetFileName(tempFile2);
Writer4DType::Pointer writer = Writer4DType::New();
writer->SetInput(reader->GetOutput());
writer->SetFileName(tempFile3);
writer->SetNumberOfStreamDivisions(4);
writer->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex;
return EXIT_FAILURE;
}
// stream read new 4D file into 3D
std::cout << "testing requested stream reading from 4D image into 3D" << std::endl;
try
{
Reader3DType::Pointer reader = Reader3DType::New();
reader->SetFileName(tempFile3);
reader->Update();
Writer3DType::Pointer writer = Writer3DType::New();
writer->SetInput(reader->GetOutput());
writer->SetFileName(tempFile4);
writer->SetNumberOfStreamDivisions(4);
writer->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex;
return EXIT_FAILURE;
}
// read the new 4D file into a 2D file
// we expect the first 2D slice to be read
std::cout << "testing reading from 4D image into 2D" << std::endl;
try
{
Reader2DType::Pointer reader = Reader2DType::New();
// we expect the filename to be 4 dimensions
reader->SetFileName(tempFile1);
reader->UpdateLargestPossibleRegion();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex;
return EXIT_FAILURE;
}
int status = 1;
// read the 4D file into a 4D image, then try to stream it as a 3D
// IORegion
std::cout << "testing requested invalid paste IORegion" << std::endl;
try
{
Reader4DType::Pointer reader = Reader4DType::New();
reader->SetFileName(tempFile4);
Image4DType::RegionType region = reader->GetOutput()->GetLargestPossibleRegion();
// the dimension of this ioregion is an error, since it is one
// less then the image file dimension
itk::ImageIORegion ioregion(3);
for (unsigned int i = 0; i < 3; ++i)
{
ioregion.SetIndex(i, 0);
ioregion.SetSize(i, region.GetSize(i));
}
Writer4DType::Pointer writer = Writer4DType::New();
writer->SetInput(reader->GetOutput());
writer->SetFileName(tempFile5); // this file name should not
// matter since it should never be written
writer->SetIORegion(ioregion);
writer->Update();
}
catch (itk::ExceptionObject &ex)
{
// this exception is expected since the ioregion should be invalid
std::cout << "------------------ Caught expected exception!" << std::endl;
std::cout << ex;
status = 0;
}
if (status)
{
std::cout << "Failed to catch expected exception." << std::endl;
return EXIT_FAILURE;
}
// regression test of bug #10529
// we expect the filename to be 2 or 3 dimensions
// and reading it into a 4D
std::cout << "testing reading to char 2D image" << std::endl;
try
{
CharReader2DType::Pointer reader = CharReader2DType::New();
reader->SetFileName(argv[1]);
reader->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|