Transfer logic tweaks for #42

This commit is contained in:
soukoku 2015-03-27 07:30:37 -04:00
parent abd3c9c65c
commit 1700e183c7
4 changed files with 101 additions and 62 deletions

View File

@ -297,6 +297,14 @@ namespace NTwain
}
}
internal DGAudio DGAudio
{
get
{
return _session.DGAudio;
}
}
#endregion
//#region INotifyPropertyChanged Members

View File

@ -23,25 +23,21 @@ namespace NTwain
/// </summary>
/// <param name="source">The source.</param>
/// <param name="nativeData">The native data.</param>
/// <param name="imageInfo">The image information.</param>
public DataTransferredEventArgs(DataSource source, IntPtr nativeData, TWImageInfo imageInfo)
public DataTransferredEventArgs(DataSource source, IntPtr nativeData)
{
DataSource = source;
NativeData = nativeData;
ImageInfo = imageInfo;
}
/// <summary>
/// Initializes a new instance of the <see cref="DataTransferredEventArgs"/> class.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="fileDataPath">The file data path.</param>
/// <param name="imageInfo">The image information.</param>
/// <param name="imageFileFormat">The image file format.</param>
public DataTransferredEventArgs(DataSource source, string fileDataPath, TWImageInfo imageInfo, FileFormat imageFileFormat)
public DataTransferredEventArgs(DataSource source, string fileDataPath, FileFormat imageFileFormat)
{
DataSource = source;
FileDataPath = fileDataPath;
ImageInfo = imageInfo;
ImageFileFormat = imageFileFormat;
}
/// <summary>
@ -49,12 +45,10 @@ namespace NTwain
/// </summary>
/// <param name="source">The source.</param>
/// <param name="memoryData">The memory data.</param>
/// <param name="imageInfo">The image information.</param>
public DataTransferredEventArgs(DataSource source, byte[] memoryData, TWImageInfo imageInfo)
public DataTransferredEventArgs(DataSource source, byte[] memoryData)
{
DataSource = source;
MemoryData = memoryData;
ImageInfo = imageInfo;
}
/// <summary>
@ -94,13 +88,27 @@ namespace NTwain
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public byte[] MemoryData { get; private set; }
TWImageInfo _imgInfo;
/// <summary>
/// Gets the final image information if applicable.
/// </summary>
/// <value>
/// The final image information.
/// </value>
public TWImageInfo ImageInfo { get; private set; }
public TWImageInfo ImageInfo
{
get
{
if (_imgInfo == null)
{
if (DataSource.DGImage.ImageInfo.Get(out _imgInfo) != ReturnCode.Success)
{
_imgInfo = null;
}
}
return _imgInfo;
}
}
/// <summary>
/// Gets the data source.

View File

