From 63682c43ecac3dfaf22eaa89ca03e4b1dce02a1b Mon Sep 17 00:00:00 2001 From: marauder2k7 Date: Thu, 30 Nov 2023 10:46:51 +0000 Subject: [PATCH] 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. --- Engine/source/gfx/bitmap/ddsFile.cpp | 12 ++++--- Engine/source/gfx/bitmap/gBitmap.cpp | 26 ++++++++------- .../source/gfx/bitmap/loaders/bitmapSTB.cpp | 10 ++---- Engine/source/gfx/gFont.cpp | 11 ------- Engine/source/gfx/gfxTextureObject.cpp | 4 --- Engine/source/gfx/screenshot.cpp | 17 +++------- Engine/source/gfx/video/videoEncoderPNG.cpp | 6 ---- Engine/source/terrain/terrExport.cpp | 14 -------- Engine/source/terrain/terrRender.cpp | 7 ---- Engine/source/ts/tsLastDetail.cpp | 11 +++---- Engine/source/util/imposterCapture.cpp | 32 ++++--------------- 11 files changed, 39 insertions(+), 111 deletions(-) diff --git a/Engine/source/gfx/bitmap/ddsFile.cpp b/Engine/source/gfx/bitmap/ddsFile.cpp index ad377c9fd..6278efd61 100644 --- a/Engine/source/gfx/bitmap/ddsFile.cpp +++ b/Engine/source/gfx/bitmap/ddsFile.cpp @@ -568,12 +568,14 @@ void DDSFile::SurfaceData::dumpImage(DDSFile *dds, U32 mip, const char *file) FileStream stream; - stream.open( file, Torque::FS::File::Write ); - - if ( stream.getStatus() == Stream::Ok ) + if (!stream.open(file, Torque::FS::File::Write)) { - // Write it out. - foo->writeBitmap("png", file); + Con::errorf("DDSFile::SurfaceData::dumpImage() - Error opening file for writing: %s !", file); + } + + if(!foo->writeBitmap("png", file)) + { + Con::errorf("DDSFile::SurfaceData::dumpImage() - Error writing %s !", file); } // Clean up. diff --git a/Engine/source/gfx/bitmap/gBitmap.cpp b/Engine/source/gfx/bitmap/gBitmap.cpp index dbbc47c1e..c18859b67 100644 --- a/Engine/source/gfx/bitmap/gBitmap.cpp +++ b/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 ) { + 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 ); if ( regInfo == NULL ) @@ -1431,21 +1442,14 @@ DefineEngineFunction(saveScaledImage, bool, (const char* bitmapSource, const cha Torque::Path destinationPath = Torque::Path(bitmapDest); 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; return false; } - else - { - image->writeBitmap("png", destinationPath.getFullPath()); - - fs.close(); - delete image; - } + + delete image; return true; } diff --git a/Engine/source/gfx/bitmap/loaders/bitmapSTB.cpp b/Engine/source/gfx/bitmap/loaders/bitmapSTB.cpp index fed229cb4..de45383af 100644 --- a/Engine/source/gfx/bitmap/loaders/bitmapSTB.cpp +++ b/Engine/source/gfx/bitmap/loaders/bitmapSTB.cpp @@ -31,17 +31,13 @@ #define STBIWDEF static inline #endif -#ifndef STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_STATIC #include "stb_image.h" -#endif -#ifndef STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_STATIC #include "stb_image_write.h" -#endif static bool sReadSTB(const Torque::Path& path, GBitmap* bitmap); 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; } - // 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())) { diff --git a/Engine/source/gfx/gFont.cpp b/Engine/source/gfx/gFont.cpp index 70b5a6d9d..af291c9b4 100644 --- a/Engine/source/gfx/gFont.cpp +++ b/Engine/source/gfx/gFont.cpp @@ -869,17 +869,6 @@ void GFont::exportStrip(const char *fileName, U32 padding, U32 kerning) // Advance. 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! gb.writeBitmap("png", fileName); diff --git a/Engine/source/gfx/gfxTextureObject.cpp b/Engine/source/gfx/gfxTextureObject.cpp index 2eb9ece81..df7367897 100644 --- a/Engine/source/gfx/gfxTextureObject.cpp +++ b/Engine/source/gfx/gfxTextureObject.cpp @@ -246,10 +246,6 @@ U32 GFXTextureObject::getEstimatedSizeInBytes() const bool GFXTextureObject::dumpToDisk( const String &bmType, const String &path ) { - FileStream stream; - if ( !stream.open( path, Torque::FS::File::Write ) ) - return false; - if ( mBitmap ) return mBitmap->writeBitmap( bmType, path); diff --git a/Engine/source/gfx/screenshot.cpp b/Engine/source/gfx/screenshot.cpp index 76acd135b..8d629db22 100644 --- a/Engine/source/gfx/screenshot.cpp +++ b/Engine/source/gfx/screenshot.cpp @@ -234,20 +234,11 @@ void ScreenShot::_singleCapture( GuiCanvas *canvas ) char filename[256]; 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 - { - // Write it and close. - if ( mWriteJPG ) - bitmap->writeBitmap( "jpg", filename); - else - bitmap->writeBitmap( "png", filename); - - fs.close(); - } + bitmap->writeBitmap( "png", filename); // Cleanup. delete bitmap; diff --git a/Engine/source/gfx/video/videoEncoderPNG.cpp b/Engine/source/gfx/video/videoEncoderPNG.cpp index df62c8a4f..b6375a8ca 100644 --- a/Engine/source/gfx/video/videoEncoderPNG.cpp +++ b/Engine/source/gfx/video/videoEncoderPNG.cpp @@ -44,13 +44,7 @@ public: /// Pushes a new frame into the video stream bool pushFrame( GBitmap * bitmap ) { - FileStream fs; 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 mCurrentFrame++; diff --git a/Engine/source/terrain/terrExport.cpp b/Engine/source/terrain/terrExport.cpp index 4c1df5ca6..1eafbae0e 100644 --- a/Engine/source/terrain/terrExport.cpp +++ b/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) ) { 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]; 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) ) { Con::errorf( "TerrainBlock::exportLayerMaps() - Error writing %s: %s !", format.c_str(), filePath ); diff --git a/Engine/source/terrain/terrRender.cpp b/Engine/source/terrain/terrRender.cpp index f06de349b..45d61fbff 100644 --- a/Engine/source/terrain/terrRender.cpp +++ b/Engine/source/terrain/terrRender.cpp @@ -477,13 +477,6 @@ void TerrainBlock::_updateBaseTexture(bool writeToCache) } else { - FileStream stream; - if (!stream.open(_getBaseTexCacheFileName(), Torque::FS::File::Write)) - { - mBaseTex = blendTex; - return; - } - GBitmap bitmap(blendTex->getWidth(), blendTex->getHeight(), false, GFXFormatR8G8B8A8); blendTex->copyToBmp(&bitmap); bitmap.writeBitmap(formatToExtension(mBaseTexFormat), _getBaseTexCacheFileName()); diff --git a/Engine/source/ts/tsLastDetail.cpp b/Engine/source/ts/tsLastDetail.cpp index 6d28d674b..a9ada6445 100644 --- a/Engine/source/ts/tsLastDetail.cpp +++ b/Engine/source/ts/tsLastDetail.cpp @@ -505,13 +505,10 @@ void TSLastDetail::_update() String normalsPath = _getNormalMapPath(); 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. diff --git a/Engine/source/util/imposterCapture.cpp b/Engine/source/util/imposterCapture.cpp index b28ae9758..426f3b0bb 100644 --- a/Engine/source/util/imposterCapture.cpp +++ b/Engine/source/util/imposterCapture.cpp @@ -316,16 +316,9 @@ void ImposterCapture::_separateAlpha( GBitmap *imposterOut ) 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 ) { // Render out the bitmaps for debug purposes. - FileStream fs; - if ( fs.open( "./blackbmp.png", Torque::FS::File::Write ) ) - mBlackBmp->writeBitmap( "png", "./blackbmp.png" ); + mBlackBmp->writeBitmap( "png", "./blackbmp.png" ); - fs.close(); + mWhiteBmp->writeBitmap( "png", "./whitebmp.png" ); - if ( fs.open( "./whitebmp.png", Torque::FS::File::Write ) ) - mWhiteBmp->writeBitmap( "png", "./whitebmp.png" ); + (*normalMapOut)->writeBitmap( "png", "./normalbmp.png" ); - fs.close(); - - if ( fs.open( "./normalbmp.png", Torque::FS::File::Write ) ) - (*normalMapOut)->writeBitmap( "png", "./normalbmp.png" ); - - fs.close(); - - if ( fs.open( "./finalimposter.png", Torque::FS::File::Write ) ) - (*imposterOut)->writeBitmap( "png", "./finalimposter.png" ); - - fs.close(); + (*imposterOut)->writeBitmap( "png", "./finalimposter.png" ); } }