aboutsummaryrefslogtreecommitdiff
path: root/pkg/dataio/export/excmap.x
blob: 486813efb7f77faf17deba95f95930516b4107f0 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
include <lexnum.h>
include "export.h"


define	EX_COLORMAPS	"|aips0|blue|color|grayscale|greyscale|green|halley\
			 |heat|rainbow|red|staircase|standard|overlay|"

define	AIPS0		1		# builtin colormaps
define  BLUE		2
define  COLOR		3
define  GRAYSCALE	4
define  GREYSCALE	5
define  GREEN		6
define  HALLEY		7
define  HEAT		8
define  RAINBOW		9
define  RED		10
define  STAIRCASE	11
define  STANDARD	12
define  OVERLAY		13


# EX_READ_CMAP - Read a colormap into the colormap structure.  We assume the
# colormap is either a normalized CLT of RGB values between zero and one, or
# RGB integer values between 0 and 255.  The format of the file is three 
# values per line given as a red, green, and blue color.  If the first line
# contains a single number assume it's the number of colors.  A maximum of
# 256 colors will be read, if fewer values are read the remaining colors will 
# be filled with zeros.

procedure ex_read_cmap (ex, cmname)

pointer	ex					#i colormap pointer
char	cmname[ARB]				#i colormap file name

pointer	cmap
pointer	sp, line
real	r, g, b, scale
int	i, stat, fd, type, ncolors

int	open(), fscan(), nscan()
int	getline(), lexnum(), strdic()
errchk	open

define	rdmap_		99

begin
	# See if this is a builtin colormap request.
	if (strdic(cmname,cmname,SZ_LINE,EX_COLORMAPS) > 0) {
	    call ex_bltin_cmap (ex, cmname)
	    return
	}

	# Open the colormap filename.
	iferr (fd = open (cmname, READ_ONLY, TEXT_FILE))
	    call error (0, "Cannot open requested colormap file.")

	# Check the first line to see if it's the number of colors or a
	# CLT entry.
	stat = fscan (fd)
    	    call gargr (r)
    	    call gargr (g)
    	    call gargr (b)
	if (nscan() == 1) {
	    ncolors = r
	    goto rdmap_
	} else if (nscan() == 3) {
	    call seek (fd, BOF)
rdmap_	    call smark (sp)
	    call salloc (line, SZ_LINE, TY_CHAR)
	    stat = getline (fd, Memc[line])
	    i = 1
	    ncolors = 256
	    type = lexnum (Memc[line], i, stat)

	    if (type == LEX_REAL)
		scale = 255.0
	    else if (type == LEX_DECIMAL)
		scale = 1.0
	    else
	        call error (0, "Colormap file has an unknown format.")

	    call sfree (sp)
	} else
	    call error (1, "Colormap file has an unknown format.")

	# Read in a normalize colormap file.
	cmap = EX_CMAP(ex)
	for (i=1; fscan(fd) != EOF && i <= ncolors; i=i+1) {
    	    call gargr (r)
    	    call gargr (g)
    	    call gargr (b)

    	    CMAP(cmap,EX_RED,i)   = max (0, min (255, int (r * scale + 0.5)))
    	    CMAP(cmap,EX_GREEN,i) = max (0, min (255, int (g * scale + 0.5)))
    	    CMAP(cmap,EX_BLUE,i)  = max (0, min (255, int (b * scale + 0.5)))
	}
	ncolors = i
	EX_NCOLORS(ex) = ncolors
	
	# Close the file.
	call close (fd)
end


# EX_SCALE_CMAP - Scale the colormap with the requested brightness and
# contrast values.

procedure ex_scale_cmap (cmap, ncolors, brightness, contrast)

pointer	cmap				#i colormap pointer
int	ncolors				#i number of colors in map
real	brightness			#i brightness offset
real	contrast			#i contrast scale

pointer	sp, ctmp
int	i, c1, c2
short 	r, g, b
real	x, y, z, frac, slope, offset

