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 12:07:38 +08:00
|
|
|
//#include"Image2D.h"
|
2019-12-02 22:10:49 +08:00
|
|
|
#include"TextureFileCreater.h"
|
2021-12-06 12:07:38 +08:00
|
|
|
#include"ImageConvertConfig.h"
|
2019-12-02 22:10:49 +08:00
|
|
|
|
2021-12-06 12:07:38 +08:00
|
|
|
bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg)
|
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
|
|
|
|
2021-12-06 12:07:38 +08:00
|
|
|
if(cfg->gen_mipmaps)
|
2020-10-25 21:22:18 +08:00
|
|
|
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
|
|
|
|
2021-12-06 15:28:01 +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);
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:07:38 +08:00
|
|
|
const PixelFormat *fmt=cfg->pixel_fmt[channels-1];
|
2021-12-07 18:18:22 +08:00
|
|
|
uint width = image.width();
|
|
|
|
uint height = image.height();
|
2020-08-06 18:18:34 +08:00
|
|
|
|
2021-12-07 18:18:22 +08:00
|
|
|
AutoDelete<TextureFileCreater> tex_file_creater=CreateTFC(fmt,channels);
|
2021-12-06 10:47:34 +08:00
|
|
|
|
2021-12-07 18:18:22 +08:00
|
|
|
if(!tex_file_creater->CreateTexFile(filename,TextureFileType::Tex2D))
|
|
|
|
{
|
|
|
|
LOG_ERROR(OS_TEXT("Create Texture failed."));
|
|
|
|
return(false);
|
|
|
|
}
|
2020-08-06 18:18:34 +08:00
|
|
|
|
2021-12-07 18:18:22 +08:00
|
|
|
if(!tex_file_creater->WriteSize2D(miplevel,width,height))
|
2019-12-02 22:10:49 +08:00
|
|
|
{
|
2021-12-07 18:18:22 +08:00
|
|
|
LOG_ERROR(OS_TEXT("Write size failed."));
|
2019-12-02 22:10:49 +08:00
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2021-12-07 18:18:22 +08:00
|
|
|
if(!tex_file_creater->WritePixelFormat())
|
|
|
|
{
|
|
|
|
LOG_ERROR(OS_TEXT("Write format failed."));
|
|
|
|
return(false);
|
|
|
|
}
|
2021-12-06 10:47:34 +08:00
|
|
|
|
2021-12-07 18:18:22 +08:00
|
|
|
if(!tex_file_creater->InitFormat(&image))
|
|
|
|
{
|
|
|
|
LOG_ERROR(OS_TEXT("Init texture format failed."));
|
|
|
|
return(false);
|
|
|
|
}
|
2020-10-25 21:22:18 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-12-07 18:18:22 +08:00
|
|
|
if(miplevel>1&&i<miplevel)
|
2020-10-25 21:22:18 +08:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
2019-12-03 11:49:02 +08:00
|
|
|
|