Browse Source

Merge pull request #733 from Areloch/saveScaledDDSImageFix

Fixes saveScaledImage to handle DDS format files, since DDS's go through a separate resource loader
Brian Roberts 3 years ago
parent
commit
96669453d8
1 changed files with 37 additions and 13 deletions
  1. 37 13
      Engine/source/gfx/bitmap/gBitmap.cpp

+ 37 - 13
Engine/source/gfx/bitmap/gBitmap.cpp

@@ -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);
+   //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)
+   {
+      if (String::ToLower(ret) == String(".dds"))
+         isDDS = true;
+   }
+   else
+   {
+      return false; //no extension? bail out
+   }
 
-   /*if (String("dds").equal(sourcePath.getExtension(), String::NoCase))
+   GBitmap* image;
+   if (isDDS)
    {
-      dds = DDSFile::load(correctPath, scalePower);
+      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;