diff --git a/ConvertImage.cpp b/ConvertImage.cpp index 3fb4c83..9b46e3d 100644 --- a/ConvertImage.cpp +++ b/ConvertImage.cpp @@ -25,29 +25,36 @@ bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg) return(false); } - TextureFileCreater *tex_file_creater; const PixelFormat *fmt=cfg->pixel_fmt[channels-1]; + uint width = image.width(); + uint height = image.height(); - Image2D *origin_img=nullptr; + AutoDelete tex_file_creater=CreateTFC(fmt,channels); - if(fmt->formatWriteFileHeader(filename,TextureFileType::Tex2D,miplevel)) + if(!tex_file_creater->CreateTexFile(filename,TextureFileType::Tex2D)) { - tex_file_creater->Delete(); - LOG_ERROR(OS_TEXT("Write file header failed.")); + LOG_ERROR(OS_TEXT("Create Texture failed.")); return(false); } -// origin_img=tex_file_creater->CreateImage2D(); + if(!tex_file_creater->WriteSize2D(miplevel,width,height)) + { + LOG_ERROR(OS_TEXT("Write size failed.")); + return(false); + } - tex_file_creater->InitFormat(); + if(!tex_file_creater->WritePixelFormat()) + { + LOG_ERROR(OS_TEXT("Write format failed.")); + return(false); + } + + if(!tex_file_creater->InitFormat(&image)) + { + LOG_ERROR(OS_TEXT("Init texture format failed.")); + return(false); + } - uint width=image.width(); - uint height=image.height(); uint total=0; uint bytes=0; @@ -63,7 +70,7 @@ bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg) total+=bytes; - if(i1&&i1)width>>=1; if(height>1)height>>=1; @@ -76,7 +83,6 @@ bool ConvertImage(const OSString &filename,const ImageConvertConfig *cfg) tex_file_creater->Close(); - delete tex_file_creater; return(true); } diff --git a/CubemapConvert.cpp b/CubemapConvert.cpp index bf1e0f7..4cb31dc 100644 --- a/CubemapConvert.cpp +++ b/CubemapConvert.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include"ILImage.h" #include"TextureFileCreater.h" #include"ImageConvertConfig.h" @@ -59,24 +60,38 @@ bool ConvertCubemap(const OSString &filename,const OSStringList &file_list,const uint total=0; uint bytes=0; + AutoDelete tex_file_creater=CreateTFC(fmt,channels); + + if (!tex_file_creater->CreateTexFile(filename, TextureFileType::TexCubemap)) + { + LOG_ERROR(OS_TEXT("Create Texture failed.")); + return(false); + } + + if (!tex_file_creater->WriteSize2D(miplevel, width, height)) + { + LOG_ERROR(OS_TEXT("Write size failed.")); + return(false); + } + + if (!tex_file_creater->WritePixelFormat()) + { + LOG_ERROR(OS_TEXT("Write format failed.")); + return(false); + } + for(int face=0;face<6;face++) { - TextureFileCreater *tex_file_creater; + image[face].Bind(); + width=image[face].width(); + height=image[face].height(); - if(fmt->formatWriteFileHeader(filename,TextureFileType::TexCubemap,miplevel)) + if (!tex_file_creater->InitFormat(&image[face])) { - tex_file_creater->Delete(); - LOG_ERROR(OS_TEXT("Write file header failed.")); + LOG_ERROR(OS_TEXT("Init texture format failed.")); return(false); } - tex_file_creater->InitFormat(); - for(int i=0;iWrite(); @@ -89,7 +104,7 @@ bool ConvertCubemap(const OSString &filename,const OSStringList &file_list,const total+=bytes; - if(i1&&i1)width>>=1; if(height>1)height>>=1; @@ -97,12 +112,10 @@ bool ConvertCubemap(const OSString &filename,const OSStringList &file_list,const image[face].Resize(width,height); } } - - tex_file_creater->Close(); - - delete tex_file_creater; } - + + tex_file_creater->Close(); + LOG_INFO(OS_TEXT("pixel total length: ")+OSString::valueOf(total)+OS_TEXT(" bytes.")); return(true); } @@ -118,7 +131,7 @@ int os_main(int argc,os_char **argv) if(argc<=7) { std::cout<< "Command format:\n" - "\tCubemapConv [/R:][/RG:][/RGB:][/RGBA:] [/mip] ,,,,,\n\n"; + "\tCubemapConv [/R:][/RG:][/RGB:][/RGBA:] [/mip] ,,,,,\n\n"; PrintFormatList(); return 0; @@ -138,20 +151,36 @@ int os_main(int argc,os_char **argv) CMP_RegisterHostPlugins(); CMP_InitializeBCLibrary(); - OSString out_filename=argv[argc-8]; + OSString out_filename=argv[argc-7]; OSStringList file_list; - for(int i=argc-7;iWrite(file_type_name.c_str(),file_type_name.Length()); dos->WriteUint8(0x1A); dos->WriteUint8(3); //版本 - dos->WriteUint8(mip_level); //mipmaps级数 - dos->WriteUint32(image->width()); - if(type!=TextureFileType::Tex1D - ||type!=TextureFileType::Tex1DArray) - dos->WriteUint32(image->height()); + return(true); +} - if(type==TextureFileType::Tex3D) - dos->WriteUint32(image->depth()); - - if(pixel_format->format>ColorFormat::COMPRESS) +bool TextureFileCreater::WriteSize1D(const uint mip_level,const uint length) +{ + if(!dos->WriteUint8(mip_level))return(false); //mipmaps级数 + if(!dos->WriteUint32(length))return(false); + + return(true); +} + +bool TextureFileCreater::WriteSize2D(const uint mip_level, const uint width,const uint height) +{ + if(!dos->WriteUint8(mip_level))return(false); //mipmaps级数 + if(!dos->WriteUint32(width))return(false); + if(!dos->WriteUint32(height))return(false); + + return(true); +} + +bool TextureFileCreater::WritePixelFormat() +{ + if (pixel_format->format > ColorFormat::COMPRESS) { - dos->WriteUint8(0); - dos->WriteUint16(uint(pixel_format->format)-uint(ColorFormat::BC1RGB)); + if(!dos->WriteUint8(0))return(false); + if(!dos->WriteUint16(uint(pixel_format->format)-uint(ColorFormat::BC1RGB)))return(false); } else { - dos->WriteUint8(pixel_format->channels); //颜色通道数 - dos->WriteUint8((uint8 *)pixel_format->color,4); //颜色标记 - dos->WriteUint8(pixel_format->bits,4); //颜色位数 - dos->WriteUint8((uint8)pixel_format->type); //数据类型 + if(!dos->WriteUint8(pixel_format->channels))return(false); //颜色通道数 + if(!dos->WriteUint8((uint8*)pixel_format->color, 4))return(false); //颜色标记 + if(!dos->WriteUint8(pixel_format->bits, 4))return(false); //颜色位数 + if(!dos->WriteUint8((uint8)pixel_format->type))return(false); //数据类型 } - return(true); + return(true); } uint32 TextureFileCreater::Write(void *data,const uint total_bytes) @@ -165,4 +178,32 @@ void TextureFileCreater::Delete() Close(); filesystem::FileDelete(filename); +} + +TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *); +TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *); +TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *); +TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *); + +TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *); + +TextureFileCreater *CreateTFC(const PixelFormat *fmt,const int channels) +{ + if(!fmt)return(nullptr); + if(channels<1||channels>4)return(nullptr); + + using CTFC_FUNC=TextureFileCreater *(*)(const PixelFormat *); + + static CTFC_FUNC CreateTFC[4]= + { + CreateTextureFileCreaterR, + CreateTextureFileCreaterRG, + CreateTextureFileCreaterRGB, + CreateTextureFileCreaterRGBA + }; + + if(fmt->formatchannels(); type=image->type(); @@ -255,7 +257,7 @@ public: } };//class TextureFileCreaterCompress:public TextureFileCreater -TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *pf,ILImage *image) +TextureFileCreater *CreateTextureFileCreaterCompress(const PixelFormat *pf) { - return(new TextureFileCreaterCompress(pf,image)); + return(new TextureFileCreaterCompress(pf)); } diff --git a/TextureFileCreaterR.cpp b/TextureFileCreaterR.cpp index c1e74a4..dde8576 100644 --- a/TextureFileCreaterR.cpp +++ b/TextureFileCreaterR.cpp @@ -12,8 +12,10 @@ public: public: - bool InitFormat() override + bool InitFormat(ILImage *img) override { + image=img; + if(!ToILType(type,pixel_format->bits[0],pixel_format->type)) return(false); @@ -36,7 +38,7 @@ public: } };//class TextureFileCreaterR:public TextureFileCreater -TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *pf,ILImage *image) +TextureFileCreater *CreateTextureFileCreaterR(const PixelFormat *pf) { - return(new TextureFileCreaterR(pf,image)); + return(new TextureFileCreaterR(pf)); } \ No newline at end of file diff --git a/TextureFileCreaterRG.cpp b/TextureFileCreaterRG.cpp index 191f987..a9d2192 100644 --- a/TextureFileCreaterRG.cpp +++ b/TextureFileCreaterRG.cpp @@ -12,8 +12,10 @@ public: public: - bool InitFormat() override + bool InitFormat(ILImage *img) override { + image=img; + if(!ToILType(type,pixel_format->bits[0],pixel_format->type)) return(false); @@ -34,7 +36,7 @@ public: } };//class TextureFileCreaterRG:public TextureFileCreater -TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *pf,ILImage *image) +TextureFileCreater *CreateTextureFileCreaterRG(const PixelFormat *pf) { - return(new TextureFileCreaterRG(pf,image)); + return(new TextureFileCreaterRG(pf)); } diff --git a/TextureFileCreaterRGB.cpp b/TextureFileCreaterRGB.cpp index a405c1e..6a90ba2 100644 --- a/TextureFileCreaterRGB.cpp +++ b/TextureFileCreaterRGB.cpp @@ -12,8 +12,10 @@ public: public: - bool InitFormat() override + bool InitFormat(ILImage* img) override { + image = img; + if(pixel_format->format==ColorFormat::RGB32U ||pixel_format->format==ColorFormat::RGB32I ||pixel_format->format==ColorFormat::RGB32F) @@ -114,7 +116,7 @@ public: } };//class TextureFileCreaterRGB:public TextureFileCreater -TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *pf,ILImage *image) +TextureFileCreater *CreateTextureFileCreaterRGB(const PixelFormat *pf) { - return(new TextureFileCreaterRGB(pf,image)); + return(new TextureFileCreaterRGB(pf)); } diff --git a/TextureFileCreaterRGBA.cpp b/TextureFileCreaterRGBA.cpp index 770736f..d15af28 100644 --- a/TextureFileCreaterRGBA.cpp +++ b/TextureFileCreaterRGBA.cpp @@ -9,9 +9,11 @@ class TextureFileCreaterRGBA:public TextureFileCreater public: using TextureFileCreater::TextureFileCreater; - - bool InitFormat() override + + bool InitFormat(ILImage* img) override { + image = img; + if(pixel_format->format==ColorFormat::RGBA8 ||pixel_format->format==ColorFormat::RGBA16 ||pixel_format->format==ColorFormat::RGBA16U @@ -182,7 +184,7 @@ public: } };//class TextureFileCreaterRGB:public TextureFileCreater -TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *pf,ILImage *image) +TextureFileCreater *CreateTextureFileCreaterRGBA(const PixelFormat *pf) { - return(new TextureFileCreaterRGBA(pf,image)); + return(new TextureFileCreaterRGBA(pf)); }