/*
 *  amiga.c
 *
 *  Author: Tomi Ollila <too@cs.hut.fi>
 *
 *  Created: Sun Jul 05 15:30:58 1992 too
 *  Last modified: Thu Feb 24 11:55:00 1994 too
 *
 */

asm(".text; jmp _start");

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/locale.h>

#define IFFL_CloseIFF	IFFL_NO1
#define IFFL_DecodePic	IFFL_NO2
#define IFFL_GetBMHD	IFFL_NO3
#define IFFL_OpenIFF	IFFL_NO4
#define IFFL_SaveBitMap IFFL_NO5
#include "iff.h"
#undef IFFL_CloseIFF
#undef IFFL_DecodePic
#undef IFFL_GetBMHD
#undef IFFL_OpenIFF
#undef IFFL_SaveBitMap

#include "inlines_other.h"
#include "inline_iff.h"

#include <stdlib.h>
#include <string.h>
#include "amiga.h"

#ifndef NULL
#define NULL 0
#endif

static const char ver[] = "$VER: phoon 2.1 (24.2.1994)";

struct ExecBase *	SysBase = NULL;
struct DosLibrary *	DOSBase = NULL;
     
struct IntuitionBase *	IntuitionBase = NULL;
struct GfxBase *	GfxBase = NULL;
struct Library *	IFFBase = NULL;

struct Library *	MathIeeeDoubBasBase = NULL;
struct Library *	MathIeeeDoubTransBase = NULL;

struct Library *	LocaleBase = NULL;

#define SL(a) ((struct Library **)a)

struct {
  struct Library **	base;
  char *		name;
  int			version;
} libtable[] = {
  { SL(&DOSBase),		"dos.library", 			36 },
  { SL(&IntuitionBase),		"intuition.library",		36 },
  { SL(&GfxBase),		"graphics.library",		36 },
  { SL(&IFFBase),		"iff.library",			23 },
  { SL(&MathIeeeDoubBasBase),	"mathieeedoubbas.library",	36 },
  { SL(&MathIeeeDoubTransBase),	"mathieeedoubtrans.library",	36 },
  { NULL }
};

struct RDArgs * rdargs = NULL;

WORD gmtoffset = 32767;

BOOL am_OpenLibraries()
{
  int i;
  struct Library ** lib;
  struct Locale * locale;
  
  SysBase = *(struct ExecBase **)4;

  for (i = 0; (lib = libtable[i].base); i++) {
    if ((*lib = OpenLibrary(libtable[i].name, libtable[i].version)) == NULL) {
      Printf("Could not open %s\n", libtable[i].name);
      return FALSE;
    }
  }

  if ((LocaleBase = OpenLibrary("locale.library", 38)) != NULL) {
    if ((locale = OpenLocale(NULL)) != NULL) {
      gmtoffset = locale->loc_GMTOffset;
      CloseLocale(locale);
    }
    CloseLibrary(LocaleBase);
  }
  
  if ((rdargs = ReadArgs(TEMPLATE, args, NULL)) == NULL) {
    Printf(usage);
    return FALSE;
  }
  return TRUE;
}

int am_CloseLibraries(int retval)
{
  int i;
  struct Library ** lib;

  if (rdargs)
    FreeArgs(rdargs);
  
  for (i = 0; (lib = libtable[i].base); i++)
    if (*lib)
      CloseLibrary(*lib);
  return retval;
}

BOOL am_GetScreenSize(int * w, int * h)
{
  struct Screen screen;

  if (GetScreenData((APTR)&screen, sizeof(struct Screen),
		    WBENCHSCREEN, NULL) == NULL) {
    Printf("Could not figure out screensize.\n");
    return FALSE;
  }
  *w = screen.Width;
  *h = screen.Height;
  return TRUE;
}

char * am_LoadFullmoon(int * w,int * h, char * filename)
{
  PLANEPTR		plane = NULL;
  IFFL_HANDLE		iffhandle;
  struct BitMap 	bitmap;
  struct IFFL_BMHD *	bmhd;
  int 			size;

  if (filename == NULL)
    filename = "fullmoon.ilbm\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

  if ((iffhandle = IFFL_OpenIFF(filename, IFFL_MODE_READ)) == NULL) {
    Printf("Error: Could not open %s for reading as fullmoon picture\n",
	   filename);
    return NULL;
  }
  if ((bmhd = IFFL_GetBMHD(iffhandle)) == NULL) {
    Printf("Error: no BMHD chunk in %s \n", filename);
    goto exit;
  }
  *w = (int)bmhd->w; *h = (int)bmhd->h;
  size = BitmapSize(bmhd->w, bmhd->h);

  if ((plane = (PLANEPTR)AllocVec(size, 0)) == NULL) {
    Printf("Out of memory\n");
    goto exit;
  }
  InitBitMap(&bitmap, 1, bmhd->w, bmhd->h);
  bitmap.Planes[0] = plane;

  if (IFFL_DecodePic(iffhandle, &bitmap) == FALSE) {
    Printf("Error: Could not decode IFF picture\n");
    FreeVec(plane);
    plane = NULL;
  }
 exit:
  IFFL_CloseIFF(iffhandle);
  return (char *)plane;
}

static UWORD colortable[] = {0xaaa, 0x000, 0xfff, 0x68b};

void am_MakeIFFile(int w, int h, char * bits, int reverseflag)
{
  struct BitMap bitmap;
  LONG		size;
  ULONG 	*plane1, *plane0;
  int		i;

  InitBitMap(&bitmap, 2, w, h);
  size = BitmapSize(w, h);
  plane0 = (ULONG *)bits;

  if ((plane1 = (ULONG *)AllocMem(size, 0)) == NULL) {
    Printf("Out of memory\n");
    return;
  }
  bitmap.Planes[reverseflag] = (PLANEPTR)plane1;
  bitmap.Planes[(reverseflag + 1) & 1] = (PLANEPTR)plane0;

  for (i = 0; i < size >> 2; i++)
    plane1[i] = plane0[i] ^ 0xffffffff;

  (void)IFFL_SaveBitMap("RAM:phoon.ilbm", &bitmap, colortable, 0);
  FreeMem(plane1, size);
}
