@@ -5,6 +5,8 @@ namespace NAPS2.Scan.Internal.Sane;
5
5
internal class SaneScanAreaController
6
6
{
7
7
private readonly SaneOptionController _optionController ;
8
+ private readonly SaneOption ? _pageW ;
9
+ private readonly SaneOption ? _pageH ;
8
10
private readonly SaneOption ? _tlx ;
9
11
private readonly SaneOption ? _tly ;
10
12
private readonly SaneOption ? _brx ;
@@ -16,6 +18,8 @@ public SaneScanAreaController(SaneOptionController optionController)
16
18
{
17
19
_optionController = optionController ;
18
20
21
+ _pageW = _optionController . GetOption ( SaneOptionNames . PAGE_WIDTH ) ;
22
+ _pageH = _optionController . GetOption ( SaneOptionNames . PAGE_HEIGHT ) ;
19
23
_tlx = _optionController . GetOption ( SaneOptionNames . TOP_LEFT_X ) ;
20
24
_tly = _optionController . GetOption ( SaneOptionNames . TOP_LEFT_Y ) ;
21
25
_brx = _optionController . GetOption ( SaneOptionNames . BOT_RIGHT_X ) ;
@@ -65,6 +69,8 @@ private bool IsNumericRange(SaneOption opt)
65
69
66
70
private double GetMaxMm ( SaneOption opt , double ? dpi ) => ToMm ( opt , GetNumericRangeMax ( opt ) , dpi ) ;
67
71
72
+ private double ? MaybeGetMaxMm ( SaneOption ? opt , double ? res ) => opt == null ? null : GetMaxMm ( opt , res ) ;
73
+
68
74
private double ToMm ( SaneOption opt , double value , double ? dpi )
69
75
{
70
76
if ( opt . Unit == SaneUnit . Pixel )
@@ -77,14 +83,22 @@ private double ToMm(SaneOption opt, double value, double? dpi)
77
83
public ( double minX , double minY , double maxX , double maxY ) GetBounds ( )
78
84
{
79
85
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
80
88
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 ) ) ;
83
93
}
84
94
85
95
public void SetArea ( double x1 , double y1 , double x2 , double y2 )
86
96
{
87
97
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 ) ;
88
102
_optionController . TrySet ( SaneOptionNames . TOP_LEFT_X , x1 ) ;
89
103
_optionController . TrySet ( SaneOptionNames . TOP_LEFT_Y , y1 ) ;
90
104
_optionController . TrySet ( SaneOptionNames . BOT_RIGHT_X , x2 ) ;
0 commit comments