/*****************************************************************************

File: SeperateCubemappedPlanetReader.h
Desc: A class for reading cubemapped planetary data from seperate heightfiles
		
Date: 2005/01/01

Author: Wim Lemkens

*****************************************************************************/

#ifndef SEPERATECUBEMAPPEDPLANETREADER_H
#define SEPERATECUBEMAPPEDPLANETREADER_H

#include "CubemappedPlanetReader.h"
#include "ClipmapConfig.h"
#include <OgreStringConverter.h>
#include <OgreImage.h>
#include <OgreVector2.h>

namespace Ogre {

class SeperateCubemappedPlanetReader : public CubemappedPlanetReader {
public:

	/**
	 * Constructor of the geometryreader
	 */
	SeperateCubemappedPlanetReader();

	/**
	 * Destructor of the geometryreader
	 */
	virtual ~SeperateCubemappedPlanetReader();
	
	/**
	 * Preprocess the heightmaps and save them to file
	 */
	void preprocessFiles(String *filenames);
	
	/**
	 * Initialises the filereader with the correct configuration
	 */
	bool initialise(const String& filename);
	
	/**
	 * Return how many lods of geometry there are in the file
	 */
	int getGeometryDepth() const;
	
protected:

	/**
	 * The files of where the heightmaps reside
	 */
	FILE *heightmapsFile[6];
	/**
	* The files of where the heightmaps reside
	*/
	Image *heightmapsImage[6];

	
	/**
	 * Read the height from file of the vertex at the given coordinates
	 * @param x		The x-coordinate of the vertex to return the height of
	 * @param y		The y-coordinate of the vertex to return the height of
	 * @param level	The level of detail from which the height of the vertex
	 * 				is to be returned
	 * @param map	The side of the cubemap from which the height is to be 
	 * 				returned
	 */
	virtual Real readHeight(int x, int y, int level, int map) const;

	/**
	 * Opens the 6 given files for input
	 */
	virtual void openFiles(String *filenames);
	
	/**
	 * Creates the 6 given files for input and output
	 */
	virtual void openFilesForWriting(String *filenames);
	
	/**
	 * Calculates the texturecoordinates for the given vertex
	 */
	virtual Vector2 getTextureCoordinates(int x, int y, int level, int map) const;

	/**
	 * Preprocess the given heightmap and save write it to the given file
	 */
	void preprocessFile(int imageIdx);

	/**
	 * Interpolates all pixels of a given level and write the pixels to file
	 */
	void interpolateLevel(int level, unsigned char* bitmap, int size, int depth,FILE *&heightmap);
	
	/**
	 * Bicubic resample the given pixel with a given sampeling range
	 */
	Real resamplePixel(int x, int y, int range, unsigned char* bitmap, int size);
	
	/**
	 * Save the given pixel to file
	 */
	void savePixel(int x, int y, int level, Real height, FILE *&mapfile);
	
	/**
	 * Returns true if this map is a valid map
	 */
	bool isValidMap(const String &filename,  int imageIdx);
	
	/**
	 * Returns the size (width) of an image
	 */
	int getSizeFromImage(String file);
	
	/**
	 * Returns the size of the first map of the preprocessed file
	 */
	int getSizeFromFile();

	/**
	 * Saves the header of the preprocessed file to disk
	 */
	void saveHeader(FILE* &mapfile, int size);
	
	int getVertexPosition(int x,int y,int level,int map) const;
};

} // namespace Ogre

#endif // SEPERATECUBEMAPPEDPLANETREADER_H
