#import "ToyWinGIF.h"
#import "gif.h"

@implementation ToyWinGIF

- (commonInfo *)drawToyWin:(const char *)fileName type:(int)type
	map:(unsigned char **)map err:(int *)err
{
	FILE *fp;
	gifHeader *gh = NULL;
	commonInfo *cinf = NULL;
	int alert;

	*err = 0;
	if ((fp = fopen(fileName, "r")) == NULL) {
		*err = Err_OPEN;
		return NULL;
	}
	if ((gh = loadGifHeader(fp, err)) == NULL) {
		(void)fclose(fp);
		return NULL;
	}

	if (!makeMapOnly)
	    [self initLocateWindow:fileName width:gh->width height:gh->height];

/* Bitmap data of planes in 'map[]' is one block of memory area.
   map[0] is beginning of the area, and map[1] = map[0] + (size of plane),
   and so on.
   The area of map[0] and (commonInfo *)cinf are kept in an object of
   ToyView, and freed by it.
*/
	alert = gifGetImage(fp, gh, map);
	cinf = gifInfo(gh);
	freeGifHeader(gh);
	(void)fclose(fp);
	if (alert) warnAlert(fileName, alert);
	if (makeMapOnly)
		return cinf;
	if ([self drawView:map info: cinf] == nil)
		*err = -1;
	return cinf;
}

@end
