/** DrawIFF.c
  *
  *    NAME
  *
  * DrawIFF
  * =======
  *
  *    SYNOPSIS
  *
  * DrawIFF(file, rp, x, y, width, height, screen)
  *
  *    FUNCTION
  *
  * This function opens an IFF ILBM file and draws it into a layered
  * rastport, and changes the colors on the screen if the screen
  * argument is given.
  *
  *    ARGUMENTS
  *
  * char *file  Name of IFF ILBM file
  * struct RastPort *rp Destination rastport
  * int x, y  Offset into destination rastport
  * int width  Width of picture in destination rastport
  * int height  Height of picture in destination rastport
  * struct Screen *screen Pointer to screen receiving the colormap
  *
  *    ADDITIONAL CONSIDERATIONS
  *
  * This routine uses a lot of memory. The IFF file is read into ram,
  * and a private RastPort is allocated the size of the picture before
  * the picture is copied into the destination rastport. This routine
  * uses the iff.library written by Werner Guenther.
  *
  *    BUGS
  *
  * None known.
  *
  *    AUTHOR/DATE
  *   ============
  *
  *   W.G.J. Langeveld, 15 November 1988
  *
  *	CURRENT VERSION:
  *
  *	This version has been converted to SAS C 6.5 format. It has been modified
  *	for modern definition sequences for ANSI compilation. This no longer works
  *	with OS versions prior to 2.04.
  *
  *   AUTHOR/DATE:  Joanne Dow, jdow@bix.com, June 1998.
  *   ============
  *
  **/

#include <functions.h>
#include "ralprotos.h"
#include <exec/types.h>
#include <exec/exec.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <devices/timer.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iff.h>

struct Library *IFFBase = NULL;
/**
 *
 *   Function to draw an IFF file into a RastPort.
 *
**/
int DrawIFF( char *file, struct RastPort *rp, int x, int y, int width, int height, struct Screen *screen )
{
	struct RastPort *trp = NULL, *AllocRastPort(), *FreeRastPort();
	UWORD colortable[128];
	struct BitMapHeader *bmhd = NULL;
	ULONG count = 0L;
	int picw = 0, picd = 0, pich = 0;
	long picv = 0L;
	APTR ifffile = NULL;
	int result = 4;
	
	IFFBase = (struct Library *) OpenLibrary("iff.library", 0L);
	if (IFFBase == NULL)
		goto cleanup;
	/*
	 *   Load the IFF file
	 */
	if ((ifffile = IFFL_OldOpenIFF(file)) == NULL)
		goto cleanup;
	/*
	 *   Get bitmap header
	 */
	result = 3;
	if ((bmhd = IFFL_GetBMHD(ifffile)) == NULL)
		goto cleanup;
	
	picw = bmhd->w;
	if (width > picw)
		width = picw;
	if (width == 0)
		width = picw;
	
	pich = bmhd->h;
	if (height > pich)
		height = pich;
	if (height == 0)
		height = pich;
	
	picd = bmhd->nPlanes;
	picv = IFFL_GetViewModes(ifffile);
	/*
	 *   Get a rastport of our own.
	 */
	result = 2;
	if ((trp = AllocRastPort(picd, picw, pich)) == NULL)
		goto cleanup;
	/*
	 *   If screen pointer is given, set colors as well.
	 */
	if (screen) 
	{
		count = IFFL_GetColorTab(ifffile, (WORD *) colortable);
		if (count > 32L)
			count = 32L;
		if (count)
			LoadRGB4(&(screen->ViewPort), colortable, count);
	}
	/*
	 *   Put the picture in the bitmap.
	 */
	result = 1;
	if ((IFFL_DecodePic(ifffile, trp->BitMap)) == NULL)
		goto cleanup;
	/*
	 *   Copy the picture into the window.
	 */
	ClipBlit(trp, 0L, 0L, rp, (long) x,     (long) y,
	(long) width, (long) height, 0xCCL);
	/*
	BltBitMapRastPort(trp->BitMap, 0L, 0L, rp, (long) x,     (long) y,
	(long) width, (long) height, 0xCCL);
	 */
	result = 0;
	
	cleanup:
	if (ifffile)
		IFFL_CloseIFF(ifffile);
	if (IFFBase)
		CloseLibrary(IFFBase);
	if (trp)
		FreeRastPort(trp);
	
	return(result);
}
