/*
**  $VER: $Id: PZ_MakePic_BW.rexx,v 5.0 1993/11/12 01:15:10 chris Exp $
**  Copyright (C) 1992, 1993 by Christian A. Weber, Zürich, Switzerland.
**  REXX script internally used by PicZoo. Do not start manually.
**
**  You may wish to change MAXMEM for ADPro if you don't have enough RAM,
**  and the delay after loading if you have a slow HD :)
*/

options results
arg adprodir filename maxwidth maxheight
address 'ADPro'


/*
** Make sure ADPro is running
*/
IF ~show(ports,'ADPro') THEN
DO
	Address COMMAND 'C:Assign ADPRO: '||adprodir
	Address COMMAND 'Run >NIL: ADPRO:ADPro MAXMEM=5000000 BEHIND'
	Address COMMAND 'C:Wait 5'
	IF ~show(ports,'ADPro') THEN EXIT
END


/*
** Screen types for ADPro
*/
LORES		= 0
HIRES		= 1
LACE		= 2
PAL		= 4
XOVERSCAN	= 8
YOVERSCAN	= 16

HIRESBIT	= 0
LACEBIT		= 1


/*
** Now load the picture ...
*/
SCREEN_TYPE	LORES
LFORMAT		'UNIVERSAL'
LOAD		filename

IF RC == 0 THEN DO

	/*
	** Get the picture size
	*/
	XSIZE
	origwidth  = ADPRO_RESULT
	YSIZE
	origheight = ADPRO_RESULT


	/*
	**  Make a text string of the picture mode and size, and store it
	**  in an environment variable
	*/
	IMAGE_TYPE
	type = WORD(ADPRO_RESULT,1)
	if type = "GRAY" THEN DO
		type = "G (" || origwidth || "x" || origheight || ")"
	END
	ELSE DO
		type = "C (" || origwidth || "x" || origheight || ")"
    END
	Address COMMAND SetEnv PICZOO_IMAGETYPE type


	/*
	** If we have an interlaced lo-res or a non-interlaced hi-res
	** picture, we must correct the aspect ratio accordingly.
	** We do this by changing orgwidth or orgheight.
	*/
	SCREEN_TYPE
	viewmode = ADPRO_RESULT
/*
	IF BITTST(viewmode, HIRESBIT) THEN DO
		origwidth = origwidth / 2
		Say 'Hi-Res.'
	END

	IF BITTST(viewmode, LACEBIT) THEN DO
		origheight = origheight / 2
		Say 'Interlaced.'
	END
*/
	IF origwidth > origheight THEN DO
		width  = maxwidth
		height = (origheight * maxwidth) / origwidth
	END
	ELSE DO
		width  = (origwidth * maxheight) / origheight
		height = maxheight
	END


	/*
	** Set the palette to match the PicZoo screen
	*/
	POFFSET		0
	PUSED		16
	PTOTAL		16
	POFFSET		0
	PPOKE		 0 170 170 170
	PPOKE		 1   0   0   0
	PPOKE		 2 255 255 255
	PPOKE		 3 255   0 255
	PPOKE		 4  34  34  34
	PPOKE		 5  51  51  51
	PPOKE		 6  68  68  68
	PPOKE		 7  85  85  85
	PPOKE		 8 102 102 102
	PPOKE		 9 119 119 119
	PPOKE		10 136 136 136
	PPOKE		11 153 153 153
	PPOKE		12 187 187 187
	PPOKE		13 204 204 204
	PPOKE		14 221 221 221
	PPOKE		15 238 238 238
	PSTATUS		LOCKED


	/*
	** Now we convert the image to grey and scale it to width/height
	*/
	OPERATOR	COLOR_TO_GRAY
	ABS_SCALE	width height
	RENDER_TYPE	16


	/*
	** Make sure we get the best dynamic range
	*/
	OPERATOR	DYNAMIC_RANGE 0 255


	/*
	** Render the image with Floyd-Steinberg dithering
	*/
	DITHER		1		/* Floyd-Steinberg */
	SCREEN_TYPE	HIRES + LACE
	EXECUTE


	/*
	** Now save the image as a temp file, where PicZoo can get it.
	*/
	SFORMAT		'IFF'
	SAVE		'T:__PicZooTmp.iff' 'IMAGE'

END
ELSE DO
	/*
	** LOAD returned an error
	*/
	say filename || ': not a picture'
END