Fixes saveScaledImage to handle DDS format files, since DDS's go through a separate resource loader

This commit is contained in:
JeffR 2022-02-25 16:55:05 -06:00
parent 5e26ce037b
commit 7fe85ab7d5

View file

@ -32,6 +32,7 @@
#include "console/console.h"
#include "platform/profiler.h"
#include "console/engineAPI.h"
#include "gfx/bitmap/ddsFile.h"
using namespace Torque;
@ -1362,30 +1363,48 @@ DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
}
DefineEngineFunction(saveScaledImage, bool, (const char* bitmapSource, const char* bitmapDest, S32 resolutionSize), ("", "", 512),
"Returns image info in the following format: width TAB height TAB bytesPerPixel TAB format. "
"It will return an empty string if the file is not found.\n"
"@ingroup Rendering\n")
"Loads an image from the source path, and scales it down to the target resolution before"
"Saving it out to the destination path.\n")
{
Resource<GBitmap> image = GBitmap::load(bitmapSource);
if (!image)
return false;
bool isDDS = false;
Torque::Path sourcePath = Torque::Path(bitmapSource);
/*if (String("dds").equal(sourcePath.getExtension(), String::NoCase))
//First, gotta check the extension, as we have some extra work to do if it's
//a DDS file
const char* ret = dStrrchr(bitmapSource, '.');
if (ret)
{
dds = DDSFile::load(correctPath, scalePower);
if (String::ToLower(ret) == String(".dds"))
isDDS = true;
}
else
{
return false; //no extension? bail out
}
GBitmap* image;
if (isDDS)
{
Resource<DDSFile> dds = DDSFile::load(bitmapSource, 0);
if (dds != NULL)
{
image = new GBitmap();
if (!dds->decompressToGBitmap(image))
{
delete image;
image = NULL;
return false;
}
}
}*/
if (isPow2(image->getWidth())&& isPow2(image->getHeight()))
}
else
{
image = GBitmap::load(bitmapSource);
}
if (!image)
return false;
Torque::Path sourcePath = Torque::Path(bitmapSource);
if (isPow2(image->getWidth()) && isPow2(image->getHeight()))
image->extrudeMipLevels();
U32 mipCount = image->getNumMipLevels();
@ -1396,9 +1415,14 @@ DefineEngineFunction(saveScaledImage, bool, (const char* bitmapSource, const cha
image->chopTopMips(mipCount - targetMips);
}
//TODO: support different format targets, for now we just force
//to png for simplicity
Torque::Path destinationPath = Torque::Path(bitmapDest);
destinationPath.setExtension("png");
// Open up the file on disk.
FileStream fs;
if (!fs.open(bitmapDest, Torque::FS::File::Write))
if (!fs.open(destinationPath.getFullPath(), Torque::FS::File::Write))
{
Con::errorf("saveScaledImage() - Failed to open output file '%s'!", bitmapDest);
return false;