aboutsummaryrefslogtreecommitdiff
path: root/pkg/dataio/import/bltins/ipras.x
blob: 100ca6dc469ec09c6d0193ae8efaa485809baad2 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
include <mach.h>
include "../import.h"


# IPRAS.X - Source file for the IMPORT task rasterfile builtin format.


define	SZ_RASHDR	13
define	RAS_MAGIC	Memi[$1]	# Magic number
define	RAS_WIDTH	Memi[$1+1]	# Image width (pixels per line)
define	RAS_HEIGHT	Memi[$1+2]	# Image height (number of lines)
define	RAS_DEPTH	Memi[$1+3]	# Image depth (bits per pixel)
define	RAS_LENGTH	Memi[$1+4]	# Image length (bytes)
define	RAS_TYPE	Memi[$1+5]	# File type
define	RAS_MAPTYPE	Memi[$1+6]	# Colormap type
define	RAS_MAPLENGTH	Memi[$1+7]	# Colormap length (bytes)

define	RAS_CMAP	Memi[$1+10]	# Colormap (ptr)
define	RAS_COUNT	Memi[$1+11]	# RLE decoding var
define	RAS_CH		Memi[$1+12]	# RLE decoding var

# Rasterfile magic number
define	RAS_MAGIC_NUM	59A66A95X
define	RAS_RLE		80X

# Sun supported ras_types
define	RT_OLD		0	# Raw pixrect image in 68000 byte order
define	RT_STANDARD	1	# Raw pixrect image in 68000 byte order
define	RT_BYTE_ENCODED	2	# Run-length compression of bytes
define	RT_FORMAT_RGB	3	# XRGB or RGB instead of XBGR or BGR
define	RT_FORMAT_TIFF	4	# tiff <-> standard rasterfile
define	RT_FORMAT_IFF	5	# iff (TAAC format) <-> standard rasterfile
define	RT_EXPERIMENTAL	65535	# Reserved for testing

# Sun supported ras_maptypes
define	RMT_NONE	0	# ras_maplength is expected to be 0
define	RMT_EQUAL_RGB	1	# red[ras_maplength/3],green[],blue[]
define	RMT_RAW		2



# IP_RAS - Read and process a Sun Rasterfile into an IRAF image.

