Skip to content

Commit

Permalink
Sane: Don't select error diffusion-based grayscale
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 11, 2024
1 parent 72aa8f4 commit ac80f4e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
35 changes: 26 additions & 9 deletions NAPS2.Sdk.Tests/Scan/SaneScanDriverOptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SetOptions_Flatbed()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF", "Duplex" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["Flatbed", "ADF", "Duplex"])
});
var options = new ScanOptions
{
Expand All @@ -39,7 +39,7 @@ public void SetOptions_Feeder()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF", "Duplex" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["Flatbed", "ADF", "Duplex"])
});
var options = new ScanOptions { PaperSource = PaperSource.Feeder };

Expand All @@ -54,7 +54,7 @@ public void SetOptions_FeederWithDuplexMatch()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF Duplex", "ADF" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["Flatbed", "ADF Duplex", "ADF"])
});
var options = new ScanOptions { PaperSource = PaperSource.Feeder };

Expand All @@ -69,7 +69,7 @@ public void SetOptions_Duplex()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF", "Duplex" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["Flatbed", "ADF", "Duplex"])
});
var options = new ScanOptions { PaperSource = PaperSource.Duplex };

Expand All @@ -84,7 +84,7 @@ public void SetOptions_AutoWithFlatbed()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF", "Duplex" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["Flatbed", "ADF", "Duplex"])
});
var options = new ScanOptions { PaperSource = PaperSource.Auto };

Expand All @@ -99,7 +99,7 @@ public void SetOptions_AutoWithNoFlatbed()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "ADF", "Duplex" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["ADF", "Duplex"])
});
var options = new ScanOptions { PaperSource = PaperSource.Auto };

Expand All @@ -115,7 +115,7 @@ public void SetOptions_DuplexWithPartialMatch()
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE,
new[] { "Feeder(left aligned)", "Feeder(left aligned,Duplex)" })
["Feeder(left aligned)", "Feeder(left aligned,Duplex)"])
});
var options = new ScanOptions { PaperSource = PaperSource.Duplex };

Expand All @@ -130,8 +130,8 @@ public void SetOptions_DuplicateOptions()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF", "Duplex" }),
SaneOption.CreateForTesting(2, SaneOptionNames.SOURCE, new[] { "Flatbed", "ADF", "Duplex" })
SaneOption.CreateForTesting(1, SaneOptionNames.SOURCE, ["Flatbed", "ADF", "Duplex"]),
SaneOption.CreateForTesting(2, SaneOptionNames.SOURCE, ["Flatbed", "ADF", "Duplex"])
});
var options = new ScanOptions
{
Expand All @@ -144,6 +144,23 @@ public void SetOptions_DuplicateOptions()
Assert.Equal("Flatbed", device.GetValue(1));
}

[Fact]
public void SetOptions_NoGrayErrorDiffusion()
{
var device = new DeviceOptionsMock(new[]
{
SaneOption.CreateForTesting(1, SaneOptionNames.MODE, ["Gray[Error Diffusion]", "True Gray"])
});
var options = new ScanOptions
{
BitDepth = BitDepth.Grayscale
};

var optionData = _driver.SetOptions(device, options);

Assert.Equal("True Gray", optionData.Mode);
}

private class DeviceOptionsMock : ISaneDevice
{
private readonly IEnumerable<SaneOption> _options;
Expand Down
7 changes: 7 additions & 0 deletions NAPS2.Sdk/Scan/Internal/Sane/SaneOptionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ public bool TrySet(string name, double value)
}

public bool TrySet(string name, SaneOptionMatcher matcher)
{
return TrySet(name, matcher, out _);
}

public bool TrySet(string name, SaneOptionMatcher matcher, out string? matchedValue)
{
_logger.LogDebug($"Maybe setting {name}");
matchedValue = null;
if (!_options.ContainsKey(name))
return false;
var opt = _options[name];
Expand All @@ -68,6 +74,7 @@ public bool TrySet(string name, SaneOptionMatcher matcher)
{
if (matcher.Matches(value))
{
matchedValue = value;
_logger.LogDebug($"Setting {name} to {value}");
_device.SetOption(opt, value, out var info);
if (info.HasFlag(SaneOptionSetInfo.ReloadOptions))
Expand Down
8 changes: 5 additions & 3 deletions NAPS2.Sdk/Scan/Internal/Sane/SaneOptionMatchers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ internal static class SaneOptionMatchers

public static readonly SaneOptionMatcher BlackAndWhite =
new SaneOptionMatcher(SaneOptionTranslations.Lineart, "black and white", "black & white", "black/white");

public static readonly SaneOptionMatcher Grayscale =
new SaneOptionMatcher(SaneOptionTranslations.Gray, "gray", "grey");

new SaneOptionMatcher(SaneOptionTranslations.Gray, "gray", "grey")
// Error diffusion isn't "real" grayscale
.Exclude(new SaneOptionMatcher([], "Error Diffusion"));

public static readonly SaneOptionMatcher Color =
new SaneOptionMatcher(SaneOptionTranslations.Color, "color", "colour");
}
9 changes: 5 additions & 4 deletions NAPS2.Sdk/Scan/Internal/Sane/SaneScanDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ internal OptionData SetOptions(ISaneDevice device, ScanOptions options)
BitDepth.Grayscale => SaneOptionMatchers.Grayscale,
_ => SaneOptionMatchers.Color
};
controller.TrySet(SaneOptionNames.MODE, mode);
controller.TrySet(SaneOptionNames.MODE, mode, out optionData.Mode);

SetResolution(options, controller, optionData);

Expand Down Expand Up @@ -580,9 +580,10 @@ private void ReadSingleChannelFrame(ISaneDevice device, IScanEvents scanEvents,

internal class OptionData
{
public bool IsFeeder { get; set; }
public double XRes { get; set; }
public double YRes { get; set; }
public bool IsFeeder;
public double XRes;
public double YRes;
public string? Mode;
}

// if (options.BitDepth == BitDepth.Color)
Expand Down

0 comments on commit ac80f4e

Please sign in to comment.