edges.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. ;;; MODIFIED, 8/97: reflect1, reflect2, repeat, extend upgraded to
  10. ;;; work properly for non-symmetric filters. Added qreflect2 to handle
  11. ;;; even-length QMF's which broke under the reflect2 modification.
  12. ;;; ----------------------------------------------------------------
  13. ;;; Object-Based Vision and Image Understanding System (OBVIUS),
  14. ;;; Copyright 1988, Vision Science Group, Media Laboratory,
  15. ;;; Massachusetts Institute of Technology.
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. */
  18. /* This file contains functions which determine how edges are to be
  19. handled when performing convolutions of images with linear filters.
  20. Any edge handling function which is local and linear may be defined,
  21. except (unfortunately) constants cannot be added. So to treat the
  22. edges as if the image is surrounded by a gray field, you must paste it
  23. into a gray image, convolve, and crop it out... The main convolution
  24. functions are called internal_reduce and internal_expand and are
  25. defined in the file convolve.c. The idea is that the convolution
  26. function calls the edge handling function which computes a new filter
  27. based on the old filter and the distance to the edge of the image.
  28. For example, reflection is done by reflecting the filter through the
  29. appropriate axis and summing. Currently defined functions are listed
  30. below.
  31. */
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include <string.h>
  35. #include "convolve.h"
  36. #define sgn(a) ( ((a)>0)?1:(((a)<0)?-1:0) )
  37. #define clip(a,mn,mx) ( ((a)<(mn))?(mn):(((a)>=(mx))?(mx-1):(a)) )
  38. int reflect1(), reflect2(), qreflect2(), repeat(), zero(), Extend(), nocompute();
  39. int ereflect(), predict();
  40. /* Lookup table matching a descriptive string to the edge-handling function */
  41. #if !THINK_C
  42. static EDGE_HANDLER edge_foos[] =
  43. {
  44. { "dont-compute", nocompute }, /* zero output for filter touching edge */
  45. { "zero", zero }, /* zero outside of image */
  46. { "repeat", repeat }, /* repeat edge pixel */
  47. { "reflect1", reflect1 }, /* reflect about edge pixels */
  48. { "reflect2", reflect2 }, /* reflect image, including edge pixels */
  49. { "qreflect2", qreflect2 }, /* reflect image, including edge pixels
  50. for even-length QMF decompositions */
  51. { "extend", Extend }, /* extend (reflect & invert) */
  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="qreflect2";
  79. edge_foos[i++].func=qreflect2;
  80. edge_foos[i].name="extend";
  81. edge_foos[i++].func=Extend;
  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) IS 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 IS 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. r_or_e - equal to one of the two constants EXPAND or REDUCE.
  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,r_or_e)
  128. register double *filt, *result;
  129. register int x_dim;
  130. int y_dim, x_pos, y_pos, r_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,r_or_e)
  143. register double *filt, *result;
  144. register int x_dim;
  145. int y_dim, x_pos, y_pos, r_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. reflect1() - Reflection through the edge pixels. Continuous, but
  166. discontinuous first derivative. This is the right thing to do if you
  167. are subsampling by 2, since it maintains parity (even pixels positions
  168. remain even, odd ones remain odd).
  169. */
  170. int reflect1(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  171. register double *filt, *result;
  172. register int x_dim;
  173. int y_dim, x_pos, y_pos, r_or_e;
  174. {
  175. int filt_sz = x_dim*y_dim;
  176. register int y_filt,x_filt, y_res, x_res;
  177. register int x_base = (x_pos>0)?(x_dim-1):0;
  178. register int y_base = x_dim * ((y_pos>0)?(y_dim-1):0);
  179. int x_overhang = (x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0);
  180. int y_overhang = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  181. int i;
  182. int mx_pos = (x_pos<0)?(x_dim/2):((x_dim-1)/2);
  183. int my_pos = x_dim * ((y_pos<0)?(y_dim/2):((y_dim-1)/2));
  184. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  185. if (r_or_e IS REDUCE)
  186. for (y_filt=0, y_res=y_overhang-y_base;
  187. y_filt<filt_sz;
  188. y_filt+=x_dim, y_res+=x_dim)
  189. for (x_filt=y_filt, x_res=x_overhang-x_base;
  190. x_filt<y_filt+x_dim;
  191. x_filt++, x_res++)
  192. result[ABS(y_base-ABS(y_res)) + ABS(x_base-ABS(x_res))]
  193. += filt[x_filt];
  194. else {
  195. y_overhang = ABS(y_overhang);
  196. x_overhang = ABS(x_overhang);
  197. for (y_res=y_base, y_filt = y_base-y_overhang;
  198. y_filt > y_base-filt_sz;
  199. y_filt-=x_dim, y_res-=x_dim)
  200. {
  201. for (x_res=x_base, x_filt=x_base-x_overhang;
  202. x_filt > x_base-x_dim;
  203. x_res--, x_filt--)
  204. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  205. if ((x_overhang ISNT mx_pos) AND (x_pos ISNT 0))
  206. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang;
  207. x_filt > x_base-x_dim;
  208. x_res--, x_filt--)
  209. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  210. }
  211. if ((y_overhang ISNT my_pos) AND (y_pos ISNT 0))
  212. for (y_res=y_base, y_filt = y_base-2*my_pos+y_overhang;
  213. y_filt > y_base-filt_sz;
  214. y_filt-=x_dim, y_res-=x_dim)
  215. {
  216. for (x_res=x_base, x_filt=x_base-x_overhang;
  217. x_filt > x_base-x_dim;
  218. x_res--, x_filt--)
  219. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  220. if ((x_overhang ISNT mx_pos) AND (x_pos ISNT 0))
  221. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang;
  222. x_filt > x_base-x_dim;
  223. x_res--, x_filt--)
  224. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  225. }
  226. }
  227. return(0);
  228. }
  229. /* --------------------------------------------------------------------
  230. reflect2() - Reflect image at boundary. The edge pixel is repeated,
  231. then the next pixel, etc. Continuous, but discontinuous first
  232. derivative.
  233. */
  234. int reflect2(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  235. register double *filt, *result;
  236. register int x_dim;
  237. int y_dim, x_pos, y_pos, r_or_e;
  238. {
  239. int filt_sz = x_dim*y_dim;
  240. register int y_filt,x_filt, y_res, x_res;
  241. register int x_base = (x_pos>0)?(x_dim-1):0;
  242. register int y_base = x_dim * ((y_pos>0)?(y_dim-1):0);
  243. int x_overhang = (x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0);
  244. int y_overhang = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  245. int i;
  246. int mx_pos = (x_pos<0)?(x_dim/2):((x_dim-1)/2);
  247. int my_pos = x_dim * ((y_pos<0)?(y_dim/2):((y_dim-1)/2));
  248. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  249. if (r_or_e IS REDUCE)
  250. for (y_filt=0, y_res = (y_overhang-y_base) - ((y_pos>0)?x_dim:0);
  251. y_filt<filt_sz;
  252. y_filt+=x_dim, y_res+=x_dim)
  253. {
  254. if (y_res IS 0) y_res+=x_dim;
  255. for (x_filt=y_filt, x_res = (x_overhang-x_base) - ((x_pos>0)?1:0);
  256. x_filt<y_filt+x_dim;
  257. x_filt++, x_res++)
  258. {
  259. if (x_res IS 0) x_res++;
  260. result[ABS(y_base-ABS(y_res)+x_dim) + ABS(x_base-ABS(x_res)+1)]
  261. += filt[x_filt];
  262. }
  263. }
  264. else {
  265. y_overhang = ABS(y_overhang);
  266. x_overhang = ABS(x_overhang);
  267. for (y_res=y_base, y_filt = y_base-y_overhang;
  268. y_filt > y_base-filt_sz;
  269. y_filt-=x_dim, y_res-=x_dim)
  270. {
  271. for (x_res=x_base, x_filt=x_base-x_overhang;
  272. x_filt > x_base-x_dim;
  273. x_res--, x_filt--)
  274. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  275. if (x_pos ISNT 0)
  276. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang-1;
  277. x_filt > x_base-x_dim;
  278. x_res--, x_filt--)
  279. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  280. }
  281. if (y_pos ISNT 0)
  282. for (y_res=y_base, y_filt = y_base-2*my_pos+y_overhang-x_dim;
  283. y_filt > y_base-filt_sz;
  284. y_filt-=x_dim, y_res-=x_dim)
  285. {
  286. for (x_res=x_base, x_filt=x_base-x_overhang;
  287. x_filt > x_base-x_dim;
  288. x_res--, x_filt--)
  289. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  290. if (x_pos ISNT 0)
  291. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang-1;
  292. x_filt > x_base-x_dim;
  293. x_res--, x_filt--)
  294. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  295. }
  296. }
  297. return(0);
  298. }
  299. /* --------------------------------------------------------------------
  300. qreflect2() - Modified version of reflect2 that works properly for
  301. even-length QMF filters.
  302. */
  303. int qreflect2(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  304. double *filt, *result;
  305. int x_dim, y_dim, x_pos, y_pos, r_or_e;
  306. {
  307. reflect2(filt,x_dim,y_dim,x_pos,y_pos,result,0);
  308. return(0);
  309. }
  310. /* --------------------------------------------------------------------
  311. repeat() - repeat edge pixel. Continuous, with discontinuous first
  312. derivative.
  313. */
  314. int repeat(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  315. register double *filt, *result;
  316. register int x_dim;
  317. int y_dim, x_pos, y_pos, r_or_e;
  318. {
  319. register int y_filt,x_filt, y_res,x_res, y_tmp, x_tmp;
  320. register int x_base = (x_pos>0)?(x_dim-1):0;
  321. register int y_base = x_dim * ((y_pos>0)?(y_dim-1):0);
  322. int x_overhang = ((x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0));
  323. int y_overhang = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  324. int filt_sz = x_dim*y_dim;
  325. int mx_pos = (x_dim/2);
  326. int my_pos = x_dim * (y_dim/2);
  327. int i;
  328. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  329. if (r_or_e IS REDUCE)
  330. for (y_filt=0, y_res=y_overhang;
  331. y_filt<filt_sz;
  332. y_filt+=x_dim, y_res+=x_dim)
  333. for (x_filt=y_filt, x_res=x_overhang;
  334. x_filt<y_filt+x_dim;
  335. x_filt++, x_res++)
  336. /* Clip the index: */
  337. result[((y_res>=0)?((y_res<filt_sz)?y_res:(filt_sz-x_dim)):0)
  338. + ((x_res>=0)?((x_res<x_dim)?x_res:(x_dim-1)):0)]
  339. += filt[x_filt];
  340. else {
  341. if ((y_base-y_overhang) ISNT my_pos)
  342. for (y_res=y_base, y_filt=y_base-ABS(y_overhang);
  343. y_filt > y_base-filt_sz;
  344. y_filt-=x_dim, y_res-=x_dim)
  345. if ((x_base-x_overhang) ISNT mx_pos)
  346. for (x_res=x_base, x_filt=x_base-ABS(x_overhang);
  347. x_filt > x_base-x_dim;
  348. x_res--, x_filt--)
  349. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  350. else /* ((x_base-x_overhang) IS mx_pos) */
  351. for (x_res=x_base, x_filt=x_base-ABS(x_overhang);
  352. x_filt > x_base-x_dim;
  353. x_filt--, x_res--)
  354. for(x_tmp=x_filt; x_tmp > x_base-x_dim; x_tmp--)
  355. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_tmp)];
  356. else /* ((y_base-y_overhang) IS my_pos) */
  357. for (y_res=y_base, y_filt=y_base-ABS(y_overhang);
  358. y_filt > y_base-filt_sz;
  359. y_filt-=x_dim, y_res-=x_dim)
  360. for (y_tmp=y_filt; y_tmp > y_base-filt_sz; y_tmp-=x_dim)
  361. if ((x_base-x_overhang) ISNT mx_pos)
  362. for (x_res=x_base, x_filt=x_base-ABS(x_overhang);
  363. x_filt > x_base-x_dim;
  364. x_filt--, x_res--)
  365. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_tmp)+ABS(x_filt)];
  366. else /* ((x_base-x_overhang) IS mx_pos) */
  367. for (x_res=x_base, x_filt=x_base-ABS(x_overhang);
  368. x_filt > x_base-x_dim;
  369. x_filt--, x_res--)
  370. for (x_tmp=x_filt; x_tmp > x_base-x_dim; x_tmp--)
  371. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_tmp)+ABS(x_tmp)];
  372. } /* End, if r_or_e IS EXPAND */
  373. return(0);
  374. }
  375. /* --------------------------------------------------------------------
  376. extend() - Extend image by reflecting and inverting about edge pixel
  377. value. Maintains continuity in intensity AND first derivative (but
  378. not higher derivs).
  379. */
  380. int Extend(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  381. register double *filt, *result;
  382. register int x_dim;
  383. int y_dim, x_pos, y_pos, r_or_e;
  384. {
  385. int filt_sz = x_dim*y_dim;
  386. register int y_filt,x_filt, y_res,x_res, y_tmp, x_tmp;
  387. register int x_base = (x_pos>0)?(x_dim-1):0;
  388. register int y_base = x_dim * ((y_pos>0)?(y_dim-1):0);
  389. int x_overhang = (x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0);
  390. int y_overhang = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  391. int mx_pos = (x_pos<0)?(x_dim/2):((x_dim-1)/2);
  392. int my_pos = x_dim * ((y_pos<0)?(y_dim/2):((y_dim-1)/2));
  393. int i;
  394. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  395. /* Modeled on reflect1() */
  396. if (r_or_e IS REDUCE)
  397. for (y_filt=0, y_res=y_overhang;
  398. y_filt<filt_sz;
  399. y_filt+=x_dim, y_res+=x_dim)
  400. if ((y_res>=0) AND (y_res<filt_sz))
  401. for (x_filt=y_filt, x_res=x_overhang;
  402. x_filt<y_filt+x_dim;
  403. x_filt++, x_res++)
  404. if ((x_res>=0) AND (x_res<x_dim))
  405. result[y_res+x_res] += filt[x_filt];
  406. else
  407. {
  408. result[y_res+ABS(x_base-ABS(x_res-x_base))] -= filt[x_filt];
  409. result[y_res+x_base] += 2*filt[x_filt];
  410. }
  411. else
  412. for (x_filt=y_filt, x_res=x_overhang;
  413. x_filt<y_filt+x_dim;
  414. x_filt++, x_res++)
  415. if ((x_res>=0) AND (x_res<x_dim))
  416. {
  417. result[ABS(y_base-ABS(y_res-y_base))+x_res] -= filt[x_filt];
  418. result[y_base+x_res] += 2*filt[x_filt];
  419. }
  420. else
  421. {
  422. result[ABS(y_base-ABS(y_res-y_base))+ABS(x_base-ABS(x_res-x_base))]
  423. -= filt[x_filt];
  424. result[y_base+x_base] += 2*filt[x_filt];
  425. }
  426. else { /* r_or_e ISNT REDUCE */
  427. y_overhang = ABS(y_overhang);
  428. x_overhang = ABS(x_overhang);
  429. for (y_res=y_base, y_filt = y_base-y_overhang;
  430. y_filt > y_base-filt_sz;
  431. y_filt-=x_dim, y_res-=x_dim)
  432. {
  433. for (x_res=x_base, x_filt=x_base-x_overhang;
  434. x_filt > x_base-x_dim;
  435. x_res--, x_filt--)
  436. result[ABS(y_res)+ABS(x_res)] += filt[ABS(y_filt)+ABS(x_filt)];
  437. if (x_pos ISNT 0)
  438. if (x_overhang ISNT mx_pos)
  439. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang;
  440. x_filt > x_base-x_dim;
  441. x_res--, x_filt--)
  442. result[ABS(y_res)+ABS(x_res)] -= filt[ABS(y_filt)+ABS(x_filt)];
  443. else /* x_overhang IS mx_pos */
  444. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang-1;
  445. x_filt > x_base-x_dim;
  446. x_res--, x_filt--)
  447. for (x_tmp=x_filt; x_tmp > x_base-x_dim; x_tmp--)
  448. result[ABS(y_res)+ABS(x_res)] += 2*filt[ABS(y_filt)+ABS(x_tmp)];
  449. }
  450. if (y_pos ISNT 0)
  451. if (y_overhang ISNT my_pos)
  452. for (y_res=y_base, y_filt = y_base-2*my_pos+y_overhang;
  453. y_filt > y_base-filt_sz;
  454. y_filt-=x_dim, y_res-=x_dim)
  455. {
  456. for (x_res=x_base, x_filt=x_base-x_overhang;
  457. x_filt > x_base-x_dim;
  458. x_res--, x_filt--)
  459. result[ABS(y_res)+ABS(x_res)] -= filt[ABS(y_filt)+ABS(x_filt)];
  460. if ((x_pos ISNT 0) AND (x_overhang ISNT mx_pos))
  461. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang;
  462. x_filt > x_base-x_dim;
  463. x_res--, x_filt--)
  464. result[ABS(y_res)+ABS(x_res)] -= filt[ABS(y_filt)+ABS(x_filt)];
  465. }
  466. else /* y_overhang IS my_pos */
  467. for (y_res=y_base, y_filt = y_base-2*my_pos+y_overhang-x_dim;
  468. y_filt > y_base-filt_sz;
  469. y_res-=x_dim, y_filt-=x_dim)
  470. for (y_tmp=y_filt; y_tmp > y_base-filt_sz; y_tmp-=x_dim)
  471. {
  472. for (x_res=x_base, x_filt=x_base-x_overhang;
  473. x_filt > x_base-x_dim;
  474. x_res--, x_filt--)
  475. result[ABS(y_res)+ABS(x_res)] += 2*filt[ABS(y_tmp)+ABS(x_filt)];
  476. if ((x_pos ISNT 0) AND (x_overhang IS mx_pos))
  477. for (x_res=x_base, x_filt=x_base-2*mx_pos+x_overhang-1;
  478. x_filt > x_base-x_dim;
  479. x_res--, x_filt--)
  480. for (x_tmp=x_filt; x_tmp > x_base-x_dim; x_tmp--)
  481. result[ABS(y_res)+ABS(x_res)] += 2*filt[ABS(y_tmp)+ABS(x_tmp)];
  482. }
  483. } /* r_or_e ISNT REDUCE */
  484. return(0);
  485. }
  486. /* --------------------------------------------------------------------
  487. predict() - Simple prediction. Like zero, but multiplies the result
  488. by the reciprocal of the percentage of filter being used. (i.e. if
  489. 50% of the filter is hanging over the edge of the image, multiply the
  490. taps being used by 2). */
  491. int predict(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  492. register double *filt, *result;
  493. register int x_dim;
  494. int y_dim, x_pos, y_pos, r_or_e;
  495. {
  496. register int y_filt,x_filt, y_res,x_res;
  497. register double taps_used = 0.0; /* int *** */
  498. register double fraction = 0.0;
  499. int filt_sz = x_dim*y_dim;
  500. int x_start = ((x_pos>0)?(x_pos-1):((x_pos<0)?(x_pos+1):0));
  501. int y_start = x_dim * ((y_pos>0)?(y_pos-1):((y_pos<0)?(y_pos+1):0));
  502. int i;
  503. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  504. for (y_filt=0, y_res=y_start;
  505. y_filt<filt_sz;
  506. y_filt+=x_dim, y_res+=x_dim)
  507. if ((y_res >= 0) AND (y_res < filt_sz))
  508. for (x_filt=y_filt, x_res=x_start;
  509. x_filt<y_filt+x_dim;
  510. x_filt++, x_res++)
  511. if ((x_res >= 0) AND (x_res < x_dim))
  512. {
  513. result[y_res+x_res] = filt[x_filt];
  514. taps_used += ABS(filt[x_filt]);
  515. }
  516. if (r_or_e IS REDUCE)
  517. {
  518. /* fraction = ( (double) filt_sz ) / ( (double) taps_used ); */
  519. for (i=0; i<filt_sz; i++) fraction += ABS(filt[i]);
  520. fraction = ( fraction / taps_used );
  521. for (i=0; i<filt_sz; i++) result[i] *= fraction;
  522. }
  523. return(0);
  524. }
  525. /* --------------------------------------------------------------------
  526. Reflect, multiplying tap of filter which is at the edge of the image
  527. by root 2. This maintains orthogonality of odd-length linear-phase
  528. QMF filters, but it is not useful for most applications, since it
  529. alters the DC level. */
  530. int ereflect(filt,x_dim,y_dim,x_pos,y_pos,result,r_or_e)
  531. register double *filt, *result;
  532. register int x_dim;
  533. int y_dim, x_pos, y_pos, r_or_e;
  534. {
  535. register int y_filt,x_filt, y_res,x_res;
  536. register int x_base = (x_pos>0)?(x_dim-1):0;
  537. register int y_base = x_dim * ((y_pos>0)?(y_dim-1):0);
  538. int filt_sz = x_dim*y_dim;
  539. int x_overhang = (x_pos>1)?(x_pos-x_dim):((x_pos<-1)?(x_pos+1):0);
  540. int y_overhang = x_dim * ( (y_pos>1)?(y_pos-y_dim):((y_pos<-1)?(y_pos+1):0) );
  541. int i;
  542. double norm,onorm;
  543. for (i=0; i<filt_sz; i++) result[i] = 0.0;
  544. /* reflect at boundary */
  545. for (y_filt=0, y_res=y_overhang;
  546. y_filt<filt_sz;
  547. y_filt+=x_dim, y_res+=x_dim)
  548. for (x_filt=y_filt, x_res=x_overhang;
  549. x_filt<y_filt+x_dim;
  550. x_filt++, x_res++)
  551. result[ABS(y_base-ABS(y_res)) + ABS(x_base-ABS(x_res))]
  552. += filt[x_filt];
  553. /* now multiply edge by root 2 */
  554. if (x_pos ISNT 0)
  555. for (y_filt=x_base; y_filt<filt_sz; y_filt+=x_dim)
  556. result[y_filt] *= ROOT2;
  557. if (y_pos ISNT 0)
  558. for (x_filt=y_base; x_filt<y_base+x_dim; x_filt++)
  559. result[x_filt] *= ROOT2;
  560. /* now normalize to norm of original filter */
  561. for (norm=0.0,i=0; i<filt_sz; i++)
  562. norm += (result[i]*result[i]);
  563. norm=sqrt(norm);
  564. for (onorm=0.0,i=0; i<filt_sz; i++)
  565. onorm += (filt[i]*filt[i]);
  566. onorm = sqrt(onorm);
  567. norm = norm/onorm;
  568. for (i=0; i<filt_sz; i++)
  569. result[i] /= norm;
  570. return(0);
  571. }
  572. /* ------- printout stuff for testing ------------------------------
  573. printf("Xpos: %d, Ypos: %d", x_pos, y_pos);
  574. for (y_filt=0; y_filt<y_dim; y_filt++)
  575. {
  576. printf("\n");
  577. for (x_filt=0; x_filt<x_dim; x_filt++)
  578. printf("%6.1f", result[y_filt*x_dim+x_filt]);
  579. }
  580. printf("\n");
  581. */
  582. /* Local Variables: */
  583. /* buffer-read-only: t */
  584. /* End: */