Saving Textures to File

What it says on the tin: a place to discuss proposed new features.
Post Reply
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
Contact:

Saving Textures to File

Post by KungFooMasta » Thu Jan 01, 2009 12:11 am

I thought I had this working, but I'm getting a crash, it seems to be related to memory allocation:

Code: Select all

	Ogre::TexturePtr Text::getFontTexture(Ogre::FontPtr fp)
	{
		// Make sure the font is loaded
		fp->load();

		return 
			static_cast<Ogre::TexturePtr>
			(
				Ogre::TextureManager::getSingleton().getByName(fp->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName())
			);
	}

	void Text::saveFontTextureToFile(Ogre::FontPtr fp, const Ogre::String& fileName)
	{
		Ogre::TexturePtr tp = getFontTexture(fp);
		
		// Declare buffer
		const size_t buffSize = tp->getSize();
		unsigned char *data = OGRE_ALLOC_T(unsigned char,buffSize,Ogre::MEMCATEGORY_GENERAL);
		
		// Clear buffer
		memset(data, 0, buffSize);

		// Setup Image with correct settings
		Ogre::Image i;
		i.loadDynamicImage(data, tp->getWidth(), tp->getHeight(), 1, Ogre::PF_R8G8B8A8, true);
		
		// Copy Texture buffer contents to image buffer
		Ogre::HardwarePixelBufferSharedPtr buf = tp->getBuffer();		
		const Ogre::PixelBox destBox = i.getPixelBox();
		buf->blitToMemory(destBox);
		
		// Save to disk!
		i.save(fileName);
	}
It would be really useful if we could have a convenience method to save Textures to file. Also, if anybody could point out whats wrong with my code that would also help me out. :)
0 x
Creator of QuickGUI!

User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
Contact:

Re: Saving Textures to File

Post by KungFooMasta » Thu Jan 01, 2009 12:14 am

Ok the problem was really simple, my buffer size was wrong, I needed this line:

Code: Select all

const size_t buffSize = (tp->getWidth() * tp->getHeight() * 4);
Still, I think a convenience method for saving Ogre::Texture to file would be a good addition to the lib.
0 x
Creator of QuickGUI!

Post Reply