Browse Source

Bug fixes:

Generating image previews of image assets was failing

DDS remove redundant check for stream status.

STB requires the file to be free before being written to, move check to make sure we can open the path into gBitmap and remove FileStream checks from everywhere else.
marauder2k7 1 năm trước cách đây
mục cha
commit
63682c43ec

+ 6 - 4
Engine/source/gfx/bitmap/ddsFile.cpp

@@ -568,12 +568,14 @@ void DDSFile::SurfaceData::dumpImage(DDSFile *dds, U32 mip, const char *file)
    
    
    FileStream  stream;
    FileStream  stream;
 
 
-   stream.open( file, Torque::FS::File::Write );
+   if (!stream.open(file, Torque::FS::File::Write))
+   {
+      Con::errorf("DDSFile::SurfaceData::dumpImage() - Error opening file for writing: %s !", file);
+   }
 
 
-   if ( stream.getStatus() == Stream::Ok )
+   if(!foo->writeBitmap("png", file))
    {
    {
-      // Write it out.
-      foo->writeBitmap("png", file);
+      Con::errorf("DDSFile::SurfaceData::dumpImage() - Error writing %s !", file);
    }
    }
 
 
    // Clean up.
    // Clean up.

+ 15 - 11
Engine/source/gfx/bitmap/gBitmap.cpp

@@ -1208,6 +1208,17 @@ bool  GBitmap::readBitmap(const String& bmType, const Torque::Path& path)
 
 
 bool  GBitmap::writeBitmap( const String &bmType, const Torque::Path& path, U32 compressionLevel )
 bool  GBitmap::writeBitmap( const String &bmType, const Torque::Path& path, U32 compressionLevel )
 {
 {
+   FileStream stream;
+   if (!stream.open(path, Torque::FS::File::Write))
+   {
+      Con::errorf("GBitmap::writeBitmap failed to open path %s", path);
+      stream.close();
+      return false;
+   }
+
+   // free file for stb
+   stream.close();
+
    const GBitmap::Registration   *regInfo = GBitmap::sFindRegInfo( bmType );
    const GBitmap::Registration   *regInfo = GBitmap::sFindRegInfo( bmType );
 
 
    if ( regInfo == NULL )
    if ( regInfo == NULL )
@@ -1431,21 +1442,14 @@ DefineEngineFunction(saveScaledImage, bool, (const char* bitmapSource, const cha
    Torque::Path destinationPath = Torque::Path(bitmapDest);
    Torque::Path destinationPath = Torque::Path(bitmapDest);
    destinationPath.setExtension("png");
    destinationPath.setExtension("png");
 
 
-   // Open up the file on disk.
-   FileStream fs;
-   if (!fs.open(destinationPath.getFullPath(), Torque::FS::File::Write))
+   if(!image->writeBitmap("png", destinationPath.getFullPath()))
    {
    {
-      Con::errorf("saveScaledImage() - Failed to open output file '%s'!", bitmapDest);
+      Con::errorf("saveScaledImage() - Error writing %s !", bitmapDest);
       delete image;
       delete image;
       return false;
       return false;
    }
    }
-   else
-   {
-      image->writeBitmap("png", destinationPath.getFullPath());
-
-      fs.close();
-      delete image;
-   }
 
 
+   
+   delete image;
    return true;
    return true;
 }
 }

+ 3 - 7
Engine/source/gfx/bitmap/loaders/bitmapSTB.cpp

@@ -31,17 +31,13 @@
 #define STBIWDEF static inline
 #define STBIWDEF static inline
 #endif
 #endif
 
 
-#ifndef STB_IMAGE_IMPLEMENTATION
 #define STB_IMAGE_IMPLEMENTATION
 #define STB_IMAGE_IMPLEMENTATION
 #define STB_IMAGE_STATIC
 #define STB_IMAGE_STATIC
 #include "stb_image.h"
 #include "stb_image.h"
-#endif
 
 
-#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
 #define STB_IMAGE_WRITE_IMPLEMENTATION
 #define STB_IMAGE_WRITE_IMPLEMENTATION
 #define STB_IMAGE_WRITE_STATIC
 #define STB_IMAGE_WRITE_STATIC
 #include "stb_image_write.h"
 #include "stb_image_write.h"
-#endif
 
 
 static bool sReadSTB(const Torque::Path& path, GBitmap* bitmap);
 static bool sReadSTB(const Torque::Path& path, GBitmap* bitmap);
 static bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel);
 static bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel);
@@ -89,9 +85,9 @@ bool sReadSTB(const Torque::Path& path, GBitmap* bitmap)
       return false;
       return false;
    }
    }
 
 
