TexConv/TextureFileCreaterRGB.cpp

121 lines
3.3 KiB
C++
Raw Normal View History

2019-12-02 22:10:49 +08:00
#include"TextureFileCreater.h"
#include"ILImage.h"
#include<hgl/log/LogInfo.h>
class TextureFileCreaterRGB:public TextureFileCreater
{
2020-10-25 21:22:18 +08:00
ILuint type;
2019-12-02 22:10:49 +08:00
public:
using TextureFileCreater::TextureFileCreater;
2020-10-25 21:22:18 +08:00
public:
bool InitFormat() override
{
if(pixel_format->format==ColorFormat::RGB32U
||pixel_format->format==ColorFormat::RGB32I
||pixel_format->format==ColorFormat::RGB32F)
{
if(!ToILType(type,pixel_format->bits[0],pixel_format->type))
return(false);
}
else if(pixel_format->format==ColorFormat::RGB565)
{
type=IL_UNSIGNED_BYTE;
}
else if(pixel_format->format==ColorFormat::B10GR11UF)
{
type=IL_HALF;
}
else
{
LOG_ERROR(OS_TEXT("Don't support this RGB format"));
return(false);
}
return image->ConvertToRGB(type);
}
2019-12-02 22:10:49 +08:00
void RGB8toRGB565(uint16 *target,uint8 *src,uint size)
{
for(uint i=0;i<size;i++)
{
2019-12-03 20:49:25 +08:00
*target=((src[0]<<8)&0xF800)
2019-12-02 22:10:49 +08:00
|((src[1]<<3)&0x7E0)
2019-12-03 20:49:25 +08:00
| (src[2]>>3);
2019-12-02 22:10:49 +08:00
++target;
src+=3;
}
}
2019-12-04 20:47:58 +08:00
// Bit depth Sign bit present Exponent bits Mantissa bits
// 32 Yes 8 23
// 16 Yes 5 10
// 11 No 5 6
// 10 No 5 5
void RGB16FtoB10GR11UF(uint32 *target,uint16 *src,uint size)
{
for(uint i=0;i<size;i++)
{
*target=((src[2]&0x7FE0)<<17)
|((src[1]&0x7FF0)<<7)
| (src[0]&0x7FF0)>>4;
++target;
src+=3;
}
}
2019-12-02 22:10:49 +08:00
public:
2020-10-25 21:22:18 +08:00
uint32 Write() override
2019-12-02 22:10:49 +08:00
{
2020-10-25 21:22:18 +08:00
const uint total_bytes=(pixel_format->total_bits*image->pixel_total())>>3;
std::cout<<"Convert Image To: "<<image->width()<<"x"<<image->height()<<" "<<total_bytes<<" bytes."<<std::endl;
2019-12-02 22:10:49 +08:00
2020-10-25 21:22:18 +08:00
if(pixel_format->format==ColorFormat::RGB32U
||pixel_format->format==ColorFormat::RGB32I
||pixel_format->format==ColorFormat::RGB32F)
{
2019-12-02 22:10:49 +08:00
void *origin_rgb=image->GetRGB(type);
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(origin_rgb,total_bytes);
2019-12-02 22:10:49 +08:00
}
2020-10-25 21:22:18 +08:00
else if(pixel_format->format==ColorFormat::RGB565)
2019-12-02 22:10:49 +08:00
{
void *origin_rgb=image->GetRGB(IL_UNSIGNED_BYTE);
2020-10-25 21:22:18 +08:00
AutoDeleteArray<uint16> rgb565(image->pixel_total());
2019-12-02 22:10:49 +08:00
RGB8toRGB565(rgb565,(uint8 *)origin_rgb,image->pixel_total());
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(rgb565,total_bytes);
2019-12-02 22:10:49 +08:00
}
2020-10-25 21:22:18 +08:00
else if(pixel_format->format==ColorFormat::B10GR11UF)
2019-12-04 20:47:58 +08:00
{
void *origin_rgb=image->GetRGB(IL_HALF);
2020-10-25 21:22:18 +08:00
AutoDeleteArray<uint32> b10gr11(image->pixel_total());
2019-12-04 20:47:58 +08:00
RGB16FtoB10GR11UF(b10gr11,(uint16 *)origin_rgb,image->pixel_total());
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(b10gr11,total_bytes);
2019-12-04 20:47:58 +08:00
}
2019-12-02 22:10:49 +08:00
else
{
LOG_ERROR(OS_TEXT("Don't support this RGB format"));
2020-10-25 21:22:18 +08:00
return(0);
2019-12-02 22:10:49 +08:00
}
}
};//class TextureFileCreaterRGB:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *pf,ILImage *image)
{
return(new TextureFileCreaterRGB(pf,image));
}