begin
	call smark (sp)
	call salloc (ctmp, 3*CMAP_SIZE, TY_CHAR)
	call aclrc (Memc[ctmp], 3*CMAP_SIZE)

	slope = max (-7.0, min (7.0, contrast))
	offset = max (0.0, min (1.0, brightness))

     	# Compute the scaled colormap.
	do i = 1, ncolors {
             x = real (i) / real (ncolors)
             y = (x - offset) * slope + 0.5
 
             if (y <= 0.0) {
                 r = CMAP(cmap,EX_RED,  1)
                 g = CMAP(cmap,EX_GREEN,1)
                 b = CMAP(cmap,EX_BLUE, 1)
             } else if (y >= 1.0) {
                 r = CMAP(cmap,EX_RED,  ncolors)
                 g = CMAP(cmap,EX_GREEN,ncolors)
                 b = CMAP(cmap,EX_BLUE, ncolors)
             } else {
                 z = y * (ncolors - 1)
                 c1 = max (1, int (z))
                 c2 = min (ncolors-1, c1 + 1)
                 frac = z - c1
                 r = CMAP(cmap,EX_RED,c1) * (1.0 - frac) + 
		     CMAP(cmap,EX_RED,c2) * frac
                 g = CMAP(cmap,EX_GREEN,c1) * (1.0 - frac) + 
		     CMAP(cmap,EX_GREEN,c2) * frac
                 b = CMAP(cmap,EX_BLUE,c1) * (1.0 - frac) + 
		     CMAP(cmap,EX_BLUE,c2) * frac
             }
     
	     CMAP(ctmp,EX_RED,  i) = r
	     CMAP(ctmp,EX_GREEN,i) = g
	     CMAP(ctmp,EX_BLUE, i) = b
        }
	call amovc (Memc[ctmp], Memc[cmap], 3*CMAP_SIZE)

	call sfree (sp)
end


# EX_BLTIN_CMAP - Load a predefined colormap.

procedure ex_bltin_cmap (ex, cmname)

pointer	ex				#i task struct pointer
char	cmname[ARB]			#i colormap name

pointer	cmap
int	i, j, strdic()

include "cmaps.inc"

begin
	j = 1
	cmap = EX_CMAP(ex)
	EX_NCOLORS(ex) = CMAP_SIZE

	switch (strdic (cmname, cmname, SZ_LINE, EX_COLORMAPS)) {
	case AIPS0:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = aips0[j]
                CMAP(cmap,EX_GREEN,i) = aips0[j+1]
                CMAP(cmap,EX_BLUE,i)  = aips0[j+2]
		j = j + 3
	    }
	case BLUE:
	    call aclrs (Mems[cmap], 3*CMAP_SIZE)
	    do i = 1, 256
                CMAP(cmap,EX_BLUE,i)   = i - 1
	case COLOR:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = color[j]
                CMAP(cmap,EX_GREEN,i) = color[j+1]
                CMAP(cmap,EX_BLUE,i)  = color[j+2]
		j = j + 3
	    }
	case GRAYSCALE, GREYSCALE:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = i - 1
                CMAP(cmap,EX_GREEN,i) = i - 1
                CMAP(cmap,EX_BLUE,i)  = i - 1
	    }
	case GREEN:
	    call aclrs (Mems[cmap], 3*CMAP_SIZE)
	    do i = 1, 256
                CMAP(cmap,EX_GREEN,i)   = i - 1
	case HALLEY:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = halley[j]
                CMAP(cmap,EX_GREEN,i) = halley[j+1]
                CMAP(cmap,EX_BLUE,i)  = halley[j+2]
		j = j + 3
	    }
	case HEAT:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = heat[j]
                CMAP(cmap,EX_GREEN,i) = heat[j+1]
                CMAP(cmap,EX_BLUE,i)  = heat[j+2]
		j = j + 3
	    }
	case RAINBOW:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = rainbow[j]
                CMAP(cmap,EX_GREEN,i) = rainbow[j+1]
                CMAP(cmap,EX_BLUE,i)  = rainbow[j+2]
		j = j + 3
	    }
	case RED:
	    call aclrs (Mems[cmap], 3*CMAP_SIZE)
	    do i = 1, 256
                CMAP(cmap,EX_RED,i)   = i - 1
	case STAIRCASE:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = staircase[j]
                CMAP(cmap,EX_GREEN,i) = staircase[j+1]
                CMAP(cmap,EX_BLUE,i)  = staircase[j+2]
		j = j + 3
	    }
	case STANDARD:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = standard[j]
                CMAP(cmap,EX_GREEN,i) = standard[j+1]
                CMAP(cmap,EX_BLUE,i)  = standard[j+2]
		j = j + 3
	    }
	case OVERLAY:
	    do i = 1, 256 {
                CMAP(cmap,EX_RED,i)   = overlay[j]
                CMAP(cmap,EX_GREEN,i) = overlay[j+1]
                CMAP(cmap,EX_BLUE,i)  = overlay[j+2]
		j = j + 3
	    }
	}
end