Skip to content

Commit 1ea310e

Browse files
committed
Sane: Fix page size for fujitsu/canon_dr backends
#281
1 parent 506ec8d commit 1ea310e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

NAPS2.Sdk/Scan/Internal/Sane/SaneOptionNames.cs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ internal static class SaneOptionNames
1010
public const string ADF_MODE1 = "adf_mode";
1111
public const string ADF_MODE2 = "adf-mode";
1212

13+
public const string PAGE_WIDTH = "page-width";
14+
public const string PAGE_HEIGHT = "page-height";
1315
public const string TOP_LEFT_X = "tl-x";
1416
public const string TOP_LEFT_Y = "tl-y";
1517
public const string BOT_RIGHT_X = "br-x";

NAPS2.Sdk/Scan/Internal/Sane/SaneScanAreaController.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace NAPS2.Scan.Internal.Sane;
55
internal class SaneScanAreaController
66
{
77
private readonly SaneOptionController _optionController;
8+
private readonly SaneOption? _pageW;
9+
private readonly SaneOption? _pageH;
810
private readonly SaneOption? _tlx;
911
private readonly SaneOption? _tly;
1012
private readonly SaneOption? _brx;
@@ -16,6 +18,8 @@ public SaneScanAreaController(SaneOptionController optionController)
1618
{
1719
_optionController = optionController;
1820

21+
_pageW = _optionController.GetOption(SaneOptionNames.PAGE_WIDTH);
22+
_pageH = _optionController.GetOption(SaneOptionNames.PAGE_HEIGHT);
1923
_tlx = _optionController.GetOption(SaneOptionNames.TOP_LEFT_X);
2024
_tly = _optionController.GetOption(SaneOptionNames.TOP_LEFT_Y);
2125
_brx = _optionController.GetOption(SaneOptionNames.BOT_RIGHT_X);
@@ -65,6 +69,8 @@ private bool IsNumericRange(SaneOption opt)
6569

6670
private double GetMaxMm(SaneOption opt, double? dpi) => ToMm(opt, GetNumericRangeMax(opt), dpi);
6771

72+
private double? MaybeGetMaxMm(SaneOption? opt, double? res) => opt == null ? null : GetMaxMm(opt, res);
73+
6874
private double ToMm(SaneOption opt, double value, double? dpi)
6975
{
7076
if (opt.Unit == SaneUnit.Pixel)
@@ -77,14 +83,22 @@ private double ToMm(SaneOption opt, double value, double? dpi)
7783
public (double minX, double minY, double maxX, double maxY) GetBounds()
7884
{
7985
if (!CanSetArea) throw new InvalidOperationException();
86+
// Checking just tl/br should be enough, but some backends have an issue where we need to check width/height too
87+
// https://gitlab.com/sane-project/backends/-/issues/730
8088
return (
81-
GetMinMm(_tlx!, _xres), GetMinMm(_tly!, _yres),
82-
GetMaxMm(_brx!, _xres), GetMaxMm(_bry!, _yres));
89+
GetMinMm(_tlx!, _xres),
90+
GetMinMm(_tly!, _yres),
91+
Math.Max(GetMaxMm(_brx!, _xres), MaybeGetMaxMm(_pageW, _xres) ?? 0),
92+
Math.Max(GetMaxMm(_bry!, _yres), MaybeGetMaxMm(_pageH, _xres) ?? 0));
8393
}
8494

8595
public void SetArea(double x1, double y1, double x2, double y2)
8696
{
8797
if (!CanSetArea) throw new InvalidOperationException();
98+
// Setting just tl/br should be enough, but some backends have an issue where we need to set width/height too
99+
// https://gitlab.com/sane-project/backends/-/issues/730
100+
_optionController.TrySet(SaneOptionNames.PAGE_WIDTH, x2 - x1);
101+
_optionController.TrySet(SaneOptionNames.PAGE_HEIGHT, y2 - y1);
88102
_optionController.TrySet(SaneOptionNames.TOP_LEFT_X, x1);
89103
_optionController.TrySet(SaneOptionNames.TOP_LEFT_Y, y1);
90104
_optionController.TrySet(SaneOptionNames.BOT_RIGHT_X, x2);

0 commit comments

Comments
 (0)