ITexture -> IImage & IImage -> ITexture

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
white tiger
Posts: 269
Joined: Tue Oct 31, 2006 3:24 pm
Contact:

ITexture -> IImage & IImage -> ITexture

Post by white tiger »

Here are two functions to convert an IImage into an ITexture and viceversa. Remember to ALWAYS drop the returned pointer. I've made it for a class to manipulate a texture\image.

For example with these functions you can serialize an ITexture to a file. Simply convert it to an IImage and call video::IVideoDriver::writeImageToFile

Hope that they helps :wink:

Examples:

Code: Select all

video::ITexture* texture = driver->getTexture("rigell.bmp");
video::IImage* image = TextureImage(texture);
[.......]
image->drop();

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

video::IImage* image = driver->createImageFromFile("rigell.bmp");
video::ITexture* texture = ImageTexture(image);
[.......]
texture->drop();
Header:

Code: Select all

		//Convert a texture to an image. Remember to drop the returned pointer
		video::IImage* TextureImage(video::ITexture* texture);

		//Convert an image to a texture. Remember to drop the returned pointer
		video::ITexture* ImageTexture(video::IImage* image, core::stringc name=core::stringc(""));
Source:

Code: Select all

video::IImage* raf::conv::TextureImage(video::ITexture* texture) {

	video::IImage* image = driver->createImageFromData (
		texture->getColorFormat(),
		texture->getSize(),
		texture->lock(),
		false  //copy mem
		);

	texture->unlock();
	return image;

}

video::ITexture* raf::conv::ImageTexture(video::IImage* image, core::stringc name) {

	video::ITexture* texture = driver->addTexture(name.c_str(),image);
	texture->grab();
	return texture;

}
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

Thanks, this code is a great resource
Post Reply