core/vidl/vidl_image_list_istream.h
Go to the documentation of this file.
00001 // This is core/vidl/vidl_image_list_istream.h
00002 #ifndef vidl_image_list_istream_h_
00003 #define vidl_image_list_istream_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A video input stream from a list of images on disk
00010 //
00011 // \author Matt Leotta
00012 // \date 19 Dec 2005
00013 
00014 #include "vidl_istream.h"
00015 #include <vcl_vector.h>
00016 #include <vcl_string.h>
00017 
00018 
00019 //: A video input stream from a list of images on disk
00020 // This istream will try to open and validate (but not read image data from)
00021 // every file in a list of file paths.  The paths to valid image files
00022 // are maintained, but only one image is opened at a time.  Keeping a list of
00023 // open file descriptors (via vil_image_resource_sptr) was found to be problematic.
00024 // The number of simultaneously open files is limited on many platforms.
00025 // The paths are tested for validity at the "open" stage rather than the "stream"
00026 // stage so that we have random access to the frames (i.e. the stream is seekable).
00027 class vidl_image_list_istream
00028   : public vidl_istream
00029 {
00030  public:
00031   //: Constructor - default
00032   vidl_image_list_istream();
00033 
00034   //: Constructor - from a file glob string
00035   vidl_image_list_istream(const vcl_string& glob);
00036 
00037   //: Constructor - from a vector of file paths
00038   vidl_image_list_istream(const vcl_vector<vcl_string>& paths);
00039 
00040   //: Destructor
00041   virtual ~vidl_image_list_istream() { close(); }
00042 
00043 
00044   //: Open a new stream using a file glob (see vul_file_iterator)
00045   // \note files are loaded in alphanumeric order by path name
00046   virtual bool open(const vcl_string& glob);
00047 
00048   //: Open a new stream using a vector of file paths
00049   // \note all files are tested and only valid image files are retained
00050   virtual bool open(const vcl_vector<vcl_string>& paths);
00051 
00052   //: Close the stream
00053   virtual void close();
00054 
00055 
00056   //: Return true if the stream is open for reading
00057   virtual bool is_open() const { return !image_paths_.empty(); }
00058 
00059   //: Return true if the stream is in a valid state
00060   virtual bool is_valid() const { return is_open() &&
00061                                          index_ < image_paths_.size(); }
00062 
00063   //: Return true if the stream support seeking
00064   virtual bool is_seekable() const { return true; }
00065 
00066   //: Return the number of frames if known
00067   //  returns -1 for non-seekable streams
00068   virtual int num_frames() const { return int(image_paths_.size()); }
00069 
00070   //: Return the current frame number
00071   virtual unsigned int frame_number() const { return index_; }
00072 
00073   //: Return the width of each frame
00074   virtual unsigned int width() const { return ni_; }
00075 
00076   //: Return the height of each frame
00077   virtual unsigned int height() const { return nj_; }
00078 
00079   //: Return the pixel format
00080   virtual vidl_pixel_format format() const { return format_; }
00081 
00082   //: Return the frame rate (0.0 if unspecified)
00083   virtual double frame_rate() const { return 0.0; }
00084 
00085   //: Return the duration in seconds (0.0 if unknown)
00086   virtual double duration() const { return 0.0; }
00087 
00088   //: Advance to the next frame (but do not open the next image)
00089   virtual bool advance();
00090 
00091   //: Read the next frame from the stream
00092   virtual vidl_frame_sptr read_frame();
00093 
00094   //: Return the current frame in the stream
00095   virtual vidl_frame_sptr current_frame();
00096 
00097   //: Return the path to the current image in the stream
00098   vcl_string current_path() const;
00099 
00100   //: Seek to the given frame number (but do not load the image)
00101   // \returns true if successful
00102   virtual bool seek_frame(unsigned int frame_number);
00103 
00104  private:
00105   //: The vector of images
00106   vcl_vector<vcl_string> image_paths_;
00107 
00108   //: The current index
00109   unsigned int index_;
00110 
00111   //: The image width
00112   unsigned int ni_;
00113   //: The image height
00114   unsigned int nj_;
00115   //: The pixel format
00116   vidl_pixel_format format_;
00117 
00118   //: The current frame (cached)
00119   vidl_frame_sptr current_frame_;
00120 };
00121 
00122 #endif // vidl_image_list_istream_h_