Skip to content

Commit

Permalink
Sane: Fix page size for fujitsu/canon_dr backends
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Feb 5, 2024
1 parent 506ec8d commit 1ea310e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NAPS2.Sdk/Scan/Internal/Sane/SaneOptionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
18 changes: 16 additions & 2 deletions NAPS2.Sdk/Scan/Internal/Sane/SaneScanAreaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 1ea310e

Please sign in to comment.