#import "ToyWinBMP.h"
#import "bmp.h"

@implementation ToyWinBMP

- (commonInfo *)drawToyWin:(const char *)fileName type:(int)type
	map:(unsigned char **)map err:(int *)err
{
	FILE *fp;
	bmpHeader *bh = NULL;
	commonInfo *cinf = NULL;
	int bits = 8, alert;

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

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

/* 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. But, if the image is monochrome, map[1] is NULL.
   The area of map[0] and (commonInfo *)cinf are kept in an object of
   ToyView, and freed by it.
*/
	alert = bmpGetImage(fp, bh, &bits, map);
	cinf = bmpInfo(bh, bits, (map[1] == NULL));
	freeBmpHeader(bh);
	(void)fclose(fp);
	if (alert) /* Alert Only */
		warnAlert(fileName, alert);
	if (makeMapOnly)
		return cinf;
	if ([self drawView:map info: cinf] == nil)
		*err = -1;
	return cinf;
}

@end
