aboutsummaryrefslogtreecommitdiff
path: root/pkg/dataio/import/bltins/ipxwd.x
blob: 62a48ff73af53cc8232c7860ae7b11287ebe897e (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
# IPXWD.X - Source file for the IMPORT task X Window Dump builtin format.

include <mach.h>
include "../import.h"


# IP_XWD - Read and process an X Window Dump into an IRAF image.

procedure ip_xwd (ip, fname, info_only, verbose)

pointer	ip					#i import struct pointer
char	fname[ARB]				#i file name
int	info_only				#i print out image info only?
int	verbose					#i verbosity flag

int	fd
pointer im, cmap
int     nchars
long	depth, cmap_entries, hdr_size
long	hskip, lpad, width,height

long	ip_getl()

begin
        # Get the input file descriptor and initialize the file position.
        fd = IP_FD(ip)
        im = IP_IM(ip)
        call ip_lseek (fd, BOF)

	# Get some information from the header we'll need for processing.
	hdr_size = ip_getl (fd, 1)
	width = IP_AXLEN(ip,1)
	height = IP_AXLEN(ip,2)
	depth = ip_getl (fd, 45)
	hskip = IP_HSKIP(ip)
	lpad = IP_LPAD(ip)
	cmap_entries = ip_getl (fd, 73)
	nchars = width + lpad

	# See if we really want to convert this thing.
	if (info_only == YES) {
	    call ip_xwd_info (ip, fname, depth, cmap_entries, verbose)
	    return
	}

	# Now process the image.  For 24-bit or 32-bit files we have an RGB
	# image and can process normally, if this is an 8-bit image see if
	# we have a colormap we need to use.

	if (depth > 8) {
	    call ip_prpix (ip, fd, im, NULL)
	} else {
	    cmap = NULL
	    if (cmap_entries > 0)
		call xwd_rdcmap (ip, fd, hdr_size, cmap_entries, cmap)
	    call ip_prband (ip, fd, im, cmap)
        }
	IP_CMAP(ip) = NULL
end


# IP_XWD_INFO - Print information about the xwd file.

procedure ip_xwd_info (ip, fname, depth, ncolors, verbose)

pointer	ip					#i ip struct pointer
char	fname[ARB]				#i file name
int	depth					#i bits per pixel
int	ncolors					#i number of colors
int	verbose					#i verbosity flag

begin
	# If not verbose print a one-liner.
	if (verbose == NO) {
            #call printf ("Input file:\n\t")
            call printf ("%s: %20t%d x %d   \t%d-bit X11 Window Dump\n")
                call pargstr (fname)
                call pargi (IP_AXLEN(ip,1))
                call pargi (IP_AXLEN(ip,2))
                call pargi (depth)

            # Print out the format comment if any.
            if (IP_COMPTR(ip) != NULL) {
                if (COMMENT(ip) != '\0') {
                    call printf ("%s\n")
                        call pargstr (COMMENT(ip))
                }
                call strcpy ("\0", COMMENT(ip), SZ_LINE)
            }
	    return
	}

        # Print a more verbose description.
        call printf ("%s: %20tX11 Window Dump\n")
            call pargstr (fname)

        # Print out the format comment if any.
        if (IP_COMPTR(ip) != NULL) {
            if (COMMENT(ip) != '\0') {
                call printf ("%s\n")
                    call pargstr (COMMENT(ip))
            }
            call strcpy ("\0", COMMENT(ip), SZ_LINE)
        }

        call printf ("%20tByte Order:%38t%s\n")
	    if (IP_SWAP(ip) == S_NONE && BYTE_SWAP2 == NO )
                call pargstr ("Most Significant Byte First")
	    else
                call pargstr ("Least Significant Byte First")

        call printf ("%20tResolution:%38t%d x %d\n")
            call pargi (IP_AXLEN(ip,1))
            call pargi (IP_AXLEN(ip,2))

        call printf ("%20tType: %38t%d-bit %s\n")
	    call pargi (depth)
    	    if (ncolors > 0)
                call pargstr ("Color Index")
	    else
                call pargstr ("")

        call printf ("%20tHeader size:%38t%d bytes\n")
            call pargi (IP_HSKIP(ip))

	if (ncolors > 0) {
           call printf ("%20tColormap:%38t%d entries\n")
                call pargi (ncolors)
	} else
            call printf ("%20tColormap:%38tnone\n")

        call printf ("%20tAlpha Channel: %38t%s\n")
	    if (depth == 32)
		call pargstr ("8-bit")
	    else
		call pargstr ("none")
end


# XWD_RDCMAP - Read colormap from an X11 Window Dump file and return a 
# pointer to it.

procedure xwd_rdcmap (ip, fd, hdr_size, ncolors, cmap)

pointer	ip					#i task struct pointer
int	fd					#i file descriptor
int	hdr_size				#i header size
int	ncolors					#i number of colormap entries
pointer	cmap					#i colormap pointer

int	i
long	filepos, pixel
int	r, g, b
char	flags, pad

short	ip_getb()
int	ip_getu()
long	ip_getl()

define	SZ_X11_CSTRUCT	12

begin
        # Now read the colormap, allocate the pointer if we need to.
	cmap = NULL
        if (ncolors == 0)
	    return
	else
            call calloc (cmap, CMAP_SIZE*3, TY_CHAR)

	filepos = hdr_size + 3
	call ip_lseek (fd, filepos)
	do i = 1, ncolors {
	    pixel = ip_getl (fd, filepos)
	    r = ip_getu (fd, filepos+4)
	    g = ip_getu (fd, filepos+6)
	    b = ip_getu (fd, filepos+8)
	    flags = ip_getb (fd, filepos+10)
	    pad = ip_getb (fd, filepos+11)

	    CMAP(cmap,IP_RED,i)   = r * 255 / 65535
	    CMAP(cmap,IP_GREEN,i) = g * 255 / 65535
	    CMAP(cmap,IP_BLUE,i)  = b * 255 / 65535

	    filepos = filepos + SZ_X11_CSTRUCT
	    call ip_lseek (fd, filepos)
	}
	IP_CMAP(ip) = cmap
end