refactor
This commit is contained in:
parent
2d6a6bd3a1
commit
d80e723b94
28 changed files with 1801 additions and 352 deletions
|
|
@ -10,23 +10,30 @@ class DiffCropHandler
|
|||
{
|
||||
private Bitmap? _referenceFrame;
|
||||
private Region? _referenceRegion;
|
||||
private readonly object _refLock = new();
|
||||
|
||||
public void HandleSnapshot(string? file = null, Region? region = null)
|
||||
{
|
||||
_referenceFrame?.Dispose();
|
||||
_referenceFrame = ScreenCapture.CaptureOrLoad(file, region);
|
||||
_referenceRegion = region;
|
||||
var newFrame = ScreenCapture.CaptureOrLoad(file, region);
|
||||
lock (_refLock)
|
||||
{
|
||||
_referenceFrame?.Dispose();
|
||||
_referenceFrame = newFrame;
|
||||
_referenceRegion = region;
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleScreenshot(string path, Region? region = null)
|
||||
{
|
||||
var bitmap = _referenceFrame ?? ScreenCapture.CaptureOrLoad(null, region);
|
||||
Bitmap? refCopy;
|
||||
lock (_refLock) { refCopy = _referenceFrame != null ? (Bitmap)_referenceFrame.Clone() : null; }
|
||||
var bitmap = refCopy ?? ScreenCapture.CaptureOrLoad(null, region);
|
||||
var format = ImageUtils.GetImageFormat(path);
|
||||
var dir = Path.GetDirectoryName(path);
|
||||
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
bitmap.Save(path, format);
|
||||
if (bitmap != _referenceFrame) bitmap.Dispose();
|
||||
bitmap.Dispose();
|
||||
}
|
||||
|
||||
public byte[] HandleCapture(Region? region = null)
|
||||
|
|
@ -44,47 +51,54 @@ class DiffCropHandler
|
|||
public (Bitmap cropped, Bitmap refCropped, Bitmap current, Region region)? DiffCrop(
|
||||
DiffCropParams c, string? file = null, Region? region = null)
|
||||
{
|
||||
if (_referenceFrame == null)
|
||||
return null;
|
||||
Bitmap refSnapshot;
|
||||
Region? refRegion;
|
||||
lock (_refLock)
|
||||
{
|
||||
if (_referenceFrame == null)
|
||||
return null;
|
||||
refSnapshot = (Bitmap)_referenceFrame.Clone();
|
||||
refRegion = _referenceRegion;
|
||||
}
|
||||
|
||||
var diffRegion = region ?? _referenceRegion;
|
||||
var diffRegion = region ?? refRegion;
|
||||
int baseX = diffRegion?.X ?? 0;
|
||||
int baseY = diffRegion?.Y ?? 0;
|
||||
var current = ScreenCapture.CaptureOrLoad(file, diffRegion);
|
||||
|
||||
Bitmap refForDiff = _referenceFrame;
|
||||
bool disposeRef = false;
|
||||
Bitmap refForDiff = refSnapshot;
|
||||
|
||||
if (diffRegion != null)
|
||||
{
|
||||
if (_referenceRegion == null)
|
||||
if (refRegion == null)
|
||||
{
|
||||
var croppedRef = CropBitmap(_referenceFrame, diffRegion);
|
||||
var croppedRef = CropBitmap(refSnapshot, diffRegion);
|
||||
if (croppedRef == null)
|
||||
{
|
||||
current.Dispose();
|
||||
refSnapshot.Dispose();
|
||||
return null;
|
||||
}
|
||||
refForDiff = croppedRef;
|
||||
disposeRef = true;
|
||||
}
|
||||
else if (!RegionsEqual(diffRegion, _referenceRegion))
|
||||
else if (!RegionsEqual(diffRegion, refRegion))
|
||||
{
|
||||
int offX = diffRegion.X - _referenceRegion.X;
|
||||
int offY = diffRegion.Y - _referenceRegion.Y;
|
||||
if (offX < 0 || offY < 0 || offX + diffRegion.Width > _referenceFrame.Width || offY + diffRegion.Height > _referenceFrame.Height)
|
||||
int offX = diffRegion.X - refRegion.X;
|
||||
int offY = diffRegion.Y - refRegion.Y;
|
||||
if (offX < 0 || offY < 0 || offX + diffRegion.Width > refSnapshot.Width || offY + diffRegion.Height > refSnapshot.Height)
|
||||
{
|
||||
current.Dispose();
|
||||
refSnapshot.Dispose();
|
||||
return null;
|
||||
}
|
||||
var croppedRef = CropBitmap(_referenceFrame, new Region(offX, offY, diffRegion.Width, diffRegion.Height));
|
||||
var croppedRef = CropBitmap(refSnapshot, new Region(offX, offY, diffRegion.Width, diffRegion.Height));
|
||||
if (croppedRef == null)
|
||||
{
|
||||
current.Dispose();
|
||||
refSnapshot.Dispose();
|
||||
return null;
|
||||
}
|
||||
refForDiff = croppedRef;
|
||||
disposeRef = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -126,7 +140,8 @@ class DiffCropHandler
|
|||
if (totalChanged == 0)
|
||||
{
|
||||
current.Dispose();
|
||||
if (disposeRef) refForDiff.Dispose();
|
||||
if (refForDiff != refSnapshot) refForDiff.Dispose();
|
||||
refSnapshot.Dispose();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -226,7 +241,8 @@ class DiffCropHandler
|
|||
{
|
||||
Log.Debug("diff-crop: no tooltip-sized region found");
|
||||
current.Dispose();
|
||||
if (disposeRef) refForDiff.Dispose();
|
||||
if (refForDiff != refSnapshot) refForDiff.Dispose();
|
||||
refSnapshot.Dispose();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -307,7 +323,8 @@ class DiffCropHandler
|
|||
|
||||
Log.Debug("diff-crop: tooltip region ({X},{Y}) {W}x{H}", minX, minY, rw, rh);
|
||||
|
||||
if (disposeRef) refForDiff.Dispose();
|
||||
if (refForDiff != refSnapshot) refForDiff.Dispose();
|
||||
refSnapshot.Dispose();
|
||||
return (cropped, refCropped, current, resultRegion);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue