Skip to content

Commit

Permalink
Caps shouldn't be records
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jul 28, 2024
1 parent e5d4810 commit 61f9232
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 94 deletions.
14 changes: 8 additions & 6 deletions NAPS2.Sdk/Scan/BitDepthCaps.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace NAPS2.Scan;

public record BitDepthCaps(
bool SupportsColor,
bool SupportsGrayscale,
bool SupportsBlackAndWhite
)
public class BitDepthCaps
{
private BitDepthCaps() : this(false, false, false)
internal BitDepthCaps()
{
}

public bool SupportsColor { get; init; }

public bool SupportsGrayscale { get; init; }

public bool SupportsBlackAndWhite { get; init; }
}
17 changes: 10 additions & 7 deletions NAPS2.Sdk/Scan/DpiCaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace NAPS2.Scan;

public record DpiCaps(
ImmutableList<int>? Values,
int Min,
int Max,
int Step
)
public class DpiCaps
{
private DpiCaps() : this(null, 0, 0, 0)
internal DpiCaps()
{
}

public ImmutableList<int>? Values { get; init; }

public int Min { get; init; }

public int Max { get; init; }

public int Step { get; init; }
}
57 changes: 36 additions & 21 deletions NAPS2.Sdk/Scan/Internal/Twain/LocalTwainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,27 @@ private List<ScanDevice> InternalGetDeviceList(ScanOptions options)

bool supportsDuplex = supportsFeeder && ds.Capabilities.CapDuplex.GetCurrent() != Duplex.None;

