mirror of
https://github.com/soukoku/ntwain.git
synced 2025-11-07 18:14:46 +08:00
Road to v1 begins!
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using NTwain.Properties;
|
||||
using NTwain.Values;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
@@ -32,7 +31,7 @@ namespace NTwain.Data
|
||||
IntPtr baseAddr = IntPtr.Zero;
|
||||
try
|
||||
{
|
||||
baseAddr = MemoryManager.Instance.Lock(capability.Container);
|
||||
baseAddr = Platform.MemoryManager.Lock(capability.Container);
|
||||
switch (capability.ContainerType)
|
||||
{
|
||||
case ContainerType.Array:
|
||||
@@ -63,7 +62,7 @@ namespace NTwain.Data
|
||||
{
|
||||
if (baseAddr != IntPtr.Zero)
|
||||
{
|
||||
MemoryManager.Instance.Unlock(baseAddr);
|
||||
Platform.MemoryManager.Unlock(baseAddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -87,7 +86,7 @@ namespace NTwain.Data
|
||||
public ItemType ItemType { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the one value if container is <see cref="NTwain.Values.ContainerType.Array"/>.
|
||||
/// Gets the one value if container is <see cref="NTwain.ContainerType.Array"/>.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The one value.
|
||||
@@ -95,7 +94,7 @@ namespace NTwain.Data
|
||||
public object OneValue { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection values if container is <see cref="NTwain.Values.ContainerType.Enum"/> or <see cref="NTwain.Values.ContainerType.Range"/> .
|
||||
/// Gets the collection values if container is <see cref="NTwain.ContainerType.Enum"/> or <see cref="NTwain.ContainerType.Range"/> .
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The collection values.
|
||||
@@ -107,11 +106,11 @@ namespace NTwain.Data
|
||||
#region enum prop
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current value index if container is <see cref="NTwain.Values.ContainerType.Enum"/>.
|
||||
/// Gets the current value index if container is <see cref="NTwain.ContainerType.Enum"/>.
|
||||
/// </summary>
|
||||
public int EnumCurrentIndex { get; private set; }
|
||||
/// <summary>
|
||||
/// Gets the default value index if container is <see cref="NTwain.Values.ContainerType.Enum" />.
|
||||
/// Gets the default value index if container is <see cref="NTwain.ContainerType.Enum" />.
|
||||
/// </summary>
|
||||
public int EnumDefaultIndex { get; private set; }
|
||||
|
||||
@@ -120,14 +119,14 @@ namespace NTwain.Data
|
||||
#region range prop
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current value if container is <see cref="NTwain.Values.ContainerType.Range" />.
|
||||
/// Gets the current value if container is <see cref="NTwain.ContainerType.Range" />.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The range current value.
|
||||
/// </value>
|
||||
public object RangeCurrentValue { get; private set; }
|
||||
/// <summary>
|
||||
/// Gets the default value if container is <see cref="NTwain.Values.ContainerType.Range" />.
|
||||
/// Gets the default value if container is <see cref="NTwain.ContainerType.Range" />.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The range default value.
|
||||
|
||||
21
NTwain/Data/SourceEnableMode.cs
Normal file
21
NTwain/Data/SourceEnableMode.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace NTwain.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates how the source should be enabled in a TWAIN session.
|
||||
/// </summary>
|
||||
public enum SourceEnableMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Start acquiring without driver UI.
|
||||
/// </summary>
|
||||
NoUI,
|
||||
/// <summary>
|
||||
/// Start acquiring with driver UI.
|
||||
/// </summary>
|
||||
ShowUI,
|
||||
/// <summary>
|
||||
/// Show driver UI for settings change but no acquisition.
|
||||
/// </summary>
|
||||
ShowUIOnly
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
// This file contains all the structs defined in the twain.h file.
|
||||
|
||||
using NTwain.Values;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using TW_BOOL = System.UInt16; // unsigned short
|
||||
@@ -1,5 +1,5 @@
|
||||
using NTwain.Properties;
|
||||
using NTwain.Values;
|
||||
using NTwain.Internals;
|
||||
using NTwain.Properties;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
@@ -625,7 +625,7 @@ namespace NTwain.Data
|
||||
public TWCapability(CapabilityId capability)
|
||||
{
|
||||
Capability = capability;
|
||||
ContainerType = Values.ContainerType.DontCare;
|
||||
ContainerType = ContainerType.DontCare;
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TWCapability" /> class.
|
||||
@@ -636,7 +636,7 @@ namespace NTwain.Data
|
||||
public TWCapability(CapabilityId capability, TWOneValue value)
|
||||
{
|
||||
Capability = capability;
|
||||
ContainerType = Values.ContainerType.OneValue;
|
||||
ContainerType = ContainerType.OneValue;
|
||||
SetOneValue(value);
|
||||
}
|
||||
/// <summary>
|
||||
@@ -648,7 +648,7 @@ namespace NTwain.Data
|
||||
public TWCapability(CapabilityId capability, TWEnumeration value)
|
||||
{
|
||||
Capability = capability;
|
||||
ContainerType = Values.ContainerType.Enum;
|
||||
ContainerType = ContainerType.Enum;
|
||||
SetEnumValue(value);
|
||||
}
|
||||
/// <summary>
|
||||
@@ -660,7 +660,7 @@ namespace NTwain.Data
|
||||
public TWCapability(CapabilityId capability, TWRange value)
|
||||
{
|
||||
Capability = capability;
|
||||
ContainerType = Values.ContainerType.Range;
|
||||
ContainerType = ContainerType.Range;
|
||||
SetRangeValue(value);
|
||||
}
|
||||
#endregion
|
||||
@@ -676,7 +676,7 @@ namespace NTwain.Data
|
||||
/// will be one of four types: <see cref="TWArray"/>, <see cref="TWEnumeration"/>,
|
||||
/// <see cref="TWOneValue"/>, or <see cref="TWRange"/>.
|
||||
/// </summary>
|
||||
public ContainerType ContainerType { get { return (Values.ContainerType)_conType; } set { _conType = (ushort)value; } }
|
||||
public ContainerType ContainerType { get { return (ContainerType)_conType; } set { _conType = (ushort)value; } }
|
||||
|
||||
internal IntPtr Container { get { return _hContainer; } }
|
||||
|
||||
@@ -688,12 +688,12 @@ namespace NTwain.Data
|
||||
void SetOneValue(TWOneValue value)
|
||||
{
|
||||
if (value == null) { throw new ArgumentNullException("value"); }
|
||||
ContainerType = Values.ContainerType.OneValue;
|
||||
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(Resources.BadValueType, "TWOneValue")); }
|
||||
|
||||
_hContainer = MemoryManager.Instance.Allocate((uint)Marshal.SizeOf(value));
|
||||
_hContainer = Platform.MemoryManager.Allocate((uint)Marshal.SizeOf(value));
|
||||
if (_hContainer != IntPtr.Zero)
|
||||
{
|
||||
Marshal.StructureToPtr(value, _hContainer, false);
|
||||
@@ -704,14 +704,14 @@ namespace NTwain.Data
|
||||
void SetEnumValue(TWEnumeration value)
|
||||
{
|
||||
if (value == null) { throw new ArgumentNullException("value"); }
|
||||
ContainerType = Values.ContainerType.Enum;
|
||||
ContainerType = ContainerType.Enum;
|
||||
|
||||
|
||||
Int32 valueSize = TWEnumeration.ItemOffset + value.ItemList.Length * TypeReader.GetItemTypeSize(value.ItemType);
|
||||
|
||||
int offset = 0;
|
||||
_hContainer = MemoryManager.Instance.Allocate((uint)valueSize);
|
||||
IntPtr baseAddr = MemoryManager.Instance.Lock(_hContainer);
|
||||
_hContainer = Platform.MemoryManager.Allocate((uint)valueSize);
|
||||
IntPtr baseAddr = Platform.MemoryManager.Lock(_hContainer);
|
||||
|
||||
// can't safely use StructureToPtr here so write it our own
|
||||
WriteValue(baseAddr, ref offset, ItemType.UInt16, value.ItemType);
|
||||
@@ -722,7 +722,7 @@ namespace NTwain.Data
|
||||
{
|
||||
WriteValue(baseAddr, ref offset, value.ItemType, item);
|
||||
}
|
||||
MemoryManager.Instance.Unlock(baseAddr);
|
||||
Platform.MemoryManager.Unlock(baseAddr);
|
||||
}
|
||||
|
||||
|
||||
@@ -730,12 +730,12 @@ namespace NTwain.Data
|
||||
void SetRangeValue(TWRange value)
|
||||
{
|
||||
if (value == null) { throw new ArgumentNullException("value"); }
|
||||
ContainerType = Values.ContainerType.Range;
|
||||
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(Resources.BadValueType, "TWRange")); }
|
||||
|
||||
_hContainer = MemoryManager.Instance.Allocate((uint)Marshal.SizeOf(value));
|
||||
_hContainer = Platform.MemoryManager.Allocate((uint)Marshal.SizeOf(value));
|
||||
if (_hContainer != IntPtr.Zero)
|
||||
{
|
||||
Marshal.StructureToPtr(value, _hContainer, false);
|
||||
@@ -899,7 +899,7 @@ namespace NTwain.Data
|
||||
if (disposing) { }
|
||||
if (_hContainer != IntPtr.Zero)
|
||||
{
|
||||
MemoryManager.Instance.Free(_hContainer);
|
||||
Platform.MemoryManager.Free(_hContainer);
|
||||
_hContainer = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
@@ -1376,7 +1376,7 @@ namespace NTwain.Data
|
||||
{
|
||||
// uintptr to intptr could be bad
|
||||
var ptr = new IntPtr(BitConverter.ToInt64(BitConverter.GetBytes(it.Item.ToUInt64()), 0));
|
||||
MemoryManager.Instance.Free(ptr);
|
||||
Platform.MemoryManager.Free(ptr);
|
||||
}
|
||||
}
|
||||
it.Item = UIntPtr.Zero;
|
||||
@@ -1794,12 +1794,12 @@ namespace NTwain.Data
|
||||
/// color is called Rgb, for R-G-B color. There is no
|
||||
/// default for this value.
|
||||
/// </summary>
|
||||
public PixelType PixelType { get { return (Values.PixelType)_pixelType; } }
|
||||
public PixelType PixelType { get { return (PixelType)_pixelType; } }
|
||||
/// <summary>
|
||||
/// The compression method used to process the data being transferred.
|
||||
/// Default is no compression.
|
||||
/// </summary>
|
||||
public CompressionType Compression { get { return (Values.CompressionType)_compression; } }
|
||||
public CompressionType Compression { get { return (CompressionType)_compression; } }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -2266,7 +2266,7 @@ namespace NTwain.Data
|
||||
}
|
||||
if (_uTF8string != IntPtr.Zero)
|
||||
{
|
||||
MemoryManager.Instance.Free(_uTF8string);
|
||||
Platform.MemoryManager.Free(_uTF8string);
|
||||
_uTF8string = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
@@ -2312,34 +2312,39 @@ namespace NTwain.Data
|
||||
/// <summary>
|
||||
/// Provides entry points required by TWAIN 2.0 Applications and Sources.
|
||||
/// </summary>
|
||||
partial class TWEntryPoint
|
||||
partial class TWEntryPoint : IMemoryManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TWEntryPoint"/> class.
|
||||
/// </summary>
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
public TWEntryPoint()
|
||||
{
|
||||
_size = (uint)Marshal.SizeOf(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The memory allocation function.
|
||||
/// </summary>
|
||||
public MemAllocateDelegate AllocateFunction { get { return _dSM_MemAllocate; } }
|
||||
/// <summary>
|
||||
/// The memory free function.
|
||||
/// </summary>
|
||||
public MemFreeDelegate FreeFunction { get { return _dSM_MemFree; } }
|
||||
/// <summary>
|
||||
/// The memory lock function.
|
||||
/// </summary>
|
||||
public MemLockDelegate LockFunction { get { return _dSM_MemLock; } }
|
||||
/// <summary>
|
||||
/// The memory unlock function.
|
||||
/// </summary>
|
||||
public MemUnlockDelegate UnlockFunction { get { return _dSM_MemUnlock; } }
|
||||
#region IMemoryManager Members
|
||||
|
||||
public IntPtr Allocate(uint size)
|
||||
{
|
||||
return _dSM_MemAllocate(size);
|
||||
}
|
||||
|
||||
public void Free(IntPtr handle)
|
||||
{
|
||||
_dSM_MemFree(handle);
|
||||
}
|
||||
|
||||
public IntPtr Lock(IntPtr handle)
|
||||
{
|
||||
return _dSM_MemLock(handle);
|
||||
}
|
||||
|
||||
public void Unlock(IntPtr handle)
|
||||
{
|
||||
_dSM_MemUnlock(handle);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
2053
NTwain/Data/TwainValues.cs
Normal file
2053
NTwain/Data/TwainValues.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,4 @@
|
||||
using NTwain.Values;
|
||||
using System;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NTwain.Data
|
||||
|
||||
84
NTwain/Data/ValueConverter.cs
Normal file
84
NTwain/Data/ValueConverter.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NTwain.Data
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility on converting (possibly bad) integer values to enum values.
|
||||
/// </summary>
|
||||
public static class ValueConverter
|
||||
{
|
||||
public static IList<T> CastToEnum<T>(this IEnumerable<object> list) where T : struct,IConvertible
|
||||
{
|
||||
return list.CastToEnum<T>(true);
|
||||
}
|
||||
public static IList<T> CastToEnum<T>(this IEnumerable<object> list, bool tryUpperWord) where T : struct,IConvertible
|
||||
{
|
||||
return list.Select(o => o.ConvertToEnum<T>(tryUpperWord)).ToList();
|
||||
}
|
||||
|
||||
public static T ConvertToEnum<T>(this object value) where T : struct,IConvertible
|
||||
{
|
||||
return ConvertToEnum<T>(value, true);
|
||||
}
|
||||
public static T ConvertToEnum<T>(this object value, bool tryUpperWord) where T : struct,IConvertible
|
||||
{
|
||||
var returnType = typeof(T);
|
||||
|
||||
// standard int values
|
||||
if (returnType.IsEnum)
|
||||
{
|
||||
if (tryUpperWord)
|
||||
{
|
||||
// small routine to work with bad sources that may put
|
||||
// 16bit value in the upper word instead of lower word (as per the twain spec).
|
||||
var rawType = Enum.GetUnderlyingType(returnType);
|
||||
if (typeof(ushort).IsAssignableFrom(rawType))
|
||||
{
|
||||
var intVal = Convert.ToUInt32(value);
|
||||
var enumVal = GetLowerWord(intVal);
|
||||
if (!Enum.IsDefined(returnType, enumVal))
|
||||
{
|
||||
return (T)Enum.ToObject(returnType, GetUpperWord(intVal));
|
||||
}
|
||||
}
|
||||
}
|
||||
// this may work better?
|
||||
return (T)Enum.ToObject(returnType, value);
|
||||
//// cast to underlying type first then to the enum
|
||||
//return (T)Convert.ChangeType(value, rawType);
|
||||
}
|
||||
else if (typeof(IConvertible).IsAssignableFrom(returnType))
|
||||
{
|
||||
// for regular integers and whatnot
|
||||
return (T)Convert.ChangeType(value, returnType);
|
||||
}
|
||||
// return as-is from cap. if caller made a mistake then there should be exceptions
|
||||
return (T)value;
|
||||
}
|
||||
|
||||
static ushort GetLowerWord(uint value)
|
||||
{
|
||||
return (ushort)(value & 0xffff);
|
||||
}
|
||||
static uint GetUpperWord(uint value)
|
||||
{
|
||||
return (ushort)(value >> 16);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to convert to a value to <see cref="TWFix32"/> if possible.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns></returns>
|
||||
public static TWFix32 ConvertToFix32(this object value)
|
||||
{
|
||||
if (value is TWFix32)
|
||||
{
|
||||
return (TWFix32)value;
|
||||
}
|
||||
return (TWFix32)Convert.ToSingle(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user