Skip to content

Conversation

@zycczy
Copy link

@zycczy zycczy commented Feb 10, 2025

Description

This PR adds support for the NV12 encoding in the cv_bridge package. Change depends on common_interfaces PR: ros2/common_interfaces#264

The following changes have been made:

  • Added NV12 encoding to the list of supported encodings.
  • Implemented conversion codes for NV12 to GRAY, RGB, BGR, RGBA, and BGRA.
  • Updated getCvType and getEncoding functions to handle NV12 encoding.
  • Modified matFromImage to correctly handle height scaling for NV12 encoding.

Changes

  1. Encoding Support:

    • Added NV12 to the Encoding enum.
    • Updated getCvType and getEncoding functions to recognize NV12.
  2. Conversion Codes:

    • Added conversion codes for NV12 to GRAY, RGB, BGR, RGBA, and BGRA in getConversionCodes.
  3. Height Scaling:

    • Modified matFromImage to account for height scaling when the encoding is NV12.

- Update getCvType to return CV_8UC1 for NV12 encoding.
- Extend Encoding enum to include NV12.
- Enhance getEncoding to recognize NV12.
- Add conversion codes for NV12 to GRAY, RGB, BGR, RGBA, and BGRA.
- Modify conversion logic to handle NV12 as a color format.
- Adjust matFromImage to account for height scaling in NV12 images.

Signed-off-by: Zhaoyuan Cheng <[email protected]>
- Corrected the error message to include height scaling in the size calculation.
- Adjusted the formatting for better readability.

Signed-off-by: Zhaoyuan Cheng <[email protected]>
@zycczy
Copy link
Author

zycczy commented Apr 1, 2025

@ahcorde could you please have a review for this PR?

@ijnek ijnek requested a review from Copilot June 14, 2025 15:50
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the NV12 image encoding to the cv_bridge package, enabling conversion and proper handling of NV12-formatted data.

  • Extended getCvType and getEncoding to recognize NV12.
  • Added NV12 conversion codes for GRAY, RGB, BGR, RGBA, and BGRA.
  • Updated matFromImage to apply height scaling for NV12’s planar layout.
Comments suppressed due to low confidence (2)

cv_bridge/src/cv_bridge.cpp:299

  • NV12-specific logic was introduced here (height scaling check) and in conversion paths; please add unit tests that cover NV12 matFromImage behavior and cvtColor conversions to ensure correctness.
if (source.height * source.step * enc::getHeightScaling(source.encoding) != source.data.size()) {

cv_bridge/src/cv_bridge.cpp:102

  • [nitpick] Consider adding a comment explaining why NV12 is mapped to CV_8UC1 (i.e., single-channel Mat with interleaved Y and UV planes) to help future maintainers understand this choice.
if (encoding == enc::NV12) {return CV_8UC1;}

Comment on lines +301 to +302
ss << "Image is wrongly formed: height * step * HeightScaling != size or " << source.height << " * " <<
source.step << "*" << enc::getHeightScaling(source.encoding) << " != " << source.data.size();
Copy link

Copilot AI Jun 14, 2025

Choose a reason for hiding this comment

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

In the error message, HeightScaling is capitalized and concatenated; consider using a user-friendly phrase like "height scaling" (lowercase, with space) for clarity and consistency.

Suggested change
ss << "Image is wrongly formed: height * step * HeightScaling != size or " << source.height << " * " <<
source.step << "*" << enc::getHeightScaling(source.encoding) << " != " << source.data.size();
ss << "Image is wrongly formed: height * step * height scaling != size or " << source.height << " * " <<
source.step << " * " << enc::getHeightScaling(source.encoding) << " != " << source.data.size();

Copilot uses AI. Check for mistakes.
@ijnek
Copy link
Member

ijnek commented Jun 14, 2025

Thanks for the PR, and sorry for the delay in reviewing. Could you please update utest2.cpp to test the new format too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants