/*

			Slide Show Screen Saver for Windows

						Image Loader

		This module is a wrapper for the various format-specific
		image loaders.  It examines the header of the file and
		invokes the correct loader for images in that format.

*/

#include "screensv.h"

char *loadError = NULL;					// Error message for image load

//  LOADIMAGE  --  Load image into memory

int loadImage(FILE *input_file, HGLOBAL *LimageBitmap,
			  int *LsizeX, int *LsizeY)
{
	unsigned char magic[8],
		 jpeg[] = "\377\330\377\340",
		 bmp[] = "BM",
		 gif[] = "GIF",
		 png[8] = {137, 80, 78, 71, 13, 10, 26, 10};

	loadError = NULL;
	if (fread(magic, sizeof magic, 1, input_file) != 1) {
		loadError = "Error reading file type";
		return FALSE;
	}
	rewind(input_file);

	if (memcmp(magic, jpeg, 4) == 0) {
		return loadJPEGimage(input_file, LimageBitmap, LsizeX, LsizeY);
	}

	if (memcmp(magic, bmp, 2) == 0) {
		return loadBMPimage(input_file, LimageBitmap, LsizeX, LsizeY);
	}

	if (memcmp(magic, gif, 3) == 0) {
		return loadGIFimage(input_file, LimageBitmap, LsizeX, LsizeY);
	}

	if (memcmp(magic, png, 8) == 0) {
		return loadPNGimage(input_file, LimageBitmap, LsizeX, LsizeY);
	}

	loadError = "Unknown format";
	return FALSE;				// Unknown format
}