Skip to content

Commit

Permalink
Add fix_line_brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
Setsugennoao authored Apr 10, 2024
2 parents 3e05f39 + 6fce10f commit 35d24ca
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions vsadjust/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,54 @@ 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)
elif adjustment < 0:
adj = lambda c: fix_levels(c, min_in=low + low_to_peak * adjustment / 100, min_out=low)
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 35d24ca

Please sign in to comment.