|
| 1 | +package net.preibisch.dbio_headless; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.ExecutorService; |
| 7 | + |
| 8 | +import mpicbg.spim.data.SpimDataException; |
| 9 | +import net.imglib2.RandomAccessibleInterval; |
| 10 | +import net.imglib2.type.numeric.real.FloatType; |
| 11 | +import net.imglib2.util.Util; |
| 12 | +import net.preibisch.distribution.algorithm.blockmanagement.blockinfo.BasicBlockInfo; |
| 13 | +import net.preibisch.distribution.algorithm.blockmanagement.blockinfo.BasicBlockInfoGenerator; |
| 14 | +import net.preibisch.distribution.algorithm.blockmanagement.io.BlockFileManager; |
| 15 | +import net.preibisch.distribution.algorithm.controllers.items.Job; |
| 16 | +import net.preibisch.distribution.algorithm.controllers.items.Metadata; |
| 17 | +import net.preibisch.distribution.algorithm.errorhandler.logmanager.MyLogger; |
| 18 | +import net.preibisch.distribution.algorithm.multithreading.Threads; |
| 19 | +import net.preibisch.distribution.io.DataExtension; |
| 20 | +import net.preibisch.distribution.io.img.load.LoadTIFF; |
| 21 | +import net.preibisch.distribution.io.img.n5.N5File; |
| 22 | +import net.preibisch.distribution.io.img.xml.XMLFile; |
| 23 | +import net.preibisch.distribution.tools.helpers.ArrayHelpers; |
| 24 | +import net.preibisch.mvrecon.fiji.spimdata.boundingbox.BoundingBox; |
| 25 | + |
| 26 | +public class TotalPipeline { |
| 27 | + static String inputFilePath = "/Users/Marwan/Desktop/Task/example_dataset/3d/dataset.xml"; |
| 28 | + static String outputN5Path = "/Users/Marwan/Desktop/Task/example_dataset/3d/output.n5"; |
| 29 | + static String metadataPath = "/Users/Marwan/Desktop/Task/example_dataset/3d/metadata.json"; |
| 30 | + static String tiffFolderPath = "/Users/Marwan/Desktop/Task/example_dataset/3d/output/"; |
| 31 | + |
| 32 | + public static void main(String[] args) throws SpimDataException, IOException { |
| 33 | + generateMetadata(inputFilePath, outputN5Path, metadataPath); |
| 34 | + splittoTiffs(inputFilePath, tiffFolderPath, metadataPath); |
| 35 | + mergeToN5(metadataPath, tiffFolderPath, outputN5Path); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public static void generateMetadata(String inputPath, String outputPath, String metadataPath) |
| 40 | + throws SpimDataException, IOException { |
| 41 | + XMLFile<FloatType> inputData = XMLFile.XMLFile(inputPath); |
| 42 | + long[] blocksizes = ArrayHelpers.fill(BasicBlockInfoGenerator.BLOCK_SIZE, inputData.getDims().length); |
| 43 | + Map<Integer, BasicBlockInfo> blocks = BasicBlockInfoGenerator.divideIntoBlockInfo(inputData.bb()); |
| 44 | + MyLogger.log().info("Total blocks: " + blocks.size()); |
| 45 | + |
| 46 | + Metadata md = new Metadata(Job.get().getId(), inputPath, outputPath, new BoundingBox(inputData.bb()), |
| 47 | + blocksizes, blocks); |
| 48 | + MyLogger.log().info(md.toString()); |
| 49 | + md.toJson(metadataPath); |
| 50 | + } |
| 51 | + |
| 52 | + public static void splittoTiffs(String inputPath, String outputTiffPath, String metadataPath) |
| 53 | + throws SpimDataException, IOException { |
| 54 | + XMLFile<FloatType> inputData = XMLFile.XMLFile(inputPath); |
| 55 | + Metadata md = Metadata.fromJson(metadataPath); |
| 56 | + MyLogger.log().info(md.toString()); |
| 57 | + MyLogger.log().info("Start process.. "); |
| 58 | + MyLogger.log().info("Block Generated.. "); |
| 59 | + ExecutorService service = Threads.createExService(Threads.numThreads()); |
| 60 | + File file = new File(outputTiffPath); |
| 61 | + file.mkdir(); |
| 62 | + BlockFileManager.saveAllBlocks(service, inputData.getImg(), md.getBlocksInfo(), outputTiffPath, DataExtension.TIF); |
| 63 | + MyLogger.log().info("Done !"); |
| 64 | + } |
| 65 | + |
| 66 | + public static void mergeToN5(String metadataPath, String tiffFolderPath, String outputn5Path) throws IOException { |
| 67 | + |
| 68 | + Metadata md = Metadata.fromJson(metadataPath); |
| 69 | + long[] dims = md.getBb().getDimensions(1); |
| 70 | + |
| 71 | + N5File n5 = createOuptut(dims, outputn5Path); |
| 72 | + |
| 73 | + Map<Integer, BasicBlockInfo> blocks = md.getBlocksInfo(); |
| 74 | + File folder = new File(tiffFolderPath); |
| 75 | + for (File f : folder.listFiles()) { |
| 76 | + if (DataExtension.fromURI(f.getName()) == DataExtension.TIF) { |
| 77 | + Integer x = Integer.valueOf(DataExtension.removeExtension(f.getName())) ; |
| 78 | + // File file = new File(outputPath, DataExtension.TIF.file(String.valueOf(x))); |
| 79 | + MyLogger.log().info("Image : " + f.getAbsolutePath()); |
| 80 | + |
| 81 | + RandomAccessibleInterval<FloatType> blockImage = LoadTIFF.load(f); |
| 82 | + long[] gridOffset = blocks.get(x).getGridOffset(); |
| 83 | + n5.saveBlock(blockImage, gridOffset); |
| 84 | + |
| 85 | + MyLogger.log().info("Block " + x + " saved in: " + Util.printCoordinates(gridOffset)); |
| 86 | + } |
| 87 | + } |
| 88 | + n5.show("Result"); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + private static N5File createOuptut(long[] dims, String outputPath) throws IOException { |
| 93 | + N5File outputFile = new N5File(outputPath, dims); |
| 94 | + outputFile.create(); |
| 95 | + MyLogger.log().info("N5 Generated"); |
| 96 | + return outputFile; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments