Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate previous pixel formats to new approach #239

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Also provided is a launch file that should launch the `usb_cam_node_exe` executa
The commands to run each of these different ways of starting the node are shown below:

**NOTE: you only need to run ONE of the commands below to run the node**

```
# run the executable with default settings (without params file)
ros2 run usb_cam usb_cam_node_exe
Expand Down Expand Up @@ -127,8 +128,10 @@ Possible options for this driver today are:
- `rgb8`: V4L2 capture format and ROS image encoding format of RGB8
- `yuyv`: V4L2 capture format and ROS image encoding format of YUYV
- `uyvy`: V4L2 capture format and ROS image encoding format of UYVY
- `m4202rgb8`: V4L2 capture format of M420 (aka YUV420), ROS image encoding of RGB8
- `mono8`: V4L2 capture format and ROS image encoding format of MONO8
- `mono16`: V4L2 capture format and ROS image encoding format of MONO16
- `y102mono8`: V4L2 capture format of Y10 (aka MONO10), ROS image encoding of MONO8

More formats and conversions can be added, contributions welcome!

Expand Down
17 changes: 0 additions & 17 deletions include/usb_cam/conversions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,6 @@ namespace conversions
{


inline void MONO102MONO8(const char * RAW, char * MONO, const int & NumPixels)
{
int i, j;
for (i = 0, j = 0; i < (NumPixels << 1); i += 2, j += 1) {
// first byte is low byte, second byte is high byte; smash together and convert to 8-bit
MONO[j] = (unsigned char)(((RAW[i + 0] >> 2) & 0x3F) | ((RAW[i + 1] << 6) & 0xC0));
}
}

inline void YUV4202RGB(char * YUV, char * RGB, const int & width, const int & height)
{
cv::Size size(height, width);
cv::Mat cv_img(height, width, CV_8UC1, YUV);
cv::Mat cv_out(height, width, CV_8UC3, RGB);
cv::cvtColor(cv_img, cv_out, cv::COLOR_YUV420p2RGB);
}

std::string FCC2S(const unsigned int & val)
{
std::string s;
Expand Down
79 changes: 79 additions & 0 deletions include/usb_cam/formats/m420.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2023 Evan Flynn
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Evan Flynn nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.


#ifndef USB_CAM__FORMATS__M420_HPP_
#define USB_CAM__FORMATS__M420_HPP_

#include "linux/videodev2.h"

#include "opencv2/imgproc.hpp"

#include "usb_cam/formats/pixel_format_base.hpp"
#include "usb_cam/formats/utils.hpp"


namespace usb_cam
{
namespace formats
{

class M4202RGB : public pixel_format_base
{
public:
M4202RGB(const int & width, const int & height)
: pixel_format_base(
"m4202rgb",
V4L2_PIX_FMT_M420,
usb_cam::constants::RGB8,
3,
8,
true),
m_width(width),
m_height(height)
{}

/// @brief Convert a YUV420 (aka M420) image to RGB8
void convert(const char * & src, char * & dest, const int & bytes_used) override
{
(void)bytes_used; // not used by this conversion method
cv::Size size(m_height, m_width);
const cv::Mat cv_img(m_height, m_width, CV_8UC1, const_cast<char *>(src));
cv::Mat cv_out(m_height, m_width, CV_8UC3, dest);
cv::cvtColor(cv_img, cv_out, cv::COLOR_YUV420p2RGB);
}

private:
int m_width;
int m_height;
};

} // namespace formats
} // namespace usb_cam

#endif // USB_CAM__FORMATS__M420_HPP_
1 change: 1 addition & 0 deletions include/usb_cam/formats/mjpeg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include <iostream>


extern "C" {
#define __STDC_CONSTANT_MACROS // Required for libavutil
#include "libavutil/imgutils.h"
Expand Down
37 changes: 37 additions & 0 deletions include/usb_cam/formats/mono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#ifndef USB_CAM__FORMATS__MONO_HPP_
#define USB_CAM__FORMATS__MONO_HPP_

#include "linux/videodev2.h"

#include "usb_cam/formats/pixel_format_base.hpp"
#include "usb_cam/formats/utils.hpp"

Expand All @@ -53,6 +55,7 @@ class MONO8 : public pixel_format_base
{}
};


class MONO16 : public pixel_format_base
{
public:
Expand All @@ -67,6 +70,40 @@ class MONO16 : public pixel_format_base
{}
};


/// @brief Also known as MONO10 to MONO8
class Y102MONO8 : public pixel_format_base
{
public:
explicit Y102MONO8(const int & number_of_pixels)
: pixel_format_base(
"y102mono8",
V4L2_PIX_FMT_Y10,
usb_cam::constants::MONO8,
1,
8,
true),
m_number_of_pixels(number_of_pixels)
{}

/// @brief Convert a Y10 (MONO10) image to MONO8
/// @param src pointer to source Y10 (MONO10) image
/// @param dest pointer to destination MONO8 image
/// @param bytes_used number of bytes used by source image
void convert(const char * & src, char * & dest, const int & bytes_used) override
{
(void)bytes_used; // not used by this conversion method
int i, j;
for (i = 0, j = 0; i < (m_number_of_pixels << 1); i += 2, j += 1) {
// first byte is low byte, second byte is high byte; smash together and convert to 8-bit
dest[j] = (unsigned char)(((src[i + 0] >> 2) & 0x3F) | ((src[i + 1] << 6) & 0xC0));
}
}

private:
int m_number_of_pixels;
};

} // namespace formats
} // namespace usb_cam

Expand Down
2 changes: 2 additions & 0 deletions include/usb_cam/formats/uyvy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#ifndef USB_CAM__FORMATS__UYVY_HPP_
#define USB_CAM__FORMATS__UYVY_HPP_

#include "linux/videodev2.h"

#include "usb_cam/formats/pixel_format_base.hpp"
#include "usb_cam/formats/utils.hpp"

Expand Down
2 changes: 2 additions & 0 deletions include/usb_cam/formats/yuyv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#ifndef USB_CAM__FORMATS__YUYV_HPP_
#define USB_CAM__FORMATS__YUYV_HPP_

#include "linux/videodev2.h"

#include "usb_cam/formats/pixel_format_base.hpp"
#include "usb_cam/formats/utils.hpp"

Expand Down
8 changes: 8 additions & 0 deletions include/usb_cam/usb_cam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ extern "C" {
#include "usb_cam/formats/rgb.hpp"
#include "usb_cam/formats/uyvy.hpp"
#include "usb_cam/formats/yuyv.hpp"
#include "usb_cam/formats/m420.hpp"


namespace usb_cam
Expand Down Expand Up @@ -252,7 +253,9 @@ class UsbCam
using usb_cam::formats::UYVY2RGB;
using usb_cam::formats::MONO8;
using usb_cam::formats::MONO16;
using usb_cam::formats::Y102MONO8;
using usb_cam::formats::MJPEG2RGB;
using usb_cam::formats::M4202RGB;

if (str == "rgb8") {
m_image.pixel_format = std::make_shared<RGB8>();
Expand All @@ -269,10 +272,15 @@ class UsbCam
} else if (str == "mjpeg2rgb") {
m_image.pixel_format = std::make_shared<MJPEG2RGB>(
m_image.width, m_image.height);
} else if (str == "m4202rgb") {
m_image.pixel_format = std::make_shared<M4202RGB>(
m_image.width, m_image.height);
} else if (str == "mono8") {
m_image.pixel_format = std::make_shared<MONO8>();
} else if (str == "mono16") {
m_image.pixel_format = std::make_shared<MONO16>();
} else if (str == "y102mono8") {
m_image.pixel_format = std::make_shared<Y102MONO8>(m_image.number_of_pixels);
} else {
throw std::invalid_argument("Unsupported pixel format specified: " + str);
}
Expand Down