TexConv/ILImageSupport.cpp

225 lines
4.8 KiB
C++
Raw Normal View History

2019-12-02 22:10:49 +08:00
//注起名为ILImageSupport是为了避免与IL中现有的ilimage冲突
#include"ILImage.h"
#include<IL/ilu.h>
#include<hgl/log/LogInfo.h>
2019-12-31 13:44:09 +08:00
#include<hgl/filesystem/FileSystem.h>
2019-12-02 22:10:49 +08:00
using namespace hgl;
2019-12-03 15:31:51 +08:00
namespace
{
const OSString GetILFormatName(const ILuint format)
{
#define IL_FMT2NAME(name) if(format==IL_##name)return OS_TEXT(#name);
IL_FMT2NAME(COLOR_INDEX)
IL_FMT2NAME(ALPHA)
IL_FMT2NAME(RGB)
IL_FMT2NAME(RGBA)
IL_FMT2NAME(BGR)
IL_FMT2NAME(BGRA)
IL_FMT2NAME(LUMINANCE)
IL_FMT2NAME(LUMINANCE_ALPHA)
#undef IL_FMT2NAME
return OS_TEXT("Error format");
}
const OSString GetILTypeName(const ILuint type)
{
#define IL_TYPE2NAME(name) if(type==IL_##name)return OS_TEXT(#name);
IL_TYPE2NAME(BYTE)
IL_TYPE2NAME(UNSIGNED_BYTE)
IL_TYPE2NAME(SHORT)
IL_TYPE2NAME(UNSIGNED_SHORT)
IL_TYPE2NAME(INT)
IL_TYPE2NAME(UNSIGNED_INT)
IL_TYPE2NAME(FLOAT)
IL_TYPE2NAME(DOUBLE)
IL_TYPE2NAME(HALF)
#undef IL_TYPE2NAME
return OS_TEXT("Error type");
}
}//namespace
2019-12-02 22:10:49 +08:00
ILImage::ILImage()
{
ilGenImages(1,&il_index);
}
ILImage::~ILImage()
{
ilDeleteImages(1,&il_index);
}
2019-12-31 13:44:09 +08:00
bool ILImage::Create(ILuint w,ILuint h,ILuint c,ILuint t,void *data)
{
2019-12-31 23:05:15 +08:00
const ILenum format_list[]=
2019-12-31 13:44:09 +08:00
{
IL_LUMINANCE,
IL_LUMINANCE_ALPHA,
IL_RGB,
IL_RGBA,
};
if(c<1||c>4)return(false);
Bind();
2019-12-31 23:05:15 +08:00
if(!ilTexImage(w,h,1,c,format_list[c-1],t,data))
return(false);
iluFlipImage();
return(true);
2019-12-31 13:44:09 +08:00
}
bool ILImage::SaveFile(const OSString &filename)
{
Bind();
ilEnable(IL_FILE_OVERWRITE);
return ilSaveImage(filename.c_str());
}
2019-12-02 22:10:49 +08:00
void ILImage::Bind()
{
ilBindImage(il_index);
}
2019-12-30 16:48:15 +08:00
bool ILImage::Resize(uint nw,uint nh)
{
2019-12-31 23:05:15 +08:00
if(nw==il_width&&nh==il_height)return(true);
2019-12-30 16:48:15 +08:00
if(nw==0||nh==0)return(false);
2019-12-31 23:05:15 +08:00
Bind();
if(!iluScale(nw,nh,il_depth))
return(false);
il_width=nw;
il_height=nh;
return(true);
2019-12-30 16:48:15 +08:00
}
2019-12-02 22:10:49 +08:00
bool ILImage::Convert(ILuint format,ILuint type)
{
if(il_format==format
&&il_type==type)return(true);
Bind();
if(!ilConvertImage(format,type))
return(false);
il_format=format;
il_type=type;
return(true);
}
bool ILImage::LoadFile(const OSString &filename)
{
Bind();
2019-12-31 13:44:09 +08:00
if(!filesystem::FileExist(filename))
{
LOG_INFO(OS_TEXT("Can't find filename: ")+filename);
return(false);
}
2019-12-02 22:10:49 +08:00
if(!ilLoadImage(filename.c_str()))
return(false);
2019-12-03 15:31:51 +08:00
LOG_INFO(OS_TEXT("File: ")+filename);
2019-12-02 22:10:49 +08:00
il_width =ilGetInteger(IL_IMAGE_WIDTH);
il_height =ilGetInteger(IL_IMAGE_HEIGHT);
2019-12-30 16:48:15 +08:00
il_depth =ilGetInteger(IL_IMAGE_DEPTH);
2019-12-02 22:10:49 +08:00
il_bit =ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
il_format =ilGetInteger(IL_IMAGE_FORMAT);
il_type =ilGetInteger(IL_IMAGE_TYPE);
if(ilGetInteger(IL_IMAGE_ORIGIN)==IL_ORIGIN_LOWER_LEFT)
iluFlipImage();
LOG_INFO(OS_TEXT("\t width: ")+OSString(il_width));
LOG_INFO(OS_TEXT("\theight: ")+OSString(il_height));
2019-12-30 16:48:15 +08:00
LOG_INFO(OS_TEXT("\t depth: ")+OSString(il_depth));
2019-12-02 22:10:49 +08:00
LOG_INFO(OS_TEXT("\t bit: ")+OSString(il_bit));
2019-12-03 15:31:51 +08:00
LOG_INFO(OS_TEXT("\tformat: ")+GetILFormatName(il_format));
LOG_INFO(OS_TEXT("\t type: ")+GetILTypeName(il_type));
2019-12-02 22:10:49 +08:00
if(il_format==IL_COLOR_INDEX)
{
uint il_pattle=ilGetInteger(IL_PALETTE_TYPE);
2019-12-04 11:06:57 +08:00
if(il_pattle==IL_PAL_RGB24||il_pattle==IL_PAL_BGR24
||il_pattle==IL_PAL_RGB32||il_pattle==IL_PAL_BGR32)
2019-12-02 22:10:49 +08:00
{
2019-12-04 11:06:57 +08:00
channel_count=3;
2019-12-02 22:10:49 +08:00
}
else
if(il_pattle==IL_PAL_RGBA32||il_pattle==IL_PAL_BGRA32)
{
2019-12-04 11:06:57 +08:00
channel_count=4;
2019-12-02 22:10:49 +08:00
}
else
{
LOG_ERROR("Don't support the pattle format.");
return(false);
}
}
if(il_format==IL_LUMINANCE||il_format==IL_ALPHA)channel_count=1;else
if(il_format==IL_LUMINANCE_ALPHA) channel_count=2;else
if(il_format==IL_RGB||il_format==IL_BGR) channel_count=3;else
if(il_format==IL_RGBA||il_format==IL_BGRA) channel_count=4;else
channel_count=0;
return(true);
}
2019-12-31 13:44:09 +08:00
void ILImage::ToRGB(ILuint type)
{
if(il_format!=IL_RGB)
Convert(IL_RGB,type);
}
void ILImage::ToGray(ILuint type)
{
if(il_format!=IL_LUMINANCE)
Convert(IL_LUMINANCE,type);
}
2019-12-02 22:10:49 +08:00
void *ILImage::GetR(ILuint type)
{
Bind();
if(il_format==IL_ALPHA)return ilGetAlpha(type);
if(il_format==IL_LUMINANCE)
{
if(il_type!=type)
if(!Convert(il_format,type))
return(nullptr);
2019-12-03 21:17:20 +08:00
return ilGetData();
2019-12-02 22:10:49 +08:00
}
return(nullptr);
}
void *ILImage::GetData(ILuint format,ILuint type)
{
Bind();
if(il_format!=format||il_type!=type)
if(!Convert(format,type))
return nullptr;
return ilGetData();
}