-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path_ImageHandler.cs
127 lines (108 loc) · 4.11 KB
/
_ImageHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Saraff.Twain {
/// <summary>
/// Base class to processing of a acquired image.
/// </summary>
/// <seealso cref="Saraff.Twain.IImageHandler" />
internal abstract class _ImageHandler : IImageHandler {
private Dictionary<string, object> _state = null;
private const string _ImagePointer = "ImagePointer";
#region IImageHandler
/// <summary>
/// Convert a block of unmanaged memory to stream.
/// </summary>
/// <param name="ptr">The pointer to block of unmanaged memory.</param>
/// <param name="provider">The provider of a streams.</param>
/// <returns>
/// Stream that contains data of a image.
/// </returns>
public Stream PtrToStream(IntPtr ptr, IStreamProvider provider) {
this._state = new Dictionary<string, object> {
{_ImageHandler._ImagePointer, ptr}
};
Stream _stream = provider != null ? provider.GetStream() : new MemoryStream();
this.PtrToStreamCore(ptr, _stream);
return _stream;
}
#endregion
/// <summary>
/// Convert a block of unmanaged memory to stream.
/// </summary>
/// <param name="ptr">The pointer to block of unmanaged memory.</param>
/// <param name="provider">The provider of a streams.</param>
protected virtual void PtrToStreamCore(IntPtr ptr, Stream stream) {
BinaryWriter _writer = new BinaryWriter(stream);
int _size = this.GetSize();
byte[] _buffer = new byte[this.BufferSize];
for(int _offset = 0, _len = 0; _offset < _size; _offset += _len) {
_len = Math.Min(this.BufferSize, _size - _offset);
Marshal.Copy((IntPtr)(ptr.ToInt64() + _offset), _buffer, 0, _len);
_writer.Write(_buffer, 0, _len);
}
}
/// <summary>
/// Gets the size of a image data.
/// </summary>
/// <returns>Size of a image data.</returns>
protected abstract int GetSize();
/// <summary>
/// Gets the size of the buffer.
/// </summary>
/// <value>
/// The size of the buffer.
/// </value>
protected abstract int BufferSize { get; }
/// <summary>
/// Gets the state of the handler.
/// </summary>
/// <value>
/// The state of the handler.
/// </value>
protected Dictionary<string, object> HandlerState => this._state;
/// <summary>
/// Gets the pointer to unmanaged memory that contain image data.
/// </summary>
/// <value>
/// The image pointer.
/// </value>
protected IntPtr ImagePointer => (IntPtr)this.HandlerState[_ImageHandler._ImagePointer];
}
/// <summary>
/// Provides processing of a acquired image.
/// </summary>
public interface IImageHandler {
/// <summary>
/// Convert a block of unmanaged memory to stream.
/// </summary>
/// <param name="ptr">The pointer to block of unmanaged memory.</param>
/// <param name="provider">The provider of a streams.</param>
/// <returns>Stream that contains data of a image.</returns>
Stream PtrToStream(IntPtr ptr, IStreamProvider provider);
}
/// <summary>
/// Provides instances of the <see cref="System.IO.Stream"/> for data writing.
/// </summary>
public interface IStreamProvider {
/// <summary>
/// Gets the stream.
/// </summary>
/// <returns>The stream.</returns>
Stream GetStream();
}
/// <summary>
/// Provides image factory.
/// </summary>
/// <typeparam name="T">Type of image</typeparam>
public interface IImageFactory<T> {
/// <summary>
/// Create and return instance of image.
/// </summary>
/// <param name="stream">Image data.</param>
/// <returns>Image.</returns>
T Create(Stream stream);
}
}