From 15c8f2298d7ae91158c4f2a83cadef87b58c7448 Mon Sep 17 00:00:00 2001 From: rh101 Date: Thu, 22 Apr 2021 21:36:26 +1000 Subject: [PATCH] Use FileStream to write JPG images to disk --- cocos/platform/CCImage.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/cocos/platform/CCImage.cpp b/cocos/platform/CCImage.cpp index faf0e36491..937e00f277 100644 --- a/cocos/platform/CCImage.cpp +++ b/cocos/platform/CCImage.cpp @@ -2319,7 +2319,7 @@ bool Image::saveImageToJPG(const std::string& filePath) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; - FILE * outfile; /* target file */ + FileStream * outfile; /* target file */ JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ int row_stride; /* physical row width in image buffer */ @@ -2327,9 +2327,12 @@ bool Image::saveImageToJPG(const std::string& filePath) /* Now we can initialize the JPEG compression object. */ jpeg_create_compress(&cinfo); - CC_BREAK_IF((outfile = fopen(filePath.c_str(), "wb")) == nullptr); - - jpeg_stdio_dest(&cinfo, outfile); + outfile = FileUtils::getInstance()->openFileStream(filePath, FileStream::Mode::WRITE); + CC_BREAK_IF((outfile == nullptr || !outfile->isOpen())); + + unsigned char* outputBuffer = nullptr; + unsigned long outputSize = 0; + jpeg_mem_dest(&cinfo, &outputBuffer, &outputSize); cinfo.image_width = _width; /* image width and height, in pixels */ cinfo.image_height = _height; @@ -2350,7 +2353,14 @@ bool Image::saveImageToJPG(const std::string& filePath) { jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); - fclose(outfile); + + delete outfile; + outfile = nullptr; + if (outputBuffer) + { + free(outputBuffer); + outputBuffer = nullptr; + } break; } @@ -2385,7 +2395,16 @@ bool Image::saveImageToJPG(const std::string& filePath) } jpeg_finish_compress(&cinfo); - fclose(outfile); + + outfile->write(outputBuffer, outputSize); + delete outfile; // closes FileStream automatically + outfile = nullptr; + if (outputBuffer) + { + free(outputBuffer); + outputBuffer = nullptr; + } + jpeg_destroy_compress(&cinfo); ret = true;