Tweak native memory image getter.

This commit is contained in:
Eugene Wang 2023-04-07 21:20:41 -04:00
parent 416c3a80b0
commit ebe277e614
3 changed files with 29 additions and 25 deletions

View File

@ -3,6 +3,9 @@ using System;
namespace NTwain namespace NTwain
{ {
// TODO: a 2-level dispose with end of event method
// and manual dispose for perf if this is not good enough.
public class DataTransferredEventArgs : EventArgs public class DataTransferredEventArgs : EventArgs
{ {
readonly TwainAppSession _twain; readonly TwainAppSession _twain;
@ -12,12 +15,13 @@ namespace NTwain
/// Ctor for array data; /// Ctor for array data;
/// </summary> /// </summary>
/// <param name="twain"></param> /// <param name="twain"></param>
/// <param name="isImage"></param>
/// <param name="data"></param> /// <param name="data"></param>
internal DataTransferredEventArgs(TwainAppSession twain, byte[] data, bool isImage) internal DataTransferredEventArgs(TwainAppSession twain, bool isImage, byte[] data)
{ {
this._twain = twain; _twain = twain;
_isImage = isImage;
Data = data; Data = data;
this._isImage = isImage;
} }
@ -30,25 +34,17 @@ namespace NTwain
public byte[]? Data { get; } public byte[]? Data { get; }
TW_IMAGEINFO? _imgInfo;
/// <summary> /// <summary>
/// Gets the final image information if applicable. /// Gets the final image information if transfer was an image.
/// </summary> /// </summary>
public TW_IMAGEINFO? ImageInfo public TW_IMAGEINFO? GetImageInfo()
{ {
get if (_isImage && _twain.GetImageInfo(out TW_IMAGEINFO info).RC == TWRC.SUCCESS)
{ {
if (_isImage && _imgInfo == null) return info;
{
if (_twain.GetImageInfo(out TW_IMAGEINFO info).RC == TWRC.SUCCESS)
{
_imgInfo = info;
}
}
return _imgInfo;
}
} }
return null;
} }
}
} }

View File

@ -24,7 +24,7 @@ namespace NTwain.Native
} }
public static byte[]? GetBitmapData(System.Buffers.ArrayPool<byte> xferMemPool, IntPtr data) public static unsafe byte[]? GetBitmapData(System.Buffers.ArrayPool<byte> xferMemPool, IntPtr data)
{ {
var infoHeader = (BITMAPINFOHEADER)Marshal.PtrToStructure(data, typeof(BITMAPINFOHEADER)); var infoHeader = (BITMAPINFOHEADER)Marshal.PtrToStructure(data, typeof(BITMAPINFOHEADER));
if (infoHeader.Validate()) if (infoHeader.Validate())
@ -43,12 +43,20 @@ namespace NTwain.Native
var dataCopy = xferMemPool.Rent((int)fileHeader.bfSize);// new byte[fileHeader.bfSize]; var dataCopy = xferMemPool.Rent((int)fileHeader.bfSize);// new byte[fileHeader.bfSize];
// TODO: reduce extra alloc // TODO: run benchmark on which one is faster
// write file header // write file header
IntPtr tempPtr = Marshal.AllocHGlobal(fileHeaderSize); //IntPtr tempPtr = Marshal.AllocHGlobal(fileHeaderSize);
Marshal.StructureToPtr(fileHeader, tempPtr, true); //Marshal.StructureToPtr(fileHeader, tempPtr, true);
Marshal.Copy(tempPtr, dataCopy, 0, fileHeaderSize); //Marshal.Copy(tempPtr, dataCopy, 0, fileHeaderSize);
Marshal.FreeHGlobal(tempPtr); //Marshal.FreeHGlobal(tempPtr);
// would this be faster?
fixed (byte* p = dataCopy)
{
Marshal.StructureToPtr(fileHeader, (IntPtr)p, false);
}
// write image // write image
Marshal.Copy(data, dataCopy, fileHeaderSize, (int)fileHeader.bfSize - fileHeaderSize); Marshal.Copy(data, dataCopy, fileHeaderSize, (int)fileHeader.bfSize - fileHeaderSize);

View File

@ -209,7 +209,7 @@ namespace NTwain
{ {
try try
{ {
var args = new DataTransferredEventArgs(this, data, true); var args = new DataTransferredEventArgs(this, true, data);
DataTransferred?.Invoke(this, args); DataTransferred?.Invoke(this, args);
} }
catch { } catch { }