1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <IL/il.h>
#include <cstdlib>
#include <fstream>
#include <cstdio>
using namespace std;
std::ifstream g_IFStream;
ILAPIENTRY void _CloseRead_CB(ILHANDLE iHandle)
{
std::ifstream & str = * reinterpret_cast<std::ifstream *>(iHandle);
str.close();
}
ILAPIENTRY ILboolean _Eof_CB(ILHANDLE iHandle)
{
std::ifstream & str = * reinterpret_cast<std::ifstream *>(iHandle);
return str.eof();
}
ILAPIENTRY ILint _GetC_CB(ILHANDLE iHandle)
{
std::ifstream & str = * reinterpret_cast<std::ifstream *>(iHandle);
return str.get();
}
ILAPIENTRY ILHANDLE _OpenRead_CB(ILconst_string iFile)
{
g_IFStream.close();
g_IFStream.open(iFile);
g_IFStream.seekg(0, std::ios_base::beg);
return & g_IFStream;
}
ILAPIENTRY ILint _Read_CB(void * iBuf, ILuint iSize, ILuint iNbElt, ILHANDLE iHandle)
{
std::ifstream & str = * reinterpret_cast<std::ifstream *>(iHandle);
str.read((char *)iBuf, iSize * iNbElt);
return iNbElt;
}
ILAPIENTRY ILint _SeekRead_CB(ILHANDLE iHandle, ILint iOffset, ILint iOrigin)
{
std::ifstream & str = * reinterpret_cast<std::ifstream *>(iHandle);
std::ios_base::seekdir origin = std::ios_base::beg;
switch (iOrigin)
{
case IL_SEEK_SET : origin = std::ios_base::beg; break;
case IL_SEEK_CUR : origin = std::ios_base::cur; break;
case IL_SEEK_END : origin = std::ios_base::end; break;
default : return 1;
}
str.seekg(iOffset, origin);
return str.rdstate();
}
ILAPIENTRY ILint _TellRead_CB(ILHANDLE iHandle)
{
std::ifstream & str = * reinterpret_cast<std::ifstream *>(iHandle);
return str.tellg();
}
int main()
{
// init
ilInit();
ilSetRead(_OpenRead_CB, _CloseRead_CB, _Eof_CB, _GetC_CB, _Read_CB, _SeekRead_CB, _TellRead_CB);
// create image
ILuint im;
ilGenImages(1, & im);
ilBindImage(im);
std::string imFile = "image.jpg";
// load image
ILboolean result = ilLoad(IL_JPG, imFile.c_str());
if (result == IL_TRUE) std::cout << "Image " << imFile << " loaded\n";
else std::cout << "Image " << imFile << " not loaded\n";
// copy image
std::string imCpyFile = "image_copy.bmp";
remove(imCpyFile.c_str());
ilSave(IL_BMP, imCpyFile.c_str());
// uninit
ilDeleteImages(1, & im);
ilShutDown();
return 0;
} |
Partager