procedure ip_ras (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

pointer	ras
int	fd, w, nchars
pointer	ras_open()

long    filepos
common  /rascom/ filepos

begin
        # Allocate the ras struct pointer.
        ras = ras_open ()
        fd = IP_FD(ip)

        # Initialize the file position.
        filepos = 1
        call ip_lseek (fd, BOF)

	# Read in the rasterfile header, dump it directly to the task struct.
	call ip_ageti (fd, ras, 8)
	filepos = filepos + SZ_INT32 * SZB_CHAR * 8
	call ip_lseek (fd, filepos)

	# Now do some sanity checking on the values.
	if (RAS_MAGIC(ras) != RAS_MAGIC_NUM)
	    call error (0, "Not a Sun rasterfile.")
        if (RAS_TYPE(ras) == RT_OLD && RAS_LENGTH(ras) == 0)
            RAS_LENGTH(ras) = RAS_WIDTH(ras) * RAS_HEIGHT(ras) *
		RAS_DEPTH(ras) / 8

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

	# Get the colormap (if any).
	call ras_rdcmap (fd, ras, RAS_CMAP(ras))
	IP_CMAP(ip) = RAS_CMAP(ras)

        # Round up to account for 16 bit line blocking.
	w = RAS_WIDTH(ras) * (RAS_DEPTH(ras) / 8)
        nchars = w + mod (w, SZB_CHAR)


        # Patch up the pixtype param if needed.
        call ip_fix_pixtype (ip)

        # See if we need to create any outbands operands if the user didn't.
        if (IP_NBANDS(ip) == ERR)
            call ip_fix_outbands (ip)

	# Now process the image.
	switch (RAS_DEPTH(ras)) {
	case 1:
	    call eprintf ("Bitmap rasterfiles aren not supported.")
	    call flush (STDERR)

	case 8:
	    # Standard or byte encoded 8-bit rasterfile.
	    if (RAS_TYPE(ras) == RT_OLD || RAS_TYPE(ras) == RT_STANDARD) {
		call ip_prband (ip, fd, IP_IM(ip), RAS_CMAP(ras))

            } else if (RAS_TYPE(ras) == RT_BYTE_ENCODED) {
	        call ras_rle8 (ip, ras, fd, nchars)

            } else {
	        call eprintf ("Unsupported 8-bit RAS_TYPE: %d\n")
		    call pargi (RAS_TYPE(ras))
	        call flush (STDERR)
	    }

	case 24, 32:
	    # 24 or 32-bit rasterfiles have no colormap (at least they 
	    # shouldn't) and are pixel-interleaved.  We already know how to 
	    # do this so just call the right routines for processing.

	    if (RAS_TYPE(ras) == RT_BYTE_ENCODED) {
	        call ip_fix_pixtype (ip)
	        call ras_rle24 (ip, ras, fd, nchars)
	    } else {
	        call ip_fix_pixtype (ip)
	        call ip_prpix (ip, fd, IP_IM(ip), NULL)
	    }

	default:
	    call eprintf ("Invalid pixel size.")
	    call flush (STDERR)
	}

	# Clean up.
	call ras_close (ras)
	IP_CMAP(ip) = NULL
end


# IP_RAS_INFO - Print information about the raster file.

procedure ip_ras_info (ip, ras, fname, verbose)

pointer	ip					#i ip struct pointer
pointer	ras					#i ras struct pointer
char	fname[ARB]				#i file name
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\t%d-bit Sun Rasterfile\n")
                call pargstr (fname)
                call pargi (RAS_WIDTH(ras))
                call pargi (RAS_HEIGHT(ras))
                call pargi (RAS_DEPTH(ras))

            # 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)
#            }
#	    if (RAS_DEPTH(ras) > 8) {
#	        if (RAS_TYPE(ras) != RT_FORMAT_RGB && RAS_TYPE(ras) != RT_OLD) {
#		    call eprintf ("\tNote: %d-bit rasterfile is stored as %s\n")
#		        call pargi (RAS_DEPTH(ras))
#			call pargstr ("ABGR and not ARGB")
#	        }
#	    }
	    return
	}

        # Print a more verbose description.
        call printf ("%s: %20tSun Rasterfile\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)
        }
	if (RAS_DEPTH(ras) > 8) {
	    if (RAS_TYPE(ras) != RT_FORMAT_RGB && RAS_TYPE(ras) != RT_OLD) {
		call eprintf ("\tNote: %d-bit rasterfile is stored as %s\n")
		    call pargi (RAS_DEPTH(ras))
		    call pargstr ("ABGR and not ARGB")
	    }
	}

        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 (RAS_WIDTH(ras))
            call pargi (RAS_HEIGHT(ras))

        call printf ("%20tType: %38t%d-bit %s %s\n")
	    call pargi (RAS_DEPTH(ras))
            switch (RAS_TYPE(ras)) {
            case RT_OLD:
                call pargstr ("Old")
            case RT_STANDARD:
                call pargstr ("Standard")
            case RT_BYTE_ENCODED:
                call pargstr ("Byte Encoded")
            case RT_FORMAT_RGB:
                call pargstr ("RGB")
            case RT_FORMAT_TIFF:
                call pargstr ("TIFF")
            case RT_FORMAT_IFF:
                call pargstr ("IFF")
            default:
                call pargstr ("Experimental (or unknown)")
            }
	    if (RAS_MAPLENGTH(ras) > 0)
                call pargstr ("Color Index")
	    else
                call pargstr ("")

	if (RAS_MAPLENGTH(ras) > 0) {
            call printf ("%20tColormap:%38t%d entries\n")
	        if (RAS_MAPTYPE(ras) == RMT_EQUAL_RGB)
                    call pargi (RAS_MAPLENGTH(ras)/3)
	        else
                    call pargi (RAS_MAPLENGTH(ras))
	} else
            call printf ("%20tColormap:%38tnone\n")

        call printf ("%20tCompression: %38t%s\n")
            if (RAS_TYPE(ras) == RT_BYTE_ENCODED)
	        call pargstr ("Run Length Encoded")
	    else 
		call pargstr ("None")

        call printf ("%20tAlpha Channel: %38t%s\n")
	    if (RAS_DEPTH(ras) == 32)
		call pargstr ("yes")
	    else
		call pargstr ("none")
