Leaked source code of windows server 2003
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.

561 lines
15 KiB

  1. #include "precomp.h"
  2. //
  3. // data source manager class implementation
  4. //
  5. CDSM::CDSM()
  6. : m_hDSM(NULL),
  7. m_DSMEntry(NULL)
  8. {
  9. }
  10. CDSM::~CDSM()
  11. {
  12. if (m_DSMEntry) {
  13. FreeLibrary(m_hDSM);
  14. }
  15. }
  16. BOOL CDSM::Notify(TW_IDENTITY *pidSrc,TW_IDENTITY *pidDst,TW_UINT32 twDG,
  17. TW_UINT16 twDAT,TW_UINT16 twMsg,TW_MEMREF pData)
  18. {
  19. if (!m_DSMEntry) {
  20. TCHAR szWindowsDirectory[MAX_PATH];
  21. memset(szWindowsDirectory,0,sizeof(szWindowsDirectory));
  22. if(GetWindowsDirectory(szWindowsDirectory,(sizeof(szWindowsDirectory)/sizeof(szWindowsDirectory[0]))) == 0)
  23. {
  24. //
  25. // could not get windows directory to load TWAIN_32.DLL to get the
  26. // DSM entry point.
  27. //
  28. return FALSE;
  29. }
  30. TCHAR szTwainDSMDir[MAX_PATH];
  31. memset(szTwainDSMDir,0,sizeof(szTwainDSMDir));
  32. //
  33. // create TWAIN_32.DLL loading, full path
  34. //
  35. _sntprintf(szTwainDSMDir,(sizeof(szTwainDSMDir)/sizeof(szTwainDSMDir[0])),TEXT("%s\\%s"),szWindowsDirectory,TEXT("TWAIN_32.DLL"));
  36. szTwainDSMDir[MAX_PATH - 1] = 0;
  37. m_hDSM = LoadLibrary(szTwainDSMDir);
  38. if (m_hDSM) {
  39. m_DSMEntry = (DSMENTRYPROC)GetProcAddress(m_hDSM, "DSM_Entry");
  40. if (!m_DSMEntry) {
  41. FreeLibrary(m_hDSM);
  42. m_hDSM = NULL;
  43. m_DSMEntry = NULL;
  44. }
  45. }
  46. }
  47. if (m_DSMEntry) {
  48. (*m_DSMEntry)(pidSrc, pidDst, twDG, twDAT, twMsg, pData);
  49. return TRUE;
  50. }
  51. return FALSE;
  52. }
  53. LPTSTR LoadResourceString(int StringId)
  54. {
  55. LPTSTR str = NULL;
  56. TCHAR strTemp[256];
  57. int len;
  58. len = ::LoadString(g_hInstance, StringId, strTemp,
  59. sizeof(strTemp)/sizeof(TCHAR));
  60. if (len) {
  61. str = new TCHAR[len + 1];
  62. if (str) {
  63. LSTRNCPY(str, strTemp, len + 1);
  64. } else {
  65. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  66. }
  67. }
  68. return str;
  69. }
  70. //
  71. // dialog class implementation
  72. //
  73. INT_PTR CALLBACK CDialog::DialogWndProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
  74. {
  75. CDialog* pThis = (CDialog *) GetWindowLongPtr(hDlg, DWLP_USER);
  76. BOOL Result;
  77. switch (uMsg) {
  78. case WM_INITDIALOG:
  79. {
  80. pThis = (CDialog *)lParam;
  81. SetWindowLongPtr(hDlg, DWLP_USER, (LONG_PTR)pThis);
  82. pThis->m_hDlg = hDlg;
  83. Result = pThis->OnInitDialog();
  84. break;
  85. }
  86. case WM_COMMAND:
  87. {
  88. if (pThis)
  89. pThis->OnCommand(wParam, lParam);
  90. Result = FALSE;
  91. break;
  92. }
  93. case WM_HELP:
  94. {
  95. if (pThis)
  96. pThis->OnHelp((LPHELPINFO)lParam);
  97. Result = FALSE;
  98. break;
  99. }
  100. case WM_CONTEXTMENU:
  101. {
  102. if (pThis)
  103. pThis->OnContextMenu((HWND)wParam, LOWORD(lParam), HIWORD(lParam));
  104. Result = FALSE;
  105. break;
  106. }
  107. case WM_NOTIFY:
  108. {
  109. if (pThis)
  110. pThis->OnNotify((LPNMHDR)lParam);
  111. Result = FALSE;
  112. break;
  113. }
  114. default:
  115. if (pThis)
  116. Result = pThis->OnMiscMsg(uMsg, wParam, lParam);
  117. else
  118. Result = FALSE;
  119. break;
  120. }
  121. return Result;
  122. }
  123. int GetDIBBitsOffset(BITMAPINFO *pbmi)
  124. {
  125. int Offset = -1;
  126. if (pbmi && pbmi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER)) {
  127. Offset = pbmi->bmiHeader.biSize;
  128. if (pbmi->bmiHeader.biBitCount <= 8) {
  129. if (pbmi->bmiHeader.biClrUsed) {
  130. Offset += pbmi->bmiHeader.biClrUsed * sizeof(RGBQUAD);
  131. } else {
  132. Offset += ((DWORD) 1 << pbmi->bmiHeader.biBitCount) * sizeof(RGBQUAD);
  133. }
  134. }
  135. if (BI_BITFIELDS == pbmi->bmiHeader.biCompression) {
  136. Offset += 3 * sizeof(DWORD);
  137. }
  138. }
  139. return Offset;
  140. }
  141. UINT GetLineSize(PMEMORY_TRANSFER_INFO pInfo)
  142. {
  143. UINT uiWidthBytes = 0;
  144. uiWidthBytes = (pInfo->mtiWidthPixels * pInfo->mtiBitsPerPixel) + 31;
  145. uiWidthBytes = ((uiWidthBytes/8) & 0xfffffffc);
  146. return uiWidthBytes;
  147. }
  148. UINT GetDIBLineSize(UINT Width,UINT BitCount)
  149. {
  150. UINT uiWidthBytes = 0;
  151. uiWidthBytes = (Width * BitCount) + 31;
  152. uiWidthBytes = ((uiWidthBytes/8) & 0xfffffffc);
  153. return uiWidthBytes;
  154. }
  155. BOOL FlipDIB(HGLOBAL hDIB, BOOL bUpsideDown)
  156. {
  157. BITMAPINFO *pbmi = NULL;
  158. if (!hDIB) {
  159. SetLastError(ERROR_INVALID_PARAMETER);
  160. return FALSE;
  161. }
  162. pbmi = (BITMAPINFO *)GlobalLock(hDIB);
  163. if (pbmi == NULL) {
  164. SetLastError(ERROR_INVALID_PARAMETER);
  165. return FALSE;
  166. }
  167. //
  168. // check upside down flag
  169. //
  170. if (bUpsideDown) {
  171. if (pbmi->bmiHeader.biHeight < 0) {
  172. //
  173. // if the height is already negative, then the image
  174. // is already upside down. Make the height positive
  175. // and you have a valid upside down image.
  176. //
  177. pbmi->bmiHeader.biHeight = abs(pbmi->bmiHeader.biHeight);
  178. GlobalUnlock(hDIB);
  179. return TRUE;
  180. }
  181. } else {
  182. //
  183. // if we do not need flipping, just return TRUE
  184. //
  185. if (pbmi->bmiHeader.biHeight > 0) {
  186. GlobalUnlock(hDIB);
  187. return TRUE;
  188. }
  189. }
  190. //
  191. // proceed to flip the DIB image
  192. //
  193. UINT LineSize = 0;
  194. UINT Height = 0;
  195. UINT Line = 0;
  196. BOOL Result = TRUE;
  197. BYTE *pTop, *pBottom, *pLine;
  198. // calculate the image height
  199. Height = abs(pbmi->bmiHeader.biHeight);
  200. //
  201. // get the line size. This is the unit we will be working on
  202. //
  203. LineSize = GetDIBLineSize(pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biBitCount);
  204. DBG_TRC(("FlipDIB, src height = %d", pbmi->bmiHeader.biHeight));
  205. //
  206. // line buffer for swapping data
  207. //
  208. pLine = new BYTE[LineSize];
  209. if (pLine) {
  210. pTop = (BYTE *)pbmi + GetDIBBitsOffset(pbmi);
  211. pBottom = pTop + (Height - 1) * LineSize;
  212. Height /= 2;
  213. for (Line = 0; Line < Height; Line++) {
  214. memcpy(pLine, pTop, LineSize);
  215. memcpy(pTop, pBottom, LineSize);
  216. memcpy(pBottom, pLine, LineSize);
  217. pTop += LineSize;
  218. pBottom -= LineSize;
  219. }
  220. pbmi->bmiHeader.biHeight = abs(pbmi->bmiHeader.biHeight);
  221. delete [] pLine;
  222. } else {
  223. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  224. Result = FALSE;
  225. }
  226. GlobalUnlock(hDIB);
  227. return Result;
  228. }
  229. TW_UINT16 WriteDIBToFile(LPSTR szFileName, HGLOBAL hDIB)
  230. {
  231. TW_UINT16 twRc = TWRC_FAILURE;
  232. //
  233. // write BITMAPFILEHEADER
  234. //
  235. BITMAPFILEHEADER bmfh;
  236. BITMAPINFOHEADER *pbmh = (BITMAPINFOHEADER *)GlobalLock(hDIB);
  237. if(pbmh){
  238. LONG lPaletteSize = pbmh->biClrUsed * sizeof(RGBQUAD);
  239. bmfh.bfType = BMPFILE_HEADER_MARKER;
  240. bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lPaletteSize;
  241. bmfh.bfSize = pbmh->biSizeImage + bmfh.bfOffBits;
  242. bmfh.bfReserved1 = 0;
  243. bmfh.bfReserved2 = 0;
  244. LONG lDataSize = sizeof(BITMAPINFOHEADER) + pbmh->biSizeImage + lPaletteSize;
  245. //
  246. // write BITMAP data (this includes header)
  247. //
  248. HANDLE hBitmapFile = NULL;
  249. hBitmapFile = CreateFileA(szFileName,
  250. GENERIC_WRITE,FILE_SHARE_READ,NULL,
  251. CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  252. if (hBitmapFile != INVALID_HANDLE_VALUE && hBitmapFile != NULL) {
  253. DWORD dwBytesWritten = 0;
  254. //
  255. // write BITMAPFILHEADER
  256. //
  257. if((WriteFile(hBitmapFile,&bmfh,sizeof(bmfh),&dwBytesWritten,NULL)) && (dwBytesWritten == sizeof(bmfh))){
  258. //
  259. // write BITMAPINFOHEADER, palette, and data
  260. //
  261. if ((WriteFile(hBitmapFile,pbmh,lDataSize,&dwBytesWritten,NULL)) && (dwBytesWritten == lDataSize)) {
  262. //
  263. // return TWRC_XFERDONE when file has been saved to disk properly
  264. //
  265. twRc = TWRC_XFERDONE;
  266. } else {
  267. DBG_ERR(("WriteDIBToFile, could not write the BITMAPINFOHEADER, palette, and data to file %s", szFileName));
  268. }
  269. } else {
  270. DBG_ERR(("WriteDIBToFile, could not write the BITMAPFILEHEADER to file %s", szFileName));
  271. }
  272. //
  273. // close file
  274. //
  275. CloseHandle(hBitmapFile);
  276. hBitmapFile = NULL;
  277. } else {
  278. DBG_ERR(("WriteDIBToFile, could not create the file %s", szFileName));
  279. }
  280. //
  281. // unlock memory when finished
  282. //
  283. GlobalUnlock(hDIB);
  284. pbmh = NULL;
  285. }
  286. return twRc;
  287. }
  288. TW_UINT16 TWCC_FROM_HRESULT(HRESULT hr)
  289. {
  290. TW_UINT16 twCc = S_OK;
  291. switch (hr) {
  292. case S_OK:
  293. twCc = TWCC_SUCCESS;
  294. break;
  295. case E_OUTOFMEMORY:
  296. twCc = TWCC_LOWMEMORY;
  297. break;
  298. case E_INVALIDARG:
  299. twCc = TWCC_BADVALUE;
  300. break;
  301. case E_FAIL:
  302. default:
  303. twCc = TWCC_BUMMER;
  304. break;
  305. }
  306. return twCc;
  307. }
  308. TW_UINT16 WIA_IPA_COMPRESSION_TO_ICAP_COMPRESSION(LONG lCompression)
  309. {
  310. TW_UINT16 Compression = TWCP_NONE;
  311. switch(lCompression){
  312. case WIA_COMPRESSION_NONE:
  313. Compression = TWCP_NONE;
  314. break;
  315. case WIA_COMPRESSION_BI_RLE4:
  316. Compression = TWCP_RLE4;
  317. break;
  318. case WIA_COMPRESSION_BI_RLE8:
  319. Compression = TWCP_RLE8;
  320. break;
  321. case WIA_COMPRESSION_G3:
  322. Compression = TWCP_GROUP31D;
  323. break;
  324. case WIA_COMPRESSION_G4:
  325. Compression = TWCP_GROUP4;
  326. break;
  327. case WIA_COMPRESSION_JPEG:
  328. Compression = TWCP_JPEG;
  329. break;
  330. default:
  331. break;
  332. }
  333. return Compression;
  334. }
  335. TW_UINT16 WIA_IPA_DATATYPE_TO_ICAP_PIXELTYPE(LONG lDataType)
  336. {
  337. TW_UINT16 PixelType = TWPT_RGB;
  338. switch (lDataType) {
  339. case WIA_DATA_THRESHOLD:
  340. PixelType = TWPT_BW;
  341. break;
  342. case WIA_DATA_GRAYSCALE:
  343. PixelType = TWPT_GRAY;
  344. break;
  345. case WIA_DATA_COLOR:
  346. PixelType = TWPT_RGB;
  347. break;
  348. case WIA_DATA_DITHER:
  349. case WIA_DATA_COLOR_THRESHOLD:
  350. case WIA_DATA_COLOR_DITHER:
  351. default:
  352. break;
  353. }
  354. return PixelType;
  355. }
  356. TW_UINT16 WIA_IPA_FORMAT_TO_ICAP_IMAGEFILEFORMAT(GUID guidFormat)
  357. {
  358. TW_UINT16 ImageFileFormat = TWFF_BMP;
  359. if (guidFormat == WiaImgFmt_BMP) {
  360. ImageFileFormat = TWFF_BMP;
  361. } else if (guidFormat == WiaImgFmt_JPEG) {
  362. ImageFileFormat = TWFF_JFIF;
  363. } else if (guidFormat == WiaImgFmt_TIFF) {
  364. ImageFileFormat = TWFF_TIFF;
  365. } else if (guidFormat == WiaImgFmt_PICT) {
  366. ImageFileFormat = TWFF_PICT;
  367. } else if (guidFormat == WiaImgFmt_PNG) {
  368. ImageFileFormat = TWFF_PNG;
  369. } else if (guidFormat == WiaImgFmt_EXIF) {
  370. ImageFileFormat = TWFF_EXIF;
  371. } else if (guidFormat == WiaImgFmt_FLASHPIX) {
  372. ImageFileFormat = TWFF_FPX;
  373. } else if (guidFormat == WiaImgFmt_UNDEFINED) {
  374. DBG_TRC(("WIA File Format WiaImgFmt_UNDEFINED does not MAP to TWAIN a file format"));
  375. } else if (guidFormat == WiaImgFmt_EMF) {
  376. DBG_TRC(("WIA File Format WiaImgFmt_EMF does not MAP to TWAIN a file format"));
  377. } else if (guidFormat == WiaImgFmt_WMF) {
  378. DBG_TRC(("WIA File Format WiaImgFmt_WMF does not MAP to TWAIN a file format"));
  379. } else if (guidFormat == WiaImgFmt_GIF) {
  380. DBG_TRC(("WIA File Format WiaImgFmt_GIF does not MAP to TWAIN a file format"));
  381. } else if (guidFormat == WiaImgFmt_PHOTOCD) {
  382. DBG_TRC(("WIA File Format WiaImgFmt_PHOTOCD does not MAP to TWAIN a file format"));
  383. } else if (guidFormat == WiaImgFmt_ICO) {
  384. DBG_TRC(("WIA File Format WiaImgFmt_ICO does not MAP to TWAIN a file format"));
  385. } else if (guidFormat == WiaImgFmt_CIFF) {
  386. DBG_TRC(("WIA File Format WiaImgFmt_CIFF does not MAP to TWAIN a file format"));
  387. } else if (guidFormat == WiaImgFmt_JPEG2K) {
  388. DBG_TRC(("WIA File Format WiaImgFmt_JPEG2K does not MAP to TWAIN a file format"));
  389. } else if (guidFormat == WiaImgFmt_JPEG2KX) {
  390. DBG_TRC(("WIA File Format WiaImgFmt_JPEG2KX does not MAP to TWAIN a file format"));
  391. } else {
  392. DBG_TRC(("WIA File Format (Unknown) does not MAP to TWAIN a file format"));
  393. }
  394. return ImageFileFormat;
  395. }
  396. LONG ICAP_COMPRESSION_TO_WIA_IPA_COMPRESSION(TW_UINT16 Compression)
  397. {
  398. LONG lCompression = WIA_COMPRESSION_NONE;
  399. switch(Compression){
  400. case TWCP_NONE:
  401. lCompression = WIA_COMPRESSION_NONE;
  402. break;
  403. case TWCP_RLE4:
  404. lCompression = WIA_COMPRESSION_BI_RLE4;
  405. break;
  406. case TWCP_RLE8:
  407. lCompression = WIA_COMPRESSION_BI_RLE8;
  408. break;
  409. case TWCP_GROUP4:
  410. lCompression = WIA_COMPRESSION_G4;
  411. break;
  412. case TWCP_JPEG:
  413. lCompression = WIA_COMPRESSION_JPEG;
  414. break;
  415. case TWCP_GROUP31D:
  416. case TWCP_GROUP31DEOL:
  417. case TWCP_GROUP32D:
  418. lCompression = WIA_COMPRESSION_G3;
  419. break;
  420. case TWCP_LZW:
  421. case TWCP_JBIG:
  422. case TWCP_PNG:
  423. case TWCP_PACKBITS:
  424. case TWCP_BITFIELDS:
  425. default:
  426. break;
  427. }
  428. return lCompression;
  429. }
  430. LONG ICAP_PIXELTYPE_TO_WIA_IPA_DATATYPE(TW_UINT16 PixelType)
  431. {
  432. LONG lDataType = WIA_DATA_COLOR;
  433. switch(PixelType){
  434. case TWPT_BW:
  435. lDataType = WIA_DATA_THRESHOLD;
  436. break;
  437. case TWPT_GRAY:
  438. lDataType = WIA_DATA_GRAYSCALE;
  439. break;
  440. case TWPT_RGB:
  441. lDataType = WIA_DATA_COLOR;
  442. break;
  443. case TWPT_PALETTE:
  444. case TWPT_CMY:
  445. case TWPT_CMYK:
  446. case TWPT_YUV:
  447. case TWPT_YUVK:
  448. case TWPT_CIEXYZ:
  449. default:
  450. break;
  451. }
  452. return lDataType;
  453. }
  454. GUID ICAP_IMAGEFILEFORMAT_TO_WIA_IPA_FORMAT(TW_UINT16 ImageFileFormat)
  455. {
  456. GUID guidFormat = WiaImgFmt_BMP;
  457. switch(ImageFileFormat){
  458. case TWFF_TIFFMULTI:
  459. case TWFF_TIFF:
  460. guidFormat = WiaImgFmt_TIFF;
  461. break;
  462. case TWFF_PICT:
  463. guidFormat = WiaImgFmt_PICT;
  464. break;
  465. case TWFF_BMP:
  466. guidFormat = WiaImgFmt_BMP;
  467. break;
  468. case TWFF_JFIF:
  469. guidFormat = WiaImgFmt_JPEG;
  470. break;
  471. case TWFF_FPX:
  472. guidFormat = WiaImgFmt_FLASHPIX;
  473. break;
  474. case TWFF_PNG:
  475. guidFormat = WiaImgFmt_PNG;
  476. break;
  477. case TWFF_EXIF:
  478. guidFormat = WiaImgFmt_EXIF;
  479. break;
  480. case TWFF_SPIFF:
  481. case TWFF_XBM:
  482. default:
  483. break;
  484. }
  485. return guidFormat;
  486. }