naps2/NAPS2.Sdk/Images/DeskewOperation.cs
Ben Olden-Cooligan cbcfd2de82 Remove render extensions in favor of using ImageContext
This should be okay in general and allows us to inject an IPdfRenderer for rendering PDF-based images.
2022-06-14 22:16:35 -07:00

64 lines
2.0 KiB
C#

using NAPS2.Images.Gdi;
namespace NAPS2.Images;
public class DeskewOperation : OperationBase
{
private readonly ImageContext _imageContext;
public DeskewOperation(ImageContext imageContext)
{
_imageContext = imageContext;
AllowCancel = true;
AllowBackground = true;
}
public bool Start(ICollection<UiImage> images, DeskewParams deskewParams)
{
ProgressTitle = MiscResources.AutoDeskewProgress;
Status = new OperationStatus
{
StatusText = MiscResources.AutoDeskewing,
MaxProgress = images.Count
};
RunAsync(async () =>
{
return await Pipeline.For(images, CancelToken).RunParallel(async img =>
{
using var processedImage = img.GetClonedImage();
var image = _imageContext.Render(processedImage);
try
{
CancelToken.ThrowIfCancellationRequested();
var transform = Deskewer.GetDeskewTransform(image);
CancelToken.ThrowIfCancellationRequested();
image = _imageContext.PerformTransform(image, transform);
var thumbnail = deskewParams.ThumbnailSize.HasValue
? _imageContext.PerformTransform(image, new ThumbnailTransform(deskewParams.ThumbnailSize.Value))
: null;
lock (img)
{
img.AddTransform(transform);
if (thumbnail != null)
{
img.SetThumbnail(thumbnail);
}
}
lock (this)
{
Status.CurrentProgress += 1;
}
InvokeStatusChanged();
}
finally
{
image.Dispose();
}
});
});
return true;
}
}