Search in sources :

Example 1 with NamedThreadFactory

use of maspack.concurrency.NamedThreadFactory in project artisynth_core by artisynth.

the class DicomImageDecoderImageMagick method checkForImageMagick.

/**
 * Checks for availability of ImageMagick on the system path
 * @return true if available
 */
public static boolean checkForImageMagick() {
    String[] cmdArray = new String[2];
    String cmd = DEFAULT_CONVERT_COMMAND;
    cmdArray[0] = cmd;
    cmdArray[1] = "--version";
    boolean found = false;
    ExecutorService executor = Executors.newCachedThreadPool(new NamedThreadFactory("ImageMagick"));
    // looking for ImageMagick in the output
    final String imStr = "ImageMagick";
    found = didContainOutput(executor, cmdArray, imStr);
    if (found) {
        executor.shutdown();
        return true;
    }
    // check paths in environment variable
    String path = System.getenv("PATH");
    String[] pathsExpanded = path.split(File.pathSeparator);
    for (int i = 0; i < pathsExpanded.length; i++) {
        cmdArray[0] = new File(pathsExpanded[i], cmd).getAbsolutePath();
        found = didContainOutput(executor, cmdArray, imStr);
        if (found) {
            executor.shutdown();
            return true;
        }
    }
    executor.shutdown();
    return false;
}
Also used : NamedThreadFactory(maspack.concurrency.NamedThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File)

Example 2 with NamedThreadFactory

use of maspack.concurrency.NamedThreadFactory in project artisynth_core by artisynth.

the class DicomReader method read.

/**
 * Populates a DicomImage based on a given list of DICOM files.
 *
 * @param im
 * image to populate (null to generate new image)
 * @param files
 * list of DICOM files
 * @param temporalPosition
 * temporal index
 * @return the populated DICOM image
 * @throws IOException
 * if there is a read failure
 */
public DicomImage read(DicomImage im, List<File> files, int temporalPosition) throws IOException {
    if (files.size() == 0) {
        return null;
    }
    String imageName = files.get(0).getParentFile().getName();
    int cpus = Runtime.getRuntime().availableProcessors();
    ExecutorService executor = Executors.newFixedThreadPool(cpus, new NamedThreadFactory("dicom_reader"));
    LinkedList<Future<DicomSlice[]>> sliceReaders = new LinkedList<Future<DicomSlice[]>>();
    ExecutorCompletionService<DicomSlice[]> ecs = new ExecutorCompletionService<DicomSlice[]>(executor);
    int nReaders = 0;
    for (int i = 0; i < files.size(); i++) {
        SliceReaderCallable reader = new SliceReaderCallable(files.get(i));
        sliceReaders.add(ecs.submit(reader));
        nReaders++;
    }
    executor.shutdown();
    FunctionTimer timer = new FunctionTimer();
    timer.start();
    int nTimes = 0;
    if (im != null) {
        nTimes = im.getNumTimes();
    }
    // process futures as they come in
    int nProcessed = 0;
    while (nProcessed < nReaders) {
        Future<DicomSlice[]> fut = null;
        try {
            fut = ecs.take();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
            return null;
        }
        if (fut.isDone()) {
            nProcessed++;
            // process
            try {
                DicomSlice[] slices = fut.get();
                if (slices != null) {
                    // split up into frames
                    for (int i = 0; i < slices.length; i++) {
                        int stime = temporalPosition;
                        if (stime < 0) {
                            // attempt to read time from slice
                            int[] vals = slices[i].getHeader().getMultiIntValue(DicomTag.TEMPORAL_POSITON_IDENTIFIER);
                            if (vals != null) {
                                stime = vals[0];
                            } else {
                                // set unknown time?
                                stime = nTimes;
                            }
                        }
                        slices[i].info.temporalPosition = stime;
                        if (im == null) {
                            im = new DicomImage(imageName, slices[i]);
                        } else {
                            im.addSlice(slices[i]);
                        }
                    }
                }
            } catch (ExecutionException e) {
                if (e.getCause() instanceof IOException) {
                    throw (IOException) (e.getCause());
                } else {
                    e.printStackTrace();
                }
            }// end try-catching errors
             catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    // end checking if future complete
    }
    // end main loop
    timer.stop();
    double usec = timer.getTimeUsec();
    System.out.println("Read took " + usec * 1e-6 + " seconds");
    if (im == null) {
        return null;
    }
    if (im.title == null) {
        im.title = imageName;
    }
    return im;
}
Also used : NamedThreadFactory(maspack.concurrency.NamedThreadFactory) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) FunctionTimer(maspack.util.FunctionTimer) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)2 NamedThreadFactory (maspack.concurrency.NamedThreadFactory)2 File (java.io.File)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 Future (java.util.concurrent.Future)1 FunctionTimer (maspack.util.FunctionTimer)1