-
Notifications
You must be signed in to change notification settings - Fork 0
/
zn.cpp
46 lines (30 loc) · 1.18 KB
/
zn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <zn.hpp>
using namespace std;
using namespace cv;
namespace zn {
void my_free(void *data, void *hint) { }
struct MetaData {
int depth;
int rows;
int cols;
int channels;
};
void send_image(zmq::socket_t &socket, cv::Mat &image) {
static MetaData meta;
static zmq::mutable_buffer mbuf(&meta, sizeof(MetaData));
static zmq::message_t image_bytes;
meta = {image.depth(), image.rows, image.cols, image.channels()};
socket.send(mbuf, zmq::send_flags::sndmore);
image_bytes.rebuild(image.data, image.total() * image.elemSize(), my_free);
socket.send(image_bytes, zmq::send_flags::none);
}
void receive_image(zmq::socket_t &socket, cv::Mat &image) {
static MetaData meta;
static zmq::mutable_buffer mbuf(&meta, sizeof(MetaData));
static unsigned char *image_data = new unsigned char [MAX_ROWS*MAX_COLS*MAX_CHANNELS*BYTES_PER_PIXEL];
static zmq::mutable_buffer mbuf_image(image_data, MAX_ROWS*MAX_COLS*MAX_CHANNELS*BYTES_PER_PIXEL);
socket.recv(mbuf, zmq::recv_flags::none);
socket.recv(mbuf_image, zmq::recv_flags::none);
image = cv::Mat(meta.rows, meta.cols, CV_MAKETYPE(meta.depth, meta.channels), image_data);
}
}