diff --git a/src/main/groovy/qupath/ext/basicstitching/functions/StitchingGUI.groovy b/src/main/groovy/qupath/ext/basicstitching/functions/StitchingGUI.groovy index 13bfe98..7204770 100644 --- a/src/main/groovy/qupath/ext/basicstitching/functions/StitchingGUI.groovy +++ b/src/main/groovy/qupath/ext/basicstitching/functions/StitchingGUI.groovy @@ -72,7 +72,7 @@ class StitchingGUI { String matchingString = matchStringField.getText() // Assuming matchStringField is accessible String stitchingType = stitchingGridBox.getValue() // Call the function with collected data - stitchingImplementations.stitchCore(stitchingType, folderPath, folderPath, compressionType, pixelSize, downsample, matchingString) + String finalImageName = stitchingImplementations.stitchCore(stitchingType, folderPath, folderPath, compressionType, pixelSize, downsample, matchingString) //stitchByFileName(folderPath, compressionType, pixelSize, downsample, matchingString) } diff --git a/src/main/groovy/qupath/ext/basicstitching/stitching/stitchingImplementations.groovy b/src/main/groovy/qupath/ext/basicstitching/stitching/stitchingImplementations.groovy index 8eed676..f09be33 100644 --- a/src/main/groovy/qupath/ext/basicstitching/stitching/stitchingImplementations.groovy +++ b/src/main/groovy/qupath/ext/basicstitching/stitching/stitchingImplementations.groovy @@ -554,7 +554,7 @@ class stitchingImplementations { * @param baseDownsample The base downsample value for the stitching process. * @param matchingString A string to match for selecting relevant subdirectories or files. */ - static void stitchCore(String stitchingType, String folderPath, String outputPath, String compressionType, double pixelSizeInMicrons, double baseDownsample, String matchingString) { + static String stitchCore(String stitchingType, String folderPath, String outputPath, String compressionType, double pixelSizeInMicrons, double baseDownsample, String matchingString) { def logger = LoggerFactory.getLogger(QuPathGUI.class) // Determine the stitching strategy based on the provided type switch(stitchingType) { @@ -636,8 +636,9 @@ class stitchingImplementations { .writePyramid(pathOutput) long endTime = System.currentTimeMillis() - println("Image written to ${pathOutput} in ${GeneralTools.formatNumber((endTime - startTime)/1000.0, 1)} s") + logger.info("Image written to ${pathOutput} in ${GeneralTools.formatNumber((endTime - startTime)/1000.0, 1)} s") server.close() + return pathOutput } else { println("No valid stitching strategy set.") } diff --git a/src/main/groovy/qupath/ext/basicstitching/utilities/UtilityFunctions.groovy b/src/main/groovy/qupath/ext/basicstitching/utilities/UtilityFunctions.groovy index 1313c50..497b0e3 100644 --- a/src/main/groovy/qupath/ext/basicstitching/utilities/UtilityFunctions.groovy +++ b/src/main/groovy/qupath/ext/basicstitching/utilities/UtilityFunctions.groovy @@ -8,6 +8,7 @@ import java.lang.reflect.Modifier import java.nio.file.Path import java.nio.file.Paths import java.nio.file.Files +import java.util.stream.Stream /** * Class containing utility functions used throughout the application. @@ -47,25 +48,31 @@ class UtilityFunctions { } } /** - * Generates a unique file path by appending a number if the file already exists. + * Generates a unique file path by appending a number to the file name if a file with the + * same name already exists. The first instance of the file will have no number appended, + * while the second will have _2, the third _3, and so on. * * @param originalPath The original file path. * @return A unique file path. */ static String getUniqueFilePath(String originalPath) { - Path path = Paths.get(originalPath) - String baseName = path.getFileName().toString().replaceAll(/\.ome\.tif$/, "") - Path parentDir = path.getParent() + Path path = Paths.get(originalPath); + String baseName = path.getFileName().toString().replaceAll('\\.ome\\.tif$', ""); + Path parentDir = path.getParent(); - int counter = 1 - while (Files.exists(path)) { - String newFileName = "${baseName}_${counter}.ome.tif" - path = parentDir.resolve(newFileName) - counter++ + // Start with the original base name + Path newPath = parentDir.resolve(baseName + ".ome.tif"); + int counter = 2; + + // If the file exists, start appending numbers + while (Files.exists(newPath)) { + newPath = parentDir.resolve(baseName + "_" + counter + ".ome.tif"); + counter++; } - return path.toString() + return newPath.toString(); } + /** * Retrieves the dimensions (width and height) of a TIFF image file. *