diff --git a/kstars/indi/indiccd.h b/kstars/indi/indiccd.h --- a/kstars/indi/indiccd.h +++ b/kstars/indi/indiccd.h @@ -354,8 +354,7 @@ void loadImageInView(IBLOB *bp, ISD::CCDChip *targetChip, FITSData *data); bool generateFilename(const QString &format, bool batch_mode, QString *filename); // Saves an image to disk on a separate thread. - bool writeImageFile(IBLOB *bp, const QString &format, bool is_fits, - bool batch_mode, QString *filename); + bool writeImageFile(const QString &filename, IBLOB *bp, bool is_fits); // Creates or finds the FITSViewer. void setupFITSViewerWindows(); void displayFits(CCDChip *targetChip, const QString &filename, IBLOB *bp, FITSData *blob_fits_data); @@ -407,6 +406,7 @@ // Used when writing the image fits file to disk in a separate thread. char *fileWriteBuffer { nullptr }; int fileWriteBufferSize { 0 }; + QString fileWriteFilename; QFuture fileWriteThread; }; } diff --git a/kstars/indi/indiccd.cpp b/kstars/indi/indiccd.cpp --- a/kstars/indi/indiccd.cpp +++ b/kstars/indi/indiccd.cpp @@ -1443,24 +1443,22 @@ return true; } -bool CCD::writeImageFile(IBLOB *bp, const QString &format, bool is_fits, - bool batch_mode, QString *filename) +bool CCD::writeImageFile(const QString &filename, IBLOB *bp, bool is_fits) { - if (!generateFilename(format, batch_mode, filename)) - return false; - // TODO: Not yet threading the writes for non-fits files. // Would need to deal with the raw conversion, etc. if (is_fits) { - // Check if the last write is still ongoing, and if so wait. // It is using the fileWriteBuffer. if (fileWriteThread.isRunning()) { fileWriteThread.waitForFinished(); } + // Wait until the file is written before overwritting the filename. + fileWriteFilename = filename; + // Will write blob data in a separate thread, and can't depend on the blob // memory, so copy it first. @@ -1476,13 +1474,13 @@ // Copy memory, and write file on a separate thread. // Probably too late to return an error if the file couldn't write. memcpy(fileWriteBuffer, bp->blob, bp->size); - fileWriteThread = QtConcurrent::run(WriteImageFileInternal, *filename, + fileWriteThread = QtConcurrent::run(WriteImageFileInternal, fileWriteFilename, fileWriteBuffer, bp->size, is_fits, filter); filter = ""; } else { - if (!WriteImageFileInternal(*filename, static_cast(bp->blob), bp->size, + if (!WriteImageFileInternal(filename, static_cast(bp->blob), bp->size, false, filter)) return false; } @@ -1578,7 +1576,8 @@ // Create file name for others else { - if (!writeImageFile(bp, format, BType == BLOB_FITS, targetChip->isBatchMode(), &filename)) + if (!generateFilename(format, targetChip->isBatchMode(), &filename) || + !writeImageFile(filename, bp, BType == BLOB_FITS)) { emit BLOBUpdated(nullptr); return;