/**
 * \file loadimage.c
 * \brief An image loader that creates an g12 buffer from a file choosing a de-compressor by itself
 *
 * \date Jul 1, 2009
 * \author Alexander Pimenov
 */

#include <stdlib.h>
#include <string.h>

#include "loadimage.h"
#include "ppm.h"
#include "st_raw.h"

#ifdef TIFF_SUPPORTED
#include "tiffloader.h"
#endif


G12Buffer *g12BufferCreateFromFile(char *filename)
{
	char* pgm =  ".pgm";

#ifdef TIFF_SUPPORTED
	char* tif =  ".tif";
	char* tiff = ".tiff";
#endif

	char* raw  = ".raw";

	if (strcmp(filename + strlen(filename) - strlen(pgm), pgm) == 0)
	{
		G12Buffer *result = g12BufferCreateFromPPM(filename);
		if (result != NULL)
			return result;
		return g12BufferCreateFromPPM6(filename);
	}

#ifdef TIFF_SUPPORTED
	if (strcmp(filename + strlen(filename) - strlen(tif) , tif) == 0 ||
		strcmp(filename + strlen(filename) - strlen(tiff), tiff)	== 0 )
	{
		return g12BufferCreateFromTiff(filename);
	}
#endif

	if (strcmp(filename + strlen(filename) - strlen(raw) , raw) == 0)
	{
		return g12BufferCreateFromSTRaw(filename);
	}


	return NULL;
}
