Code: Select all
/*------------------------------------------------------------------------------
| This code should write raster image data in to text file in format which can |
| be than used to store image directly inside code. For example if you want to |
| provide your application with small icons without need to provide image files|
| Using it for displaying larger files is probably not practical. |
| |
| author: arras |
| date: 09.10.2009 |
| using: Irrlicht v 1.5 |
------------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
using namespace std;
#include <irrlicht.h>
using namespace irr;
// Will write array of integers, representing raster image in to the file in
// text format. First number represent color format and next two size.
// Rest are pixels.
void writeIconDataToFile(const u32 *data, const c8 *filename)
{
ofstream out;
out.open(filename);
u32 size = data[1] * data[2] + 3;
if(size > 0)
{
for(u32 n=0; n<size-1; n++) out << data[n] << "u,";
out << data[size-1];
}
out.close();
}
int main()
{
IrrlichtDevice *device = createDevice(video::EDT_OPENGL,
core::dimension2d<s32>(640, 480), 32, false, false, false, 0);
video::IVideoDriver* driver = device->getVideoDriver();
// Load image from file.
video::IImage *image = driver->createImageFromFile("image.png");
video::ITexture *texture = driver->addTexture ("image", image);
// Store image rectangle for later use.
core::rect<s32> rect(core::position2d<s32>(0,0), image->getDimension());
// Size of pixel data.
const u32 size = image->getDimension().Width * image->getDimension().Height;
// Create data and reserve space for additional info in memory.
u32 data[size+3];
// Write color format and size information at the begining.
data[0] = image->getColorFormat();
data[1] = image->getDimension().Width;
data[2] = image->getDimension().Height;
// Write pixel data itself in to array.
u32 n=3;
for(s32 y=0; y<image->getDimension().Height; y++)
for(s32 x=0; x<image->getDimension().Width; x++)
{
data[n] = image->getPixel(x,y).color;
n++;
}
// Create image from data above for comparison.
video::IImage *image2 =
driver->createImageFromData((video::ECOLOR_FORMAT)data[0],
core::dimension2d<s32>(data[1], data[2]), &data[3]);
video::ITexture *texture2 = driver->addTexture ("image2", image2);
// Write image data in to file.
writeIconDataToFile(data, "output.txt");
// Open that file afther executing code and put its context here
// (below is image I was using):
u32 icon[] = {3u,8u,8u,16777215u,16777215u,16777215u,16777215u,16777215u,
16777215u,419430400u,1996488704u,16777215u,16777215u,16777215u,16777216u,
922746880u,3271557120u,4278190080u,3791650816u,16777215u,134217728u,
1560281088u,3942645760u,4278190080u,4278190080u,4278190080u,3791650816u,
2432696320u,4278190080u,4278190080u,4278190080u,4278190080u,4278190080u,
4278190080u,3791650816u,1895825408u,4143972352u,4278190080u,4278190080u,
4278190080u,4278190080u,4278190080u,3791650816u,16777215u,67108864u,
1342177280u,3825205248u,4278190080u,4278190080u,4278190080u,3791650816u,
16777215u,16777215u,16777215u,16777216u,889192448u,3305111552u,
4278190080u,3791650816u,16777215u,16777215u,16777215u,16777215u,16777215u,
16777215u,503316480u,2281701376u};
// Create image from data above.
video::IImage *image3 = driver->createImageFromData((video::ECOLOR_FORMAT)icon[0],
core::dimension2d<s32>(icon[1], icon[2]), &icon[3]);
video::ITexture *texture3 = driver->addTexture ("image3", image3);
// Draw all three images to see if it works (all three should be the same).
while(device->run())
{
driver->beginScene(true, true, video::SColor(0,200,200,200));
driver->draw2DImage(texture, core::position2d<s32>(100,100),
rect, 0, video::SColor(255, 255, 255, 255), true);
driver->draw2DImage(texture2, core::position2d<s32>(200,100),
rect, 0, video::SColor(255, 255, 255, 255), true);
driver->draw2DImage(texture3, core::position2d<s32>(300,100),
rect, 0, video::SColor(255, 255, 255, 255), true);
driver->endScene();
}
device->drop();
return 0;
}