include # flnorm -- normalize an image # This task divides an image by the average of the middle section. # # Phil Hodge, 12-Mar-1990 Task created. procedure flnorm() pointer input # scratch for name of input image pointer output # scratch for name of output image int box1, box2 # size of region to use to compute average #-- pointer sp pointer iim, oim # pointer to imhdr structs for input, output images pointer ix, ox # pointers to input, output data real avg # average of the middle section of input image real sigma # sigma of middle section (ignored) real datamin # minimum pixel value real datamax # maximum pixel value int line # loop index int naxis1, naxis2 # size of image int ja, jb, ka, kb # limits of section to be averaged int k int scr bool inplace # true if input = output pointer immap(), imgs2r(), imgl2r(), impl2r() int clgeti() bool streq() begin call smark (sp) call salloc (input, SZ_FNAME, TY_CHAR) call salloc (output, SZ_FNAME, TY_CHAR) # Get input parameters. call clgstr ("input", Memc[input], SZ_FNAME) call clgstr ("output", Memc[output], SZ_FNAME) box1 = clgeti ("box1") box2 = clgeti ("box2") if (streq (Memc[input], Memc[output]) || (Memc[output] == EOS)) { inplace = true call mktemp ("flattemp", Memc[output], SZ_FNAME) } else { inplace = false } # Open input image. iim = immap (Memc[input], READ_ONLY, NULL) naxis1 = IM_LEN(iim,1) naxis2 = IM_LEN(iim,2) if ((box1 > naxis1) || (box2 > naxis2)) { call imunmap (iim) call error (1, "central box is too large for size of image") } # Create output image. oim = immap (Memc[output], NEW_COPY, iim) # Find limits of middle section. scr = (naxis1 - box1) / 2 ja = scr + 1 jb = ja + box1 - 1 scr = (naxis2 - box2) / 2 ka = scr + 1 kb = ka + box2 - 1 ix = imgs2r (iim, ja, jb, ka, kb) # get the data # Get the average of the section. call aavgr (Memr[ix], box1 * box2, avg, sigma) if (avg == 0.) { call imunmap (oim) call imunmap (iim) call imdelete (Memc[output]) call error (1, "the average is zero") } # Set invalid initial values for min & max. datamax = 0. datamin = 2. # Normalize and update datamin & datamax. do line = 1, naxis2 { ix = imgl2r (iim, line) ox = impl2r (oim, line) do k = 0, naxis1-1 { Memr[ox+k] = Memr[ix+k] / avg # normalize if (Memr[ox+k] > datamax) # update datamin & datamax datamax = Memr[ox+k] else if (Memr[ox+k] < datamin) datamin = Memr[ox+k] } } # Put datamin & datamax into header. call imputr (oim, "i_maxpixval", datamax) call imputr (oim, "i_minpixval", datamin) IM_LIMTIME(oim) = IM_MTIME(oim) + 1 call imunmap (oim) call imunmap (iim) if (inplace) { call imdelete (Memc[input]) call imrename (Memc[output], Memc[input]) } call sfree (sp) end