libvisiontransfer  10.6.0
server_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies 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 using namespace std;
31 using namespace visiontransfer;
32 
33 int main() {
34  try {
35  // Create an image transfer object that will serve as server
36  AsyncTransfer asyncTransfer("0.0.0.0", "7681", ImageProtocol::PROTOCOL_UDP, true);
37 
38  // Initialize the image set meta data
39  ImageSet imageSet;
40  imageSet.setWidth(640);
41  imageSet.setHeight(480);
42  // Define the set of contained image types, assigning indices
43  imageSet.setNumberOfImages(2);
44  imageSet.setIndexOf(ImageSet::IMAGE_LEFT, 0);
45  imageSet.setIndexOf(ImageSet::IMAGE_RIGHT, 1);
46  // Initialize data for all constituent images
47  imageSet.setPixelFormat(0, ImageSet::FORMAT_8_BIT_MONO);
48  imageSet.setPixelFormat(1, ImageSet::FORMAT_8_BIT_MONO);
49  imageSet.setRowStride(0, imageSet.getBytesPerPixel(0)*imageSet.getWidth());
50  imageSet.setRowStride(1, imageSet.getBytesPerPixel(0)*imageSet.getWidth());
51  std::vector<unsigned char> pixelData(imageSet.getRowStride(0) * imageSet.getHeight());
52  imageSet.setPixelData(0, &pixelData[0]);
53  imageSet.setPixelData(1, &pixelData[0]);
54 
55  int transfer = 0;
56  while(true) {
57 #ifdef _WIN32
58  Sleep(500);
59 #else
60  usleep(50000);
61 #endif
62  if(!asyncTransfer.isConnected()) {
63  // Continue waiting until a client connects
64  continue;
65  }
66 
67  if(transfer == 0) {
68  cout << "Client IP: " << asyncTransfer.getRemoteAddress() << endl;
69  }
70 
71  // Generate test image data
72  for(int y=0; y<imageSet.getHeight(); y++) {
73  for(int x=0; x<imageSet.getWidth(); x++) {
74  pixelData[y*imageSet.getRowStride(0) + x] = (y + x + transfer) & 0xff;
75  }
76  }
77  transfer++;
78 
79  // Send the image data
80  asyncTransfer.sendImageSetAsync(imageSet);
81  }
82  } catch(const std::exception& ex) {
83  std::cerr << "Exception occurred: " << ex.what() << std::endl;
84  }
85 
86  return 0;
87 }
visiontransfer::ImageSet::getHeight
int getHeight() const
Returns the height of each image.
Definition: imageset.h:234
visiontransfer::ImageSet::getWidth
int getWidth() const
Returns the width of each image.
Definition: imageset.h:229
visiontransfer::ImageSet::setIndexOf
void setIndexOf(ImageType what, int idx)
Assign an image index to a specified ImageType, -1 to disable.
Definition: imageset.cpp:240
visiontransfer::ImageSet::getRowStride
int getRowStride(int imageNumber) const
Returns the row stride for the pixel data of one image.
Definition: imageset.h:245
visiontransfer::ImageSet::setPixelData
void setPixelData(int imageNumber, unsigned char *pixelData)
Sets the pixel data for the given image.
Definition: imageset.h:161
visiontransfer::ImageSet
A set of one to three images, but usually two (the left camera image and the disparity map)....
Definition: imageset.h:50
visiontransfer::ImageSet::setRowStride
void setRowStride(int imageNumber, int stride)
Sets a new row stride for the pixel data of one image.
Definition: imageset.h:131
visiontransfer::ImageSet::getBytesPerPixel
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
Definition: imageset.h:399
visiontransfer::ImageSet::setHeight
void setHeight(int h)
Sets a new width for both images.
Definition: imageset.h:122
visiontransfer::ImageSet::setPixelFormat
void setPixelFormat(int imageNumber, ImageFormat format)
Sets the pixel format for the given image.
Definition: imageset.h:143
visiontransfer::AsyncTransfer
Class for asynchronous transfer of image sets.
Definition: asynctransfer.h:45
visiontransfer::ImageSet::setWidth
void setWidth(int w)
Sets a new width for both images.
Definition: imageset.h:117
visiontransfer::ImageSet::setNumberOfImages
void setNumberOfImages(int number)
Sets the number of valid images in this set.
Definition: imageset.h:438
Allied Vision