Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions PhotoLocator/BitmapOperations/CombineFramesOperationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public bool Supports16BitResult()
return _pixelFormat == PixelFormats.Rgb24 || _pixelFormat == PixelFormats.Bgr24 || _pixelFormat == PixelFormats.Gray8;
}

public virtual bool IsResultReady => _accumulatorPixels is not null;

public BitmapSource GetResult8()
{
if (_accumulatorPixels is null)
Expand Down
37 changes: 37 additions & 0 deletions PhotoLocator/BitmapOperations/TimeCompressionAverageOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading;

namespace PhotoLocator.BitmapOperations
{
sealed class TimeCompressionAverageOperation : CombineFramesOperationBase
{
readonly int _numberOfFramesToAverage;
int _collectedFrames;

public TimeCompressionAverageOperation(int numberOfFramesToAverage, string? darkFramePath, CombineFramesRegistration? registrationSettings, CancellationToken ct)
: base(darkFramePath, registrationSettings?.ToCombineFramesRegistrationBase(RegistrationOperation.Borders.Mirror), ct)
{
_numberOfFramesToAverage = numberOfFramesToAverage;
}
Comment on lines +11 to +15
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.

⚠️ Potential issue | 🔴 Critical

Validate numberOfFramesToAverage parameter.

The constructor doesn't validate that numberOfFramesToAverage is positive. If it's zero or negative, this will cause issues:

  • Zero would cause division by zero in GetResultScaling() (line 34)
  • Zero or negative values would break the readiness logic in IsResultReady (line 17)
🔎 Proposed fix
 public TimeCompressionAverageOperation(int numberOfFramesToAverage, string? darkFramePath, CombineFramesRegistration? registrationSettings, CancellationToken ct) 
     : base(darkFramePath, registrationSettings?.ToCombineFramesRegistrationBase(RegistrationOperation.Borders.Mirror), ct)
 {
+    if (numberOfFramesToAverage <= 0)
+        throw new ArgumentOutOfRangeException(nameof(numberOfFramesToAverage), "Must be greater than zero");
     _numberOfFramesToAverage = numberOfFramesToAverage;
 }
🤖 Prompt for AI Agents
In PhotoLocator/BitmapOperations/TimeCompressionAverageOperation.cs around lines
11 to 15, the constructor does not validate numberOfFramesToAverage allowing
zero or negative values which will lead to division by zero and incorrect
readiness logic; fix by validating that numberOfFramesToAverage is > 0 at the
start of the constructor and throw an ArgumentOutOfRangeException (or
ArgumentException) with the parameter name if the check fails, and only assign
_numberOfFramesToAverage after the validation.


public override bool IsResultReady => _collectedFrames == _numberOfFramesToAverage;

public override void ProcessImage(System.Windows.Media.Imaging.BitmapSource image)
{
if (_collectedFrames == _numberOfFramesToAverage)
{
_collectedFrames = 0;
Array.Clear(_accumulatorPixels!);
}
var pixels = PrepareFrame(image);
for (int i = 0; i < pixels.Length; i++)
_accumulatorPixels![i] += pixels[i];
_collectedFrames++;
}

protected override double GetResultScaling()
{
return 1.0 / _numberOfFramesToAverage;
}
}
}
26 changes: 20 additions & 6 deletions PhotoLocator/VideoTransformCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public ComboBoxItem SelectedEffect
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EffectStrength)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsParameterizedEffect)));
UpdateStabilizeArgs();
UpdateProcessArgs();
}
}
Expand Down Expand Up @@ -395,6 +396,7 @@ public CombineFramesMode CombineFramesMode
var isTimeSlice = field is CombineFramesMode.TimeSlice or CombineFramesMode.TimeSliceInterpolated;
if (wasTimeSlice != isTimeSlice)
CombineFramesCount = isTimeSlice ? 1 : DefaultAverageFramesCount;
UpdateProcessArgs();
UpdateOutputArgs();
}
}
Expand All @@ -403,7 +405,11 @@ public CombineFramesMode CombineFramesMode
public int CombineFramesCount
{
get;
set => SetProperty(ref field, Math.Max(1, value));
set
{
if (SetProperty(ref field, Math.Max(1, value)))
UpdateProcessArgs();
}
} = DefaultAverageFramesCount;

