<commit_before>//
// ImageOSG.cpp
//
// Copyright (c) 2001-2005 Virtual Terrain Project
// Free for all uses, see license.txt for details.
//

#include "vtlib/vtlib.h"
#include "vtdata/vtString.h"
#include <osgDB/ReadFile>

//
// Set any of these definitions to use OSG's own support for the various
// image file formats instead of our own.
//
#define USE_OSG_FOR_PNG		1
#define USE_OSG_FOR_BMP		1
#define USE_OSG_FOR_JPG		1

// Simple cache
typedef std::map< vtString, osg::ref_ptr<vtImage> > ImageCache;
ImageCache s_ImageCache;

void vtImageCacheClear()
{
	s_ImageCache.clear();
}

////////////////////////////////////////////////////////////////////////

vtImage::vtImage()
{
	ref();
}

vtImage::vtImage(const char *fname, bool bAllowCache)
{
	ref();
	Read(fname, bAllowCache);
}

vtImage::vtImage(vtDIB *pDIB)
{
	ref();
	m_b16bit = false;
	_CreateFromDIB(pDIB);
}

vtImage::~vtImage()
{
}

bool vtImage::Create(int width, int height, int bitdepth, bool create_palette)
{
	GLenum pixelFormat;
	GLenum dataType = GL_UNSIGNED_BYTE;

	if (bitdepth == 24)
	{
		pixelFormat = GL_RGB;
	}
	else if (bitdepth == 32)
	{
		pixelFormat = GL_RGBA;
	}
	else if (bitdepth == 8)
	{
		pixelFormat = GL_LUMINANCE;
	}
	else
		return false;

	allocateImage(width, height, 1, pixelFormat, dataType);
	m_iRowSize = computeRowWidthInBytes(_s, _pixelFormat, _dataType, _packing);

	return true;
}

vtImage *vtImageRead(const char *fname, bool bAllowCache)
{
	ImageCache::iterator iter;
	vtImage *image;

	iter = s_ImageCache.find(vtString(fname));
	if (iter == s_ImageCache.end())
	{
		// not found.  must try loading;
		image = new vtImage();
		if (image->Read(fname, bAllowCache))
		{
			s_ImageCache[fname] = image; // store in cache
			return image;
		}
		else
			return NULL;
	}
	else
	{
		image = iter->second.get();
		// because we don't use 'smart pointers', we must pretend that this is a
		//  unique instance with its own self-possessed refcount
		image->ref();
		return image;
	}
}

