
/*
 *
 * Module	: bitmaps.c
 *
 * Description	: Functions for processing my bit map files.
 *
 * Author	: Simon J Raybould.	(sie@fulcrum.bt.co.uk)
 *
 * Date		:  5th September 1990.
 *
 */

#include <stdio.h>
#include <fcntl.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfx.h>
#include "bitmaps.h"

static struct Remember *RememberKey = NULL;

SetUpBitMap(BitMap, Name)
struct BitMap *BitMap;
char *Name;
{
	struct BitMapHeader BMHdr;
	int PSize, TSize;	/* Plane size & Total Size */
	int fd;
	char *MemPtr, *AllocRemember();
	UBYTE Plane;

	if((fd = open(Name, O_RDONLY)) == -1) {
		perror("open bitmap");
		return -1;
	}
	if(read(fd, &BMHdr, sizeof(struct BitMapHeader)) < sizeof(struct BitMapHeader)) {
		perror("read bitmap header");
		return -1;
	}
	PSize = RASSIZE(BMHdr.Width, BMHdr.Height);	/* size of one plane */
	TSize = PSize * BMHdr.Depth;	/* size of all planes */
	InitBitMap(BitMap, BMHdr.Depth, BMHdr.Width, BMHdr.Height);
	if(!(MemPtr = AllocRemember(&RememberKey, TSize, MEMF_CHIP))) {
		fprintf(stderr, "Not enough memory for bitmaps\n");
		return -1;
	}
	if(read(fd, MemPtr, TSize) < TSize) {
		perror("read bitmap");
		return -1;
	}
	close(fd);
	for(Plane=0; Plane<BMHdr.Depth; Plane++) {
		BitMap->Planes[Plane] = (PLANEPTR)MemPtr;
		MemPtr += PSize;
	}
	return 0;
}

FreeBitMaps()
{
	FreeRemember(&RememberKey, TRUE);
	return 0;
}
