Merge branch 'master' of https://github.com/hyzboy/TexConv into HEAD

This commit is contained in:
hyzboy 2021-05-22 18:29:41 +08:00
commit bc3e141a34
4 changed files with 409 additions and 44 deletions

342
Bitmap.h Normal file
View File

@ -0,0 +1,342 @@
#ifndef HGL_GRAPH_BITMAP2D_INCLUDE
#define HGL_GRAPH_BITMAP2D_INCLUDE
#include<hgl/type/DataType.h>
#include"pixel_format.h"
namespace hgl
{
namespace graph
{
class Bitmap
{
protected:
uint32 width,height;
uint32 channel;
uint32 bit;
uint8 component[4]; //成份排列顺序
uint32 total_pixels;
uint32 line_bytes;
uint32 total_bytes;
uint8 *data;
public:
const uint32 GetWidth()const{return width;}
const uint32 GetHeight()const{return height;}
const uint32 GetCahnnel()const{return channel;}
const uint32 GetBit()const{return bit;}
const uint32 GetTotalPixels()const{return total_pixels;}
const uint32 GetTotalBytes()const{return total_bytes;}
void *GetData(){return data;}
public:
Bitmap()
{
hgl_zero(*this);
}
Bitmap(const uint32 w,const uint32 h,const uint32 c,const uint32 b)
{
width=w;
height=h;
channel=c;
bit=b;
total_pixels=width*height;
line_bytes=(width*channel*bit)>>3;
total_bytes=line_bytes*height;
data=new uint8[total_bytes];
}
virtual ~Bitmap()
{
SAFE_CLEAR_ARRAY(data);
}
void SetComponentOrder(int n,const uint8 *c)
{
if(n<0||n>4)return;
if(!c)return;
memcpy(component,c,n);
}
void Flip()
{
uint8 *tmp=new uint8[line_bytes];
uint8 *tp=tmp;
uint8 *bp=tmp+line_bytes*(height-1);
while(tp<bp)
{
memcpy(tmp,tp,line_bytes);
memcpy(tp,bp,line_bytes);
memcpy(bp,tmp,line_bytes);
tp+=line_bytes;
bp-=line_bytes;
}
delete[] tmp;
}
};//class Bitmap2D
template<typename T>
class BitmapUInteger:public Bitmap
{
public:
using Bitmap::Bitmap;
virtual ~BitmapUInteger()=default;
BitmapUInteger<T> *Half()
{
}
};
template<typename T>
class BitmapR:public Bitmap
{
T *pixels;
public:
BitmapR():Bitmap()
{
pixels=nullptr;
}
BitmapR(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,1,b)
{
pixels=data;
}
virtual ~BitmapR()=default;
};//class BitmapR:public Bitmap
template<typename T>
class BitmapRG:public Bitmap
{
T *pixels;
public:
BitmapRG():Bitmap()
{
pixels=nullptr;
}
BitmapRG(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,2,b)
{
pixels=data;
}
virtual ~BitmapRG()=default;
};//class BitmapRG:public Bitmap
enum class RGBOrder
{
RGB=0,
BGR=1,
};//
template<typename T>
class BitmapRGB:public Bitmap
{
T *pixels;
RGBOrder component_order;
public:
BitmapRGB():Bitmap()
{
pixels=nullptr;
}
BitmapRGB(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,3,b)
{
pixels=data;
}
virtual ~BitmapRGB()=default;
bool SetComponentOrder(RGBOrder order)
{
if(order==RGBOrder::RGB)Bitmap::SetComponent(3,"RGB");else
if(order==RGBOrder::BGR)Bitmap::SetComponent(3,"BGR");else
return(false);
component_order=order;
return(true);
}
void ChangeComponentOrder(RGBOrder order)
{
if(component_order==order)return;
T *p=pixels;
T temp;
for(uint32 i=0;i<total_pixels;i++)
{
temp=p[0];
p[0]=p[2];
p[2]=temp;
p+=3;
}
SetComponentOrder(order);
}
void FlipG(); //Nomral中经常需要翻转Y值
};//class BitmapRGB:public Bitmap
template<> void BitmapRGB<uint8>::FlipG()
{
uint8 *p=pixels+1;
for(uint i=0;i<total_pixels;i++)
{
*p=0xFF-(*p);
p+=3;
}
}
template<> void BitmapRGB<uint16>::FlipG()
{
uint16 *p=pixels+1;
for(uint i=0;i<total_pixels;i++)
{
*p=0xFFFF-(*p);
p+=3;
}
}
template<> void BitmapRGB<uint32>::FlipG()
{
uint32 *p=pixels+1;
for(uint i=0;i<total_pixels;i++)
{
*p=0xFFFFFFFF-(*p);
p+=3;
}
}
template<> void BitmapRGB<float>::FlipG()
{
float *p=pixels+1;
for(uint i=0;i<total_pixels;i++)
{
*p=-(*p);
p+=3;
}
}
template<> void BitmapRGB<double>::FlipG()
{
double *p=pixels+1;
for(uint i=0;i<total_pixels;i++)
{
*p=-(*p);
p+=3;
}
}
enum class RGBAOrder
{
RGBA=0,
BGRA=1,
ARGB=2,
ABGR=3
};
template<typename T>
class BitmapRGBA:public Bitmap
{
T *pixels;
RGBAOrder component_order;
public:
BitmapRGBA():Bitmap()
{
pixels=nullptr;
}
BitmapRGBA(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,4,b)
{
pixels=data;
}
virtual ~BitmapRGBA()=default;
bool SetComponentOrder(RGBAOrder order)
{
if(order==RGBAOrder::RGBA)Bitmap::SetComponent(4,"RGBA",b);else
if(order==RGBAOrder::BGRA)Bitmap::SetComponent(4,"BGRA",b);else
if(order==RGBAOrder::ARGB)Bitmap::SetComponent(4,"ARGB",b);else
if(order==RGBAOrder::ABGR)Bitmap::SetComponent(4,"ABGR",b);else
return(false);
component_order=order;
return(true);
}
void ChangeComponentOrder(RGBAOrder order)
{
if(component_order==order)return;
T *p=pixels;
T temp;
if((component_order==RGBAOrder::RGBA&&order==RGBAOrder::BGRA)
||(component_order==BGRAOrder::BGRA&&order==RGBAOrder::RGBA)){for(uint32 i=0;i<total_pixels;i++){temp=p[0];p[0]=p[2];p[2]=temp;p+=4;}}else
if((component_order==RGBAOrder::RGBA&&order==RGBAOrder::ARGB)
||(component_order==BGRAOrder::BGRA&&order==RGBAOrder::ABGR)){for(uint32 i=0;i<total_pixels;i++){temp=p[3];p[3]=p[2];p[2]=p[1];p[1]=p[0];p[0]=temp;p+=4;}}else
if((component_order==RGBAOrder::RGBA&&order==RGBAOrder::ABGR)
||(component_order==BGRAOrder::BGRA&&order==RGBAOrder::ARGB)){for(uint32 i=0;i<total_pixels;i++){temp=p[3];p[3]=p[0];p[2]=p[1];p[1]=p[2];p[0]=temp;p+=4;}}else
if((component_order==RGBAOrder::ARGB&&order==RGBAOrder::RGBA)
||(component_order==RGBAOrder::ABGR&&order==RGBAOrder::BGRA)){for(uint32 i=0;i<total_pixels;i++){temp=p[0];p[0]=p[1];p[1]=p[2];p[2]=p[3];p[3]=temp;p+=4;}}else
if((component_order==RGBAOrder::ARGB&&order==RGBAOrder::BGRA)
||(component_order==RGBAOrder::ABGR&&order==RGBAOrder::RGBA)){for(uint32 i=0;i<total_pixels;i++){temp=p[0];p[0]=p[3];p[1]=p[2];p[2]=p[1];p[3]=temp;p+=4;}}else
if((component_order==RGBAOrder::ARGB&&order==RGBAOrder::ABGR)
||(component_order==RGBAOrder::ABGR&&order==RGBAOrder::ARGB)){for(uint32 i=0;i<total_pixels;i++){temp=p[1];p[1]=p[3];p[3]=temp;p+=4;}}else
SetComponentOrder(order);
}
};//class BitmapRGBA:public Bitmap
#define DEF_BITMAP(n,t) \
using Bitmap##n##b =Bitmap##t<int8>; \
using Bitmap##n##ub =Bitmap##t<uint8>; \
using Bitmap##n##s =Bitmap##t<int16>; \
using Bitmap##n##us =Bitmap##t<uint16>; \
using Bitmap##n##i =Bitmap##t<int32>; \
using Bitmap##n##u =Bitmap##t<uint32>; \
using Bitmap##n##f =Bitmap##t<float>; \
using Bitmap##n##d =Bitmap##t<double>;
DEF_BITMAP(1,R)
DEF_BITMAP(2,RG)
DEF_BITMAP(3,RGB)
DEF_BITMAP(4,RGBA)
#undef DEF_BITMAP
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_BITMAP2D_INCLUDE

View File

@ -1,4 +1,5 @@
#include<hgl/log/LogInfo.h>
#include<IL/ilu.h>
#include"ILImage.h"
#include"TextureFileCreater.h"

View File

@ -12,6 +12,8 @@ class ILImage
uint channel_count;
void Refresh();
private:
bool Convert(ILuint,ILuint);
@ -36,11 +38,13 @@ public:
public:
ILImage();
ILImage(ILImage *);
~ILImage();
bool LoadFile(const OSString &);
void Bind();
void Copy(ILImage *);
bool Resize(uint,uint);

View File

@ -52,11 +52,72 @@ ILImage::ILImage()
ilGenImages(1,&il_index);
}
ILImage::ILImage(ILImage *img):ILImage()
{
ilGenImages(1,&il_index);
Bind();
ilCopyImage(img->il_index);
Refresh();
}
ILImage::~ILImage()
{
ilDeleteImages(1,&il_index);
}
void ILImage::Copy(ILImage *img)
{
ilCopyImage(img->il_index);
Refresh();
}
void ILImage::Refresh()
{
il_width =ilGetInteger(IL_IMAGE_WIDTH);
il_height =ilGetInteger(IL_IMAGE_HEIGHT);
il_depth =ilGetInteger(IL_IMAGE_DEPTH);
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();
if(il_format==IL_COLOR_INDEX)
{
uint il_pattle=ilGetInteger(IL_PALETTE_TYPE);
if(il_pattle==IL_PAL_RGB24||il_pattle==IL_PAL_BGR24
||il_pattle==IL_PAL_RGB32||il_pattle==IL_PAL_BGR32)
{
channel_count=3;
il_format=IL_RGB;
il_type=IL_UNSIGNED_BYTE;
ilConvertImage(il_format,il_type);
}
else
if(il_pattle==IL_PAL_RGBA32||il_pattle==IL_PAL_BGRA32)
{
channel_count=4;
il_format=IL_RGBA;
il_type=IL_UNSIGNED_BYTE;
ilConvertImage(il_format,il_type);
}
else
{
LOG_ERROR("Don't support the pattle format.");
}
}
il_bit =ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
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;
}
constexpr ILenum format_by_channel[]=
{
IL_LUMINANCE,
@ -154,15 +215,7 @@ bool ILImage::LoadFile(const OSString &filename)
LOG_INFO(OS_TEXT("\nFile: ")+filename);
il_width =ilGetInteger(IL_IMAGE_WIDTH);
il_height =ilGetInteger(IL_IMAGE_HEIGHT);
il_depth =ilGetInteger(IL_IMAGE_DEPTH);
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();
Refresh();
LOG_INFO(OS_TEXT("\t width: ")+OSString::valueOf(il_width));
LOG_INFO(OS_TEXT("\theight: ")+OSString::valueOf(il_height));
@ -171,41 +224,6 @@ bool ILImage::LoadFile(const OSString &filename)
LOG_INFO(OS_TEXT("\tformat: ")+GetILFormatName(il_format));
LOG_INFO(OS_TEXT("\t type: ")+GetILTypeName(il_type));
if(il_format==IL_COLOR_INDEX)
{
uint il_pattle=ilGetInteger(IL_PALETTE_TYPE);
if(il_pattle==IL_PAL_RGB24||il_pattle==IL_PAL_BGR24
||il_pattle==IL_PAL_RGB32||il_pattle==IL_PAL_BGR32)
{
channel_count=3;
il_format=IL_RGB;
il_type=IL_UNSIGNED_BYTE;
ilConvertImage(il_format,il_type);
return(true);
}
else
if(il_pattle==IL_PAL_RGBA32||il_pattle==IL_PAL_BGRA32)
{
channel_count=4;
il_format=IL_RGBA;
il_type=IL_UNSIGNED_BYTE;
ilConvertImage(il_format,il_type);
return(true);
}
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);
}