Skip to content

Commit

Permalink
Refactors OCS image callbacks, pulling image formatting into a separa…
Browse files Browse the repository at this point in the history
…te function.

Fixes #5
  • Loading branch information
barulicm committed May 24, 2016
1 parent 577ee69 commit e8dcbfc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
60 changes: 28 additions & 32 deletions autorally_core/src/ocs/qnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,7 @@ void QNode::imageMaskCallback(const autorally_msgs::imageMask& msg)

void QNode::imageCallback1(const sensor_msgs::ImageConstPtr& msg)
{
QImage::Format format = QImage::Format_Indexed8;
unsigned char *newData = 0;
if(msg->encoding == "bgr8") {
format = QImage::Format_RGB888;
newData = convertBGRtoRGB(&msg->data[0], msg->step * msg->height);
}
if(msg->encoding == "rgb8")
format = QImage::Format_RGB888;
if(msg->encoding == "mono16")
ROS_WARN("The OCS does not currently support 16bpp images. This image topic will not render correctly.");
QImage img(msg->encoding == "bgr8" ? newData : &msg->data[0],
msg->width,
msg->height,
msg->step,
format);
QImage img = formatImageForDisplay(msg);

if(!pthread_mutex_trylock(&m_imageMutex))
{
Expand All @@ -271,26 +257,11 @@ void QNode::imageCallback1(const sensor_msgs::ImageConstPtr& msg)
}
pthread_mutex_unlock(&m_imageMutex);
}
delete newData;
}

void QNode::imageCallback2(const sensor_msgs::ImageConstPtr& msg)
{
QImage::Format format = QImage::Format_Indexed8;
unsigned char *newData = 0;
if(msg->encoding == "bgr8") {
format = QImage::Format_RGB888;
newData = convertBGRtoRGB(&msg->data[0], msg->step * msg->height);
}
if(msg->encoding == "rgb8")
format = QImage::Format_RGB888;
if(msg->encoding == "mono16")
ROS_WARN("The OCS does not currently support 16bpp images. This image topic will not render correctly.");
QImage img(msg->encoding == "bgr8" ? newData : &msg->data[0],
msg->width,
msg->height,
msg->step,
format);
QImage img = formatImageForDisplay(msg);

if(!pthread_mutex_trylock(&m_imageMutex))
{
Expand All @@ -300,7 +271,6 @@ void QNode::imageCallback2(const sensor_msgs::ImageConstPtr& msg)
}
pthread_mutex_unlock(&m_imageMutex);
}
delete newData;
}

unsigned char* QNode::convertBGRtoRGB(const unsigned char* data, int size) {
Expand Down Expand Up @@ -401,3 +371,29 @@ void QNode::switchImageTopic(int subscriber, std::string name)
}
}
}

QImage QNode::formatImageForDisplay(const sensor_msgs::ImageConstPtr &msg)
{
QImage::Format format = QImage::Format_Indexed8;

std::string encoding = msg->encoding;

unsigned char *newData = nullptr;

if(encoding == "bgr8") {
format = QImage::Format_RGB888;
newData = convertBGRtoRGB(&msg->data[0], msg->step * msg->height);
}
else if(encoding == "rgb8") {
format = QImage::Format_RGB888;
}
else {
ROS_WARN_STREAM("The OCS does not currently support " << encoding << " images. This image topic will not render correctly.");
}

QImage img = QImage(encoding == "bgr8" ? newData : &msg->data[0], msg->width, msg->height, msg->step, format).copy();

delete newData;

return img;
}

This comment has been minimized.

Copy link
@elfring

elfring Jun 2, 2016

How do you think about to add a newline here?

This comment has been minimized.

Copy link
@barulicm

barulicm Jun 3, 2016

Author Contributor

@elfring According to the C++11 Standard, which we compile against, the newline is no longer required:

A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file (C++11 §2.2/1).

Is there another reason you might recommend this?

Thank you for your feedback.

This comment has been minimized.

Copy link
@elfring

elfring Jun 3, 2016

Do you care for specifications from previous versions of the programming language "C++" (and corresponding compilers)?

3 changes: 3 additions & 0 deletions autorally_core/src/ocs/qnode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ public slots:
*/
unsigned char* convertBGRtoRGB(const unsigned char* data, int size);


QImage formatImageForDisplay(const sensor_msgs::ImageConstPtr& msg);

};

#endif /* QNODE_HPP_ */

0 comments on commit e8dcbfc

Please sign in to comment.