File: itkHDF5ImageIOStreamingReadWriteTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,588 kB
  • sloc: cpp: 784,579; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,934; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (252 lines) | stat: -rw-r--r-- 8,512 bytes parent folder | download | duplicates (2)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#include "itkHDF5ImageIOFactory.h"
#include "itkIOTestHelper.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "itkGenerateImageSource.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageDuplicator.h"
#include "itkMath.h"

namespace itk
{
/**
 * \class DemoImageSource
 *
 * \brief Streamable process that will generate image regions from the write requests
 *
 * We do not allocate directly the Image, because as an Image is a data and is not streamable,
 * the writer would write the image in one pass, without streaming.
 *
 * Instead, we need to set a streamable source process as the writer input.
 * This source process, 'DemoImageSource', will allocate a region of the image
 * (and set pixels values) on the fly, based on the informations
 * received from the writer requests.
 */
template <class TOutputImage>
class DemoImageSource : public GenerateImageSource<TOutputImage>
{
public:
  ITK_DISALLOW_COPY_AND_MOVE(DemoImageSource);

  /** Standard class type aliases. */
  using Self = DemoImageSource;
  using Superclass = DemoImageSource<TOutputImage>;
  using Pointer = SmartPointer<Self>;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** \see LightObject::GetNameOfClass() */
  itkOverrideGetNameOfClassMacro(DemoImageSource);

  /** Set the value to fill the image. */
  itkSetMacro(Value, typename TOutputImage::PixelType);

protected:
  DemoImageSource() { m_Value = typename TOutputImage::PixelType{}; }
  ~DemoImageSource() override = default;

  /** Does the real work. */
  void
  GenerateData() override
  {
    TOutputImage * out = this->GetOutput();
    out->SetBufferedRegion(out->GetRequestedRegion());
    out->Allocate();
    itk::ImageRegionIteratorWithIndex<TOutputImage> it(out, out->GetRequestedRegion());
    for (it.GoToBegin(); !it.IsAtEnd(); ++it)
    {
      typename TOutputImage::IndexType       idx = it.GetIndex();
      const typename TOutputImage::PixelType pixel(idx[2] * 100 + idx[1] * 10 + idx[0]);
      it.Set(pixel);
    }
  };

private:
  typename TOutputImage::PixelType m_Value;
};

} // namespace itk

