|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +#include "itkImageFileReader.h" |
| 20 | +#include "itkImageFileWriter.h" |
| 21 | +#include "itkTestingMacros.h" |
| 22 | + |
| 23 | +#include "itkRGBPixel.h" |
| 24 | +#include "itkVectorImage.h" |
| 25 | + |
| 26 | +namespace |
| 27 | +{ |
| 28 | +template <typename TImageType> |
| 29 | +int |
| 30 | +ReadWrite(const std::string & inputImage, const std::string & outputImage) |
| 31 | +{ |
| 32 | + auto image = itk::ReadImage<TImageType>(inputImage); |
| 33 | + ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteImage(image, outputImage)); |
| 34 | + return EXIT_SUCCESS; |
| 35 | +} |
| 36 | + |
| 37 | +template <unsigned int Dimension> |
| 38 | +int |
| 39 | +internalMain(const std::string & inputImage, const std::string & outputImage, itk::ImageIOBase::Pointer imageIO) |
| 40 | +{ |
| 41 | + const unsigned int numberOfComponents = imageIO->GetNumberOfComponents(); |
| 42 | + using IOPixelType = itk::IOPixelEnum; |
| 43 | + const IOPixelType pixelType = imageIO->GetPixelType(); |
| 44 | + |
| 45 | + switch (pixelType) |
| 46 | + { |
| 47 | + case IOPixelType::SCALAR: |
| 48 | + ITK_TEST_EXPECT_EQUAL(numberOfComponents, 1); |
| 49 | + return ReadWrite<itk::Image<float, Dimension>>(inputImage, outputImage); |
| 50 | + |
| 51 | + case IOPixelType::RGB: |
| 52 | + ITK_TEST_EXPECT_EQUAL(numberOfComponents, 3); |
| 53 | + return ReadWrite<itk::Image<itk::RGBPixel<unsigned char>, Dimension>>(inputImage, outputImage); |
| 54 | + |
| 55 | + case IOPixelType::VECTOR: |
| 56 | + return ReadWrite<itk::VectorImage<float, Dimension>>(inputImage, outputImage); |
| 57 | + |
| 58 | + default: |
| 59 | + std::cerr << "Test does not support pixel type of " << itk::ImageIOBase::GetPixelTypeAsString(pixelType) |
| 60 | + << std::endl; |
| 61 | + return EXIT_FAILURE; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace |
| 66 | + |
| 67 | +int |
| 68 | +itkVTIImageIOReadWriteTest(int argc, char * argv[]) |
| 69 | +{ |
| 70 | + if (argc < 3) |
| 71 | + { |
| 72 | + std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv); |
| 73 | + std::cerr << " InputImage OutputImage" << std::endl; |
| 74 | + return EXIT_FAILURE; |
| 75 | + } |
| 76 | + const char * inputImage = argv[1]; |
| 77 | + const char * outputImage = argv[2]; |
| 78 | + |
| 79 | + using ReaderType = itk::ImageFileReader<itk::Image<float, 3>>; |
| 80 | + auto reader = ReaderType::New(); |
| 81 | + reader->SetFileName(inputImage); |
| 82 | + ITK_TRY_EXPECT_NO_EXCEPTION(reader->UpdateOutputInformation()); |
| 83 | + |
| 84 | + auto imageIO = reader->GetImageIO(); |
| 85 | + imageIO->SetFileName(inputImage); |
| 86 | + |
| 87 | + ITK_TRY_EXPECT_NO_EXCEPTION(imageIO->ReadImageInformation()); |
| 88 | + |
| 89 | + std::cout << imageIO << std::endl; |
| 90 | + |
| 91 | + const unsigned int dimension = imageIO->GetNumberOfDimensions(); |
| 92 | + |
| 93 | + switch (dimension) |
| 94 | + { |
| 95 | + case 2: |
| 96 | + return internalMain<2>(inputImage, outputImage, imageIO); |
| 97 | + case 3: |
| 98 | + return internalMain<3>(inputImage, outputImage, imageIO); |
| 99 | + default: |
| 100 | + std::cerr << "Test only supports dimensions 2 and 3. Detected dimension " << dimension << std::endl; |
| 101 | + return EXIT_FAILURE; |
| 102 | + } |
| 103 | +} |
0 commit comments