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.

351 lines
8.5 KiB

  1. /*==============================================================================
  2. This module provides RBA rendering support for viewing faxes.
  3. 03-Mar-94 RajeevD Created.
  4. ==============================================================================*/
  5. #ifdef VIEWRBA
  6. #include <memory.h>
  7. #include "viewrend.hpp"
  8. #include "resexec.h"
  9. #define COMMON_SIZE 6
  10. //==============================================================================
  11. RBAVIEW::RBAVIEW (DWORD nType)
  12. {
  13. _fmemset ((LPBYTE) this + sizeof(LPVOID), 0, sizeof(RBAVIEW) - sizeof(LPVOID));
  14. nTypeOut = nType;
  15. }
  16. //==============================================================================
  17. RBAVIEW::~RBAVIEW ()
  18. {
  19. if (hHRE)
  20. uiHREClose (hHRE);
  21. for (UINT iRes = 0; iRes < 256; iRes++)
  22. if (ResDir[iRes])
  23. GlobalFreePtr (ResDir[iRes]);
  24. if (lpCodec)
  25. GlobalFreePtr (lpCodec);
  26. if (bufIn.lpbBegBuf)
  27. GlobalFreePtr (bufIn.lpbBegBuf);
  28. }
  29. //==============================================================================
  30. BOOL RBAVIEW::Init (LPVOID lpFilePath, LPVIEWINFO lpvi, LPWORD lpwBandSize)
  31. {
  32. ENDJOB EndJob;
  33. if (!Open (lpFilePath, 0))
  34. return_error (("VIEWREND could not reopen spool file!\r\n"));
  35. if (!Read((LPBYTE) &BegJob, sizeof(BegJob)))
  36. return_error (("VIEWREND could not read spool header!\r\n"));
  37. dwOffset[0] = Tell();
  38. DEBUGCHK (lpwBandSize);
  39. *lpwBandSize = (WORD) BegJob.xBand/8 * (WORD) BegJob.yBand + OUTBUF_SLACK;
  40. if (BegJob.cResDir)
  41. {
  42. hHRE = hHREOpen (NULL, (UINT) BegJob.xBand/8, (UINT) BegJob.cResDir);
  43. if (!hHRE)
  44. return_error (("VIEWREND could not initialize resource executor!\r\n"));
  45. }
  46. if (1)
  47. {
  48. FC_PARAM fcp;
  49. UINT cbCodec;
  50. // Query for codec size.
  51. fcp.nTypeIn = MMR_DATA;
  52. fcp.nTypeOut = LRAW_DATA;
  53. fcp.cbLine = (UINT) BegJob.xBand / 8;
  54. cbCodec = FaxCodecInit (NULL, &fcp);
  55. DEBUGCHK (cbCodec);
  56. // Allocate codec context.
  57. lpCodec = GlobalAllocPtr (0, cbCodec);
  58. if (!lpCodec)
  59. return_error (("VIEWREND could allocate codec context!\r\n"));
  60. FaxCodecInit (lpCodec, &fcp);
  61. bufIn.wLengthBuf = 2000;
  62. bufIn.lpbBegBuf = (LPBYTE) GlobalAllocPtr (0, bufIn.wLengthBuf);
  63. if (!bufIn.lpbBegBuf)
  64. return_error (("VIEWREND could not allocate input buffer!\r\n"));
  65. }
  66. // Fill VIEWINFO.
  67. lpvi->xRes = BegJob.xRes;
  68. lpvi->yRes = BegJob.yRes;
  69. if
  70. ( Seek (- (long) sizeof(ENDJOB), SEEK_END)
  71. && Read (&EndJob, sizeof(ENDJOB))
  72. && EndJob.dwID == ID_ENDJOB
  73. )
  74. {
  75. lpvi->cPage = EndJob.cPage;
  76. lpvi->yMax = EndJob.yMax;
  77. }
  78. else
  79. {
  80. lpvi->cPage = 0;
  81. while (SetPage (lpvi->cPage))
  82. lpvi->cPage++;
  83. lpvi->yMax = 0;
  84. }
  85. return SetPage (0);
  86. }
  87. //==============================================================================
  88. BOOL RBAVIEW::SetPage (UINT iPage)
  89. {
  90. if (iPage < iMaxPage)
  91. {
  92. Seek (dwOffset[iPage], STREAM_SEEK_SET); // BKD: changed to STREAM_SEEK_SET
  93. return TRUE;
  94. }
  95. Seek (dwOffset[iMaxPage], STREAM_SEEK_SET); // BKD: changed to STREAM_SEEK_SET
  96. while (1)
  97. {
  98. RESHDR Header;
  99. FRAME Frame;
  100. if (!Read ((LPBYTE) &Header, sizeof(Header)))
  101. return_error (("VIEWREND could not read RBA resource header!"));
  102. switch (Header.wClass)
  103. {
  104. case ID_GLYPH:
  105. case ID_BRUSH:
  106. {
  107. UINT cbRaw;
  108. // Allocate mmeory from cache.
  109. Frame.lpData = (LPBYTE) GlobalAllocPtr (0, Header.cbRest);
  110. if (!Frame.lpData)
  111. return_error (("VIEWREND could not allocate memory!\r\n"));
  112. // Read resource from stream.
  113. if (!Read (Frame.lpData + COMMON_SIZE, Header.cbRest - COMMON_SIZE))
  114. return_error (("VIEWREND could not read resource!\r\n"));
  115. // Trap chaingon compressed glyph sets.
  116. cbRaw = HIWORD (Header.dwID);
  117. if (cbRaw)
  118. {
  119. LPVOID lpRaw;
  120. DEBUGCHK (Header.wClass == ID_GLYPH);
  121. if (!(lpRaw = GlobalAllocPtr (0, cbRaw)))
  122. return_error (("VIEWREND could not allocate decompression buffer!\r\n"));
  123. UnpackGlyphSet (Frame.lpData, lpRaw);
  124. GlobalFreePtr (Frame.lpData);
  125. Header.cbRest = (USHORT)cbRaw;
  126. Header.dwID = LOWORD (Header.dwID);
  127. Frame.lpData = (LPBYTE) lpRaw;
  128. }
  129. // Past common header.
  130. _fmemcpy (Frame.lpData, &Header.dwID, COMMON_SIZE);
  131. Frame.wSize = Header.cbRest;
  132. // Add resource to directory.
  133. uiHREWrite (hHRE, &Frame, 1);
  134. ResDir[Header.dwID] = Frame.lpData;
  135. break;
  136. }
  137. case ID_CONTROL:
  138. if (Header.dwID == ID_ENDPAGE)
  139. {
  140. iMaxPage++;
  141. dwOffset [iMaxPage] = Tell ();
  142. if (iPage < iMaxPage)
  143. {
  144. // BKD: changed to STREAM_SEEK_SET
  145. Seek (dwOffset[iPage], STREAM_SEEK_SET);
  146. return TRUE;
  147. }
  148. }
  149. // Yes, fall through to default case!
  150. default:
  151. // Skip everything else.
  152. if (!Seek (Header.cbRest - COMMON_SIZE, SEEK_CUR))
  153. return_error (("VIEWREND could not skip unknown RBA resource"));
  154. } // switch (Header.wClass)
  155. } // while (1)
  156. }
  157. //==============================================================================
  158. BOOL RBAVIEW::GetBand (LPBITMAP lpbmBand)
  159. {
  160. DEBUGCHK (lpbmBand && lpbmBand->bmBits);
  161. lpbmBand->bmType = 0;
  162. lpbmBand->bmWidth = (WORD) BegJob.xBand;
  163. lpbmBand->bmWidthBytes = lpbmBand->bmWidth / 8;
  164. lpbmBand->bmPlanes = 1;
  165. lpbmBand->bmBitsPixel = 1;
  166. while (1)
  167. {
  168. RESHDR Header;
  169. if (!Read ((LPBYTE) &Header, sizeof(Header)))
  170. return FALSE;
  171. switch (Header.wClass)
  172. {
  173. case ID_RPL:
  174. return ExecuteRPL (lpbmBand, &Header);
  175. case ID_BAND:
  176. return ExecuteBand (lpbmBand, &Header);
  177. case ID_CONTROL:
  178. // Trap page breaks.
  179. if (Header.dwID == ID_ENDPAGE)
  180. {
  181. Seek (-8, SEEK_CUR);
  182. lpbmBand->bmHeight = 0;
  183. return TRUE;
  184. }
  185. // Yes, fall through to default case!
  186. default:
  187. // Skip everything else.
  188. if (!Seek (Header.cbRest - COMMON_SIZE, SEEK_CUR))
  189. return FALSE;
  190. } // switch (Header.wClass)
  191. } // while (1)
  192. }
  193. //==============================================================================
  194. BOOL RBAVIEW::ExecuteRPL (LPBITMAP lpbmBand, LPRESHDR lpHeader)
  195. {
  196. FRAME Frame;
  197. // Clear band.
  198. lpbmBand->bmHeight = (WORD) BegJob.yBand;
  199. _fmemset (lpbmBand->bmBits, 0, lpbmBand->bmHeight * lpbmBand->bmWidthBytes);
  200. // Trap blank bands.
  201. if (lpHeader->cbRest == COMMON_SIZE)
  202. return TRUE;
  203. // Allocate RPL.
  204. Frame.lpData = (LPBYTE) GlobalAllocPtr (0, lpHeader->cbRest);
  205. if (!Frame.lpData)
  206. return_error (("VIEWREND could not allocate RPL!\r\n"));
  207. // Load RPL.
  208. Frame.wSize = lpHeader->cbRest;
  209. _fmemcpy (Frame.lpData, &lpHeader->dwID, COMMON_SIZE);
  210. Read (Frame.lpData + COMMON_SIZE, Frame.wSize - COMMON_SIZE);
  211. // Execute RPL.
  212. uiHREWrite (hHRE, &Frame, 1);
  213. uiHREExecute (hHRE, lpbmBand, NULL);
  214. // Free RPL.
  215. GlobalFreePtr (Frame.lpData);
  216. return TRUE;
  217. }
  218. //==============================================================================
  219. BOOL RBAVIEW::ExecuteBand (LPBITMAP lpbmBand, LPRESHDR lpHeader)
  220. {
  221. BMPHDR bmh;
  222. UINT cbIn;
  223. FC_PARAM fcp;
  224. BUFFER bufOut;
  225. // Read bitmap header.
  226. if (!Read ((LPBYTE) &bmh, sizeof(bmh)))
  227. return FALSE;
  228. lpbmBand->bmHeight = bmh.wHeight;
  229. cbIn = lpHeader->cbRest - COMMON_SIZE - sizeof(bmh);
  230. // Trap uncompressed bands.
  231. if (!bmh.bComp)
  232. {
  233. if (!Read (lpbmBand->bmBits, cbIn))
  234. return FALSE;
  235. if (nTypeOut == LRAW_DATA)
  236. {
  237. BUFFER bufOut2;
  238. bufOut2.lpbBegData = (LPBYTE) lpbmBand->bmBits;
  239. bufOut2.wLengthData = (USHORT)cbIn;
  240. bufOut2.dwMetaData = HRAW_DATA;
  241. BitReverseBuf (&bufOut2);
  242. }
  243. return TRUE;
  244. }
  245. // Initialize codec.
  246. fcp.nTypeIn = bmh.bComp >> 2;
  247. fcp.nTypeOut = LRAW_DATA;
  248. fcp.cbLine = (WORD) BegJob.xBand / 8;
  249. FaxCodecInit (lpCodec, &fcp);
  250. // Initialize input.
  251. bufIn.dwMetaData = fcp.nTypeIn;
  252. // Initialize output.
  253. bufOut.lpbBegBuf = (LPBYTE) lpbmBand->bmBits;
  254. bufOut.wLengthBuf = fcp.cbLine * bmh.wHeight;
  255. bufOut.lpbBegData = bufOut.lpbBegBuf;
  256. bufOut.wLengthData = 0;
  257. bufOut.dwMetaData = fcp.nTypeOut;
  258. // Convert.
  259. while (cbIn)
  260. {
  261. bufIn.lpbBegData = bufIn.lpbBegBuf;
  262. bufIn.wLengthData = min (cbIn, bufIn.wLengthBuf);
  263. if (!Read (bufIn.lpbBegData, bufIn.wLengthData))
  264. return FALSE;
  265. cbIn -= bufIn.wLengthData;
  266. if (FaxCodecConvert (lpCodec, &bufIn, &bufOut) == FC_DECODE_ERR)
  267. return_error (("VIEWREND MMR decode error!\r\n"));
  268. } // while (cbIn)
  269. if (nTypeOut == HRAW_DATA)
  270. BitReverseBuf (&bufOut);
  271. return TRUE;
  272. }
  273. #endif // VIEWRBA