Fix invalid thumbnail size handling

This commit is contained in:
Ben Olden-Cooligan 2022-07-08 17:15:01 -07:00
parent bd927ee509
commit 749d80e7dc
12 changed files with 35 additions and 16 deletions

View File

@ -96,7 +96,10 @@ public static class AppTestHelper
public static void AssertNoErrorLog(string appData)
{
var path = Path.Combine(appData, "NAPS2", "errorlog.txt");
Assert.False(File.Exists(path), File.ReadAllText(path));
if (File.Exists(path))
{
Assert.False(File.Exists(path), File.ReadAllText(path));
}
}
public static void AssertErrorLog(string appData)

View File

@ -37,4 +37,14 @@ public static class ConfigExtensions
}
return config.DefaultOcrParams();
}
public static int ThumbnailSize(this Naps2Config config)
{
var size = config.Get(c => c.ThumbnailSize);
if (size == 0)
{
return ThumbnailSizes.DEFAULT_SIZE;
}
return ThumbnailSizes.Validate(size);
}
}

View File

@ -87,6 +87,7 @@ public class ConfigSerializer : VersionedSerializer<ConfigStorage<CommonConfig>>
private ConfigStorage<CommonConfig> AppConfigV0ToCommonConfigDefault(AppConfigV0 c) =>
new(new CommonConfig
{
// TODO: This initializes all properties, we need to specify exactly what's set
// Could maybe move the app-only properties to Locked scope, but it really shouldn't matter
Version = CommonConfig.CURRENT_VERSION,
Culture = c.DefaultCulture,

View File

@ -52,7 +52,7 @@ public static class InternalDefaults
OcrMode = LocalizedOcrMode.Default,
OcrAfterScanning = true,
LastImageExt = "",
ThumbnailSize = 128,
ThumbnailSize = ThumbnailSizes.DEFAULT_SIZE,
DesktopToolStripDock = DockStyle.Top,
EventLogging = EventType.None,
PdfSettings = new PdfSettings

View File

@ -219,7 +219,7 @@ public class ProfilesForm : EtoDialogBase
{
NoAutoSave = Config.Get(c => c.DisableAutoSave),
OcrParams = Config.OcrAfterScanningParams(),
ThumbnailSize = Config.Get(c => c.ThumbnailSize)
ThumbnailSize = Config.ThumbnailSize()
};
private void ContextMenuOpening(object sender, EventArgs e)

View File

@ -93,7 +93,7 @@ public class BatchScanPerformer : IBatchScanPerformer
? _config.DefaultOcrParams()
: OcrParams.Empty,
OcrCancelToken = CancelToken,
ThumbnailSize = _config.Get(c => c.ThumbnailSize)
ThumbnailSize = _config.ThumbnailSize()
};
try

View File

@ -276,7 +276,7 @@ public class DesktopController
// Allow scanned images to be recovered in case of an unexpected close
var op = _operationFactory.Create<RecoveryOperation>();
if (op.Start(_desktopImagesController.ReceiveScannedImage(),
new RecoveryParams { ThumbnailSize = _config.Get(c => c.ThumbnailSize) }))
new RecoveryParams { ThumbnailSize = _config.ThumbnailSize() }))
{
_operationProgress.ShowProgress(op);
}
@ -284,7 +284,7 @@ public class DesktopController
private void InitThumbnailRendering()
{
_thumbnailRenderQueue.SetThumbnailSize(_config.Get(c => c.ThumbnailSize));
_thumbnailRenderQueue.SetThumbnailSize(_config.ThumbnailSize());
_thumbnailRenderQueue.StartRendering(_imageList);
}
@ -292,7 +292,7 @@ public class DesktopController
{
var op = _operationFactory.Create<ImportOperation>();
if (op.Start(OrderFiles(files), _desktopImagesController.ReceiveScannedImage(),
new ImportParams { ThumbnailSize = _config.Get(c => c.ThumbnailSize) }))
new ImportParams { ThumbnailSize = _config.ThumbnailSize() }))
{
_operationProgress.ShowProgress(op);
}
@ -310,7 +310,7 @@ public class DesktopController
{
var op = _operationFactory.Create<DirectImportOperation>();
if (op.Start(data, copy, _desktopImagesController.ReceiveScannedImage(),
new DirectImportParams { ThumbnailSize = _config.Get(c => c.ThumbnailSize) }))
new DirectImportParams { ThumbnailSize = _config.ThumbnailSize() }))
{
_operationProgress.ShowProgress(op);
}

