/* MPMorph - Amiga Morphing program */
/* Copyright (C) © 1993  Topicsave Limited */

/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* any later version. */

/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
/* GNU General Public License for more details. */

/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

/* mpaddock@cix.compulink.co.uk */

#include "Render.h"

#ifndef __MPCLI
#include "a/Render.h"
#endif

APTR
MyAllocVec(ULONG size,ULONG requirements) {
	ULONG *MyMem;
#ifdef __MPCLI
	if (requirements & MEMF_CLEAR) {
		if (MyMem = calloc(1,size+4)) {
			*MyMem = size;
			++MyMem;
			return MyMem;
		}
		else {
			return 0;
		}
	}
	else {
		if (MyMem = malloc(size+4)) {
			*MyMem = size;
			++MyMem;
			return MyMem;
		}
		else {
			return 0;
		}
	}
#else
	if (Chain) {
		if (MyMem = AllocPooled(Chain,size+4)) {
			*MyMem = size;
			++MyMem;
			if (requirements & MEMF_CLEAR) {
				memset(MyMem,0,size);
			}
			return MyMem;
		}
		else {
			return NULL;
		}
	}
	else {
		if (MyMem = AllocVec(size,requirements)) {
			return MyMem;
		}
		else {
			return NULL;
		}
	}
#endif
}

void
MyFreeVec(APTR memptr) {
	ULONG *MyMem;
#ifdef __MPCLI
	MyMem = memptr;
	--MyMem;
	free(MyMem);
#else
	MyMem = memptr;
	if (Chain) {
		--MyMem;
		FreePooled(Chain,MyMem,*MyMem+4);
	}
	else {
		FreeVec(MyMem);
	}
#endif
}

APTR
MyAllocMem(ULONG size,ULONG requirements) {
#ifdef __MPCLI
	if (requirements & MEMF_CLEAR) {
		return calloc(1,size);
	}
	else {
		return malloc(size);
	}
#else
	ULONG *MyMem;
	if (Chain) {
		if (MyMem = AllocPooled(Chain,size)) {
			if (requirements & MEMF_CLEAR) {
				memset(MyMem,0,size);
			}
			return MyMem;
		}
		else {
			return NULL;
		}
	}
	else {
		if (MyMem = AllocMem(size,requirements)) {
			return MyMem;
		}
		else {
			return NULL;
		}
	}
#endif
}

void MyFreeMem(APTR memptr,ULONG size) {
#ifdef __MPCLI
	free(memptr);
#else
	ULONG *MyMem;
	MyMem = memptr;
	if (Chain) {
		FreePooled(Chain,MyMem,size);
	}
	else {
		FreeMem(MyMem,size);
	}
#endif
}

/* Note no free() is needed */
APTR
Mymalloc(ULONG size) {
#ifdef __MPCLI
	return malloc(size);
#else
	if (Chain) {
		return AllocPooled(Chain,size);
	}
	else {
		return malloc(size);
	}
#endif
}

APTR
Mystrdup(UBYTE *str) {
	UBYTE *t;
	if (t = Mymalloc(strlen(str)+1)) {
		strcpy(t,str);
	}
	else {
		return NULL;
	}
}



/* save an image */
BOOL
SaveFile(void) {
	BOOL OkFlag;
#ifndef __MPCLI
	struct DiskObject *MyDiskObject;	/* The Icon */
	char					*iconname;	/* iconname */
	char					*iconname1;	/* alternate iconname */
#endif

	sprintf(FileName,AnimName,f+Start-1);
	sprintf(buffer,"Saving frame %ld",f+Start-1);
	AddMessage(buffer);
	OkFlag = SaveMPImage(FileName,RED,Grey?RED:GREEN,Grey?RED:BLUE,width,height
#ifdef __MPCLI
				);
#else
				,
				MPIS_MODENAME, (ULONG)ModeName,
				MPIS_FORMAT, (ULONG)SaveFormat,
				MPIS_PALETTE, (ULONG)palette,
				MPIS_COLOURS,	(ULONG)Colours,	// 4.8
				MPIS_12BIT, Bit12,
				TAG_END);
	if (OkFlag) {
		if (CreateIcons) {
			/* try to save an Icon */
			if (MyDiskObject = GetDiskObject(FileName)) {
				/* Do not overwrite if already present */
				FreeDiskObject(MyDiskObject);
			}
			else {
				/* Set up two possible names */
				iconname = "ENV:MPMorph/def_pic";
				iconname1 = "ENV:SYS/def_pic";
				/* Try two names, as last resort use default project */
				if ((MyDiskObject = GetDiskObject(iconname)) ||
					 (MyDiskObject = GetDiskObject(iconname1)) ||
					 (MyDiskObject = GetDefDiskObject(WBPROJECT))) {
					PutDiskObject(FileName,MyDiskObject);
					FreeDiskObject(MyDiskObject);
				}
			}
		}
	}
#endif
	if (!OkFlag) {
		Error("Error saving image\n%s","Quit",MPImageErrorMessage(),HE_RSave);
	}
#ifndef __MPCLI
	else {
		/* Post image processing */
		if (strcmp(Postscript,"OFF")) {
			strcpy(buffer,Postscript);
			strcat(buffer," %ld %ld %ld %s");
			sprintf(buffer1,buffer,f+Start-1,Frames,Single,FileName);
			AddMessage("Postscript processing");
			OkFlag = !SendRxMsg(buffer1,FALSE);
		}
	}
#endif
	return OkFlag;
}
