;i=0
;fls=findfile('corregions/*.fits')
;
;cor_im=readfits(fls(i),/silent)
;dim=size(cor_im,/dim)
;
;cor_bin=contig_pixels(cor_im,thresh=50,contig=50,lim_wr=fix( ( dim(0)>dim(1))/3.),$
;    centxy=[fix(dim(0)/2.),fix(dim(1)/2.)],/quiet)
;
;cor_cont=contig_pixels(cor_im,thresh=50,contig=50,$
;    /change_lim_wr,/quiet)
;
;cor_bin=abs(cor_im)
;cor_bin(where(cor_bin lt 50))=0
;cor_bin(where(cor_bin gt 0))=1
;
;
;
;
;
PRO calc_pixel_indices, bin, cont, filled,quiet=quiet
;catch blank images
IF ((min(bin) eq 0 AND max(bin) eq 0) OR (min(cont) eq 0 AND max(cont) eq 0)) THEN BEGIN
    filled = cont
    filled(*,*)=0
    GOTO, no_image
ENDIF
quiet=KEYWORD_SET(quiet)
sz=size(bin,/dim)
filled1=cont
filled2=cont
printxy,cont,where(cont NE 0),xpix,ypix
;for each row
FOR y=0,sz(1)- 1 DO BEGIN
    IF ((where(ypix eq y))(0) eq -1) THEN GOTO, skip_this_row
    this_row_cont_x= [0,xpix(where(ypix eq y))]
    num_this_row_cont_x=N_ELEMENTS(this_row_cont_x)
    ;pix_cont=where(cont(*,y) eq 1)
    ;if pix_cont(0) eq -1 THEN GOTO, skip_this_row
    ;STOP
    ;for each pixel in this row
    ;FOR x=0,sz(0)-1 DO BEGIN
    xindex=0
    ;STOP
    WHILE xindex LE num_this_row_cont_x-1 DO BEGIN
    	x=this_row_cont_x(xindex)
	IF x eq sz(0)-1 THEN BEGIN
	    filled1(x,y)=2
	    x=sz(0)-2
	ENDIF
	 IF NOT(quiet) THEN print,num_this_row_cont_x-1,xindex,x
	IF cont(x+1,y) eq 1 THEN BEGIN
	    IF NOT(quiet) THEN print,x,y,'  still on contour - not filling'
	    xindex=xindex+1
	    GOTO, next_x
	ENDIF
	;so the next pixel in 'cont' must be 0, hence a possible start of a contour
	IF bin(x+1,y) eq 0 THEN BEGIN
	    IF NOT(quiet) THEN print,x,y,' outside contoured area - not filling'
	    xindex=xindex+1
	    GOTO, next_x
	ENDIF
	;so the next pixel in 'bin' is a contoured pixel
	IF NOT(quiet) THEN print,x,y,'  found gap - starting fill'
	startl=x
	j=1
	endl=-1
	WHILE ((cont(x+j,y) NE 1) AND (x+j LE sz(0)-2))  DO BEGIN
	       endl=x+j
    	       j=j+1
	ENDWHILE
	IF endl LT startl OR endl GT sz(0) OR endl eq sz(0)-2 OR endl eq -1 THEN endl=sz(0)-1
	filled1(startl:endl,y)=2
	;account for parts inside the contour which should not be counted.
	filled1(startl:endl,y)=filled1(startl:endl,y)*bin(startl:endl,y)
	IF NOT(quiet) THEN print,'going to next x'
	xindex=xindex+1
	next_x:
	IF NOT(quiet) THEN plot_image,filled1,/as
    ENDWHILE
    skip_this_row:
ENDFOR
;now repeat for columns as a double check
;for each row
FOR x=0,sz(0)- 1 DO BEGIN
    ;IF x eq 145 THEN STOP
    IF ((where(xpix eq x))(0) eq -1) THEN GOTO, skip_this_col
    this_row_cont_y= [0,ypix(where(xpix eq x))]
    num_this_row_cont_y=N_ELEMENTS(this_row_cont_y)
    ;pix_cont=where(cont(*,y) eq 1)
    ;if pix_cont(0) eq -1 THEN GOTO, skip_this_row
    ;STOP
    ;for each pixel in this row
    ;FOR x=0,sz(0)-1 DO BEGIN
    yindex=0
    WHILE yindex LE num_this_row_cont_y-1 DO BEGIN
    	y=this_row_cont_y(yindex)
	IF y eq sz(1)-1 THEN BEGIN
	    filled2(x,y)=2
	    y=sz(1)-2
	ENDIF
	IF NOT(quiet) THEN print,num_this_row_cont_y-1,yindex,y
	IF cont(x,y+1) eq 1 THEN BEGIN
	    IF NOT(quiet) THEN print,x,y,'  still on contour - not filling'
	    yindex=yindex+1
	    GOTO, next_y
	ENDIF
	;so the next pixel in 'cont' must be 0, hence a possible start of a contour
	IF bin(x,y+1) eq 0 THEN BEGIN
	    IF NOT(quiet) THEN print,x,y,' outside contoured area - not filling'
	    yindex=yindex+1
	    GOTO, next_y
	ENDIF
	;so the next pixel in 'bin' is a contoured pixel
	IF NOT(quiet) THEN print,x,y,'  found gap - starting fill'
	startl=y
	j=1
	endl=-1
	WHILE ((cont(x,y+j) NE 1) AND (y+j LE sz(1)-2))  DO BEGIN
	       endl=y+j
    	       j=j+1
	ENDWHILE
	IF endl LT startl OR endl GT sz(1) OR endl eq sz(1)-2 OR endl eq -1 THEN endl=sz(1)-1
	filled2(x,startl:endl)=2
	;account for parts inside the contour which should not be counted.
	filled2(x,startl:endl)=filled2(x,startl:endl)*bin(x,startl:endl)
	IF NOT(quiet) THEN print,'going to next y'
	yindex=yindex+1
	next_y:
	IF NOT(quiet) THEN plot_image,filled2,/as
    ENDWHILE
    skip_this_col:
ENDFOR
filled=filled1*filled2*bin
;new 01/18/05
filled(where(filled NE 4)) = 0
no_image:
END