/************************************************************************\
* C64 koala to portable pixmap converter 26-Sep-92                       *
* By Pasi 'Albert' Ojala ) 1991,1992                                     *
*    po87553@cs.tut.fi  albert@cc.tut.fi                                 *
*                                                                        *
* Compile with SAS/C: lc -. -ctf -L -ba -r -v -y -NODEBUG koalatoppm.c   *
*                                                                        *
* 26-Sep-92	Changed the palette a bit                                    *
*                                                                        *
\************************************************************************/
 
/* #define AMIGA */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef	AMIGA

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/libraries.h>
#include <exec/interrupts.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

#include <proto/dos.h>
#include <proto/exec.h>

#else

typedef unsigned char UBYTE;

#endif
 
char *vers="\0$VER: koalatoppm 1.02 26-Sep-92\n";

UBYTE	gfxmem[8192];
UBYTE	chrmem[1024];
UBYTE	colmem[1024];
UBYTE	bgcol;

const UBYTE col[16][3]=
{	0,0,0,
	240,240,240,
	192,0,64,
	0,208,192,
	192,0,192,
	0,176,0,
	0,0,208,
	224,240,96,
	208,96,0,
	112,48,0,
	240,112,144,
	80,80,80,
	128,128,128,
	144,240,160,
	112,144,240,
	192,192,208
};

void Convert(char *name);

void main(int argc,char *argv[])
{	UBYTE temp[100];
	int i=0;
#ifdef AMIGA
	long handle;
#else
	FILE *handle;
#endif

	if(argc==3 || argc==2)
	{
#ifdef AMIGA
		if(handle=Open(argv[1],MODE_OLDFILE))
		{	Read(handle,temp,2);	/* remove load address */

			Read(handle,gfxmem,8000);
			Read(handle,chrmem,1000);
			Read(handle,colmem,1000);
			i=Read(handle,&bgcol,1);

			Close(handle);
#else
		if(handle=fopen(argv[1],"rb"))
		{
			fread(temp,2,sizeof(UBYTE),handle);

			fread(gfxmem,8000,sizeof(UBYTE),handle);
			fread(chrmem,1000,sizeof(UBYTE),handle);
			fread(colmem,1000,sizeof(UBYTE),handle);
			i=fread(&bgcol,1,sizeof(UBYTE),handle);

			fclose(handle);
#endif
			if(i && argc==2)
				Convert(NULL);
			else
			if(i && argc==3)
				Convert(argv[2]);
			else
				fprintf(stderr,"Short file!\n");
		}
		else
		{
			fprintf(stderr,"Could not access %s\n",argv[1]);
			exit(20);
		}
	}
	else
	{
		fprintf(stderr,"\n%s",vers+7);
		fprintf(stderr,"Usage: koalatoppm koalafile [ppmfile]\n");
		exit(10);
	}
}


void Convert(char *name)
{	int x,y,ind,pos,bits;
	UBYTE a,b,buffer[3*160];
	const UBYTE bitmask[4]={0xc0,0x30,0x0c,0x03};
	const UBYTE bitshift[4]={0x40,0x10,0x04,0x01};
#ifdef AMIGA
	long handle;
#else
	FILE *handle;
#endif

	fprintf(stderr,"Converting picture\n");

#ifdef AMIGA
	if((!name) || (handle=Open(name,MODE_NEWFILE)))
	{
		if(name)
			Write(handle,"P6\n160 200\n255\n",15);	/* ppm byte-model ident */
#else
	if((!name) || (handle=fopen(name,"wb")))
	{
		if(name)
			fwrite("P6\n160 200\n255\n",15,sizeof(char),handle);
#endif
		else
			fwrite("P6\n160 200\n255\n",15,sizeof(char),stdout);

		for(y=0;y<200;y++)
		{	for(x=0;x<160;x++)
			{	ind=x/4+(y/8)*40;			/* color memory index */
				pos=y%8+(x/4)*8+(y/8)*320;	/* grafix memory byte */
				bits=(x%4);					/* bit numbers */

				a=(gfxmem[pos] & bitmask[bits])/bitshift[bits];

				switch(a)
				{	case 0:
						b=bgcol;
						break;
					case 1:
						b=chrmem[ind]/16;
						break;
					case 2:
						b=chrmem[ind]%16;
						break;
					case 3:
						b=colmem[ind]%16;
						break;
				}

				buffer[3*x]=col[b][0];
				buffer[3*x+1]=col[b][1];
				buffer[3*x+2]=col[b][2];
			}
			if(name)
#ifdef AMIGA
				Write(handle,buffer,3*160);
#else
				fwrite(buffer,3*160,sizeof(char),handle);
#endif
			else
				fwrite(buffer,3*160,sizeof(char),stdout);
		}
		if(name)
#ifdef AMIGA
			Close(handle);
#else
			fclose(handle);
#endif
	}
	else
		fprintf(stderr,"Could not open %s\n",name);
}
