Skip to content

Fix alpha-channel check in IsImageOnlyTransparent and CopyRectangle rect math#12579

Merged
niksedk merged 4 commits into
SubtitleEdit:mainfrom
ivandrofly:fix/bitmap-latent-fixes
Jul 18, 2026
Merged

Fix alpha-channel check in IsImageOnlyTransparent and CopyRectangle rect math#12579
niksedk merged 4 commits into
SubtitleEdit:mainfrom
ivandrofly:fix/bitmap-latent-fixes

Conversation

@ivandrofly

@ivandrofly ivandrofly commented Jul 17, 2026

Copy link
Copy Markdown
Member

Verified fixes for latent bitmap bugs, plus tests pinning them:

  • NikseBitmap.IsImageOnlyTransparent tested the blue byte instead of alpha (_bitmapData[i] in BGRA data) — an opaque blue-less image (black/yellow/red/green text) was reported as fully transparent, dropping line parts in the OCR image splitter. The identical bug in the OCR duplicate NikseBitmap2 is fixed in the same commit.
  • NikseBitmap.CopyRectangle(SKRect) passed width/height into SKRectI's (left, top, right, bottom) constructor — correct only when the section starts at the origin. The clamping statements in the SKRectI overload had the same Rectangle(x, y, w, h)SKRectI(l, t, r, b) porting bug and are fixed as a follow-up commit, matching the SE 4 original's clamp-to-bitmap intent.
  • New unit tests pin the alpha-vs-blue regression (verified to fail against the old code) and the CopyRectangle offset/clamping math, for both NikseBitmap and NikseBitmap2.

The two FixInvalidItalicTags commits from the original version of this PR were dropped after review — see comment below.

All 508 libse and 14 libuilogic tests pass.

🤖 Generated with Claude Code

@niksedk
niksedk requested a review from Copilot July 18, 2026 07:25

Copilot AI commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

@niksedk Unfortunately, I hit an error while trying to use the custom Copilot setup steps configured for this repository and had to close this PR. The error I am seeing is:

no `copilot-setup-steps` job found in your `copilot-setup-steps.yml` workflow file. Please ensure you have a single job named `copilot-setup-steps`. For more details, see https://gh.io/copilot/actions-setup-steps

Once you or someone with the necessary access fixes the problem, please unassign and then reassign issue #0 to me and I'll retry. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes several small but correctness-impacting issues in bitmap handling and HTML italic tag cleanup, improving OCR/image processing reliability and tag normalization.

Changes:

  • Fix alpha-channel detection in IsImageOnlyTransparent for both NikseBitmap and the OCR copy NikseBitmap2.
  • Correct CopyRectangle(SKRect) to construct SKRectI using Right/Bottom rather than Width/Height.
  • Fix duplicated EndsWith(...) conditions in HtmlUtil.FixInvalidItalicTags for 2-line edge cases.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libuilogic/Ocr/NikseBitmap2.cs Fix alpha-byte indexing when checking transparency.
src/libse/Common/NikseBitmap.cs Fix CopyRectangle(SKRect) rect construction and alpha-byte indexing for transparency checks.
src/libse/Common/HtmlUtil.cs Fix incorrect line index usage in italic tag repair conditions.

Comment on lines +1181 to 1182
var rect = new SKRectI((int)section.Left, (int)section.Top, (int)section.Right, (int)section.Bottom);
return CopyRectangle(rect);
@niksedk

niksedk commented Jul 18, 2026

Copy link
Copy Markdown
Member

AI-generated bug fix pull requests

For future AI-generated bug fix PRs, please limit each pull request to 1–3 related fixes (depending on their size and complexity). Smaller, focused PRs are much easier to review, test, and merge.

Please also include a brief manual comment describing how you personally verified each fix. AI-generated code can contain mistakes or unintended side effects, so every submitted change should be tested and validated by the contributor before opening the PR.

ivandrofly and others added 4 commits July 18, 2026 10:59
The loop tested _bitmapData[i] (blue in BGRA) instead of the alpha
byte at i+3, so an opaque all-black image was reported as fully
transparent. Fix both the libse and OCR-duplicate implementations.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The SKRectI constructor takes (left, top, right, bottom), but the
overload passed Width/Height as the right/bottom coordinates, which
is only correct when Left and Top are both 0.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The clamp statements had the same Rectangle(x, y, width, height) to
SKRectI(left, top, right, bottom) porting bug as the SKRect overload:
they passed width/height values as right/bottom. Clamp bottom to Height
and right to Width directly, matching the SE 4 original's intent.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Pin the alpha-vs-blue regression (opaque black must not be "only
transparent") for both NikseBitmap and the OCR NikseBitmap2, and the
CopyRectangle offset/clamping rect math.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@niksedk
niksedk force-pushed the fix/bitmap-latent-fixes branch from 96b431b to b42230d Compare July 18, 2026 09:02
@niksedk niksedk changed the title Fix alpha-channel check in IsImageOnlyTransparent, CopyRectangle rect math, and FixInvalidItalicTags conditions Fix alpha-channel check in IsImageOnlyTransparent and CopyRectangle rect math Jul 18, 2026
@niksedk

niksedk commented Jul 18, 2026

Copy link
Copy Markdown
Member

Thanks for the PR! The two bitmap fixes are verified and merged (with the SKRectI clamp statements completed, since they had the same (x, y, w, h)(l, t, r, b) porting issue, plus regression tests).

The two FixInvalidItalicTags commits were dropped after careful review:

  1. The "duplicated" lines[1].EndsWith condition is identical in SE 4 (se4-legacy branch) — it's decade-old shipped behavior, not an SE 5 porting typo.
  2. Behavioral testing showed the change is a regression: the loose condition is load-bearing. Inputs like </i>Hello\n</i>a</i>World</i> or <i>Hello<i>x\n<i>World<i> are repaired to valid italics today (<i>Hello</i> + <i>aWorld</i>), but with the tightened condition they fall through unfixed — the function returns the invalid tags unchanged, as there is no downstream fallback for these shapes.

🤖 Generated with Claude Code

@niksedk
niksedk merged commit 92f879d into SubtitleEdit:main Jul 18, 2026
1 of 2 checks passed
niksedk added a commit to ivandrofly/subtitleedit that referenced this pull request Jul 18, 2026
…aired

The two-line branches for "only four </i> tags" / "only four <i> tags"
had an asymmetric condition (lines[1].EndsWith checked twice, lines[0]'s
end never) - identical in SE 4, and load-bearing: shapes it matched were
repaired to whole-line italics, but near-miss shapes (e.g. second line
not starting with the tag, or all four tags on the first line) fell
through and the function returned the invalid tags unchanged.

Rather than tightening the condition (which would repair fewer shapes -
see PR SubtitleEdit#12579 review), generalize it: under the count guard the stray
tags carry no pairing information, so strip them from each line that
contains one and wrap that line in proper <i>...</i>; tag-less lines are
kept as-is. Output is byte-identical for every previously matched shape,
and the fall-through shapes now come out as valid italics.

Tests pin both the long-standing shapes and the newly repaired ones.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ivandrofly
ivandrofly deleted the fix/bitmap-latent-fixes branch July 18, 2026 09:55
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.

4 participants