/************************************************************************
 * ImageLink sample ARexx script					*
 * Read IFF Frames numbered sequentially out to Targa Direct		*
 *  for frame-by-frame animation capability.				*
 * By Eric Lavitsky, 8/23/89 - updated 3/10/90				*
 *									*
 *	Syntax:								*
 *		1>rx script num						*
 *									*
 *		Where num is the number of frames you wish to convert.	*
 *		The frames must be numbered sequentially in the order	*
 *		in which they are to be converted.			*
 ************************************************************************/

/* Name the file as you like */
file = 'RAM:Cork.';

/************************************************************************
 *									*
 * Change the value of depth to reflect the desired depth of the output	*
 * image, e.g: 4, 8, 24, 32, etc. Targa-24 users should use "32".	*
 *									*
 ************************************************************************/
depth = 32;

/************************************************************************
 *									*
 * Change the value of dithmethod to reflect the type of dithering you	*
 * require. Valid values would be 'none' or 'floyd'.			*
 *									*
 ************************************************************************/
dithmethod = 'floyd';

/************************************************************************
 * The only other changes you may have to make would be specifically	*
 * related to the output module and image sizes being used (see below)	*
 ************************************************************************/

arg arg1

cur_dir = pragma(d,'')

/* Turn on result code passing */
options results;
options failat 20

call addlib 'rexxsupport.library',0,-30;
list = showlist(p);
beg = pos('ILINK',list);

if (beg = 0) then
	/* Need to start ImageLink */
        address command 'cd "' || cur_dir || '"' || '0A'x,
	'run imagelink'

/* Make sure WaitForPort is in your path! */
address command 'WaitForPort ILINK';

/* Find ImageLink's ARexx port and attempt to lock a */
/* private communications channel with it */
address 'ILINK' lock;

if (rc ~= 0) then do;
	say 'Could not lock ImageLink.';
	exit;
end;

/* Result contains the private communications channel */
address value result;

signal on error;
signal on syntax;
signal on break_c;

main:
	/* Assure the initial state */
	init;

	/* Get the version number for ImageLink */
	version;
	say 'ImageLink version is: 'result;

	/* Select initial modules */
	inpmod 'IFF ILBM File';
	outmod 'Truevision TARGA Direct';

	link;

	images = arg1;
	if (images = 0) then
		images = 1;

do i = 1 to images
	j = POS('.',file);
	file = DELSTR(file,j+1);
	file = INSERT(i,file,j);
	file = TRIM(file);

	/* No output file for Targa Direct */
	inpfil file;

	dither dithmethod;
	color cval depth;

	/* These values are good for a Targa board */
	iscale 512 480 0 0 512 480 12 10;
	oscale 512 480 0 0 512 480 12 10;

	/* Start the conversion */
	start;

	/* Single frame record here and loop till done */
	/* Uncomment the next line to record a frame with Transport Controller */
	/* record 1; */
	say 'Frame 'i' Complete';
end;

finish:

	unlink;

	/* Free the channel */
	unlock;


	/* Tell ImageLink to exit */
	signal off error;
	options failat 101
	quit;
	options failat 20

	exit;

error:
	say 'ImageLink/ARexx Error:'
	select
	 when rc = 80 then say ' LOCK: ImageLink already Locked';
	 when rc = 81 then say ' UNLOCK: Nothing to UnLock';
	 when rc = 82 then say ' LINK: No Input Module Selected';
	 when rc = 83 then say ' LINK: No Output Module Selected';
	 when rc = 84 then say ' START: No Input File';
	 when rc = 85 then say ' START: No Output File';
	 when rc = 96 then say ' Transfer Already In Progress';
	 when rc = 97 then say ' No Such Module Exists';
	 when rc = 98 then say " No Such File or Can't Create File";
	 when rc = 99 then say ' RECORD: Problem with Transport Controller';
	end;
	/* Tell ImageLink to exit */
	signal off error;
	unlock;
	options failat 101
	quit;
	options failat 20
	exit;

syntax:
	unlock;
	say 'ImageLink aborted script: syntax error'
	exit;

break_c:
	say 'Halted by ^C';
	unlock;
	exit;
