Sometime it might be useful to process the texture files to give them smaller size or compress them using a different algorithm.
For example, some might want to use mozjpeg or oxipng to reduce the size of the output file.
The simplest way to do that is to process textures before it is saved by the optimizer.
Your first have to override the default CBatchOptimizer by your own one (ie. CMyBatchOptimizer) and override
Code: Select all
virtual void OnBatchStateChanged(BatchState state, void* info) {};
Code: Select all
void CMyBatchOptimizer::OnBatchStateChanged(BatchState state, void* info)
{
if (state == BATCH_PRESAVE_SCENE)
{
C3DScene *scene = (C3DScene *) info;
CXString srcfile, dstfile;
HashPos pos = scene->GetFirstMaterial();
while (pos != HashEnd)
{
C3DMaterial* mat = scene->GetNextMaterial(pos);
MapType type;
CMaterialMap* map;
HashPos pos2 = mat->GetFirstMap();
while (pos2 != HashEnd)
{
map = mat->GetNextMap(pos2, type);
// alpha path
srcfile = map->GetBitmapPath(false, true);
if (!srcfile.IsEmpty())
{
// You can call a process to retrieve size of the image for example.
// If image is too large, you may reduce it or you can call some compression tools such oxipng appropriately
//
// During that process, you may either replace the original file which is really not recommanded, or save a copy to any location.
// Using C3DScene::GetFilepath, you can know the scene filename and save the new bitmap file to folder relative to the scene folder.
// Using then map->AdjustPath(CXString newfilename, const CXString& newTargetPath = CXString(), bool allowRelative = true) const; //!< Adjust a single filename and make it relative to newTargetPath if provided or the scene path otherwise.
// you will attach the new texture file to the material map and make it saved when CBatchOptimizer::SaveCruncherBatchCallback is called.
}
// alpha path
srcfile = map->GetBitmapPath(true, true);
if (!srcfile.IsEmpty())
{
// Same has above
}
}
}
}
}