Skip to content

Commit f423f2d

Browse files
jhlegarretaN-Dekker
authored andcommitted
ENH: Add a test to check reading large NIfTI image subregions
Add a test to check reading large `NIfTI` image subregions.
1 parent 3c8ff8b commit f423f2d

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Modules/IO/NIFTI/test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ itkNiftiImageIOTest10.cxx
1313
itkNiftiImageIOTest11.cxx
1414
itkNiftiImageIOTest12.cxx
1515
itkNiftiImageIOTest13.cxx
16+
itkNiftiLargeImageRegionReadTest.cxx
1617
itkNiftiReadAnalyzeTest.cxx
1718
itkNiftiReadWriteDirectionTest.cxx
1819
itkExtractSlice.cxx
@@ -115,6 +116,10 @@ itk_add_test(NAME itkNiftiQFormSFormDifferentSpacingTest
115116
DATA{Input/ChickenEgg-zeros.nii.gz}
116117
)
117118

119+
itk_add_test(NAME itkNiftiLargeImageRegionReadTest
120+
COMMAND ITKIONIFTITestDriver itkNiftiLargeImageRegionReadTest
121+
${ITK_TEST_OUTPUT_DIR}/itkNiftiLargeImageRegionReadTest.nii.gz)
122+
118123
itk_add_test(NAME itkNiftiWriteCoerceOrthogonalDirectionTest
119124
COMMAND ITKIONIFTITestDriver itkNiftiWriteCoerceOrthogonalDirectionTest
120125
${ITK_TEST_OUTPUT_DIR}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
24+
int
25+
itkNiftiLargeImageRegionReadTest(int argc, char * argv[])
26+
{
27+
28+
if (argc != 2)
29+
{
30+
std::cerr << "Missing arguments" << std::endl;
31+
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " outputFileName" << std::endl;
32+
return EXIT_FAILURE;
33+
}
34+
35+
36+
constexpr int Dimension = 3;
37+
using PixelType = unsigned short;
38+
39+
using ImageType = itk::Image<PixelType, Dimension>;
40+
41+
// Create a large image
42+
ImageType::SizeType size = { { 1034, 1034, 1020 } };
43+
ImageType::RegionType region;
44+
region.SetSize(size);
45+
46+
auto image = ImageType::New();
47+
image->SetRegions(region);
48+
image->Allocate(true);
49+
50+
const std::string fname{ argv[1] };
51+
itk::WriteImage(image, fname);
52+
53+
54+
using ReaderType = itk::ImageFileReader<ImageType>;
55+
auto reader = ReaderType::New();
56+
57+
reader->SetFileName(fname);
58+
59+
// Ensure that a sufficiently large subregion, requiring allocating a pixel buffer larger than INT_MAX bytes, can be
60+
// read, as nifti_clib did not support reading such large subregions previously.
61+
region.SetSize({ { 1029, 1029, 1017 } });
62+
reader->GetOutput()->SetRequestedRegion(region);
63+
64+
ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());
65+
66+
const auto output = reader->GetOutput();
67+
ITK_TEST_EXPECT_TRUE(output != nullptr);
68+
ITK_TEST_EXPECT_TRUE(output->GetBufferedRegion() == region);
69+
70+
71+
std::cout << "Test finished." << std::endl;
72+
return EXIT_SUCCESS;
73+
}

0 commit comments

Comments
 (0)