View File

@ -31,7 +31,7 @@ public class DesktopScanController : IDesktopScanController
{
NoAutoSave = _config.Get(c => c.DisableAutoSave),
OcrParams = _config.OcrAfterScanningParams(),
ThumbnailSize = _config.Get(c => c.ThumbnailSize)
ThumbnailSize = _config.ThumbnailSize()
};
public async Task ScanWithDevice(string deviceID)

View File

@ -129,7 +129,7 @@ public partial class FDesktop : FormBase
WinFormsHacks.SetControlStyle(panel, ControlStyles.Selectable, true);
}
_imageList.ThumbnailRenderer = _thumbnailRenderer;
int thumbnailSize = Config.Get(c => c.ThumbnailSize);
int thumbnailSize = Config.ThumbnailSize();
_listView.ImageSize = thumbnailSize;
SetThumbnailSpacing(thumbnailSize);
@ -275,8 +275,8 @@ public partial class FDesktop : FormBase
ctxSelectAll.Enabled = _imageList.Images.Any();
// Other
btnZoomIn.Enabled = _imageList.Images.Any() && Config.Get(c => c.ThumbnailSize) < ThumbnailSizes.MAX_SIZE;
btnZoomOut.Enabled = _imageList.Images.Any() && Config.Get(c => c.ThumbnailSize) > ThumbnailSizes.MIN_SIZE;
btnZoomIn.Enabled = _imageList.Images.Any() && Config.ThumbnailSize() < ThumbnailSizes.MAX_SIZE;
btnZoomOut.Enabled = _imageList.Images.Any() && Config.ThumbnailSize() > ThumbnailSizes.MIN_SIZE;
tsNewProfile.Enabled =
!(Config.Get(c => c.NoUserProfiles) && _profileManager.Profiles.Any(x => x.IsLocked));
@ -694,10 +694,10 @@ public partial class FDesktop : FormBase
private void StepThumbnailSize(double step)
{
int thumbnailSize = Config.Get(c => c.ThumbnailSize);
int thumbnailSize = Config.ThumbnailSize();
thumbnailSize =
(int) ThumbnailSizes.StepNumberToSize(ThumbnailSizes.SizeToStepNumber(thumbnailSize) + step);
thumbnailSize = Math.Max(Math.Min(thumbnailSize, ThumbnailSizes.MAX_SIZE), ThumbnailSizes.MIN_SIZE);
thumbnailSize = ThumbnailSizes.Validate(thumbnailSize);
Config.User.Set(c => c.ThumbnailSize, thumbnailSize);
ResizeThumbnails(thumbnailSize);
}

View File

@ -448,7 +448,7 @@ public class FViewer : FormBase
private async void tsDeskew_Click(object sender, EventArgs e)
{
var op = _operationFactory.Create<DeskewOperation>();
if (op.Start(new[] { CurrentImage }, new DeskewParams { ThumbnailSize = Config.Get(c => c.ThumbnailSize) }))
if (op.Start(new[] { CurrentImage }, new DeskewParams { ThumbnailSize = Config.ThumbnailSize() }))
{
_operationProgress.ShowProgress(op);
}

View File

@ -155,7 +155,7 @@ public partial class ImageForm : FormBase
if (img == Image)
{
var transformed = _imageContext.PerformAllTransforms(new GdiImage(workingImage).Clone(), Transforms);
img.SetThumbnail(_imageContext.PerformTransform(transformed, new ThumbnailTransform(Config.Get(c => c.ThumbnailSize))));
img.SetThumbnail(_imageContext.PerformTransform(transformed, new ThumbnailTransform(Config.ThumbnailSize())));
}
}
}

View File

@ -6,6 +6,11 @@ public static class ThumbnailSizes
public const int DEFAULT_SIZE = 128;
public static int MAX_SIZE = 1024;
public static int Validate(int inputSize)
{
return inputSize.Clamp(MIN_SIZE, MAX_SIZE);
}
public static double StepNumberToSize(double stepNumber)
{
// 64-256:32:6 256-448:48:4 448-832:64:6 832-1024:96:2