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

Enhance NV12 and NV21 Support in sensor_msgs::image_encodings #264

Open
wants to merge 1 commit into
base: rolling
Choose a base branch
from
Open
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
105 changes: 62 additions & 43 deletions sensor_msgs/include/sensor_msgs/image_encodings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,41 @@ static inline bool hasAlpha(const std::string & encoding)
encoding == RGBA16 || encoding == BGRA16;
}

static inline bool isPlanar(const std::string & encoding)
{
return encoding == NV12 || encoding == NV21 || encoding == NV24;
}

static inline int numChannels(const std::string & encoding)
{
// First do the common-case encodings
if (encoding == MONO8 ||
encoding == MONO16)
encoding == MONO16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the whitespace changes. It makes the diff smaller, and a little easier to review

{
return 1;
}
if (encoding == BGR8 ||
encoding == RGB8 ||
encoding == BGR16 ||
encoding == RGB16)
encoding == RGB8 ||
encoding == BGR16 ||
encoding == RGB16)
{
return 3;
}
if (encoding == BGRA8 ||
encoding == RGBA8 ||
encoding == BGRA16 ||
encoding == RGBA16)
encoding == RGBA8 ||
encoding == BGRA16 ||
encoding == RGBA16)
{
return 4;
}
if (encoding == BAYER_RGGB8 ||
encoding == BAYER_BGGR8 ||
encoding == BAYER_GBRG8 ||
encoding == BAYER_GRBG8 ||
encoding == BAYER_RGGB16 ||
encoding == BAYER_BGGR16 ||
encoding == BAYER_GBRG16 ||
encoding == BAYER_GRBG16)
encoding == BAYER_BGGR8 ||
encoding == BAYER_GBRG8 ||
encoding == BAYER_GRBG8 ||
encoding == BAYER_RGGB16 ||
encoding == BAYER_BGGR16 ||
encoding == BAYER_GBRG16 ||
encoding == BAYER_GRBG16)
{
return 1;
}
Expand All @@ -191,13 +196,16 @@ static inline int numChannels(const std::string & encoding)
return (m[3] == "") ? 1 : std::atoi(m[3].str().c_str());
}

if (encoding == NV12 ||
encoding == NV21)
{
return 1;
}
if (encoding == YUV422 ||
encoding == YUV422_YUY2 ||
encoding == UYVY ||
encoding == YUYV ||
encoding == NV12 ||
encoding == NV21 ||
encoding == NV24)
encoding == YUV422_YUY2 ||
encoding == UYVY ||
encoding == YUYV ||
encoding == NV24)
{
return 2;
}
Expand All @@ -212,27 +220,27 @@ static inline int bitDepth(const std::string & encoding)
return 16;
}
if (encoding == MONO8 ||
encoding == BGR8 ||
encoding == RGB8 ||
encoding == BGRA8 ||
encoding == RGBA8 ||
encoding == BAYER_RGGB8 ||
encoding == BAYER_BGGR8 ||
encoding == BAYER_GBRG8 ||
encoding == BAYER_GRBG8)
encoding == BGR8 ||
encoding == RGB8 ||
encoding == BGRA8 ||
encoding == RGBA8 ||
encoding == BAYER_RGGB8 ||
encoding == BAYER_BGGR8 ||
encoding == BAYER_GBRG8 ||
encoding == BAYER_GRBG8)
{
return 8;
}

if (encoding == MONO16 ||
encoding == BGR16 ||
encoding == RGB16 ||
encoding == BGRA16 ||
encoding == RGBA16 ||
encoding == BAYER_RGGB16 ||
encoding == BAYER_BGGR16 ||
encoding == BAYER_GBRG16 ||
encoding == BAYER_GRBG16)
encoding == BGR16 ||
encoding == RGB16 ||
encoding == BGRA16 ||
encoding == RGBA16 ||
encoding == BAYER_RGGB16 ||
encoding == BAYER_BGGR16 ||
encoding == BAYER_GBRG16 ||
encoding == BAYER_GRBG16)
{
return 16;
}
Expand All @@ -241,23 +249,34 @@ static inline int bitDepth(const std::string & encoding)
std::cmatch m;
// ex. 8UC -> 8, 8UC10 -> 10
if (std::regex_match(encoding.c_str(), m, cv_type_regex)) {
return std::atoi(m[0].str().c_str());
return std::atoi(m[1].str().c_str());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this change do? Mind also adding a bounds check to make sure m[1] exists?

}

if (encoding == YUV422 ||
encoding == YUV422_YUY2 ||
encoding == UYVY ||
encoding == YUYV ||
encoding == NV12 ||
encoding == NV21 ||
encoding == NV24)
encoding == YUV422_YUY2 ||
encoding == UYVY ||
encoding == YUYV ||
encoding == NV12 ||
encoding == NV21 ||
encoding == NV24)
{
return 8;
}

throw std::runtime_error("Unknown encoding " + encoding);
return -1;
}

static inline float getHeightScaling(const std::string & encoding)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
if (isPlanar(encoding)) {
if (encoding == NV12 ||
encoding == NV21)
return 1.5f;
}
return 1.0f;
}

} // namespace image_encodings
} // namespace sensor_msgs

Expand Down