/*
*
* Copyright (C) 2007 Loic Dachary <loic@dachary.org>
* Copyright (C) 2004, 2005, 2006 Mekensleep
*
* Mekensleep
* 24 rue vieille du temple
* 75004 Paris
*       licensing@mekensleep.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
*
* Authors:
*  Igor Kravtchenko <igor@tsarevitch.org>
*
*/

#ifndef __OSGCAL__IMAGECACHE_H__
#define __OSGCAL__IMAGECACHE_H__

#include <osgCal/Export>

#include <osgDB/ReadFile>

#include <sstream>
#include <istream>

namespace osgCal {

/*
class memorystream : public std::stringbuf {
public:
	memorystream(char *ptr, int size) {
		pubsetbuf(ptr, size);
		setg(ptr, ptr, ptr + size);
	};
};
*/
struct ReadImageFunctor : public osgDB::Registry::ReadFunctor {

	ReadImageFunctor(const std::string& filename, const osgDB::ReaderWriter::Options* options):ReadFunctor(filename,options) {}
	~ReadImageFunctor() { }

	virtual osgDB::ReaderWriter::ReadResult doRead(osgDB::ReaderWriter& rw) const;
	virtual bool isValid(osgDB::ReaderWriter::ReadResult& readResult) const { return readResult.validImage(); }
	virtual bool isValid(osg::Object* object) const { return dynamic_cast<osg::Image*>(object)!=0;  }

	//memorystream *_memstream;
	mutable std::istringstream _stream;
	//char *_ptr;
};

class OSGCAL_EXPORT ImageCache {
public:

	static void* getOrLoadBinary(const std::string &filename);
	static osg::Image* loadImage(const std::string &filename);
	static void removeImage(const std::string &filename);
	
protected:
	static ReadImageFunctor* getOrCreateRIF(const std::string &filename);
};

} // namespace osgCal

#endif
