edges-orig.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;; File: edges.c
  4. ;;; Author: Eero Simoncelli
  5. ;;; Description: Boundary handling routines for use with convolve.c
  6. ;;; Creation Date: Spring 1987.
  7. ;;; MODIFIED, 6/96, to operate on double float arrays.
  8. ;;; MODIFIED by dgp, 4/1/97, to support THINK C.
  9. ;;; ----------------------------------------------------------------
  10. ;;; Object-Based Vision and Image Understanding System (OBVIUS),
  11. ;;; Copyright 1988, Vision Science Group, Media Laboratory,
  12. ;;; Massachusetts Institute of Technology.
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. */
  15. /*
  16. This file contains functions which determine how edges are to be
  17. handled when performing convolutions of images with linear filters.
  18. Any edge handling function which is local and linear may be defined,
  19. except (unfortunately) constants cannot be added. So to treat the
  20. edges as if the image is surrounded by a gray field, you must paste it
  21. into a gray image, convolve, and crop it out...
  22. The main convolution function is called internal_filter and is defined
  23. in the file convolve.c. The idea is that the convolution function
  24. calls the edge handling function which computes a new filter based on
  25. the old filter and the distance to the edge of the image. For
  26. example, reflection is done by reflecting the filter through the
  27. appropriate axis and summing. Currently defined functions are listed
  28. below.
  29. */
  30. /*
  31. #define DEBUG
  32. */
  33. #include <stdio.h>
  34. #include <math.h>
  35. #include <string.h>
  36. #include "convolve.h"
  37. #define sgn(a) ( ((a)>0)?1:(((a)<0)?-1:0) )
  38. #define clip(a,mn,mx) ( ((a)<(mn))?(mn):(((a)>=(mx))?(mx-1):(a)) )
  39. int reflect1(), reflect2(), repeat(), zero(), Extend(), nocompute();
  40. int ereflect(), predict();
  41. /* Lookup table matching a descriptive string to the edge-handling function */
  42. #if !THINK_C
  43. static EDGE_HANDLER edge_foos[] =
  44. {
  45. { "dont-compute", nocompute }, /* zero output for filter touching edge */
  46. { "zero", zero }, /* zero outside of image */
  47. { "repeat", repeat }, /* repeat edge pixel */
  48. { "reflect1", reflect1 }, /* reflect about edge pixels */
  49. { "reflect2", reflect2 }, /* reflect image, including edge pixels */
  50. { "extend", Extend }, /* extend (reflect & invert) */
  51. { "predict", predict }, /* predict based on portion covered by filt */
  52. { "ereflect", ereflect }, /* orthogonal QMF reflection */
  53. };
  54. #else
  55. /*
  56. This is really stupid, but THINK C won't allow initialization of static variables in
  57. a code resource with string addresses. So we do it this way.
  58. The 68K code for a MATLAB 4 MEX file can only be created by THINK C.
  59. However, for MATLAB 5, we'll be able to use Metrowerks CodeWarrior for both 68K and PPC, so this
  60. cludge can be dropped when we drop support for MATLAB 4.
  61. Denis Pelli, 4/1/97.
  62. */
  63. static EDGE_HANDLER edge_foos[8];
  64. void InitializeTable(EDGE_HANDLER edge_foos[])
  65. {
  66. static int i=0;
  67. if(i>0) return;
  68. edge_foos[i].name="dont-compute";
  69. edge_foos[i++].func=nocompute;
  70. edge_foos[i].name="zero";
  71. edge_foos[i++].func=zero;
  72. edge_foos[i].name="repeat";
  73. edge_foos[i++].func=repeat;
  74. edge_foos[i].name="reflect1";
  75. edge_foos[i++].func=reflect1;
  76. edge_foos[i].name="reflect2";
  77. edge_foos[i++].func=reflect2;
  78. edge_foos[i].name="extend";
  79. edge_foos[i++].func=Extend;
  80. edge_foos[i].name="predict";
  81. edge_foos[i++].func=predict;
  82. edge_foos[i].name="ereflect";
  83. edge_foos[i++].func=ereflect;
  84. }
  85. #endif
  86. /*
  87. Function looks up an edge handler id string in the structure above, and
  88. returns the associated function
  89. */
  90. fptr edge_function(char *edges)
  91. {
  92. int i;
  93. #if THINK_C
  94. InitializeTable(edge_foos);
  95. #endif
  96. for (i = 0; i<sizeof(edge_foos)/sizeof(EDGE_HANDLER); i++)
  97. if (strcmp(edges,edge_foos[i].name) == 0)
  98. return(edge_foos[i].func);
  99. printf("Error: '%s' is not the name of a valid edge-handler!\n",edges);
  100. for (i=0; i<sizeof(edge_foos)/sizeof(EDGE_HANDLER); i++)
  101. {
  102. if (i==0) printf(" Options are: ");
  103. else printf(", ");
  104. printf("%s",edge_foos[i].name);
  105. }
  106. printf("\n");
  107. return(0);
  108. }
  109. /*
  110. ---------------- EDGE HANDLER ARGUMENTS ------------------------
  111. filt - array of filter taps.
  112. x_dim, y_dim - x and y dimensions of filt.
  113. x_pos - position of filter relative to the horizontal image edges. Negative
  114. values indicate left edge, positive indicate right edge. Zero
  115. indicates that the filter is not touching either edge. An absolute
  116. value of 1 indicates that the edge tap of the filter is over the
  117. edge pixel of the image.
  118. y_pos - analogous to x_pos.
  119. result - array where the resulting filter will go. The edge
  120. of this filter will be aligned with the image for application...
  121. f_or_e - equal to one of the two constants EXPAND or FILTER.
  122. --------------------------------------------------------------------
  123. */
  124. /* --------------------------------------------------------------------
  125. nocompute() - Return zero for values where filter hangs over the edge.
  126. */
  127. int nocompute(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  128. register double *filt, *result;
  129. register int x_dim;
  130. int y_dim, x_pos, y_pos, f_or_e;
  131. {
  132. register int i;
  133. register int size = x_dim*y_dim;
  134. if ( (x_pos>1) OR (x_pos<-1) OR (y_pos>1) OR (y_pos<-1) )
  135. for (i=0; i<size; i++) result[i] = 0.0;
  136. else
  137. for (i=0; i<size; i++) result[i] = filt[i];
  138. return(0);
  139. }
  140. /* --------------------------------------------------------------------
  141. zero() - Zero outside of image. Discontinuous, but adds zero energy. */
  142. int zero(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  143. register double *filt, *result;
  144. register int x_dim;
  145. int y_dim, x_pos, y_pos, f_or_e;
  146. {
  147. register int y_filt,x_filt, y_res,x_res;
  148. int filt_sz = x_dim*y_dim;
  149. int x_start = ((x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0));
  150. int y_start = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  151. int i;
  152. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  153. for (y_filt=0, y_res=y_start;
  154. y_filt<filt_sz;
  155. y_filt+=x_dim, y_res+=x_dim)
  156. if ((y_res >= 0) AND (y_res < filt_sz))
  157. for (x_filt=y_filt, x_res=x_start;
  158. x_filt<y_filt+x_dim;
  159. x_filt++, x_res++)
  160. if ((x_res >= 0) AND (x_res < x_dim))
  161. result[y_res+x_res] = filt[x_filt];
  162. return(0);
  163. }
  164. /* --------------------------------------------------------------------
  165. repeat() - repeat edge pixel. Continuous, but content is usually
  166. different from image.
  167. */
  168. int repeat(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  169. register double *filt, *result;
  170. register int x_dim;
  171. int y_dim, x_pos, y_pos, f_or_e;
  172. {
  173. register int y_filt,x_filt, y_res,x_res;
  174. int filt_sz = x_dim*y_dim;
  175. int x_start = ((x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0));
  176. int y_start = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  177. int i;
  178. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  179. for (y_filt=0, y_res=y_start;
  180. y_filt<filt_sz;
  181. y_filt+=x_dim, y_res+=x_dim)
  182. for (x_filt=y_filt, x_res=x_start;
  183. x_filt<y_filt+x_dim;
  184. x_filt++, x_res++)
  185. result[((y_res>=0)?((y_res<filt_sz)?y_res:(filt_sz-x_dim)):0)
  186. + ((x_res>=0)?((x_res<x_dim)?x_res:(x_dim-1)):0)]
  187. += filt[x_filt];
  188. return(0);
  189. }
  190. /* --------------------------------------------------------------------
  191. reflect2() - "Normal" image reflection. The edge pixel is repeated,
  192. then the next pixel, etc. Continuous, attempting to maintain
  193. "similar" content, but discontinuous first derivative.
  194. */
  195. int reflect2(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  196. register double *filt, *result;
  197. register int x_dim;
  198. int y_dim, x_pos, y_pos, f_or_e;
  199. {
  200. register int y_filt,x_filt, y_edge,x_edge;
  201. register int x_base = (x_pos>0)?(x_dim-1):0;
  202. register int y_base = (y_pos>0)?(x_dim*(y_dim-1)):0;
  203. int filt_sz = x_dim*y_dim;
  204. int x_edge_dist = (x_pos>0)?(x_pos-x_dim-1):(x_pos+1);
  205. int y_edge_dist = x_dim * ((y_pos>0)?(y_pos-y_dim-1):(y_pos+1));
  206. int i;
  207. #ifdef DEBUG
  208. printf("(%d,%d) ",y_pos,x_pos);
  209. if (x_pos==0) printf("\n");
  210. #endif
  211. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  212. for (y_filt=0, y_edge=y_edge_dist;
  213. y_filt<filt_sz;
  214. y_filt+=x_dim, y_edge+=x_dim)
  215. {
  216. if (y_edge IS 0) y_edge+=x_dim;
  217. for (x_filt=y_filt, x_edge=x_edge_dist;
  218. x_filt<y_filt+x_dim;
  219. x_filt++, x_edge++)
  220. {
  221. if (x_edge IS 0) x_edge++;
  222. result[ABS(y_base-ABS(y_edge)+x_dim) + ABS(x_base-ABS(x_edge)+1)]
  223. += filt[x_filt];
  224. }
  225. }
  226. return(0);
  227. }
  228. /* --------------------------------------------------------------------
  229. reflect1() - Reflection through the edge pixels. This is the right thing
  230. to do if you are subsampling by 2, since it maintains parity (even
  231. pixels positions remain even, odd ones remain odd). (note: procedure differs
  232. depending on f_or_e parameter). */
  233. int reflect1(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  234. register double *filt, *result;
  235. register int x_dim;
  236. int y_dim, x_pos, y_pos, f_or_e;
  237. {
  238. int filt_sz = x_dim*y_dim;
  239. register int x_start = 0, y_start = 0, x_stop = x_dim, y_stop = filt_sz;
  240. register int y_filt,x_filt, y_edge,x_edge;
  241. register int x_base = (x_pos>0)?(x_dim-1):0;
  242. register int y_base = (y_pos>0)?(x_dim*(y_dim-1)):0;
  243. int x_edge_dist = (x_pos>0)?(x_pos-x_dim):((x_pos<0)?(x_pos+1):0);
  244. int y_edge_dist = x_dim * ((y_pos>0)?(y_pos-y_dim):((y_pos<0)?(y_pos+1):0));
  245. int i;
  246. int mx_pos = (x_dim/2)+1;
  247. int my_pos = (y_dim/2)+1;
  248. #ifdef DEBUG
  249. printf("(%d,%d) ",y_pos,x_pos);
  250. if (x_pos==0) printf("\n");
  251. #endif
  252. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  253. /* if EXPAND and filter is centered on image edge, do not reflect */
  254. if (f_or_e IS EXPAND)
  255. {
  256. if (x_pos IS mx_pos) x_stop = (x_dim+1)/2;
  257. else if (x_pos IS -mx_pos) { x_start = x_dim/2; x_edge_dist = 0; }
  258. if (y_pos IS my_pos) y_stop = x_dim*((y_dim+1)/2);
  259. else if (y_pos IS -my_pos) { y_start = x_dim*(y_dim/2); y_edge_dist = 0;}
  260. }
  261. /* reflect at boundary of image */
  262. for (y_filt=y_start, y_edge=y_edge_dist;
  263. y_filt<y_stop;
  264. y_filt+=x_dim, y_edge+=x_dim)
  265. for (x_filt=y_filt+x_start, x_edge=x_edge_dist;
  266. x_filt<y_filt+x_stop;
  267. x_filt++, x_edge++)
  268. result[ABS(y_base-ABS(y_edge)) + ABS(x_base-ABS(x_edge))]
  269. += filt[x_filt];
  270. /* if EXPAND and filter is not centered on image edge, mult edge by 2 */
  271. if (f_or_e IS EXPAND)
  272. {
  273. if ( (ABS(x_pos) ISNT mx_pos) AND (x_pos ISNT 0) )
  274. for (y_filt=x_base; y_filt<filt_sz; y_filt+=x_dim)
  275. result[y_filt] += result[y_filt];
  276. if ( (ABS(y_pos) ISNT my_pos) AND (y_pos ISNT 0) )
  277. for (x_filt=y_base; x_filt<y_base+x_dim; x_filt++)
  278. result[x_filt] += result[x_filt];
  279. }
  280. return(0);
  281. }
  282. /* --------------------------------------------------------------------
  283. Extend() - Extend image by reflecting and inverting about edge pixel
  284. value. Maintains continuity in intensity AND first derivative (but
  285. not higher derivs).
  286. */
  287. int Extend(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  288. register double *filt, *result;
  289. register int x_dim;
  290. int y_dim, x_pos, y_pos, f_or_e;
  291. {
  292. int filt_sz = x_dim*y_dim;
  293. register int x_start = 0, y_start = 0, x_stop = x_dim, y_stop = filt_sz;
  294. register int y_filt,x_filt, y_edge,x_edge;
  295. register int x_base = (x_pos>0)?(x_dim-1):0;
  296. register int y_base = (y_pos>0)?(x_dim*(y_dim-1)):0;
  297. int x_edge_dist = (x_pos>0)?(x_pos-x_dim):((x_pos<-1)?(x_pos+1):0);
  298. int y_edge_dist = x_dim * ((y_pos>0)?(y_pos-y_dim):((y_pos<-1)?(y_pos+1):0));
  299. int i;
  300. int mx_pos = (x_dim/2)+1;
  301. int my_pos = (y_dim/2)+1;
  302. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  303. /* if EXPAND and filter is centered on image edge, do not reflect */
  304. if (f_or_e IS EXPAND)
  305. {
  306. if (x_pos IS mx_pos) x_stop = (x_dim+1)/2;
  307. else if (x_pos IS -mx_pos) { x_start = x_dim/2; x_edge_dist = 0; }
  308. if (y_pos IS my_pos) y_stop = x_dim*((y_dim+1)/2);
  309. else if (y_pos IS -my_pos) { y_start = x_dim*(y_dim/2); y_edge_dist = 0;}
  310. }
  311. /* reflect at boundary of image */
  312. for (y_filt=y_start, y_edge=y_edge_dist;
  313. y_filt<y_stop;
  314. y_filt+=x_dim, y_edge+=x_dim)
  315. for (x_filt=y_filt+x_start, x_edge=x_edge_dist;
  316. x_filt<y_filt+x_stop;
  317. x_filt++, x_edge++)
  318. if (((!y_base AND (sgn(y_edge) IS -1)) /* y overhanging */
  319. OR
  320. (y_base AND (sgn(y_edge) IS 1)))
  321. ISNT /* XOR */
  322. ((!x_base AND (sgn(x_edge) IS -1)) /* x overhanging */
  323. OR
  324. (x_base AND (sgn(x_edge) IS 1))))
  325. {
  326. result[ABS(y_base-ABS(y_edge)) + ABS(x_base-ABS(x_edge))]
  327. -= filt[x_filt];
  328. result[clip(y_base+y_edge,0,y_dim) + clip(x_base+x_edge,0,x_dim)]
  329. += filt[x_filt] + filt[x_filt];
  330. }
  331. else result[ABS(y_base-ABS(y_edge)) + ABS(x_base-ABS(x_edge))]
  332. += filt[x_filt];
  333. return(0);
  334. }
  335. /* --------------------------------------------------------------------
  336. predict() - Simple prediction. Like zero, but multiplies the result
  337. by the reciprocal of the percentage of filter being used. (i.e. if
  338. 50% of the filter is hanging over the edge of the image, multiply the
  339. taps being used by 2). */
  340. int predict(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  341. register double *filt, *result;
  342. register int x_dim;
  343. int y_dim, x_pos, y_pos, f_or_e;
  344. {
  345. register int y_filt,x_filt, y_res,x_res;
  346. register double taps_used = 0.0; /* int *** */
  347. register double fraction = 0.0;
  348. int filt_sz = x_dim*y_dim;
  349. int x_start = ((x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0));
  350. int y_start = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  351. int i;
  352. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  353. for (y_filt=0, y_res=y_start;
  354. y_filt<filt_sz;
  355. y_filt+=x_dim, y_res+=x_dim)
  356. if ((y_res >= 0) AND (y_res < filt_sz))
  357. for (x_filt=y_filt, x_res=x_start;
  358. x_filt<y_filt+x_dim;
  359. x_filt++, x_res++)
  360. if ((x_res >= 0) AND (x_res < x_dim))
  361. {
  362. result[y_res+x_res] = filt[x_filt];
  363. taps_used += ABS(filt[x_filt]);
  364. }
  365. printf("TU: %f\n",taps_used);
  366. if (f_or_e IS FILTER)
  367. {
  368. /* fraction = ( (double) filt_sz ) / ( (double) taps_used ); */
  369. for (i=0; i<filt_sz; i++) fraction += ABS(filt[i]);
  370. fraction = ( fraction / taps_used );
  371. for (i=0; i<filt_sz; i++) result[i] *= fraction;
  372. }
  373. return(0);
  374. }
  375. /* --------------------------------------------------------------------
  376. Reflect, multiplying tap of filter which is at the edge of the image
  377. by root 2. This maintains orthogonality of odd-length linear-phase
  378. QMF filters, but it is not useful for most applications, since it
  379. alters the DC level. */
  380. int ereflect(filt,x_dim,y_dim,x_pos,y_pos,result,f_or_e)
  381. register double *filt, *result;
  382. register int x_dim;
  383. int y_dim, x_pos, y_pos, f_or_e;
  384. {
  385. register int y_filt,x_filt, y_edge,x_edge;
  386. register int x_base = (x_pos>0)?(x_dim-1):0;
  387. register int y_base = x_dim * ( (y_pos>0)?(y_dim-1):0 );
  388. int filt_sz = x_dim*y_dim;
  389. int x_edge_dist = (x_pos>1)?(x_pos-x_dim):((x_pos<-1)?(x_pos+1):0);
  390. int y_edge_dist = x_dim * ( (y_pos>1)?(y_pos-y_dim):((y_pos<-1)?(y_pos+1):0) );
  391. int i;
  392. double norm,onorm;
  393. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  394. /* reflect at boundary */
  395. for (y_filt=0, y_edge=y_edge_dist;
  396. y_filt<filt_sz;
  397. y_filt+=x_dim, y_edge+=x_dim)
  398. for (x_filt=y_filt, x_edge=x_edge_dist;
  399. x_filt<y_filt+x_dim;
  400. x_filt++, x_edge++)
  401. result[ABS(y_base-ABS(y_edge)) + ABS(x_base-ABS(x_edge))]
  402. += filt[x_filt];
  403. /* now multiply edge by root 2 */
  404. if (x_pos ISNT 0)
  405. for (y_filt=x_base; y_filt<filt_sz; y_filt+=x_dim)
  406. result[y_filt] *= ROOT2;
  407. if (y_pos ISNT 0)
  408. for (x_filt=y_base; x_filt<y_base+x_dim; x_filt++)
  409. result[x_filt] *= ROOT2;
  410. /* now normalize to norm of original filter */
  411. for (norm=0.0,i=0; i<filt_sz; i++)
  412. norm += (result[i]*result[i]);
  413. norm=sqrt(norm);
  414. for (onorm=0.0,i=0; i<filt_sz; i++)
  415. onorm += (filt[i]*filt[i]);
  416. onorm = sqrt(onorm);
  417. norm = norm/onorm;
  418. for (i=0; i<filt_sz; i++)
  419. result[i] /= norm;
  420. return(0);
  421. }
  422. /* ------- printout stuff for testing ------------------------------
  423. printf("Xpos: %d, Ypos: %d", x_pos, y_pos);
  424. for (y_filt=0; y_filt<y_dim; y_filt++)
  425. {
  426. printf("\n");
  427. for (x_filt=0; x_filt<x_dim; x_filt++)
  428. printf("%6.1f", result[y_filt*x_dim+x_filt]);
  429. }
  430. printf("\n");
  431. */
  432. /* Local Variables: */
  433. /* buffer-read-only: t */
  434. /* End: */