// TSMorph - 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 precompiled header if not already
#ifndef TSMOPRH_H
#include "TSMorph.h"
#endif

/* version of loadbrush which first check file exists
 * to prevent any error message being displayed if not present
 */
int
myloadbrush(struct ILBMInfo *info,UBYTE *filename) {
	BPTR fh;
	if (fh = Open(filename,MODE_OLDFILE)) {
		Close(fh);
		return loadbrush(info,filename);
	}
	return 1;
}

/* load a pointer from a 4 colour ILBM file.
 *
 * ilbm     - ILBM stuff
 * address  - address to store sprite
 * filename - name of iff file to load
 */
void
mycopysprite(struct ILBMInfo *ilbm,UWORD *address,UBYTE *filename) {
	int i;
	UWORD *p0,*p1,*o0;
	// try and load brush first
	if (!myloadbrush(ilbm,filename)) {
		// if ok then convert to pointer format
		p0 = (UWORD *)ilbm->brbitmap->Planes[0];
		p1 = (UWORD *)ilbm->brbitmap->Planes[1];
		o0 = address + 2;
		for (i = 0;
			  i < 32;
			  ++i) {
			*o0++ = *p0++;
			*o0++ = *p1++;
		}
	}
	// close and unload brush
	closeifile(&(ilbm->ParseInfo));
	unloadbrush(ilbm);
}

/* load an image from a 4 colour ILBM file
 *
 * ilbm     - ILBM stuff
 * im       - pointer to image to store data
 * filename - name of iff file to load
 * size     - size of image bit plane
 */
void
mycopybrush(struct ILBMInfo *ilbm,struct Image *im,UBYTE *filename,UWORD size) {
	// try and load brush first
	if (!myloadbrush(ilbm,filename)) {
		// if ok then copy to image data
		CopyMem(im->ImageData,ilbm->brbitmap->Planes[0],size);
		CopyMem(((UBYTE *)im->ImageData)+size,ilbm->brbitmap->Planes[1],size);
	}
	// close and unload brush
	closeifile(&(ilbm->ParseInfo));
	unloadbrush(ilbm);
}

/* Invert an image for a highlighted gadget
 * this is used rather than complement to look good on >4 colour screen
 * (There must be a better way)
 *
 * im    - Image pointer, note that the memory for the image data is twice the required size
 * size  - size of image data
 */
void
invertbrush(struct Image *im,UWORD size) {
	UWORD *a,*b;
	UWORD i;
	a = im->ImageData,
	b = im->ImageData+size;
	for (i = 0;
		  i < size;
		  i++) {
		*b++ = ~*a++;
	}
}

/* Load pointer and
 * gadget images
 */
void
LoadBrushes(void) {
	struct ILBMInfo	ilbm = {0};		// ILBM info for iff read
	BPTR 					lock, cd;		// lock on new directory and old directory
	if (ilbm.ParseInfo.iff = AllocIFF()) {	// get IFF stuff
		ilbm.ParseInfo.propchks = props;		// and initialise chunks required
		ilbm.ParseInfo.collectchks = nowt;
		ilbm.ParseInfo.stopchks = stops;
		if (lock = Lock("TSMorph:brush",ACCESS_READ)) {	// try and change to this directory
			cd = CurrentDir(lock);
			// Load all image files
			mycopybrush(&ilbm,&Add_im,"add",204);
			mycopybrush(&ilbm,&Del_im,"del",204);
			mycopybrush(&ilbm,&Link_im,"link",204);
			mycopybrush(&ilbm,&None_im,"none",204);
			mycopybrush(&ilbm,&One_im,"one",204);
			mycopybrush(&ilbm,&Rel_im,"rel",204);
			mycopybrush(&ilbm,&Two_im,"two",204);
			mycopybrush(&ilbm,&Unlink_im,"unlink",204);
			mycopybrush(&ilbm,&st_im,"1st",30);
			mycopybrush(&ilbm,&prev_im,"prev",30);
			mycopybrush(&ilbm,&goto_im,"goto",30);
			mycopybrush(&ilbm,&next_im,"next",30);
			mycopybrush(&ilbm,&last_im,"last",30);
			// switch back to old dir
			CurrentDir(cd);
			UnLock(lock);
		}
		// invert all gadgets for hilite
		invertbrush(&Add_im,204);
		invertbrush(&Del_im,204);
		invertbrush(&Link_im,204);
		invertbrush(&None_im,204);
		invertbrush(&One_im,204);
		invertbrush(&Rel_im,204);
		invertbrush(&Two_im,204);
		invertbrush(&Unlink_im,204);
		invertbrush(&st_im,30);
		invertbrush(&prev_im,30);
		invertbrush(&goto_im,30);
		invertbrush(&next_im,30);
		invertbrush(&last_im,30);
		if (lock = Lock("TSMorph:cursor",ACCESS_READ)) {	// try and change to this directory
			cd = CurrentDir(lock);
			// Load all pointer files
			mycopysprite(&ilbm,Add,"XAdd");
			mycopysprite(&ilbm,Del,"XDel");
			mycopysprite(&ilbm,L1,"XL1");
			mycopysprite(&ilbm,L2,"XL2");
			mycopysprite(&ilbm,Mov,"XMov");
			mycopysprite(&ilbm,One,"XOne");
			mycopysprite(&ilbm,Rel,"XRel");
			mycopysprite(&ilbm,Two,"XTwo");
			mycopysprite(&ilbm,U1,"XU1");
			mycopysprite(&ilbm,U2,"XU2");
			// switch back to old dir
			CurrentDir(cd);
			UnLock(lock);
		}
		// free IFF stuff
		FreeIFF(ilbm.ParseInfo.iff);
	}
}	
