Skip to content

Fix RGB channel swap in STBICodec RGB-to-RGBA conversion (v2-3)#567

Open
taylorhoward92 wants to merge 1 commit intoOGRECave:v2-3from
taylorhoward92:fix-stbi-rgb-channel-order
Open

Fix RGB channel swap in STBICodec RGB-to-RGBA conversion (v2-3)#567
taylorhoward92 wants to merge 1 commit intoOGRECave:v2-3from
taylorhoward92:fix-stbi-rgb-channel-order

Conversation

@taylorhoward92
Copy link
Copy Markdown

Summary

  • Fix red/blue channel swap when loading 3-channel RGB images through the STB codec
  • The variable names b, g, r assume BGR input, but stb_image always returns RGB
  • Writing r, g, b (actually B, G, R) into PFG_RGBA8_UNORM (R-first) swaps red and blue
  • master (v3-0) already has the correct b, g, r write order — this backports the fix to v2-3

Visual evidence

See the screenshots in osrf/homebrew-simulation#3394 (comment) — the "Rescue Randy" model has blue skin and teal jacket instead of brown skin and lime jacket when ogre 2.3 is built without FreeImage (STB codec active).

Test plan

  • Build ogre-next 2.3 without FreeImage (OGRE_CONFIG_ENABLE_FREEIMAGE=OFF, OGRE_CONFIG_ENABLE_STBI=ON)
  • Load a scene with 3-channel RGB textures (e.g. fuel_textured_mesh.sdf in gz-sim)
  • Verify that colors render correctly (no red/blue swap)

stb_image always returns pixel data in RGB byte order, but the
variable names in the 3-component conversion loop (b, g, r) assume
BGR order. The code then writes r, g, b — which actually outputs
B, G, R — into a buffer declared as PFG_RGBA8_UNORM (R first),
swapping the red and blue channels.

Fix by writing b, g, r (preserving stb's native RGB order), which
matches the correct behaviour already present on master (v3-0).

The visual effect of this bug is that all 3-channel RGB textures
rendered through the STB codec have red and blue swapped (e.g. brown
skin appears blue, lime-green appears teal). See the screenshots in
osrf/homebrew-simulation#3394 (comment 4101535199) for an example.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@taylorhoward92 taylorhoward92 changed the title Fix RGB channel swap in STBICodec RGB-to-RGBA conversion Fix RGB channel swap in STBICodec RGB-to-RGBA conversion (v2-3) Apr 9, 2026
@taylorhoward92 taylorhoward92 marked this pull request as ready for review April 9, 2026 08:40
@wentasah
Copy link
Copy Markdown
Contributor

wentasah commented Apr 9, 2026

@taylorhoward92 I quickly tested your patch and it fixes the color swap problem in fuel_textured_mesh.sdf as well as in Rubicon world (see lopsided98/nix-ros-overlay#746 (comment)). Thanks!

taylorhoward92 added a commit to taylorhoward92/nixpkgs that referenced this pull request Apr 9, 2026
Adds patch from OGRECave/ogre-next#567 to fix RGB→RGBA channel swap in
STBICodec. Updates hash for PR 566 patch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
taylorhoward92 added a commit to taylorhoward92/nixpkgs that referenced this pull request Apr 9, 2026
Adds patch from OGRECave/ogre-next#567 to fix RGB→RGBA channel swap in
STBICodec. Updates hash for PR 566 patch.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@taylorhoward92
Copy link
Copy Markdown
Author

Tested via Gazebo.

Without the fix:
Without RGB Patch

With the fix:
With RGB Patch

@scpeters
Copy link
Copy Markdown
Contributor

scpeters commented Apr 9, 2026

Summary

  • Fix red/blue channel swap when loading 3-channel RGB images through the STB codec
  • The variable names b, g, r assume BGR input, but stb_image always returns RGB
  • Writing r, g, b (actually B, G, R) into PFG_RGBA8_UNORM (R-first) swaps red and blue
  • master (v3-0) already has the correct b, g, r write order — this backports the fix to v2-3

it looks like the fix in 3.0.0 was made in #317 as part of a larger change

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.

3 participants