Created ParamParse.cpp/.h and ImageConvertConfig.h
This commit is contained in:
parent
21391024b2
commit
f86f862522
@ -38,7 +38,8 @@ SET(ILIMAGE_SOURCE ILImage.h
|
||||
# Image2D.cpp)
|
||||
|
||||
SET(PIXEL_FORMAT_SOURCE pixel_format.cpp
|
||||
pixel_format.h)
|
||||
pixel_format.h
|
||||
ImageConvertConfig.h)
|
||||
|
||||
SET(TEXTURE_FILE_CREATER_SOURCE TextureFileCreater.h
|
||||
TextureFileCreater.cpp
|
||||
@ -48,11 +49,14 @@ SET(TEXTURE_FILE_CREATER_SOURCE TextureFileCreater.h
|
||||
TextureFileCreaterRGBA.cpp
|
||||
TextureFileCreaterCompress.cpp)
|
||||
|
||||
SET(PARAM_PARSE_SOURCE ParamParse.h ParamParse.cpp)
|
||||
|
||||
SOURCE_GROUP("Image File" FILES ${ILIMAGE_SOURCE})
|
||||
SOURCE_GROUP("Pixel Format" FILES ${PIXEL_FORMAT_SOURCE})
|
||||
SOURCE_GROUP("Texture File Creater" FILES ${TEXTURE_FILE_CREATER_SOURCE})
|
||||
SOURCE_GROUP("CMD Param parse" FILES ${PARAM_PARSE_SOURCE})
|
||||
|
||||
SET(TEX_CONV_SOURCE ${ILIMAGE_SOURCE} ${PIXEL_FORMAT_SOURCE} ${TEXTURE_FILE_CREATER_SOURCE})
|
||||
SET(TEX_CONV_SOURCE ${ILIMAGE_SOURCE} ${PIXEL_FORMAT_SOURCE} ${TEXTURE_FILE_CREATER_SOURCE} ${PARAM_PARSE_SOURCE})
|
||||
SET(TEX_CONV_LIBRARY CMCore CMPlatform CMUtil DevIL ILU CMP_Compressonator CMP_Framework CMP_Core)
|
||||
|
||||
add_executable(TexConv ${TEX_CONV_SOURCE} main.cpp ConvertImage.cpp)
|
||||
|
@ -1,21 +1,11 @@
|
||||
#include<hgl/log/LogInfo.h>
|
||||
#include<IL/ilu.h>
|
||||
#include"ILImage.h"
|
||||
#include"Image2D.h"
|
||||
//#include"Image2D.h"
|
||||
#include"TextureFileCreater.h"
|
||||
#include"ImageConvertConfig.h"
|
||||
|
||||
TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *,ILImage *);
|
||||
TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *,ILImage *);
|
||||
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *,ILImage *);
|
||||
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *,ILImage *);
|
||||
|
||||
TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *,ILImage *);
|
||||
|
||||
using CTFC_FUNC=TextureFileCreater *(*)(const PixelFormat *,ILImage *);
|
||||
|
||||
static CTFC_FUNC CreateTFC[4]={CreateTextureFileCreaterR,CreateTextureFileCreaterRG,CreateTextureFileCreaterRGB,CreateTextureFileCreaterRGBA};
|
||||
|
||||
bool ConvertImage(const OSString &filename,const PixelFormat **pf,const bool mipmaps)
|
||||
bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg)
|
||||
{
|
||||
ILImage image;
|
||||
|
||||
@ -24,7 +14,7 @@ bool ConvertImage(const OSString &filename,const PixelFormat **pf,const bool mip
|
||||
|
||||
int miplevel=1;
|
||||
|
||||
if(mipmaps)
|
||||
if(cfg->gen_mipmaps)
|
||||
miplevel=hgl::GetMipLevel(image.width(),image.height());
|
||||
|
||||
const uint channels=image.channels();
|
||||
@ -36,7 +26,7 @@ bool ConvertImage(const OSString &filename,const PixelFormat **pf,const bool mip
|
||||
}
|
||||
|
||||
TextureFileCreater *tex_file_creater;
|
||||
const PixelFormat *fmt=pf[channels-1];
|
||||
const PixelFormat *fmt=cfg->pixel_fmt[channels-1];
|
||||
|
||||
Image2D *origin_img=nullptr;
|
||||
|
||||
|
@ -0,0 +1,126 @@
|
||||
#include<iostream>
|
||||
#include<hgl/util/cmd/CmdParse.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
#include"ILImage.h"
|
||||
#include"TextureFileCreater.h"
|
||||
#include"ImageConvertConfig.h"
|
||||
#include"CMP_CompressonatorLib/Compressonator.h"
|
||||
#include"ParamParse.h"
|
||||
|
||||
using namespace hgl;
|
||||
using namespace hgl::filesystem;
|
||||
using namespace hgl::util;
|
||||
|
||||
bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg)
|
||||
{
|
||||
ILImage image;
|
||||
|
||||
if(!image.LoadFile(filename))
|
||||
return(false);
|
||||
|
||||
int miplevel=1;
|
||||
|
||||
if(cfg->gen_mipmaps)
|
||||
miplevel=hgl::GetMipLevel(image.width(),image.height());
|
||||
|
||||
const uint channels=image.channels();
|
||||
|
||||
if(channels<0||channels>4)
|
||||
{
|
||||
LOG_ERROR(OS_TEXT("image format don't support "));
|
||||
return(false);
|
||||
}
|
||||
|
||||
TextureFileCreater *tex_file_creater;
|
||||
const PixelFormat *fmt=cfg->pixel_fmt[channels-1];
|
||||
|
||||
Image2D *origin_img=nullptr;
|
||||
|
||||
if(fmt->format<ColorFormat::COMPRESS)
|
||||
tex_file_creater=CreateTFC[channels-1](fmt,&image);
|
||||
else
|
||||
tex_file_creater=CreateTextureFileCreaterCompress(fmt,&image);
|
||||
|
||||
if(!tex_file_creater->WriteFileHeader(filename,TextureFileType::TexCubemap,miplevel))
|
||||
{
|
||||
tex_file_creater->Delete();
|
||||
LOG_ERROR(OS_TEXT("Write file header failed."));
|
||||
return(false);
|
||||
}
|
||||
|
||||
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++)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
LOG_INFO(OS_TEXT("pixel total length: ")+OSString::valueOf(total)+OS_TEXT(" bytes."));
|
||||
|
||||
tex_file_creater->Close();
|
||||
|
||||
delete tex_file_creater;
|
||||
return(true);
|
||||
}
|
||||
|
||||
void CMP_RegisterHostPlugins();
|
||||
|
||||
bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg);
|
||||
|
||||
int os_main(int argc,os_char **argv)
|
||||
{
|
||||
std::cout<<"Cubemap to Texture Convert tools 1.2"<<std::endl<<std::endl;
|
||||
|
||||
if(argc<=1)
|
||||
{
|
||||
std::cout<< "Command format:\n"
|
||||
"\tCubemapConv [/R:][/RG:][/RGB:][/RGBA:] [/mip] <filename>\n\n";
|
||||
|
||||
PrintFormatList();
|
||||
return 0;
|
||||
}
|
||||
|
||||
CmdParse cp(argc,argv);
|
||||
|
||||
ImageConvertConfig icc;
|
||||
|
||||
if(cp.Find(OS_TEXT("/mip"))!=-1)icc.gen_mipmaps=true; //检测是否生成mipmaps
|
||||
|
||||
ParseParamColorKey(&icc,cp);
|
||||
ParseParamFormat(&icc,cp); //检测推荐格式
|
||||
|
||||
ilInit();
|
||||
|
||||
CMP_RegisterHostPlugins();
|
||||
CMP_InitializeBCLibrary();
|
||||
|
||||
if(filesystem::FileCanRead(argv[argc-1]))
|
||||
{
|
||||
ConvertImage(argv[argc-1],&icc);
|
||||
}
|
||||
|
||||
CMP_ShutdownBCLibrary();
|
||||
ilShutDown();
|
||||
return 0;
|
||||
}
|
18
ImageConvertConfig.h
Normal file
18
ImageConvertConfig.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include"pixel_format.h"
|
||||
|
||||
struct ImageConvertConfig
|
||||
{
|
||||
const PixelFormat * pixel_fmt[4]; ///<选中格式
|
||||
bool gen_mipmaps; ///<是否产生mipmaps
|
||||
|
||||
bool use_color_key; ///<是否使用ColorKey
|
||||
uint8 color_key[3]; ///<ColorKey颜色
|
||||
|
||||
public:
|
||||
|
||||
ImageConvertConfig()
|
||||
{
|
||||
hgl_zero(*this);
|
||||
}
|
||||
};//struct ImageConvertConfig
|
43
ParamParse.cpp
Normal file
43
ParamParse.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include"ParamParse.h"
|
||||
|
||||
const PixelFormat *ParseParamFormat(const CmdParse &cmd,const os_char *flag,const PixelFormat *default_format)
|
||||
{
|
||||
OSString fmtstr;
|
||||
|
||||
if(!cmd.GetString(flag,fmtstr))return(default_format);
|
||||
|
||||
const PixelFormat *result=GetPixelFormat(fmtstr.c_str());
|
||||
|
||||
if(result)return(result);
|
||||
|
||||
LOG_INFO(OS_TEXT("[FORMAT ERROR] Don't support ")+fmtstr+OS_TEXT(" format."));
|
||||
|
||||
return default_format;
|
||||
}
|
||||
|
||||
void ParseParamFormat(ImageConvertConfig *icc,const CmdParse &cmd)
|
||||
{
|
||||
//Ö¸¶¨¸ñʽ
|
||||
icc->pixel_fmt[0]=ParseParamFormat(cmd,OS_TEXT("/R:"), GetPixelFormat(ColorFormat::R8));
|
||||
icc->pixel_fmt[1]=ParseParamFormat(cmd,OS_TEXT("/RG:"), GetPixelFormat(ColorFormat::RG8));
|
||||
icc->pixel_fmt[2]=ParseParamFormat(cmd,OS_TEXT("/RGB:"), GetPixelFormat(ColorFormat::RGB565));
|
||||
icc->pixel_fmt[3]=ParseParamFormat(cmd,OS_TEXT("/RGBA:"), GetPixelFormat(ColorFormat::RGBA8));
|
||||
|
||||
for(uint i=0;i<4;i++)
|
||||
std::cout<<(i+1)<<": "<<icc->pixel_fmt[i]->name<<std::endl;
|
||||
}
|
||||
|
||||
void ParseParamColorKey(ImageConvertConfig *icc,const CmdParse &cmd)
|
||||
{
|
||||
OSString ckstr;
|
||||
|
||||
if(!cmd.GetString(OS_TEXT("/ColorKey:"),ckstr))return;
|
||||
|
||||
const os_char *rgbstr=ckstr.c_str();
|
||||
|
||||
ParseHexStr(icc->color_key[0],rgbstr+0);
|
||||
ParseHexStr(icc->color_key[1],rgbstr+2);
|
||||
ParseHexStr(icc->color_key[2],rgbstr+4);
|
||||
|
||||
icc->use_color_key=true;
|
||||
}
|
10
ParamParse.h
Normal file
10
ParamParse.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include<hgl/util/cmd/CmdParse.h>
|
||||
#include"ImageConvertConfig.h"
|
||||
|
||||
using namespace hgl;
|
||||
using namespace hgl::util;
|
||||
|
||||
const PixelFormat *ParseParamFormat(const CmdParse &cmd,const os_char *flag,const PixelFormat *default_format);
|
||||
void ParseParamFormat(ImageConvertConfig *icc,const CmdParse &cmd);
|
||||
void ParseParamColorKey(ImageConvertConfig *icc,const CmdParse &cmd);
|
@ -52,3 +52,15 @@ public:
|
||||
virtual void Close();
|
||||
virtual void Delete();
|
||||
};//class TextureFileCreater
|
||||
|
||||
TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *,ILImage *);
|
||||
TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *,ILImage *);
|
||||
TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *,ILImage *);
|
||||
TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *,ILImage *);
|
||||
|
||||
TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *,ILImage *);
|
||||
|
||||
using CTFC_FUNC=TextureFileCreater *(*)(const PixelFormat *,ILImage *);
|
||||
|
||||
static CTFC_FUNC CreateTFC[4]={CreateTextureFileCreaterR,CreateTextureFileCreaterRG,CreateTextureFileCreaterRGB,CreateTextureFileCreaterRGBA};
|
||||
|
||||
|
79
main.cpp
79
main.cpp
@ -1,15 +1,12 @@
|
||||
#include<iostream>
|
||||
#include<IL/il.h>
|
||||
#include<hgl/util/cmd/CmdParse.h>
|
||||
#include<hgl/type/DataType.h>
|
||||
#include<hgl/type/StrChar.h>
|
||||
#include<hgl/Time.h>
|
||||
#include<hgl/filesystem/FileSystem.h>
|
||||
#include<hgl/filesystem/EnumFile.h>
|
||||
#include<hgl/log/LogInfo.h>
|
||||
#include"pixel_format.h"
|
||||
#include"IntelTextureCompression/ispc_texcomp.h"
|
||||
#include"CMP_CompressonatorLib/Compressonator.h"
|
||||
#include"ImageConvertConfig.h"
|
||||
#include"ParamParse.h"
|
||||
|
||||
using namespace hgl;
|
||||
using namespace hgl::filesystem;
|
||||
@ -17,74 +14,32 @@ using namespace hgl::util;
|
||||
|
||||
bool sub_folder =false;
|
||||
|
||||
const PixelFormat * pixel_fmt[4] ={nullptr,nullptr,nullptr,nullptr}; //选中格式
|
||||
bool gen_mipmaps =false; //是否产生mipmaps
|
||||
|
||||
bool use_color_key =false; //是否使用ColorKey
|
||||
uint8 color_key[3]; //ColorKey颜色
|
||||
|
||||
void CMP_RegisterHostPlugins();
|
||||
|
||||
bool ConvertImage(const OSString &filename,const PixelFormat **pf,const bool mipmap);
|
||||
|
||||
const PixelFormat *ParseParamFormat(const CmdParse &cmd,const os_char *flag,const PixelFormat *default_format)
|
||||
{
|
||||
OSString fmtstr;
|
||||
|
||||
if(!cmd.GetString(flag,fmtstr))return(default_format);
|
||||
|
||||
const PixelFormat *result=GetPixelFormat(fmtstr.c_str());
|
||||
|
||||
if(result)return(result);
|
||||
|
||||
LOG_INFO(OS_TEXT("[FORMAT ERROR] Don't support ")+fmtstr+OS_TEXT(" format."));
|
||||
|
||||
return default_format;
|
||||
}
|
||||
|
||||
void ParseParamFormat(const CmdParse &cmd)
|
||||
{
|
||||
//指定格式
|
||||
pixel_fmt[0]=ParseParamFormat(cmd,OS_TEXT("/R:"), GetPixelFormat(ColorFormat::R8));
|
||||
pixel_fmt[1]=ParseParamFormat(cmd,OS_TEXT("/RG:"), GetPixelFormat(ColorFormat::RG8));
|
||||
pixel_fmt[2]=ParseParamFormat(cmd,OS_TEXT("/RGB:"), GetPixelFormat(ColorFormat::RGB565));
|
||||
pixel_fmt[3]=ParseParamFormat(cmd,OS_TEXT("/RGBA:"), GetPixelFormat(ColorFormat::RGBA8));
|
||||
|
||||
for(uint i=0;i<4;i++)
|
||||
std::cout<<(i+1)<<": "<<pixel_fmt[i]->name<<std::endl;
|
||||
}
|
||||
|
||||
void ParseParamColorKey(const CmdParse &cmd)
|
||||
{
|
||||
OSString ckstr;
|
||||
|
||||
if(!cmd.GetString(OS_TEXT("/ColorKey:"),ckstr))return;
|
||||
|
||||
const os_char *rgbstr=ckstr.c_str();
|
||||
|
||||
ParseHexStr(color_key[0],rgbstr+0);
|
||||
ParseHexStr(color_key[1],rgbstr+2);
|
||||
ParseHexStr(color_key[2],rgbstr+4);
|
||||
|
||||
use_color_key=true;
|
||||
}
|
||||
bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg);
|
||||
|
||||
class EnumConvertImage:public EnumFile
|
||||
{
|
||||
private:
|
||||
|
||||
ImageConvertConfig *cfg;
|
||||
uint convert_count=0;
|
||||
|
||||
protected:
|
||||
|
||||
void ProcFile(EnumFileConfig *efc,FileInfo &fi) override
|
||||
{
|
||||
ConvertImage(fi.fullname,pixel_fmt,gen_mipmaps);
|
||||
ConvertImage(fi.fullname,cfg);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
const uint GetConvertCount()const{return convert_count;}
|
||||
|
||||
EnumConvertImage(ImageConvertConfig *icc)
|
||||
{
|
||||
cfg=icc;
|
||||
}
|
||||
};//class EnumConvertImage:public EnumFile
|
||||
|
||||
int os_main(int argc,os_char **argv)
|
||||
@ -106,11 +61,13 @@ int os_main(int argc,os_char **argv)
|
||||
|
||||
CmdParse cp(argc,argv);
|
||||
|
||||
if(cp.Find(OS_TEXT("/s"))!=-1)sub_folder=true; //检测是否处理子目录
|
||||
if(cp.Find(OS_TEXT("/mip"))!=-1)gen_mipmaps=true; //检测是否生成mipmaps
|
||||
ImageConvertConfig icc;
|
||||
|
||||
ParseParamColorKey(cp);
|
||||
ParseParamFormat(cp); //检测推荐格式
|
||||
if(cp.Find(OS_TEXT("/s"))!=-1)sub_folder=true; //检测是否处理子目录
|
||||
if(cp.Find(OS_TEXT("/mip"))!=-1)icc.gen_mipmaps=true; //检测是否生成mipmaps
|
||||
|
||||
ParseParamColorKey(&icc,cp);
|
||||
ParseParamFormat(&icc,cp); //检测推荐格式
|
||||
|
||||
ilInit();
|
||||
|
||||
@ -119,7 +76,7 @@ int os_main(int argc,os_char **argv)
|
||||
|
||||
if(filesystem::FileCanRead(argv[argc-1]))
|
||||
{
|
||||
ConvertImage(argv[argc-1],pixel_fmt,gen_mipmaps);
|
||||
ConvertImage(argv[argc-1],&icc);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -131,7 +88,7 @@ int os_main(int argc,os_char **argv)
|
||||
efc.proc_file =true;
|
||||
efc.sub_folder =sub_folder;
|
||||
|
||||
EnumConvertImage eci;
|
||||
EnumConvertImage eci(&icc);
|
||||
|
||||
eci.Enum(&efc);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user