Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
7.6 KiB

  1. #ifndef IMAGING_H
  2. #define IMAGING_H
  3. //////////////////////////////////////////////////////////////////////////
  4. //
  5. // CImagingMgr
  6. //
  7. class CImagingMgr
  8. {
  9. public:
  10. virtual ~CImagingMgr() = 0;
  11. virtual
  12. HRESULT
  13. SelectSource(
  14. HWND hWndParent,
  15. LONG lFlags
  16. ) = 0;
  17. virtual
  18. HRESULT
  19. Acquire(
  20. HWND hWndParent,
  21. HGLOBAL *phDib
  22. ) = 0;
  23. virtual
  24. HRESULT
  25. Select(
  26. LPCTSTR pDeviceId
  27. ) = 0;
  28. virtual
  29. int
  30. NumDevices(
  31. HWND hWndParent
  32. ) = 0;
  33. virtual
  34. BOOL
  35. IsAvailable() = 0;
  36. };
  37. //////////////////////////////////////////////////////////////////////////
  38. //
  39. // TWAIN
  40. //
  41. #ifdef USE_TWAIN
  42. #include <twain.h>
  43. //////////////////////////////////////////////////////////////////////////
  44. //
  45. // CTwainMgr
  46. //
  47. class CTwainMgr : public CImagingMgr
  48. {
  49. public:
  50. CTwainMgr();
  51. ~CTwainMgr();
  52. HRESULT
  53. SelectSource(
  54. HWND hWndParent,
  55. LONG lFlags
  56. );
  57. HRESULT
  58. Acquire(
  59. HWND hWndParent,
  60. HGLOBAL *phDib
  61. );
  62. HRESULT
  63. Select(
  64. LPCTSTR pDeviceId
  65. );
  66. int
  67. NumDevices(
  68. HWND hWndParent
  69. );
  70. BOOL
  71. IsAvailable()
  72. {
  73. return m_DSM_Entry != 0;
  74. }
  75. private:
  76. TW_UINT16 SetCapability(TW_UINT16 Cap, TW_UINT16 ItemType, TW_UINT32 Item);
  77. TW_UINT16 GetCapability(TW_UINT16 Cap, pTW_UINT16 pItemType, pTW_UINT32 pItem);
  78. private:
  79. enum eTwainState
  80. {
  81. State_1_Pre_Session = 1,
  82. State_2_Source_Manager_Loaded = 2,
  83. State_3_Source_Manager_Open = 3,
  84. State_4_Source_Open = 4,
  85. State_5_Source_Enabled = 5,
  86. State_6_Transfer_Ready = 6,
  87. State_7_Transferring = 7
  88. };
  89. private:
  90. eTwainState m_TwainState;
  91. TW_IDENTITY m_AppId;
  92. TW_IDENTITY m_SrcId;
  93. HINSTANCE m_hTwainDll;
  94. DSMENTRYPROC m_DSM_Entry;
  95. };
  96. #endif //USE_TWAIN
  97. //////////////////////////////////////////////////////////////////////////
  98. //
  99. // WIA
  100. //
  101. #include <atlbase.h>
  102. //////////////////////////////////////////////////////////////////////////
  103. //
  104. // CEventCallback
  105. //
  106. class CEventCallback : public IWiaEventCallback
  107. {
  108. public:
  109. CEventCallback();
  110. // IUnknown interface
  111. STDMETHOD(QueryInterface)(REFIID iid, LPVOID *ppvObj);
  112. STDMETHOD_(ULONG, AddRef)();
  113. STDMETHOD_(ULONG, Release)();
  114. // IWiaEventCallback interface
  115. STDMETHOD(ImageEventCallback)(
  116. LPCGUID pEventGuid,
  117. BSTR bstrEventDescription,
  118. BSTR bstrDeviceID,
  119. BSTR bstrDeviceDescription,
  120. DWORD dwDeviceType,
  121. BSTR bstrFullItemName,
  122. ULONG *pulEventType,
  123. ULONG ulReserved
  124. );
  125. // CEventCallback methods
  126. HRESULT Register();
  127. ULONG GetNumDevices() const;
  128. private:
  129. LONG m_cRef;
  130. ULONG m_nNumDevices;
  131. CComPtr<IUnknown> m_pConnectEventObject;
  132. CComPtr<IUnknown> m_pDisconnectEventObject;
  133. };
  134. //////////////////////////////////////////////////////////////////////////
  135. //
  136. // CDataCallback
  137. //
  138. class CDataCallback : public IWiaDataCallback
  139. {
  140. public:
  141. CDataCallback(IWiaProgressDialog *pProgress);
  142. ~CDataCallback();
  143. // IUnknown interface
  144. STDMETHOD(QueryInterface)(REFIID iid, LPVOID *ppvObj);
  145. STDMETHOD_(ULONG, AddRef)();
  146. STDMETHOD_(ULONG, Release)();
  147. // IWiaDataCallback interface
  148. STDMETHOD(BandedDataCallback) (
  149. LONG lReason,
  150. LONG lStatus,
  151. LONG lPercentComplete,
  152. LONG lOffset,
  153. LONG lLength,
  154. LONG lReserved,
  155. LONG lResLength,
  156. PBYTE pbBuffer
  157. );
  158. // CDataCallback methods
  159. HGLOBAL GetBuffer();
  160. // Debugging / performance functions
  161. void QueryStartTimes(LONG lStatus, LONG lPercentComplete);
  162. void QueryStopTimes(LONG lStatus, LONG lPercentComplete);
  163. void PrintTimes();
  164. private:
  165. HRESULT ReAllocBuffer(LONG lBufferSize);
  166. void UpdateStatus(LONG lStatus, LONG lPercentComplete);
  167. private:
  168. LONG m_cRef;
  169. HGLOBAL m_hBuffer;
  170. LONG m_lBufferSize;
  171. LONG m_lDataSize;
  172. IWiaProgressDialog *m_pProgress;
  173. #ifdef DBG
  174. HANDLE m_hDumpFile;
  175. LARGE_INTEGER m_TimeDeviceBegin;
  176. LARGE_INTEGER m_TimeDeviceEnd;
  177. LARGE_INTEGER m_TimeProcessBegin;
  178. LARGE_INTEGER m_TimeProcessEnd;
  179. LARGE_INTEGER m_TimeClientBegin;
  180. LARGE_INTEGER m_TimeClientEnd;
  181. #endif //DBG
  182. };
  183. //////////////////////////////////////////////////////////////////////////
  184. //
  185. // CWIAMgr
  186. //
  187. class CWIAMgr : public CImagingMgr
  188. {
  189. public:
  190. CWIAMgr();
  191. HRESULT
  192. SelectSource(
  193. HWND hWndParent,
  194. LONG lFlags
  195. );
  196. HRESULT
  197. Acquire(
  198. HWND hWndParent,
  199. HGLOBAL *phDib
  200. );
  201. HRESULT
  202. Select(
  203. LPCTSTR pDeviceId
  204. );
  205. int
  206. NumDevices(
  207. HWND hWndParent
  208. );
  209. BOOL
  210. IsAvailable()
  211. {
  212. return m_pEventCallback != 0;
  213. }
  214. private:
  215. struct CGetBandedDataThreadData
  216. {
  217. CGetBandedDataThreadData(
  218. IWiaDataTransfer *pIWiaDataTransfer,
  219. WIA_DATA_TRANSFER_INFO *pWiaDataTransferInfo,
  220. IWiaDataCallback *pIWiaDataCallback
  221. );
  222. ~CGetBandedDataThreadData()
  223. {
  224. }
  225. HRESULT Marshal();
  226. HRESULT Unmarshal();
  227. CComPtr<IStream> m_pIWiaDataTransferStream;
  228. CComPtr<IStream> m_pIWiaDataCallbackStream;
  229. CComPtr<IWiaDataTransfer> m_pIWiaDataTransfer;
  230. PWIA_DATA_TRANSFER_INFO m_pWiaDataTransferInfo;
  231. CComPtr<IWiaDataCallback> m_pIWiaDataCallback;
  232. };
  233. HRESULT GetBandedData(CGetBandedDataThreadData &ThreadData);
  234. static unsigned WINAPI GetBandedDataThread(PVOID pVoid);
  235. private:
  236. CComPtr<CEventCallback> m_pEventCallback;
  237. CComBSTR m_bstrDeviceID;
  238. };
  239. //////////////////////////////////////////////////////////////////////////
  240. //
  241. //
  242. //
  243. class CProgressDialog : public IWiaProgressDialog
  244. {
  245. public:
  246. CProgressDialog();
  247. virtual ~CProgressDialog();
  248. STDMETHOD(QueryInterface)(REFIID iid, LPVOID *ppvObj);
  249. STDMETHOD_(ULONG,AddRef)();
  250. STDMETHOD_(ULONG,Release)();
  251. STDMETHOD(Create)(HWND hwndParent, LONG lFlags);
  252. STDMETHOD(Show)();
  253. STDMETHOD(Hide)();
  254. STDMETHOD(Cancelled)(BOOL *pbCancelled);
  255. STDMETHOD(SetTitle)(LPCWSTR pszMessage);
  256. STDMETHOD(SetMessage)(LPCWSTR pszTitle);
  257. STDMETHOD(SetPercentComplete)(UINT nPercent);
  258. STDMETHOD(Destroy)();
  259. private:
  260. LONG m_cRef;
  261. CProgressCtrl m_ProgressCtrl;
  262. };
  263. //////////////////////////////////////////////////////////////////////////
  264. //
  265. // CComPtrArray
  266. //
  267. // helper class for automatically releasing an array of interface pointers
  268. //
  269. template <class T>
  270. class CComPtrArray
  271. {
  272. public:
  273. CComPtrArray()
  274. {
  275. m_pArray = 0;
  276. m_nItemCount = 0;
  277. }
  278. ~CComPtrArray()
  279. {
  280. if (m_pArray)
  281. {
  282. for (int i = 0; i < m_nItemCount; ++i)
  283. {
  284. if (m_pArray[i])
  285. {
  286. m_pArray[i]->Release();
  287. }
  288. }
  289. CoTaskMemFree(m_pArray);
  290. }
  291. }
  292. operator T**()
  293. {
  294. return m_pArray;
  295. }
  296. bool operator!()
  297. {
  298. return m_pArray == 0;
  299. }
  300. T*** operator&()
  301. {
  302. ASSERT(m_pArray == 0);
  303. return &m_pArray;
  304. }
  305. LONG &ItemCount()
  306. {
  307. return m_nItemCount;
  308. }
  309. private:
  310. T** m_pArray;
  311. LONG m_nItemCount;
  312. };
  313. //////////////////////////////////////////////////////////////////////////
  314. //
  315. //
  316. //
  317. ULONG FindDibSize(LPCVOID pDib);
  318. ULONG FindDibOffBits(LPCVOID pDib);
  319. VOID FixDibHeader(LPVOID pDib, DWORD dwSize);
  320. #endif //IMAGING_H