-   // do this to map one channel to 3 and 2 channels to 4
-   if (channels == 2)
-      channels = 4;
+   //// do this to map one channel to 3 and 2 channels to 4
+   //if (channels == 2)
+   //   channels = 4;
 
 
    if (stbi_is_16_bit(path.getFullPath().c_str()))
    if (stbi_is_16_bit(path.getFullPath().c_str()))
    {
    {

+ 0 - 11
Engine/source/gfx/gFont.cpp

@@ -869,17 +869,6 @@ void GFont::exportStrip(const char *fileName, U32 padding, U32 kerning)
       // Advance.
       // Advance.
       curWidth +=  mCharInfoList[i].width + kerning + 2*padding;
       curWidth +=  mCharInfoList[i].width + kerning + 2*padding;
    }
    }
-
-   // Write the image!
-   FileStream fs;
-   
-   fs.open( fileName, Torque::FS::File::Write );
-
-   if(fs.getStatus() != Stream::Ok)
-   {
-      Con::errorf("GFont::exportStrip - failed to open '%s' for writing.", fileName);
-      return;
-   }
  
  
    // Done!
    // Done!
    gb.writeBitmap("png", fileName);
    gb.writeBitmap("png", fileName);

+ 0 - 4
Engine/source/gfx/gfxTextureObject.cpp

@@ -246,10 +246,6 @@ U32 GFXTextureObject::getEstimatedSizeInBytes() const
 
 
 bool GFXTextureObject::dumpToDisk( const String &bmType, const String &path )
 bool GFXTextureObject::dumpToDisk( const String &bmType, const String &path )
 {   
 {   
-   FileStream stream;
-   if ( !stream.open( path, Torque::FS::File::Write ) )
-      return false;
-
    if ( mBitmap )
    if ( mBitmap )
       return mBitmap->writeBitmap( bmType, path);
       return mBitmap->writeBitmap( bmType, path);
 
 

+ 4 - 13
Engine/source/gfx/screenshot.cpp

@@ -234,20 +234,11 @@ void ScreenShot::_singleCapture( GuiCanvas *canvas )
    char filename[256];
    char filename[256];
    dSprintf( filename, 256, "%s.%s", mFilename, mWriteJPG ? "jpg" : "png" );
    dSprintf( filename, 256, "%s.%s", mFilename, mWriteJPG ? "jpg" : "png" );
 
 
-   // Open up the file on disk.
-   FileStream fs;
-   if ( !fs.open( filename, Torque::FS::File::Write ) )
-      Con::errorf( "ScreenShot::_singleCapture() - Failed to open output file '%s'!", filename );
+   // Write it and close.
+   if ( mWriteJPG )
+      bitmap->writeBitmap( "jpg", filename);
    else
    else
-   {
-      // Write it and close.
-      if ( mWriteJPG )
-         bitmap->writeBitmap( "jpg", filename);
-      else
-         bitmap->writeBitmap( "png", filename);
-
-      fs.close();
-   }
+      bitmap->writeBitmap( "png", filename);
 
 
    // Cleanup.
    // Cleanup.
    delete bitmap;
    delete bitmap;

+ 0 - 6
Engine/source/gfx/video/videoEncoderPNG.cpp

@@ -44,13 +44,7 @@ public:
    /// Pushes a new frame into the video stream
    /// Pushes a new frame into the video stream
    bool pushFrame( GBitmap * bitmap )
    bool pushFrame( GBitmap * bitmap )
    {
    {
-      FileStream fs;
       String framePath = mPath + String::ToString("%.6u.png", mCurrentFrame);
       String framePath = mPath + String::ToString("%.6u.png", mCurrentFrame);
-      if ( !fs.open( framePath, Torque::FS::File::Write ) )
-      {
-         Con::errorf( "VideoEncoderPNG::pushFrame() - Failed to open output file '%s'!", framePath.c_str() );
-         return false;
-      }
 
 
       //Increment
       //Increment
       mCurrentFrame++;
       mCurrentFrame++;

+ 0 - 14
Engine/source/terrain/terrExport.cpp

@@ -67,13 +67,6 @@ bool TerrainBlock::exportHeightMap( const UTF8 *filePath, const String &format )
       }
       }
    }
    }
 
 
