Skip to content

Commit 89b4ff7

Browse files
committed
Fix raw_mjpeg function and shrink the compressed image msg size
1 parent 52dd75f commit 89b4ff7

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

include/usb_cam/usb_cam.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ class UsbCam
221221
return m_image.size_in_bytes;
222222
}
223223

224+
inline void reset_image_size_in_bytes()
225+
{
226+
m_image.set_size_in_bytes();
227+
}
228+
224229
inline size_t get_image_size_in_pixels()
225230
{
226231
return m_image.number_of_pixels;

src/ros2/usb_cam_node.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void UsbCamNode::init()
171171

172172
// if pixel format is equal to 'mjpeg', i.e. raw mjpeg stream, initialize compressed image message
173173
// and publisher
174-
if (m_parameters.pixel_format_name == "mjpeg") {
174+
if (m_parameters.pixel_format_name == "raw_mjpeg") {
175175
m_compressed_img_msg.reset(new sensor_msgs::msg::CompressedImage());
176176
m_compressed_img_msg->header.frame_id = m_parameters.frame_id;
177177
m_compressed_image_publisher =
@@ -391,6 +391,8 @@ bool UsbCamNode::take_and_send_image_mjpeg()
391391

392392
// grab the image, pass image msg buffer to fill
393393
m_camera->get_image(reinterpret_cast<char *>(&m_compressed_img_msg->data[0]));
394+
m_compressed_img_msg->data.resize(m_camera->get_image_size_in_bytes());
395+
m_camera->reset_image_size_in_bytes(); // reset for next grab
394396

395397
auto stamp = m_camera->get_image_timestamp();
396398
m_compressed_img_msg->header.stamp.sec = stamp.tv_sec;
@@ -423,7 +425,7 @@ void UsbCamNode::update()
423425
// If the camera exposure longer higher than the framerate period
424426
// then that caps the framerate.
425427
// auto t0 = now();
426-
bool isSuccessful = (m_parameters.pixel_format_name == "mjpeg") ?
428+
bool isSuccessful = (m_parameters.pixel_format_name == "raw_mjpeg") ?
427429
take_and_send_image_mjpeg() :
428430
take_and_send_image();
429431
if (!isSuccessful) {

src/usb_cam.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ void UsbCam::process_image(const char * src, char * & dest, const int & bytes_us
8282
// TODO(flynneva): could we skip the copy here somehow?
8383
// If no conversion required, just copy the image from V4L2 buffer
8484
if (m_image.pixel_format->requires_conversion() == false) {
85-
memcpy(dest, src, m_image.size_in_bytes);
85+
m_image.size_in_bytes = bytes_used;
86+
memcpy(dest, src, bytes_used);
8687
} else {
8788
m_image.pixel_format->convert(src, dest, bytes_used);
8889
}

0 commit comments

Comments
 (0)