public string NumberOfFramesHint => CombineFramesMode < CombineFramesMode.TimeSlice ? "Number of frames to combine" : "Time slice video loops";
Expand Down Expand Up @@ -661,7 +667,7 @@ private void UpdateStabilizeArgs()
var filters = new List<string>();
if (IsCropChecked)
filters.Add($"crop={CropWindow}");
if (IsScaleChecked)
if (IsScaleChecked && SelectedEffect.Content != ZoomEffect)
filters.Add($"scale={ScaleTo}");
filters.Add($"vidstabdetect=shakiness=7{(IsTripodChecked ? ":tripod=1" : "")}:result={VideoTransformCommands.TransformsFileName}");
StabilizeArguments = $"-vf \"{string.Join(", ", filters)}\" -f null -";
Expand Down Expand Up @@ -692,9 +698,9 @@ private void UpdateProcessArgs()
EffectStrength,
IsScaleChecked ? ScaleTo.Replace(':', 'x') : "1920x1080",
string.IsNullOrEmpty(FrameRate) ? "30" : FrameRate));
if (IsSpeedupChecked)
if (IsSpeedupChecked && (CombineFramesMode != CombineFramesMode.RollingAverage || !SpeedupByEqualsCombineFramesCount))
filters.Add($"setpts=PTS/({SpeedupBy})");
if (!string.IsNullOrEmpty(FrameRate))
if (!string.IsNullOrEmpty(FrameRate) && SelectedEffect.Content != ZoomEffect)
filters.Add($"fps={FrameRate}");
if (HasOnlyImageInput)
filters.Add("colorspace=all=bt709:iall=bt601-6-625:fast=1");
Expand Down Expand Up @@ -1080,7 +1086,9 @@ async Task RunLocalFrameProcessingAsync(string outFileName, CancellationToken ct
}
using CombineFramesOperationBase? runningAverage = CombineFramesMode switch
{
CombineFramesMode.RollingAverage => new RollingAverageOperation(CombineFramesCount, DarkFramePath, ParseRegistrationSettings(), ct),
CombineFramesMode.RollingAverage => IsSpeedupChecked && SpeedupByEqualsCombineFramesCount ?
new TimeCompressionAverageOperation(CombineFramesCount, DarkFramePath, ParseRegistrationSettings(), ct) :
new RollingAverageOperation(CombineFramesCount, DarkFramePath, ParseRegistrationSettings(), ct),
CombineFramesMode.FadingAverage => new FadingAverageOperation(CombineFramesCount, DarkFramePath, ParseRegistrationSettings(), ct),
CombineFramesMode.FadingMax => new FadingMaxOperation(CombineFramesCount, DarkFramePath, ParseRegistrationSettings(), ct),
_ => null,
Expand All @@ -1093,6 +1101,8 @@ async Task RunLocalFrameProcessingAsync(string outFileName, CancellationToken ct
if (runningAverage is not null)
{
runningAverage.ProcessImage(source);
if (!runningAverage.IsResultReady)
return;
if (_localContrastSetup is null || _localContrastSetup.IsNoOperation)
{
frameEnumerator.AddItem(runningAverage.GetResult8());
Expand All @@ -1112,6 +1122,8 @@ async Task RunLocalFrameProcessingAsync(string outFileName, CancellationToken ct
await writeTask.ConfigureAwait(false);
}

bool SpeedupByEqualsCombineFramesCount => int.TryParse(SpeedupBy, CultureInfo.InvariantCulture, out var speedupBy) && speedupBy == CombineFramesCount;

SaveFileDialog SetupSaveFileDialog(PictureItemViewModel[] allSelected, string inPath)
{
string postfix, ext;
Expand All @@ -1127,7 +1139,9 @@ SaveFileDialog SetupSaveFileDialog(PictureItemViewModel[] allSelected, string in
postfix = "fadeavg" + CombineFramesCount;
else if (CombineFramesMode == CombineFramesMode.FadingMax && CombineFramesCount > 1)
postfix = "fademax" + CombineFramesCount;
else if (IsStabilizeChecked || RegistrationMode > RegistrationMode.Off && CombineFramesMode > CombineFramesMode.None)
else if (IsStabilizeChecked)
postfix = "stabilized" + SmoothFrames;
else if (RegistrationMode > RegistrationMode.Off && CombineFramesMode > CombineFramesMode.None)
postfix = "stabilized";
else if (allSelected.Length > 1)
postfix = "combined";
Expand Down
Loading