From 5ccf0df1ab597197b090959465341ace77ba4271 Mon Sep 17 00:00:00 2001 From: kervala Date: Mon, 28 Dec 2015 16:37:32 +0100 Subject: [PATCH 1/3] Changed: Allow panoply_maker to optimize textures and masks (remove alpha channel if useless) --HG-- branch : develop --- .../tools/3d/panoply_maker/panoply_maker.cpp | 147 ++++++++++++++++-- 1 file changed, 135 insertions(+), 12 deletions(-) diff --git a/code/nel/tools/3d/panoply_maker/panoply_maker.cpp b/code/nel/tools/3d/panoply_maker/panoply_maker.cpp index ac9bbc3d7..80aabfaba 100644 --- a/code/nel/tools/3d/panoply_maker/panoply_maker.cpp +++ b/code/nel/tools/3d/panoply_maker/panoply_maker.cpp @@ -55,16 +55,17 @@ string DivideBy2Dir= "/d4/"; /// describes the building infos struct CBuildInfo { - std::string InputPath; - std::string OutputPath; - std::string HlsInfoPath; - std::string CachePath; - std::vector BitmapExtensions; // the supported extension for bitmaps - std::string OutputFormat; // png or tga - std::string DefaultSeparator; - TColorMaskVect ColorMasks; + std::string InputPath; + std::string OutputPath; + std::string HlsInfoPath; + std::string CachePath; + std::vector BitmapExtensions; // the supported extension for bitmaps + std::string OutputFormat; // png or tga + std::string DefaultSeparator; + TColorMaskVect ColorMasks; // how to shift right the size of the src Bitmap for the .hlsinfo - uint LowDefShift; + uint LowDefShift; + uint OptimizeTextures; // 0 = don't optimize, 1 = check, 2 = optimize }; @@ -345,6 +346,15 @@ int main(int argc, char* argv[]) bi.LowDefShift= 3; } + try + { + bi.OptimizeTextures = cf.getVar ("optimize_textures").asInt(); + } + catch (const NLMISC::EUnknownVar &) + { + // don't optimize files by default + bi.OptimizeTextures = 0; + } } catch (const std::exception &e) { @@ -648,11 +658,83 @@ static void BuildColoredVersionForOneBitmap(const CBuildInfo &bi, const std::str if (is.open(fullInputBitmapPath)) { depth = srcBitmap.load(is); + is.close(); + if (depth == 0 || srcBitmap.getPixels().empty()) { throw NLMISC::Exception("Failed to load bitmap"); } + // if bitmap is RGBA but has an alpha channel fully transparent, + // we can save it as RGB to optimize it + if (bi.OptimizeTextures > 0 && depth == 32) + { + uint32 size = srcBitmap.getSize(0); + + if (size > 0) + { + // get a pointer on original data + uint8 *data = srcBitmap.getPixels().getPtr(); + + // pointer on first alpha value + uint8 *tmp = data + 3; + uint8 *endData = data + size; + uint8 value = *tmp; + + // check if all alphas have the same value + while(tmp < endData && *tmp == value) tmp += 4; + + // texture can be converted if all alphas are 0 or 255 + if (tmp >= endData && (value == 255 || value == 0)) + { + if (bi.OptimizeTextures > 1) + { + // original depth is now 24 bits, since we discarded alpha channel + depth = 24; + + // if texture is fully transparent, make it fully opaque + if (value == 0) + { + tmp = data + 3; + + while(tmp < endData) + { + *tmp = 255; + tmp += 4; + } + } + + NLMISC::COFile os; + + if (os.open(fullInputBitmapPath)) + { + nlwarning("Optimizing texture %s...", fullInputBitmapPath.c_str()); + + std::string ext = CFile::getExtension(fullInputBitmapPath); + + // resave the texture in optimized same format + if (ext == "png") + { + srcBitmap.writePNG(os, 24); + } + else if (ext == "tga") + { + srcBitmap.writeTGA(os, 24); + } + else + { + nlwarning("Don't support %s format for texture, unable to save it", ext.c_str()); + } + } + } + else + { + nlwarning("Texture %s can be optimized", fullInputBitmapPath.c_str()); + } + } + } + } + if (srcBitmap.PixelFormat != NLMISC::CBitmap::RGBA) { srcBitmap.convertToType(NLMISC::CBitmap::RGBA); @@ -702,8 +784,8 @@ static void BuildColoredVersionForOneBitmap(const CBuildInfo &bi, const std::str for (k = 0; k < bi.ColorMasks.size(); ++k) { std::string maskName = fileName + "_" + bi.ColorMasks[k].MaskExt + "." + fileExt; - std::string maskFileName = NLMISC::CPath::lookup(maskName, - false, false); + std::string maskFileName = NLMISC::CPath::lookup(maskName, false, false); + if (!maskFileName.empty()) // found the mask ? { CLoopInfo li; @@ -717,11 +799,52 @@ static void BuildColoredVersionForOneBitmap(const CBuildInfo &bi, const std::str if (is.open(maskFileName)) { - if (li.Mask.load(is) == 0 || li.Mask.getPixels().empty()) + // masks are always opaque, if the mask is 8bits, it's in grayscale + li.Mask.loadGrayscaleAsAlpha(false); + + uint8 maskDepth = li.Mask.load(is); + + is.close(); + + if (maskDepth == 0 || li.Mask.getPixels().empty()) { throw NLMISC::Exception("Failed to load mask"); } + // masks can be converted to grayscale files + if (bi.OptimizeTextures > 0 && maskDepth > 8) + { + if (bi.OptimizeTextures > 1) + { + NLMISC::COFile os; + + if (os.open(maskFileName)) + { + std::string ext = CFile::getExtension(maskFileName); + + nlwarning("Optimizing mask %s...", maskFileName.c_str()); + + // resave the texture in optimized same format + if (ext == "png") + { + li.Mask.writePNG(os, 8); + } + else if (ext == "tga") + { + li.Mask.writeTGA(os, 8); + } + else + { + nlwarning("Don't support %s format for mask, unable to save it", ext.c_str()); + } + } + } + else + { + nlwarning("Mask %s can be optimized", maskFileName.c_str()); + } + } + if (li.Mask.PixelFormat != NLMISC::CBitmap::RGBA) { li.Mask.convertToType(NLMISC::CBitmap::RGBA); From b93414cd272bbf4963c7fbddf4f31a7f76748b81 Mon Sep 17 00:00:00 2001 From: kervala Date: Mon, 28 Dec 2015 16:41:26 +0100 Subject: [PATCH 2/3] Fixed: Warning when DDS_HEADER is not initialized --HG-- branch : develop --- code/nel/tools/3d/s3tc_compressor_lib/s3tc_compressor.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/code/nel/tools/3d/s3tc_compressor_lib/s3tc_compressor.cpp b/code/nel/tools/3d/s3tc_compressor_lib/s3tc_compressor.cpp index 9d6506197..1ffeabf77 100644 --- a/code/nel/tools/3d/s3tc_compressor_lib/s3tc_compressor.cpp +++ b/code/nel/tools/3d/s3tc_compressor_lib/s3tc_compressor.cpp @@ -179,6 +179,8 @@ void CS3TCCompressor::compress(const NLMISC::CBitmap &bmpSrc, bool optMipMap, u DDS_HEADER dest; NLMISC::CBitmap picSrc= bmpSrc; + // initialize DDS_HEADER + memset(&dest, 0, sizeof(dest)); // For all mipmaps, compress. if(optMipMap) From 77352d575970ac485ca393c3f69fab6bf4ae041c Mon Sep 17 00:00:00 2001 From: kervala Date: Mon, 28 Dec 2015 16:43:20 +0100 Subject: [PATCH 3/3] Changed: Minor change --HG-- branch : develop --- code/nel/include/nel/misc/ucstring.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/nel/include/nel/misc/ucstring.h b/code/nel/include/nel/misc/ucstring.h index 2f921f9ae..605a14a78 100644 --- a/code/nel/include/nel/misc/ucstring.h +++ b/code/nel/include/nel/misc/ucstring.h @@ -355,7 +355,7 @@ namespace NLMISC // Traits for hash_map using CEntityId struct CUCStringHashMapTraits { - enum { bucket_size = 4, min_buckets = 8, }; + enum { bucket_size = 4, min_buckets = 8 }; CUCStringHashMapTraits() { } size_t operator() (const ucstring &id ) const {