1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
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  *         http://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 #ifndef itkFileListVideoIO_h
19 #define itkFileListVideoIO_h
20 
21 #include "itkVideoIOBase.h"
22 #include "ITKVideoIOExport.h"
23 
24 namespace itk
25 {
26 /** \class FileListVideoIO
27  *
28  * \brief VideoIO object for reading and writing videos as a sequence of frame
29  *  files.
30  *
31  * This VideoIO treats a sequential list of file names as the frames of a
32  * video. The frames must be specified in a comma-separated list. Also, the
33  * SplitFileNames(...) static method is made public in order to allow the
34  * splitting functionality to be accessed publicly.
35  *
36  * \ingroup ITKVideoIO
37  *
38  */
39 class ITKVideoIO_EXPORT FileListVideoIO : public VideoIOBase
40 {
41 public:
42   ITK_DISALLOW_COPY_AND_ASSIGN(FileListVideoIO);
43 
44   /** Standard class type aliases. */
45   using Self = FileListVideoIO;
46   using Superclass = VideoIOBase;
47   using Pointer = SmartPointer< Self >;
48   using ConstPointer = SmartPointer< const Self >;
49 
50   /** Method for creation through the object factory. */
51   itkNewMacro(Self);
52 
53   /** Run-time type information (and related methods). */
54   itkTypeMacro(FileListVideoIO, VideoIOBase);
55 
56   /** Get the internal ImageIOBase object. */
57   itkGetConstObjectMacro(ImageIO, ImageIOBase);
58 
59   /** Get the list of files to read. */
60   itkGetConstMacro(FileNames, std::vector<std::string>);
61 
62   /** Override SetFileName to do parsing. */
63   void SetFileName(const std::string& fileList) override;
64   void SetFileName(const char* fileList) override;
65 
66   /** Close the reader and writer and reset members. */
67   void FinishReadingOrWriting() override;
68 
69   /** Split up the input file names using comma (',') as the separator character.
70    * This method is made public so that places where FileListVideoIO is used
71    * can access the individual file names. This is mostly an issue for testing. */
72   static std::vector<std::string> SplitFileNames(const std::string& fileList);
73 
74   /** Set to reading from file. */
75   void SetReadFromFile() override;
76 
77   /** Set to reading from a camera. */
78   void SetReadFromCamera() override;
79 
80   /** Determine the file type. Returns true if this ImageIO can read the
81    * file specified. */
82   bool CanReadFile(const char *) override;
83 
84   /** Return whether or not the VideoIO can read from a camera. */
85   bool CanReadCamera( CameraIDType cameraID )const override;
86 
87   /** Set the spacing and dimension information for the set filename. */
88   void ReadImageInformation() override;
89 
90   /** Reads the data from disk into the memory buffer provided. */
91   void Read(void *buffer) override;
92 
93   /** Set the next frame that should be read. Return true if you operation
94    * successful. */
95   bool SetNextFrameToRead(FrameOffsetType frameNumber) override;
96 
97   /** Accessor functions for video specific information. */
GetPositionInMSec()98   TemporalOffsetType GetPositionInMSec() const override
99     {
100     return this->m_PositionInMSec;
101     }
GetRatio()102   TemporalOffsetType GetRatio() const override
103     {
104     return this->m_Ratio;
105     }
GetFrameTotal()106   FrameOffsetType GetFrameTotal() const override
107     {
108     return this->m_FrameTotal;
109     }
GetFramesPerSecond()110   TemporalRatioType GetFramesPerSecond() const override
111     {
112     return this->m_FramesPerSecond;
113     }
GetCurrentFrame()114   FrameOffsetType GetCurrentFrame() const override
115     {
116     return this->m_CurrentFrame;
117     }
118   itkGetConstMacro(IFrameInterval,FrameOffsetType);
GetLastIFrame()119   FrameOffsetType GetLastIFrame() const override
120     {
121     return this->m_LastIFrame;
122     }
123 
124   /** Override accessors to pass through to internal image reader. */
125   double GetSpacing(unsigned int i) const override;
126 
127   double GetOrigin(unsigned int i) const override;
128 
129   std::vector< double > GetDirection(unsigned int i) const override;
130 
131   /** Determine the file type. Returns true if this ImageIO can write the
132    * file specified. */
133   bool CanWriteFile(const char *) override;
134 
135   /** Writes the spacing and dimensions of the image.
136    * Assumes SetFileName has been called with a valid file name. */
137   void WriteImageInformation() override;
138 
139   /** Writes the data to disk from the memory buffer provided. Make sure
140    * that the IORegion has been set properly. */
141   void Write(const void *buffer) override;
142 
143   /** Set Writer parameters. */
144   void SetWriterParameters( TemporalRatioType framesPerSecond,
145                                     const std::vector<SizeValueType>& dim,
146                                     const char* fourCC,
147                                     unsigned int nChannels,
148                                     IOComponentType componentType ) override;
149 
150 protected:
151   FileListVideoIO();
152   ~FileListVideoIO() override;
153 
154   void PrintSelf(std::ostream & os, Indent indent) const override;
155 
156   /** Reset member variables to empty state closed. */
157   void ResetMembers();
158 
159   /** Open the reader if the reader and writer are not open. */
160   void OpenReader();
161 
162   /** Open the writer if the reader and reader are not open. */
163   void OpenWriter();
164 
165   /** Verify that all file names in the have the same extension. */
166   bool VerifyExtensions( const std::vector<std::string>& fileList ) const;
167 
168 private:
169   ImageIOBase::Pointer m_ImageIO;
170 
171   std::vector<std::string> m_FileNames;
172 
173 };
174 } // end namespace itk
175 
176 #endif // itkFileListVideoIO_h
177