Moved TWAIN type reading/writing under one class.

This commit is contained in:
soukoku 2015-01-11 13:16:02 -05:00
parent 8d30e9066f
commit 5fad8b3c02
10 changed files with 313 additions and 323 deletions

View File

@ -69,8 +69,8 @@
<Compile Include="..\NTwain\Data\TwainValues.cs">
<Link>Data\TwainValues.cs</Link>
</Compile>
<Compile Include="..\NTwain\Data\TypeReader.cs">
<Link>Data\TypeReader.cs</Link>
<Compile Include="..\NTwain\Data\TypeExtensions.cs">
<Link>Data\TypeExtensions.cs</Link>
</Compile>
<Compile Include="..\NTwain\Data\ValueExtensions.cs">
<Link>Data\ValueExtensions.cs</Link>

View File

@ -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);
}
}
}

View File

@ -179,12 +179,50 @@ namespace NTwain
#region reader methods
/// <summary>
/// Don't care what contain it is, just populates the specified list with the capability values (count be one or many).
/// </summary>
/// <param name="toPopulate">The list to populate the values.</param>
/// <returns></returns>
public IList<object> PopulateFromCapValues(IList<object> toPopulate)
{
if (toPopulate == null) { toPopulate = new List<object>(); }
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;

View File

@ -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
/// <summary>
/// Entry call for writing values to a pointer.
/// </summary>
/// <param name="baseAddr"></param>
/// <param name="offset"></param>
/// <param name="type"></param>
/// <param name="value"></param>
[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);
}
/// <summary>
/// Writes string value.
/// </summary>
/// <param name="baseAddr"></param>
/// <param name="offset"></param>
/// <param name="item"></param>
/// <param name="maxLength"></param>
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);
}
}
///// <summary>
///// Writes unicode string value.
///// </summary>
///// <param name="baseAddr"></param>
///// <param name="offset"></param>
///// <param name="item"></param>
///// <param name="maxLength"></param>
//[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
/// <summary>
/// A general method that returns the data in a <see cref="TWCapability" />.
/// </summary>
/// <param name="toPopulate">The list to populate if necessary.</param>
/// <returns></returns>
public IList<object> ReadMultiCapValues(IList<object> toPopulate)
{
if (toPopulate == null) { toPopulate = new List<object>(); }
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;
}
}
/// <summary>
@ -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
{

View File

@ -0,0 +1,249 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
namespace NTwain.Data
{
/// <summary>
/// Contains extensions for reading/writing TWAIN types from/to pointer.
/// </summary>
public static class TypeExtensions
{
static readonly IDictionary<ItemType, int> __sizes = new Dictionary<ItemType, int>
{
{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},
};
/// <summary>
/// Gets the byte size of the item type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static int GetItemTypeSize(this ItemType type)
{
if (__sizes.ContainsKey(type))
{
return __sizes[type];
}
return 0;
}
/// <summary>
/// Reads a TWAIN value.
/// </summary>
/// <param name="baseAddress">The base address.</param>
/// <param name="offset">The offset.</param>
/// <param name="type">The TWAIN type.</param>
/// <returns></returns>
[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
/// <summary>
/// Writes a TWAIN value.
/// </summary>
/// <param name="baseAddr">The base addr.</param>
/// <param name="offset">The offset.</param>
/// <param name="type">The TWAIN type.</param>
/// <param name="value">The value.</param>
[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);
}
/// <summary>
/// Writes string value. THIS MAY BE WRONG.
/// </summary>
/// <param name="baseAddr"></param>
/// <param name="offset"></param>
/// <param name="item"></param>
/// <param name="maxLength"></param>
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);
}
}
///// <summary>
///// Writes unicode string value.
///// </summary>
///// <param name="baseAddr"></param>
///// <param name="offset"></param>
///// <param name="item"></param>
///// <param name="maxLength"></param>
//[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
}
}

View File

@ -1,110 +0,0 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace NTwain.Data
{
/// <summary>
/// Class to read common TWAIN values.
/// </summary>
public static class TypeReader
{
/// <summary>
/// Reads a TWAIN value.
/// </summary>
/// <param name="baseAddress">The base address.</param>
/// <param name="offset">The offset.</param>
/// <param name="type">The TWAIN type.</param>
/// <returns></returns>
[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<ItemType, int> __sizes = GenerateSizes();
private static Dictionary<ItemType, int> GenerateSizes()
{
var sizes = new Dictionary<ItemType, int>();
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;
}
/// <summary>
/// Gets the byte size of the item type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static int GetItemTypeSize(ItemType type)
{
if (__sizes.ContainsKey(type))
{
return __sizes[type];
}
return 0;
}
}
}

View File

@ -6,7 +6,7 @@ using System.Linq;
namespace NTwain.Data
{
/// <summary>
/// Utility on converting (possibly bad) integer values to enum values.
/// Contains extension methods for converting (possibly bad) integer values to enum values.
/// </summary>
public static class ValueExtensions
{

View File

@ -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;

View File

@ -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<XferMech>();
}
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<XferMech>();
var mech = session.CurrentSource.ACapXferMech.GetCurrent();
switch (mech)
{
case XferMech.File:

View File

@ -57,7 +57,7 @@
<ItemGroup>
<Compile Include="CapWrapper.cs" />
<Compile Include="CapabilityReader.cs" />
<Compile Include="Data\TypeReader.cs" />
<Compile Include="Data\TypeExtensions.cs" />
<Compile Include="Data\TwainTypesExtended.cs" />
<Compile Include="DeviceEventArgs.cs" />
<Compile Include="IDataSource.cs" />