TexConv/ConvertImage.cpp

88 lines
1.9 KiB
C++
Raw Normal View History

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"
#include"TextureFileCreater.h"
#include"ImageConvertConfig.h"
2019-12-02 22:10:49 +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
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
if(channels<=0||channels>4)
2019-12-02 22:10:49 +08:00
{
LOG_ERROR(OS_TEXT("image format don't support "));
return(false);
}
const PixelFormat *fmt=cfg->pixel_fmt[channels-1];
uint width = image.width();
uint height = image.height();
AutoDelete<TextureFileCreater> tex_file_creater=CreateTFC(fmt,channels);
2021-12-06 10:47:34 +08:00
2021-12-13 13:47:08 +08:00
if(!tex_file_creater->CreateTexFile(filename,VK_IMAGE_VIEW_TYPE_2D))
{
LOG_ERROR(OS_TEXT("Create Texture failed."));
return(false);
}
2021-12-13 13:47:08 +08:00
if(!tex_file_creater->WriteSize2D(width,height))
2019-12-02 22:10:49 +08:00
{
LOG_ERROR(OS_TEXT("Write size failed."));
2019-12-02 22:10:49 +08:00
return(false);
}
2021-12-13 13:47:08 +08:00
if(!tex_file_creater->WritePixelFormat(miplevel))
{
LOG_ERROR(OS_TEXT("Write format failed."));
return(false);
}
2021-12-06 10:47:34 +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;
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