From 97f38a39dc356475ba58f4027dea0a882bf4e565 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 8 Jan 2020 15:54:58 +0800 Subject: [PATCH] new combo texture mode --- CMakeLists.txt | 12 +-- ComboTexture.cpp | 156 +++++++++++++++++++++++++++++ ILImage.h | 6 +- ILImageSupport.cpp | 12 ++- MicroPBR.cpp | 242 --------------------------------------------- YUV.cpp | 87 +++++----------- YUVTest.cpp | 23 +++-- 7 files changed, 208 insertions(+), 330 deletions(-) create mode 100644 ComboTexture.cpp delete mode 100644 MicroPBR.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a8d13da..12a07a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,11 +53,11 @@ target_link_libraries(TexConv PRIVATE CMCore CMPlatform CMUtil Qt5::Core Qt5::Gui Qt5::Widgets DevIL ILU) -add_executable(MicroPBR MicroPBR.cpp YUV.cpp SpheremapNormal.cpp ${ILIMAGE_SOURCE}) -target_link_libraries(MicroPBR PRIVATE CMCore CMPlatform CMUtil DevIL ILU MathGeoLib) +add_executable(ComboTexture ComboTexture.cpp YUV.cpp ${ILIMAGE_SOURCE}) +target_link_libraries(ComboTexture PRIVATE CMCore CMPlatform CMUtil DevIL ILU MathGeoLib) -add_executable(YUVTest YUVTest.cpp YUV.cpp SpheremapNormal.cpp ${ILIMAGE_SOURCE}) -target_link_libraries(YUVTest PRIVATE CMCore CMPlatform CMUtil DevIL ILU MathGeoLib) +#add_executable(YUVTest YUVTest.cpp YUV.cpp SpheremapNormal.cpp ${ILIMAGE_SOURCE}) +#target_link_libraries(YUVTest PRIVATE CMCore CMPlatform CMUtil DevIL ILU MathGeoLib) -add_executable(NormalTest NormalTest.cpp SpheremapNormal.cpp ${ILIMAGE_SOURCE}) -target_link_libraries(NormalTest PRIVATE CMCore CMPlatform CMUtil DevIL ILU MathGeoLib) \ No newline at end of file +#add_executable(NormalTest NormalTest.cpp SpheremapNormal.cpp ${ILIMAGE_SOURCE}) +#target_link_libraries(NormalTest PRIVATE CMCore CMPlatform CMUtil DevIL ILU MathGeoLib) \ No newline at end of file diff --git a/ComboTexture.cpp b/ComboTexture.cpp new file mode 100644 index 0000000..4b263d0 --- /dev/null +++ b/ComboTexture.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include + +using namespace hgl; +using namespace hgl::util; + +#if HGL_OS == HGL_OS_Windows + #define std_cout std::wcout +#else + #define std_cout std::cout +#endif// + +namespace hgl +{ + void RGB2YUV(uint8 *y,uint8 *u,uint8 *v,const uint8 *rgb,const uint count); + void normal_compress(uint8 *x,uint8 *y,const uint8 *rgb,const uint count); + + template + void MixRGB(T *rgb,const T *r,const T *g,const T *b,const uint count) + { + for(uint i=0;i + void MixRGBA(T *rgba,const T *r,const T *g,const T *b,const T *a,const uint count) + { + for(uint i=0;i pixels=new uint8[w*h*4]; + + MixRGBA(pixels,r,g,b,a,w*h); + + if(SaveImageToFile(out_filename,w,h,scale,4,IL_UNSIGNED_BYTE,pixels)) + { + std_cout< pixels=new uint8[w*h*3]; + + MixRGB(pixels,r,g,b,w*h); + + if(SaveImageToFile(out_filename,w,h,scale,3,IL_UNSIGNED_BYTE,pixels)) + { + std_cout< C2G1 Color1.png Color2.png Grayscale.png"< MPBR6 Color.png Matallic.png Roughness.png Grayscale.png"< MPBR7 Color.png Matallic.png Roughness.png Grayscale1.png Grayscale2.png"<>2; + + AutoDeleteArray y1=new uint8[pixel_total]; + AutoDeleteArray y2=new uint8[pixel_total]; + AutoDeleteArray u1=new uint8[half_pixel_total]; + AutoDeleteArray u2=new uint8[half_pixel_total]; + AutoDeleteArray v1=new uint8[half_pixel_total]; + AutoDeleteArray v2=new uint8[half_pixel_total]; + + RGB2YUV(y1,u1,v1,(uint8 *)c1.GetRGB(IL_UNSIGNED_BYTE),pixel_total); + RGB2YUV(y2,u2,v2,(uint8 *)c2.GetRGB(IL_UNSIGNED_BYTE),pixel_total); + + SaveRGBFile( argv[1], + w,h,1.0, + y1,y2,(uint8 *)g.GetLum(IL_UNSIGNED_BYTE), + OS_TEXT("Y2G")); + + SaveRGBAFile( argv[1], + w,h,0.5, + u1,v1,u2,v2, + OS_TEXT("UV2")); + } + + ilShutDown(); + return 0; +} diff --git a/ILImage.h b/ILImage.h index 87619a3..f673ffc 100644 --- a/ILImage.h +++ b/ILImage.h @@ -44,8 +44,8 @@ public: bool Resize(uint,uint); - void ToRGB(ILuint type=IL_UNSIGNED_BYTE); - void ToGray(ILuint type=IL_UNSIGNED_BYTE); + void *ToRGB(ILuint type=IL_UNSIGNED_BYTE); + void *ToGray(ILuint type=IL_UNSIGNED_BYTE); void *GetR(ILuint type); @@ -58,4 +58,4 @@ public: void *GetLum(ILuint type){return GetData(IL_LUMINANCE,type);} };//class ILImage -bool SaveImageToFile(const OSString &filename,ILuint w,ILuint h,ILuint c,ILuint t,void *data); +bool SaveImageToFile(const OSString &filename,ILuint w,ILuint h,const float scale,ILuint c,ILuint t,void *data); diff --git a/ILImageSupport.cpp b/ILImageSupport.cpp index 9b9f915..cef7105 100644 --- a/ILImageSupport.cpp +++ b/ILImageSupport.cpp @@ -65,7 +65,7 @@ constexpr ILenum format_by_channel[]= IL_RGBA, }; -bool SaveImageToFile(const OSString &filename,ILuint w,ILuint h,ILuint c,ILuint t,void *data) +bool SaveImageToFile(const OSString &filename,ILuint w,ILuint h,const float scale,ILuint c,ILuint t,void *data) { if(filename.IsEmpty())return(false); if(w<=0||h<=1)return(false); @@ -80,6 +80,8 @@ bool SaveImageToFile(const OSString &filename,ILuint w,ILuint h,ILuint c,ILuint if(!ilTexImage(w,h,1,c,format_by_channel[c-1],t,data)) return(false); + iluScale(w*scale,h*scale,1); + iluFlipImage(); ilEnable(IL_FILE_OVERWRITE); @@ -187,16 +189,20 @@ bool ILImage::LoadFile(const OSString &filename) return(true); } -void ILImage::ToRGB(ILuint type) +void *ILImage::ToRGB(ILuint type) { if(il_format!=IL_RGB) Convert(IL_RGB,type); + + return ilGetData(); } -void ILImage::ToGray(ILuint type) +void *ILImage::ToGray(ILuint type) { if(il_format!=IL_LUMINANCE) Convert(IL_LUMINANCE,type); + + return ilGetData(); } void *ILImage::GetR(ILuint type) diff --git a/MicroPBR.cpp b/MicroPBR.cpp deleted file mode 100644 index f4aa9e4..0000000 --- a/MicroPBR.cpp +++ /dev/null @@ -1,242 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace hgl; -using namespace hgl::util; - -#if HGL_OS == HGL_OS_Windows - #define std_cout std::wcout -#else - #define std_cout std::cout -#endif// - -namespace hgl -{ - void RGB2YUV(uint8 *y,uint8 *u,uint8 *v,const uint8 *rgb,const uint w,const uint h); - void normal_compress(uint8 *x,uint8 *y,const uint8 *rgb,const uint count); - - template - void MixRGB(T *rgb,const T *r,const T *g,const T *b,const uint count) - { - for(uint i=0;i - void MixRGBA(T *rgba,const T *r,const T *g,const T *b,const T *a,const uint count) - { - for(uint i=0;i pixels=new uint8[w*h*3]; - - MixRGBA(pixels,r,g,b,a,w*h); - - if(SaveImageToFile(out_filename,w,h,4,IL_UNSIGNED_BYTE,pixels)) - { - std_cout< pixels=new uint8[w*h*3]; - - MixRGB(pixels,r,g,b,w*h); - - if(SaveImageToFile(out_filename,w,h,3,IL_UNSIGNED_BYTE,pixels)) - { - std_cout< /C:BaseColor.png /N:Normal.png /M:Metallic.png /R:Roughness.png"<>1; - const uint half_h=h>>1; - const uint pixel_total=w*h; - const uint half_pixel_total=half_w*half_h; - - if(metallic.isHas()) - { - metallic.ToGray(); - metallic.Resize(half_w,half_h); - } - - if(roughness.isHas()) - { - roughness.ToGray(); - roughness.Resize(half_w,half_h); - } - - AutoDeleteArray y,u,v,nx,ny; - - y=new uint8[pixel_total]; - u=new uint8[half_pixel_total]; - v=new uint8[half_pixel_total]; - nx=new uint8[pixel_total]; - ny=new uint8[pixel_total]; - - // BaseColor Y + Normal XY - { - RGB2YUV(y,u,v,color.GetRGB(),w,h); - normal_compress(nx,ny,normal.GetRGB(),w*h); - - SaveRGBFile( argv[1], - w,h, - y,nx,ny, - OS_TEXT("YN")); - } - - if(metallic.isHas()&&roughness.isHas()) - { - SaveRGBAFile(argv[1], - half_w,half_h, - u,v,metallic.GetLum(),roughness.GetLum(), - OS_TEXT("UVMR")); - } - else - if(metallic.isHas()) - { - SaveRGBFile( argv[1], - half_w,half_h, - u,v,metallic.GetLum(), - OS_TEXT("UVM")); - } - else - if(roughness.isHas()) - { - SaveRGBFile( argv[1], - half_w,half_h, - u,v,roughness.GetLum(), - OS_TEXT("UVR")); - } - - ilShutDown(); - return 0; -} diff --git a/YUV.cpp b/YUV.cpp index adc3a5b..aa8a8ed 100644 --- a/YUV.cpp +++ b/YUV.cpp @@ -5,82 +5,39 @@ using namespace hgl; namespace hgl { - void write_to_yuv_frame(uint w,uint h,uint8 *yuv,uint8 *yuv_y,uint8 *yuv_u,uint8 *yuv_v) - { - uint x; - uint y; - - uint yuv_h=((h+1)>>1)<<1; - uint yuv_w=((w+1)>>1)<<1; - - memset(yuv_y,0,yuv_h*yuv_w); - memset(yuv_u,0,yuv_h*yuv_w/4); - memset(yuv_v,0,yuv_h*yuv_w/4); - - for(y = 0; y < h; y++) - for(x = 0; x < w; x++) - yuv_y[x + y * yuv_w] = yuv[3 * (x + y * w) + 0]; - - for(y = 0; y < h; y += 2) - for(x = 0; x < w; x += 2) - { - yuv_u[(x >> 1) + (y >> 1) * (yuv_w >> 1)] = yuv[3 * (x + y * w) + 1]; - yuv_v[(x >> 1) + (y >> 1) * (yuv_w >> 1)] = yuv[3 * (x + y * w) + 2]; - } - } - - uint8 __inline clamp(double d) - { - if(d < 0 )return 0; - if(d > 255 )return 255; - - return d; - } - const double clamp_u=0.436*255.0; const double clamp_v=0.615*255.0; - - void RGB2YUV(uint8 *y,uint8 *u,uint8 *v,const uint8 *rgb,const uint w,const uint h) + + void RGB2YUV(uint8 *y,uint8 *u,uint8 *v,const uint8 *rgb,const uint count) { - const uint8 *sp=rgb; - uint8 *tp,*temp=new uint8[w*h*3]; + uint8 r,g,b; - int size=w*h; - - tp=temp; - - while(size--) + for(uint i=0;i>1)<<1; - uint yuv_w=((w+1)>>1)<<1; - - write_to_yuv_frame(w,h,temp,y,u,v); - - delete[] temp; } }//namespace hgl namespace hgl { + static bool yuv2rgb=false; static int sAdjCrr[256]; static int sAdjCrg[256]; static int sAdjCbg[256]; static int sAdjCbb[256]; static int sAdjY[256]; - static uint8 sClampBuff[1024]; - static uint8 *sClamp = sClampBuff + 384; + static unsigned char sClampBuff[1024]; + static unsigned char *sClamp = sClampBuff + 384; - static uint8 YV_R[256][256]; - static uint8 YU_B[256][256]; + static unsigned char YV_R[256][256]; + static unsigned char YU_B[256][256]; inline int getMin(int a, int b) { @@ -119,28 +76,30 @@ namespace hgl YV_R[v][y]=sClamp[sAdjY[y]+sAdjCrr[v]]; YU_B[v][y]=sClamp[sAdjY[y]+sAdjCbb[v]]; } - } + yuv2rgb=true; + } + void YUV2RGB(uint8 *rgb,const uint8 *y,const uint8 *u,const uint8 *v,const uint w,const uint h) { const uint line_gap=w*3; const uint y_stride=w; - const uint uv_stride=w/2; + const uint uv_stride=w>>1; const uint8 *pY0, *pY1, *pU, *pV; - for(uint _y = 0; _y < h; _y += 2) + for(int _y = 0; _y < h; _y += 2) { pY0 = y + _y * y_stride; pY1 = y + (_y | 1)* y_stride; pU = u + (( _y * uv_stride) >> 1); pV = v + (( _y * uv_stride) >> 1); - uint8 *dst0=rgb; - uint8 *dst1=rgb+line_gap; + unsigned __int8 *dst0=rgb; + unsigned __int8 *dst1=rgb+line_gap; - for(uint _x = 0; _x < w; _x += 2) + for(int _x = 0; _x < w; _x += 2) { const int G = sAdjCrg[*pV] + sAdjCbg[*pU]; diff --git a/YUVTest.cpp b/YUVTest.cpp index 9c29c74..6fa9687 100644 --- a/YUVTest.cpp +++ b/YUVTest.cpp @@ -8,11 +8,10 @@ using namespace hgl::filesystem; namespace hgl { - void RGB2YUV(uint8 *y,uint8 *u,uint8 *v,const uint8 *rgb,const uint w,const uint h); + void InitYUV2RGBDecode(); + void RGB2YUV(uint8 *y,uint8 *u,uint8 *v,const uint8 *rgb,const uint count); void YUV2RGB(uint8 *rgb,const uint8 *y,const uint8 *u,const uint8 *v,const uint w,const uint h); - - void InitYUV2RGBDecode(); - + void normal_compress(uint8 *x,uint8 *y,const uint8 *rgb,const uint count); void normal_decompress(uint8 *rgb,const uint8 *x,const uint8 *y,const uint count); } @@ -29,9 +28,9 @@ int main(int argc,char **argv) { if(argc<1) return 0; + + InitYUV2RGBDecode(); - InitYUV2RGBDecode(); - ilInit(); ILImage rgb_image; @@ -44,13 +43,13 @@ int main(int argc,char **argv) uint8 *rgb=(uint8 *)rgb_image.GetRGB(IL_UNSIGNED_BYTE); - const uint pixels=rgb_image.pixel_total(); + const uint pixel_total=rgb_image.pixel_total(); - uint8 *y=new uint8[pixels]; - uint8 *u=new uint8[pixels/4]; - uint8 *v=new uint8[pixels/4]; + uint8 *y=new uint8[pixel_total]; + uint8 *u=new uint8[pixel_total>>2]; + uint8 *v=new uint8[pixel_total>>2]; - RGB2YUV(y,u,v,rgb,rgb_image.width(),rgb_image.height()); + RGB2YUV(y,u,v,rgb,pixel_total); YUV2RGB(rgb,y,u,v,rgb_image.width(),rgb_image.height()); @@ -59,7 +58,7 @@ int main(int argc,char **argv) filename=ClipFileMainname(argv[1]); filename+=OS_TEXT("_YUV.png"); - if(SaveImageToFile(filename,rgb_image.width(),rgb_image.height(),3,IL_UNSIGNED_BYTE,rgb)) + if(SaveImageToFile(filename,rgb_image.width(),rgb_image.height(),1.0,3,IL_UNSIGNED_BYTE,rgb)) std_cout<