Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Net.cpp
1 #include <Base/Common/W32Compat.hh>
2 
3 #ifdef WIN32
4 # pragma warning(disable: 4996) // vs8
5 #endif //WIN32
6 
7 // for gcc 4.7
8 #ifndef WIN32
9 #include <unistd.h>
10 #endif
11 
12 #include <string>
13 #include <sstream>
14 #include <iostream>
15 
16 #include <Image/Camera.hh>
17 #include "VideoSource_Net.hh"
18 #include <limits.h>
19 
20 using namespace BIAS;
21 using namespace std;
22 
24  VideoSource(), client_(threaded) {
25 
26  ColorChannels_ = 3; // fake
27  ColorMode_ = ImageBase::CM_RGB; // fake
28 #ifdef BIAS_HAVE_LIBJPEG
30 #endif //BIAS_HAVE_LIBJPEG
31  pImgBuffer_ = new std::stringstream;
32  count_=0;
33  UseTCP_=true;
34  if (!threaded) client_.SetKeepAll(true);
35 }
36 
37 
38 int VideoSource_Net::OpenDevice(int /*port*/)
39 {
40  BIASERR("no UDP comm yet");
41  return 0;
42 }
43 
44 int VideoSource_Net::OpenDevice(const char* serverName)
45 {
46  return OpenDevice(serverName, D_CS_DEFAULT_PORT);
47 }
48 
49 int VideoSource_Net::OpenDevice(const std::string &serverName,
50  int port)
51 {
52  client_.SetVerbose(true);
53  if (client_.ConnectServer(serverName,port,UINT_MAX)!=0) {
54  return -1;
55  }
56  client_.RegisterMsg("BIAS_IMAGE",CS_BINARY);
57  client_.RegisterMsg("JPEG_IMAGE",CS_BINARY);
58 
59  BIAS::ImageBase img;
60  GrabSingle(img);
61 
62  Width_=img.GetWidth();
63  Height_=img.GetHeight();
65  BytesPerPixel_ = float( img.GetDepth()*img.GetChannelCount() );
66  return 0;
67 }
68 
69 
71 {
72 
73  return 0;
74 
75 } // PreGrab
76 
78 {
79  ImageBase imgBase;
80  int res=GrabSingle(imgBase);
82  image=BIAS::Camera <unsigned char>(imgBase);
83  else {
84  BIASERR("received image is not unsigned char");
85  return -2;
86  }
87  image.ParseMetaData();
88  return res;
89 }
90 
92 {
93  bool haveJpeg=false;
94  if (!UseTCP_) {
95  BIASERR("UDP not yet");
96  return -1;
97  }
98  static std::vector<char> data;
99  int res=-1;
100  while (res!=0) {
101  res=client_.GetData("BIAS_IMAGE",data);
102  if (res!=0) {
103  res=client_.GetData("JPEG_IMAGE",data);
104  if (res==0)
105  haveJpeg=true;
106  }
107 #ifdef WIN32
108  Sleep(1);
109 #else
110  usleep(10);
111 #endif
112  }
113  if (!haveJpeg) {
114  pImgBuffer_->rdbuf()->sputn(&data[0],data.size());
115 
116  // this does the trick
117  *pImgBuffer_>>image;
118 
119  count_++;
120  if (count_==20) {
121  count_=0;
122  ////// free some memory
123  // save unprocessed data
124  string dummy(pImgBuffer_->rdbuf()->in_avail(),0);
125  pImgBuffer_->rdbuf()->sgetn(&dummy[0],pImgBuffer_->rdbuf()->in_avail());
126  // delete and reallocate
127  delete pImgBuffer_;
128  pImgBuffer_ = new stringstream;
129  pImgBuffer_->rdbuf()->sputn(&dummy[0],dummy.length());
130  }
131  } else {
132 #ifdef BIAS_HAVE_LIBJPEG
133  //received image is jpeg-compressed
134  Decompressor_.SetCompressedData((void *)&(data[0]),
135  (long) data.size());
136  if (Decompressor_.Decompress(jpegImg_) < 0) {
137  BIASERR("Decompression failed!");
138  return -1;
139  }
142  else
143  image=jpegImg_;
144 #endif //BIAS_HAVE_LIBJPEG
145  }
146 
147  return 0;
148 } // GrabSingle
149 
150 
152 {
153  return 0;
154 }
155 
156 
158 # ifdef BIAS_HAVE_LIBJPEG
160 # endif //BIAS_HAVE_LIBJPEG
161 
162  return 0;
163 } // CloseDevice
Defines a common interface to different devices.
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
virtual int GrabSingle(BIAS::Camera< unsigned char > &image)
void CopyIn_NoInit(void *data)
Take some data and fill it into the Image.
Definition: ImageBase.cpp:827
VideoSource_Net(bool threaded=true)
Standard Constructor use threaded param for dis-/enabling threaded receiving! If threaded receiving i...
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
BIAS::Image< unsigned char > jpegImg_
float BytesPerPixel_
BytesPerPixel cannot be int (e.g. it is 1.5 for YUV420p)
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int RegisterMsg(std::string msgName, EdataType dataType, int amount=1)
only registered msgs are accepted by commPartners Register a msg with this functions.
Definition: CScommBase.cpp:315
int Width_
Image format.
std::stringstream * pImgBuffer_
int ConnectServer(std::string serverName, unsigned int port=D_CS_DEFAULT_PORT, unsigned int timeOut=20000)
for a client: tries to connect to server with hostname serverName.
void SetVerbose(bool on)
gives some information about establising conn, and disconnecting etc.
virtual int OpenDevice()
selects the first available device to open (e.g.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int Decompress(BIAS::Image< unsigned char > &destImg, bool readComment=false)
Start decompression of previously given data (from ReadJPEG() or SetCompressedData()), results will be stored in destination image.
void Release()
Releases allocated memory.
void SetKeepAll(bool on)
If not threaded you can set this true to keep all received data.
Definition: CScommBase.hh:101
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same &quot;size&quot; as Image of other type
Definition: ImageBase.hh:73
int PostGrab()
Stop anything started in PreGrab()
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
void SetCompressedData(void *data, long length)
Copy memory area in which compressed data can be found and set its length in byte.
int Init(long size=10485760)
Initialize object with standard values and limit shared memory area to given size (in byte...
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154
int GetData(const std::string &msgName, std::vector< float > &floatData)
returns instantly:&lt;br&gt; 0 if new data has arrived and the data in xxxData 1 if no new data has arri...
Definition: CScommBase.cpp:170