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.

702 lines
24 KiB

  1. /* File: sv_h263_morph.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. /*
  19. #define _SLIBDEBUG_
  20. */
  21. #include "sv_h263.h"
  22. #include "proto.h"
  23. #ifdef _SLIBDEBUG_
  24. #include "sc_debug.h"
  25. #define _DEBUG_ 0 /* detailed debuging statements */
  26. #define _VERBOSE_ 1 /* show progress */
  27. #define _VERIFY_ 0 /* verify correct operation */
  28. #define _WARN_ 1 /* warnings about strange behavior */
  29. #endif
  30. static unsigned char min5(unsigned char a, unsigned char b,
  31. unsigned char c, unsigned char d,
  32. unsigned char e) ;
  33. static unsigned char max5(unsigned char a, unsigned char b,
  34. unsigned char c, unsigned char d,
  35. unsigned char e) ;
  36. static void ErodeX(unsigned char *image, unsigned char *out,
  37. int rows, int cols);
  38. static void DilateX(unsigned char *image, unsigned char *out,
  39. int rows, int cols);
  40. static void ErodeS(unsigned char *image, unsigned char *out,
  41. int rows, int cols, int sr, int sc);
  42. static void DilateS(unsigned char *image, unsigned char *out,
  43. int rows, int cols, int sr, int sc);
  44. static void Dilate(unsigned char *image, unsigned char *out,
  45. int rows, int cols, int sr, int sc);
  46. static void Erode(unsigned char *image, unsigned char *out,
  47. int rows, int cols, int sr, int sc);
  48. static void Open(unsigned char *image, unsigned char *out,
  49. int rows, int cols, int sr, int sc);
  50. static void EdgeSelect(H263_PictImage *input, H263_PictImage *filtd,
  51. unsigned char *edge,
  52. H263_PictImage *output, int rows, int cols) ;
  53. /*****************************************************************************************************
  54. * Function min5
  55. * Computes the min of the five elements
  56. ****************************************************************************************************/
  57. static unsigned char min5(unsigned char a, unsigned char b,
  58. unsigned char c, unsigned char d, unsigned char e)
  59. {
  60. unsigned char out;
  61. out = a;
  62. if(b<out) out=b;
  63. if(c<out) out=c;
  64. if(d<out) out=d;
  65. if(e<out) out=e;
  66. return out;
  67. }
  68. /*****************************************************************************************************
  69. * Function max5
  70. * Computes the max of the five elements
  71. ****************************************************************************************************/
  72. static unsigned char max5(unsigned char a, unsigned char b, unsigned char c,
  73. unsigned char d, unsigned char e)
  74. {
  75. unsigned char out;
  76. out = a;
  77. if(b>out) out=b;
  78. if(c>out) out=c;
  79. if(d>out) out=d;
  80. if(e>out) out=e;
  81. return out;
  82. }
  83. /*****************************************************************************************************
  84. * Function: ErodeX
  85. * Erosion of image (dimensions rows, cols) by a "+" structuring element
  86. ****************************************************************************************************/
  87. static void ErodeX(unsigned char *image, unsigned char *out, int rows, int cols)
  88. {
  89. int i, j;
  90. unsigned char *pi, *po;
  91. pi = image;
  92. po = out;
  93. /**** First line ****/
  94. /* First pixel */
  95. *po = min5(*pi, *pi, *(pi+1), *pi, *(pi+cols));
  96. pi++; po++;
  97. /* Center pixels */
  98. for(j=1; j<cols-1; j++, pi++, po++) {
  99. *po = min5(*(pi-1), *pi, *(pi+1), *pi, *(pi+cols));
  100. }
  101. /* Last pixel */
  102. *po = min5(*(pi-1), *pi, *pi, *pi, *(pi+cols));
  103. pi++; po++;
  104. /**** Center lines ****/
  105. for(i=1; i<rows-1; i++) {
  106. /* First pixel */
  107. *po = min5(*pi, *pi, *(pi+1), *(pi-cols), *(pi+cols));
  108. pi++; po++;
  109. /* Center pixels */
  110. for(j=1; j<cols-1; j++, pi++, po++) {
  111. *po = min5(*(pi-1), *pi, *(pi+1), *(pi-cols), *(pi+cols));
  112. }
  113. /* Last pixel */
  114. *po = min5(*(pi-1), *pi, *pi, *(pi-cols), *(pi+cols));
  115. pi++; po++;
  116. }
  117. /**** Last line ****/
  118. /* First pixel */
  119. *po = min5(*pi, *pi, *(pi+1), *(pi-cols), *pi);
  120. pi++; po++;
  121. /* Center pixels */
  122. for(j=1; j<cols-1; j++, pi++, po++) {
  123. *po = min5(*(pi-1), *pi, *(pi+1), *(pi-cols), *pi);
  124. }
  125. /* Last pixel */
  126. *po = min5(*(pi-1), *pi, *pi, *(pi-cols), *pi);
  127. pi++; po++;
  128. }
  129. /*****************************************************************************************************
  130. * Function: ErodeX
  131. * Erosion of image (dimensions rows, cols) by a "+" structuring element
  132. ****************************************************************************************************/
  133. static void DilateX(unsigned char *image, unsigned char *out, int rows, int cols)
  134. {
  135. int i, j;
  136. unsigned char *pi, *po;
  137. pi = image;
  138. po = out;
  139. /**** First line ****/
  140. /* First pixel */
  141. *po = max5(*pi, *pi, *(pi+1), *pi, *(pi+cols));
  142. pi++; po++;
  143. /* Center pixels */
  144. for(j=1; j<cols-1; j++, pi++, po++) {
  145. *po = max5(*(pi-1), *pi, *(pi+1), *pi, *(pi+cols));
  146. }
  147. /* Last pixel */
  148. *po = max5(*(pi-1), *pi, *pi, *pi, *(pi+cols));
  149. pi++; po++;
  150. /**** Center lines ****/
  151. for(i=1; i<rows-1; i++) {
  152. /* First pixel */
  153. *po = max5(*pi, *pi, *(pi+1), *(pi-cols), *(pi+cols));
  154. pi++; po++;
  155. /* Center pixels */
  156. for(j=1; j<cols-1; j++, pi++, po++) {
  157. *po = max5(*(pi-1), *pi, *(pi+1), *(pi-cols), *(pi+cols));
  158. }
  159. /* Last pixel */
  160. *po = max5(*(pi-1), *pi, *pi, *(pi-cols), *(pi+cols));
  161. pi++; po++;
  162. }
  163. /**** Last line ****/
  164. /* First pixel */
  165. *po = max5(*pi, *pi, *(pi+1), *(pi-cols), *pi);
  166. pi++; po++;
  167. /* Center pixels */
  168. for(j=1; j<cols-1; j++, pi++, po++) {
  169. *po = max5(*(pi-1), *pi, *(pi+1), *(pi-cols), *pi);
  170. }
  171. /* Last pixel */
  172. *po = max5(*(pi-1), *pi, *pi, *(pi-cols), *pi);
  173. pi++; po++;
  174. }
  175. /*****************************************************************************************************
  176. * Function: ErodeS
  177. * Erosion of image (dimensions rows, cols) by a square structuring element of dimensions (sr, sc)
  178. ****************************************************************************************************/
  179. static void ErodeS(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  180. {
  181. int i, j, k, l, du, db, dl, dr, sr2, sc2;
  182. unsigned char *pi, *po, *pse, min, odd;
  183. odd = 1;
  184. if (!(sr%2) || !(sc%2)) odd = 0;
  185. sr2 = sr >> 1; sc2 = sc >> 1;
  186. pi = image;
  187. po = out;
  188. for(i=0; i<rows; i++) {
  189. for(j=0; j<cols; j++, pi++, po++) {
  190. du = i>sr2 ? sr2 : i;
  191. dl = j > sc2 ? sc2 : j;
  192. if(odd) {
  193. db = (rows-1-i) > sr2 ? sr2 : (rows-1-i);
  194. dr = (cols-1-j) > sc2 ? sc2 : (cols-1-j);
  195. } else {
  196. db = (rows-1-i) > sr2-1 ? sr2-1 : (rows-1-i);
  197. dr = (cols-1-j) > sc2-1 ? sc2-1 : (cols-1-j);
  198. }
  199. min = 255;
  200. for(k=-du; k<=db; k++) {
  201. pse = pi + k * cols - dl;
  202. for(l=-dl; l<=dr; l++, pse++) {
  203. min = *pse < min ? *pse : min;
  204. }
  205. }
  206. *po = min;
  207. }
  208. }
  209. }
  210. /*****************************************************************************************************
  211. * Function: DilateS
  212. * Dilation of image (dimensions rows, cols) by a square structuring element of dimensions (sr, sc)
  213. ****************************************************************************************************/
  214. static void DilateS(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  215. {
  216. int i, j, k, l, du, db, dl, dr, sr2, sc2;
  217. unsigned char *pi, *po, *pse, max, odd;
  218. odd = 1;
  219. if (!(sr%2) || !(sc%2)) odd = 0;
  220. sr2 = sr >> 1; sc2 = sc >> 1;
  221. pi = image;
  222. po = out;
  223. for(i=0; i<rows; i++) {
  224. for(j=0; j<cols; j++, pi++, po++) {
  225. du = i>sr2 ? sr2 : i;
  226. dl = j > sc2 ? sc2 : j;
  227. if(odd) {
  228. db = (rows-1-i) > sr2 ? sr2 : (rows-1-i);
  229. dr = (cols-1-j) > sc2 ? sc2 : (cols-1-j);
  230. } else {
  231. db = (rows-1-i) > sr2-1 ? sr2-1 : (rows-1-i);
  232. dr = (cols-1-j) > sc2-1 ? sc2-1 : (cols-1-j);
  233. }
  234. max = 0;
  235. for(k=-du; k<=db; k++) {
  236. for(l=-dl; l<=dr; l++, pse++) {
  237. pse = pi + k * cols + l;
  238. max = (*pse > max) ? *pse : max;
  239. }
  240. }
  241. *po = max;
  242. }
  243. }
  244. }
  245. /*****************************************************************************************************
  246. * Function: Dilate
  247. * Dilation of image (dimensions rows, cols) by a structuring element of dimensions (sr, sc).
  248. * If (sr, sc) are positive the structuring element is positive. If they are -1 it is
  249. * the cross '+'
  250. ****************************************************************************************************/
  251. static void Dilate(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  252. {
  253. if(sr > 0 && sc > 0) {
  254. DilateS(image, out, rows, cols, sr, sc);
  255. } else if(sr==-1 && sc ==-1) {
  256. DilateX(image, out, rows, cols);
  257. } else {
  258. _SlibDebug(_WARN_, printf("Dilate() Unknown structuring element\n") );
  259. return;
  260. }
  261. }
  262. /*****************************************************************************************************
  263. * Function: Erode
  264. * Erosion of image (dimensions rows, cols) by a structuring element of dimensions (sr, sc).
  265. * If (sr, sc) are positive the structuring element is positive. If they are -1 it is
  266. * the cross '+'
  267. ****************************************************************************************************/
  268. static void Erode(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  269. {
  270. if(sr > 0 && sc > 0) {
  271. ErodeS(image, out, rows, cols, sr, sc);
  272. } else if(sr==-1 && sc ==-1) {
  273. ErodeX(image, out, rows, cols);
  274. } else {
  275. _SlibDebug(_WARN_, printf("Erode() Unknown structuring element\n") );
  276. return;
  277. }
  278. }
  279. /*****************************************************************************************************
  280. * Function: Open
  281. * Opening of image (dimensions rows, cols) by a square structuring element of dimensions (sr, sc)
  282. ****************************************************************************************************/
  283. static void Open(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  284. {
  285. unsigned char *tmp;
  286. if (!(tmp = (unsigned char *)ScAlloc(rows*cols))) {
  287. _SlibDebug(_WARN_, printf("Open() ScAlloc failed\n") );
  288. return;
  289. }
  290. Erode(image, tmp, rows, cols, sr, sc);
  291. Dilate(tmp, out, rows, cols, sr, sc);
  292. ScFree(tmp);
  293. }
  294. /*****************************************************************************************************
  295. * Function: Close
  296. * Closing of image (dimensions rows, cols) by a square structuring element of dimensions (sr, sc)
  297. ****************************************************************************************************/
  298. void Close(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  299. {
  300. unsigned char *tmp;
  301. if (!(tmp = (unsigned char *)ScAlloc(rows*cols))) {
  302. _SlibDebug(_WARN_, printf("Close() ScAlloc failed\n") );
  303. return;
  304. }
  305. Dilate(image, tmp, rows, cols, sr, sc);
  306. Erode(tmp, out, rows, cols, sr, sc);
  307. ScFree(tmp);
  308. }
  309. /*****************************************************************************************************
  310. * Function: OpenClose
  311. * Open/Closing of image (dimensions rows, cols) by a square structuring element of dimensions (sr, sc)
  312. ****************************************************************************************************/
  313. void OpenClose(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  314. {
  315. unsigned char *tmp;
  316. if (!(tmp = (unsigned char *)ScAlloc(rows*cols))) {
  317. _SlibDebug(_WARN_, printf("OpenClose() ScAlloc failed\n") );
  318. return;
  319. }
  320. Open(image, tmp, rows, cols, sr, sc);
  321. Close(tmp, out, rows, cols, sr, sc);
  322. ScFree(tmp);
  323. }
  324. /*****************************************************************************************************
  325. * Function: CloseOpen
  326. * Open/Closing of image (dimensions rows, cols) by a square structuring element of dimensions (sr, sc)
  327. ****************************************************************************************************/
  328. void CloseOpen(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  329. {
  330. unsigned char *tmp;
  331. if (!(tmp = (unsigned char *)ScAlloc(rows*cols))) {
  332. _SlibDebug(_WARN_, printf("CloseOpen() ScAlloc failed\n") );
  333. return;
  334. }
  335. Close(image, tmp, rows, cols, sr, sc);
  336. Open(tmp, out, rows, cols, sr, sc);
  337. ScFree(tmp);
  338. }
  339. /*****************************************************************************************************
  340. * Function: GeoDilate
  341. * Geodesic dilation of size one of image with respect to reference
  342. ****************************************************************************************************/
  343. void GeoDilate(unsigned char *image, unsigned char *reference, int rows, int cols, int sr, int sc)
  344. {
  345. int i, j;
  346. unsigned char *pi, *pr, *pt, *tmp;
  347. if (!(tmp = (unsigned char *)ScAlloc(rows*cols))) {
  348. _SlibDebug(_WARN_, printf("GeoDilate() ScAlloc failed\n") );
  349. return;
  350. }
  351. Dilate(image, tmp, rows, cols, sr, sc);
  352. pi = image;
  353. pr = reference;
  354. pt = tmp;
  355. for(i=0; i<rows; i++) {
  356. for(j=0; j<cols; j++, pi++, pr++, pt++) {
  357. *pi = *pr < *pt ? *pr : *pt;
  358. }
  359. }
  360. ScFree(tmp);
  361. }
  362. /*****************************************************************************************************
  363. * Function: GeoErode
  364. * Geodesic erosion of size one of image with respect to reference
  365. ****************************************************************************************************/
  366. void GeoErode(unsigned char *image, unsigned char *reference, int rows, int cols, int sr, int sc)
  367. {
  368. int i, j;
  369. unsigned char *pi, *pr, *pt, *tmp;
  370. if (!(tmp = (unsigned char *)ScAlloc(rows*cols))) {
  371. _SlibDebug(_WARN_, printf("GeoErode() ScAlloc failed\n") );
  372. return;
  373. }
  374. Erode(image, tmp, rows, cols, sr, sc);
  375. pi = image;
  376. pr = reference;
  377. pt = tmp;
  378. for(i=0; i<rows; i++) {
  379. for(j=0; j<cols; j++, pi++, pr++, pt++) {
  380. *pi = (-(*pr) < (-*pt)) ? *pr : *pt;
  381. }
  382. }
  383. ScFree(tmp);
  384. }
  385. /****************************************************************************************************
  386. * Function: RecDilate
  387. * Reconstruction by dilation of image with respect to reference using a structural element of
  388. * dimenions (sr, sc).
  389. ****************************************************************************************************/
  390. void RecDilate(unsigned char *image, unsigned char *reference, int rows, int cols, int sr, int sc)
  391. {
  392. int i, sz;
  393. unsigned char *prevImg, *pi, *Pi, differ;
  394. sz = rows * cols;
  395. if (!(prevImg = (unsigned char *)ScAlloc(sz))) {
  396. _SlibDebug(_WARN_, printf("RecDilate() ScAlloc failed\n") );
  397. return;
  398. }
  399. do {
  400. memcpy(prevImg, image, rows*cols);
  401. GeoDilate(image, reference, rows, cols, sr, sc);
  402. pi = image; Pi = prevImg;
  403. differ = 0;
  404. for(i=0; i<sz; i++) if(*(pi++) != *(Pi++)) { differ = 1; break;}
  405. } while (differ);
  406. ScFree(prevImg);
  407. }
  408. /****************************************************************************************************
  409. * Function: RecErode
  410. * Reconstruction by erosion of image with respect to reference using a structural element of
  411. * dimenions (sr, sc).
  412. ****************************************************************************************************/
  413. void RecErode(unsigned char *image, unsigned char *reference, int rows, int cols, int sr, int sc)
  414. {
  415. int i, sz;
  416. unsigned char *prevImg, *pi, *Pi, differ;
  417. sz = rows * cols;
  418. if (!(prevImg = (unsigned char *)ScAlloc(sz))) {
  419. _SlibDebug(_WARN_, printf("RecErode() ScAlloc failed\n") );
  420. return;
  421. }
  422. do {
  423. memcpy(prevImg, image, rows*cols);
  424. GeoErode(image, reference, rows, cols, sr, sc);
  425. pi = image; Pi = prevImg;
  426. differ = 0;
  427. for(i=0; i<sz; i++) if(*(pi++) != *(Pi++)) { differ = 1; break;}
  428. } while (differ);
  429. ScFree(prevImg);
  430. }
  431. /****************************************************************************************************
  432. * Function: OpenRecErode
  433. * Open by reconstruction of erosion of image using a structural element of
  434. * dimenions (sr, sc).
  435. ****************************************************************************************************/
  436. void OpenRecErode(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  437. {
  438. int sz;
  439. sz = rows * cols;
  440. Erode(image, out, rows, cols, sr, sc);
  441. RecDilate(out, image, rows, cols, sr, sc);
  442. }
  443. /****************************************************************************************************
  444. * Function: CloseRecDilate
  445. * Closing by reconstruction of dilation of image using a structural element of
  446. * dimenions (sr, sc).
  447. ****************************************************************************************************/
  448. void CloseRecDilate(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  449. {
  450. int sz;
  451. sz = rows * cols;
  452. Dilate(image, out, rows, cols, sr, sc);
  453. RecErode(out, image, rows, cols, sr, sc);
  454. }
  455. /****************************************************************************************************
  456. * Function: OpenCloseRec
  457. * Open-closing by reconstruction of image using a structural element of
  458. * dimenions (sr, sc).
  459. ****************************************************************************************************/
  460. void OpenCloseRec(unsigned char *image, unsigned char *out, int rows, int cols, int sr, int sc)
  461. {
  462. int sz;
  463. unsigned char *opened;
  464. sz = rows * cols;
  465. if (!(opened = (unsigned char *)ScAlloc(sz))) {
  466. _SlibDebug(_WARN_, printf("OpenCloseRec() ScAlloc failed\n") );
  467. return;
  468. }
  469. OpenRecErode(image, opened, rows, cols, sr, sc);
  470. CloseRecDilate(opened, out, rows, cols, sr, sc);
  471. ScFree(opened);
  472. }
  473. /****************************************************************************************************
  474. * Function: PredOpenCloseRec
  475. * Open-closing by reconstruction of image using a structural element of
  476. * dimenions (sr, sc), for prediction images.
  477. ****************************************************************************************************/
  478. void PredOpenCloseRec(int *predimage, int *predout, int rows, int cols, int sr, int sc)
  479. {
  480. int sz, i;
  481. unsigned char *opened, *image, *out;
  482. sz = rows * cols;
  483. if (!(image = (unsigned char *)ScAlloc(sz))) {
  484. _SlibDebug(_WARN_, printf("PredOpenCloseRec() ScAlloc failed\n") );
  485. return;
  486. }
  487. if (!(out = (unsigned char *)ScAlloc(sz))) {
  488. _SlibDebug(_WARN_, printf("PredOpenCloseRec() ScAlloc failed\n") );
  489. return;
  490. }
  491. for(i=0; i<sz; i++) image[i] = (unsigned char) predimage[i] + 128;
  492. if (!(opened = (unsigned char *)ScAlloc(sz))) {
  493. _SlibDebug(_WARN_, printf("PredOpenCloseRec() ScAlloc failed\n") );
  494. return;
  495. }
  496. OpenRecErode(image, opened, rows, cols, sr, sc);
  497. CloseRecDilate(opened, out, rows, cols, sr, sc);
  498. for(i=0; i<sz; i++) predout[i] = (int) out[i] - 128;
  499. ScFree(opened);
  500. ScFree(image);
  501. ScFree(out);
  502. }
  503. /**************************************************************************************************
  504. * Function: EdgeSelect
  505. * Given the edge map, copies to the output: pixels from the input image if edge points,
  506. * pixels form the filtered image if not edge points.
  507. *************************************************************************************************/
  508. static void EdgeSelect(H263_PictImage *input, H263_PictImage *filtd, unsigned char *edge,
  509. H263_PictImage *output, int rows, int cols)
  510. {
  511. unsigned char *pi, *po, *pf, *pe;
  512. int i, j;
  513. /* Luminance */
  514. pi = input->lum; pf = filtd->lum;
  515. po = output->lum; pe = edge;
  516. for(i=0; i<rows; i++) {
  517. for(j=0; j<cols; j++, pi++, pf++, pe++, po++) {
  518. *po = (*pe ? *pi : *pf);
  519. }
  520. }
  521. rows /=2; cols /=2;
  522. /* Color 1 */
  523. pi = input->Cr; pf = filtd->Cr;
  524. po = output->Cr; pe = edge;
  525. for(i=0; i<rows; i++) {
  526. for(j=0; j<cols; j++, pi++, pf++, pe+=2, po++) {
  527. *po = (*pe ? *pi : *pf);
  528. }
  529. pe += cols;
  530. }
  531. /* Color 2 */
  532. pi = input->Cb; pf = filtd->Cb;
  533. po = output->Cb; pe = edge;
  534. for(i=0; i<rows; i++) {
  535. for(j=0; j<cols; j++, pi++, pf++, pe+=2, po++) {
  536. *po = (*pe ? *pi : *pf);
  537. }
  538. pe += cols;
  539. }
  540. }
  541. /**************************************************************************************************
  542. * Function: AdaptClean
  543. * Adaptly cleans curr_image, by filtering it by open/close by reconstruction at the pixels
  544. * where there is no edge info. The edge map is grown by the size of the morphological
  545. * operator, to avoid oversmoothing of details. sr, sc are the dimensions of the structuring
  546. * element for the morphologic operations
  547. *************************************************************************************************/
  548. H263_PictImage *sv_H263AdaptClean(SvH263CompressInfo_t *H263Info,
  549. H263_PictImage *curr_image, int rows, int cols, int sr, int sc)
  550. {
  551. H263_PictImage *morph, *clean;
  552. unsigned char *Edge, *Orient, *CleanEmap;
  553. if (!(Edge = (unsigned char *)ScAlloc(rows*cols))) {
  554. _SlibDebug(_WARN_, printf("PredOpenCloseRec() ScAlloc failed\n") );
  555. return(NULL);
  556. }
  557. if (!(Orient = (unsigned char *)ScAlloc(rows*cols))) {
  558. _SlibDebug(_WARN_, printf("PredOpenCloseRec() ScAlloc failed\n") );
  559. return(NULL);
  560. }
  561. sv_H263EdgeMap(curr_image->lum, Edge, Orient, rows, cols);
  562. morph = sv_H263InitImage(H263Info->pels*H263Info->lines);
  563. OpenCloseRec(H263Info->curr_image->lum, morph->lum, rows, cols, sr, sc);
  564. OpenCloseRec(H263Info->curr_image->Cr, morph->Cr, rows >> 1, cols >> 1, sr, sc);
  565. OpenCloseRec(H263Info->curr_image->Cb, morph->Cb, rows >> 1, cols >> 1, sr, sc);
  566. clean = sv_H263InitImage(rows*cols);
  567. if (!(CleanEmap = (unsigned char *)ScAlloc(rows*cols))) {
  568. _SlibDebug(_WARN_, printf("PredOpenCloseRec() ScAlloc failed\n") );
  569. return(NULL);
  570. }
  571. CloseOpen(Edge, CleanEmap, rows, cols, sr, sc);
  572. EdgeSelect(H263Info->curr_image, morph, CleanEmap, clean, rows, cols);
  573. sv_H263FreeImage(morph);
  574. ScFree(CleanEmap);
  575. ScFree(Orient);
  576. ScFree(Edge);
  577. return clean;
  578. }
  579. /*****************************************************************
  580. * Function MorphLayers
  581. * Builds an array of successively more morphologically low pass
  582. * filtered images. sz is the size of the structuring element (-1 for
  583. * the cross '+').
  584. *****************************************************************/
  585. H263_PictImage **sv_H263MorphLayers(H263_PictImage *img, int depth, int rows, int cols, int sz)
  586. {
  587. int d;
  588. H263_PictImage **PictFiltd;
  589. PictFiltd = (H263_PictImage **) ScAlloc(depth*sizeof(H263_PictImage *));
  590. for(d=0; d<depth; d++) {
  591. PictFiltd[d] = sv_H263InitImage(rows*cols);
  592. }
  593. /* Luminance */
  594. memcpy(PictFiltd[0]->lum, img->lum, rows*cols);
  595. for(d=1; d<depth; d++)
  596. OpenCloseRec(PictFiltd[d-1]->lum, PictFiltd[d]->lum, rows, cols, sz, sz);
  597. rows/=2; cols/=2;
  598. /* Chroma 1 */
  599. memcpy(PictFiltd[0]->Cr, img->Cr, rows*cols);
  600. for(d=1; d<depth; d++)
  601. OpenCloseRec(PictFiltd[d-1]->Cr, PictFiltd[d]->Cr, rows, cols, sz, sz);
  602. /* Chroma 2 */
  603. memcpy(PictFiltd[0]->Cb, img->Cb, rows*cols);
  604. for(d=1; d<depth; d++)
  605. OpenCloseRec(PictFiltd[d-1]->Cb, PictFiltd[d]->Cb, rows, cols, sz, sz);
  606. return PictFiltd;
  607. }