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.

544 lines
13 KiB

  1. /***********************************************************************
  2. *
  3. * BitMap Openration modules
  4. *
  5. * Copyright (c) 1997-1999 Microsoft Corporation.
  6. *
  7. ***********************************************************************
  8. * BitMap Specifications
  9. *
  10. * Coordinate
  11. * (0,0) X (255,0)
  12. * +--------------*
  13. * |
  14. * Y |
  15. * |
  16. * |
  17. * |
  18. * *
  19. * (0,255)
  20. * Memory Boundary : Word Boundary
  21. *
  22. * Entry List
  23. * BMPDefine,
  24. * BMPZoomUp,
  25. * BMPOutline
  26. ***********************************************************************/
  27. #include "stdafx.h"
  28. #include "vdata.h"
  29. #include "extfunc.h"
  30. #define BMPWIDMAX 256
  31. #define BMPDEPMAX 256
  32. #define BMPMAX 8
  33. struct BMPDef {
  34. int width, depth;
  35. unsigned char *buf;
  36. int bWid;
  37. };
  38. void BMPInit(void);
  39. int BMPDefine(unsigned char *buf,int xWid,int yWid);
  40. int BMPFreDef(int bmpno);
  41. int BMPMkCont(int BMPNo,int wkBMP,int refBMP,int lsthdl);
  42. static int SearchON(int BMPNo,int x,int y);
  43. static int outline(int BMPNo,int x,int y,int lsthdl,int wkBMP,int refBMP);
  44. static int ContributeOutside(int BMPNo,int wkBMP,struct vecdata *org,int lsthdl);
  45. static int ContributeInside(int BMPNo,int wkBMP,struct vecdata *org,int lsthdl);
  46. int rdot(int BMP,int x,int y);
  47. void wdot(int BMP,int x,int y,int onoff);
  48. int ReverseRight(int BMPNo,int x,int y);
  49. static void cpybuf(int src,int dst);
  50. int BMPReverse(int bmpNo);
  51. int BMPClear(int bmpNo);
  52. struct BMPDef BMPTbl[BMPMAX]={0};
  53. /* On Bit Most left position */
  54. static unsigned char bitptbl[256] = {
  55. 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
  56. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  59. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  61. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  62. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  63. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  64. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  69. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  70. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  71. };
  72. static unsigned char wmaskB[8]={
  73. 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  74. };
  75. static unsigned char rightmask[8] = {
  76. 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01
  77. };
  78. /***********************************************************************
  79. * BMP Initialize
  80. */
  81. /* */ void
  82. /* */ BMPInit()
  83. /*
  84. * returns; none
  85. ***********************************************************************/
  86. {
  87. int i;
  88. for ( i = 0; i < BMPMAX; i++)
  89. BMPTbl[i].buf=(unsigned char *)0;
  90. return;
  91. }
  92. /***********************************************************************
  93. * Define BitMap
  94. */
  95. /* */ int
  96. /* */ BMPDefine(
  97. /* */ unsigned char *buf,
  98. /* */ int xWid,
  99. /* */ int yWid)
  100. /*
  101. * returns : 0-(BMPMAX-1), -1
  102. ***********************************************************************/
  103. {
  104. int i;
  105. if (!buf)
  106. {
  107. goto ERET;
  108. }
  109. /* Check Size */
  110. if ( xWid > BMPWIDMAX || xWid < 0
  111. || yWid > BMPWIDMAX || yWid < 0)
  112. goto ERET;
  113. /* Set Define */
  114. for ( i = 0; i < BMPMAX; i++) {
  115. if (BMPTbl[i].buf==(unsigned char *)0) {
  116. BMPTbl[i].bWid = (xWid + 15)/16*2;
  117. BMPTbl[i].width = xWid;
  118. BMPTbl[i].depth = yWid;
  119. BMPTbl[i].buf = buf;
  120. return(i);
  121. }
  122. }
  123. ERET:
  124. return( -1);
  125. }
  126. /***********************************************************************
  127. * Free BMP define
  128. */
  129. /* */ int
  130. /* */ BMPFreDef( int bmpno)
  131. /*
  132. * returns : 0, -1
  133. ***********************************************************************/
  134. {
  135. if ( bmpno < 0 || bmpno >= BMPMAX)
  136. return -1;
  137. else {
  138. BMPTbl[bmpno].buf = 0;
  139. return 0;
  140. }
  141. }
  142. /***********************************************************************
  143. * Get Outline
  144. */
  145. /* */ int
  146. /* */ BMPMkCont( int BMPNo, int wkBMP, int refBMP, int lsthdl)
  147. /*
  148. * returns : Number of Contour, -1
  149. * REMARKS : Used BMP be destroyed
  150. ***********************************************************************/
  151. {
  152. int x, y;
  153. int ncont;
  154. int sts;
  155. VDNew( lsthdl);
  156. sts = 0;
  157. ncont = 0;
  158. cpybuf( BMPNo, wkBMP);
  159. cpybuf( BMPNo, refBMP);
  160. for ( y = 0; y < BMPTbl[BMPNo].depth; y++) {
  161. x = 0;
  162. while ( (x = SearchON( wkBMP, x, y)) <BMPTbl[BMPNo].width) {
  163. if ((sts = outline( BMPNo, x, y,lsthdl, wkBMP, refBMP))<0)
  164. goto RET;
  165. ncont++;
  166. x++;
  167. }
  168. }
  169. sts = ncont;
  170. cpybuf(refBMP,BMPNo);
  171. RET:
  172. return( sts);
  173. }
  174. /***********************************************************************
  175. * Search ON dot
  176. */
  177. /* */ static int
  178. /* */ SearchON( int BMPNo, int x, int y)
  179. /*
  180. * returns : found position, Width(Not Found case)
  181. ***********************************************************************/
  182. {
  183. int bpos; /* byte position */
  184. int sbitpos; /* Start Byte Bit position */
  185. unsigned char *p;
  186. bpos = x/8;
  187. sbitpos = x % 8;
  188. p = BMPTbl[BMPNo].buf + BMPTbl[BMPNo].bWid*y + bpos;
  189. /* First Byte */
  190. if ( *p & rightmask[sbitpos])
  191. x = bpos*8 + bitptbl[(int)(*p& rightmask[sbitpos])];
  192. else {
  193. bpos++;
  194. x = bpos*8;
  195. for ( ; bpos < BMPTbl[BMPNo].bWid; bpos++, x+=8) {
  196. p++;
  197. if (*p) {
  198. x += bitptbl[(int)*p];
  199. break;
  200. }
  201. }
  202. }
  203. return( x);
  204. }
  205. /***********************************************************************
  206. * make outline data
  207. */
  208. /* */ static int
  209. /* */ outline(
  210. /* */ int BMPNo,
  211. /* */ int x,
  212. /* */ int y,
  213. /* */ int lsthdl,
  214. /* */ int wkBMP,
  215. /* */ int refBMP)
  216. /*
  217. * returns : 0, -1
  218. **********************************************************************/
  219. {
  220. int inout;
  221. struct vecdata vd;
  222. int sts;
  223. /* Check Inside/Outside */
  224. if ( rdot( refBMP, x, y) ==rdot( wkBMP, x, y)) /* OUTSIDE */
  225. inout = 0;
  226. else /* INSIDE */
  227. inout = 1;
  228. /* copy buffer */
  229. cpybuf( wkBMP, BMPNo);
  230. /* contribute */
  231. vd.x = (short)x;
  232. vd.y = (short)y;
  233. vd.atr = 0;
  234. if ( inout==0)
  235. sts = ContributeOutside( BMPNo, wkBMP, &vd, lsthdl);
  236. else
  237. sts = ContributeInside( BMPNo, wkBMP, &vd, lsthdl);
  238. return( sts);
  239. }
  240. /***********************************************************************
  241. * Contribute Outside Contour
  242. */
  243. /* */ static int
  244. /* */ ContributeOutside(int BMPNo, int wkBMP, struct vecdata *org, int lsthdl)
  245. /*
  246. * returns : 0, -1
  247. * Direction 2
  248. * |
  249. * |
  250. * 3-------+-------1
  251. * |
  252. * |
  253. * 0
  254. ***********************************************************************/
  255. {
  256. int orgx, orgy;
  257. struct vecdata vd;
  258. int dir;
  259. if (!org)
  260. {
  261. return -1;
  262. }
  263. orgx = org->x;
  264. orgy = org->y;
  265. vd = *org;
  266. dir = 0;
  267. /*
  268. if (ReverseRight( wkBMP, vd.x, vd.y))
  269. return( -1);
  270. */
  271. do {
  272. if (VDSetData( lsthdl, &vd))
  273. return(-1);
  274. switch( dir) {
  275. case 0:
  276. if (ReverseRight( wkBMP, vd.x, vd.y))
  277. return( -1);
  278. vd.y++;
  279. if ( rdot( BMPNo, vd.x-1, vd.y))
  280. dir = 3;
  281. else if ( rdot( BMPNo, vd.x, vd.y))
  282. dir = 0;
  283. else dir = 1;
  284. break;
  285. case 1:
  286. vd.x++;
  287. if ( rdot( BMPNo, vd.x, vd.y))
  288. dir = 0;
  289. else if ( rdot( BMPNo, vd.x, vd.y-1))
  290. dir = 1;
  291. else dir = 2;
  292. break;
  293. case 2:
  294. vd.y--;
  295. if (ReverseRight( wkBMP, vd.x, vd.y))
  296. return( -1);
  297. if ( rdot( BMPNo, vd.x, vd.y-1))
  298. dir = 1;
  299. else if ( rdot( BMPNo, vd.x-1, vd.y-1))
  300. dir = 2;
  301. else dir = 3;
  302. break;
  303. case 3:
  304. vd.x--;
  305. if ( rdot( BMPNo, vd.x-1, vd.y-1))
  306. dir = 2;
  307. else if ( rdot( BMPNo, vd.x-1, vd.y))
  308. dir = 3;
  309. else dir = 0;
  310. break;
  311. }
  312. } while( vd.x!=orgx || vd.y != orgy);
  313. VDClose(lsthdl);
  314. return( 0);
  315. }
  316. /***********************************************************************
  317. * Contribute Outside Contour
  318. */
  319. /* */ static int
  320. /* */ ContributeInside( int BMPNo, int wkBMP, struct vecdata *org, int lsthdl)
  321. /*
  322. * returns : 0, -1
  323. ***********************************************************************/
  324. {
  325. int orgx, orgy;
  326. struct vecdata vd;
  327. int dir;
  328. if (!org)
  329. {
  330. return -1;
  331. }
  332. orgx = org->x;
  333. orgy = org->y;
  334. vd = *org;
  335. dir = 1;
  336. do {
  337. if (VDSetData( lsthdl, &vd))
  338. return(-1);
  339. switch( dir) {
  340. case 0:
  341. if (ReverseRight( wkBMP, vd.x, vd.y))
  342. return( -1);
  343. vd.y++;
  344. if ( rdot( BMPNo, vd.x-1, vd.y)==0) /* right */
  345. dir = 3;
  346. else if ( rdot( BMPNo, vd.x, vd.y)==0) /* left */
  347. dir = 0;
  348. else dir = 1;
  349. break;
  350. case 1:
  351. vd.x++;
  352. if ( rdot( BMPNo, vd.x, vd.y)==0) /* right */
  353. dir = 0;
  354. else if ( rdot( BMPNo, vd.x, vd.y-1)==0) /* left */
  355. dir = 1;
  356. else dir = 2;
  357. break;
  358. case 2:
  359. vd.y--;
  360. if (ReverseRight( wkBMP, vd.x, vd.y))
  361. return( -1);
  362. if ( rdot( BMPNo, vd.x, vd.y-1)==0)
  363. dir = 1;
  364. else if ( rdot( BMPNo, vd.x-1, vd.y-1)==0)
  365. dir = 2;
  366. else dir = 3;
  367. break;
  368. case 3:
  369. vd.x--;
  370. if ( rdot( BMPNo, vd.x-1, vd.y-1)==0)
  371. dir = 2;
  372. else if ( rdot( BMPNo, vd.x-1, vd.y)==0)
  373. dir = 3;
  374. else dir = 0;
  375. break;
  376. }
  377. } while( vd.x!=orgx || vd.y != orgy);
  378. VDClose(lsthdl);
  379. return( 0);
  380. }
  381. /***********************************************************************
  382. * Read Dot
  383. */
  384. /* */ int
  385. /* */ rdot( int BMP, int x, int y)
  386. /*
  387. * returns : 0, nonzero
  388. ***********************************************************************/
  389. {
  390. unsigned char *radd;
  391. int rbit;
  392. int onoff;
  393. if ( x < 0 || y < 0 || x>=BMPTbl[BMP].width ||y>=BMPTbl[BMP].depth)
  394. return( 0);
  395. radd = BMPTbl[BMP].buf + BMPTbl[BMP].bWid*y + x/8;
  396. rbit = x % 8;
  397. onoff = (int)(wmaskB[rbit] & *radd);
  398. return onoff;
  399. }
  400. /***********************************************************************
  401. * Write Dot
  402. */
  403. /* */ void
  404. /* */ wdot( int BMP, int x, int y, int onoff)
  405. /*
  406. * returns : none
  407. ***********************************************************************/
  408. {
  409. unsigned char *radd;
  410. int rbit;
  411. if ( x < 0 || y < 0 || x>=BMPTbl[BMP].width ||y>=BMPTbl[BMP].depth)
  412. return;
  413. radd = BMPTbl[BMP].buf + BMPTbl[BMP].bWid*y + x/8;
  414. rbit = x % 8;
  415. if ( onoff) *radd |= wmaskB[rbit];
  416. else *radd &= ~wmaskB[rbit];
  417. return;
  418. }
  419. /***********************************************************************
  420. * Reverse right side ( Edge fill method)
  421. */
  422. /* */ int
  423. /* */ ReverseRight( int BMPNo, int x, int y)
  424. /*
  425. * returns : 0, -1
  426. ***********************************************************************/
  427. {
  428. int rb;
  429. int bitp;
  430. unsigned char *wp;
  431. if ( BMPNo < 0 || BMPNo >= BMPMAX)
  432. return 0;
  433. if ( x < 0 || x >= BMPTbl[BMPNo].width
  434. || y < 0 || y >= BMPTbl[BMPNo].depth)
  435. return 0;
  436. rb = BMPTbl[BMPNo].bWid - x/8 -1;
  437. bitp = x%8;
  438. wp = BMPTbl[BMPNo].buf + y*BMPTbl[BMPNo].bWid + x/8;
  439. /* First Byte */
  440. *wp ^= rightmask[bitp];
  441. /* to right limit */
  442. while( rb-->0) {
  443. wp++;
  444. *wp = (unsigned char)~(*wp);
  445. }
  446. return ( 0);
  447. }
  448. /***********************************************************************
  449. * Copy Buffer
  450. */
  451. /* */ static void
  452. /* */ cpybuf( int src, int dst)
  453. /*
  454. * returns : none
  455. ***********************************************************************/
  456. {
  457. int siz;
  458. if ( src < 0 || src >= BMPMAX)
  459. return;
  460. if ( dst < 0 || dst >= BMPMAX)
  461. return;
  462. siz = BMPTbl[src].bWid * BMPTbl[src].depth;
  463. memcpy( BMPTbl[dst].buf, BMPTbl[src].buf, siz);
  464. }
  465. /***********************************************************************
  466. * Reverse bitmap
  467. */
  468. /* */ int
  469. /* */ BMPReverse( int bmpNo)
  470. /*
  471. * returns : none
  472. ***********************************************************************/
  473. {
  474. int siz;
  475. char *buf;
  476. if ( bmpNo < 0 || bmpNo >= BMPMAX)
  477. return -1;
  478. else if (BMPTbl[bmpNo].buf==(unsigned char *)0)
  479. return -1;
  480. else {
  481. siz = BMPTbl[bmpNo].bWid * BMPTbl[bmpNo].depth;
  482. buf = (char *)BMPTbl[bmpNo].buf;
  483. while ( siz-->0) {
  484. *buf = (char)~*buf;
  485. buf++;
  486. }
  487. }
  488. return 0;
  489. }
  490. /***********************************************************************
  491. * Clear BMP
  492. */
  493. /* */ int
  494. /* */ BMPClear( int bmpNo)
  495. /*
  496. * returns : 0,-1
  497. ***********************************************************************/
  498. {
  499. int siz;
  500. if ( bmpNo < 0 || bmpNo >= BMPMAX)
  501. return -1;
  502. else if (BMPTbl[bmpNo].buf==(unsigned char *)0)
  503. return -1;
  504. siz = BMPTbl[bmpNo].bWid * BMPTbl[bmpNo].depth;
  505. memset( BMPTbl[bmpNo].buf, 0, siz);
  506. return 0;
  507. }
  508. /* EOF */