@ -19,37 +19,37 @@ namespace NTwain.Internals
/// </summary>
public static void DoTransferRoutine(ITwainSessionInternal session)
{
#region get xfer types
bool xferImage = true; // default to always xfer image
bool xferAudio = false;
DataGroups xferGroup = DataGroups.None;
XferMech imgXferMech = XferMech.Native;
XferMech audXferMech = XferMech.Native;
if (session.DGControl.XferGroup.Get(ref xferGroup) == ReturnCode.Success)
{
xferAudio = (xferGroup & DataGroups.Audio) == DataGroups.Audio;
xferImage = xferGroup == DataGroups.None || (xferGroup & DataGroups.Image) == DataGroups.Image;
}
// some DS end up getting none but we will assume it's image
if (xferImage)
{
imgXferMech = session.CurrentSource.Capabilities.ICapXferMech.GetCurrent();
}
if (xferAudio)
{
var mech = session.CurrentSource.Capabilities.ACapXferMech.GetCurrent();
}
#endregion
var pending = new TWPendingXfers();
//var rc = ReturnCode.Success;
var rc = session.DGControl.PendingXfers.Get(pending);
do
{
#region build and raise xfer ready
#region raise xfer ready
bool xferImage = true; // default to always xfer image
bool xferAudio = false;
DataGroups xferGroup = DataGroups.None;
if (session.DGControl.XferGroup.Get(ref xferGroup) == ReturnCode.Success)
{
xferAudio = (xferGroup & DataGroups.Audio) == DataGroups.Audio;
xferImage = xferGroup == DataGroups.None || (xferGroup & DataGroups.Image) == DataGroups.Image;
}
TWAudioInfo audInfo = null;
if (xferAudio && session.DGAudio.AudioInfo.Get(out audInfo) != ReturnCode.Success)
{
audInfo = null;
}
TWImageInfo imgInfo = null;
if (xferImage && session.DGImage.ImageInfo.Get(out imgInfo) != ReturnCode.Success)
{
imgInfo = null;
}
// ask consumer for xfer details
var preXferArgs = new TransferReadyEventArgs(pending.Count, pending.EndOfJob == 0, imgInfo, audInfo); ;
var preXferArgs = new TransferReadyEventArgs(session.CurrentSource, pending.Count, pending.EndOfJob == 0); ;
session.SafeSyncableRaiseEvent(preXferArgs);
#endregion
@ -64,13 +64,9 @@ namespace NTwain.Internals
{
if (!preXferArgs.CancelCurrent)
{
// some DS end up getting none but we will assume it's image
if (xferImage)
{
var mech = session.CurrentSource.Capabilities.ICapXferMech.GetCurrent();
switch (mech)
switch (imgXferMech)
{
case XferMech.Memory:
DoImageMemoryXfer(session);
@ -85,13 +81,11 @@ namespace NTwain.Internals
default: // always assume native
DoImageNativeXfer(session);
break;
}
}
if (xferAudio)
{
var mech = session.CurrentSource.Capabilities.ACapXferMech.GetCurrent();
switch (mech)
switch (audXferMech)
{
case XferMech.File:
DoAudioFileXfer(session);
@ -135,7 +129,7 @@ namespace NTwain.Internals
lockedPtr = PlatformInfo.Current.MemoryManager.Lock(dataPtr);
}
session.SafeSyncableRaiseEvent(new DataTransferredEventArgs(session.CurrentSource, lockedPtr, null));
session.SafeSyncableRaiseEvent(new DataTransferredEventArgs(session.CurrentSource, lockedPtr));
}
else
{
@ -176,7 +170,7 @@ namespace NTwain.Internals
var xrc = session.DGAudio.AudioFileXfer.Get();
if (xrc == ReturnCode.XferDone)
{
session.SafeSyncableRaiseEvent(new DataTransferredEventArgs(session.CurrentSource, filePath, null, (FileFormat)0));
session.SafeSyncableRaiseEvent(new DataTransferredEventArgs(session.CurrentSource, filePath, (FileFormat)0));
}
else
{
@ -421,23 +415,18 @@ namespace NTwain.Internals
static void DoImageXferredEventRoutine(ITwainSessionInternal session, IntPtr dataPtr, byte[] dataArray, string filePath, FileFormat format)
{
DataTransferredEventArgs args = null;
TWImageInfo imgInfo;
if (session.DGImage.ImageInfo.Get(out imgInfo) != ReturnCode.Success)
{
imgInfo = null;
}
if (dataPtr != IntPtr.Zero)
{
args = new DataTransferredEventArgs(session.CurrentSource, dataPtr, imgInfo);
args = new DataTransferredEventArgs(session.CurrentSource, dataPtr);
}
else if (dataArray != null)
{
args = new DataTransferredEventArgs(session.CurrentSource, dataArray, imgInfo);
args = new DataTransferredEventArgs(session.CurrentSource, dataArray);
}
else
{
args = new DataTransferredEventArgs(session.CurrentSource, filePath, imgInfo, format);
args = new DataTransferredEventArgs(session.CurrentSource, filePath, format);
}
session.SafeSyncableRaiseEvent(args);
}

View File

@ -11,18 +11,24 @@ namespace NTwain
/// <summary>
/// Initializes a new instance of the <see cref="TransferReadyEventArgs"/> class.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="pendingCount">The pending count.</param>
/// <param name="endOfJob">if set to <c>true</c> [end of job].</param>
/// <param name="imageInfo">The image information.</param>
/// <param name="audioInfo">The audio information.</param>
public TransferReadyEventArgs(int pendingCount, bool endOfJob, TWImageInfo imageInfo, TWAudioInfo audioInfo)
/// <param name="endOfJob">if set to <c>true</c> then it's the end of job.</param>
public TransferReadyEventArgs(DataSource source, int pendingCount, bool endOfJob)
{
DataSource = source;
PendingTransferCount = pendingCount;
EndOfJob = endOfJob;
PendingImageInfo = imageInfo;
AudioInfo = audioInfo;
}
/// <summary>
/// Gets the data source.
/// </summary>
/// <value>
/// The data source.
/// </value>
public DataSource DataSource { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether the current transfer should be canceled
/// and continue next transfer if there are more data.
@ -49,6 +55,7 @@ namespace NTwain
/// <value>The pending count.</value>
public int PendingTransferCount { get; private set; }
TWImageInfo _imgInfo;
/// <summary>
/// Gets the tentative image information for the current transfer if applicable.
/// This may differ from the final image depending on the transfer mode used (mostly when doing mem xfer).
@ -56,15 +63,42 @@ namespace NTwain
/// <value>
/// The image info.
/// </value>
public TWImageInfo PendingImageInfo { get; private set; }
public TWImageInfo PendingImageInfo
{
get
{
if (_imgInfo == null)
{
if (DataSource.DGImage.ImageInfo.Get(out _imgInfo) != ReturnCode.Success)
{
_imgInfo = null;
}
}
return _imgInfo;
}
}
TWAudioInfo _audInfo;
/// <summary>
/// Gets the audio information for the current transfer if applicable.
/// </summary>
/// <value>
/// The audio information.
/// </value>
public TWAudioInfo AudioInfo { get; private set; }
public TWAudioInfo AudioInfo
{
get
{
if (_audInfo == null)
{
if (DataSource.DGAudio.AudioInfo.Get(out _audInfo) != ReturnCode.Success)
{
_audInfo = null;
}
}
return _audInfo;
}
}
}
}