YARP
Yet Another Robot Platform
FrameGrabberOf_Forwarder-inl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_FRAMEGRABBER_PROTOCOL_FRAMEGRABBEROF_FORWARDER_INL_H
7 #define YARP_FRAMEGRABBER_PROTOCOL_FRAMEGRABBEROF_FORWARDER_INL_H
8 
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 
12 namespace {
13 YARP_LOG_COMPONENT(FRAMEGRABBEROF_FORWARDER, "yarp.proto.framegrabber.FrameGrabberOf_Forwarder")
14 }
15 
16 namespace yarp::proto::framegrabber {
17 
18 template <typename ImageType,
19  yarp::conf::vocab32_t IfVocab,
20  yarp::conf::vocab32_t ImgVocab>
22  m_port(port)
23 {
24 };
25 
26 template <typename ImageType,
27  yarp::conf::vocab32_t IfVocab,
28  yarp::conf::vocab32_t ImgVocab>
30 {
31  yarp::os::Bottle cmd;
32  yarp::os::Bottle response;
33  cmd.addVocab32(IfVocab);
36 
37  if (m_port.getOutputCount() == 0) {
38  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
39  return false;
40  }
41 
42  if (!m_port.write(cmd, response)) {
43  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
44  return 0;
45  }
46 
47  // reply should be [fgi|fgir] [h] [is] <height>
48  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
49  !response.get(1).isVocab32() || (response.get(1).asVocab32() != VOCAB_HEIGHT) ||
50  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
51  !response.get(3).isInt32()) {
52  yCWarning(FRAMEGRABBEROF_FORWARDER, "Invalid reply received");
53  return 0;
54  }
55 
56  return response.get(3).asInt32();
57 }
58 
59 template <typename ImageType,
60  yarp::conf::vocab32_t IfVocab,
61  yarp::conf::vocab32_t ImgVocab>
63 {
64  yarp::os::Bottle cmd;
65  yarp::os::Bottle response;
66  cmd.addVocab32(IfVocab);
67  cmd.addVocab32(VOCAB_GET);
69 
70  if (m_port.getOutputCount() == 0) {
71  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
72  return false;
73  }
74 
75  if (!m_port.write(cmd, response)) {
76  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
77  return 0;
78  }
79 
80  // reply should be [fgi|fgir] [w] [is] <width>
81  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
82  !response.get(1).isVocab32() || (response.get(1).asVocab32() != VOCAB_WIDTH) ||
83  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
84  !response.get(3).isInt32()) {
85  yCWarning(FRAMEGRABBEROF_FORWARDER, "Invalid reply received");
86  return 0;
87  }
88 
89  return response.get(3).asInt32();
90 }
91 
92 template <typename ImageType,
93  yarp::conf::vocab32_t IfVocab,
94  yarp::conf::vocab32_t ImgVocab>
96 {
97  image.zero();
98 
99  yarp::os::Bottle cmd;
100  yarp::os::Bottle response;
101  cmd.addVocab32(IfVocab);
102  cmd.addVocab32(VOCAB_GET);
103  cmd.addVocab32(ImgVocab);
104 
105  if (m_port.getOutputCount() == 0) {
106  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
107  return false;
108  }
109 
110  if (!m_port.write(cmd, response)) {
111  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
112  return false;
113  }
114 
115  // reply should be [fgi|fgir] [imgr|?] [is] (<image>)
116  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
117  !response.get(1).isVocab32() || (response.get(1).asVocab32() != ImgVocab) ||
118  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
119  !response.get(3).isList()) {
120  yCWarning(FRAMEGRABBEROF_FORWARDER, "Invalid reply received");
121  return false;
122  }
123 
124  auto& serializedImage = response.get(3);
125  // FIXME Is there a way to avoid this extra copy?
126  if (!yarp::os::Portable::copyPortable(serializedImage, image)) {
127  yCWarning(FRAMEGRABBEROF_FORWARDER, "Image is not serialized properly");
128  return false;
129  }
130 
131  return true;
132 }
133 
134 
135 template <typename ImageType,
136  yarp::conf::vocab32_t IfVocab,
137  yarp::conf::vocab32_t ImgVocab>
139  yarp::sig::VectorOf<std::pair<int, int>> vertices,
140  ImageType& image)
141 {
142  image.zero();
143 
144  yarp::os::Bottle cmd;
145  yarp::os::Bottle response;
147  cmd.addVocab32(VOCAB_GET);
148  cmd.addVocab32(VOCAB_CROP);
149  cmd.addInt32(cropType);
150  yarp::os::Bottle & list = cmd.addList();
151  for (size_t i=0; i<vertices.size(); i++)
152  {
153  list.addInt32(vertices[i].first);
154  list.addInt32(vertices[i].second);
155  }
156 
157  if (m_port.getOutputCount() == 0) {
158  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
159  return false;
160  }
161 
162  if (!m_port.write(cmd, response)) {
163  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
164  return false;
165  }
166 
167  // reply should be [fgi|fgir] [crop] [is] (<image>)
168  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
169  !response.get(1).isVocab32() || (response.get(1).asVocab32() != VOCAB_CROP) ||
170  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
171  !response.get(3).isList()) {
172  yCWarning(FRAMEGRABBEROF_FORWARDER, "Cropped image is not serialized properly");
173  return false;
174  }
175 
176  auto& serializedImage = response.get(3);
177  // FIXME Is there a way to avoid this extra copy?
178  if (!yarp::os::Portable::copyPortable(serializedImage, image)) {
179  yCWarning(FRAMEGRABBEROF_FORWARDER, "...");
180  return false;
181  }
182 
183  return true;
184 }
185 
186 
187 } // namespace yarp::proto::framegrabber
188 
189 #endif // YARP_FRAMEGRABBER_PROTOCOL_FRAMEGRABBEROF_FORWARDER_INL_H
constexpr yarp::conf::vocab32_t VOCAB_FRAMEGRABBER_IMAGE
Definition: CameraVocabs.h:16
constexpr yarp::conf::vocab32_t VOCAB_CROP
Definition: CameraVocabs.h:38
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:14
constexpr yarp::conf::vocab32_t VOCAB_WIDTH
Definition: GenericVocabs.h:41
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:13
constexpr yarp::conf::vocab32_t VOCAB_HEIGHT
Definition: GenericVocabs.h:42
cropType_id_t
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:182
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition: Bottle.cpp:140
A mini-server for network communication.
Definition: Port.h:46
static bool copyPortable(const PortWriter &writer, PortReader &reader)
Copy one portable to another, via writing and reading.
Definition: Portable.cpp:16
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual bool isList() const
Checks if value is a list.
Definition: Value.cpp:162
virtual bool isInt32() const
Checks if value is a 32-bit integer.
Definition: Value.cpp:132
virtual bool isVocab32() const
Checks if value is a vocabulary identifier.
Definition: Value.cpp:174
int height() const override
Return the height of each frame.
int width() const override
Return the width of each frame.
bool getImage(ImageType &image) override
Get an image from the frame grabber.
bool getImageCrop(cropType_id_t cropType, yarp::sig::VectorOf< std::pair< int, int >> vertices, ImageType &image) override
Get a crop of the image from the frame grabber.
Provides:
Definition: Vector.h:117
#define yCWarning(component,...)
Definition: LogComponent.h:192
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
#define yCWarningThrottle(component, period,...)
Definition: LogComponent.h:195
std::int32_t vocab32_t
Definition: numeric.h:78