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.

623 lines
15 KiB

  1. /******************************************************************************
  2. acquire.cpp
  3. Copyright (C) Microsoft Corporation, 1997 - 1998
  4. All rights reserved
  5. Notes:
  6. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  7. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  8. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  9. PURPOSE.
  10. ******************************************************************************/
  11. #include <scanner.h> // SCL commands
  12. //
  13. // Hewlett-Packard ScanJet command strings
  14. //
  15. CHAR SCLReset[] = "E";
  16. CHAR SetXRes[] = "*a%dR";
  17. CHAR SetYRes[] = "*a%dS";
  18. CHAR SetXExtPix[] = "*f%dP";
  19. CHAR SetYExtPix[] = "*f%dQ";
  20. CHAR InqXRes[] = "*s10323R";
  21. CHAR SetBitsPerPixel[] = "*a%dG";
  22. CHAR SetIntensity[] = "*a%dL";
  23. CHAR SetContrast[] = "*a%dK";
  24. CHAR SetNegative[] = "*a%dI";
  25. CHAR SetMirror[] = "*a%dM";
  26. CHAR SetDataType[] = "*a%dT";
  27. CHAR ScanCmd[] = "*f0S";
  28. CHAR LampOn[] = "*f1L";
  29. CHAR LampOff[] = "*f0L";
  30. CHAR PollButton[] = "*s1044E";
  31. LPBITMAPINFO pDIB = NULL; // pointer to DIB bitmap header
  32. HBITMAP hDIBSection = NULL; // handle to DIB
  33. LPBYTE pDIBBits = NULL; // pointer to DIB bit data
  34. int m_XSize = 800, // horizontal size in pixels
  35. m_YSize = 800; // vertical size in pixels
  36. BYTE bRed = 0, // bitmap colors
  37. bGreen = 100,
  38. bBlue = 50;
  39. /*****************************************************************************
  40. int IsScanDevice(PSTI_DEVICE_INFORMATION pStiDevI)
  41. Determine whether we have Acquire commands for device
  42. Parameters:
  43. Pointer to Device Information structure
  44. Return:
  45. 1 if Acquire commands available, 0 otherwise
  46. *****************************************************************************/
  47. int IsScanDevice(PSTI_DEVICE_INFORMATION pStiDevI)
  48. {
  49. int n;
  50. WCHAR szScanReadyMfr[] = L"Hewlett-Packard";
  51. WCHAR szScanReadyDev[][48] = {
  52. L"Hewlett-Packard ScanJet 5p",
  53. L"Hewlett-Packard ScanJet 6100c or 4c/3c",
  54. L"Hewlett-Packard ScanJet 4p",
  55. L"Hewlett-Packard ScanJet 3p",
  56. L"Hewlett-Packard ScanJet IIcx",
  57. L"Hewlett-Packard ScanJet IIp",
  58. L"Hewlett-Packard ScanJet IIc",
  59. L""
  60. };
  61. //
  62. // look for non-camera from Hewlett-Packard
  63. //
  64. if ((GET_STIDEVICE_TYPE(pStiDevI->DeviceType) == 1) &&
  65. (wcscmp(pStiDevI->pszVendorDescription,szScanReadyMfr) == 0)) {
  66. for (n = 0;*szScanReadyDev[n];n++) {
  67. //
  68. // is it an HP SCL compatible device?
  69. //
  70. if (wcscmp(pStiDevI->pszLocalName,szScanReadyDev[n]) == 0)
  71. return (1);
  72. }
  73. }
  74. return (0);
  75. }
  76. /******************************************************************************
  77. HRESULT
  78. WINAPI
  79. SendDeviceCommandString(
  80. PSTIDEVICE pStiDevice,
  81. LPSTR pszFormat,
  82. ...
  83. )
  84. Send formatted SCL string to the device
  85. Parameters:
  86. StiDevice buffer and the command string
  87. Return:
  88. HRESULT of last failed Sti call
  89. ******************************************************************************/
  90. HRESULT
  91. WINAPI
  92. SendDeviceCommandString(
  93. PSTIDEVICE pStiDevice,
  94. LPSTR pszFormat,
  95. ...
  96. )
  97. {
  98. HRESULT hres = STI_OK,
  99. hError = STI_OK;
  100. CHAR ScanCommand[255];
  101. UINT cbChar = 1;
  102. //
  103. // lock device first
  104. //
  105. hres = pStiDevice->LockDevice(2000);
  106. if (! SUCCEEDED(hres)) {
  107. StiDisplayError(hres,"LockDevice",TRUE);
  108. hError = hres;
  109. }
  110. else {
  111. //
  112. // Format command string
  113. //
  114. ZeroMemory(ScanCommand,sizeof(ScanCommand));
  115. ScanCommand[0]='\033';
  116. va_list ap;
  117. va_start(ap, pszFormat);
  118. cbChar += wvsprintfA(ScanCommand+1, pszFormat, ap);
  119. va_end(ap);
  120. DisplayOutput("->RawWriteData sending \"%2x %s\"",
  121. ScanCommand[0],ScanCommand+1);
  122. //
  123. // Send command string to the device
  124. //
  125. hres = pStiDevice->RawWriteData(
  126. ScanCommand, //
  127. cbChar, //
  128. NULL //
  129. );
  130. if (! SUCCEEDED(hres)) {
  131. StiDisplayError(hres,"RawWriteData",TRUE);
  132. hError = hres;
  133. }
  134. }
  135. //
  136. // unlock device
  137. //
  138. hres = pStiDevice->UnLockDevice();
  139. if (! SUCCEEDED(hres)) {
  140. StiDisplayError(hres,"UnLockDevice",TRUE);
  141. hError = hres;
  142. }
  143. return (hError);
  144. }
  145. /******************************************************************************
  146. HRESULT
  147. WINAPI
  148. TransactDevice(
  149. PSTIDEVICE pStiDevice,
  150. LPSTR lpResultBuffer,
  151. UINT cbResultBufferSize,
  152. LPSTR pszFormat,
  153. ...
  154. )
  155. Send formatted SCL string to the device and return data in a buffer.
  156. Parameters:
  157. StiDevice buffer, data buffer, sizeof databuffer and the command string.
  158. Return:
  159. HRESULT of last failed Sti call
  160. ******************************************************************************/
  161. HRESULT
  162. WINAPI
  163. TransactDevice(
  164. PSTIDEVICE pStiDevice,
  165. LPSTR lpResultBuffer,
  166. UINT cbResultBufferSize,
  167. LPSTR pszFormat,
  168. ...
  169. )
  170. {
  171. HRESULT hres = STI_OK,
  172. hError = STI_OK;
  173. CHAR ScanCommand[255];
  174. UINT cbChar = 1;
  175. ULONG cbActual = 0;
  176. //
  177. // lock device first
  178. //
  179. hres = pStiDevice->LockDevice(2000);
  180. if (! SUCCEEDED(hres)) {
  181. StiDisplayError(hres,"LockDevice",TRUE);
  182. hError = hres;
  183. }
  184. else {
  185. //
  186. // Format command string
  187. //
  188. ZeroMemory(ScanCommand,sizeof(ScanCommand));
  189. ScanCommand[0]='\033';
  190. va_list ap;
  191. va_start(ap, pszFormat);
  192. cbChar += wvsprintfA(ScanCommand+1, pszFormat, ap);
  193. va_end(ap);
  194. DisplayOutput("->Escape sending \"%2x %s\"",
  195. ScanCommand[0],ScanCommand+1);
  196. //
  197. // Send command string to the device
  198. //
  199. hres = pStiDevice->Escape(
  200. StiTransact, // EscapeFunction
  201. ScanCommand, // lpInData
  202. cbChar, // cbInDataSize
  203. lpResultBuffer, // pOutData
  204. cbResultBufferSize, // dwOutDataSize
  205. &cbActual); // pdwActualData
  206. if (! SUCCEEDED(hres)) {
  207. StiDisplayError(hres,"Escape",TRUE);
  208. hError = hres;
  209. }
  210. if (cbActual != 0)
  211. DisplayOutput(" cbActual %xh",cbActual);
  212. }
  213. //
  214. // unlock device
  215. //
  216. hres = pStiDevice->UnLockDevice();
  217. if (! SUCCEEDED(hres)) {
  218. StiDisplayError(hres,"UnLockDevice",TRUE);
  219. hError = hres;
  220. }
  221. return (hError);
  222. }
  223. /*****************************************************************************
  224. void StiLamp(int nOnOff)
  225. Turn the scanner lamp on and off
  226. Parameters:
  227. Send "ON" to turn lamp on, "OFF" to turn it off.
  228. Return:
  229. none
  230. *****************************************************************************/
  231. void StiLamp(int nOnOff)
  232. {
  233. HRESULT hres;
  234. //
  235. // check that an Sti device is selected
  236. //
  237. if (pStiDevice == NULL)
  238. return;
  239. //
  240. // Test lamp on/off capability
  241. //
  242. if (nOnOff == ON) {
  243. strcpy(pszStr1,LampOn);
  244. strcpy(pszStr2,"On");
  245. }
  246. else {
  247. strcpy(pszStr1,LampOff);
  248. strcpy(pszStr2,"Off");
  249. }
  250. hres = SendDeviceCommandString(pStiDevice,pszStr1);
  251. if (SUCCEEDED(hres)) {
  252. DisplayOutput("Turned Lamp %s",pszStr2);
  253. }
  254. return;
  255. }
  256. /*****************************************************************************
  257. INT StiScan(HWND hWnd)
  258. Scan and display an image from device.
  259. Parameters:
  260. Handle to the window to display image in.
  261. Return:
  262. 0 on success, -1 on error
  263. *****************************************************************************/
  264. INT StiScan(HWND hWnd)
  265. {
  266. HRESULT hres;
  267. ULONG cbDataSize,
  268. ulDIBSize,
  269. ulScanSize;
  270. RGBTRIPLE *pTriplet;
  271. LPBYTE pDIBPtr;
  272. UINT i,
  273. iPixel,
  274. xRes = 0;
  275. int m_XResolution = 100,
  276. m_YResolution = 100;
  277. CHAR ScanData[1024*16];
  278. //
  279. // ensure there is an active still imaging device open
  280. //
  281. if (pStiDevice == NULL)
  282. return (-1);
  283. //
  284. // Set basic parameters
  285. //
  286. hres = SendDeviceCommandString(pStiDevice,SetBitsPerPixel,24);
  287. if (! SUCCEEDED(hres))
  288. return (-1);
  289. hres = SendDeviceCommandString(pStiDevice,SetIntensity,0);
  290. if (! SUCCEEDED(hres))
  291. return (-1);
  292. hres = SendDeviceCommandString(pStiDevice,SetContrast,0);
  293. if (! SUCCEEDED(hres))
  294. return (-1);
  295. hres = SendDeviceCommandString(pStiDevice,SetNegative,1);
  296. if (! SUCCEEDED(hres))
  297. return (-1);
  298. hres = SendDeviceCommandString(pStiDevice,SetMirror,0);
  299. if (! SUCCEEDED(hres))
  300. return (-1);
  301. hres = SendDeviceCommandString(pStiDevice,SetDataType,5); // Color
  302. if (! SUCCEEDED(hres))
  303. return (-1);
  304. hres = SendDeviceCommandString(pStiDevice,SetXRes,m_XResolution);
  305. if (! SUCCEEDED(hres))
  306. return (-1);
  307. hres = SendDeviceCommandString(pStiDevice,SetYRes,m_YResolution);
  308. if (! SUCCEEDED(hres))
  309. return (-1);
  310. hres = SendDeviceCommandString(pStiDevice,SetXExtPix,(m_XSize*300/m_XResolution));
  311. if (! SUCCEEDED(hres))
  312. return (-1);
  313. hres = SendDeviceCommandString(pStiDevice,SetYExtPix,(m_YSize*300/m_YResolution));
  314. if (! SUCCEEDED(hres))
  315. return (-1);
  316. //
  317. // Inquire commands ( X and Y resolution)
  318. //
  319. cbDataSize = sizeof(ScanData);
  320. ZeroMemory(ScanData,sizeof(ScanData));
  321. /*
  322. hres = TransactDevice(pStiDevice,ScanData,cbDataSize,InqXRes);
  323. if (! SUCCEEDED(hres))
  324. return (-1);
  325. */
  326. //
  327. // calculate the size of the DIB
  328. //
  329. ulDIBSize = pDIB->bmiHeader.biWidth * (-pDIB->bmiHeader.biHeight);
  330. //
  331. // start the scan
  332. //
  333. hres = SendDeviceCommandString(pStiDevice,ScanCmd);
  334. for (i = 0,pDIBPtr = pDIBBits,cbDataSize = sizeof(ScanData);
  335. cbDataSize == sizeof(ScanData);i++) {
  336. //
  337. // lock device first
  338. //
  339. hres = pStiDevice->LockDevice(2000);
  340. if (! SUCCEEDED(hres)) {
  341. StiDisplayError(hres,"LockDevice",TRUE);
  342. }
  343. else {
  344. hres = pStiDevice->RawReadData(ScanData,&cbDataSize,NULL);
  345. if (! SUCCEEDED(hres)) {
  346. StiDisplayError(hres,"RawReadData",TRUE);
  347. }
  348. }
  349. //
  350. // unlock device
  351. //
  352. hres = pStiDevice->UnLockDevice();
  353. if (! SUCCEEDED(hres)) {
  354. StiDisplayError(hres,"UnLockDevice",TRUE);
  355. }
  356. if ((cbDataSize * i) < ulDIBSize) {
  357. //
  358. // copy this scanline into the DIB until it is full
  359. //
  360. memcpy(pDIBPtr,ScanData,cbDataSize);
  361. pDIBPtr += cbDataSize;
  362. }
  363. }
  364. //
  365. // how large was the scan?
  366. //
  367. ulScanSize = (sizeof(ScanData))*i+cbDataSize;
  368. DisplayOutput("Scan done. Total passes %d, bytes %lu.",
  369. i,ulScanSize);
  370. //
  371. // Triplets coming in from scanner are inverted from DIB format
  372. //
  373. for (iPixel = 0,pTriplet = (RGBTRIPLE *) pDIBBits;
  374. iPixel < ulDIBSize/3;iPixel++,pTriplet++) {
  375. BYTE bTemp;
  376. bTemp = pTriplet->rgbtBlue;
  377. pTriplet->rgbtBlue = pTriplet->rgbtRed;
  378. pTriplet->rgbtRed = bTemp;
  379. }
  380. //
  381. // display the DIB
  382. //
  383. DisplayScanDIB(hWnd);
  384. nScanCount++;
  385. return (0);
  386. }
  387. /*****************************************************************************
  388. INT CreateScanDIB(HWND);
  389. Create a DIB to display scanned image..
  390. Parameters:
  391. Handle to the window to display image in.
  392. Return:
  393. 0 on success, -1 on error
  394. *****************************************************************************/
  395. INT CreateScanDIB(HWND hWnd)
  396. {
  397. HDC hScreenDC;
  398. RGBTRIPLE *pTriplet;
  399. LPBITMAPINFOHEADER pHdr;
  400. int x,
  401. y;
  402. GdiFlush();
  403. // delete the DIB object if it exists
  404. if (hDIBSection)
  405. DeleteObject(hDIBSection);
  406. /*
  407. hWindow = CreateWindow((LPSTR) pszB,
  408. (LPSTR) pszA,
  409. WS_OVERLAPPEDWINDOW,
  410. rect.left,
  411. rect.top,
  412. rect.right,
  413. rect.bottom,
  414. (HWND) NULL,
  415. 0,
  416. hInst,
  417. NULL);
  418. */
  419. //
  420. // initialize the DIB
  421. //
  422. pDIB = (LPBITMAPINFO) GlobalAlloc(GPTR,sizeof(BITMAPINFO));
  423. pHdr = &pDIB->bmiHeader;
  424. pHdr->biSize = sizeof(BITMAPINFOHEADER);
  425. pHdr->biWidth = m_XSize;
  426. pHdr->biHeight = -m_YSize; // indicate top-down dib
  427. pHdr->biPlanes = 1;
  428. pHdr->biBitCount = 24;
  429. pHdr->biCompression = BI_RGB;
  430. pHdr->biSizeImage = 0;
  431. pHdr->biXPelsPerMeter = 0;
  432. pHdr->biYPelsPerMeter = 0;
  433. pHdr->biClrUsed = 0;
  434. pHdr->biClrImportant = 0;
  435. //
  436. // create the DIB
  437. //
  438. hScreenDC = GetDC(hWnd);
  439. if (NULL == (hDIBSection = CreateDIBSection(hScreenDC,
  440. (PBITMAPINFO) pDIB,
  441. DIB_RGB_COLORS,
  442. (void **) &pDIBBits,
  443. NULL,
  444. 0)))
  445. {
  446. LastError(TRUE);
  447. DisplayOutput("*failed to create DIB");
  448. ReleaseDC(hWnd,hScreenDC);
  449. return (-1);
  450. }
  451. ReleaseDC(hWnd,hScreenDC);
  452. //
  453. // Fill the DIB with colors
  454. //
  455. pTriplet = (RGBTRIPLE *) pDIBBits;
  456. for (x = 0;x < m_XSize;x++) {
  457. for (y = 0;y < m_YSize;y++,pTriplet++) {
  458. pTriplet->rgbtRed = bRed;
  459. pTriplet->rgbtGreen = bGreen;
  460. pTriplet->rgbtBlue = bBlue;
  461. }
  462. }
  463. return (0);
  464. }
  465. /*****************************************************************************
  466. INT DeleteScanDIB();
  467. Delete the DIB used to display a scanned image..
  468. Parameters:
  469. Return:
  470. 0 on success, -1 on error
  471. *****************************************************************************/
  472. INT DeleteScanDIB()
  473. {
  474. GdiFlush();
  475. DeleteObject(hDIBSection);
  476. return (0);
  477. }
  478. /*****************************************************************************
  479. INT DisplayScanDIB(HWND);
  480. Show the DIB.
  481. Parameters:
  482. Handle to the window to display image in.
  483. Return:
  484. 0 on success, -1 on error
  485. *****************************************************************************/
  486. INT DisplayScanDIB(HWND hWnd)
  487. {
  488. HDC hScreenDC;
  489. //
  490. // display the DIB
  491. //
  492. hScreenDC = GetDC(hWnd);
  493. SetDIBitsToDevice(hScreenDC,
  494. 0,0,
  495. m_XSize,m_YSize,
  496. 0,0,
  497. 0,m_YSize,
  498. pDIBBits,
  499. (LPBITMAPINFO) pDIB,
  500. DIB_RGB_COLORS);
  501. ReleaseDC(hWnd,hScreenDC);
  502. return (0);
  503. }