-   FileStream stream;
-   if ( !stream.open( filePath, Torque::FS::File::Write ) )
-   {
-      Con::errorf( "TerrainBlock::exportHeightMap() - Error opening file for writing: %s !", filePath );
-      return false;
-   }
-
    if ( !output.writeBitmap( format, filePath) )
    if ( !output.writeBitmap( format, filePath) )
    {
    {
       Con::errorf( "TerrainBlock::exportHeightMap() - Error writing %s: %s !", format.c_str(), filePath );
       Con::errorf( "TerrainBlock::exportHeightMap() - Error writing %s: %s !", format.c_str(), filePath );
@@ -120,13 +113,6 @@ bool TerrainBlock::exportLayerMaps( const UTF8 *filePrefix, const String &format
       UTF8 filePath[1024];
       UTF8 filePath[1024];
       dSprintf( filePath, 1024, "%s_%d_%s.%s", filePrefix, i, mFile->mMaterials[i]->getInternalName(), format.c_str() );
       dSprintf( filePath, 1024, "%s_%d_%s.%s", filePrefix, i, mFile->mMaterials[i]->getInternalName(), format.c_str() );
 
 
-      FileStream stream;
-      if ( !stream.open( filePath, Torque::FS::File::Write ) )
-      {
-         Con::errorf( "TerrainBlock::exportLayerMaps() - Error opening file for writing: %s !", filePath );
-         return false;
-      }
-
       if ( !output.writeBitmap( format, filePath) )
       if ( !output.writeBitmap( format, filePath) )
       {
       {
          Con::errorf( "TerrainBlock::exportLayerMaps() - Error writing %s: %s !", format.c_str(), filePath );
          Con::errorf( "TerrainBlock::exportLayerMaps() - Error writing %s: %s !", format.c_str(), filePath );

+ 0 - 7
Engine/source/terrain/terrRender.cpp

@@ -477,13 +477,6 @@ void TerrainBlock::_updateBaseTexture(bool writeToCache)
    }
    }
    else
    else
    {
    {
-      FileStream stream;
-      if (!stream.open(_getBaseTexCacheFileName(), Torque::FS::File::Write))
-      {
-         mBaseTex = blendTex;
-         return;
-      }
-
       GBitmap bitmap(blendTex->getWidth(), blendTex->getHeight(), false, GFXFormatR8G8B8A8);
       GBitmap bitmap(blendTex->getWidth(), blendTex->getHeight(), false, GFXFormatR8G8B8A8);
       blendTex->copyToBmp(&bitmap);
       blendTex->copyToBmp(&bitmap);
       bitmap.writeBitmap(formatToExtension(mBaseTexFormat), _getBaseTexCacheFileName());
       bitmap.writeBitmap(formatToExtension(mBaseTexFormat), _getBaseTexCacheFileName());

+ 4 - 7
Engine/source/ts/tsLastDetail.cpp

@@ -505,13 +505,10 @@ void TSLastDetail::_update()
       String normalsPath = _getNormalMapPath();
       String normalsPath = _getNormalMapPath();
 
 
       FileStream stream;
       FileStream stream;
-      if ( stream.open( imposterPath, Torque::FS::File::Write  ) )
-         destBmp.writeBitmap( "png", imposterPath);
-      stream.close();
-
-      if ( stream.open( normalsPath, Torque::FS::File::Write ) )
-         destNormal.writeBitmap( "png", normalsPath);
-      stream.close();
+      if (!destBmp.writeBitmap("png", imposterPath))
+         Con::errorf("TSLastDetail::_update() - failed to write imposter %s", imposterPath);
+      if (!destNormal.writeBitmap("png", normalsPath))
+         Con::errorf("TSLastDetail::_update() - failed to write normal %s", normalsPath);
    }
    }
 
 
    // DEBUG: Some code to force usage of a test image.
    // DEBUG: Some code to force usage of a test image.

+ 6 - 26
Engine/source/util/imposterCapture.cpp

@@ -316,16 +316,9 @@ void ImposterCapture::_separateAlpha( GBitmap *imposterOut )
       
       
       if ( 0 )
       if ( 0 )
       {
       {
-         FileStream fs;
-         if ( fs.open( "./imposterout.png", Torque::FS::File::Write ) )
-            imposterOut->writeBitmap( "png", "./imposterout.png" );
+         imposterOut->writeBitmap("png", "./imposterout.png");
 
 
-         fs.close();
-
-         if ( fs.open( "./temp.png", Torque::FS::File::Write ) )
-            bmp->writeBitmap( "png", "./temp.png" );
-
-         fs.close();
+         bmp->writeBitmap("png", "./temp.png");
       }
       }
    
    
 
 
@@ -482,26 +475,13 @@ void ImposterCapture::capture(   const MatrixF &rotMatrix,
    if ( 0 )
    if ( 0 )
    {
    {
       // Render out the bitmaps for debug purposes.
       // Render out the bitmaps for debug purposes.
-      FileStream fs;
-      if ( fs.open( "./blackbmp.png", Torque::FS::File::Write ) )
-         mBlackBmp->writeBitmap( "png", "./blackbmp.png" );
-
-      fs.close();
-
-      if ( fs.open( "./whitebmp.png", Torque::FS::File::Write ) )
-         mWhiteBmp->writeBitmap( "png", "./whitebmp.png" );
-
-      fs.close();
-
-      if ( fs.open( "./normalbmp.png", Torque::FS::File::Write ) )
-         (*normalMapOut)->writeBitmap( "png", "./normalbmp.png" );
+      mBlackBmp->writeBitmap( "png", "./blackbmp.png" );
 
 
-      fs.close();
+      mWhiteBmp->writeBitmap( "png", "./whitebmp.png" );
 
 
-      if ( fs.open( "./finalimposter.png", Torque::FS::File::Write ) )
-         (*imposterOut)->writeBitmap( "png", "./finalimposter.png" );
+      (*normalMapOut)->writeBitmap( "png", "./normalbmp.png" );
 
 
-      fs.close();
+      (*imposterOut)->writeBitmap( "png", "./finalimposter.png" );
    }
    }
 }
 }