end


# RAS_OPEN - Open the RAS structure descriptor.

pointer procedure ras_open ()

pointer	ras

begin
	iferr (call calloc (ras, SZ_RASHDR, TY_STRUCT))
	    call error (0, "Error allocating RAS structure.")
	RAS_CMAP(ras) = NULL

	return (ras)
end


# RAS_CLOSE - Close the RAS structure descriptor.

procedure ras_close (ras)

pointer	ras					#i RAS struct pointer

begin
	if (RAS_CMAP(ras) != NULL)
	    call mfree (RAS_CMAP(ras), TY_CHAR)
	call mfree (ras, TY_STRUCT)
end


# RAS_RDCMAP - Read the colormap from the image if necessary.

procedure ras_rdcmap (fd, ras, cmap)

int	fd					#i file descriptor
pointer	ras					#i RAS struct pointer
pointer	cmap					#i colormap array ptr

int	ncolors

long    filepos
common  /rascom/ filepos

begin
	# Now read the colormap, allocate the pointer if we need to.
	ncolors = RAS_MAPLENGTH(ras)
	if (RAS_MAPTYPE(ras) == RMT_EQUAL_RGB && ncolors > 0) {
	    if (cmap == NULL)
	        call calloc (cmap, ncolors*3, TY_CHAR)
	    call ip_agetb (fd, cmap, ncolors)

	} else if (RAS_MAPTYPE(ras) == RMT_RAW) {
	    call eprintf ("Warning: Can't handle RMT_RAW maptype - ignoring.\n")
	    call flush (STDERR)

	    # Skip over the bytes anyway.
	    filepos = filepos + ncolors
	    call ip_lseek (fd, filepos)
	    return
	}

	filepos = filepos + ncolors
	call ip_lseek (fd, filepos)
end


# RAS_RLE8 - Process an 8-bit rasterfile into an IRAF image.  This
# procedure handles both standard and RLE files.

procedure ras_rle8 (ip, ras, fd, nchars)

pointer	ip					#i ip struct pointer
pointer	ras					#i ras struct pointer
int	fd					#i input file descriptor
int	nchars					#i line size

pointer	im, data, op
int	i, percent

long    filepos
common  /rascom/ filepos

begin
        im = IP_IM(ip)
        op = PTYPE(ip,1)
        call malloc (data, nchars, TY_CHAR)
        IO_DATA(op) = data
        IO_NPIX(op) = RAS_WIDTH(ras)

        percent = 0
        do i = 1, RAS_HEIGHT(ras) {
	    call ras_read_rle (ras, fd, Memc[data], nchars)

            # Apply the colormap since this is just an index.
            if (RAS_MAPLENGTH(ras) != 0 && IP_USE_CMAP(ip) == YES)
                call ip_gray_cmap (Memc[data], RAS_WIDTH(ras),
                    RAS_CMAP(ras))

            # Evaluate and write the outbands expressions.
            call ip_probexpr (ip, im, RAS_WIDTH(ras), i)

            # Print percent done if being verbose
            if (IP_VERBOSE(ip) == YES) {
                if (i * 100 / RAS_HEIGHT(ras) >= percent + 10) {
                    percent = percent + 10
                    call printf ("    Status: %2d%% complete\r")
                        call pargi (percent)
                    call flush (STDOUT)
                }
            }

        }

        if (IP_VERBOSE(ip) == YES) {
            call printf ("    Status: Done          \n")
            call flush (STDOUT)
        }
