File: itkVideoFileReader.hxx

package info (click to toggle)
insighttoolkit5 5.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,404 kB
  • sloc: cpp: 783,697; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; 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 (329 lines) | stat: -rw-r--r-- 11,917 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*=========================================================================
 *
 *  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.
 *
 *=========================================================================*/

#ifndef itkVideoFileReader_hxx
#define itkVideoFileReader_hxx

#include "itkConvertPixelBuffer.h"
#include "itkMakeUniqueForOverwrite.h"


namespace itk
{

template <typename TOutputVideoStream>
VideoFileReader<TOutputVideoStream>::VideoFileReader()
{
  // Initialize members
  m_FileName = "";
  m_VideoIO = nullptr;
  m_PixelConversionNeeded = false;
  m_IFrameSafe = true;

  // TemporalProcessObject inherited members
  this->SetUnitOutputNumberOfFrames(1);
  this->SetFrameSkipPerOutput(1);
  this->SetInputStencilCurrentFrameIndex(0);
}

template <typename TOutputVideoStream>
void
VideoFileReader<TOutputVideoStream>::UpdateOutputInformation()
{
  //
  // Use the VideoIOFactory to generate a VideoIOBase if needed
  //
  if (m_VideoIO.IsNull())
  {
    this->InitializeVideoIO();
  }

  //
  // Check that the desired dimension matches that read from the file
  //
  if (m_VideoIO->GetNumberOfDimensions() != FrameDimension)
  {
    itkExceptionMacro("Output dimension doesn't match dimension of read "
                      << "data. Expected " << FrameDimension << " but the IO "
                      << "method has " << m_VideoIO->GetNumberOfDimensions() << '.');
  }

  //
  // Set up the largest possible temporal region for the output
  //
  TemporalRegion largestPossibleTemporalRegion;
  largestPossibleTemporalRegion.SetFrameStart(0);
  if (m_IFrameSafe)
  {
    largestPossibleTemporalRegion.SetFrameDuration(m_VideoIO->GetLastIFrame() + 1);
  }
  else
  {
    largestPossibleTemporalRegion.SetFrameDuration(m_VideoIO->GetFrameTotal());
  }
  this->GetOutput()->SetLargestPossibleTemporalRegion(largestPossibleTemporalRegion);

  //
  // Set up the information for the output frames
  //

  // Set up largest possible spatial region
  SizeType      size;
  PointType     origin;
  SpacingType   spacing;
  DirectionType direction;
  for (unsigned int i = 0; i < FrameDimension; ++i)
  {
    size[i] = m_VideoIO->GetDimensions(i);
    origin[i] = m_VideoIO->GetOrigin(i);
    spacing[i] = m_VideoIO->GetSpacing(i);
    std::vector<double> directionInI = m_VideoIO->GetDirection(i);
    for (unsigned int j = 0; j < FrameDimension; ++j)
    {
      direction[j][i] = directionInI[j];
    }
  }
  const RegionType region(size);

  VideoStreamPointer output = this->GetOutput();

  output->SetAllLargestPossibleSpatialRegions(region);
  output->SetAllBufferedSpatialRegions(region);
  output->SetAllRequestedSpatialRegions(region);

  output->SetAllFramesSpacing(spacing);
  output->SetAllFramesOrigin(origin);
  output->SetAllFramesDirection(direction);
}

template <typename TOutputVideoStream>
auto
VideoFileReader<TOutputVideoStream>::GetCurrentPositionFrame() -> FrameOffsetType
{
  if (m_VideoIO.IsNull())
  {
    this->InitializeVideoIO();
  }
  return m_VideoIO->GetCurrentFrame();
}

template <typename TOutputVideoStream>
auto
VideoFileReader<TOutputVideoStream>::GetCurrentPositionRatio() -> TemporalRatioType
{
  if (m_VideoIO.IsNull())
  {
    this->InitializeVideoIO();
  }
  return m_VideoIO->GetRatio();
}

template <typename TOutputVideoStream>
auto
VideoFileReader<TOutputVideoStream>::GetCurrentPositionMSec() -> TemporalRatioType
{
  if (m_VideoIO.IsNull())
  {
    this->InitializeVideoIO();
  }
  return m_VideoIO->GetPositionInMSec();
}

template <typename TOutputVideoStream>
auto
VideoFileReader<TOutputVideoStream>::GetNumberOfFrames() -> FrameOffsetType
{
  if (m_VideoIO.IsNull())
  {
    this->InitializeVideoIO();
  }
  return m_VideoIO->GetFrameTotal();
}

template <typename TOutputVideoStream>
auto
VideoFileReader<TOutputVideoStream>::GetFramesPerSecond() -> TemporalRatioType
{
  if (m_VideoIO.IsNull())
  {
    this->InitializeVideoIO();
  }
  return m_VideoIO->GetFramesPerSecond();
}

template <typename TOutputVideoStream>
void
VideoFileReader<TOutputVideoStream>::InitializeVideoIO()
{
  m_VideoIO = itk::VideoIOFactory::CreateVideoIO(itk::VideoIOFactory::IOModeEnum::ReadFileMode, m_FileName.c_str());
  m_VideoIO->SetFileName(m_FileName.c_str());
  m_VideoIO->ReadImageInformation();

  // Make sure the input video has the same number of dimensions as the desired
  // output
  //
  // Note: This may be changed with the implementation of the Image
  //       Interpretation Layer
  if (m_VideoIO->GetNumberOfDimensions() != FrameType::ImageDimension)
  {
    itkExceptionMacro("Cannot convert " << m_VideoIO->GetNumberOfDimensions()
                                        << "D "
                                           "image set to "
                                        << FrameType::ImageDimension << 'D');
  }

  // See if a buffer conversion is needed
  IOComponentEnum ioType = ImageIOBase::MapPixelType<typename ConvertPixelTraits::ComponentType>::CType;
  if (m_VideoIO->GetComponentType() != ioType ||
      m_VideoIO->GetNumberOfComponents() != ConvertPixelTraits::GetNumberOfComponents())
  {
    // the pixel types don't match so a type conversion needs to be
    // performed
    itkDebugMacro("Buffer conversion required from: "
                  << m_VideoIO->GetComponentTypeAsString(m_VideoIO->GetComponentType())
                  << " to: " << m_VideoIO->GetComponentTypeAsString(ioType) << " ConvertPixelTraits::NumComponents "
                  << ConvertPixelTraits::GetNumberOfComponents() << " m_VideoIO->NumComponents "
                  << m_VideoIO->GetNumberOfComponents());
    m_PixelConversionNeeded = true;
  }
  else
  {
    m_PixelConversionNeeded = false;
  }
}

template <typename TOutputVideoStream>
void
VideoFileReader<TOutputVideoStream>::TemporalStreamingGenerateData()
{
  // Allocate the output frames
  this->AllocateOutputs();

  // Get the frame number for the frame we're reading
  typename VideoStreamType::Pointer            output;
  typename VideoStreamType::TemporalRegionType requestedTemporalRegion;
  output = this->GetOutput();
  requestedTemporalRegion = output->GetRequestedTemporalRegion();
  FrameOffsetType frameNum = requestedTemporalRegion.GetFrameStart();

  // Figure out if we need to skip frames
  FrameOffsetType currentIOFrame = m_VideoIO->GetCurrentFrame();
  if (frameNum != currentIOFrame)
  {
    m_VideoIO->SetNextFrameToRead(frameNum);
  }

  // Read a single frame
  if (this->m_PixelConversionNeeded)
  {
    // Set up temporary buffer for reading
    size_t     bufferSize = m_VideoIO->GetImageSizeInBytes();
    const auto loadBuffer = make_unique_for_overwrite<char[]>(bufferSize);

    // Read into a temporary buffer
    this->m_VideoIO->Read(static_cast<void *>(loadBuffer.get()));

    // Convert the buffer into the output buffer location
    this->DoConvertBuffer(static_cast<void *>(loadBuffer.get()), frameNum);
  }
  else
  {
    FrameType * frame = this->GetOutput()->GetFrame(frameNum);
    m_VideoIO->Read(reinterpret_cast<void *>(frame->GetBufferPointer()));
  }

  // Mark ourselves modified
  this->Modified();
}

template <typename TOutputVideoStream>
void
VideoFileReader<TOutputVideoStream>::DoConvertBuffer(const void * inputData, FrameOffsetType frameNumber)
{
  PixelType *  outputData = this->GetOutput()->GetFrame(frameNumber)->GetPixelContainer()->GetBufferPointer();
  unsigned int numberOfPixels = this->GetOutput()->GetFrame(frameNumber)->GetPixelContainer()->Size();
  bool         isVectorImage(strcmp(this->GetOutput()->GetFrame(frameNumber)->GetNameOfClass(), "VectorImage") == 0);
#define ITK_CONVERT_BUFFER_IF_BLOCK(_CType, type)                                                              \
  else if (m_VideoIO->GetComponentType() == _CType)                                                            \
  {                                                                                                            \
    if (isVectorImage)                                                                                         \
    {                                                                                                          \
      ConvertPixelBuffer<type, PixelType, ConvertPixelTraits>::ConvertVectorImage(                             \
        static_cast<const type *>(inputData), m_VideoIO->GetNumberOfComponents(), outputData, numberOfPixels); \
    }                                                                                                          \
    else                                                                                                       \
    {                                                                                                          \
      ConvertPixelBuffer<type, PixelType, ConvertPixelTraits>::Convert(                                        \
        static_cast<const type *>(inputData), m_VideoIO->GetNumberOfComponents(), outputData, numberOfPixels); \
    }                                                                                                          \
  }

  if (false)
  {
  }
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::UCHAR, unsigned char)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::CHAR, char)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::USHORT, unsigned short)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::SHORT, short)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::UINT, unsigned int)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::INT, int)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::ULONG, unsigned long)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::LONG, long)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::ULONGLONG, unsigned long long)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::LONGLONG, long long)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::FLOAT, float)
  ITK_CONVERT_BUFFER_IF_BLOCK(IOComponentEnum::DOUBLE, double)
  else
  {
#define TYPENAME_VideoFileReader(x) m_VideoIO->GetComponentTypeAsString(ImageIOBase::MapPixelType<x>::CType)

    ExceptionObject    e(__FILE__, __LINE__);
    std::ostringstream msg;
    msg << "Couldn't convert component type: " << std::endl
        << "    " << m_VideoIO->GetComponentTypeAsString(m_VideoIO->GetComponentType()) << std::endl
        << "to one of: " << std::endl
        << "    " << TYPENAME_VideoFileReader(unsigned char) << std::endl
        << "    " << TYPENAME_VideoFileReader(char) << std::endl
        << "    " << TYPENAME_VideoFileReader(unsigned short) << std::endl
        << "    " << TYPENAME_VideoFileReader(short) << std::endl
        << "    " << TYPENAME_VideoFileReader(unsigned int) << std::endl
        << "    " << TYPENAME_VideoFileReader(int) << std::endl
        << "    " << TYPENAME_VideoFileReader(FrameOffsetType) << std::endl
        << "    " << TYPENAME_VideoFileReader(long) << std::endl
        << "    " << TYPENAME_VideoFileReader(float) << std::endl
        << "    " << TYPENAME_VideoFileReader(double) << std::endl;
    e.SetDescription(msg.str().c_str());
    e.SetLocation(ITK_LOCATION);
    throw e;
  }
#undef ITK_CONVERT_BUFFER_IF_BLOCK
}

template <typename TOutputVideoStream>
void
VideoFileReader<TOutputVideoStream>::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "FileName: " << this->m_FileName << std::endl;
  itkPrintSelfObjectMacro(VideoIO);
}

} // end namespace itk

#endif