VSE: Fix various "off by half a pixel" issues in image transform #116628

Merged
Aras Pranckevicius merged 2 commits from aras_p/blender:vse-fix-filter into main 2024-01-08 16:51:45 +01:00

Code inside IMB_transform (which is pretty much only used inside VSE to do translation/rotation/scale of image or movie strips) was not correctly doing mapping between pixel and texel spaces. This is similar to e.g. GPU rasterization rules and has to do with whether some coordinate refers to pixel/texel "corner" or "center" etc. It's a long topic, but short summary would be:

  • Coordinates refer to pixel/texel corner,
  • Do sampling at pixel centers,
  • Bilinear filter should use floor(x-0.5) and floor(x-0.5)+1 texels.

Also, there was a sign error introduced in Subsampling 3x3 filter, in commit b3fd169259 (shipped in Blender 3.5 and later).

After making the PR, I found out that this seems to fix #90785, #112923 and possibly some others.

Question 1: should IMB_transform get some sort of test coverage to ensure all the sampling mode edge cases work as expected? Maybe similar to the images below, actually.

Question 2: looks like VSE "transform" effect bilinear and bicubic options also have the "result is shifted by half a pixel" issue. Should that also be fixed here, or separately?

Long explanation with lots of images!

All of this is best explained with images. VSE render output is setup to be 192x108 and image strips with very thin single-pixel features. Resulting images scaled up 400% with nearest neighbor filtering, so that things can be seen better. Test input images:

12x6.png 96x54.png 192x108.png 384x216.png

Current behavior in main:

And now the same input images scaled as needed to cover "full frame", also with some cases of rotation. Under the image is pure magenta color strip. 12x6 scaled up to 192x108, using nearest, bilinear, subsampling 3x3 respectively:

Nearest :
shot-12x6-anear-A.png

Bilinear shifted by half the (very enlarged) pixel, magenta background leaks in:
shot-12x6-bilin-A.png

Subsampled almost correct, but a tiny sliver of magenta leaks in top/right:
shot-12x6-subs3-A.png

96x54 scaled up to 192x108:

Nearest :
shot-96x54-anear-A.png

Bilinear again shifted by half a texel:
shot-96x54-bilin-A.png

Subsampled again magenta sliver at top/right:
shot-96x54-subs3-A.png

192x108 image: this needs no scaling, and looks identical between different filter modes, both before and after the PR :

shot-192x108-anear-A.png

384x216 image scaled down to 192x108:

Nearest (misses half of pixel-size features, but by design):
shot-384x216-anear-A.png

Bilinear - this downsamples exactly 2x, so you'd expect it would average each 2x2 pixel block. It does not!
shot-384x216-bilin-A.png

Subsampled - not great, not terrible. Strangely misses any trace of green pixel at top/right edge:
shot-384x216-subs3-A.png

96x54 image, rotated -90 degrees, scaled up to 192x108:

Nearest : introduces diagonal "shift line":
shot-96x54rot90-anear-A.png

Bilinear : magenta background leaks on the left side:
shot-96x54rot90-bilin-A.png

Subsampled slight magenta leak on top; horizontal lines at top/left not fully horizontal:
shot-96x54rot90-subs3-A.png

384x216 image, rotated -90 degrees, scaled down to 192x108:

Nearest magenta leak on left:
shot-384x216rot90-anear-A.png

Bilinear : magenta background leaks on left, also similar to un-rotated, it does not average 2x2 pixels at all:
shot-384x216rot90-bilin-A.png

Subsampled : loses any trace of green outline on top/right:
shot-384x216rot90-subs3-A.png

And here's things after this PR

I'm only including things that are different, and all of them I think are "clearly better".

12x6 scaled up, bilinear: shot-12x6-bilin-C.png

12x6 scaled up, subsampled: shot-12x6-subs3-C.png

96x54 scaled up, bilinear: shot-96x54-bilin-C.png

96x54 scaled up, subsampled: shot-96x54-subs3-C.png

