convolve.c
Go to the documentation of this file.
1 /* cc -c -Bstatic -Dsun4 -I../c convolve.c */
2 
3 #include "../c/eus.h"
4 
5 #pragma init (init_object_module)
7 static void init_object_module()
8  { add_module_initializer("convolve", convolve);}
9 
10 #define is2Dstring(a) (isarray(a) && \
11  ((a)->c.ary.rank==makeint(2)) && \
12  isstring((a)->c.ary.entity))
13 pointer CONVOLVE3(ctx,n,argv)
14 /* (convolve3 convolution_mask divisor image result) */
15 context *ctx;
16 int n;
17 pointer argv[];
18 {
19  pointer convp, imgp, resultp;
20  char *conv;
21  unsigned char *img, *result, *g;
22  register int x,y,width,height, v, diviser;
23 
24  ckarg(4);
25  convp= argv[0]; imgp=argv[2]; resultp= argv[3];
26  diviser=ckintval(argv[1]);
27  if (!is2Dstring(convp) || !is2Dstring(imgp) || !is2Dstring(resultp))
29  conv=(char *)(convp->c.ary.entity->c.str.chars);
30  img=imgp->c.ary.entity->c.str.chars;
31  result=resultp->c.ary.entity->c.str.chars;
32  width=intval(imgp->c.ary.dim[1]); height=intval(imgp->c.ary.dim[0]);
33 
34  for (y=1; y<=height-1; y++) {
35  for (x=1; x<=width-1; x++) {
36  g= &img[width*y+x];
37  v= g[-width-1]*conv[0] + g[-width]* conv[1] + g[-width+1]*conv[2] +
38  g[-1]*conv[3] + g[0]* conv[4] + g[1]*conv[5] +
39  g[width-1]*conv[6] + g[width]* conv[7] + g[width+1]*conv[8];
40  result[width*y+x]=v/diviser;}
41  }
42  return(resultp);}
43 
44 pointer LOOK_UP(ctx,n,argv)
45 /* foreach element indexed i, result[i]=table[src[i]] */
46 context *ctx;
47 int n;
48 pointer argv[];
49 { pointer imgp, tablep, resultp;
50  register unsigned char *img, *ctable, *result;
51  register long size, i;
52  register eusinteger_t *itable;
53 
54  ckarg(3);
55  imgp=argv[0]; resultp= argv[1]; tablep=argv[2];
56  if (!isstring(imgp) || !isvector(tablep) || !isstring(resultp))
58  if (elmtypeof(imgp)==ELM_FOREIGN)
59  img=(unsigned char *)imgp->c.ivec.iv[0];
60  else img=imgp->c.str.chars;
61  if (elmtypeof(resultp)==ELM_FOREIGN)
62  result=(unsigned char *)resultp->c.ivec.iv[0];
63  else result=resultp->c.str.chars;
64  size=vecsize(imgp);
65  if (elmtypeof(tablep)==ELM_INT) {
66  itable=tablep->c.ivec.iv;
67  for (i=0; i<size; i++) result[i]=itable[img[i]];}
68  else if (elmtypeof(tablep)==ELM_CHAR || elmtypeof(tablep)==ELM_BYTE) {
69  ctable=tablep->c.str.chars;
70  for (i=0; i<size; i++) result[i]=ctable[img[i]];}
71  return(resultp);}
72 
74 context *ctx;
75 int n;
76 pointer argv[];
77 { pointer imgp, half_imgp;
78  register unsigned char *img, *half_img;
79  register int i, j, base, half_base;
80  int width, height;
81  register int half_width, half_height;
82 
83  ckarg(2);
84  imgp=argv[0]; half_imgp=argv[1];
85  if (!is2Dstring(imgp) || !is2Dstring(half_imgp)) error(E_NOARRAY);
86  width=intval(imgp->c.ary.dim[1]);
87  height=intval(imgp->c.ary.dim[0]);
88  half_width=width/2; half_height=height/2;
89 
90  img= imgp->c.ary.entity->c.str.chars;
91  half_img= half_imgp->c.ary.entity->c.str.chars;
92 
93  for (j=0; j<half_height; j++) {
94  base= width*j*2; half_base=half_width*j;
95  for (i=0; i<half_width; i++)
96  half_img[half_base+i]=
97  (unsigned long)(img[base+i*2]+img[base+i*2+1]+
98  img[base+width+i*2]+img[base+i*2+width+1])/4; }
99  return(half_imgp);}
100 
102 context *ctx;
103 int n;
104 pointer argv[];
105 { pointer imgp, double_imgp;
106  register unsigned char *img, *double_img;
107  register int i, j, base, double_base;
108  int width, height;
109  register int double_width, double_height;
110 
111  ckarg(2);
112  imgp=argv[0]; double_imgp=argv[1];
113  if (!is2Dstring(imgp) || !is2Dstring(double_imgp)) error(E_NOARRAY);
114  width=intval(imgp->c.ary.dim[1]);
115  height=intval(imgp->c.ary.dim[0]);
116  double_width=width*2; double_height=height*2;
117 
118  img= imgp->c.ary.entity->c.str.chars;
119  double_img= double_imgp->c.ary.entity->c.str.chars;
120 
121  for (j=0; j<height; j++) {
122  base= width*j; double_base=double_width*j*2;
123  for (i=0; i<width; i++)
124  double_img[double_base+i*2]=
125  double_img[double_base+i*2+1]=
126  double_img[double_base+double_width+i*2]=
127  double_img[double_base+double_width+i*2+1]=
128  (unsigned long)(img[base+i]);
129  }
130  return(double_imgp);}
131 
132 /* (median img1 grid scale imgr) */
133 static int median_img(img, width, height, x, y, size)
134 unsigned char *img;
135 register int width, height, x, y, size;
136 { register int med, p, q, r;
137  register int i, j, k, v, cindex;
138  unsigned char seq[100], up[100], down[100];
139 
140  cindex=size*size/2;
141  k=0;
142  for (j=0; j<size; j++)
143  for (i=0; i<size; i++)
144  seq[k++]= img[(j+y)*width+(x+i)];
145 
146  find_med:
147 
148  med=seq[0];
149  p=q=r=0;
150 
151  for (i=0; i<k; i++)
152  v=seq[i];
153  if (v==med) r++;
154  else if (v>med) up[p++]=v;
155  else down[q++]=v;
156 
157  if (q<=cindex)
158  if (q+r>=cindex) return(med);
159  else { /* find (p-cindex)th in up */
160  for (i=0; i<p; i++) seq[i]=up[i];
161  cindex=cindex-(q+r); k=p;
162  goto find_med; }
163  else {
164  for (i=0; i<q; i++) seq[i]=down[i];
165  k=q;
166  goto find_med; }
167  }
168 
170 context *ctx;
171 int n;
172 pointer argv[];
173 { pointer img1, img2;
174  register unsigned char *img1p, *img2p;
175  register int i,j;
176  int width, height, swidth, sheight;
177  int hgrid, grid_size, hscale, scale;
178 
179  ckarg2(2,4);
180  img1=argv[0];
181  width=intval(img1->c.ary.dim[1]);
182  height=intval(img1->c.ary.dim[0]);
183  grid_size=ckintval(argv[1]);
184  scale = ckintval(argv[2]);
185  hgrid=grid_size/2;
186  hscale=scale/2;
187  img2=argv[3];
188  if (!is2Dstring(img1) || !is2Dstring(img2)) error(E_NOARRAY);
189  img1p=img1->c.ary.entity->c.str.chars;
190  img2p=img2->c.ary.entity->c.str.chars;
191  swidth=width/scale;
192  sheight=height/scale;
193  for (j=0; j<sheight-1; j++) {
194  for (i=0; i<swidth-1; i++) {
195  img2p[j*swidth+i]=
196  median_img(img1p, width, height, scale*i, scale*j, grid_size);
197  }}
198  return(img2);}
199 
200 pointer convolve(ctx,n,argv)
201 context *ctx;
202 int n;
203 pointer argv[];
204 { pointer mod=argv[0];
205 
206 /* printf("convolve is being initialized. mod=%x\n",mod); */
207  defun(ctx,"CONVOLVE3",mod,CONVOLVE3,NULL);
208  defun(ctx,"LOOK-UP",mod,LOOK_UP,NULL);
209  defun(ctx,"HALVE-IMAGE",mod,HALVE_IMAGE,NULL);
210  defun(ctx,"DOUBLE-IMAGE",mod,DOUBLE_IMAGE,NULL);
211  defun(ctx,"MEDIAN-IMAGE",mod,MEDIAN_IMAGE,NULL);
212 }
213 
if
if(n==1)
Definition: unixcall.c:492
defun
defun("ADR_TO_STRING", mod, ADR_TO_STRING)
context
Definition: eus.h:524
intval
#define intval(p)
Definition: sfttest.c:1
add_module_initializer
void add_module_initializer(char *, pointer(*)())
Definition: loadelf.c:86
g
static int(* g)()
Definition: cfunc.c:9
E_NOSTRING
@ E_NOSTRING
Definition: eus.h:957
cell::cellunion::ary
struct arrayheader ary
Definition: eus.h:413
cell::cellunion::ivec
struct intvector ivec
Definition: eus.h:416
string::chars
byte chars[1]
Definition: eus.h:212
cell::c
union cell::cellunion c
init_object_module
static void init_object_module()
Definition: convolve.c:7
NULL
#define NULL
Definition: transargv.c:8
convolve
pointer convolve()
HALVE_IMAGE
pointer HALVE_IMAGE(context *ctx, int n, argv)
Definition: convolve.c:73
error
pointer error(enum errorcode ec,...) pointer error(va_alist) va_dcl
Definition: eus.c:297
E_NOARRAY
@ E_NOARRAY
Definition: eus.h:995
cell
Definition: eus.h:381
eusinteger_t
long eusinteger_t
Definition: eus.h:19
DOUBLE_IMAGE
pointer DOUBLE_IMAGE(context *ctx, int n, argv)
Definition: convolve.c:101
arrayheader::entity
pointer entity
Definition: eus.h:313
MEDIAN_IMAGE
pointer MEDIAN_IMAGE(context *ctx, int n, argv)
Definition: convolve.c:169
is2Dstring
#define is2Dstring(a)
Definition: convolve.c:10
arrayheader::dim
pointer dim[ARRAYRANKLIMIT]
Definition: eus.h:317
intvector::iv
eusinteger_t iv[1]
Definition: eus.h:305
CONVOLVE3
pointer CONVOLVE3(context *ctx, int n, argv)
Definition: convolve.c:13
n
GLfloat n[6][3]
Definition: cube.c:15
v
GLfloat v[8][3]
Definition: cube.c:21
median_img
static int median_img(unsigned char *img, int width, int height, int x, int y, int size)
Definition: convolve.c:133
LOOK_UP
pointer LOOK_UP(context *ctx, int n, argv)
Definition: convolve.c:44
ckarg
ckarg(2)
cell::cellunion::str
struct string str
Definition: eus.h:402


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43