Skip to content

fix: ROSBag finds the end of image data from the message layout, not a guess - #163

Merged
petercorke merged 2 commits into
mainfrom
fix/rosbag-realsense-footer
Oct 22, 2026
Merged

petercorke merged 2 commits into
mainfrom
fix/rosbag-realsense-footer

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

The Intel RealSense SDK follows every image message in a bag with 4 extra bytes (a float32 depth scale). The reader handled that by assuming 4 trailing bytes on every message of any bag with a realsense_msgs connection, and only when release="auto". Tested on the real bags in a development checkout, this had three problems:

Bag Old reader New reader
RealSense SDK recording (4-byte footers) default ROSBag(path) reads 0 frames (SerdeError: ROS1 message size mismatch); auto works but discards the footer reads all frames; depth_units == 0.001 on the depth stream
Intel's sample bag single_depth_color_640x480.bag (realsense_msgs connections, no footers) auto returns 0 frames, silently (it removes 4 real bytes from every image) reads all frames
the 12 other bags (ROS 1 and a ROS 2 MCAP recording) fine byte-identical output

What changes

  • sensor_msgs/Image and CompressedImage (ROS 1 and CDR, either byte order) are parsed to the end of their data array, checking length == height * step. Only those bytes are deserialized; anything after them is the footer. Pixels are unchanged.
  • What to make of the footer is decided by context, because the bytes cannot decide it (about half of all 4-byte patterns are a positive finite float32). In a bag recognised as RealSense, a positive finite float becomes the image's depth_units attribute (metres per unit) and four zero bytes (a color image) mean no scale. Anything else after an image (the same 4 bytes in an unrecognised bag, another length, a negative or non-finite value) is ignored with a RuntimeWarning, once per topic. CDR padding is silent.
  • Every other message type must be exactly the length its type says, and is no longer retried with 0/4/8 bytes removed. All 68,889 non-image messages in the 13 real bags decode at their exact length, so that guess was not needed.
  • SerdeError is caught as well as AssertionError (current rosbags raises SerdeError).
  • The reasoning is in the docstrings of ROSBag, _image_payload_end, _footer_depth_units and _image_depth_units.

Related issue

Refs #151. The depth_units= / invalid_is_nan= conversion options are not in this PR; they will follow separately.

Credit

The approach of finding the end of the data from the message layout follows #154 (@TianHengZhuang), and the technique for building a synthetic RealSense-style bag in the tests follows #155 (@theluckystrike). Both are credited as co-authors on the commit. Thank you both.

Verification

  • New tests/test_ros_realsense.py (332 tests): the layout parser, footer decoder, synthetic RealSense-style bags written to a temporary directory by tests/create_ros_fixture.py (ROS 1 / CDR LE / CDR BE, with and without footers and realsense_msgs), unexpected trailing bytes, exact-length decoding of other messages, and checks against real bags that skip when bags/ is absent. No binary fixtures are committed.
  • Run against the old reader, 7 of 12 synthetic cases come out wrong or incomplete.
  • pytest tests: 1367 passed, 44 skipped.
  • Old vs new reader on every bag in bags/ (14 files): identical pixels on every frame.

Checklist

  • PR title follows Conventional Commits
  • Tests pass locally (pytest)
  • Added tests for this change
  • Docstrings updated

🤖 Generated with Claude Code

…a guess

The Intel RealSense SDK follows every image message in a bag with 4 extra bytes
(a float32 depth scale). The reader handled that by assuming 4 trailing bytes on
every message of any bag that has a realsense_msgs connection, and only when
release="auto". Tested on real bags this had three problems:

- ROSBag(path) with the default release="ROS1_NOETIC" read no frames at all from
  the RealSense recording: SerdeError "ROS1 message size mismatch". Current
  rosbags raises SerdeError, the code only caught AssertionError.
- Intel's own sample bag (single_depth_color_640x480.bag) has realsense_msgs
  connections but no extra bytes. With release="auto" the reader removed 4 real
  bytes from every image and silently returned no frames.
- The depth scale was discarded.

Now sensor_msgs/Image and CompressedImage (ROS 1 and CDR, either byte order) are
parsed up to the end of their data array, checking that the array length is
height * step. Only those bytes are deserialized and anything after them is the
footer. Pixels are unchanged.

What to make of the footer is decided by context, because the bytes cannot
decide it: about half of all 4-byte patterns are a positive, finite float32.
In a bag recognised as RealSense (a realsense_msgs connection), a positive finite
float32 is attached to the image as `depth_units` (metres per unit) and 4 zero
bytes (a color image) mean no scale. Anything else after an image, such as the
same 4 bytes in a bag that is not recognised, another length, or a negative or
non-finite value, is ignored with a RuntimeWarning, once per topic. CDR padding
(up to 3 zero bytes) is silent.

Every other message type must be exactly the length its type says, as a
serialization format requires, and is no longer tried with 0, 4 and 8 bytes
removed. That guess was unnecessary: all 68,889 non-image messages in the 13 real
bags tested decode at their exact length.

Verified on every bag in bags/ (14 files, old reader vs new): output is
byte-identical for all except the two cases above, which now work; the RealSense
bag gives depth_units == 0.001 on its 37 depth frames and no warnings.

Adds tests/test_ros_realsense.py: the layout parser and footer decoder, synthetic
RealSense-style bags written to a temporary directory by
tests/create_ros_fixture.py (ROS 1 and CDR, with and without footers and
realsense_msgs), unexpected trailing bytes, exact-length decoding of other
messages, and checks against the real bags when they are present.

Refs #151. The depth_units=/invalid_is_nan= conversion options are not in this
change. The layout-parsing approach follows #154 and the technique for building
the synthetic bag follows #155.

Co-authored-by: TianHengZhuang <39034691+TianHengZhuang@users.noreply.github.com>
Co-authored-by: theluckystrike <51033404+theluckystrike@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 77 complexity · 0 duplication

Metric Results
Complexity 77
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 20c6efe into main Oct 22, 2026
39 checks passed
@petercorke
petercorke deleted the fix/rosbag-realsense-footer branch October 22, 2026 02:33
@github-actions github-actions Bot mentioned this pull request Oct 22, 2026
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.

1 participant