From a11add856db398c840d0ec1820316fdbc1b728ae Mon Sep 17 00:00:00 2001 From: Eugene Wang Date: Mon, 13 Feb 2017 21:23:37 -0500 Subject: [PATCH] First attempt on #76 TWFix32 onevalue being wrong. --- src/NTwain/Capabilities.cs | 113 +++++--------------------- src/NTwain/Data/TwainTypesExtended.cs | 27 ++++++ src/NTwain/Data/ValueExtensions.cs | 12 +-- src/NTwain/Properties/VersionInfo.cs | 4 +- 4 files changed, 54 insertions(+), 102 deletions(-) diff --git a/src/NTwain/Capabilities.cs b/src/NTwain/Capabilities.cs index 9b08139..0451334 100644 --- a/src/NTwain/Capabilities.cs +++ b/src/NTwain/Capabilities.cs @@ -326,10 +326,10 @@ namespace NTwain { return _autoBright ?? (_autoBright = new CapWrapper(_source, CapabilityId.ICapAutoBright, ValueExtensions.ConvertToEnum, value => new TWOneValue - { - Item = (uint)value, - ItemType = ItemType.Bool - })); + { + Item = (uint)value, + ItemType = ItemType.Bool + })); } } @@ -345,12 +345,7 @@ namespace NTwain { get { - return _brightness ?? (_brightness = new CapWrapper(_source, CapabilityId.ICapBrightness, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _brightness ?? (_brightness = new CapWrapper(_source, CapabilityId.ICapBrightness, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -366,12 +361,7 @@ namespace NTwain { get { - return _contrast ?? (_contrast = new CapWrapper(_source, CapabilityId.ICapContrast, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _contrast ?? (_contrast = new CapWrapper(_source, CapabilityId.ICapContrast, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -408,12 +398,7 @@ namespace NTwain { get { - return _exposureTime ?? (_exposureTime = new CapWrapper(_source, CapabilityId.ICapExposureTime, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _exposureTime ?? (_exposureTime = new CapWrapper(_source, CapabilityId.ICapExposureTime, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -450,12 +435,7 @@ namespace NTwain { get { - return _gamma ?? (_gamma = new CapWrapper(_source, CapabilityId.ICapGamma, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _gamma ?? (_gamma = new CapWrapper(_source, CapabilityId.ICapGamma, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -487,12 +467,7 @@ namespace NTwain { get { - return _highlight ?? (_highlight = new CapWrapper(_source, CapabilityId.ICapHighlight, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _highlight ?? (_highlight = new CapWrapper(_source, CapabilityId.ICapHighlight, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -625,12 +600,7 @@ namespace NTwain { get { - return _shadow ?? (_shadow = new CapWrapper(_source, CapabilityId.ICapShadow, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _shadow ?? (_shadow = new CapWrapper(_source, CapabilityId.ICapShadow, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -701,12 +671,7 @@ namespace NTwain { get { - return _xResolution ?? (_xResolution = new CapWrapper(_source, CapabilityId.ICapXResolution, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _xResolution ?? (_xResolution = new CapWrapper(_source, CapabilityId.ICapXResolution, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -723,12 +688,7 @@ namespace NTwain { get { - return _yResolution ?? (_yResolution = new CapWrapper(_source, CapabilityId.ICapYResolution, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _yResolution ?? (_yResolution = new CapWrapper(_source, CapabilityId.ICapYResolution, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -891,12 +851,7 @@ namespace NTwain { get { - return _rotation ?? (_rotation = new CapWrapper(_source, CapabilityId.ICapRotation, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _rotation ?? (_rotation = new CapWrapper(_source, CapabilityId.ICapRotation, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -933,12 +888,7 @@ namespace NTwain { get { - return _threshold ?? (_threshold = new CapWrapper(_source, CapabilityId.ICapThreshold, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _threshold ?? (_threshold = new CapWrapper(_source, CapabilityId.ICapThreshold, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -954,12 +904,7 @@ namespace NTwain { get { - return _xscaling ?? (_xscaling = new CapWrapper(_source, CapabilityId.ICapXScaling, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _xscaling ?? (_xscaling = new CapWrapper(_source, CapabilityId.ICapXScaling, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -975,12 +920,7 @@ namespace NTwain { get { - return _yscaling ?? (_yscaling = new CapWrapper(_source, CapabilityId.ICapYScaling, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _yscaling ?? (_yscaling = new CapWrapper(_source, CapabilityId.ICapYScaling, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -1932,12 +1872,7 @@ namespace NTwain { get { - return _mergeHeight ?? (_mergeHeight = new CapWrapper(_source, CapabilityId.ICapImageMergeHeightThreshold, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _mergeHeight ?? (_mergeHeight = new CapWrapper(_source, CapabilityId.ICapImageMergeHeightThreshold, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -3250,12 +3185,7 @@ namespace NTwain { get { - return _dblFeedLength ?? (_dblFeedLength = new CapWrapper(_source, CapabilityId.CapDoubleFeedDetectionLength, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _dblFeedLength ?? (_dblFeedLength = new CapWrapper(_source, CapabilityId.CapDoubleFeedDetectionLength, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } @@ -3355,12 +3285,7 @@ namespace NTwain { get { - return _printVOffset ?? (_printVOffset = new CapWrapper(_source, CapabilityId.CapPrinterVerticalOffset, ValueExtensions.ConvertToFix32, - value => new TWOneValue - { - Item = (uint)value,// ((uint)dpi) << 16; - ItemType = ItemType.Fix32 - })); + return _printVOffset ?? (_printVOffset = new CapWrapper(_source, CapabilityId.CapPrinterVerticalOffset, ValueExtensions.ConvertToFix32, value => value.ToOneValue())); } } diff --git a/src/NTwain/Data/TwainTypesExtended.cs b/src/NTwain/Data/TwainTypesExtended.cs index f878e20..800ac86 100644 --- a/src/NTwain/Data/TwainTypesExtended.cs +++ b/src/NTwain/Data/TwainTypesExtended.cs @@ -70,6 +70,33 @@ namespace NTwain.Data return ToFloat().ToString(CultureInfo.InvariantCulture); } + /// + /// Converts this to for capability set methods. + /// + /// + public TWOneValue ToOneValue() + { + // copy struct parts as-is. + // probably has a faster way but can't think now + + byte[] array = new byte[4]; + var part = BitConverter.GetBytes(Whole); + Buffer.BlockCopy(part, 0, array, 0, 2); + + part = BitConverter.GetBytes(Fraction); + Buffer.BlockCopy(part, 0, array, 2, 2); + + var converted = BitConverter.ToUInt32(array, 0); + + return new TWOneValue + { + ItemType = ItemType.Fix32, + Item = converted + // old wrong conversion + // (uint)this,// ((uint)dpi) << 16; + }; + } + #region equals /// diff --git a/src/NTwain/Data/ValueExtensions.cs b/src/NTwain/Data/ValueExtensions.cs index c986af3..d152cb4 100644 --- a/src/NTwain/Data/ValueExtensions.cs +++ b/src/NTwain/Data/ValueExtensions.cs @@ -16,7 +16,7 @@ namespace NTwain.Data /// The type of the enum. /// The list. /// - public static IList CastToEnum(this IEnumerable list) where TEnum : struct,IConvertible + public static IList CastToEnum(this IEnumerable list) where TEnum : struct, IConvertible { return list.CastToEnum(true); } @@ -27,7 +27,7 @@ namespace NTwain.Data /// The list. /// set to true for working with bad values. /// - public static IList CastToEnum(this IEnumerable list, bool tryUpperWord) where TEnum : struct,IConvertible + public static IList CastToEnum(this IEnumerable list, bool tryUpperWord) where TEnum : struct, IConvertible { return list.Select(o => o.ConvertToEnum(tryUpperWord)).ToList(); } @@ -38,7 +38,7 @@ namespace NTwain.Data /// The type of the enum. /// The value. /// - public static TEnum ConvertToEnum(this object value) where TEnum : struct,IConvertible + public static TEnum ConvertToEnum(this object value) where TEnum : struct, IConvertible { return ConvertToEnum(value, true); } @@ -49,7 +49,7 @@ namespace NTwain.Data /// The value. /// if set to true [try upper word]. /// - public static TEnum ConvertToEnum(this object value, bool tryUpperWord) where TEnum : struct,IConvertible + public static TEnum ConvertToEnum(this object value, bool tryUpperWord) where TEnum : struct, IConvertible { if (value != null) { @@ -59,7 +59,7 @@ namespace NTwain.Data if (returnType.IsEnum) { var rawType = Enum.GetUnderlyingType(returnType); - + if (tryUpperWord) { // small routine to work with bad sources that may put @@ -76,7 +76,7 @@ namespace NTwain.Data } // old method: // return (TEnum)Enum.ToObject(returnType, value); - + // new method: // try to convert to enum's underlying type first then cast to the enum return (TEnum)Convert.ChangeType(value, rawType, CultureInfo.InvariantCulture); diff --git a/src/NTwain/Properties/VersionInfo.cs b/src/NTwain/Properties/VersionInfo.cs index d5ec71d..3dd43d0 100644 --- a/src/NTwain/Properties/VersionInfo.cs +++ b/src/NTwain/Properties/VersionInfo.cs @@ -1,6 +1,6 @@ using System.Reflection; -[assembly: AssemblyCopyright("Copyright \x00a9 Yin-Chun Wang 2012-2016")] +[assembly: AssemblyCopyright("Copyright \x00a9 Yin-Chun Wang 2012-2017")] [assembly: AssemblyCompany("Yin-Chun Wang")] [assembly: AssemblyVersion(NTwain.VersionInfo.Release)] @@ -23,7 +23,7 @@ namespace NTwain /// /// The build release version number. /// - public const string Build = "3.4.0"; // change this for each nuget release + public const string Build = "3.4.1"; // change this for each nuget release }