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.

247 lines
6.5 KiB

  1. #include "stdafx.h"
  2. #include "hpscl.h"
  3. #include "datadump.h"
  4. CHPSCL::CHPSCL(PDEVCTRL pDeviceControl)
  5. {
  6. m_pDeviceControl = pDeviceControl;
  7. }
  8. CHPSCL::~CHPSCL()
  9. {
  10. }
  11. BOOL CHPSCL::SetXRes(LONG xRes)
  12. {
  13. m_xres = xRes;
  14. CHAR pbuffer[64];
  15. memset(pbuffer,0,sizeof(pbuffer));
  16. sprintf(pbuffer,"\x1B*a%dR",xRes);
  17. return RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)pbuffer,lstrlenA(pbuffer),0);
  18. }
  19. BOOL CHPSCL::SetYRes(LONG yRes)
  20. {
  21. m_yres = yRes;
  22. CHAR pbuffer[64];
  23. memset(pbuffer,0,sizeof(pbuffer));
  24. sprintf(pbuffer,"\x1B*a%dS",yRes);
  25. return RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)pbuffer,lstrlenA(pbuffer),0);
  26. }
  27. BOOL CHPSCL::SetXPos(LONG xPos)
  28. {
  29. m_xpos = xPos;
  30. CHAR pbuffer[64];
  31. memset(pbuffer,0,sizeof(pbuffer));
  32. LONG OpticalValue = xPos;
  33. OpticalValue = ((OpticalValue * 300) / m_xres);
  34. sprintf(pbuffer,"\x1B*f%dX",OpticalValue);
  35. return RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)pbuffer,lstrlenA(pbuffer),0);
  36. }
  37. BOOL CHPSCL::SetYPos(LONG yPos)
  38. {
  39. m_ypos = yPos;
  40. CHAR pbuffer[64];
  41. memset(pbuffer,0,sizeof(pbuffer));
  42. LONG OpticalValue = yPos;
  43. OpticalValue = ((OpticalValue * 300) / m_yres);
  44. sprintf(pbuffer,"\x1B*f%dY",OpticalValue);
  45. return RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)pbuffer,lstrlenA(pbuffer),0);
  46. }
  47. BOOL CHPSCL::SetXExt(LONG xExt)
  48. {
  49. m_xext = xExt;
  50. CHAR pbuffer[64];
  51. memset(pbuffer,0,sizeof(pbuffer));
  52. LONG OpticalValue = xExt;
  53. OpticalValue = ((OpticalValue * 300) / m_xres);
  54. sprintf(pbuffer,"\x1B*f%dP",OpticalValue);
  55. return RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)pbuffer,lstrlenA(pbuffer),0);
  56. }
  57. BOOL CHPSCL::SetYExt(LONG yExt)
  58. {
  59. m_yext = yExt;
  60. CHAR pbuffer[64];
  61. memset(pbuffer,0,sizeof(pbuffer));
  62. LONG OpticalValue = yExt;
  63. OpticalValue = ((OpticalValue * 300) / m_yres);
  64. sprintf(pbuffer,"\x1B*f%dQ",OpticalValue);
  65. return RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)pbuffer,lstrlenA(pbuffer),0);
  66. }
  67. BOOL CHPSCL::SetDataType(LONG DataType)
  68. {
  69. m_datatype = DataType;
  70. BOOL bSuccess = TRUE;
  71. switch(m_datatype){
  72. case 0: // WIA_DATA_THRESHOLD
  73. bSuccess = RawWrite(m_pDeviceControl->BulkInPipeIndex,
  74. (PBYTE)"\x1B*a0T",5,0);
  75. if(bSuccess){
  76. return RawWrite(m_pDeviceControl->BulkInPipeIndex,
  77. (PBYTE)"\x1B*a1G",5,0);
  78. }
  79. break;
  80. case 1: // WIA_DATA_GRAYSCALE
  81. bSuccess = RawWrite(m_pDeviceControl->BulkInPipeIndex,
  82. (PBYTE)"\x1B*a4T",5,0);
  83. if(bSuccess){
  84. return RawWrite(m_pDeviceControl->BulkInPipeIndex,
  85. (PBYTE)"\x1B*a8G",5,0);
  86. }
  87. break;
  88. case 2: // WIA_DATA_COLOR
  89. bSuccess = RawWrite(m_pDeviceControl->BulkInPipeIndex,
  90. (PBYTE)"\x1B*a5T",5,0);
  91. if(bSuccess){
  92. return RawWrite(m_pDeviceControl->BulkInPipeIndex,
  93. (PBYTE)"\x1B*a24G",6,0);
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. return FALSE;
  100. }
  101. BOOL CHPSCL::Scan()
  102. {
  103. LONG lBytesRead = 1;
  104. DWORD dwTotalImageSize = 0;
  105. DWORD dwbpp = 0;
  106. DWORD BytesPerLine = 0;
  107. switch(m_datatype){
  108. case 0:
  109. dwbpp = 1;
  110. BytesPerLine = ((m_xext +7)/8);
  111. dwTotalImageSize = (BytesPerLine * m_yext);
  112. break;
  113. case 1:
  114. dwbpp = 8;
  115. BytesPerLine = m_xext;
  116. dwTotalImageSize = (m_xext * m_yext);
  117. break;
  118. case 2:
  119. dwbpp = 24;
  120. BytesPerLine = (m_xext * 3);
  121. dwTotalImageSize = ((m_xext * m_yext) * 3);
  122. break;
  123. default:
  124. return FALSE;
  125. break;
  126. }
  127. //
  128. // setup data dumper, so we can see if the image needs
  129. // work.
  130. //
  131. DATA_DESCRIPTION DataDesc;
  132. DataDesc.dwbpp = dwbpp;
  133. DataDesc.dwDataSize = dwTotalImageSize;
  134. DataDesc.dwHeight = m_yext;
  135. DataDesc.dwWidth = m_xext;
  136. DataDesc.pRawData = (PBYTE)LocalAlloc(LPTR,DataDesc.dwDataSize + 1024);
  137. PBYTE pbuffer = DataDesc.pRawData;
  138. Trace(TEXT("Total bytes to Read: = %d"),dwTotalImageSize);
  139. //
  140. // start scan
  141. //
  142. if(!RawWrite(m_pDeviceControl->BulkInPipeIndex,(PBYTE)"\x1B*f0S",5,0)){
  143. MessageBox(NULL,TEXT("Starting Scan failed.."),TEXT(""),MB_OK);
  144. return FALSE;
  145. }
  146. //
  147. // read scanned data
  148. //
  149. if(!RawRead(m_pDeviceControl->BulkOutPipeIndex,pbuffer,(dwTotalImageSize),&lBytesRead,0)){
  150. MessageBox(NULL,TEXT("Reading first band failed"),TEXT(""),MB_OK);
  151. return FALSE;
  152. }
  153. Trace(TEXT("Total Bytes Read: = %d"),lBytesRead);
  154. /*
  155. pbuffer+=lBytesRead;
  156. dwTotalImageSize -=lBytesRead;
  157. Trace(TEXT("Bytes left to read: = %d"),dwTotalImageSize);
  158. while(dwTotalImageSize > 0){
  159. lBytesRead = 0;
  160. //
  161. // read scanned data (continued..)
  162. //
  163. if(!RawRead(m_pDeviceControl->BulkOutPipeIndex,pbuffer,(dwTotalImageSize),&lBytesRead,0)){
  164. MessageBox(NULL,TEXT("Reading band failed"),TEXT(""),MB_OK);
  165. dwTotalImageSize = 0;
  166. }
  167. pbuffer+=lBytesRead;
  168. dwTotalImageSize -=lBytesRead;
  169. Trace(TEXT("Bytes left to read: = %d"),dwTotalImageSize);
  170. }
  171. */
  172. CDATADUMP Data;
  173. Data.DumpDataToBitmap(TEXT("HPSCL.BMP"),&DataDesc);
  174. if(NULL != DataDesc.pRawData)
  175. LocalFree(DataDesc.pRawData);
  176. return TRUE;
  177. }
  178. BOOL CHPSCL::RawWrite(LONG lPipeNum,BYTE *pbuffer,LONG lbuffersize,LONG lTimeout)
  179. {
  180. DWORD dwBytesWritten = 0;
  181. BOOL bSuccess = TRUE;
  182. bSuccess = WriteFile(m_pDeviceControl->DeviceIOHandles[lPipeNum],
  183. pbuffer,
  184. lbuffersize,
  185. &dwBytesWritten,
  186. NULL);
  187. if(dwBytesWritten < (ULONG)lbuffersize)
  188. return FALSE;
  189. return bSuccess;
  190. }
  191. BOOL CHPSCL::RawRead(LONG lPipeNum,BYTE *pbuffer,LONG lbuffersize,LONG *plbytesread,LONG lTimeout)
  192. {
  193. DWORD dwBytesRead = 0;
  194. BOOL bSuccess = ReadFile(m_pDeviceControl->DeviceIOHandles[lPipeNum],
  195. pbuffer,
  196. lbuffersize,
  197. &dwBytesRead,
  198. NULL);
  199. *plbytesread = dwBytesRead;
  200. return bSuccess;
  201. }
  202. VOID CHPSCL::Trace(LPCTSTR format,...)
  203. {
  204. TCHAR Buffer[1024];
  205. va_list arglist;
  206. va_start(arglist, format);
  207. wvsprintf(Buffer, format, arglist);
  208. va_end(arglist);
  209. OutputDebugString(Buffer);
  210. OutputDebugString(TEXT("\n"));
  211. }