end


# RAS_RLE24 - Process an 24-bit rasterfile into an IRAF image.  This
# procedure handles both standard and RLE files.

procedure ras_rle24 (ip, ras, fd, nchars)

pointer	ip					#i ip struct pointer
pointer	ras					#i ras struct pointer
int	fd					#i input file descriptor
int	nchars					#i line size

pointer	im, data, op
int	i, percent, npix

long    filepos
common  /rascom/ filepos

begin
        im = IP_IM(ip)
        op = PTYPE(ip,1)
        call malloc (data, nchars, TY_SHORT)
        IO_DATA(op) = data
        IO_NPIX(op) = RAS_WIDTH(ras)

        # See if we need to create any outbands operands if the user didn't.
        if (IP_NBANDS(ip) == ERR)
            call ip_fix_outbands (ip)

        # Allocate the pixtype data pointers.
        npix = RAS_WIDTH(ras)
        do i = 1, IP_NPIXT(ip) {
            op = PTYPE(ip,i)
            IO_NPIX(op) = npix
            call calloc (IO_DATA(op), npix, TY_SHORT)
        }

        percent = 0
        do i = 1, RAS_HEIGHT(ras) {
	    call ras_read_rle (ras, fd, Memc[data], nchars)

            # Separate pixels into different vectors.
            call ip_upkpix (ip, data, npix)

            # Evaluate and write the outbands expressions.
            call ip_probexpr (ip, im, npix, i)

            # Print percent done if being verbose
            if (IP_VERBOSE(ip) == YES) {
                if (i * 100 / RAS_HEIGHT(ras) >= percent + 10) {
                    percent = percent + 10
                    call printf ("    Status: %2d%% complete\r")
                        call pargi (percent)
                    call flush (STDOUT)
                }
            }

        }
        if (IP_VERBOSE(ip) == YES) {
            call printf ("    Status: Done          \n")
            call flush (STDOUT)
        }
end


# RAS_READ_RLE - Read a line of RLE encoded data from the file.

procedure ras_read_rle (ras, fd, data, nchars)

pointer	ras					#i ras struct pointer
int	fd					#i file descriptor
char	data[ARB]				#u output pixels
int	nchars					#i number of pixels to read

int	i
short	pix, ras_rdbyte()

long    filepos
common  /rascom/ filepos

begin
	i = 1
	while (i <= nchars) {
	    if (RAS_COUNT(ras) > 0) {
		data[i] = RAS_CH(ras)
		i = i + 1
		RAS_COUNT(ras) = RAS_COUNT(ras) - 1

	    } else {
		pix = ras_rdbyte (fd)
		if (pix == RAS_RLE) {
		    RAS_COUNT(ras) = ras_rdbyte (fd)
		    if (RAS_COUNT(ras) == 0) {
			data[i] = pix
			i = i + 1
		    } else {
		        RAS_CH(ras) = ras_rdbyte (fd)
			data[i] = RAS_CH(ras)
			i = i + 1
		    }
		} else {
		    data[i] = pix
		    i = i + 1
		}
	    }
	}
end


# RAS_RDBYTE - Read a single byte at the current offset from the file.

short procedure ras_rdbyte (fd)

int     fd                                      #i file descriptor

short   val
short   ip_getb()

long    filepos
common  /rascom/ filepos

begin
        iferr (val = ip_getb (fd, filepos))
            return (ERR)

        filepos = filepos + 1
        call ip_lseek (fd, filepos)

        return (val)
end