00001 /* This file is part of the Pangolin Project. 00002 * http://github.com/stevenlovegrove/Pangolin 00003 * 00004 * Copyright (c) 2011 Steven Lovegrove 00005 * 00006 * Permission is hereby granted, free of charge, to any person 00007 * obtaining a copy of this software and associated documentation 00008 * files (the "Software"), to deal in the Software without 00009 * restriction, including without limitation the rights to use, 00010 * copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 * copies of the Software, and to permit persons to whom the 00012 * Software is furnished to do so, subject to the following 00013 * conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be 00016 * included in all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00019 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00020 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00021 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00022 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00023 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00024 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00025 * OTHER DEALINGS IN THE SOFTWARE. 00026 */ 00027 00028 #include "pvn_video.h" 00029 00030 #include <iostream> 00031 00032 using namespace std; 00033 00034 namespace pangolin 00035 { 00036 00037 PvnVideo::PvnVideo(const char* filename, bool realtime ) 00038 : realtime(realtime), last_frame(TimeNow()) 00039 { 00040 file.open(filename, ios::binary ); 00041 00042 if(!file.is_open() ) 00043 throw VideoException("Cannot open file - does not exist or bad permissions."); 00044 00045 ReadFileHeader(); 00046 } 00047 00048 PvnVideo::~PvnVideo() 00049 { 00050 00051 } 00052 00053 void PvnVideo::ReadFileHeader() 00054 { 00055 string sfmt; 00056 float framerate; 00057 00058 VideoStream strm0; 00059 file >> sfmt; 00060 file >> strm0.w; 00061 file >> strm0.h; 00062 file >> framerate; 00063 file.get(); 00064 00065 if(file.bad() || !(strm0.w >0 && strm0.h >0) ) 00066 throw VideoException("Unable to read video header"); 00067 00068 strm0.fmt = VideoFormatFromString(sfmt); 00069 strm0.frame_size_bytes = (strm0.w * strm0.h * strm0.fmt.bpp) / 8; 00070 frame_interval = TimeFromSeconds( 1.0 / framerate); 00071 00072 stream_info.push_back(strm0); 00073 } 00074 00075 00076 void PvnVideo::Start() 00077 { 00078 } 00079 00080 void PvnVideo::Stop() 00081 { 00082 } 00083 00084 unsigned PvnVideo::Width() const 00085 { 00086 return stream_info[0].w; 00087 } 00088 00089 unsigned PvnVideo::Height() const 00090 { 00091 return stream_info[0].h; 00092 } 00093 00094 size_t PvnVideo::SizeBytes() const 00095 { 00096 return stream_info[0].frame_size_bytes; 00097 } 00098 00099 std::string PvnVideo::PixFormat() const 00100 { 00101 return stream_info[0].fmt.format; 00102 } 00103 00104 bool PvnVideo::GrabNext( unsigned char* image, bool /*wait*/ ) 00105 { 00106 file.read((char*)image,stream_info[0].frame_size_bytes); 00107 00108 const basetime next_frame = TimeAdd(last_frame, frame_interval); 00109 00110 if( realtime ) 00111 { 00112 WaitUntil(next_frame); 00113 } 00114 00115 last_frame = TimeNow(); 00116 return file.good(); 00117 } 00118 00119 bool PvnVideo::GrabNewest( unsigned char* image, bool wait ) 00120 { 00121 return GrabNext(image,wait); 00122 } 00123 00124 }