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 {
17 namespace proto {
18 namespace framegrabber {
19 
20 template <typename ImageType,
21  yarp::conf::vocab32_t IfVocab,
22  yarp::conf::vocab32_t ImgVocab>
24  m_port(port)
25 {
26 };
27 
28 template <typename ImageType,
29  yarp::conf::vocab32_t IfVocab,
30  yarp::conf::vocab32_t ImgVocab>
32 {
33  yarp::os::Bottle cmd;
34  yarp::os::Bottle response;
35  cmd.addVocab32(IfVocab);
38 
39  if (m_port.getOutputCount() == 0) {
40  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
41  return false;
42  }
43 
44  if (!m_port.write(cmd, response)) {
45  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
46  return 0;
47  }
48 
49  // reply should be [fgi|fgir] [h] [is] <height>
50  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
51  !response.get(1).isVocab32() || (response.get(1).asVocab32() != VOCAB_HEIGHT) ||
52  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
53  !response.get(3).isInt32()) {
54  yCWarning(FRAMEGRABBEROF_FORWARDER, "Invalid reply received");
55  return 0;
56  }
57 
58  return response.get(3).asInt32();
59 }
60 
61 template <typename ImageType,
62  yarp::conf::vocab32_t IfVocab,
63  yarp::conf::vocab32_t ImgVocab>
65 {
66  yarp::os::Bottle cmd;
67  yarp::os::Bottle response;
68  cmd.addVocab32(IfVocab);
69  cmd.addVocab32(VOCAB_GET);
71 
72  if (m_port.getOutputCount() == 0) {
73  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
74  return false;
75  }
76 
77  if (!m_port.write(cmd, response)) {
78  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
79  return 0;
80  }
81 
82  // reply should be [fgi|fgir] [w] [is] <width>
83  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
84  !response.get(1).isVocab32() || (response.get(1).asVocab32() != VOCAB_WIDTH) ||
85  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
86  !response.get(3).isInt32()) {
87  yCWarning(FRAMEGRABBEROF_FORWARDER, "Invalid reply received");
88  return 0;
89  }
90 
91  return response.get(3).asInt32();
92 }
93 
94 template <typename ImageType,
95  yarp::conf::vocab32_t IfVocab,
96  yarp::conf::vocab32_t ImgVocab>
98 {
99  image.zero();
100 
101  yarp::os::Bottle cmd;
102  yarp::os::Bottle response;
103  cmd.addVocab32(IfVocab);
104  cmd.addVocab32(VOCAB_GET);
105  cmd.addVocab32(ImgVocab);
106 
107  if (m_port.getOutputCount() == 0) {
108  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
109  return false;
110  }
111 
112  if (!m_port.write(cmd, response)) {
113  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
114  return false;
115  }
116 
117  // reply should be [fgi|fgir] [imgr|?] [is] (<image>)
118  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
119  !response.get(1).isVocab32() || (response.get(1).asVocab32() != ImgVocab) ||
120  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
121  !response.get(3).isList()) {
122  yCWarning(FRAMEGRABBEROF_FORWARDER, "Invalid reply received");
123  return false;
124  }
125 
126  auto& serializedImage = response.get(3);
127  // FIXME Is there a way to avoid this extra copy?
128  if (!yarp::os::Portable::copyPortable(serializedImage, image)) {
129  yCWarning(FRAMEGRABBEROF_FORWARDER, "Image is not serialized properly");
130  return false;
131  }
132 
133  return true;
134 }
135 
136 
137 template <typename ImageType,
138  yarp::conf::vocab32_t IfVocab,
139  yarp::conf::vocab32_t ImgVocab>
141  yarp::sig::VectorOf<std::pair<int, int>> vertices,
142  ImageType& image)
143 {
144  image.zero();
145 
146  yarp::os::Bottle cmd;
147  yarp::os::Bottle response;
149  cmd.addVocab32(VOCAB_GET);
150  cmd.addVocab32(VOCAB_CROP);
151  cmd.addInt32(cropType);
152  yarp::os::Bottle & list = cmd.addList();
153  for (size_t i=0; i<vertices.size(); i++)
154  {
155  list.addInt32(vertices[i].first);
156  list.addInt32(vertices[i].second);
157  }
158 
159  if (m_port.getOutputCount() == 0) {
160  yCWarningThrottle(FRAMEGRABBEROF_FORWARDER, 5.0, "Remote port is not connected");
161  return false;
162  }
163 
164  if (!m_port.write(cmd, response)) {
165  yCWarning(FRAMEGRABBEROF_FORWARDER, "Could not execute RPC command");
166  return false;
167  }
168 
169  // reply should be [fgi|fgir] [crop] [is] (<image>)
170  if (!response.get(0).isVocab32() || (response.get(0).asVocab32() != IfVocab) ||
171  !response.get(1).isVocab32() || (response.get(1).asVocab32() != VOCAB_CROP) ||
172  !response.get(2).isVocab32() || (response.get(2).asVocab32() != VOCAB_IS) ||
173  !response.get(3).isList()) {
174  yCWarning(FRAMEGRABBEROF_FORWARDER, "Cropped image is not serialized properly");
175  return false;
176  }
177 
178  auto& serializedImage = response.get(3);
179  // FIXME Is there a way to avoid this extra copy?
180  if (!yarp::os::Portable::copyPortable(serializedImage, image)) {
181  yCWarning(FRAMEGRABBEROF_FORWARDER, "...");
182  return false;
183  }
184 
185  return true;
186 }
187 
188 
189 
190 } // namespace framegrabber
191 } // namespace proto
192 } // namespace yarp
193 
194 #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:74
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:47
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:119
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
#define yCWarningThrottle(component, period,...)
Definition: LogComponent.h:146
std::int32_t vocab32_t
Definition: numeric.h:78
The main, catch-all namespace for YARP.
Definition: dirs.h:16