TexConv/TextureFileCreaterRGBA.cpp

136 lines
4.4 KiB
C++
Raw Permalink Normal View History

2019-12-02 22:10:49 +08:00
#include"TextureFileCreater.h"
#include"ILImage.h"
#include<hgl/log/LogInfo.h>
#include<hgl/color/ColorFormat.h>
2019-12-02 22:10:49 +08:00
class TextureFileCreaterRGBA:public TextureFileCreater
{
2020-10-25 21:22:18 +08:00
ILuint type;
2019-12-02 22:10:49 +08:00
public:
using TextureFileCreater::TextureFileCreater;
bool InitFormat(ILImage* img) override
2020-10-25 21:22:18 +08:00
{
image = img;
2020-10-25 21:22:18 +08:00
if(pixel_format->format==ColorFormat::RGBA8
||pixel_format->format==ColorFormat::RGBA16
2021-11-29 15:28:37 +08:00
||pixel_format->format==ColorFormat::RGBA16U
||pixel_format->format==ColorFormat::RGBA16I
2020-10-25 21:22:18 +08:00
||pixel_format->format==ColorFormat::RGBA16F
||pixel_format->format==ColorFormat::RGBA32U
||pixel_format->format==ColorFormat::RGBA32I
||pixel_format->format==ColorFormat::RGBA32F)
{
if(!ToILType(type,pixel_format->bits[0],pixel_format->type))
return(nullptr);
}
2021-11-29 15:28:37 +08:00
else if(pixel_format->format==ColorFormat::RGBA4
||pixel_format->format==ColorFormat::BGRA4)
2020-10-25 21:22:18 +08:00
{
type=IL_UNSIGNED_BYTE;
}
else if(pixel_format->format==ColorFormat::A1RGB5)
{
type=IL_UNSIGNED_BYTE;
}
else if(pixel_format->format==ColorFormat::A2BGR10)
{
type=IL_UNSIGNED_SHORT;
}
else
{
LOG_ERROR(OS_TEXT("Don't support this RGBA format"));
return(false);
}
return image->ConvertToRGBA(type);
}
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;
2019-12-02 22:10:49 +08:00
2020-10-25 21:22:18 +08:00
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::RGBA8
2021-11-29 15:28:37 +08:00
||pixel_format->format==ColorFormat::RGBA8SN
||pixel_format->format==ColorFormat::RGBA8U
||pixel_format->format==ColorFormat::RGBA8I
2020-10-25 21:22:18 +08:00
||pixel_format->format==ColorFormat::RGBA16
2021-11-29 15:28:37 +08:00
||pixel_format->format==ColorFormat::RGBA16U
||pixel_format->format==ColorFormat::RGBA16I
2020-10-25 21:22:18 +08:00
||pixel_format->format==ColorFormat::RGBA16F
||pixel_format->format==ColorFormat::RGBA32U
||pixel_format->format==ColorFormat::RGBA32I
||pixel_format->format==ColorFormat::RGBA32F)
{
2019-12-02 22:10:49 +08:00
void *origin_rgba=image->GetRGBA(type);
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(origin_rgba,total_bytes);
2019-12-02 22:10:49 +08:00
}
2021-11-29 15:28:37 +08:00
else if(pixel_format->format==ColorFormat::ABGR8)
{
uint32 *origin_rgba=(uint32 *)(image->GetRGBA(type));
EndianSwap<uint32>(origin_rgba,image->pixel_total());
return TextureFileCreater::Write(origin_rgba,total_bytes);
}
2020-10-25 21:22:18 +08:00
else if(pixel_format->format==ColorFormat::BGRA4)
2019-12-02 22:10:49 +08:00
{
void *origin_rgba=image->GetRGBA(IL_UNSIGNED_BYTE);
2020-10-25 21:22:18 +08:00
AutoDeleteArray<uint16> bgra4(image->pixel_total());
2019-12-02 22:10:49 +08:00
2021-11-29 15:28:37 +08:00
RGBA8toBGRA4(bgra4,(uint8 *)origin_rgba,image->pixel_total());
2019-12-02 22:10:49 +08:00
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(bgra4,total_bytes);
2019-12-02 22:10:49 +08:00
}
2021-11-29 15:28:37 +08:00
else if(pixel_format->format==ColorFormat::RGBA4)
{
void *origin_rgba=image->GetRGBA(IL_UNSIGNED_BYTE);
AutoDeleteArray<uint16> rgba4(image->pixel_total());
RGBA8toRGBA4(rgba4,(uint8 *)origin_rgba,image->pixel_total());
return TextureFileCreater::Write(rgba4,total_bytes);
}
2020-10-25 21:22:18 +08:00
else if(pixel_format->format==ColorFormat::A1RGB5)
2019-12-02 22:10:49 +08:00
{
void *origin_rgba=image->GetRGBA(IL_UNSIGNED_BYTE);
2020-10-25 21:22:18 +08:00
AutoDeleteArray<uint16> a1_rgb5(image->pixel_total());
2019-12-02 22:10:49 +08:00
RGBA8toA1RGB5(a1_rgb5,(uint8 *)origin_rgba,image->pixel_total());
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(a1_rgb5,total_bytes);
2019-12-02 22:10:49 +08:00
}
2020-10-25 21:22:18 +08:00
else if(pixel_format->format==ColorFormat::A2BGR10)
2019-12-02 22:10:49 +08:00
{
void *origin_rgba=image->GetRGBA(IL_UNSIGNED_SHORT);
2020-10-25 21:22:18 +08:00
AutoDeleteArray<uint32> a2_bgr10(image->pixel_total());
2019-12-02 22:10:49 +08:00
RGBA16toA2BGR10(a2_bgr10,(uint16 *)origin_rgba,image->pixel_total());
2020-10-25 21:22:18 +08:00
return TextureFileCreater::Write(a2_bgr10,total_bytes);
2019-12-02 22:10:49 +08:00
}
else
{
LOG_ERROR(OS_TEXT("Don't support this RGBA format"));
2020-10-25 21:22:18 +08:00
return(0);
2019-12-02 22:10:49 +08:00
}
}
};//class TextureFileCreaterRGB:public TextureFileCreater
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *pf)
2019-12-02 22:10:49 +08:00
{
return(new TextureFileCreaterRGBA(pf));
2019-12-02 22:10:49 +08:00
}