From 1ea310e7153811faba4826bbf70070ddf1ef9509 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Sun, 4 Feb 2024 18:34:53 -0800 Subject: [PATCH] Sane: Fix page size for fujitsu/canon_dr backends #281 --- .../Scan/Internal/Sane/SaneOptionNames.cs | 2 ++ .../Internal/Sane/SaneScanAreaController.cs | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/NAPS2.Sdk/Scan/Internal/Sane/SaneOptionNames.cs b/NAPS2.Sdk/Scan/Internal/Sane/SaneOptionNames.cs index f36d5b1177..a9b79550c2 100644 --- a/NAPS2.Sdk/Scan/Internal/Sane/SaneOptionNames.cs +++ b/NAPS2.Sdk/Scan/Internal/Sane/SaneOptionNames.cs @@ -10,6 +10,8 @@ internal static class SaneOptionNames public const string ADF_MODE1 = "adf_mode"; public const string ADF_MODE2 = "adf-mode"; + public const string PAGE_WIDTH = "page-width"; + public const string PAGE_HEIGHT = "page-height"; public const string TOP_LEFT_X = "tl-x"; public const string TOP_LEFT_Y = "tl-y"; public const string BOT_RIGHT_X = "br-x"; diff --git a/NAPS2.Sdk/Scan/Internal/Sane/SaneScanAreaController.cs b/NAPS2.Sdk/Scan/Internal/Sane/SaneScanAreaController.cs index 2358553986..9bae19832b 100644 --- a/NAPS2.Sdk/Scan/Internal/Sane/SaneScanAreaController.cs +++ b/NAPS2.Sdk/Scan/Internal/Sane/SaneScanAreaController.cs @@ -5,6 +5,8 @@ namespace NAPS2.Scan.Internal.Sane; internal class SaneScanAreaController { private readonly SaneOptionController _optionController; + private readonly SaneOption? _pageW; + private readonly SaneOption? _pageH; private readonly SaneOption? _tlx; private readonly SaneOption? _tly; private readonly SaneOption? _brx; @@ -16,6 +18,8 @@ public SaneScanAreaController(SaneOptionController optionController) { _optionController = optionController; + _pageW = _optionController.GetOption(SaneOptionNames.PAGE_WIDTH); + _pageH = _optionController.GetOption(SaneOptionNames.PAGE_HEIGHT); _tlx = _optionController.GetOption(SaneOptionNames.TOP_LEFT_X); _tly = _optionController.GetOption(SaneOptionNames.TOP_LEFT_Y); _brx = _optionController.GetOption(SaneOptionNames.BOT_RIGHT_X); @@ -65,6 +69,8 @@ private bool IsNumericRange(SaneOption opt) private double GetMaxMm(SaneOption opt, double? dpi) => ToMm(opt, GetNumericRangeMax(opt), dpi); + private double? MaybeGetMaxMm(SaneOption? opt, double? res) => opt == null ? null : GetMaxMm(opt, res); + private double ToMm(SaneOption opt, double value, double? dpi) { if (opt.Unit == SaneUnit.Pixel) @@ -77,14 +83,22 @@ private double ToMm(SaneOption opt, double value, double? dpi) public (double minX, double minY, double maxX, double maxY) GetBounds() { if (!CanSetArea) throw new InvalidOperationException(); + // Checking just tl/br should be enough, but some backends have an issue where we need to check width/height too + // https://gitlab.com/sane-project/backends/-/issues/730 return ( - GetMinMm(_tlx!, _xres), GetMinMm(_tly!, _yres), - GetMaxMm(_brx!, _xres), GetMaxMm(_bry!, _yres)); + GetMinMm(_tlx!, _xres), + GetMinMm(_tly!, _yres), + Math.Max(GetMaxMm(_brx!, _xres), MaybeGetMaxMm(_pageW, _xres) ?? 0), + Math.Max(GetMaxMm(_bry!, _yres), MaybeGetMaxMm(_pageH, _xres) ?? 0)); } public void SetArea(double x1, double y1, double x2, double y2) { if (!CanSetArea) throw new InvalidOperationException(); + // Setting just tl/br should be enough, but some backends have an issue where we need to set width/height too + // https://gitlab.com/sane-project/backends/-/issues/730 + _optionController.TrySet(SaneOptionNames.PAGE_WIDTH, x2 - x1); + _optionController.TrySet(SaneOptionNames.PAGE_HEIGHT, y2 - y1); _optionController.TrySet(SaneOptionNames.TOP_LEFT_X, x1); _optionController.TrySet(SaneOptionNames.TOP_LEFT_Y, y1); _optionController.TrySet(SaneOptionNames.BOT_RIGHT_X, x2);