aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/tv/display/findz.x
blob: e1f0f73ed56ef7a3b4aa75674b972cddf42e92b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<imhdr.h>
include	"iis.h"

# FINDZ -- Estimate the range of greylevels Z1 to Z2 containing a specified
# fraction of the greylevels in the image.  The technique is to sample the
# image at some interval, computing the values of the greylevels a fixed
# distance either side of the median.  Since it is not necessary to compute
# the full histogram we do not need to know the image zmin, zmax in advance.
# Works for images of any dimensionality, size, or datatype.

procedure findz (im, z1, z2, zfrac, maxcols, nsample_lines)

pointer	im
real	z1, z2, zfrac
int	maxcols, nsample_lines

real	rmin, rmax
real	frac
int	imin, imax, ncols, nlines
int	i, n, step, sample_size, imlines

pointer	sp, buf
pointer	imgl2r()
include	"iis.com"

begin
	call smark (sp)
	call salloc (buf, ncols, TY_REAL)

	ncols  = IM_LEN(im,1)
	nlines = IM_LEN(im,2)

	# Try to include a constant number of pixels in the sample
	# regardless of the image size.  The entire image is used if we
	# have a small image, and at least sample_lines lines are read
	# if we have a large image.

	sample_size = iis_ydim * nsample_lines
	imlines = min(nlines, max(nsample_lines, sample_size / ncols))
	step = nlines / (imlines + 1)

	frac = (1.0 - zfrac) / 2.
	imin = frac * (ncols - 1)
	imax = (1.0 - frac) * (ncols - 1)
	rmin = 0.0
	rmax = 0.0
	n = 0

	do i = 1 + step, nlines, max (1, step) {
	    call asrtr (Memr[imgl2r (im, i)], Memr[buf], ncols)
	    rmin = rmin + Memr[buf + imin]
	    rmax = rmax + Memr[buf + imax]
	    n = n + 1
	}

	z1 = rmin / n
	z2 = rmax / n

	call sfree (sp)
end