Skip to content

Commit

Permalink
Move fix_line_brightness to borders submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
emotion3459 committed Sep 14, 2024
1 parent 62dcd9a commit 90c61ea
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 56 deletions.
63 changes: 61 additions & 2 deletions vsadjust/borders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

from itertools import chain
from typing import Sequence
from vstools import CustomEnum, CustomValueError, FunctionUtil, KwargsT, NotFoundEnumValue, PlanesT, core, vs

from vsmasktools import rekt_partial
from vstools import (
CustomEnum, CustomValueError, FunctionUtil, KwargsT, NotFoundEnumValue,
PlanesT, ColorRange, get_peak_value, get_lowest_value, core, vs
)

from .levels import fix_levels

__all__ = [
'bore'
'bore',
'fix_line_brightness'
]


Expand Down Expand Up @@ -75,3 +83,54 @@ def __call__(
proc_clip = plugin(proc_clip, *plane_values, plane=plane, **kwargs)

return func.return_clip(proc_clip)


def fix_line_brightness(
clip: vs.VideoNode,
rows: dict[int, float] = {},
columns: dict[int, float] = {},
) -> vs.VideoNode:
"""
Fix darkened or brightened luma rows or columns using manual level adjustments.
Adjustments are fix_levels calls where max/min in is moved by adjustment / 100 * peak - low.
As such, adjustment values must lie in (-100, 100).
:param clip: The clip to process.
:param rows: Rows and their adjustment values. Rows < 0 are counted from the bottom.
:param columns: Columns and their adjustment values. Columns < 0 are counted from the left.
:return: Clip with adjusted rows and columns.
"""
func = FunctionUtil(clip, fix_line_brightness, 0, vs.GRAY, 32)

fix = func.work_clip
color_range = ColorRange.from_video(fix)
peak = get_peak_value(fix, False, color_range)
low = get_lowest_value(fix, False, color_range)
low_to_peak = peak - low

def _fix_line(clip: vs.VideoNode, is_row: bool, num: int, adjustment: float) -> vs.VideoNode:
if 100 < adjustment < -100:
raise ValueError("fix_line_brightness: adjustment values must be in (-100, 100)")

if adjustment > 0:
adj = lambda c: fix_levels(c, max_in=peak - low_to_peak * adjustment / 100, max_out=peak) # noqa: E731
elif adjustment < 0:
adj = lambda c: fix_levels(c, min_in=low + low_to_peak * adjustment / 100, min_out=low) # noqa: E731
else:
return clip
if is_row:
return rekt_partial(clip, top=num, bottom=clip.height - num - 1, func=adj)
return rekt_partial(clip, left=num, right=clip.width - num - 1, func=adj)

for row, adj in rows.items():
if row < 0:
row += clip.height
fix = _fix_line(fix, True, row, adj)
for col, adj in columns.items():
if col < 0:
col += clip.width
fix = _fix_line(fix, False, col, adj)

return func.return_clip(fix)
56 changes: 2 additions & 54 deletions vsadjust/levels.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Sequence

from vsmasktools import rekt_partial
from vstools import (
ColorRange, ColorRangeT, DitherType, FunctionUtil, PlanesT, depth, get_lowest_value, get_lowest_values,
get_peak_value, get_peak_values, normalize_seq, scale_value, vs
ColorRange, ColorRangeT, DitherType, FunctionUtil, PlanesT, depth,
get_lowest_values, get_peak_values, normalize_seq, scale_value, vs
)

__all__ = [
Expand Down Expand Up @@ -91,54 +90,3 @@ def fix_double_range(clip: vs.VideoNode) -> vs.VideoNode:
)

return ColorRange.LIMITED.apply(fix)


def fix_line_brightness(
clip: vs.VideoNode,
rows: dict[int, float] = {},
columns: dict[int, float] = {},
) -> vs.VideoNode:
"""
Fix darkened or brightened luma rows or columns using manual level adjustments.
Adjustments are fix_levels calls where max/min in is moved by adjustment / 100 * peak - low.
As such, adjustment values must lie in (-100, 100).
:param clip: The clip to process.
:param rows: Rows and their adjustment values. Rows < 0 are counted from the bottom.
:param columns: Columns and their adjustment values. Columns < 0 are counted from the left.
:return: Clip with adjusted rows and columns.
"""
func = FunctionUtil(clip, fix_line_brightness, 0, vs.GRAY, 32)

fix = func.work_clip
color_range = ColorRange.from_video(fix)
peak = get_peak_value(fix, False, color_range)
low = get_lowest_value(fix, False, color_range)
low_to_peak = peak - low

def _fix_line(clip: vs.VideoNode, is_row: bool, num: int, adjustment: float) -> vs.VideoNode:
if 100 < adjustment < -100:
raise ValueError("fix_line_brightness: adjustment values must be in (-100, 100)")

if adjustment > 0:
adj = lambda c: fix_levels(c, max_in=peak - low_to_peak * adjustment / 100, max_out=peak) # noqa: E731
elif adjustment < 0:
adj = lambda c: fix_levels(c, min_in=low + low_to_peak * adjustment / 100, min_out=low) # noqa: E731
else:
return clip
if is_row:
return rekt_partial(clip, top=num, bottom=clip.height - num - 1, func=adj)
return rekt_partial(clip, left=num, right=clip.width - num - 1, func=adj)

for row, adj in rows.items():
if row < 0:
row += clip.height
fix = _fix_line(fix, True, row, adj)
for col, adj in columns.items():
if col < 0:
col += clip.width
fix = _fix_line(fix, False, col, adj)

return func.return_clip(fix)

0 comments on commit 90c61ea

Please sign in to comment.