bool vtImage::Read(const char *fname, bool bAllowCache)
{
	m_b16bit = false;
	m_strFilename = fname;

#if !USE_OSG_FOR_BMP
	if (!stricmp(fname + strlen(fname) - 3, "bmp"))
	{
		vtDIB dib;
		if (dib.ReadBMP(fname))
		{
			if (dib.GetDepth() == 8)
			{
				// We are presumably going to use this for a texture, and we
				// don't want to worry about 8-bit color paletted textures.
				vtDIB dib2;
				dib2.Create24From8bit(dib);
				_CreateFromDIB(&dib2);
			}
			else
				_CreateFromDIB(&dib);
		}
	}
	else
#endif

#if !USE_OSG_FOR_JPG
	if (!stricmp(fname + strlen(fname) - 3, "jpg"))
	{
		vtDIB pDIB;
		if (pDIB.ReadJPEG(fname))
		{
			_CreateFromDIB(&pDIB);
		}
	}
	else
#endif

#if !USE_OSG_FOR_PNG
	if (!stricmp(fname + strlen(fname) - 3, "png"))
	{
		_ReadPNG(fname);
	}
	else
#endif

	// try to load with OSG (osgPlugins libraries)
	{
		// important for efficiency: use OSG's cache
#define OPTS osgDB::ReaderWriter::Options
#define HINT OPTS::CacheHintOptions
		osgDB::Registry *reg = osgDB::Registry::instance();
		OPTS *opts;

		opts = reg->getOptions();
		if (!opts) opts = new OPTS;
		int before = (int) opts->getObjectCacheHint();
		if (bAllowCache)
		{
			opts->setObjectCacheHint((HINT) ((opts->getObjectCacheHint()) |
				OPTS::CACHE_IMAGES));
		}
		else
		{
			opts->setObjectCacheHint((HINT) ((opts->getObjectCacheHint()) &
				~(OPTS::CACHE_IMAGES)));
		}
		int after = (int) opts->getObjectCacheHint();
		reg->setOptions(opts);

		// Call OSG to attempt image load.
		osg::ref_ptr<osg::Image> pOsgImage = osgDB::readImageFile(fname);<commit_msg>added extra opts ref() to workaround exit crash<commit_after>//
// ImageOSG.cpp
//
// Copyright (c) 2001-2005 Virtual Terrain Project
// Free for all uses, see license.txt for details.
//

#include "vtlib/vtlib.h"
#include "vtdata/vtString.h"
#include <osgDB/ReadFile>

//
// Set any of these definitions to use OSG's own support for the various
// image file formats instead of our own.
//
#define USE_OSG_FOR_PNG		1
#define USE_OSG_FOR_BMP		1
#define USE_OSG_FOR_JPG		1

// Simple cache
typedef std::map< vtString, osg::ref_ptr<vtImage> > ImageCache;
ImageCache s_ImageCache;

void vtImageCacheClear()
{
	s_ImageCache.clear();
}

////////////////////////////////////////////////////////////////////////

vtImage::vtImage()
{
	ref();
}

vtImage::vtImage(const char *fname, bool bAllowCache)
{
	ref();
	Read(fname, bAllowCache);
}

vtImage::vtImage(vtDIB *pDIB)
{
	ref();
	m_b16bit = false;
	_CreateFromDIB(pDIB);
}

vtImage::~vtImage()
{
}

bool vtImage::Create(int width, int height, int bitdepth, bool create_palette)
{
	GLenum pixelFormat;
	GLenum dataType = GL_UNSIGNED_BYTE;

	if (bitdepth == 24)
	{
		pixelFormat = GL_RGB;
	}
	else if (bitdepth == 32)
	{
		pixelFormat = GL_RGBA;
	}
	else if (bitdepth == 8)
	{
		pixelFormat = GL_LUMINANCE;
	}
	else
		return false;

	allocateImage(width, height, 1, pixelFormat, dataType);
	m_iRowSize = computeRowWidthInBytes(_s, _pixelFormat, _dataType, _packing);

	return true;
}

vtImage *vtImageRead(const char *fname, bool bAllowCache)
{
	ImageCache::iterator iter;
	vtImage *image;

	iter = s_ImageCache.find(vtString(fname));
	if (iter == s_ImageCache.end())
	{
		// not found.  must try loading;
		image = new vtImage();
		if (image->Read(fname, bAllowCache))
		{
			s_ImageCache[fname] = image; // store in cache
			return image;
		}
		else
			return NULL;
	}
	else
	{
		image = iter->second.get();
		// because we don't use 'smart pointers', we must pretend that this is a
		//  unique instance with its own self-possessed refcount
		image->ref();
		return image;
	}
}

bool vtImage::Read(const char *fname, bool bAllowCache)
{
	m_b16bit = false;
	m_strFilename = fname;

#if !USE_OSG_FOR_BMP
	if (!stricmp(fname + strlen(fname) - 3, "bmp"))
	{
		vtDIB dib;
		if (dib.ReadBMP(fname))
		{
			if (dib.GetDepth() == 8)
			{
				// We are presumably going to use this for a texture, and we
				// don't want to worry about 8-bit color paletted textures.
				vtDIB dib2;
				dib2.Create24From8bit(dib);
				_CreateFromDIB(&dib2);
			}
			else
				_CreateFromDIB(&dib);
		}
	}
	else
#endif

#if !USE_OSG_FOR_JPG
	if (!stricmp(fname + strlen(fname) - 3, "jpg"))
	{
		vtDIB pDIB;
		if (pDIB.ReadJPEG(fname))
		{
			_CreateFromDIB(&pDIB);
		}
	}
	else
#endif

#if !USE_OSG_FOR_PNG
	if (!stricmp(fname + strlen(fname) - 3, "png"))
	{
		_ReadPNG(fname);
	}
	else
#endif

	// try to load with OSG (osgPlugins libraries)
	{
		// important for efficiency: use OSG's cache
#define OPTS osgDB::ReaderWriter::Options
#define HINT OPTS::CacheHintOptions
		osgDB::Registry *reg = osgDB::Registry::instance();
		OPTS *opts;

		opts = reg->getOptions();
		if (!opts)
		{
			opts = new OPTS;
			opts->ref();	// workaround!
		}
		int before = (int) opts->getObjectCacheHint();
		if (bAllowCache)
		{
			opts->setObjectCacheHint((HINT) ((opts->getObjectCacheHint()) |
				OPTS::CACHE_IMAGES));
		}
		else
		{
			opts->setObjectCacheHint((HINT) ((opts->getObjectCacheHint()) &
				~(OPTS::CACHE_IMAGES)));
		}
		int after = (int) opts->getObjectCacheHint();
		reg->setOptions(opts);

		// Call OSG to attempt image load.
		osg::ref_ptr<osg::Image> pOsgImage = osgDB::readImageFile(fname);
{'fraction_non_alphanumeric': 0.15651201788708777, 'fraction_numerical': 0.008943543879262158, 'mean_word_length': 3.495603015075377, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2252004', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2758, 'n_words': 654}