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.

1197 lines
48 KiB

  1. //***************************************************************************************************
  2. // PRNCTL.C
  3. //
  4. // Functions of controlling printer
  5. //---------------------------------------------------------------------------------------------------
  6. // copyright(C) 1997-2000 CASIO COMPUTER CO.,LTD. / CASIO ELECTRONICS MANUFACTURING CO.,LTD.
  7. //***************************************************************************************************
  8. #include "PDEV.H"
  9. #include <stdio.h>
  10. #include "PRNCTL.H"
  11. #include "strsafe.h" // Security-Code 2002.3.6
  12. // Replacement of strsafe-api 2002.3.6 >>>
  13. //#ifdef wsprintf
  14. //#undef wsprintf
  15. //#endif // wsprintf
  16. //#define wsprintf sprintf
  17. // Replacement of strsafe-api 2002.3.6 <<<
  18. //***************************************************************************************************
  19. // Data define
  20. //***************************************************************************************************
  21. //---------------------------------------------------------------------------------------------------
  22. // Type of pressing raster image
  23. //---------------------------------------------------------------------------------------------------
  24. #define RASTER_COMP 0 // Press
  25. #define RASTER_NONCOMP 1 // Not press
  26. #define RASTER_EMPTY 2 // Empty
  27. //---------------------------------------------------------------------------------------------------
  28. // Buffer for setting command
  29. //---------------------------------------------------------------------------------------------------
  30. static BYTE CmdBuf[1 * 1024]; // 1KB
  31. //---------------------------------------------------------------------------------------------------
  32. // Structure for setting command
  33. //---------------------------------------------------------------------------------------------------
  34. typedef const struct {
  35. WORD Size; // Command size
  36. LPBYTE Cmd; // Command buffer
  37. } CMDDEF, FAR *LPCMDDEF;
  38. //===================================================================================================
  39. // Command define
  40. //===================================================================================================
  41. //---------------------------------------------------------------------------------------------------
  42. // Change mode
  43. //---------------------------------------------------------------------------------------------------
  44. static CMDDEF ModOrgIn = { 4, "\x1b""z""\xd0\x01"}; // ESC/Page -> original
  45. static CMDDEF ModOrgOut = { 4, "\x1b""z""\x00\x01"}; // original -> ESC/Page
  46. //---------------------------------------------------------------------------------------------------
  47. // Setting overwrite
  48. //---------------------------------------------------------------------------------------------------
  49. static CMDDEF CfgWrtMod = { 6, "\x1d""%uowE"}; // Setting overwrite
  50. //---------------------------------------------------------------------------------------------------
  51. // Setting spool positon
  52. //---------------------------------------------------------------------------------------------------
  53. static CMDDEF PosAbsHrz = { 4, "\x1d""%dX"}; // Horizontal
  54. static CMDDEF PosAbsVtc = { 4, "\x1d""%dY"}; // Vertical
  55. //---------------------------------------------------------------------------------------------------
  56. // Spool bitmap data
  57. //---------------------------------------------------------------------------------------------------
  58. static CMDDEF ImgDrw = {16, "\x1d""%u;%u;%u;%dbi{I"}; // Spool bit image
  59. static CMDDEF ImgRasStr = {15, "\x1d""%u;%u;%u;%dbrI"}; // Start spool raster image
  60. static CMDDEF ImgRasEnd = { 4, "\x1d""erI"}; // End spool raster image
  61. static CMDDEF ImgRasDrw = { 6, "\x1d""%ur{I"}; // Spool raster image
  62. static CMDDEF ImgRasNon = { 6, "\x1d""%uu{I"}; // Spool raster image(Not press)
  63. static CMDDEF ImgRasEpy = { 5, "\x1d""%ueI"}; // Spool empty raster image
  64. //---------------------------------------------------------------------------------------------------
  65. // CASIO original
  66. //---------------------------------------------------------------------------------------------------
  67. static CMDDEF OrgColCmy = {15, "Cc,%u,%u,%u,%u*"}; // CMYK
  68. static CMDDEF OrgDrwPln = {15, "Da,%u,%u,%u,%u*"}; // Setting plane
  69. static CMDDEF OrgImgCmy = {26, "Cj%w,%u,%u,%u,%l,%l,%u,%u*"}; // CMYK bitimage
  70. static BYTE OVERWRITE[] =
  71. "\x1D" "1owE" //MOV1
  72. "\x1D" "0tsE";
  73. //***************************************************************************************************
  74. // Prototype declaration
  75. //***************************************************************************************************
  76. static WORD PlaneCmdStore(PDEVOBJ, LPBYTE, WORD);
  77. static void BitImgImgCmd(PDEVOBJ, WORD, WORD, WORD, WORD, WORD, WORD, LPBYTE);
  78. static BOOL RasterImgCmd(PDEVOBJ, WORD, WORD, WORD, WORD, WORD, WORD, WORD, LPBYTE, LPBYTE);
  79. static WORD RasterSize(WORD, WORD, WORD, WORD, LPBYTE);
  80. static WORD RasterComp(LPBYTE, WORD, LPBYTE, LPBYTE, LPWORD);
  81. static void CMYKImgCmd(PDEVOBJ, WORD, LONG, LONG, WORD, WORD, WORD, WORD, WORD, DWORD, DWORD, LPBYTE, LPBYTE, LONG, LONG);
  82. static WORD CmdCopy(LPBYTE, LPCMDDEF);
  83. static WORD CmdStore(LPBYTE, LPCMDDEF, LPINT);
  84. static WORD INTtoASC(LPBYTE, int);
  85. static WORD USINTtoASC(LPBYTE, WORD);
  86. static WORD LONGtoASC(LPBYTE, LONG);
  87. static WORD USLONGtoASC(LPBYTE, DWORD);
  88. //***************************************************************************************************
  89. // Functions
  90. //***************************************************************************************************
  91. //===================================================================================================
  92. // Spool bitmap data
  93. //===================================================================================================
  94. void FAR PASCAL PrnBitmap(
  95. PDEVOBJ pdevobj, // Pointer to PDEVOBJ structure
  96. LPDRWBMP lpBmp // Pointer to DRWBMP structure
  97. )
  98. {
  99. WORD siz,size;
  100. WORD comp;
  101. WORD width; // dot
  102. WORD height; // dot
  103. WORD widthByte; // byte
  104. LPBYTE lpTmpBuf;
  105. LPBYTE lpSchBit;
  106. LPBYTE lpBit; // Pointer to Bitmap data
  107. POINT drwPos;
  108. WORD higSiz; // dot
  109. WORD higCnt;
  110. WORD strHigCnt;
  111. WORD widLCnt; // Width from the left edge
  112. WORD widRCnt; // Width from the right edge
  113. WORD invLft; // Invalid size from the left edge
  114. WORD invRgt; // Invalid size from the right edge
  115. WORD img1st; // Spool first image data?
  116. int pam[4];
  117. int palm[1];
  118. PMYPDEV pOEM = (PMYPDEV)pdevobj->pdevOEM;
  119. lpTmpBuf = NULL;
  120. width = lpBmp->Width;
  121. height = lpBmp->Height;
  122. widthByte = lpBmp->WidthByte;
  123. comp = No;
  124. MY_VERBOSE((" PB "));
  125. img1st = Yes;
  126. lpSchBit = lpBmp->lpBit;
  127. for (higCnt = 0; higCnt < height; higCnt++) {
  128. higSiz = 0;
  129. invLft = 0;
  130. invRgt = 0;
  131. for (; higCnt < height; higCnt++) { // 1 Spool bitmap data
  132. // Search NULL data from the left edge
  133. for (widLCnt = 0; widLCnt < widthByte; widLCnt++) {
  134. if (lpSchBit[widLCnt] != 0x00) {
  135. if (higSiz == 0) { // first line?
  136. strHigCnt = higCnt;
  137. invLft = widLCnt; // Invalid size from the left edge
  138. } else {
  139. if (invLft > widLCnt) {
  140. invLft = widLCnt; // Renew invalid size from the left edge
  141. }
  142. }
  143. // Search NULL data from the right edge
  144. for (widRCnt = 0; widRCnt < widthByte; widRCnt++) {
  145. if (lpSchBit[widthByte - widRCnt - 1] != 0x00) {
  146. if (higSiz == 0) { // first line?
  147. invRgt = widRCnt; // Invalid size from the right edge
  148. } else {
  149. if (invRgt > widRCnt) {
  150. invRgt = widRCnt; // Renew invalid size from the right edge
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. higSiz++; // Renew height size
  157. break;
  158. }
  159. }
  160. lpSchBit += widthByte; // Next line bitmap data
  161. if (widLCnt == widthByte && higSiz != 0) { // 1line all NULL data & There were data except NULL data in previous line
  162. break; // Go to spool bitmap data
  163. }
  164. }
  165. if (higSiz != 0) { // There are data for spool?
  166. if (img1st == Yes) { // Spool for the first time
  167. // Compress?
  168. if (pOEM->iCompress != XX_COMPRESS_OFF) {
  169. if ((lpTmpBuf = MemAllocZ(widthByte * height)) != NULL) {
  170. comp = Yes;
  171. }
  172. }
  173. // Original mode in
  174. siz = CmdCopy(CmdBuf, &ModOrgIn);
  175. // Color?
  176. if (pOEM->iColor == XX_COLOR_SINGLE
  177. || pOEM->iColor == XX_COLOR_MANY
  178. || pOEM->iColor == XX_COLOR_MANY2) {
  179. pam[0] = lpBmp->Color.Cyn;
  180. pam[1] = lpBmp->Color.Mgt;
  181. pam[2] = lpBmp->Color.Yel;
  182. pam[3] = lpBmp->Color.Bla;
  183. siz += CmdStore(CmdBuf + siz, &OrgColCmy, pam);
  184. siz += PlaneCmdStore(pdevobj, CmdBuf + siz, lpBmp->Plane);
  185. }
  186. // Original mode out
  187. siz += CmdCopy(CmdBuf + siz, &ModOrgOut);
  188. if (siz != 0) { // There are data for spool?
  189. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  190. }
  191. img1st = No; // Not first time
  192. }
  193. drwPos.x = lpBmp->DrawPos.x + invLft * 8; // x coordinates
  194. drwPos.y = lpBmp->DrawPos.y + strHigCnt; // y coordinates
  195. palm[0] = drwPos.x;
  196. siz = CmdStore(CmdBuf, &PosAbsHrz, palm);
  197. palm[0] = drwPos.y;
  198. siz += CmdStore(CmdBuf + siz, &PosAbsVtc, palm);
  199. if (siz != 0) { // There are data for spool?
  200. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  201. }
  202. lpBit = lpBmp->lpBit + widthByte * strHigCnt;
  203. if (comp == Yes) { // Compress?
  204. if (RasterImgCmd(pdevobj, pOEM->iCompress, width, higSiz,
  205. widthByte, 0, invLft, invRgt, lpBit, lpTmpBuf) == No) {
  206. comp = No; // But compress rate is poor, no compress
  207. }
  208. }
  209. if (comp == No) { // Not compress
  210. BitImgImgCmd(pdevobj, width, higSiz, widthByte, 0, invLft, invRgt, lpBit);
  211. }
  212. }
  213. }
  214. if (lpTmpBuf) {
  215. MemFree(lpTmpBuf);
  216. }
  217. return;
  218. }
  219. //===================================================================================================
  220. // Spool CMYK bitmap data
  221. //===================================================================================================
  222. void FAR PASCAL PrnBitmapCMYK(
  223. PDEVOBJ pdevobj, // Pointer to PDEVOBJ structure
  224. LPDRWBMPCMYK lpBmp // Pointer to DRWBMPCMYK structure
  225. )
  226. {
  227. WORD siz;
  228. WORD comp;
  229. WORD width; // dot
  230. WORD height; // dot
  231. WORD widthByte;
  232. LPBYTE lpSchBit; // Pointer to bitmap data
  233. LPBYTE lpBit; // Pointer to bitmap data
  234. LONG xPos;
  235. LONG yPos;
  236. WORD posClpLft; // Clipping dot size
  237. DWORD posAdj; // 1/7200inch
  238. WORD higSiz; // dot
  239. WORD higCnt;
  240. WORD strHigCnt;
  241. WORD widLCnt;
  242. WORD widRCnt;
  243. WORD invLft; // Invalid size from the left edge
  244. WORD invRgt; // Invalid size from the right edge
  245. DWORD invLftBit; // Invalid bit size from the left edge
  246. DWORD invRgtBit; // Invalid bit size from the right edge
  247. WORD rgtBit; // Valid bit size from the right edge
  248. WORD img1st; // Spool for the first time
  249. DWORD dstSiz;
  250. LPBYTE lpDst;
  251. LPBYTE lpTmp;
  252. int pam[1];
  253. WORD img1st_2; // Spool for the first time
  254. PMYPDEV pOEM = (PMYPDEV)pdevobj->pdevOEM;
  255. MY_VERBOSE((" CM \n"));
  256. // Check of zero divide 2002.3.23 >>>
  257. if (lpBmp->DataBit == 0) {
  258. ERR(("PrnBitmapCMYK() 0Div-Check [lpBmp->DataBit=0] \n"));
  259. return;
  260. }
  261. // Check of zero divide 2002.3.23 <<<
  262. lpTmp = NULL;
  263. posAdj = 7200 / pOEM->Col.wReso; // 1/7200inch
  264. width = lpBmp->Width;
  265. height = lpBmp->Height;
  266. widthByte = lpBmp->WidthByte;
  267. comp = No;
  268. if (pOEM->iCompress != XX_COMPRESS_OFF) {
  269. if ((lpTmp = MemAllocZ(widthByte * height)) != NULL) {
  270. comp = Yes;
  271. }
  272. }
  273. img1st = Yes;
  274. img1st_2 = Yes;
  275. lpSchBit = lpBmp->lpBit;
  276. for (higCnt = 0; higCnt < height; higCnt++) {
  277. higSiz = 0;
  278. invLft = 0;
  279. invRgt = 0;
  280. for (; higCnt < height; higCnt++) { // 1 Spool bitmap data
  281. // Search NULL data from the left edge
  282. for (widLCnt = 0; widLCnt < widthByte; widLCnt++) {
  283. if (lpSchBit[widLCnt] != 0x00) {
  284. if (higSiz == 0) {
  285. strHigCnt = higCnt; // first line
  286. invLft = widLCnt; // Invalid size from the left edge
  287. } else {
  288. if (invLft > widLCnt) {
  289. invLft = widLCnt; // Renew invalid size from the left edge
  290. }
  291. }
  292. // Search NULL data from the right edge
  293. for (widRCnt = 0; widRCnt < widthByte; widRCnt++) {
  294. if (lpSchBit[widthByte - widRCnt - 1] != 0x00) {
  295. if (higSiz == 0) { // first line
  296. invRgt = widRCnt; // Invalid size from the right edge
  297. } else {
  298. if (invRgt > widRCnt) {
  299. invRgt = widRCnt; // Renew size from the right edge
  300. }
  301. }
  302. break;
  303. }
  304. }
  305. higSiz++; // Renew height size
  306. break;
  307. }
  308. }
  309. lpSchBit += widthByte; // Next line bitmap data
  310. if (widLCnt == widthByte && higSiz != 0) { // 1line all NULL data & There were data except NULL data in previous line
  311. break; // goto spool
  312. }
  313. }
  314. if (higSiz != 0) { // There are data for spool
  315. if (img1st_2 == Yes) {
  316. WRITESPOOLBUF(pdevobj, OVERWRITE, BYTE_LENGTH(OVERWRITE));
  317. img1st_2 = No;
  318. }
  319. // When Colormode is XX_COLOR_MANY or XX_COLOR_MANY2 ,not compress
  320. if (comp == Yes) {
  321. if (pOEM->iColor == XX_COLOR_MANY || pOEM->iColor == XX_COLOR_MANY2) {
  322. comp = No;
  323. }
  324. }
  325. if (comp == No && img1st == Yes) {
  326. // Original mode in
  327. siz = CmdCopy(CmdBuf, &ModOrgIn);
  328. // Plane
  329. siz += PlaneCmdStore(pdevobj, CmdBuf + siz, lpBmp->Plane);
  330. if (siz != 0) { // There are data for spool
  331. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  332. }
  333. img1st = No; // not first
  334. }
  335. invLftBit = (DWORD)invLft * 8;
  336. if (invRgt != 0) {
  337. if ((rgtBit = (WORD)((DWORD)width * lpBmp->DataBit % 8)) == 0) {
  338. rgtBit = 8;
  339. }
  340. if (rgtBit == 8) {
  341. invRgtBit = (DWORD)invRgt * 8;
  342. } else {
  343. invRgtBit = ((DWORD)invRgt - 1) * 8 + rgtBit;
  344. }
  345. } else {
  346. invRgtBit = 0;
  347. }
  348. posClpLft = (WORD)(invLftBit / lpBmp->DataBit);
  349. // Start position of spooling
  350. xPos = ((LONG)lpBmp->DrawPos.x + posClpLft) * posAdj;
  351. yPos = ((LONG)lpBmp->DrawPos.y + strHigCnt) * posAdj;
  352. lpBit = lpBmp->lpBit + widthByte * strHigCnt;
  353. // Spool CMYK bit image
  354. CMYKImgCmd(pdevobj, comp, xPos, yPos, lpBmp->Frame, lpBmp->DataBit, width, higSiz, widthByte,
  355. invLftBit, invRgtBit, lpBit, lpTmp, (LONG)lpBmp->DrawPos.x + posClpLft, (LONG)lpBmp->DrawPos.y + strHigCnt);
  356. }
  357. }
  358. if (img1st == No) { // Already spool
  359. // Original mode out
  360. siz = CmdCopy(CmdBuf, &ModOrgOut);
  361. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  362. }
  363. if (lpTmp) {
  364. MemFree(lpTmp);
  365. }
  366. return;
  367. }
  368. //===================================================================================================
  369. // Spool plane command
  370. //===================================================================================================
  371. WORD PlaneCmdStore( // Size of command
  372. PDEVOBJ pdevobj, // Pointer to PDEVOBJ structure
  373. LPBYTE lpDst,
  374. WORD Plane
  375. )
  376. {
  377. int pam[4];
  378. WORD siz;
  379. PMYPDEV pOEM = (PMYPDEV)pdevobj->pdevOEM;
  380. if (Plane & PLN_CYAN) {
  381. pam[0] = 0; // Spool
  382. } else {
  383. pam[0] = 1; // Not spool
  384. }
  385. if (Plane & PLN_MGENTA) {
  386. pam[1] = 0; // Spool
  387. } else {
  388. pam[1] = 1; // Not spool
  389. }
  390. if (Plane & PLN_YELLOW) {
  391. pam[2] = 0; // Spool
  392. } else {
  393. pam[2] = 1; // Not spool
  394. }
  395. if (Plane & PLN_BLACK) {
  396. pam[3] = 0; // Spool
  397. } else {
  398. pam[3] = 1; // Not spool
  399. }
  400. siz = CmdStore(lpDst, &OrgDrwPln, pam);
  401. return siz;
  402. }
  403. //===================================================================================================
  404. // Spool bitimage command data
  405. //===================================================================================================
  406. void BitImgImgCmd(
  407. PDEVOBJ pdevobj, // Pointer to PDEVOBJ structure
  408. WORD Width, // dot
  409. WORD Height, // dot
  410. WORD WidthByte, // byte
  411. WORD Rotation, // rotare(0fixed)
  412. WORD InvLeft, // Invalid size from the left edge
  413. WORD InvRight, // Invalid size from the right edge
  414. LPBYTE lpBit // Bitmap data
  415. )
  416. {
  417. int pam[10];
  418. WORD siz;
  419. WORD widByt;
  420. WORD linCnt;
  421. if (InvLeft == 0 && InvRight == 0) { // There are no invalid size
  422. pam[0] = WidthByte * Height; // Number of Data byte
  423. pam[1] = Width;
  424. pam[2] = Height;
  425. pam[3] = Rotation;
  426. siz = CmdStore(CmdBuf, &ImgDrw, pam);
  427. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  428. WRITESPOOLBUF(pdevobj, lpBit, pam[0]);
  429. } else { // There are invalid size
  430. widByt = WidthByte - InvLeft - InvRight; // Width byte
  431. pam[0] = widByt * Height;
  432. if (InvRight == 0) { // There are no invalid size from the right edge
  433. pam[1] = Width - InvLeft * 8; // Width bit image
  434. } else {
  435. pam[1] = widByt * 8; // Width bit image
  436. }
  437. pam[2] = Height; // Height bit image
  438. pam[3] = Rotation;
  439. siz = CmdStore(CmdBuf, &ImgDrw, pam);
  440. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  441. for (linCnt = 0; linCnt < Height; linCnt++) { // Spool bitmap data by 1 line
  442. lpBit += InvLeft;
  443. WRITESPOOLBUF(pdevobj, lpBit, widByt);
  444. lpBit += widByt;
  445. lpBit += InvRight;
  446. }
  447. }
  448. return;
  449. }
  450. //===================================================================================================
  451. // Spool raster image command data
  452. //===================================================================================================
  453. BOOL RasterImgCmd(
  454. PDEVOBJ pdevobj, // Pointer to PDEVOBJ structure
  455. WORD Comp,
  456. WORD Width, // dot
  457. WORD Height, // dot
  458. WORD WidthByte, // byte
  459. WORD Rotation, // rotate(0:fixed)
  460. WORD InvLeft, // Invalid size from the left edge
  461. WORD InvRight, // Invalid size from the left edge
  462. LPBYTE lpBit, // Pointer to bitmap data
  463. LPBYTE lpBuf // Pointer to raster image data buffer
  464. )
  465. {
  466. int pam[4];
  467. WORD siz;
  468. WORD widByt;
  469. WORD setCnt;
  470. WORD ras; // Type of raster image
  471. WORD befRas; // Type of raster image(Privious line)
  472. LPBYTE lpLas; // Privious raster data
  473. WORD dstSiz; // byte size
  474. WORD rasSiz; // Raster image data byte size
  475. WORD rasEpy;
  476. MY_VERBOSE((" RAS "));
  477. widByt = WidthByte - InvLeft - InvRight; // Width byte (Not include invalid size)
  478. if (Comp == XX_COMPRESS_AUTO) {
  479. // Get raster image size
  480. rasSiz = RasterSize(Height, widByt, InvLeft, InvRight, lpBit);
  481. if (rasSiz > (widByt * Height / 5 * 4)) { // Raster rate is more than 80%
  482. return No; // Error
  483. }
  484. }
  485. pam[0] = 4;
  486. if (InvRight == 0) { // No invalid size from the right edge
  487. pam[1] = Width - InvLeft * 8; // Width
  488. } else {
  489. pam[1] = widByt * 8; // Width
  490. }
  491. pam[2] = Height; // Height
  492. pam[3] = Rotation;
  493. siz = CmdStore(CmdBuf, &ImgRasStr, pam);
  494. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  495. lpLas = NULL;
  496. rasSiz = 0;
  497. rasEpy = 0;
  498. for (setCnt = 0; setCnt < Height; setCnt++) {
  499. lpBit += InvLeft;
  500. // Compress
  501. ras = RasterComp(lpBuf + rasSiz, widByt, lpBit, lpLas, &dstSiz);
  502. if (setCnt != 0 && befRas != ras) { // Not same as raster state of previous line
  503. if (befRas == RASTER_COMP) {
  504. pam[0] = rasSiz;
  505. siz = CmdStore(CmdBuf, &ImgRasDrw, pam);
  506. WRITESPOOLBUF(pdevobj, CmdBuf, siz); // Spool command
  507. WRITESPOOLBUF(pdevobj, lpBuf, rasSiz); // Spool data
  508. rasSiz = 0;
  509. } else if (befRas == RASTER_EMPTY) {
  510. pam[0] = rasEpy;
  511. siz = CmdStore(CmdBuf, &ImgRasEpy, pam);
  512. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  513. rasEpy = 0;
  514. }
  515. } // Spool state of current line
  516. if (ras == RASTER_COMP) {
  517. rasSiz += dstSiz;
  518. } else if (ras == RASTER_EMPTY) {
  519. rasEpy++;
  520. } else {
  521. pam[0] = dstSiz;
  522. siz = CmdStore(CmdBuf, &ImgRasNon, pam);
  523. WRITESPOOLBUF(pdevobj, CmdBuf, siz); // Spool command
  524. WRITESPOOLBUF(pdevobj, lpBit, dstSiz); // Spool data
  525. }
  526. befRas = ras; // Renew
  527. lpLas = lpBit; // Renew
  528. lpBit += widByt; // Renew
  529. lpBit += InvRight;
  530. }
  531. if (rasSiz != 0) { // There are raster data without spooling
  532. pam[0] = rasSiz;
  533. siz = CmdStore(CmdBuf, &ImgRasDrw, pam);
  534. WRITESPOOLBUF(pdevobj, CmdBuf, siz); // Spool command
  535. WRITESPOOLBUF(pdevobj, lpBuf, rasSiz); // Spool data
  536. } else if (rasEpy != 0) { // There are empty raster data without spooling
  537. pam[0] = rasEpy;
  538. siz = CmdStore(CmdBuf, &ImgRasEpy, pam);
  539. WRITESPOOLBUF(pdevobj, CmdBuf, siz); // Spool command
  540. }
  541. siz = CmdCopy(CmdBuf, &ImgRasEnd);
  542. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  543. return Yes;
  544. }
  545. //===================================================================================================
  546. // Get size of raster image
  547. //===================================================================================================
  548. WORD RasterSize(
  549. WORD Height, // dot
  550. WORD WidthByte, // byte
  551. WORD InvLeft,
  552. WORD InvRight,
  553. LPBYTE lpBit
  554. )
  555. {
  556. WORD rasSiz;
  557. WORD chkCnt;
  558. WORD rasEpy;
  559. LPBYTE lpLas;
  560. WORD dstCnt;
  561. WORD srcCnt;
  562. WORD empSiz;
  563. BYTE cmpDat;
  564. WORD equCnt;
  565. rasSiz = 0;
  566. rasEpy = 0;
  567. lpLas = NULL;
  568. for (chkCnt = 0; chkCnt < Height; chkCnt++) { // Check size of raster image
  569. lpBit += InvLeft;
  570. srcCnt = WidthByte;
  571. for (; srcCnt != 0; srcCnt--) {
  572. if (lpBit[srcCnt - 1] != 0x00) {
  573. break;
  574. }
  575. }
  576. if (srcCnt == 0) { // 1 line All white data?
  577. rasEpy++;
  578. lpLas = lpBit;
  579. lpBit += WidthByte;
  580. lpBit += InvRight;
  581. continue;
  582. }
  583. if (rasEpy != 0) {
  584. rasSiz += 8;
  585. rasEpy = 0;
  586. }
  587. empSiz = WidthByte - srcCnt;
  588. for (dstCnt = 0, srcCnt = 0; srcCnt < WidthByte; ) {
  589. if (lpLas != NULL) {
  590. if (lpLas[srcCnt] == lpBit[srcCnt]) {
  591. equCnt = 1;
  592. srcCnt++;
  593. for (; srcCnt < WidthByte; srcCnt++) {
  594. if (lpLas[srcCnt] != lpBit[srcCnt]) {
  595. break;
  596. }
  597. equCnt++;
  598. }
  599. if (srcCnt == WidthByte) {
  600. rasSiz++;
  601. break;
  602. }
  603. }
  604. rasSiz++;
  605. if (equCnt >= 63) {
  606. rasSiz += ((equCnt / 255) + 1);
  607. }
  608. }
  609. if (srcCnt < (WidthByte - 1) && lpBit[srcCnt] == lpBit[srcCnt + 1]) {
  610. cmpDat = lpBit[srcCnt];
  611. equCnt = 2;
  612. for (srcCnt += 2; srcCnt < WidthByte; srcCnt++) {
  613. if (cmpDat != lpBit[srcCnt]) {
  614. break;
  615. }
  616. equCnt++;
  617. }
  618. rasSiz += 2;
  619. if (equCnt >= 63) {
  620. rasSiz += equCnt / 255 + 1;
  621. }
  622. } else {
  623. if (WidthByte < (dstCnt + 9)) {
  624. rasSiz += WidthByte - empSiz + 9;
  625. break;
  626. }
  627. if ((WidthByte - srcCnt) < 8) {
  628. rasSiz += WidthByte - srcCnt + 1;
  629. srcCnt += WidthByte - srcCnt;
  630. } else {
  631. rasSiz += 9;
  632. srcCnt += 8;
  633. }
  634. }
  635. }
  636. lpLas = lpBit;
  637. lpBit += WidthByte;
  638. lpBit += InvRight;
  639. }
  640. return rasSiz;
  641. }
  642. //===================================================================================================
  643. // Compress raster image
  644. //===================================================================================================
  645. WORD RasterComp(
  646. LPBYTE lpDst,
  647. WORD Siz,
  648. LPBYTE lpSrc,
  649. LPBYTE lpLas,
  650. LPWORD lpDstSiz
  651. )
  652. {
  653. WORD dstCnt;
  654. WORD srcCnt;
  655. WORD empSiz;
  656. BYTE cmpDat;
  657. WORD equCnt;
  658. WORD setCnt;
  659. BYTE flgByt;
  660. WORD flgPnt;
  661. static const BYTE flgTbl[8] = {0x00, 0x01, 0x02, 0x04,
  662. 0x08, 0x10, 0x20, 0x40};
  663. srcCnt = Siz;
  664. for (; srcCnt != 0; srcCnt--) {
  665. if (lpSrc[srcCnt - 1] != 0x00) {
  666. break;
  667. }
  668. }
  669. if (srcCnt == 0) {
  670. *lpDstSiz = 0;
  671. return RASTER_EMPTY;
  672. }
  673. empSiz = Siz - srcCnt;
  674. for (dstCnt = 0, srcCnt = 0; srcCnt < Siz; ) {
  675. if (lpLas != NULL) {
  676. if (lpLas[srcCnt] == lpSrc[srcCnt]) {
  677. equCnt = 1;
  678. srcCnt++;
  679. for (; srcCnt < Siz; srcCnt++) {
  680. if (lpLas[srcCnt] != lpSrc[srcCnt]) {
  681. break;
  682. }
  683. equCnt++;
  684. }
  685. if (srcCnt == Siz) {
  686. break;
  687. }
  688. if (Siz < (dstCnt + equCnt / 255 + 1)) {
  689. *lpDstSiz = Siz - empSiz;
  690. return RASTER_NONCOMP;
  691. }
  692. if (equCnt < 63) {
  693. lpDst[dstCnt++] = 0x80 | (BYTE)equCnt;
  694. } else {
  695. lpDst[dstCnt++] = 0x80 | 0x3f;
  696. for (equCnt -= 63; equCnt >= 255; equCnt -= 255) {
  697. lpDst[dstCnt++] = 0xff;
  698. }
  699. lpDst[dstCnt++] = (BYTE)equCnt;
  700. }
  701. }
  702. }
  703. if (srcCnt < (Siz - 1) && lpSrc[srcCnt] == lpSrc[srcCnt + 1]) {
  704. cmpDat = lpSrc[srcCnt];
  705. equCnt = 2;
  706. for (srcCnt += 2; srcCnt < Siz; srcCnt++) {
  707. if (cmpDat != lpSrc[srcCnt]) {
  708. break;
  709. }
  710. equCnt++;
  711. }
  712. if (Siz < (dstCnt + equCnt / 255 + 2)) {
  713. *lpDstSiz = Siz - empSiz;
  714. return RASTER_NONCOMP;
  715. }
  716. if (equCnt < 63) {
  717. lpDst[dstCnt++] = 0xc0 | (BYTE)equCnt;
  718. } else {
  719. lpDst[dstCnt++] = 0xc0 | 0x3f;
  720. for (equCnt -= 63; equCnt >= 255; equCnt -= 255) {
  721. lpDst[dstCnt++] = 0xff;
  722. }
  723. lpDst[dstCnt++] = (BYTE)equCnt;
  724. }
  725. lpDst[dstCnt++] = cmpDat;
  726. } else {
  727. if (Siz < (dstCnt + 9)) {
  728. *lpDstSiz = Siz - empSiz;
  729. return RASTER_NONCOMP;
  730. }
  731. flgPnt = dstCnt;
  732. dstCnt++;
  733. flgByt = 0x00;
  734. if (lpLas != NULL) {
  735. for (setCnt = 0; srcCnt < Siz && setCnt < 8; srcCnt++, setCnt++) {
  736. if (lpLas[srcCnt] != lpSrc[srcCnt]) {
  737. lpDst[dstCnt++] = lpSrc[srcCnt];
  738. flgByt |= flgTbl[setCnt];
  739. }
  740. }
  741. } else {
  742. for (setCnt = 0; srcCnt < Siz && setCnt < 8; srcCnt++, setCnt++) {
  743. lpDst[dstCnt++] = lpSrc[srcCnt];
  744. flgByt |= flgTbl[setCnt];
  745. }
  746. }
  747. lpDst[flgPnt] = flgByt;
  748. }
  749. }
  750. if (Siz == dstCnt) {
  751. *lpDstSiz = Siz - empSiz;
  752. return RASTER_NONCOMP;
  753. }
  754. lpDst[dstCnt++] = 0x80;
  755. *lpDstSiz = dstCnt;
  756. return RASTER_COMP;
  757. }
  758. //===================================================================================================
  759. // Spool CMYK Bit image command
  760. //===================================================================================================
  761. void CMYKImgCmd(
  762. PDEVOBJ pdevobj, // Pointer to PDEVOBJ structure
  763. WORD Comp,
  764. LONG XPos,
  765. LONG YPos,
  766. WORD Frame,
  767. WORD DataBit, // (1:2value 2:4value 4:16value)
  768. WORD Width, // dot
  769. WORD Height, // dot
  770. WORD WidthByte, // byte
  771. DWORD InvLeft, // Invalid size from the left edge
  772. DWORD InvRight, // Invalid size from the right edge
  773. LPBYTE lpBit,
  774. LPBYTE lpTmp,
  775. LONG XPos_P,
  776. LONG YPos_P
  777. )
  778. {
  779. int pam[11];
  780. WORD siz;
  781. WORD widByt; // Width byte(Not include invalid size)
  782. WORD Plane;
  783. LPBYTE lpDst; // Memory copy
  784. LPBYTE lpSrc; // Memory copy
  785. WORD linCnt;
  786. DWORD widBit; // Width bit(Not include invalid size)
  787. DWORD dstSiz;
  788. DWORD rasSiz;
  789. PMYPDEV pOEM = (PMYPDEV)pdevobj->pdevOEM;
  790. // Check of zero divide 2002.3.23 >>>
  791. if (DataBit == 0) {
  792. ERR(("CMYKImgCmd() 0Div-Check [DataBit=0] \n"));
  793. return;
  794. }
  795. // Check of zero divide 2002.3.23 <<<
  796. widBit = (DWORD)Width * DataBit - InvLeft - InvRight;
  797. if (Comp == Yes) { // Compress
  798. siz = CmdCopy(CmdBuf, &ModOrgIn); // Original mode in
  799. if (pOEM->iColor == XX_COLOR_SINGLE
  800. || pOEM->iColor == XX_COLOR_MANY
  801. || pOEM->iColor == XX_COLOR_MANY2) {
  802. pam[0] = 0;
  803. pam[1] = 0;
  804. pam[2] = 0;
  805. pam[3] = 0;
  806. if (Frame == 1) {
  807. pam[0] = 255;
  808. Plane = PLN_CYAN;
  809. }
  810. if (Frame == 2) {
  811. pam[1] = 255;
  812. Plane = PLN_MGENTA;
  813. }
  814. if (Frame == 3) {
  815. pam[2] = 255;
  816. Plane = PLN_YELLOW;
  817. }
  818. if (Frame == 0) {
  819. pam[3] = 255;
  820. Plane = PLN_BLACK;
  821. }
  822. siz += CmdStore(CmdBuf + siz, &OrgColCmy, pam);
  823. siz += PlaneCmdStore(pdevobj, CmdBuf + siz, Plane);
  824. }
  825. siz += CmdCopy(CmdBuf + siz, &ModOrgOut); // Original mode out
  826. if (siz != 0) { // There are data for spool?
  827. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  828. }
  829. pam[0] = XPos_P;
  830. siz = CmdStore(CmdBuf, &PosAbsHrz, pam);
  831. pam[0] = YPos_P;
  832. siz += CmdStore(CmdBuf + siz, &PosAbsVtc, pam);
  833. if (siz != 0) { // There are data for spool
  834. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  835. }
  836. if (RasterImgCmd(pdevobj, pOEM->iCompress, Width, Height,
  837. WidthByte, 0, (WORD)((InvLeft + 7) / 8), (WORD)((InvRight + 7) / 8), lpBit, lpTmp) == No) {
  838. // Not compress because compress rate is poor
  839. BitImgImgCmd(pdevobj, Width, Height, WidthByte, 0, (WORD)((InvLeft + 7) / 8), (WORD)((InvRight + 7) / 8), lpBit);
  840. }
  841. }
  842. if (Comp == No) { // Not compress
  843. pam[2] = 0;
  844. pam[3] = Frame;
  845. pam[4] = DataBit;
  846. pam[5] = HIWORD(XPos);
  847. pam[6] = LOWORD(XPos);
  848. pam[7] = HIWORD(YPos);
  849. pam[8] = LOWORD(YPos);
  850. pam[10] = Height;
  851. if (InvLeft == 0 && InvRight == 0) { // Not include invalid size
  852. pam[0] = 0; // Data byte size (high byte)
  853. pam[1] = WidthByte * Height; // Data byte size (low byte)
  854. pam[9] = Width;
  855. siz = CmdStore(CmdBuf, &OrgImgCmy, pam);
  856. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  857. WRITESPOOLBUF(pdevobj, lpBit, pam[1]);
  858. } else { // Include invalid size
  859. widByt = (WORD)((widBit + 7) / 8);
  860. pam[0] = 0; // Data byte size (high byte)
  861. pam[1] = widByt * Height; // Data byte size (low byte)
  862. pam[9] = (WORD)(widBit / DataBit);
  863. siz = CmdStore(CmdBuf, &OrgImgCmy, pam);
  864. WRITESPOOLBUF(pdevobj, CmdBuf, siz);
  865. for (linCnt = 0; linCnt < Height; linCnt++) { // Spool bitmap data by 1 line
  866. WRITESPOOLBUF(pdevobj, lpBit + (WORD)(InvLeft / 8), widByt);
  867. lpBit += WidthByte;
  868. }
  869. }
  870. }
  871. return;
  872. }
  873. //===================================================================================================
  874. // Copy command buffer
  875. //===================================================================================================
  876. WORD CmdCopy(
  877. LPBYTE lpDst,
  878. LPCMDDEF lpCmdInf
  879. )
  880. {
  881. WORD siz;
  882. LPBYTE lpCmd;
  883. lpCmd = lpCmdInf->Cmd;
  884. for (siz = 0; siz < lpCmdInf->Size; siz++) {
  885. lpDst[siz] = lpCmd[siz];
  886. }
  887. return siz;
  888. }
  889. //===================================================================================================
  890. // Copy command data
  891. //===================================================================================================
  892. WORD CmdStore(
  893. LPBYTE lpDst,
  894. LPCMDDEF CmdInf,
  895. LPINT lpPam
  896. )
  897. {
  898. LPBYTE lpCmd;
  899. BYTE cmdDat;
  900. WORD cmdCnt;
  901. WORD setCnt;
  902. WORD pamCnt;
  903. WORD upmDat;
  904. int pamDat;
  905. DWORD dDat;
  906. LONG lDat;
  907. setCnt = 0;
  908. pamCnt = 0;
  909. lpCmd = CmdInf->Cmd;
  910. for (cmdCnt = 0; cmdCnt < CmdInf->Size; cmdCnt++) { // Copy
  911. cmdDat = *lpCmd++;
  912. if (cmdDat != '%') {
  913. lpDst[setCnt++] = cmdDat;
  914. } else {
  915. cmdCnt++;
  916. switch (cmdDat = *lpCmd++) { // Type
  917. case 'u':
  918. setCnt += USINTtoASC(&lpDst[setCnt], (WORD)lpPam[pamCnt++]);
  919. break;
  920. case 'd':
  921. setCnt += INTtoASC(&lpDst[setCnt], lpPam[pamCnt++]);
  922. break;
  923. #if 0 /* 441435: Currently Not used */
  924. case 'y':
  925. upmDat = (WORD)lpPam[pamCnt++];
  926. if (upmDat == 0 || (upmDat / 100) != 0) {
  927. setCnt += USINTtoASC(&lpDst[setCnt], (WORD)(upmDat / 100));
  928. }
  929. if ((upmDat % 100) != 0) {
  930. lpDst[setCnt++] = '.';
  931. setCnt += USINTtoASC(&lpDst[setCnt], (WORD)(upmDat % 100));
  932. }
  933. break;
  934. case 'z':
  935. pamDat = lpPam[pamCnt++];
  936. if (upmDat == 0 || (upmDat / 100) != 0) {
  937. setCnt += INTtoASC(&lpDst[setCnt], (pamDat / 100));
  938. }
  939. if ((pamDat % 100) != 0) {
  940. lpDst[setCnt++] = '.';
  941. if (pamDat < 0) {
  942. pamDat = 0 - pamDat;
  943. }
  944. setCnt += USINTtoASC(&lpDst[setCnt], (WORD)(pamDat % 100));
  945. }
  946. break;
  947. #endif /* 441435: Currently Not used */
  948. case 'w':
  949. dDat = MAKELONG(lpPam[pamCnt + 1], lpPam[pamCnt]);
  950. setCnt += USLONGtoASC(&lpDst[setCnt], dDat);
  951. pamCnt += 2;
  952. break;
  953. case 'l':
  954. lDat = MAKELONG(lpPam[pamCnt + 1], lpPam[pamCnt]);
  955. setCnt += LONGtoASC(&lpDst[setCnt], lDat);
  956. pamCnt += 2;
  957. break;
  958. #if 0 /* 441435: Currently Not used */
  959. case 'b':
  960. lpDst[setCnt++] = (BYTE)lpPam[pamCnt++];
  961. break;
  962. #endif /* 441435: Currently Not used */
  963. case '%':
  964. lpDst[setCnt++] = cmdDat;
  965. break;
  966. }
  967. }
  968. }
  969. return setCnt;
  970. }
  971. //===================================================================================================
  972. // int -> ascii
  973. //===================================================================================================
  974. WORD INTtoASC(
  975. LPBYTE lpDst,
  976. int Dat // Conversion data
  977. )
  978. {
  979. WORD setCnt;
  980. WORD divDat;
  981. WORD setVal;
  982. setCnt = 0;
  983. if (Dat == 0) {
  984. lpDst[setCnt++] = '0';
  985. return setCnt;
  986. }
  987. if (Dat < 0) {
  988. lpDst[setCnt++] = '-';
  989. Dat = 0 - Dat;
  990. }
  991. setVal = No;
  992. for (divDat = 10000; divDat != 1; divDat /= 10) {
  993. if (setVal == Yes) {
  994. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  995. } else {
  996. if (Dat >= (int)divDat) {
  997. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  998. setVal = Yes;
  999. }
  1000. }
  1001. Dat %= divDat;
  1002. }
  1003. lpDst[setCnt++] = (BYTE)(Dat + '0');
  1004. return setCnt;
  1005. }
  1006. //===================================================================================================
  1007. // usint -> ascii
  1008. //===================================================================================================
  1009. WORD USINTtoASC(
  1010. LPBYTE lpDst,
  1011. WORD Dat
  1012. )
  1013. {
  1014. WORD setCnt;
  1015. WORD divDat;
  1016. WORD setVal;
  1017. setCnt = 0;
  1018. if (Dat == 0) {
  1019. lpDst[setCnt++] = '0';
  1020. return setCnt;
  1021. }
  1022. setVal = No;
  1023. for (divDat = 10000; divDat != 1; divDat /= 10) {
  1024. if (setVal == Yes) {
  1025. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  1026. } else {
  1027. if (Dat >= divDat) {
  1028. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  1029. setVal = Yes;
  1030. }
  1031. }
  1032. Dat %= divDat;
  1033. }
  1034. lpDst[setCnt++] = (BYTE)(Dat + '0');
  1035. return setCnt;
  1036. }
  1037. //===================================================================================================
  1038. // long -> ascii
  1039. //===================================================================================================
  1040. WORD LONGtoASC(
  1041. LPBYTE lpDst,
  1042. LONG Dat
  1043. )
  1044. {
  1045. WORD setCnt;
  1046. DWORD divDat;
  1047. WORD setVal;
  1048. setCnt = 0;
  1049. if (Dat == 0) {
  1050. lpDst[setCnt++] = '0';
  1051. return setCnt;
  1052. }
  1053. if (Dat < 0) {
  1054. lpDst[setCnt++] = '-';
  1055. Dat = 0 - Dat;
  1056. }
  1057. setVal = No;
  1058. for (divDat = 1000000000; divDat != 1; divDat /= 10) {
  1059. if (setVal == Yes) {
  1060. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  1061. } else {
  1062. if (Dat >= (LONG)divDat) {
  1063. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  1064. setVal = Yes;
  1065. }
  1066. }
  1067. Dat %= divDat;
  1068. }
  1069. lpDst[setCnt++] = (BYTE)(Dat + '0');
  1070. return setCnt;
  1071. }
  1072. //===================================================================================================
  1073. // uslong -> ascii
  1074. //===================================================================================================
  1075. WORD USLONGtoASC(
  1076. LPBYTE lpDst,
  1077. DWORD Dat
  1078. )
  1079. {
  1080. WORD setCnt;
  1081. DWORD divDat;
  1082. WORD setVal;
  1083. setCnt = 0;
  1084. if (Dat == 0) {
  1085. lpDst[setCnt++] = '0';
  1086. return setCnt;
  1087. }
  1088. setVal = No;
  1089. for (divDat = 1000000000; divDat != 1; divDat /= 10) {
  1090. if (setVal == Yes) {
  1091. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  1092. } else {
  1093. if (Dat >= divDat) {
  1094. lpDst[setCnt++] = (BYTE)(Dat / divDat + '0');
  1095. setVal = Yes;
  1096. }
  1097. }
  1098. Dat %= divDat;
  1099. }
  1100. lpDst[setCnt++] = (BYTE)(Dat + '0');
  1101. return setCnt;
  1102. }
  1103. // End of File