38 #include "vircam_utils.h"
39 #include "vircam_filt.h"
41 static void docols(
float *data,
unsigned char *bpm,
int nx,
int ny,
42 int filter,
int stat);
43 static void dorows(
float *data,
unsigned char *bpm,
int nx,
int ny,
44 int filter,
int stat);
45 static void wraparound(
float *data,
unsigned char *bpm,
int npts,
int nfilt,
46 int whichstat,
float **ybuf,
unsigned char **ybbuf,
48 static void medavg(
float *array,
unsigned char *bpm,
int *ipoint,
int npix,
49 int whichstat,
int newl,
float *outval,
50 unsigned char *outbp);
51 static void quickie(
float *array,
unsigned char *iarray,
int *iarray2,
53 static void sortm(
float *a1,
unsigned char *a2,
int *a3,
int n);
54 static void plugholes(
float *data,
unsigned char *bpm,
int nx);
65 static short int nextc;
66 static short int lastc;
122 extern void vircam_bfilt(
float *data,
unsigned char *bpm,
int nx,
int ny,
123 int filt,
int stat,
int axis) {
129 dorows(data,bpm,nx,ny,filt,stat);
130 docols(data,bpm,nx,ny,filt,stat);
132 docols(data,bpm,nx,ny,filt,stat);
133 dorows(data,bpm,nx,ny,filt,stat);
170 static void docols(
float *data,
unsigned char *bpm,
int nx,
int ny,
171 int filter,
int stat) {
173 unsigned char *goodval,*bbuf;
184 dbuf = cpl_malloc(nbuf*
sizeof(*dbuf));
185 bbuf = cpl_malloc(nbuf*
sizeof(*bbuf));
186 goodval = cpl_malloc(ny*
sizeof(*goodval));
190 for (k = 0; k < nx; k++) {
191 memset((
char *)goodval,0,ny);
192 for (j = 0; j < ny; j++) {
194 dbuf[j] = data[indx];
201 plugholes(dbuf,goodval,ny);
205 for (j = 0; j < ny; j++) {
207 data[indx] = dbuf[j];
250 static void dorows(
float *data,
unsigned char *bpm,
int nx,
int ny,
251 int filter,
int stat) {
253 unsigned char *goodval,*bbuf;
264 dbuf = cpl_malloc(nbuf*
sizeof(*dbuf));
265 bbuf = cpl_malloc(nbuf*
sizeof(*bbuf));
266 goodval = cpl_malloc(nx*
sizeof(*goodval));
270 for (k = 0; k < ny; k++) {
271 memset((
char *)goodval,0,nx);
272 for (j = 0; j < nx; j++) {
274 dbuf[j] = data[indx];
281 plugholes(dbuf,goodval,nx);
285 for (j = 0; j < nx; j++) {
287 data[indx] = dbuf[j];
331 unsigned char *goodval,
int npts,
int nfilt,
333 int nbuf,jl,jh,j,*ipoint,ifree,i;
334 unsigned char *ybbuf,*barray,bval;
335 float *ybuf,*darray,val;
339 if ((nfilt/2)*2 == nfilt)
344 wraparound(data,bpm,npts,nfilt,whichstat,&ybuf,&ybbuf,&nbuf);
348 darray = cpl_malloc(nfilt*
sizeof(*darray));
349 barray = cpl_malloc(nfilt*
sizeof(*barray));
350 ipoint = cpl_malloc(nfilt*
sizeof(*ipoint));
351 memmove((
char *)darray,(
char *)ybuf,nfilt*
sizeof(*ybuf));
352 memmove((
char *)barray,(
char *)ybbuf,nfilt*
sizeof(*ybbuf));
353 for (j = 0; j < nfilt; j++)
359 medavg(darray,barray,ipoint,nfilt,whichstat,-1,&val,&bval);
368 jh = nfilt + npts - 2;
369 for (j = jl; j <= jh; j++) {
370 for (i = 0; i < nfilt; i++) {
371 if (ipoint[i] == 0) {
373 ipoint[i] = nfilt - 1;
374 lastval = darray[ifree];
377 if (barray[ifree] == 0) {
381 darray[ifree] = ybuf[j];
382 barray[ifree] = ybbuf[j];
383 nextval = darray[ifree];
386 if (barray[ifree] == 0) {
393 medavg(darray,barray,ipoint,nfilt,whichstat,ifree,&val,&bval);
396 goodval[j-jl+1] = bval;
445 static void wraparound(
float *data,
unsigned char *bpm,
int npts,
int nfilt,
446 int whichstat,
float **ybuf,
unsigned char **ybbuf,
int *nbuf) {
448 float *darray,xmns,xmnf;
449 int i1,ilow,i,*ipoint;
450 unsigned char *barray,bxmns,bxmnf;
455 ilow = max(3,nfilt/4);
456 ilow = (ilow/2)*2 + 1;
460 darray = cpl_malloc(nfilt*
sizeof(*darray));
461 barray = cpl_malloc(nfilt*
sizeof(*barray));
462 ipoint = cpl_calloc(nfilt,
sizeof(*ipoint));
464 *ybuf = cpl_malloc(*nbuf*
sizeof(
float));
465 *ybbuf = cpl_malloc(*nbuf*
sizeof(
unsigned char));
469 memmove((
char *)darray,(
char *)data,ilow*
sizeof(*data));
470 memmove((
char *)barray,(
char *)bpm,ilow*
sizeof(*bpm));
471 medavg(darray,barray,ipoint,ilow,whichstat,-1,&xmns,&bxmns);
472 memmove((
char *)darray,(
char *)(data+npts-ilow),ilow*
sizeof(*data));
473 memmove((
char *)barray,(
char *)(bpm+npts-ilow),ilow*
sizeof(*bpm));
474 medavg(darray,barray,ipoint,ilow,whichstat,-1,&xmnf,&bxmnf);
475 for (i = 0; i < i1; i++) {
477 (*ybuf)[i] = 2.0*xmns - data[i1+ilow-i-1];
478 (*ybbuf)[i] = bpm[i1+ilow-i-1];
480 (*ybuf)[i] = data[i1+ilow-i-1];
484 (*ybuf)[npts+i1+i] = 2.0*xmnf - data[npts-i-ilow-1];
485 (*ybbuf)[npts+i1+i] = bpm[npts-i-ilow-1];
487 (*ybuf)[npts+i1+i] = data[npts-i-ilow-1];
488 (*ybbuf)[npts+i1+i] = 1;
494 memmove((
char *)(*ybuf+i1),data,npts*
sizeof(*data));
495 memmove((
char *)(*ybbuf+i1),bpm,npts*
sizeof(*bpm));
543 static void medavg(
float *array,
unsigned char *bpm,
int *ipoint,
int npix,
544 int whichstat,
int newl,
float *outval,
545 unsigned char *outbp) {
555 if (whichstat == MEDIANCALC) {
557 sortm(array,bpm,ipoint,npix);
559 quickie(array,bpm,ipoint,newl,npix);
563 buf = cpl_malloc(npix*
sizeof(*buf));
568 for (i = 0; i < npix; i++) {
580 }
else if (whichstat == MEANCALC) {
585 for (i = 0; i < npix; i++) {
594 sum += (nextw*nextval - lastw*lastval);
595 sumw += (nextw - lastw);
596 naver += (nextc - lastc);
606 if (whichstat == MEDIANCALC)
612 if (whichstat == MEDIANCALC) {
614 *outval = 0.5*(buf[(m/2)-1] + buf[m/2]);
618 }
else if (whichstat == MEANCALC)
650 static void quickie(
float *array,
unsigned char *iarray,
int *iarray2,
651 int testloc,
int narray) {
657 test = array[testloc];
658 it = iarray[testloc];
659 it2 = iarray2[testloc];
661 for (i = 0; i < narray; i++) {
662 if (i != testloc && test <= array[i]) {
669 if (j - 1 == testloc)
672 if (j - testloc < 0) {
674 for (i = 0; i < npt; i++) {
675 array[testloc-i] = array[testloc-i-1];
676 iarray[testloc-i] = iarray[testloc-i-1];
677 iarray2[testloc-i] = iarray2[testloc-i-1];
686 for (i = 0; i < npt; i++) {
687 array[testloc+i] = array[testloc+i+1];
688 iarray[testloc+i] = iarray[testloc+i+1];
689 iarray2[testloc+i] = iarray2[testloc+i+1];
722 static void sortm(
float *a1,
unsigned char *a2,
int *a3,
int n) {
723 int iii,ii,i,ifin,j,b3;
730 iii = min(n,(3*iii)/4 - 1);
735 for (ii = 0; ii < ifin; ii++) {
748 if (i < 0 || a1[i] <= b1)
781 static void plugholes(
float *data,
unsigned char *bpm,
int nx) {
782 int i,ifirst,ilast,i1,i2,j;
783 float nc,d1,d2,t1,t2,slope;
788 while (i < nx && bpm[i] != 0)
800 while (i >= 0 && bpm[i] != 0)
817 nc = (float)(i2 - i1 + 1);
820 for (j = i1+1; j <= i2-1; j++) {
821 t1 = 1.0 - (float)(j - i1)/nc;
823 data[j] = t1*d1 + t2*d2;
830 slope = data[ifirst+1] - data[ifirst];
831 for (j = 0; j < ifirst; j++)
832 data[j] = slope*(j - ifirst) + data[ifirst];
837 if (ilast < nx - 1) {
838 slope = data[ilast] - data[ilast-1];
839 for (j = ilast; j < nx; j++)
840 data[j] = slope*(j - ilast) + data[ilast];