template <typename TPixel>
int
HDF5ReadWriteTest2(const char * fileName)
{
  // Define image type.
  using ImageType = typename itk::Image<TPixel, 3>;

  // Create a source object (in this case a constant image).
  typename ImageType::SizeType size;
  size[2] = 5;
  size[1] = 5;
  size[0] = 5;
  typename itk::DemoImageSource<ImageType>::Pointer imageSource = itk::DemoImageSource<ImageType>::New();
  imageSource->SetValue(static_cast<TPixel>(23)); // Not used.
  imageSource->SetSize(size);

  // Write image with streaming.
  using WriterType = typename itk::ImageFileWriter<ImageType>;
  auto writer = WriterType::New();
  using MonitorFilterType = typename itk::PipelineMonitorImageFilter<ImageType>;
  auto writerMonitor = MonitorFilterType::New();
  writerMonitor->SetInput(imageSource->GetOutput());
  writer->SetFileName(fileName);
  writer->SetInput(writerMonitor->GetOutput());
  writer->SetNumberOfStreamDivisions(5);
  try
  {
    writer->Write();
  }
  catch (const itk::ExceptionObject & err)
  {
    std::cout << "itkHDF5ImageIOTest" << std::endl << "Exception Object caught: " << std::endl << err << std::endl;
    return EXIT_FAILURE;
  }

  // Check streaming regions.
  if (!writerMonitor->VerifyInputFilterExecutedStreaming(5))
  {
    return EXIT_FAILURE;
  }
  typename ImageType::RegionType expectedRegion;
  expectedRegion.SetIndex(0, 0);
  expectedRegion.SetIndex(1, 0);
  expectedRegion.SetSize(0, 5);
  expectedRegion.SetSize(1, 5);
  expectedRegion.SetSize(2, 1);
  typename MonitorFilterType::RegionVectorType   writerRegionVector = writerMonitor->GetUpdatedBufferedRegions();
  typename ImageType::RegionType::IndexValueType iRegion;
  for (iRegion = 0; iRegion < 5; ++iRegion)
  {
    expectedRegion.SetIndex(2, iRegion);
    if (writerRegionVector[iRegion] != expectedRegion)
    {
      std::cout << "Written image region number " << iRegion << " :" << writerRegionVector[iRegion]
                << " doesn't match expected one: " << expectedRegion << std::endl;
      return EXIT_FAILURE;
    }
  }

  // Force writer close.
  writer = typename WriterType::Pointer();

  // Read image with streaming.
  using ReaderType = typename itk::ImageFileReader<ImageType>;
  auto reader = ReaderType::New();
  reader->SetFileName(fileName);
  reader->SetUseStreaming(true);
  auto readerMonitor = MonitorFilterType::New();
  readerMonitor->SetInput(reader->GetOutput());
  using StreamingFilter = typename itk::StreamingImageFilter<ImageType, ImageType>;
  auto streamer = StreamingFilter::New();
  streamer->SetInput(readerMonitor->GetOutput());
  streamer->SetNumberOfStreamDivisions(5);
  typename ImageType::Pointer image;
  try
  {
    streamer->Update();
  }
  catch (const itk::ExceptionObject & err)
  {
    std::cout << "itkHDF5ImageIOTest" << std::endl << "Exception Object caught: " << std::endl << err << std::endl;
    return EXIT_FAILURE;
  }
  image = streamer->GetOutput();

  // Check largest possible and buffered regions.
  expectedRegion.SetIndex(0, 0);
  expectedRegion.SetIndex(1, 0);
  expectedRegion.SetIndex(2, 0);
  expectedRegion.SetSize(0, 5);
  expectedRegion.SetSize(1, 5);
  expectedRegion.SetSize(2, 5);
  if (image->GetLargestPossibleRegion() != expectedRegion)
  {
    std::cout << "Read image largest possible region: " << image->GetLargestPossibleRegion()
              << "n doesn't match expectedo one: " << expectedRegion << std::endl;
  }
  if (image->GetBufferedRegion() != expectedRegion)
  {
    std::cout << "Read image buffered region: " << image->GetBufferedRegion()
              << "n doesn't match expectedo one: " << expectedRegion << std::endl;
  }

  // Check image pixel values.
  itk::ImageRegionIterator<ImageType> it(image, image->GetLargestPossibleRegion());
  typename ImageType::IndexType       idx;
  for (it.GoToBegin(); !it.IsAtEnd(); ++it)
  {
    idx = it.GetIndex();
    const TPixel origValue(idx[2] * 100 + idx[1] * 10 + idx[0]);
    if (itk::Math::NotAlmostEquals(it.Get(), origValue))
    {
      std::cout << "Original Pixel (" << origValue << ") doesn't match read-in Pixel (" << it.Get() << ')' << std::endl;
      return EXIT_FAILURE;
    }
  }

  // Check number of streaming regions.
  if (!readerMonitor->VerifyInputFilterExecutedStreaming(5))
  {
    return EXIT_FAILURE;
  }

  // Check streaming regions.
  typename MonitorFilterType::RegionVectorType readerRegionVector = readerMonitor->GetUpdatedBufferedRegions();
  expectedRegion.SetIndex(0, 0);
  expectedRegion.SetIndex(1, 0);
  expectedRegion.SetSize(0, 5);
  expectedRegion.SetSize(1, 5);
  expectedRegion.SetSize(2, 1);
  for (iRegion = 0; iRegion < 5; ++iRegion)
  {
    expectedRegion.SetIndex(2, iRegion);
    if (readerRegionVector[iRegion] != expectedRegion)
    {
      std::cout << "Read image region number " << iRegion << " :" << readerRegionVector[iRegion]
                << " doesn't match expected one: " << expectedRegion << std::endl;
      return EXIT_FAILURE;
    }
  }

  // Clean working directory.
  itk::IOTestHelper::Remove(fileName);

  return EXIT_SUCCESS;
}

int
itkHDF5ImageIOStreamingReadWriteTest(int argc, char * argv[])
{
  std::string prefix("");
  if (argc > 1)
  {
    prefix = *++argv;
    --argc;
    itksys::SystemTools::ChangeDirectory(prefix.c_str());
  }
  itk::ObjectFactoryBase::RegisterFactory(itk::HDF5ImageIOFactory::New());

  int result(0);
  result += HDF5ReadWriteTest2<unsigned char>("StreamingUCharImage.hdf5");
  result += HDF5ReadWriteTest2<float>("StreamingFloatImage.hdf5");
  result += HDF5ReadWriteTest2<itk::RGBPixel<unsigned char>>("StreamingRGBImage.hdf5");
  return result != 0;
}