2019-12-03 11:49:02 +08:00
|
|
|
#include<hgl/log/LogInfo.h>
|
2021-01-18 15:11:37 +08:00
|
|
|
#include<IL/ilu.h>
|
2019-12-02 22:10:49 +08:00
|
|
|
#include"ILImage.h"
|
2021-12-06 10:47:34 +08:00
|
|
|
#include"Image2D.h"
|
2019-12-02 22:10:49 +08:00
|
|
|
#include"TextureFileCreater.h"
|
|
|
|
|
|
|
|
TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *,ILImage *);
|
|
|
|
TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *,ILImage *);
|
|
|
|
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *,ILImage *);
|
|
|
|
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *,ILImage *);
|
|
|
|
|
2020-08-06 18:18:34 +08:00
|
|
|
TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *,ILImage *);
|
|
|
|
|
|
|
|
using CTFC_FUNC=TextureFileCreater *(*)(const PixelFormat *,ILImage *);
|
|
|
|
|
|
|
|
static CTFC_FUNC CreateTFC[4]={CreateTextureFileCreaterR,CreateTextureFileCreaterRG,CreateTextureFileCreaterRGB,CreateTextureFileCreaterRGBA};
|
|
|
|
|
2020-10-25 21:22:18 +08:00
|
|
|
bool ConvertImage(const OSString &filename,const PixelFormat **pf,const bool mipmaps)
|
2019-12-02 22:10:49 +08:00
|
|
|
{
|
2019-12-03 11:49:02 +08:00
|
|
|
ILImage image;
|
2019-12-02 22:10:49 +08:00
|
|
|
|
2019-12-03 11:49:02 +08:00
|
|
|
if(!image.LoadFile(filename))
|
2019-12-02 22:10:49 +08:00
|
|
|
return(false);
|
2020-10-25 21:22:18 +08:00
|
|
|
|
|
|
|
int miplevel=1;
|
2019-12-02 22:10:49 +08:00
|
|
|
|
2020-10-25 21:22:18 +08:00
|
|
|
if(mipmaps)
|
|
|
|
miplevel=hgl::GetMipLevel(image.width(),image.height());
|
|
|
|
|
2019-12-03 11:49:02 +08:00
|
|
|
const uint channels=image.channels();
|
2019-12-02 22:10:49 +08:00
|
|
|
|
2020-08-06 18:18:34 +08:00
|
|
|
if(channels<0||channels>4)
|
2019-12-02 22:10:49 +08:00
|
|
|
{
|
|
|
|
LOG_ERROR(OS_TEXT("image format don't support "));
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:18:34 +08:00
|
|
|
TextureFileCreater *tex_file_creater;
|
|
|
|
const PixelFormat *fmt=pf[channels-1];
|
|
|
|
|
2021-12-06 10:47:34 +08:00
|
|
|
Image2D *origin_img=nullptr;
|
|
|
|
|
2020-08-06 18:18:34 +08:00
|
|
|
if(fmt->format<ColorFormat::COMPRESS)
|
|
|
|
tex_file_creater=CreateTFC[channels-1](fmt,&image);
|
|
|
|
else
|
|
|
|
tex_file_creater=CreateTextureFileCreaterCompress(fmt,&image);
|
|
|
|
|
2021-12-06 10:47:34 +08:00
|
|
|
if(!tex_file_creater->WriteFileHeader(filename,TextureFileType::Tex2D,miplevel))
|
2019-12-02 22:10:49 +08:00
|
|
|
{
|
|
|
|
tex_file_creater->Delete();
|
|
|
|
LOG_ERROR(OS_TEXT("Write file header failed."));
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:47:34 +08:00
|
|
|
// origin_img=tex_file_creater->CreateImage2D();
|
|
|
|
|
2020-10-25 21:22:18 +08:00
|
|
|
tex_file_creater->InitFormat();
|
|
|
|
|
|
|
|
uint width=image.width();
|
|
|
|
uint height=image.height();
|
|
|
|
uint total=0;
|
|
|
|
uint bytes=0;
|
|
|
|
|
|
|
|
for(int i=0;i<miplevel;i++)
|
2019-12-02 22:10:49 +08:00
|
|
|
{
|
2020-10-25 21:22:18 +08:00
|
|
|
bytes=tex_file_creater->Write();
|
|
|
|
|
|
|
|
if(bytes<=0)
|
|
|
|
{
|
|
|
|
tex_file_creater->Delete();
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
total+=bytes;
|
|
|
|
|
|
|
|
if(i<miplevel)
|
|
|
|
{
|
|
|
|
if(width>1)width>>=1;
|
|
|
|
if(height>1)height>>=1;
|
|
|
|
|
|
|
|
image.Resize(width,height);
|
|
|
|
}
|
2019-12-02 22:10:49 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 21:22:18 +08:00
|
|
|
LOG_INFO(OS_TEXT("pixel total length: ")+OSString::valueOf(total)+OS_TEXT(" bytes."));
|
|
|
|
|
2019-12-02 22:10:49 +08:00
|
|
|
tex_file_creater->Close();
|
|
|
|
|
|
|
|
delete tex_file_creater;
|
|
|
|
return(true);
|
|
|
|
}
|
2019-12-03 11:49:02 +08:00
|
|
|
|