From 4068b3207e7e12f5aaadea2bee54362cf7d2a4f7 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 5 Feb 2021 01:15:19 -0800 Subject: [PATCH 1/2] visualize single channel 8 bit image Signed-off-by: Ian Chen --- src/plugins/image_display/ImageDisplay.cc | 57 +++++++++++++++++++++++ src/plugins/image_display/ImageDisplay.hh | 3 ++ 2 files changed, 60 insertions(+) diff --git a/src/plugins/image_display/ImageDisplay.cc b/src/plugins/image_display/ImageDisplay.cc index 0bbe17dd3..80a471d55 100644 --- a/src/plugins/image_display/ImageDisplay.cc +++ b/src/plugins/image_display/ImageDisplay.cc @@ -160,6 +160,9 @@ void ImageDisplay::ProcessImage() case msgs::PixelFormatType::L_INT16: this->UpdateFromLInt16(); break; + case msgs::PixelFormatType::L_INT8: + this->UpdateFromLInt8(); + break; default: { ignwarn << "Unsupported image type: " @@ -339,6 +342,60 @@ void ImageDisplay::UpdateFromLInt16() delete[] buffer; } +///////////////////////////////////////////////// +void ImageDisplay::UpdateFromLInt8() +{ + unsigned int height = this->dataPtr->imageMsg.height(); + unsigned int width = this->dataPtr->imageMsg.width(); + QImage::Format qFormat = QImage::Format_RGB888; + + QImage image = QImage(width, height, qFormat); + + unsigned int samples = width * height; + unsigned char type; + // cppchecker recommends using sizeof(varname) + unsigned int bufferSize = samples * sizeof(type); + + unsigned char *buffer = new unsigned char[samples]; + memcpy(buffer, this->dataPtr->imageMsg.data().c_str(), + bufferSize); + + // get min and max of temperature values + unsigned int min = 255; + unsigned int max = 0; + for (unsigned int i = 0; i < samples; ++i) + { + unsigned int temp = static_cast(buffer[i]); + if (temp > max) + max = temp; + if (temp < min) + min = temp; + } + + // convert temperature to grayscale image + double range = static_cast(max - min); + if (ignition::math::equal(range, 0.0)) + range = 1.0; + unsigned int idx = 0; + for (unsigned int j = 0; j < height; ++j) + { + for (unsigned int i = 0; i < width; ++i) + { + unsigned int temp = static_cast(buffer[idx++]); + double t = static_cast(temp-min) / range; + int r = 255*t; + int g = r; + int b = r; + QRgb value = qRgb(r, g, b); + image.setPixel(i, j, value); + } + } + this->dataPtr->provider->SetImage(image); + this->newImage(); + + delete[] buffer; +} + ///////////////////////////////////////////////// QStringList ImageDisplay::TopicList() const { diff --git a/src/plugins/image_display/ImageDisplay.hh b/src/plugins/image_display/ImageDisplay.hh index 240bce24f..5ff8429e9 100644 --- a/src/plugins/image_display/ImageDisplay.hh +++ b/src/plugins/image_display/ImageDisplay.hh @@ -99,6 +99,9 @@ namespace plugins /// \brief Update from L_INT16 private: void UpdateFromLInt16(); + /// \brief Update from L_IN8T + private: void UpdateFromLInt8(); + /// \brief Subscriber callback when new image is received /// \param[in] _msg New image private: void OnImageMsg(const ignition::msgs::Image &_msg); From c7b0fce46369c1ac9570fa434c5254b5c2ac8643 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Mon, 8 Feb 2021 13:57:55 -0800 Subject: [PATCH 2/2] add todo note Signed-off-by: Ian Chen --- src/plugins/image_display/ImageDisplay.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/plugins/image_display/ImageDisplay.cc b/src/plugins/image_display/ImageDisplay.cc index 80a471d55..030e88c18 100644 --- a/src/plugins/image_display/ImageDisplay.cc +++ b/src/plugins/image_display/ImageDisplay.cc @@ -345,6 +345,8 @@ void ImageDisplay::UpdateFromLInt16() ///////////////////////////////////////////////// void ImageDisplay::UpdateFromLInt8() { + // todo(anyone) the code in this function is very similar to + // UpdateFromInt16 and UpdateFromFloat32. Consider merging these functions. unsigned int height = this->dataPtr->imageMsg.height(); unsigned int width = this->dataPtr->imageMsg.width(); QImage::Format qFormat = QImage::Format_RGB888;