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.

219 lines
6.7 KiB

  1. /* File: sv_h263_edge.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1995, 1997 **
  4. ** **
  5. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  6. ** laws of the United States. **
  7. ** **
  8. ** The software contained on this media is proprietary to and embodies **
  9. ** the confidential technology of Digital Equipment Corporation. **
  10. ** Possession, use, duplication or dissemination of the software and **
  11. ** media is authorized only pursuant to a valid written license from **
  12. ** Digital Equipment Corporation. **
  13. ** **
  14. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  15. ** Government is subject to restrictions as set forth in Subparagraph **
  16. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  17. ******************************************************************************/
  18. #include "sv_h263.h"
  19. #include "proto.h"
  20. #define BSIZE 8
  21. #define BSZSHIFT 6
  22. #define EDGE_TH 150
  23. static void lineDetect(int H, int V, int D1, int D2, unsigned char *pe, unsigned char *po);
  24. static void findEdge(int ul, int u, int ur, int l, int c, int r, int bl, int b, int br,
  25. unsigned char *pe, unsigned char *po, char mode);
  26. /***********************************************************************************
  27. * Function: Sobel
  28. * Sobel gradient-based edge detector (see Jain, page 349)
  29. **********************************************************************************/
  30. void Sobel(int ul, int u, int ur, int l, int c, int r, int bl, int b, int br,
  31. unsigned char *pe, unsigned char *po)
  32. {
  33. int gx, gy, AGX, AGY;
  34. gx = -ul + ur - (l<<1) + (r<<1) - bl + br;
  35. gy = -ul + bl - (u<<1) + (b<<1) - ur + br;
  36. AGX = gx > 0 ? gx : -gx;
  37. AGY = gy > 0 ? gy : -gy;
  38. *pe = (AGX+AGY)> EDGE_TH ? 255 : 0;
  39. }
  40. /***********************************************************************************
  41. * Function: lineDetect
  42. * Compass-based line detector (see Jain, page 357)
  43. **********************************************************************************/
  44. void lineDetect(int H, int V, int D1, int D2, unsigned char *pe, unsigned char *po)
  45. {
  46. int AH, AV, AD1, AD2, ED;
  47. AH = (H>0 ? H : -H);
  48. AV = (V>0 ? V : -V);
  49. AD1 = (D1>0 ? D1: -D1);
  50. AD2 = (D2>0 ? D2: -D2);
  51. if(AH>=AV && AH>=AD1 && AH>=AD2) {
  52. ED = AH;
  53. *po = 1;
  54. } else if (AV>=AH && AV>=AD1 && AV>=AD2) {
  55. ED = AV;
  56. *po = 2;
  57. } else if (AD1>=AH && AD1>=AV && AD1>=AD2) {
  58. ED = AD1;
  59. *po = 3;
  60. } else {
  61. ED = AD2;
  62. *po = 4;
  63. }
  64. if (ED < EDGE_TH) { *pe = 0; *po = 0;}
  65. else *pe = 255;
  66. }
  67. /**********************************************************************************
  68. * Function: findEdge
  69. * Computes edge magnitude and orientation given the pixel's neighborhood
  70. *********************************************************************************/
  71. void findEdge(int ul, int u, int ur, int l, int c, int r, int bl, int b, int br,
  72. unsigned char *pe, unsigned char *po, char mode)
  73. {
  74. switch(mode) {
  75. case 'L':
  76. {
  77. int H, V, D1, D2;
  78. /* Horizontal gradient */
  79. H = -ul -u -ur + (l<<1) + (c<<1) + (r<<1) -bl -b -br;
  80. /* Vertical gradient */
  81. V = -ul + (u<<1) -ur -l + (c<<1) -r -bl + (b<<1) -br;
  82. /* Diagonal gradient 1 */
  83. D1 = -ul -u + (ur<<1) -l + (c<<1) -r + (bl<<1) -b -br;
  84. /* Diagonal gradient 2*/
  85. D2 = (ul<<1) -u -ur -l + (c<<1) -r -bl -b +(br<<1);
  86. lineDetect(H, V, D1, D2, pe, po);
  87. break;
  88. }
  89. case 'S':
  90. {
  91. Sobel(ul, u, ur, l, c, r, bl, b, br, pe, po);
  92. break;
  93. }
  94. default:
  95. /* printf("Unknown edge finder in findEdge...\n"); */
  96. /* exit(0); */
  97. return;
  98. }
  99. }
  100. /***********************************************************************************
  101. * Function: EdgeMap
  102. * Computes an edge map for image. Edge magnitude is returned in EdgeMag and
  103. * orientation in EdgeOrient.
  104. **********************************************************************************/
  105. void sv_H263EdgeMap(unsigned char *image, unsigned char *EdgeMag, unsigned char *EdgeOrient,
  106. int rows, int cols)
  107. {
  108. unsigned char *pi, *pe, *po;
  109. int i, j, ul, u, ur, l, c, r, bl, b, br;
  110. pi = image;
  111. pe = EdgeMag;
  112. po = EdgeOrient;
  113. /* Clear first line */
  114. for(j=0; j<cols; j++) {
  115. *(pe++) = 0;
  116. *(po++) = 0;
  117. }
  118. pi = image + cols;
  119. for(i=1; i<rows-1; i++) {
  120. /* Clear first pixel */
  121. *(pe++) = 0; *(po++) = 0; pi++;
  122. /* Start gathering 3x3 neighberhood */
  123. ul = *(pi-cols-1); u = *(pi-cols);
  124. l = *(pi-1); c = *pi;
  125. bl = *(pi+cols-1); b = *(pi+cols);
  126. /* Compute edge map */
  127. for(j=1; j<cols-1; j++, pi++, pe++, po++) {
  128. /* finish neighborhood */
  129. ur = *(pi-cols+1);
  130. r = *(pi+1);
  131. br = *(pi+cols+1);
  132. findEdge(ul, u, ur, l, c, r, bl, b, br, pe, po, 'S');
  133. /* start next neigborhood */
  134. ul = u; u = ur;
  135. l = c; c = r;
  136. bl = b; b = br;
  137. }
  138. /* Clear last pixel */
  139. *(pe++) = 0; *(po++) = 0; pi++;
  140. }
  141. /* Clear last line */
  142. for(j=0; j<cols; j++) {
  143. *(pe++) = 0;
  144. *(po++) = 0;
  145. }
  146. }
  147. /*******************************************************************************************
  148. * Function EdgeGrow
  149. * Thickens the edge map by considering as an edge any pixel which has an edge pixel in a
  150. * neighborhood of dimensions sr, sc
  151. ******************************************************************************************/
  152. unsigned char *sv_H263EdgeGrow(unsigned char *Edge, int rows, int cols, int sr, int sc)
  153. {
  154. unsigned char *pse, *pf, *pe, ed, *Fat;
  155. int du, db, dl, dr, i, j, k, l, sr2, sc2;
  156. if (!(Fat = (unsigned char *)ScAlloc(rows*cols))) {
  157. /* fprintf(stderr,"malloc failed\n"); */
  158. /* exit(-1); */
  159. return(NULL);
  160. }
  161. if (!(sr%2) || !(sc%2)) {
  162. /* printf("Structuring Element must have odd dimensions\n");
  163. exit(0); */
  164. return(NULL);
  165. }
  166. sr2 = sr >> 1; sc2 = sc >> 1;
  167. pe = Edge;
  168. pf = Fat;
  169. for(i=0; i<rows; i++) {
  170. for(j=0; j<cols; j++, pe++, pf++) {
  171. du = i>sr2 ? sr2 : i;
  172. db = (rows-1-i) > sr2 ? sr2 : (rows-1-i);
  173. dl = j > sc2 ? sc2 : j;
  174. dr = (cols-1-j) > sc2 ? sc2 : (cols-1-j);
  175. ed = 0;
  176. for(k=-du; k<=db; k++) {
  177. pse = pe + k * cols - dl;
  178. for(l=-dl; l<=dr; l++, pse++) {
  179. if (ed = *pse) break;
  180. }
  181. if(ed) break;
  182. }
  183. *pf = ed;
  184. }
  185. }
  186. return Fat;
  187. }