;+
;
; FUNCTION: fftrebin
; PURPOSE:
; Rebins an image by adding zeros in between the FFT components as
; necessary.
; NOTES:
; Right now it only upsamples, so don't try to use this
; for shrinking images. But it does seem like it does a really nice job
; at upsampling things.
;
; INPUTS:
; img an image
; scale a scale factor. Can be any real number greater than 1.
; KEYWORDS:
; OUTPUTS:
; the enlarged version of the image
;
; HISTORY:
; 2001-08-03 - Marshall Perrin, based on an idea by JRG, and building
; on code from fouriershift.
;
;-
FUNCTION fftrebin,img,scale,nomask=nomask
sz=size(img)
; mask the edges of the image to prevent sharp discontinuities
; from causing ringing and other edge effects.
edgemask=fltarr(sz[1],sz[2])+1
edgemask[0,*]=0.
edgemask[sz[1]-1,*]=.0
edgemask[1,*]=.1
edgemask[sz[1]-2,*]=.1
edgemask[2,*]=.3
edgemask[sz[1]-3,*]=.3
edgemask[3,*]=.7
edgemask[sz[1]-4,*]=.7
edgemask[4,*]=.9
edgemask[sz[1]-5,*]=.9
edgemask[*,0]=0.
edgemask[*,sz[2]-1]=.0
edgemask[*,1]=edgemask[*,1]*.1
edgemask[*,sz[2]-2]=edgemask[*,sz[2]-2]*.1
edgemask[*,2]=edgemask[*,2]*.3
edgemask[*,sz[2]-3]=edgemask[*,sz[2]-3]*.3
edgemask[*,3]=edgemask[*,3]*.7
edgemask[*,sz[2]-4]=edgemask[*,sz[2]-4]*.7
edgemask[*,4]=edgemask[*,4]*.9
edgemask[*,sz[2]-5]=edgemask[*,sz[2]-5]*.9
if keyword_set(nomask) then edgemask = edgemask*0+1.0
m=median(img) ; use the median not the mean because it's
; better at grabbing the sky rather than the star
f=fft((img-m)*edgemask) ; remove the mean before FFTing, and filter the edges
; split the actual fft data into four quarters. Spread them apart and
; stick a mess of zeros in between.
z=scale
bigf=complexarr(z*sz[1],z*sz[2])
a=sz[1]/2
b=floor(sz[1]*z-sz[1]/2.0)
c=ceil(sz[2]/2.0)
d=sz[2]*z-sz[2]/2
bigf[0:a-1,0:c-1] = f[0:a-1,0:c-1]
bigf[0:a-1,d:*] = f[0:a-1,c:*]
bigf[b:*,0:c-1] = f[a:*,0:c-1]
bigf[b:*,d:*] = f[a:*,c:*]
;stop
return,float(fft(bigf,/inverse))+m ; don't forget to add mean back in
end