384x216 scaled down, nearest (this now picks different features than before, expected): shot-384x216-anear-C.png

384x216 scaled down, bilinear: shot-384x216-bilin-C.png

384x216 scaled down, subsampled: shot-384x216-subs3-C.png

96x54, rotated -90 degrees, scaled up, nearest: shot-96x54rot90-anear-C.png

96x54, rotated -90 degrees, scaled up, bilinear: shot-96x54rot90-bilin-C.png

96x54, rotated -90 degrees, scaled up, subsampled: shot-96x54rot90-subs3-C.png

384x216, rotated -90 degrees, scaled down, nearest: shot-384x216rot90-anear-C.png

384x216, rotated -90 degrees, scaled down, bilinear: shot-384x216rot90-bilin-C.png

384x216, rotated -90 degrees, scaled down, subsampled: shot-384x216rot90-subs3-C.png

Code inside `IMB_transform` (which is pretty much only used inside VSE to do translation/rotation/scale of image or movie strips) was not correctly doing mapping between pixel and texel spaces. This is similar to e.g. GPU rasterization rules and has to do with whether some coordinate refers to pixel/texel "corner" or "center" etc. It's a long topic, but short summary would be: - Coordinates refer to pixel/texel corner, - Do sampling at pixel centers, - Bilinear filter should use `floor(x-0.5)` and `floor(x-0.5)+1` texels. Also, there was a sign error introduced in Subsampling 3x3 filter, in commit b3fd169259ac (shipped in Blender 3.5 and later). After making the PR, I found out that this seems to fix #90785, #112923 and possibly some others. **Question 1**: should IMB_transform get some sort of test coverage to ensure all the sampling mode edge cases work as expected? Maybe similar to the images below, actually. **Question 2**: looks like VSE "transform" effect bilinear and bicubic options also have the "result is shifted by half a pixel" issue. Should that also be fixed here, or separately? ## Long explanation with lots of images! All of this is best explained with images. VSE render output is setup to be 192x108 and image strips with very thin single-pixel features. Resulting images scaled up 400% with nearest neighbor filtering, so that things can be seen better. Test input images: ![12x6.png](/attachments/4988f328-2d7d-4d75-8fa5-334a1abccebd) ![96x54.png](/attachments/f5290c4a-9400-4791-aff9-c430ab5a6492) ![192x108.png](/attachments/20897a39-e746-4960-89a9-1df353adf878) ![384x216.png](/attachments/221aaefa-3bfc-48e4-b196-28323223326f) ## Current behavior in main: And now the same input images scaled as needed to cover "full frame", also with some cases of rotation. Under the image is pure magenta color strip. **12x6 scaled up to 192x108**, using nearest, bilinear, subsampling 3x3 respectively: Nearest ✅: ![shot-12x6-anear-A.png](/attachments/1d350939-0da5-48b4-a635-98c6d6d6abdb) Bilinear ❌ shifted by half the (very enlarged) pixel, magenta background leaks in: ![shot-12x6-bilin-A.png](/attachments/05d1a5e8-6206-4407-9756-0aa4a0f8ed7a) Subsampled ❌ almost correct, but a tiny sliver of magenta leaks in top/right: ![shot-12x6-subs3-A.png](/attachments/b2b631dd-58b7-4867-adb9-b4bceaf02674) **96x54 scaled up to 192x108**: Nearest ✅: ![shot-96x54-anear-A.png](/attachments/9e492b55-a4fe-4c3b-9f1c-40e5eaec80cf) Bilinear ❌ again shifted by half a texel: ![shot-96x54-bilin-A.png](/attachments/2c7fdec7-98de-485d-8235-7a01266c044c) Subsampled ❌ again magenta sliver at top/right: ![shot-96x54-subs3-A.png](/attachments/69517339-7aaf-45e4-a877-74f41af1ef8d) **192x108 image**: this needs no scaling, and looks identical between different filter modes, both before and after the PR ✅: ![shot-192x108-anear-A.png](/attachments/72de31d9-573e-4c99-9aec-06deecb63104) **384x216 image scaled down to 192x108**: Nearest ✅ (misses half of pixel-size features, but by design): ![shot-384x216-anear-A.png](/attachments/5f8b13ab-67c0-4767-badd-79df905c39a9) Bilinear ❌ - this downsamples exactly 2x, so you'd expect it would average each 2x2 pixel block. It does not! ![shot-384x216-bilin-A.png](/attachments/cadcf0e3-2c68-4957-8b0f-8238c7bcfb8c) Subsampled ❌ - not great, not terrible. Strangely misses any trace of green pixel at top/right edge: ![shot-384x216-subs3-A.png](/attachments/747b9e13-226a-4fa7-8826-6bb885584ad5) **96x54 image, rotated -90 degrees, scaled up to 192x108**: Nearest ❌: introduces diagonal "shift line": ![shot-96x54rot90-anear-A.png](/attachments/fc92a4a0-d08d-4be4-a002-375d999a0304) Bilinear ❌: magenta background leaks on the left side: ![shot-96x54rot90-bilin-A.png](/attachments/0c39de1c-b762-4054-b707-20175dae7ab7) Subsampled ❌ slight magenta leak on top; horizontal lines at top/left not fully horizontal: ![shot-96x54rot90-subs3-A.png](/attachments/abf60c56-f4e4-4ffe-9570-154595dc9d3b) **384x216 image, rotated -90 degrees, scaled down to 192x108**: Nearest ❌ magenta leak on left: ![shot-384x216rot90-anear-A.png](/attachments/4fe10052-a1cd-4a83-b7da-19711cc87308) Bilinear ❌: magenta background leaks on left, also similar to un-rotated, it does not average 2x2 pixels at all: ![shot-384x216rot90-bilin-A.png](/attachments/f9aa53c8-40ad-4f8e-86e9-f10dbb376e32) Subsampled ❌: loses any trace of green outline on top/right: ![shot-384x216rot90-subs3-A.png](/attachments/5d438368-0871-4d6b-9b12-0083350c5395) ### And here's things after this PR I'm only including things that are different, and all of them I think are "clearly better". 12x6 scaled up, bilinear: ![shot-12x6-bilin-C.png](/attachments/1c1c4183-dc0c-4bf5-b585-b8703f310740) 12x6 scaled up, subsampled: ![shot-12x6-subs3-C.png](/attachments/5a50225f-0df7-4eb1-b0f8-381b2e9bec58) 96x54 scaled up, bilinear: ![shot-96x54-bilin-C.png](/attachments/cc2d1017-2257-41f1-84ed-ba5d159420ee) 96x54 scaled up, subsampled: ![shot-96x54-subs3-C.png](/attachments/72c0d022-cc83-428f-9663-5d1fe9e7e4aa) 384x216 scaled down, nearest (this now picks different features than before, expected): ![shot-384x216-anear-C.png](/attachments/5494bae3-ad48-4dd4-8cd6-753b9ad84226) 384x216 scaled down, bilinear: ![shot-384x216-bilin-C.png](/attachments/1e217d13-5ab5-4830-8741-45fdc8289082) 384x216 scaled down, subsampled: ![shot-384x216-subs3-C.png](/attachments/c4c3dab1-5199-4097-b7c9-2555df98a453) 96x54, rotated -90 degrees, scaled up, nearest: ![shot-96x54rot90-anear-C.png](/attachments/13b04284-29c1-481d-a45c-f712cef989c4) 96x54, rotated -90 degrees, scaled up, bilinear: ![shot-96x54rot90-bilin-C.png](/attachments/9a800051-667f-43c4-9122-c77ac0bbd7e0) 96x54, rotated -90 degrees, scaled up, subsampled: ![shot-96x54rot90-subs3-C.png](/attachments/c3395795-9a87-4a36-9a93-54e29d7fd36f) 384x216, rotated -90 degrees, scaled down, nearest: ![shot-384x216rot90-anear-C.png](/attachments/a7225234-9097-448f-94e6-9725ada95883) 384x216, rotated -90 degrees, scaled down, bilinear: ![shot-384x216rot90-bilin-C.png](/attachments/11404dec-ad9a-4773-8546-23b1d59b241d) 384x216, rotated -90 degrees, scaled down, subsampled: ![shot-384x216rot90-subs3-C.png](/attachments/f7676b1d-db0e-4e02-b788-c2a2e0511e75)
Aras Pranckevicius added 2 commits 2023-12-29 12:47:35 +01:00
a4be6be15d ImBuf: simplify TransformUserData init math
Adding X and then subtracting X, or multiplying by Y and then dividing
by Y do kinda cancel out, yo.
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
ba8e0e0ea2
ImBuf: fix off by half a pixel issues in IMB_transform
IMB_transform code was not properly doing mapping between pixel and
texel space. This caused for example bilinear with 0.5 scale
not properly averaging each 2x2 source image pixel, but rather just
picking one out of 2x2 source pixels. The subsampling case setup
code had a sign error too.
Author
Member

