Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoServer.cpp
1 #ifdef WIN32
2 # pragma warning (disable: 4251) // STL MAP DLL warning noise (JW)
3 #endif
4 
5 #include "VideoServer.hh"
6 
7 #include <VideoSource/VideoSource_Disk.hh>
8 
9 #ifdef BIAS_HAVE_DCAM
10 # include <VideoSource/VideoSource_DCAM.hh>
11 #endif // BIAS_HAVE_DCAM
12 
13 #ifdef BIAS_HAVE_V4L
14 # include <VideoSource/VideoSource_V4L.hh>
15 #endif // BIAS_HAVE_V4L
16 
17 using namespace BIAS;
18 using namespace std;
19 
21 {
22  port_ = D_CS_DEFAULT_PORT;
23  targetAddress_ = "";
24  camType_= 1;
25  camParam_ = BIAS::Vector<double>(3);
26  camParam_[0] = 0;// fps
27  camParam_[1] = 0; //shutter
28  camParam_[2] = 0; //gain
29 
30  imgWidth_ = 640;
31  imgHeight_ = 480;
32  jpeg_ = -1; // no compression
33  compressionColor_=true;
34  useUDP_ = false;
35 
36  resizeFactor_= 1.0;
37  UDPServerThrottle_= 1;
38  useUDP_ = false;
39 
40  msgSize_=0;
41  Initialized_ = false;
42  camera_ = NULL;
43 }
44 
46 {
47  if (camera_ != NULL) {
48  if (camera_->IsActive()) {
49  camera_->PostGrab();
50  camera_->CloseDevice();
51  }
52  }
53 }
54 
55 
56 int VideoServer::Init(int port) {
57  int res;
58  if (port!=port_)
59  port_=port;
60  cout <<"Initializing VideoServer on port:"<<port_<<endl;
61  switch (camType_) {
62  case 0:
63  camera_= new VideoSource_Disk;
64  break;
65 #ifdef BIAS_HAVE_DCAM
66  case 1:
67  camera_= new VideoSource_DCAM;
68  break;
69 #endif // BIAS_HAVE_DCAM
70 #ifdef BIAS_HAVE_V4L
71  case 2:
72  camera_= new VideoSource_V4L;
73  break;
74 #endif // BIAS_HAVE_V4L
75  default:
76  BIASERR("no camera type selected for input");
77  }
78 
79  //camera_->SetSize(imgWidth_, imgHeight_);
80  //Camera_->SetLeftTop((*ImageParam_)[3], (*ImageParam_)[4]);
81  //camera_->SetFPS(float(camParam_[0]));
82  res = camera_->OpenDevice();
83  if (res<0)
84  return res;
85  res = camera_->PreGrab();
86  if (res<0)
87  return res;
88 
89 #ifdef BIAS_HAVE_DCAM
90  if (camParam_[1] > 0) {
91  //camera_->AutoMode(DC1394_FEATURE_SHUTTER,DC1394_FEATURE_MODE_MANUAL);
92  //camera_->SetShutter(float(camParam_[1]));
93  cout <<"Shutter: "<<camera_->GetShutter();
94  }
95  else
96  //camera_->AutoMode(DC1394_FEATURE_SHUTTER,DC1394_FEATURE_MODE_AUTO);
97  if (camParam_[2] > 0) {
98  //camera_->AutoMode(DC1394_FEATURE_GAIN,DC1394_FEATURE_MODE_MANUAL);
99  //camera_->SetShutter(float(camParam_[2]));
100  cout <<"Shutter: "<<camera_->GetGain()<<endl;
101  }
102  else
103  //camera_->AutoMode(DC1394_FEATURE_GAIN,DC1394_FEATURE_MODE_AUTO);
104 #endif // BIAS_HAVE_DCAM
105 
106  imgWidth_=camera_->GetWidth();
107  imgHeight_=camera_->GetHeight();
108 
109 #ifdef BIAS_HAVE_LIBJPEG
110  if (jpeg_ > 0) {
111  if (Compressor_.Init() < 0) {
112  BIASERR("Could not initialize JPEG-compression!!!");
113  return -1;
114  }
115  }
116 #else //BIAS_HAVE_LIBJPEG
117  if (jpeg_ > 0) {
118  BIASERR("Compression mode not available without LIBJPEG!");
119  }
120 #endif //BIAS_HAVE_LIBJPEG
121  if (useUDP_){
122  // Transmitter_.Connect(TargetAddress_->c_str(),*SendPort_);
123  } else {
124  server_.WaitForConnections(port_);
125  }
126  camera_->InitImage(CamImage_);
127  cout<<"Grabbing image size: "<< imgWidth_<<"x"<< imgHeight_<<endl;
128  Initialized_ = true;
129  return 0;
130 }
131 
133  int res;
134  if (port!=port_)
135  port_=port;
136  cout <<"Initializing VideoServer on port:"<<port_<<endl;
137  camera_ = theSource;
138  if (!camera_->IsActive()) {
139  res = camera_->OpenDevice();
140  if (res!=0) return res;
141  res = camera_->PreGrab();
142  if (res!=0) return res;
143  }
144 
145  imgWidth_=camera_->GetWidth();
146  imgHeight_=camera_->GetHeight();
147 
148 #ifdef BIAS_HAVE_LIBJPEG
149  if (jpeg_ > 0) {
150  if (Compressor_.Init() < 0) {
151  BIASERR("Could not initialize JPEG-compression!!!");
152  return -1;
153  }
154  }
155 #else //BIAS_HAVE_LIBJPEG
156  if (jpeg_ > 0) {
157  BIASERR("Compression mode not available without LIBJPEG!");
158  }
159 #endif //BIAS_HAVE_LIBJPEG
160  if (useUDP_){
161  // Transmitter_.Connect(TargetAddress_->c_str(),*SendPort_);
162  } else {
163  server_.WaitForConnections(port_);
164  }
165  camera_->InitImage(CamImage_);
166  cout<<"Grabbing image size: "<< imgWidth_<<"x"<< imgHeight_<<endl;
167  Initialized_ = true;
168  return 0;
169 }
170 
171 
173 
174  int ret;
175  if (! Initialized_) {
176  BIASERR("VideoServer not initialized");
177  return -1;
178  }
179  if (server_.GetConnections()<=0)
180  {
181 // server_.WaitForConnections(port_);
182  return 1;
183  }
184  //cout << "server conns: " << server_.GetConnections() << endl;
185  camera_->GrabSingle(CamImage_);
186  //cout << "grabbed image" << endl;
187  if (resizeFactor_ == 1.0)
188  SendImage_=CamImage_;
189  else {
190  RescaleFilter_.SetFactor(resizeFactor_);
191  RescaleFilter_.Filter(CamImage_, SendImage_);
192  }
193  SendImage_.SetUID(BIAS::UUID::GenerateUUID());
194 
195  //if (useUDP_) Transmitter_.SetThrottle(*UDPServerThrottle_);
196  if (jpeg_ > 0) {
197 #ifdef BIAS_HAVE_LIBJPEG
198  // compress here --->> add parameter for jpeg-quality ???
199  if (compressionColor_) {
200  if (SendImage_.GetColorModel() != ImageBase::CM_RGB) {
202  ImageConvert::ToRGB(SendImage_, temp);
203  Compressor_.Compress(temp,jpeg_);
204  } else {
205  Compressor_.Compress(SendImage_,jpeg_);
206  }
207  } else {
208  if (SendImage_.GetColorModel() != ImageBase::CM_Grey) {
210  ImageConvert::ToGrey(SendImage_, temp);
211  Compressor_.Compress(temp,jpeg_);
212  } else {
213  Compressor_.Compress(SendImage_,jpeg_);
214  }
215  }
216  void *data = NULL;
217  long length;
218  Compressor_.GetCompressedData(data,length);
219  if (useUDP_){
220  // Transmitter_.Send(&Header_, (unsigned char*) data);
221  } else {
222  ret = server_.SendMsg("JPEG_IMAGE", (char*)data, length);
223  if (ret == -1) {
224  BIASERR("Sending of image data failed");
225  return ret;
226  }
227  }
228  msgSize_=length;
229 #else //BIAS_HAVE_LIBJPEG
230  BIASERR("LIBJPEG not configured --> no compression available !!!");
231  return -1;
232 #endif //BIAS_HAVE_LIBJPEG
233  } else {
234  if (useUDP_){
235  // Transmitter_.Send(&Header_, SendImage_.GetImageData());
236  } else {
237  stringstream dataToSend;
238  dataToSend<<SendImage_;
239  msgSize_= dataToSend.str().length();
240  //cout << "sending with size: " << msgSize_ << endl;
241 
242  ret = server_.SendMsg("BIAS_IMAGE",(char*)&(dataToSend.str()[0]), msgSize_);
243  if (ret == -1) {
244  BIASERR("Sending of image data failed");
245  return ret;
246  }
247  }
248  }
249 
250  return 0;
251 }
252 
253 
Defines a common interface to different devices.
gray values, 1 channel
Definition: ImageBase.hh:130
int InitFromExistingSource(VideoSource *theSource, int port=D_CS_DEFAULT_PORT)
This class VideoSource_DCAM implements access to IEEE1394 (Firewire, iLink) cameras following the DCa...
virtual int OpenDevice()
selects the first available device to open (e.g.
int Init(int port=D_CS_DEFAULT_PORT)
Definition: VideoServer.cpp:56
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
This class extends VideoSource for the use of Video4Linux supported devices like framegrabber, USB cameras.
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235
This class simulates a video device by loading images from disk.
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey