-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageDataSource.cs
405 lines (381 loc) · 21.5 KB
/
ImageDataSource.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Этот файл является частью библиотеки Saraff.Twain.DS
* © SARAFF SOFTWARE (Кирножицкий Андрей), 2015.
* Saraff.Twain.DS - свободная программа: вы можете перераспространять ее и/или
* изменять ее на условиях Меньшей Стандартной общественной лицензии GNU в том виде,
* в каком она была опубликована Фондом свободного программного обеспечения;
* либо версии 3 лицензии, либо (по вашему выбору) любой более поздней
* версии.
* Saraff.Twain.DS распространяется в надежде, что она будет полезной,
* но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА
* или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Меньшей Стандартной
* общественной лицензии GNU.
* Вы должны были получить копию Меньшей Стандартной общественной лицензии GNU
* вместе с этой программой. Если это не так, см.
* <http://www.gnu.org/licenses/>.)
*
* This file is part of Saraff.Twain.DS.
* © SARAFF SOFTWARE (Kirnazhytski Andrei), 2015.
* Saraff.Twain.DS is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Saraff.Twain.DS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with Saraff.Twain.DS. If not, see <http://www.gnu.org/licenses/>.
*
* PLEASE SEND EMAIL TO: twain@saraff.ru.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
namespace Saraff.Twain.DS {
/// <summary>
/// Provide a Data Source that controls the image acquisition device and is written by the device developer to
/// comply with TWAIN specifications. Traditional device drivers are now included with the
/// Source software and do not need to be shipped by applications.
/// </summary>
[SupportedGroups(TwDG.Image|TwDG.DS2)]
[Capability(typeof(Capabilities.BitDepthDataSourceCapability))]
[Capability(typeof(Capabilities.BitOrderDataSourceCapability))]
[Capability(typeof(Capabilities.CompressionDataSourceCapability))]
[Capability(typeof(Capabilities.PhysicalHeightDataSourceCapability))]
[Capability(typeof(Capabilities.PhysicalWidthDataSourceCapability))]
[Capability(typeof(Capabilities.PixelFlavorDataSourceCapability))]
[Capability(typeof(Capabilities.PixelTypeDataSourceCapability))]
[Capability(typeof(Capabilities.PlanarChunkyDataSourceCapability))]
[Capability(typeof(Capabilities.UnitsDataSourceCapability))]
[Capability(typeof(Capabilities.XNativeResolutionDataSourceCapability))]
[Capability(typeof(Capabilities.XResolutionDataSourceCapability))]
[Capability(typeof(Capabilities.YNativeResolutionDataSourceCapability))]
[Capability(typeof(Capabilities.YResolutionDataSourceCapability))]
[Capability(typeof(Capabilities.ImageFileFormatDataSourceCapability))]
[Capability(typeof(Capabilities.BitDepthReductionDataSourceCapability))]
[Capability(typeof(Capabilities.ThresholdDataSourceCapabilitiy))]
[Capability(typeof(Capabilities.HalftonesDataSourceCapability))]
[Capability(typeof(Capabilities.CustHalftoneDataSourceCapability))]
[Capability(typeof(Capabilities.XferMechDataSourceCapability))]
[Capability(typeof(Capabilities.IndicatorsDataSourceCapability))]
[Capability(typeof(Capabilities.IndicatorsModeDataSourceCapability))]
[SupportedDataCodes(TwDAT.ImageLayout, TwDAT.ImageInfo, TwDAT.ImageNativeXfer, TwDAT.ImageMemXfer, TwDAT.ImageFileXfer)]
public abstract class ImageDataSource:DataSource {
/// <summary>
/// Invoked to processing a TWAIN operations (Triplets).
/// </summary>
/// <param name="dg">The Data Group of the operation triplet. Currently, only DG_CONTROL, DG_IMAGE, and DG_AUDIO are defined.</param>
/// <param name="dat">The Data Argument Type of the operation triplet.</param>
/// <param name="msg">The Message of the operation triplet.</param>
/// <param name="data">The pData parameter is a pointer to the data (a variable or, more
/// typically, a structure) that will be used according to the action specified by the operation
/// triplet.</param>
/// <returns>
/// TWAIN Return Codes.
/// </returns>
/// <exception cref="DataSourceException">
/// </exception>
protected override TwRC OnProcessRequest(TwDG dg, TwDAT dat, TwMSG msg, IntPtr data) {
if(dg==TwDG.Image) {
switch(dat) {
case TwDAT.ImageLayout:
return this._ImageLayoutProcessRequest(msg, data);
case TwDAT.ImageInfo:
if((this.State&DataSourceState.Ready)!=0) {
return this._ImageInfoProcessRequest(msg, data);
}
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
case TwDAT.ImageNativeXfer:
if((this.State&DataSourceState.Ready)!=0) {
return this._ImageNativeXferProcessRequest(msg, data);
}
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
case TwDAT.ImageMemXfer:
if((this.State&DataSourceState.Ready)!=0) {
return this._ImageMemXferProcessRequest(msg, data, false);
}
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
case TwDAT.ImageMemFileXfer:
if((this.State&DataSourceState.Ready)!=0) {
return this._ImageMemXferProcessRequest(msg, data, true);
}
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
case TwDAT.ImageFileXfer:
if((this.State&DataSourceState.Ready)!=0) {
return this._ImageFileXferProcessRequest(msg);
}
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
}
}
return base.OnProcessRequest(dg, dat, msg, data);
}
/// <summary>
/// Returns the Data Group (the type of data) for the upcoming transfer. The Source is required to
/// only supply one of the DGs specified in the SupportedGroups field of a AppIdentity.
/// </summary>
/// <returns>
/// The DG_xxxx constant that identifies the type of data that is ready for transfer
/// from the Source
/// </returns>
protected override TwDG OnGetXferGroup() {
return TwDG.Image;
}
#region Process Request
/// <summary>
/// DG_IMAGE / DAT_IMAGELAYOUT / MSG_GET
/// The DAT_IMAGELAYOUT operations control information on the physical layout of the image on the
/// acquisition platform of the Source(e.g.the glass of a flatbed scanner,the size of a photograph,
/// etc.).
/// The MSG_GET operation describes both the size and placement of the image on the scanner.The
/// coordinates on the scanner and the extents of the image are expressed in the unit of measure
/// currently negotiated for ICAP_UNITS(default is inches).
///
/// DG_IMAGE / DAT_IMAGELAYOUT / MSG_GETDEFAULT
/// This operation returns the default information on the layout of an image. This is the size and
/// position of the image that will be acquired from the Source if the acquisition is started with the
/// Source(and the device it is controlling) in its power-on state(for instance,most flatbed scanners
/// will capture the entire bed).
///
/// DG_IMAGE / DAT_IMAGELAYOUT / MSG_RESET
/// This operation sets the image layout information for the next transfer to its default settings.
///
/// DG_IMAGE / DAT_IMAGELAYOUT / MSG_SET
/// This operation sets the layout for the next image transfer. This allows the application to specify
/// the physical area to be acquired during the next image transfer(for instance,a frame-based
/// application would pass to the Source the size of the frame the user selected within the
/// application—the helpful Source would present a selection region already sized to match the
/// layout frame size).
/// </summary>
/// <param name="msg">The Message of the operation triplet.</param>
/// <param name="data">The data parameter is a pointer to the data (a variable or, more
/// typically, a structure) that will be used according to the action specified by the operation
/// triplet.</param>
/// <returns>TWAIN Return Codes.</returns>
/// <exception cref="DataSourceException">
/// </exception>
private TwRC _ImageLayoutProcessRequest(TwMSG msg, IntPtr data) {
var _layout=Marshal.PtrToStructure(data, typeof(TwImageLayout)) as TwImageLayout;
switch(msg) {
case TwMSG.Get:
Marshal.StructureToPtr(new TwImageLayout {
Frame=this.CurrentImageLayout,
DocumentNumber=1,
PageNumber=1,
FrameNumber=1
}, data, true);
return TwRC.Success;
case TwMSG.GetDefault:
Marshal.StructureToPtr(new TwImageLayout {
Frame=this.DefaultImageLayout,
DocumentNumber=1,
PageNumber=1,
FrameNumber=1
}, data, true);
return TwRC.Success;
case TwMSG.Set:
if((this.State&DataSourceState.Enabled)!=0) {
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
}
this.CurrentImageLayout=_layout.Frame;
return TwRC.Success;
case TwMSG.Reset:
if((this.State&DataSourceState.Enabled)!=0) {
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
}
this.CurrentImageLayout=this.DefaultImageLayout;
return TwRC.Success;
}
throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol);
}
/// <summary>
/// DG_IMAGE / DAT_IMAGEINFO / MSG_GET
/// This operation provides the Application with specific image description
/// information about the current image that has just been transferred.
/// </summary>
/// <param name="msg">The Message of the operation triplet.</param>
/// <param name="data">The data parameter is a pointer to the data (a variable or, more
/// typically, a structure) that will be used according to the action specified by the operation
/// triplet.</param>
/// <returns>TWAIN Return Codes.</returns>
/// <exception cref="DataSourceException"></exception>
private TwRC _ImageInfoProcessRequest(TwMSG msg, IntPtr data) {
switch(msg) {
case TwMSG.Get:
Marshal.StructureToPtr(this.XferEnvironment.ImageInfo.ToTwImageInfo(), data, true);
return TwRC.Success;
}
throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol);
}
/// <summary>
/// DG_IMAGE / DAT_IMAGENATIVEXFER / MSG_GET
/// Causes the transfer of an image’s data from the Source to the application, via the Native transfer
/// mechanism, to begin.The resulting data is stored in main memory in a single block.The data is
/// stored in the Operating Systems native image format. The size of the image that can be transferred
/// is limited to the size of the memory block that can be allocated by the Source.If the image is too
/// large to fit,the Source may resize or crop the image.
/// </summary>
/// <param name="msg">The Message of the operation triplet.</param>
/// <param name="data">The data parameter is a pointer to the data (a variable or, more
/// typically, a structure) that will be used according to the action specified by the operation
/// triplet.</param>
/// <returns>TWAIN Return Codes.</returns>
/// <exception cref="DataSourceException"></exception>
private TwRC _ImageNativeXferProcessRequest(TwMSG msg, IntPtr data) {
switch(msg) {
case TwMSG.Get:
using(var _stream=new MemoryStream()) {
using(var _image=this.OnImageNativeXfer()) {
_image.Save(_stream, ImageFormat.Bmp);
}
_stream.Seek(14,SeekOrigin.Begin);
var _dib=new byte[_stream.Length-14];
_stream.Read(_dib, 0, _dib.Length);
var _hData=DataSourceServices.Memory.Alloc(_dib.Length);
var _pData=DataSourceServices.Memory.Lock(_hData);
DataSourceServices.Memory.Unlock(_hData);
Marshal.Copy(_dib, 0, _pData, _dib.Length);
Marshal.WriteIntPtr(data, _pData);
}
this.State|=DataSourceState.Transferring;
return TwRC.XferDone;
}
throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol);
}
/// <summary>
/// DG_IMAGE / DAT_IMAGEMEMXFER / MSG_GET
/// This operation is used to initiate the transfer of an image from the Source to the application via the
/// Buffered Memory transfer mechanism.
/// This operation supports the transfer of successive blocks of image data (in strips or, optionally,
/// tiles) from the Source into one or more main memory transfer buffers. These buffers(for strips)
/// are allocated and owned by the application. For tiled transfers, the source allocates the buffers.
/// The application should repeatedly invoke this operation while TWRC_SUCCESS is returned by the Source.
/// </summary>
/// <param name="msg">The Message of the operation triplet.</param>
/// <param name="data">The data parameter is a pointer to the data (a variable or, more
/// typically, a structure) that will be used according to the action specified by the operation
/// triplet.</param>
/// <param name="isMemFile">If set to <c>true</c> that transfer a MemFile.</param>
/// <returns>TWAIN Return Codes.</returns>
/// <exception cref="DataSourceException">
/// </exception>
private TwRC _ImageMemXferProcessRequest(TwMSG msg, IntPtr data, bool isMemFile) {
switch(msg) {
case TwMSG.Get:
var _memXfer=Marshal.PtrToStructure(data, typeof(TwImageMemXfer)) as TwImageMemXfer;
if(_memXfer.Memory.Length>this.XferEnvironment.MaxMemXferBufferSize||_memXfer.Memory.Length<this.XferEnvironment.MinMemXferBufferSize) {
throw new DataSourceException(TwRC.Failure, TwCC.BadValue);
}
var _image=this.OnImageMemXfer(_memXfer.Memory.Length, isMemFile);
if(_image==null) {
return TwRC.Cancel;
}
Marshal.StructureToPtr(
new TwImageMemXfer {
BytesPerRow=_image.BytesPerRow,
BytesWritten=(uint)_image.ImageData.Length,
Columns=_image.Columns,
Compression=_image.Compression,
Rows=_image.Rows,
XOffset=_image.XOffset,
YOffset=_image.YOffset,
Memory=_memXfer.Memory
},
data,
true);
var _pImageData=IntPtr.Zero;
switch(_memXfer.Memory.Flags&(TwMF.Handle|TwMF.Pointer)) {
case TwMF.Handle:
_pImageData=DataSourceServices.Memory.Lock(_memXfer.Memory.TheMem);
DataSourceServices.Memory.Unlock(_memXfer.Memory.TheMem);
break;
case TwMF.Pointer:
_pImageData=_memXfer.Memory.TheMem;
break;
default:
throw new DataSourceException(TwRC.Failure, TwCC.BadValue);
}
Marshal.Copy(_image.ImageData, 0, _pImageData, _image.ImageData.Length);
return _image.IsXferDone?TwRC.XferDone:TwRC.Success;
}
throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol);
}
/// <summary>
/// DG_IMAGE / DAT_IMAGEFILEXFER / MSG_GET
/// This operation is used to initiate the transfer of an image from the Source to the application via the
/// disk-file transfer mechanism. It causes the transfer to begin.
/// </summary>
/// <param name="msg">The Message of the operation triplet.</param>
/// <returns>TWAIN Return Codes.</returns>
/// <exception cref="DataSourceException"></exception>
private TwRC _ImageFileXferProcessRequest(TwMSG msg) {
switch(msg) {
case TwMSG.Get:
this.OnImageFileXfer();
return TwRC.XferDone;
}
throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol);
}
#endregion
#region protected virtual
/// <summary>
/// Causes the transfer of an image’s data from the Source to the application, via the Native transfer
/// mechanism, to begin. The resulting data is stored in main memory in a single block. The data is
/// stored in the Operating Systems native image format. The size of the image that can be transferred
/// is limited to the size of the memory block that can be allocated by the Source. If the image is too
/// large to fit, the Source may resize or crop the image.
/// </summary>
/// <returns>A image to transfer.</returns>
protected abstract Image OnImageNativeXfer();
/// <summary>
/// This operation is used to initiate the transfer of an image from the Source to the application via the
/// Buffered Memory transfer mechanism.
/// This operation supports the transfer of successive blocks of image data (in strips or,optionally,
/// tiles) from the Source into one or more main memory transfer buffers. These buffers(for strips)
/// are allocated and owned by the application. For tiled transfers, the source allocates the buffers.
/// The application should repeatedly invoke this operation while TWRC_SUCCESS is returned by the Source.
/// </summary>
/// <param name="length">The length.</param>
/// <param name="isMemFile">If set to <c>true</c> that transfer a MemFile.</param>
/// <returns>Information about transmitting data.</returns>
protected abstract ImageMemXfer OnImageMemXfer(long length, bool isMemFile);
/// <summary>
/// This operation is used to initiate the transfer of an image from the Source to the application via the
/// disk-file transfer mechanism. It causes the transfer to begin.
/// </summary>
/// <exception cref="DataSourceException"></exception>
protected virtual void OnImageFileXfer() {
throw new DataSourceException(TwRC.Failure, TwCC.SeqError);
}
/// <summary>
/// Gets or sets the current image layout.
/// </summary>
/// <value>
/// The current image layout.
/// </value>
protected virtual RectangleF CurrentImageLayout {
get;
set;
}
/// <summary>
/// Gets the default image layout.
/// </summary>
/// <value>
/// The default image layout.
/// </value>
/// <exception cref="System.InvalidOperationException">Не указан ImageLayoutAttribute.</exception>
protected virtual RectangleF DefaultImageLayout {
get {
foreach(ImageLayoutAttribute _attr in this.GetType().GetCustomAttributes(typeof(ImageLayoutAttribute), false)) {
return _attr.Frame;
}
throw new InvalidOperationException("Не указан ImageLayoutAttribute.");
}
}
#endregion
}
}