#include <stdio.h>
#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/sprite.h>
#include <graphics/gels.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include "shoot.h"

extern struct ViewPort my_view_port;
extern struct BitMap my_bit_map;

struct FileHandle *tbild;

UBYTE picture[128];
long int chunksize;
int bodygot;

unsigned int chunksread;

UWORD width, height;
int bytezeile, ncol, farben;

BYTE depth, compr, aspx, aspy;

ULONG camg;
int bprow, bytezscreen;

BYTE colors[ 256*4 ];

int line, plane;

void iffbmhd( void )
{
	chunksread = chunksread | 1;

	if( Read( tbild, &width, 2 ) != 2 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &height, 2 ) != 2 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &picture, 4 ) != 4 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &depth, 1 ) != 1 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &picture, 1 ) != 1 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &compr, 1 ) != 1 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &picture, 3 ) != 3 )
	{	
		printf("Read error bmhd\n");
	}

	if( Read( tbild, &aspx, 1 ) != 1 )
	{	
		printf("Read error bmhd\n");
	}
	if( Read( tbild, &aspy, 1 ) != 1 )
	{	
		printf("Read error bmhd\n");
	}

	/*printf("chunksize %d\n", chunksize);*/
	Seek( tbild, chunksize-16, OFFSET_CURRENT );
}

void iffcmap( void )
{
	int a;
	BYTE *colo;

	chunksread = chunksread | 2;
	ncol = (int) chunksize/3;
	if (ncol<=256)
	{
		colo=&colors[ 0 ];
		for (a=0;a<ncol;a++)
		{
			if( Read( tbild, colo, 3 ) != 3 )
			{	
				printf("Read error cmap\n");
			}
			colo = colo + 4;
			SetRGB4( &my_view_port, a, colors[ a*4 ], colors[ a*4+1 ], colors[ a*4+2 ] );
		}
		Seek( tbild, chunksize-ncol*3, OFFSET_CURRENT );
	}
	else
	{
		printf("zu viele farben\n");
	}
}

void iffcamg( void )
{
	chunksread = chunksread | 4;
	if( Read( tbild, &camg, 4 ) != 4 )
	{	
		printf("Read error bmhd\n");
	}
}

void zeilelesen( void )
{
	UWORD a, b;
	UBYTE aa;

	bprow=bytezeile;
	if (compr==1)
	{
		while (bprow>0)
		{
			if( Read( tbild, &aa, 1 ) != 1 )
			{	
				printf("Read error line (comand)\n");
				bprow = 0;
			}
			a = aa;
			if ( a<128 )
			{
				/* ungecruncht */
				a=a+1;
				if( Read( tbild, my_bit_map.Planes[ plane ]+line*RWIDTH/8+bytezeile-bprow, a ) != a )
				{	
					printf("Read error line\n");
					bprow = 0;
				}
				bprow=bprow-a;
			}
			else if ( a>128 )
			{
				a=256-a+1;
	
				if( Read( tbild, &aa, 1 ) != 1 )
				{	
					printf("Read error bmhd\n");
				}
	
				while( a>0 )
				{
					*((UBYTE *) my_bit_map.Planes[ plane ]+line*RWIDTH/8+bytezeile-bprow) = aa;
					bprow--;
					a--;
				}
			}
		}
	}
	else
	{
		/* nicht gecruncht */
		if( Read( tbild, my_bit_map.Planes[ plane ]+line*RWIDTH/8, bytezeile ) != bytezeile )
		{	
			printf("Read error not crunched\n");
		}
	}
}

void iffbody( void )
{
	int a;

	if ( (chunksread & 4) == 0 )
	{
		camg = 0;
	}
	if (depth<=8)
	{
		bytezscreen = 40;		/*	;Bytes pro Zeile im Screen*/
		farben=(1<<depth);

		bytezeile= ((width+15) & 0xfff0) / 8;

		for (line=0;line<height;line++)
		{
			for (plane=0;plane<depth;plane++)
			{
				zeilelesen();
			}
		}
	}
}


void getilbm( void )
{
	tbild = Open( "data/shoot_title.lbm", MODE_OLDFILE );
	if( tbild!=NULL )
	{
		if( Read( tbild, &picture, 4 ) != 4 )
		{	
			printf("Read error tbild\n");
		}

		if ( (picture[0]=='F') && (picture[1]=='O') && (picture[2]=='R') && (picture[3]=='M'))
		{
			chunksread = 0;

			bodygot = 0;
			while( bodygot==0 )
			{
				if( Read( tbild, &picture, 4 ) != 4 )
				{	
					printf("Read error chunk header\n");
				}
				if( Read( tbild, &chunksize, 4 ) != 4 )
				{	
					printf("Read error chunk size\n");
				}

				if ((picture[0]=='B') && (picture[1]=='M') && (picture[2]=='H') && (picture[3]=='D'))
				{
					iffbmhd();
				}
				else if ((picture[0]=='C') && (picture[1]=='M') && (picture[2]=='A') && (picture[3]=='P'))
				{
					iffcmap();
				}
				
				else if ((picture[0]=='C') && (picture[1]=='A') && (picture[2]=='M') && (picture[3]=='G'))
				{
					iffcamg();
				}

				else if ((picture[0]=='B') && (picture[1]=='O') && (picture[2]=='D') && (picture[3]=='Y'))
				{
					iffbody();
					bodygot = 1;
				}
				else
				{
					Seek( tbild, chunksize, OFFSET_CURRENT );
				}
			}
		}
		else
		{
			printf("file is not an IFF-ILBM picture\n");
		}

		Close( tbild );
	}
	else
	{
		printf("Couldn't read title\n");
	}
}