@blender-bot build

@blender-bot build
Aras Pranckevicius requested review from Jeroen Bakker 2023-12-29 16:19:17 +01:00
Aras Pranckevicius changed title from WIP: VSE: Fix various "off by half a pixel" issues in image transform to VSE: Fix various "off by half a pixel" issues in image transform 2023-12-29 16:19:26 +01:00
Aras Pranckevicius added this to the Video Sequencer project 2023-12-29 16:19:31 +01:00

This is great! I have spent quite a bit of time on this issue, but never got it quite right :/ I guess there will be still some limitations around image edges, but this is big improvement

This is great! I have spent quite a bit of time on this issue, but never got it quite right :/ I guess there will be still some limitations around image edges, but this is big improvement
Member

When. I refactored IMB_transform I kept the previous behavior. I am fine to mark this to solve a bug. I will leave the code reviewing to Richard for this one. To my knowledge the image editor doesn't use interpolation.

When. I refactored IMB_transform I kept the previous behavior. I am fine to mark this to solve a bug. I will leave the code reviewing to Richard for this one. To my knowledge the image editor doesn't use interpolation.
Jeroen Bakker requested review from Richard Antalik 2024-01-08 15:17:02 +01:00
Jeroen Bakker refused to review 2024-01-08 15:18:52 +01:00
Richard Antalik approved these changes 2024-01-08 16:41:57 +01:00

