Skip to content

Improve RGB input handling in color conversion (support normalized an…#2264

Open
THE-Amrit-mahto-05 wants to merge 2 commits intoCCExtractor:masterfrom
THE-Amrit-mahto-05:fix/color-normalization-clean
Open

Improve RGB input handling in color conversion (support normalized an…#2264
THE-Amrit-mahto-05 wants to merge 2 commits intoCCExtractor:masterfrom
THE-Amrit-mahto-05:fix/color-normalization-clean

Conversation

@THE-Amrit-mahto-05
Copy link
Copy Markdown
Contributor

@THE-Amrit-mahto-05 THE-Amrit-mahto-05 commented Apr 13, 2026

fix Standardize RGB input to normalized range in hardsubx imgops


Reason for this PR:

  • This PR adds new functionality.
  • This PR fixes a bug that I have personally experienced or that a real user has reported and for which a sample exists.
  • This PR is porting code from C to Rust.

Sanity check:

  • I have read and understood the contributors' guide.
  • I have checked that another pull request for this purpose does not exist.
  • If the PR adds new functionality, I've added it to the changelog.
  • I am NOT adding new C code unless it's to fix an existing, reproducible bug.

Problem

The rgb_to_hsv and rgb_to_lab functions in src/rust/src/hardsubx/imgops.rs assumed inconsistent RGB input formats across callers.

Some inputs were provided in normalized format [0.0, 1.0], while legacy C-side callers used [0, 255], leading to incorrect color conversion results.


Root Cause

The implementation did not consistently handle mixed input formats, causing:

  • incorrect Lab/Hue calculations
  • mismatch between tests and expected outputs

Fix

This PR adds backward-compatible input normalization:

  • If RGB values are <= 1.0, they are treated as normalized input
  • If any value is > 1.0, values are treated as legacy [0–255] and normalized internally
  • Applied consistently to both functions:
    • rgb_to_hsv
    • rgb_to_lab
      This ensures both legacy C callers and Rust callers work correctly.

Repro Steps

cargo test --features hardsubx_ocr imgops::test

Before fix:

  • test_rgb_to_lab fails due to incorrect scaling
test

After fix:

  • All tests pass successfully
pass

Expected behavior

RGB inputs are treated consistently as normalized values in [0.0, 1.0].

Actual behavior (before fix)

Incorrect color conversion due to missing normalization handling.


Impact

  • Fixes incorrect RGB → HSV/Lab conversion in Rust FFI layer
  • Improves robustness for mixed Rust + C usage
  • Maintains backward compatibility with legacy inputs
  • Prevents hidden scaling bugs in production pipelines

@ccextractor-bot
Copy link
Copy Markdown
Collaborator

CCExtractor CI platform finished running the test files on linux. Below is a summary of the test results, when compared to test for commit ad4886e...:
Report Name Tests Passed
Broken 9/13
CEA-708 1/14
DVB 2/7
DVD 3/3
DVR-MS 2/2
General 22/27
Hardsubx 0/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 77/86
Teletext 20/21
WTV 13/13
XDS 31/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 8e8229b88b...
  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...
  • ccextractor --autoprogram --out=ttxt --latin1 99e5eaafdc...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 7aad20907e...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --autoprogram --out=ttxt --latin1 01509e4d27...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --hardsubx 1a0302f7fd...
  • ccextractor --out=spupng c83f765c66...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla 85058ad37e...
  • ccextractor --autoprogram --out=srt --latin1 --ucla b22260d065...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 7f41299cc7...

NOTE: The following tests have been failing on the master branch as well as the PR:

Congratulations: Merging this PR would fix the following tests:

  • ccextractor --autoprogram --out=ttxt --latin1 132d7df7e9..., Last passed: Never
  • ccextractor --autoprogram --out=srt --latin1 b22260d065..., Last passed: Never
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never

It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

@ccextractor-bot
Copy link
Copy Markdown
Collaborator

CCExtractor CI platform finished running the test files on windows. Below is a summary of the test results, when compared to test for commit ad4886e...:
Report Name Tests Passed
Broken 9/13
CEA-708 1/14
DVB 3/7
DVD 3/3
DVR-MS 2/2
General 22/27
Hardsubx 0/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 81/86
Teletext 20/21
WTV 13/13
XDS 31/34

Your PR breaks these cases:

NOTE: The following tests have been failing on the master branch as well as the PR:

Congratulations: Merging this PR would fix the following tests:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65..., Last passed: Never
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b..., Last passed: Never
  • ccextractor --out=spupng c83f765c66..., Last passed: Never
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never

It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

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