aboutsummaryrefslogtreecommitdiff
path: root/pkg/plot/crtpict/tweakndc.x
blob: 1a8f674b5333b8826165be999f0166a1117f4381 (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
63
64
65
66
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# CRT_TWEAK_NDC -- Alter the input ndc endpoints so that the ratio
# of device_pixels to image_pixels is an integer.  This procedure insures
# an integer replication (or decimation) factor.  For replication, 
# ndevice_pixels_spanned / nimage_values_output = an_integer.  For
# decimation, ndevice_pixels_spanned / nimage_values_output = 1 / an_integer.
# The NDC coordinates returned also represent integer device pixels.

procedure crt_tweak_ndc (nvalues, ndc_start, ndc_end, device_res)

int	nvalues			# Number of image values to output
real	ndc_start, ndc_end	# NDC endpoints - changed on output
int	device_res		# Full device resolution

int	ndevice_elements, first_device_element, last_device_element, int_extra
int	dev_pixel_1, dev_pixel_2, desired_dev_elements, desired_inverse
real	ndc_extra, extra, real_extra
double	gki_tweak

begin
	gki_tweak = double (32767) / double (32768)
	if (int (ndc_end) == 1)
	    ndc_end = gki_tweak
	first_device_element = ndc_start * device_res 
	last_device_element  = ndc_end * device_res
	ndevice_elements = (last_device_element - first_device_element) + 1

	if (mod (ndevice_elements, nvalues) != 0) {
	    # Calculate amount to be altered
	    real_extra = real (ndevice_elements) / real (nvalues)
	    if (real_extra > 1.0) {
	        # Tweak to get an integer replication factor
	        int_extra  = ndevice_elements / nvalues
	        extra = real ((real_extra - int_extra) * nvalues)
	        ndc_extra = extra / device_res
	        ndc_start = ndc_start + (ndc_extra / 2.0)
	        ndc_end   = ndc_end   - (ndc_extra / 2.0)
	    } else {
		# Tweak to get an integer decimation factor
		real_extra = real (nvalues) / real (ndevice_elements)
		desired_inverse = int (real_extra) + 1
		desired_dev_elements = nvalues / desired_inverse
		extra = desired_dev_elements - ndevice_elements
		ndc_extra = real (extra) / real (device_res)
		ndc_start = ndc_start - (ndc_extra / 2.0)
		ndc_end   = ndc_end   + (ndc_extra / 2.0)
	    }
	}

	# Now have ndc coordinates of starting and ending pixel such
	# that the replication or decimation factor is
	# an integer.  Now insure that the ndc coordinates refer to 
	# integer device pixels  so that truncation later in the
	# processing doesn't alter this replication factor.  In what
	# follows, note that dev_pixel_1 and dev_pixel_2
	# are 0-based; dev_pixel_1 is the first pixel to be filled, and 
	# dev_pixel_2 is the first pixel NOT to be filled, in accordance
	# with Richard's notes.

	dev_pixel_1 = ndc_start * device_res
	dev_pixel_2 = (ndc_end  * device_res) + 1

	ndc_start = real (dev_pixel_1) / real (device_res) / gki_tweak
	ndc_end = real (dev_pixel_2) / real (device_res) / gki_tweak
end