return new ScanCaps(
new MetadataCaps(
Manufacturer: ds.Manufacturer,
Model: ds.Name,
SerialNumber: ds.Capabilities.CapSerialNumber.GetCurrent()
),
new PaperSourceCaps(supportsFlatbed, supportsFeeder, supportsDuplex,
ds.Capabilities.CapAutomaticSenseMedium.IsSupported ||
ds.Capabilities.CapFeederLoaded.IsSupported),
flatbedCaps,
feederCaps,
supportsDuplex ? feederCaps : null
);
return new ScanCaps
{
MetadataCaps = new MetadataCaps
{
Manufacturer = ds.Manufacturer,
Model = ds.Name,
SerialNumber = ds.Capabilities.CapSerialNumber.GetCurrent()
},
PaperSourceCaps = new PaperSourceCaps
{
SupportsFlatbed = supportsFlatbed,
SupportsFeeder = supportsFeeder,
SupportsDuplex = supportsDuplex,
CanCheckIfFeederHasPaper =
ds.Capabilities.CapAutomaticSenseMedium.IsSupported ||
ds.Capabilities.CapFeederLoaded.IsSupported
},
FlatbedCaps = flatbedCaps,
FeederCaps = feederCaps,
DuplexCaps = supportsDuplex ? feederCaps : null
};
}
finally
{
Expand Down Expand Up @@ -184,20 +192,27 @@ private PerSourceCaps GetPerSourceCaps(DataSource ds)
{
var xRes = ds.Capabilities.ICapXResolution.GetValues().Select(x => (int) x.Whole);
var yRes = ds.Capabilities.ICapYResolution.GetValues().Select(x => (int) x.Whole);
var dpiCaps = new DpiCaps(xRes.Intersect(yRes).ToImmutableList(), 0, 0, 0);
var dpiCaps = new DpiCaps { Values = xRes.Intersect(yRes).ToImmutableList() };
var pixelTypes = ds.Capabilities.ICapPixelType.GetValues().ToList();
var bitDepthCaps = new BitDepthCaps(
pixelTypes.Contains(PixelType.RGB),
pixelTypes.Contains(PixelType.Gray),
pixelTypes.Contains(PixelType.BlackWhite));
var bitDepthCaps = new BitDepthCaps
{
SupportsColor = pixelTypes.Contains(PixelType.RGB),
SupportsGrayscale = pixelTypes.Contains(PixelType.Gray),
SupportsBlackAndWhite = pixelTypes.Contains(PixelType.BlackWhite)
};
var w = ds.Capabilities.ICapPhysicalWidth.GetCurrent();
var h = ds.Capabilities.ICapPhysicalHeight.GetCurrent();
var scanAreaSize = new PageSize(
var scanArea = new PageSize(
decimal.Round(w.Whole + w.Fraction / 65536m, 4),
decimal.Round(h.Whole + h.Fraction / 65536m, 4),
PageSizeUnit.Inch);
var pageSizeCaps = new PageSizeCaps(scanAreaSize);
return new PerSourceCaps(dpiCaps, bitDepthCaps, pageSizeCaps);
var pageSizeCaps = new PageSizeCaps { ScanArea = scanArea };
return new PerSourceCaps
{
DpiCaps = dpiCaps,
BitDepthCaps = bitDepthCaps,
PageSizeCaps = pageSizeCaps
};
}

public async Task StartScan(ScanOptions options, ITwainEvents twainEvents, CancellationToken cancelToken)
Expand Down
64 changes: 38 additions & 26 deletions NAPS2.Sdk/Scan/Internal/Wia/WiaScanDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ public Task GetDevices(ScanOptions options, CancellationToken cancelToken, Actio
var feeder = items.FirstOrDefault(x => x.Name() == "Feeder");
var flatbedCaps = flatbed != null ? GetItemCaps(device, flatbed, true) : null;
var feederCaps = feeder != null ? GetItemCaps(device, feeder, false) : null;
return (ScanCaps?) new ScanCaps(
new MetadataCaps(
Manufacturer: device.Properties.GetOrNull(WiaPropertyId.DIP_VEND_DESC)?.Value as string,
Model: device.Properties.GetOrNull(WiaPropertyId.DIP_DEV_DESC)?.Value as string
),
new PaperSourceCaps(device.SupportsFlatbed(), device.SupportsFeeder(), device.SupportsDuplex(),
true),
device.SupportsFlatbed() ? flatbedCaps : null,
device.SupportsFeeder() ? feederCaps : null,
device.SupportsDuplex() ? feederCaps : null
);
return (ScanCaps?) new ScanCaps
{
MetadataCaps = new MetadataCaps
{
Manufacturer = device.Properties.GetOrNull(WiaPropertyId.DIP_VEND_DESC)?.Value as string,
Model = device.Properties.GetOrNull(WiaPropertyId.DIP_DEV_DESC)?.Value as string
},
PaperSourceCaps = new PaperSourceCaps
{
SupportsFlatbed = device.SupportsFlatbed(),
SupportsFeeder = device.SupportsFeeder(),
SupportsDuplex = device.SupportsDuplex(),
CanCheckIfFeederHasPaper = true
},
FlatbedCaps = device.SupportsFlatbed() ? flatbedCaps : null,
FeederCaps = device.SupportsFeeder() ? feederCaps : null,
DuplexCaps = device.SupportsDuplex() ? feederCaps : null
};
}
catch (WiaException e)
{
Expand All @@ -70,27 +77,32 @@ private PerSourceCaps GetItemCaps(WiaDevice device, WiaItem item, bool flatbed)
{
var xRes = item.Properties.GetOrNull(WiaPropertyId.IPS_XRES);
var dpiCaps = xRes != null
? new DpiCaps(
xRes.Attributes.Values?.Cast<int>().ToImmutableList(),
xRes.Attributes.Min,
xRes.Attributes.Max,
xRes.Attributes.Step)
? new DpiCaps
{
Values = xRes.Attributes.Values?.Cast<int>().ToImmutableList(),
Min = xRes.Attributes.Min,
Max = xRes.Attributes.Max,
Step = xRes.Attributes.Step
}
: null;
var dataType = item.Properties.GetOrNull(WiaPropertyId.IPA_DATATYPE);
var validDataTypes = dataType?.Attributes.Values;
var bitDepthCaps = validDataTypes != null
? new BitDepthCaps(
validDataTypes.Contains(3),
validDataTypes.Contains(2),
validDataTypes.Contains(0))
? new BitDepthCaps
{
SupportsColor = validDataTypes.Contains(3),
SupportsGrayscale = validDataTypes.Contains(2),
SupportsBlackAndWhite = validDataTypes.Contains(0)
}
: null;
var (horizontalSize, verticalSize) = GetScanArea(device, item, flatbed);
var scanAreaSize = new PageSize(horizontalSize / 1000m, verticalSize / 1000m, PageSizeUnit.Inch);
return new PerSourceCaps(
dpiCaps,
bitDepthCaps,
new PageSizeCaps(scanAreaSize)
);
var scanArea = new PageSize(horizontalSize / 1000m, verticalSize / 1000m, PageSizeUnit.Inch);
return new PerSourceCaps
{
DpiCaps = dpiCaps,
BitDepthCaps = bitDepthCaps,
PageSizeCaps = new PageSizeCaps { ScanArea = scanArea }
};
}

public Task Scan(ScanOptions options, CancellationToken cancelToken, IScanEvents scanEvents,
Expand Down
23 changes: 14 additions & 9 deletions NAPS2.Sdk/Scan/MetadataCaps.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
namespace NAPS2.Scan;

public record MetadataCaps(
string? DriverSubtype = null,
string? Manufacturer = null,
string? Model = null,
string? SerialNumber = null,
string? Location = null,
string? IconUri = null
)
public class MetadataCaps
{
private MetadataCaps() : this(null, null, null, null, null, null)
internal MetadataCaps()
{
}

public string? DriverSubtype { get; init; }

public string? Manufacturer { get; init; }

public string? Model { get; init; }

public string? SerialNumber { get; init; }

public string? Location { get; init; }

public string? IconUri { get; init; }
}
8 changes: 4 additions & 4 deletions NAPS2.Sdk/Scan/PageSizeCaps.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace NAPS2.Scan;

public record PageSizeCaps(
PageSize ScanAreaSize
)
public class PageSizeCaps
{
private PageSizeCaps() : this(new PageSize(0, 0, PageSizeUnit.Inch))
internal PageSizeCaps()
{
}

public PageSize? ScanArea { get; init; }
}
17 changes: 10 additions & 7 deletions NAPS2.Sdk/Scan/PaperSourceCaps.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
namespace NAPS2.Scan;

public record PaperSourceCaps(
bool SupportsFlatbed,
bool SupportsFeeder,
bool SupportsDuplex,
bool CanCheckIfFeederHasPaper
)
public class PaperSourceCaps
{
private PaperSourceCaps() : this(false, false, false, false)
internal PaperSourceCaps()
{
}

public bool SupportsFlatbed { get; init; }

public bool SupportsFeeder { get; init; }

public bool SupportsDuplex { get; init; }

public bool CanCheckIfFeederHasPaper { get; init; }
}
14 changes: 8 additions & 6 deletions NAPS2.Sdk/Scan/PerSourceCaps.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
namespace NAPS2.Scan;

public record PerSourceCaps(
DpiCaps? DpiCaps,
BitDepthCaps? BitDepthCaps,
PageSizeCaps? PageSizeCaps
)
public class PerSourceCaps
{
private PerSourceCaps() : this(null, null, null)
internal PerSourceCaps()
{
}

public DpiCaps? DpiCaps { get; init; }

public BitDepthCaps? BitDepthCaps { get; init; }

public PageSizeCaps? PageSizeCaps { get; init; }
}
20 changes: 12 additions & 8 deletions NAPS2.Sdk/Scan/ScanCaps.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace NAPS2.Scan;

public record ScanCaps(
MetadataCaps? MetadataCaps,
PaperSourceCaps? PaperSourceCaps,
PerSourceCaps? FlatbedCaps,
PerSourceCaps? FeederCaps,
PerSourceCaps? DuplexCaps
)
public class ScanCaps
{
private ScanCaps() : this(null, null, null, null, null)
internal ScanCaps()
{
}

public MetadataCaps? MetadataCaps { get; init; }

public PaperSourceCaps? PaperSourceCaps { get; init; }

public PerSourceCaps? FlatbedCaps { get; init; }

public PerSourceCaps? FeederCaps { get; init; }

public PerSourceCaps? DuplexCaps { get; init; }
}

0 comments on commit 61f9232

Please sign in to comment.