Skip to content

Conversation

@MohammadSadeghSalehi
Copy link

This PR fixes a potential overflow bug in rgblike_to_depthmap when computing
depth values from RGB-like depth images.

Changes:

  • Cast image tensors/arrays to int32 before performing the 16-bit combination (*256 +).
  • Prevents overflow when input dtype is uint8 or small int.
  • Supports both PyTorch and NumPy paths consistently.
  • Added comments for optional uint16 casting.

Rationale:
The original implementation used 2**8 multiplication directly on small integer tensors,
which could overflow for high byte values (≥128). The fix ensures safe arithmetic and
consistent return types.

cc @patrickvonplaten @multimodalart @psuraj

# You may want to cast the final result to uint16, but casting to a
# larger int type (like int32) is sufficient to fix the overflow.
# depth_map = depth_map.to(torch.uint16) # Uncomment if uint16 is strictly required
return depth_map
Copy link
Collaborator

Choose a reason for hiding this comment

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

Change looks good. Could we just store the original dtype and cast back once the computation is done.

 original_dtype = image.dtype

# perform compute
return depth_map.to(original_dtype)

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