/*
	ShowIFF.c  -  an easy IFF file view program by Christian A. Weber
	Requires the iff.library (in the LIBS: dircetory or in a ROM)
	This program may be freely used and modified!
	I compiled it with Aztec C V3.6, for Lattice you must change iff.h
*/

#include "exec/types.h"
#include "intuition/intuition.h"
#include "libraries/iff.h"


struct Library *GfxBase,*IntuitionBase,*IFFBase=NULL;

struct NewScreen ns =
{
	0,0,0,0,0,0,0, NULL, CUSTOMSCREEN, NULL,
	(STRPTR)"IFF test screen", NULL, NULL
};

struct Screen *myscreen = NULL;
APTR ifffile = NULL;


main(argc,argv)
int argc;
char **argv;
{
	struct Library *OpenLibrary();
	struct Screen *OpenScreen();

	UWORD colortable[128];
	struct BitMapHeader *bmhd;
	ULONG count;
	int i;

	if((argc != 2) || !strcmp(argv[1],"?")) {
		printf("Format: %s filename\n",argv[0]);
		exit(20);
	}

	GfxBase = OpenLibrary("graphics.library",0L);
	IntuitionBase = OpenLibrary("intuition.library",0L);

	if(!(IFFBase = OpenLibrary(IFFNAME,IFFVERSION))) {
		printf("Copy the iff.library to your LIBS: directory!\n");
		exit(10);
	}

	printf("Loading file %s ... ",argv[1]);

	if(!(ifffile=OpenIFF(argv[1]))) {
		Fail("Error opening file");
	}

	if(!(bmhd=GetBMHD(ifffile))) {
		Fail("BitMapHeader not found");
	}

	ns.Width      = bmhd->w;
	ns.Height     = bmhd->h;
	ns.Depth      = bmhd->nPlanes;
	ns.ViewModes  = GetViewModes(ifffile);

	if(!(myscreen = OpenScreen(&ns))) {
		Fail("Can't open screen!");
	}

	count = GetColorTab(ifffile,colortable);
	if(count>32L) count = 32L; /* Some HAM pictures have 64 colors ?! */
	LoadRGB4(&(myscreen->ViewPort),colortable,count);

	if(!(DecodePic(ifffile,&(myscreen->BitMap))))
	{
		Fail("Can't decode the picture");
	}

	for(i=0; i<100; ++i)
	{
		if(!((*((UBYTE*)0xbfe001))&64)) break;
		Delay(5L);
	}

	Fail("done"); /* Close the whole stuff */

}


Fail(text)
char *text;
{
	if(ifffile) CloseIFF(ifffile);
	if(myscreen) CloseScreen(myscreen);

	printf("%s\n",text);
	printf("IffError: %ld\n",IffError());

	if(IFFBase) CloseLibrary(IFFBase);
	CloseLibrary(IntuitionBase);
	CloseLibrary(GfxBase);
	exit(0);
}

