libvisiontransfer  8.3.0
server_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2020 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <visiontransfer/deviceenumeration.h>
16 #include <visiontransfer/asynctransfer.h>
17 #include <visiontransfer/imageset.h>
18 #include <iostream>
19 #include <exception>
20 #include <vector>
21 #include <cstdio>
22 #include <cstring>
23 
24 #ifdef _WIN32
25 # include <windows.h>
26 #else
27 # include <unistd.h>
28 #endif
29 
30 #ifdef _MSC_VER
31 // Visual studio does not come with snprintf
32 #define snprintf _snprintf_s
33 #endif
34 
35 using namespace std;
36 using namespace visiontransfer;
37 
38 int main() {
39  try {
40  // Create an image transfer object that will serve as server
41  AsyncTransfer asyncTransfer("0.0.0.0", "7681", ImageProtocol::PROTOCOL_UDP, true);
42 
43  // Initialize the image set meta data
44  ImageSet imageSet;
45  imageSet.setWidth(640);
46  imageSet.setHeight(480);
47  // Define the set of contained image types, assigning indices
48  imageSet.setNumberOfImages(2);
49  imageSet.setIndexOf(ImageSet::IMAGE_LEFT, 0);
50  imageSet.setIndexOf(ImageSet::IMAGE_RIGHT, 1);
51  // Initialize data for all constituent images
52  imageSet.setPixelFormat(0, ImageSet::FORMAT_8_BIT_MONO);
53  imageSet.setPixelFormat(1, ImageSet::FORMAT_8_BIT_MONO);
54  imageSet.setRowStride(0, imageSet.getBytesPerPixel(0)*imageSet.getWidth());
55  imageSet.setRowStride(1, imageSet.getBytesPerPixel(0)*imageSet.getWidth());
56  std::vector<unsigned char> pixelData(imageSet.getRowStride(0) * imageSet.getHeight());
57  imageSet.setPixelData(0, &pixelData[0]);
58  imageSet.setPixelData(1, &pixelData[0]);
59 
60  int transfer = 0;
61  while(true) {
62 #ifdef _WIN32
63  Sleep(500);
64 #else
65  usleep(50000);
66 #endif
67  if(!asyncTransfer.isConnected()) {
68  // Continue waiting until a client connects
69  continue;
70  }
71 
72  if(transfer == 0) {
73  cout << "Client IP: " << asyncTransfer.getRemoteAddress() << endl;
74  }
75 
76  // Generate test image data
77  for(int y=0; y<imageSet.getHeight(); y++) {
78  for(int x=0; x<imageSet.getWidth(); x++) {
79  pixelData[y*imageSet.getRowStride(0) + x] = (y + x + transfer) & 0xff;
80  }
81  }
82  transfer++;
83 
84  // Send the image data
85  asyncTransfer.sendImageSetAsync(imageSet);
86  }
87  } catch(const std::exception& ex) {
88  std::cerr << "Exception occurred: " << ex.what() << std::endl;
89  }
90 
91  return 0;
92 }
void setNumberOfImages(int number)
Sets the number of valid images in this set.
Definition: imageset.h:411
int getRowStride(int imageNumber) const
Returns the row stride for the pixel data of one image.
Definition: imageset.h:218
void setPixelData(int imageNumber, unsigned char *pixelData)
Sets the pixel data for the given image.
Definition: imageset.h:134
void setWidth(int w)
Sets a new width for both images.
Definition: imageset.h:90
int getHeight() const
Returns the height of each image.
Definition: imageset.h:207
void setHeight(int h)
Sets a new width for both images.
Definition: imageset.h:95
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
Definition: imageset.h:372
void setPixelFormat(int imageNumber, ImageFormat format)
Sets the pixel format for the given image.
Definition: imageset.h:116
void setRowStride(int imageNumber, int stride)
Sets a new row stride for the pixel data of one image.
Definition: imageset.h:104
A set of one to three images, but usually two (the left camera image and the disparity map)...
Definition: imageset.h:38
Class for asynchronous transfer of image sets.
Definition: asynctransfer.h:33
void setIndexOf(ImageType what, int idx)
Assign an image index to a specified ImageType, -1 to disable.
Definition: imageset.cpp:234
int getWidth() const
Returns the width of each image.
Definition: imageset.h:202
Nerian Vision Technologies