On topic of tests, I don't see any harm done by including some. I think, that it would make sense to ensure quality of this feature.

For Q2, I am not quite sure what the issue is right now, so would have to leave up to you.

On topic of tests, I don't see any harm done by including some. I think, that it would make sense to ensure quality of this feature. For Q2, I am not quite sure what the issue is right now, so would have to leave up to you.
Author
Member

On topic of tests, I don't see any harm done by including some. I think, that it would make sense to ensure quality of this feature.

Alright, I'll add some tests separately.

For Q2, I am not quite sure what the issue is right now, so would have to leave up to you.

It's kinda the same issue as here, just in different place of the code. Why "strip transform" effect uses a separate code instead of IMB_transform... that's a good question :) I'll fix that later separately.

> On topic of tests, I don't see any harm done by including some. I think, that it would make sense to ensure quality of this feature. Alright, I'll add some tests separately. > For Q2, I am not quite sure what the issue is right now, so would have to leave up to you. It's kinda the same issue as here, just in different place of the code. Why "strip transform" effect uses a separate code instead of IMB_transform... that's a good question :) I'll fix that later separately.
Aras Pranckevicius merged commit 877d9c596a into main 2024-01-08 16:51:45 +01:00
Aras Pranckevicius deleted branch vse-fix-filter 2024-01-08 16:51:49 +01:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#116628
No description provided.