先看看 MipmapCreate() 建立的效果怎樣,此圖是將 Mipmap 各層的圖片疊加在一起:
看起來效果還不錯,這測試程式使用 vs2008 編寫,源碼下載:
https://www.dropbox.com/s/q3nlvb7qwkr4wg8/MipmapCreateTest.rar
http://pan.baidu.com/s/1i3BUiJ7
要編譯請先看一下內附的 ReadMe.txt 有關 DevIL 的說明。
以下列出程式碼:
#include <IL\il.h>
#include <io.h>
#include <stdio.h>
#include <sys/stat.h>
#include <iostream>
#include <sstream>
using namespace std;
// 以下兩個函數定義在 MipmapCreate.cpp
bool MipmapCreate(unsigned char ***Mipmap, unsigned int **WidthHeight,
unsigned int *LevelNum,
const unsigned char *SrcMap, const unsigned int SrcWidth,
const unsigned int SrcHeight, const unsigned int bpp);
void MipmapDelete(unsigned char **Mipmap, unsigned int *WidthHeight, unsigned int LevelNum);
bool dir_exists (char * dirName)
{
struct stat buf;
if ( stat ( dirName, &buf ) == 0 && buf.st_mode & _S_IFDIR)
return true;
return false;
}
bool file_exists (char * fileName)
{
if( (_access( fileName, 0 )) != -1 )
return true;
return false;
}
string ults(unsigned int v)
{
stringstream ss;
ss << v;
return ss.str();
}
int main(int argc, char* argv[])
{
ILuint ImagesID;
ilInit();
ilEnable(IL_ORIGIN_SET);
ilGenImages(1, &ImagesID);
ilBindImage(ImagesID);
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
char input[_MAX_PATH];
string SrcFileName;
do
{
do
{
cout << "請輸入圖檔來源: ";
cin.getline(input,_MAX_PATH);
}while(file_exists(input) == false);
SrcFileName = input;
}while(ilLoadImage(SrcFileName.c_str()) == false);
string TargetDirName;
do
{
cout << "請輸入 Mipmap 圖檔存放目錄: ";
cin.getline(input,_MAX_PATH);
}while(dir_exists(input) == false);
TargetDirName = input;
//ILint format = ilGetInteger(IL_IMAGE_FORMAT);
//ILint bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
ILint width = ilGetInteger(IL_IMAGE_WIDTH); // getting image width
ILint height = ilGetInteger(IL_IMAGE_HEIGHT); // and height
ILubyte *SrcMap = new ILubyte[width*height*4];
ilCopyPixels(0, 0, 0, width, height, 1, IL_RGBA, IL_UNSIGNED_BYTE, SrcMap);
unsigned char **Mipmap;
unsigned int *WidthHeight;
unsigned int LevelNum;
MipmapCreate(&Mipmap, &WidthHeight, &LevelNum, SrcMap, width, height, 4);
_splitpath( SrcFileName.c_str(), drive, dir, fname, ext );
for(unsigned int n = 0; n < LevelNum; ++n)
{
string TargetFileName = TargetDirName + '\\' + fname + '_' +
ults(WidthHeight[n*2]) + 'x' + ults(WidthHeight[n*2+1]) + ".png";
// 若用這個圖片會疊在一起
// ilSetPixels(0, 0, 0, WidthHeight[n*2], WidthHeight[n*2+1], 1,
// IL_RGBA, IL_UNSIGNED_BYTE, Mipmap[n]);
ilTexImage(WidthHeight[n*2], WidthHeight[n*2+1], 0, 4, IL_RGBA,
IL_UNSIGNED_BYTE, Mipmap[n]);
ilSaveImage(TargetFileName.c_str());
}
MipmapDelete(Mipmap, WidthHeight, LevelNum);
delete [] SrcMap;
ilDeleteImages(1, &ImagesID);
return 0;
}
沒有留言:
張貼留言