1 /* This file is part of the Pangolin Project.
2  * http://github.com/stevenlovegrove/Pangolin
3  *
4  * Copyright (c) 2013 Steven Lovegrove
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #pragma once
29 
30 #include <pangolin/image/image_io.h>
31 #include <pangolin/pangolin.h>
32 #include <pangolin/video/video.h>
33 
34 #include <deque>
35 #include <vector>
36 
37 namespace pangolin
38 {
39 
40 // Video class that outputs test video signal.
41 class PANGOLIN_EXPORT ImagesVideo : public VideoInterface, public VideoPlaybackInterface, public VideoPropertiesInterface
42 {
43 public:
44     ImagesVideo(const std::string& wildcard_path);
45     ImagesVideo(const std::string& wildcard_path, const PixelFormat& raw_fmt, size_t raw_width, size_t raw_height);
46 
47     // Explicitly delete copy ctor and assignment operator.
48     // See http://stackoverflow.com/questions/29565299/how-to-use-a-vector-of-unique-pointers-in-a-dll-exported-class-with-visual-studi
49     // >> It appears adding __declspec(dllexport) forces the compiler to define the implicitly-declared copy constructor and copy assignment operator
50     ImagesVideo(const ImagesVideo&) = delete;
51     ImagesVideo& operator=(const ImagesVideo&) = delete;
52 
53     ~ImagesVideo();
54 
55     ///////////////////////////////////
56     // Implement VideoInterface
57 
58     void Start() override;
59 
60     void Stop() override;
61 
62     size_t SizeBytes() const override;
63 
64     const std::vector<StreamInfo>& Streams() const override;
65 
66     bool GrabNext( unsigned char* image, bool wait = true ) override;
67 
68     bool GrabNewest( unsigned char* image, bool wait = true ) override;
69 
70     ///////////////////////////////////
71     // Implement VideoPlaybackInterface
72 
73     size_t GetCurrentFrameId() const override;
74 
75     size_t GetTotalFrames() const override;
76 
77     size_t Seek(size_t frameid) override;
78 
79     ///////////////////////////////////
80     // Implement VideoPropertiesInterface
81 
82     const picojson::value& DeviceProperties() const override;
83 
84     const picojson::value& FrameProperties() const override;
85 
86 protected:
87     typedef std::vector<TypedImage> Frame;
88 
Filename(size_t frameNum,size_t channelNum)89     const std::string& Filename(size_t frameNum, size_t channelNum) {
90         return filenames[channelNum][frameNum];
91     }
92 
93     void PopulateFilenames(const std::string& wildcard_path);
94 
95     void PopulateFilenamesFromJson(const std::string& filename);
96 
97     bool LoadFrame(size_t i);
98 
99     void ConfigureStreamSizes();
100 
101     std::vector<StreamInfo> streams;
102     size_t size_bytes;
103 
104     size_t num_files;
105     size_t num_channels;
106     size_t next_frame_id;
107     std::vector<std::vector<std::string> > filenames;
108     std::vector<Frame> loaded;
109 
110     bool unknowns_are_raw;
111     PixelFormat raw_fmt;
112     size_t raw_width;
113     size_t raw_height;
114 
115     // Load any json properties if they are defined
116     picojson::value device_properties;
117     picojson::value json_frames;
118     picojson::value null_props;
119 };
120 
121 }
122