From 5fad8b3c020ff1f1f102fe4bdf123fbb6c344d73 Mon Sep 17 00:00:00 2001 From: soukoku Date: Sun, 11 Jan 2015 13:16:02 -0500 Subject: [PATCH] Moved TWAIN type reading/writing under one class. --- NTwain.Net35/NTwain.Net35.csproj | 4 +- NTwain/CapWrapper.cs | 2 +- NTwain/CapabilityReader.cs | 44 +++++- NTwain/Data/TwainTypesExtended.cs | 210 ++----------------------- NTwain/Data/TypeExtensions.cs | 249 ++++++++++++++++++++++++++++++ NTwain/Data/TypeReader.cs | 110 ------------- NTwain/Data/ValueExtensions.cs | 2 +- NTwain/DataSource.Caps.cs | 2 +- NTwain/Internals/TransferLogic.cs | 11 +- NTwain/NTwain.csproj | 2 +- 10 files changed, 313 insertions(+), 323 deletions(-) create mode 100644 NTwain/Data/TypeExtensions.cs delete mode 100644 NTwain/Data/TypeReader.cs diff --git a/NTwain.Net35/NTwain.Net35.csproj b/NTwain.Net35/NTwain.Net35.csproj index 9ebbb92..4d6bd4b 100644 --- a/NTwain.Net35/NTwain.Net35.csproj +++ b/NTwain.Net35/NTwain.Net35.csproj @@ -69,8 +69,8 @@ Data\TwainValues.cs - - Data\TypeReader.cs + + Data\TypeExtensions.cs Data\ValueExtensions.cs diff --git a/NTwain/CapWrapper.cs b/NTwain/CapWrapper.cs index 49f8a15..c706bfe 100644 --- a/NTwain/CapWrapper.cs +++ b/NTwain/CapWrapper.cs @@ -321,7 +321,7 @@ namespace NTwain var rc = _source.DGControl.Capability.GetLabelEnum(cap); if (rc == ReturnCode.Success) { - cap.ReadMultiCapValues(list); + CapabilityReader.ReadValue(cap).PopulateFromCapValues(list); } } } diff --git a/NTwain/CapabilityReader.cs b/NTwain/CapabilityReader.cs index 3226efb..411f3ac 100644 --- a/NTwain/CapabilityReader.cs +++ b/NTwain/CapabilityReader.cs @@ -179,12 +179,50 @@ namespace NTwain #region reader methods + /// + /// Don't care what contain it is, just populates the specified list with the capability values (count be one or many). + /// + /// The list to populate the values. + /// + public IList PopulateFromCapValues(IList toPopulate) + { + if (toPopulate == null) { toPopulate = new List(); } + + switch (ContainerType) + { + case ContainerType.OneValue: + if (OneValue != null) + { + toPopulate.Add(OneValue); + } + break; + case ContainerType.Array: + case ContainerType.Enum: + if (CollectionValues != null) + { + foreach (var o in CollectionValues) + { + toPopulate.Add(o); + } + } + break; + case ContainerType.Range: + for (var i = RangeMinValue; i >= RangeMinValue && i <= RangeMaxValue; i += RangeStepSize) + { + toPopulate.Add(i); + } + break; + } + return toPopulate; + } + + CapabilityReader ReadOneValue(IntPtr baseAddr) { int offset = 0; ItemType = (ItemType)(ushort)Marshal.ReadInt16(baseAddr, offset); offset += 2; - OneValue = TypeReader.ReadValue(baseAddr, ref offset, ItemType); + OneValue = baseAddr.ReadValue(ref offset, ItemType); return this; } @@ -200,7 +238,7 @@ namespace NTwain CollectionValues = new object[count]; for (int i = 0; i < count; i++) { - CollectionValues[i] = TypeReader.ReadValue(baseAddr, ref offset, ItemType); + CollectionValues[i] = baseAddr.ReadValue(ref offset, ItemType); } } return this; @@ -222,7 +260,7 @@ namespace NTwain CollectionValues = new object[count]; for (int i = 0; i < count; i++) { - CollectionValues[i] = TypeReader.ReadValue(baseAddr, ref offset, ItemType); + CollectionValues[i] = baseAddr.ReadValue(ref offset, ItemType); } } return this; diff --git a/NTwain/Data/TwainTypesExtended.cs b/NTwain/Data/TwainTypesExtended.cs index 24d8aa1..e68fb56 100644 --- a/NTwain/Data/TwainTypesExtended.cs +++ b/NTwain/Data/TwainTypesExtended.cs @@ -743,7 +743,7 @@ namespace NTwain.Data ContainerType = ContainerType.OneValue; // since one value can only house UInt32 we will not allow type size > 4 - if (TypeReader.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.BadValueType, "TWOneValue")); } + if (TypeExtensions.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.BadValueType, "TWOneValue")); } _hContainer = memoryManager.Allocate((uint)Marshal.SizeOf(value)); if (_hContainer != IntPtr.Zero) @@ -758,20 +758,20 @@ namespace NTwain.Data ContainerType = ContainerType.Enum; - Int32 valueSize = TWEnumeration.ItemOffset + value.ItemList.Length * TypeReader.GetItemTypeSize(value.ItemType); + Int32 valueSize = TWEnumeration.ItemOffset + value.ItemList.Length * TypeExtensions.GetItemTypeSize(value.ItemType); int offset = 0; _hContainer = memoryManager.Allocate((uint)valueSize); IntPtr baseAddr = memoryManager.Lock(_hContainer); // can't safely use StructureToPtr here so write it our own - WriteValue(baseAddr, ref offset, ItemType.UInt16, value.ItemType); - WriteValue(baseAddr, ref offset, ItemType.UInt32, (uint)value.ItemList.Length); - WriteValue(baseAddr, ref offset, ItemType.UInt32, value.CurrentIndex); - WriteValue(baseAddr, ref offset, ItemType.UInt32, value.DefaultIndex); + baseAddr.WriteValue(ref offset, ItemType.UInt16, value.ItemType); + baseAddr.WriteValue(ref offset, ItemType.UInt32, (uint)value.ItemList.Length); + baseAddr.WriteValue(ref offset, ItemType.UInt32, value.CurrentIndex); + baseAddr.WriteValue(ref offset, ItemType.UInt32, value.DefaultIndex); foreach (var item in value.ItemList) { - WriteValue(baseAddr, ref offset, value.ItemType, item); + baseAddr.WriteValue(ref offset, value.ItemType, item); } //memoryManager.Unlock(baseAddr); memoryManager.Unlock(_hContainer); @@ -783,7 +783,7 @@ namespace NTwain.Data ContainerType = ContainerType.Range; // since range value can only house UInt32 we will not allow type size > 4 - if (TypeReader.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.BadValueType, "TWRange")); } + if (TypeExtensions.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.BadValueType, "TWRange")); } _hContainer = memoryManager.Allocate((uint)Marshal.SizeOf(value)); if (_hContainer != IntPtr.Zero) @@ -797,18 +797,18 @@ namespace NTwain.Data if (value == null) { throw new ArgumentNullException("value"); } ContainerType = ContainerType.Array; - Int32 valueSize = 6 + value.ItemList.Length * TypeReader.GetItemTypeSize(value.ItemType); + Int32 valueSize = 6 + value.ItemList.Length * TypeExtensions.GetItemTypeSize(value.ItemType); int offset = 0; _hContainer = memoryManager.Allocate((uint)valueSize); IntPtr baseAddr = memoryManager.Lock(_hContainer); // can't safely use StructureToPtr here so write it our own - WriteValue(baseAddr, ref offset, ItemType.UInt16, value.ItemType); - WriteValue(baseAddr, ref offset, ItemType.UInt32, (uint)value.ItemList.Length); + baseAddr.WriteValue(ref offset, ItemType.UInt16, value.ItemType); + baseAddr.WriteValue(ref offset, ItemType.UInt32, (uint)value.ItemList.Length); foreach (var item in value.ItemList) { - WriteValue(baseAddr, ref offset, value.ItemType, item); + baseAddr.WriteValue(ref offset, value.ItemType, item); } memoryManager.Unlock(_hContainer); //memoryManager.Unlock(baseAddr); @@ -816,145 +816,6 @@ namespace NTwain.Data #endregion - #region writes - - /// - /// Entry call for writing values to a pointer. - /// - /// - /// - /// - /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] - void WriteValue(IntPtr baseAddr, ref int offset, ItemType type, object value) - { - switch (type) - { - case ItemType.Int8: - case ItemType.UInt8: - Marshal.WriteByte(baseAddr, offset, Convert.ToByte(value, CultureInfo.InvariantCulture));// (byte)value); - break; - case ItemType.Bool: - case ItemType.Int16: - case ItemType.UInt16: - Marshal.WriteInt16(baseAddr, offset, Convert.ToInt16(value, CultureInfo.InvariantCulture));//(short)value); - break; - case ItemType.UInt32: - case ItemType.Int32: - Marshal.WriteInt32(baseAddr, offset, Convert.ToInt32(value, CultureInfo.InvariantCulture));//(int)value); - break; - case ItemType.Fix32: - TWFix32 f32 = (TWFix32)value; - Marshal.WriteInt16(baseAddr, offset, f32.Whole); - if (f32.Fraction > Int16.MaxValue) - { - Marshal.WriteInt16(baseAddr, offset + 2, (Int16)(f32.Fraction - 32768)); - } - else - { - Marshal.WriteInt16(baseAddr, offset + 2, (Int16)f32.Fraction); - } - break; - case ItemType.Frame: - TWFrame frame = (TWFrame)value; - WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Left); - WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Top); - WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Right); - WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Bottom); - return; // no need to update offset for this - //case ItemType.String1024: - // WriteString(baseAddr, offset, value as string, 1024); - // break; - case ItemType.String128: - WriteString(baseAddr, offset, (string)value, 128); - break; - case ItemType.String255: - WriteString(baseAddr, offset, (string)value, 255); - break; - case ItemType.String32: - WriteString(baseAddr, offset, (string)value, 32); - break; - case ItemType.String64: - WriteString(baseAddr, offset, (string)value, 64); - break; - //case ItemType.Unicode512: - // WriteUString(baseAddr, offset, value as string, 512); - // break; - } - offset += TypeReader.GetItemTypeSize(type); - } - /// - /// Writes string value. - /// - /// - /// - /// - /// - static void WriteString(IntPtr baseAddr, int offset, string item, int maxLength) - { - if (string.IsNullOrEmpty(item)) - { - // write zero - Marshal.WriteByte(baseAddr, offset, 0); - } - else - { - for (int i = 0; i < maxLength; i++) - { - if (i == item.Length) - { - // string end reached, so write \0 and quit - Marshal.WriteByte(baseAddr, offset, 0); - return; - } - else - { - Marshal.WriteByte(baseAddr, offset, (byte)item[i]); - offset++; - } - } - // when ended normally also write \0 - Marshal.WriteByte(baseAddr, offset, 0); - } - } - ///// - ///// Writes unicode string value. - ///// - ///// - ///// - ///// - ///// - //[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)] - //private void WriteUString(IntPtr baseAddr, int offset, string item, int maxLength) - //{ - // if (string.IsNullOrEmpty(item)) - // { - // // write zero - // Marshal.WriteInt16(baseAddr, offset, (char)0); - // } - // else - // { - // // use 2 bytes per char - // for (int i = 0; i < maxLength; i++) - // { - // if (i == item.Length) - // { - // // string end reached, so write \0 and quit - // Marshal.WriteInt16(baseAddr, offset, (char)0); - // return; - // } - // else - // { - // Marshal.WriteInt16(baseAddr, offset, item[i]); - // offset += 2; - // } - // } - // // when ended normally also write \0 - // Marshal.WriteByte(baseAddr, offset, 0); - // } - //} - - #endregion #region IDisposable Members @@ -986,47 +847,6 @@ namespace NTwain.Data } #endregion - - - /// - /// A general method that returns the data in a . - /// - /// The list to populate if necessary. - /// - public IList ReadMultiCapValues(IList toPopulate) - { - if (toPopulate == null) { toPopulate = new List(); } - - var read = CapabilityReader.ReadValue(this); - - switch (read.ContainerType) - { - case ContainerType.OneValue: - if (read.OneValue != null) - { - toPopulate.Add(read.OneValue); - } - break; - case ContainerType.Array: - case ContainerType.Enum: - if (read.CollectionValues != null) - { - foreach (var o in read.CollectionValues) - { - toPopulate.Add(o); - } - } - break; - case ContainerType.Range: - for (var i = read.RangeMinValue; i >= read.RangeMinValue && i <= read.RangeMaxValue; i += read.RangeStepSize) - { - toPopulate.Add(i); - } - break; - } - return toPopulate; - } - } /// @@ -1454,7 +1274,7 @@ namespace NTwain.Data get { return ItemType == Data.ItemType.Handle || - (TypeReader.GetItemTypeSize(ItemType) * NumItems) > 4;// IntPtr.Size + (TypeExtensions.GetItemTypeSize(ItemType) * NumItems) > 4;// IntPtr.Size } } @@ -1479,7 +1299,7 @@ namespace NTwain.Data for (int i = 0; i < NumItems; i++) { - values.Add(TypeReader.ReadValue(lockPtr, ref offset, ItemType)); + values.Add(TypeExtensions.ReadValue(lockPtr, ref offset, ItemType)); } } finally @@ -1501,7 +1321,7 @@ namespace NTwain.Data tempPtr = Marshal.AllocHGlobal(IntPtr.Size); Marshal.WriteIntPtr(tempPtr, Item); int offset = 0; - values.Add(TypeReader.ReadValue(tempPtr, ref offset, ItemType)); + values.Add(TypeExtensions.ReadValue(tempPtr, ref offset, ItemType)); } finally { diff --git a/NTwain/Data/TypeExtensions.cs b/NTwain/Data/TypeExtensions.cs new file mode 100644 index 0000000..73c2a6e --- /dev/null +++ b/NTwain/Data/TypeExtensions.cs @@ -0,0 +1,249 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Runtime.InteropServices; + +namespace NTwain.Data +{ + /// + /// Contains extensions for reading/writing TWAIN types from/to pointer. + /// + public static class TypeExtensions + { + static readonly IDictionary __sizes = new Dictionary + { + {ItemType.Int8, 1}, + {ItemType.UInt8, 1}, + {ItemType.Int16, 2}, + {ItemType.UInt8, 2}, + {ItemType.Bool, 2}, + {ItemType.Int32, 4}, + {ItemType.UInt32, 4}, + {ItemType.Fix32, 4}, + {ItemType.Frame, 16}, + {ItemType.String32, TwainConst.String32}, + {ItemType.String64, TwainConst.String64}, + {ItemType.String128, TwainConst.String128}, + {ItemType.String255, TwainConst.String255}, + // TODO: find out if it should be fixed 4 bytes or intptr size + {ItemType.Handle, IntPtr.Size}, + }; + + /// + /// Gets the byte size of the item type. + /// + /// + /// + public static int GetItemTypeSize(this ItemType type) + { + if (__sizes.ContainsKey(type)) + { + return __sizes[type]; + } + return 0; + } + + + /// + /// Reads a TWAIN value. + /// + /// The base address. + /// The offset. + /// The TWAIN type. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "1#")] + public static object ReadValue(this IntPtr baseAddress, ref int offset, ItemType type) + { + object val = null; + switch (type) + { + case ItemType.Int8: + val = (sbyte)Marshal.ReadByte(baseAddress, offset); + break; + case ItemType.UInt8: + val = Marshal.ReadByte(baseAddress, offset); + break; + case ItemType.Bool: + case ItemType.UInt16: + val = (ushort)Marshal.ReadInt16(baseAddress, offset); + break; + case ItemType.Int16: + val = Marshal.ReadInt16(baseAddress, offset); + break; + case ItemType.UInt32: + val = (uint)Marshal.ReadInt32(baseAddress, offset); + break; + case ItemType.Int32: + val = Marshal.ReadInt32(baseAddress, offset); + break; + case ItemType.Fix32: + TWFix32 f32 = new TWFix32(); + f32.Whole = Marshal.ReadInt16(baseAddress, offset); + f32.Fraction = (ushort)Marshal.ReadInt16(baseAddress, offset + 2); + val = f32; + break; + case ItemType.Frame: + TWFrame frame = new TWFrame(); + frame.Left = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); + frame.Top = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); + frame.Right = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); + frame.Bottom = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); + return frame; // no need to update offset again after reading fix32 + case ItemType.String128: + case ItemType.String255: + case ItemType.String32: + case ItemType.String64: + val = Marshal.PtrToStringAnsi(new IntPtr(baseAddress.ToInt64() + offset)); + break; + case ItemType.Handle: + val = Marshal.ReadIntPtr(baseAddress, offset); + break; + } + offset += GetItemTypeSize(type); + return val; + } + + + + #region writes + + /// + /// Writes a TWAIN value. + /// + /// The base addr. + /// The offset. + /// The TWAIN type. + /// The value. + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] + public static void WriteValue(this IntPtr baseAddr, ref int offset, ItemType type, object value) + { + switch (type) + { + case ItemType.Int8: + case ItemType.UInt8: + Marshal.WriteByte(baseAddr, offset, Convert.ToByte(value, CultureInfo.InvariantCulture));// (byte)value); + break; + case ItemType.Bool: + case ItemType.Int16: + case ItemType.UInt16: + Marshal.WriteInt16(baseAddr, offset, Convert.ToInt16(value, CultureInfo.InvariantCulture));//(short)value); + break; + case ItemType.UInt32: + case ItemType.Int32: + Marshal.WriteInt32(baseAddr, offset, Convert.ToInt32(value, CultureInfo.InvariantCulture));//(int)value); + break; + case ItemType.Fix32: + TWFix32 f32 = (TWFix32)value; + Marshal.WriteInt16(baseAddr, offset, f32.Whole); + if (f32.Fraction > Int16.MaxValue) + { + Marshal.WriteInt16(baseAddr, offset + 2, (Int16)(f32.Fraction - 32768)); + } + else + { + Marshal.WriteInt16(baseAddr, offset + 2, (Int16)f32.Fraction); + } + break; + case ItemType.Frame: + TWFrame frame = (TWFrame)value; + WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Left); + WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Top); + WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Right); + WriteValue(baseAddr, ref offset, ItemType.Fix32, frame.Bottom); + return; // no need to update offset for this + //case ItemType.String1024: + // WriteString(baseAddr, offset, value as string, 1024); + // break; + case ItemType.String128: + WriteString(baseAddr, offset, (string)value, 128); + break; + case ItemType.String255: + WriteString(baseAddr, offset, (string)value, 255); + break; + case ItemType.String32: + WriteString(baseAddr, offset, (string)value, 32); + break; + case ItemType.String64: + WriteString(baseAddr, offset, (string)value, 64); + break; + //case ItemType.Unicode512: + // WriteUString(baseAddr, offset, value as string, 512); + // break; + } + offset += TypeExtensions.GetItemTypeSize(type); + } + /// + /// Writes string value. THIS MAY BE WRONG. + /// + /// + /// + /// + /// + static void WriteString(IntPtr baseAddr, int offset, string item, int maxLength) + { + if (string.IsNullOrEmpty(item)) + { + // write zero + Marshal.WriteByte(baseAddr, offset, 0); + } + else + { + for (int i = 0; i < maxLength; i++) + { + if (i == item.Length) + { + // string end reached, so write \0 and quit + Marshal.WriteByte(baseAddr, offset, 0); + return; + } + else + { + Marshal.WriteByte(baseAddr, offset, (byte)item[i]); + offset++; + } + } + // when ended normally also write \0 + Marshal.WriteByte(baseAddr, offset, 0); + } + } + ///// + ///// Writes unicode string value. + ///// + ///// + ///// + ///// + ///// + //[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)] + //private void WriteUString(IntPtr baseAddr, int offset, string item, int maxLength) + //{ + // if (string.IsNullOrEmpty(item)) + // { + // // write zero + // Marshal.WriteInt16(baseAddr, offset, (char)0); + // } + // else + // { + // // use 2 bytes per char + // for (int i = 0; i < maxLength; i++) + // { + // if (i == item.Length) + // { + // // string end reached, so write \0 and quit + // Marshal.WriteInt16(baseAddr, offset, (char)0); + // return; + // } + // else + // { + // Marshal.WriteInt16(baseAddr, offset, item[i]); + // offset += 2; + // } + // } + // // when ended normally also write \0 + // Marshal.WriteByte(baseAddr, offset, 0); + // } + //} + + #endregion + + } +} diff --git a/NTwain/Data/TypeReader.cs b/NTwain/Data/TypeReader.cs deleted file mode 100644 index 19638e2..0000000 --- a/NTwain/Data/TypeReader.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; - -namespace NTwain.Data -{ - /// - /// Class to read common TWAIN values. - /// - public static class TypeReader - { - - /// - /// Reads a TWAIN value. - /// - /// The base address. - /// The offset. - /// The TWAIN type. - /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "1#")] - public static object ReadValue(IntPtr baseAddress, ref int offset, ItemType type) - { - object val = null; - switch (type) - { - case ItemType.Int8: - val = (sbyte)Marshal.ReadByte(baseAddress, offset); - break; - case ItemType.UInt8: - val = Marshal.ReadByte(baseAddress, offset); - break; - case ItemType.Bool: - case ItemType.UInt16: - val = (ushort)Marshal.ReadInt16(baseAddress, offset); - break; - case ItemType.Int16: - val = Marshal.ReadInt16(baseAddress, offset); - break; - case ItemType.UInt32: - val = (uint)Marshal.ReadInt32(baseAddress, offset); - break; - case ItemType.Int32: - val = Marshal.ReadInt32(baseAddress, offset); - break; - case ItemType.Fix32: - TWFix32 f32 = new TWFix32(); - f32.Whole = Marshal.ReadInt16(baseAddress, offset); - f32.Fraction = (ushort)Marshal.ReadInt16(baseAddress, offset + 2); - val = f32; - break; - case ItemType.Frame: - TWFrame frame = new TWFrame(); - frame.Left = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); - frame.Top = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); - frame.Right = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); - frame.Bottom = (TWFix32)ReadValue(baseAddress, ref offset, ItemType.Fix32); - return frame; // no need to update offset again after reading fix32 - case ItemType.String128: - case ItemType.String255: - case ItemType.String32: - case ItemType.String64: - val = Marshal.PtrToStringAnsi(new IntPtr(baseAddress.ToInt64() + offset)); - break; - case ItemType.Handle: - val = Marshal.ReadIntPtr(baseAddress, offset); - break; - } - offset += GetItemTypeSize(type); - return val; - } - - static readonly Dictionary __sizes = GenerateSizes(); - - private static Dictionary GenerateSizes() - { - var sizes = new Dictionary(); - sizes[ItemType.Int8] = 1; - sizes[ItemType.UInt8] = 1; - sizes[ItemType.Int16] = 2; - sizes[ItemType.UInt16] = 2; - sizes[ItemType.Bool] = 2; - sizes[ItemType.Int32] = 4; - sizes[ItemType.UInt32] = 4; - sizes[ItemType.Fix32] = 4; - sizes[ItemType.Frame] = 16; - sizes[ItemType.String32] = TwainConst.String32; - sizes[ItemType.String64] = TwainConst.String64; - sizes[ItemType.String128] = TwainConst.String128; - sizes[ItemType.String255] = TwainConst.String255; - // TODO: find out if it should be fixed 4 bytes or intptr size - sizes[ItemType.Handle] = IntPtr.Size; - - return sizes; - } - - /// - /// Gets the byte size of the item type. - /// - /// - /// - public static int GetItemTypeSize(ItemType type) - { - if (__sizes.ContainsKey(type)) - { - return __sizes[type]; - } - return 0; - } - } -} diff --git a/NTwain/Data/ValueExtensions.cs b/NTwain/Data/ValueExtensions.cs index c761cbc..18cfdcc 100644 --- a/NTwain/Data/ValueExtensions.cs +++ b/NTwain/Data/ValueExtensions.cs @@ -6,7 +6,7 @@ using System.Linq; namespace NTwain.Data { /// - /// Utility on converting (possibly bad) integer values to enum values. + /// Contains extension methods for converting (possibly bad) integer values to enum values. /// public static class ValueExtensions { diff --git a/NTwain/DataSource.Caps.cs b/NTwain/DataSource.Caps.cs index 20cbc6b..0dc68ec 100644 --- a/NTwain/DataSource.Caps.cs +++ b/NTwain/DataSource.Caps.cs @@ -131,7 +131,7 @@ namespace NTwain var rc = _session.DGControl.Capability.Get(cap); if (rc == ReturnCode.Success) { - cap.ReadMultiCapValues(list); + CapabilityReader.ReadValue(cap).PopulateFromCapValues(list); } } return list; diff --git a/NTwain/Internals/TransferLogic.cs b/NTwain/Internals/TransferLogic.cs index b6c69d0..ac33c88 100644 --- a/NTwain/Internals/TransferLogic.cs +++ b/NTwain/Internals/TransferLogic.cs @@ -68,14 +68,7 @@ namespace NTwain.Internals // some DS end up getting none but we will assume it's image if (xferImage) { - // default to native - var mech = XferMech.Native; - - object mechRaw = session.CurrentSource.CapGetCurrent(CapabilityId.ICapXferMech); - if (mechRaw != null) - { - mech = mechRaw.ConvertToEnum(); - } + var mech = session.CurrentSource.ICapXferMech.GetCurrent(); switch (mech) { @@ -97,7 +90,7 @@ namespace NTwain.Internals } if (xferAudio) { - var mech = session.CurrentSource.CapGetCurrent(CapabilityId.ACapXferMech).ConvertToEnum(); + var mech = session.CurrentSource.ACapXferMech.GetCurrent(); switch (mech) { case XferMech.File: diff --git a/NTwain/NTwain.csproj b/NTwain/NTwain.csproj index 6a1fd8f..b3e67f6 100644 --- a/NTwain/NTwain.csproj +++ b/NTwain/NTwain.csproj @@ -57,7 +57,7 @@ - +