Search in sources :

Example 91 with AsynchClientTask

use of cbit.vcell.client.task.AsynchClientTask in project vcell by virtualcell.

the class ROIMultiPaintManager method askInitialize.

private void askInitialize(boolean bForceAddDistinct) {
    final TreeSet<Integer> sortedPixVal = new TreeSet<Integer>();
    BitSet uniquePixelBS = new BitSet((int) Math.pow(2, Short.SIZE));
    for (int i = 0; i < getImageDataSetChannel().getAllImages().length; i++) {
        short[] dataToSegment = getImageDataSetChannel().getAllImages()[i].getPixels();
        for (int j = 0; j < dataToSegment.length; j++) {
            if ((int) (dataToSegment[j] & 0x0000FFFF) != 0) {
                if (!uniquePixelBS.get((int) (dataToSegment[j] & 0x0000FFFF))) {
                    sortedPixVal.add((int) (dataToSegment[j] & 0x0000FFFF));
                }
                uniquePixelBS.set((int) (dataToSegment[j] & 0x0000FFFF));
            }
        }
    }
    boolean bNotAddDomains = uniquePixelBS.cardinality() >= 256 || /*too many to add*/
    !bHasOriginalData || /*no import*/
    uniquePixelBS.cardinality() == 0;
    final String addROIManual = "1. Add empty Domain";
    final String addAllDistinct = "2. Assume Pre-Segmented";
    final String cancel = "Cancel";
    String result = null;
    String distinctDescr = "The current image contains " + uniquePixelBS.cardinality() + " distinct non-zero pixel values.";
    if (!bForceAddDistinct) {
        result = DialogUtils.showWarningDialog(overlayEditorPanelJAI, "Image Editor", distinctDescr + "  Segmenting an image begins with defining Domain(s) manually or automatically." + "  Editing tools are used to create/edit more Domains.  Choose an action:\n" + "1. Add an 'empty' Domain to begin segmenting manually." + (bNotAddDomains ? "" : "\n2. Pre-Segmented (add Domains for every distinct pixel value)."), (bNotAddDomains ? new String[] { addROIManual, cancel } : new String[] { addROIManual, addAllDistinct, cancel }), cancel);
        if (result.equals(cancel)) {
            // throw UserCancelException.CANCEL_GENERIC;
            return;
        }
    } else {
        if (uniquePixelBS.cardinality() == 0) {
            DialogUtils.showWarningDialog(overlayEditorPanelJAI, "Underlay contains no non-zero pixel values available for Domain assignment.");
            return;
        }
        boolean bHasExistingROIs = overlayEditorPanelJAI.getAllCompositeROINamesAndColors() != null && overlayEditorPanelJAI.getAllCompositeROINamesAndColors().length > 0;
        result = DialogUtils.showWarningDialog(overlayEditorPanelJAI, (bHasExistingROIs ? "Warning: Existing Domains may be overwritten.  " : "") + distinctDescr, new String[] { addAllDistinct, cancel }, addAllDistinct);
        if (result.equals(cancel)) {
            // throw UserCancelException.CANCEL_GENERIC;
            return;
        }
    }
    try {
        if (result.equals(addAllDistinct)) {
            // try add all distinct, fail if too many regions
            final String LOOKUP_KEY = "LOOKUP_KEY";
            AsynchClientTask createDistinctROI = new AsynchClientTask("Create distinct ROI...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

                @Override
                public void run(Hashtable<String, Object> hashTable) throws Exception {
                    Integer[] uniquePivValArr = sortedPixVal.toArray(new Integer[0]);
                    int[] lookup = new int[uniquePivValArr[uniquePivValArr.length - 1] + 1];
                    Arrays.fill(lookup, -1);
                    for (int i = 0; i < uniquePivValArr.length; i++) {
                        lookup[uniquePivValArr[i]] = i + 1;
                        overlayEditorPanelJAI.addROIName("roi_" + uniquePivValArr[i], false, "roi_" + uniquePivValArr[0], true, i + 1);
                    }
                    hashTable.put(LOOKUP_KEY, lookup);
                }
            };
            AsynchClientTask applyDistinctROI = new AsynchClientTask("Apply distinct ROI...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

                @Override
                public void run(Hashtable<String, Object> hashTable) throws Exception {
                    int[] lookup = (int[]) hashTable.get(LOOKUP_KEY);
                    for (int i = 0; i < getImageDataSetChannel().getAllImages().length; i++) {
                        short[] dataToSegment = getImageDataSetChannel().getAllImages()[i].getPixels();
                        byte[] roiBytes = ((DataBufferByte) roiComposite[i].getRaster().getDataBuffer()).getData();
                        for (int j = 0; j < dataToSegment.length; j++) {
                            if ((int) (dataToSegment[j] & 0x0000FFFF) != 0) {
                                roiBytes[j] = (byte) lookup[(int) (dataToSegment[j] & 0x0000FFFF)];
                            }
                        }
                    }
                }
            };
            AsynchClientTask failTask = new AsynchClientTask("Check fail...", AsynchClientTask.TASKTYPE_SWING_BLOCKING, false, false, true) {

                @Override
                public void run(Hashtable<String, Object> hashTable) throws Exception {
                    Throwable throwable = (Throwable) hashTable.get(ClientTaskDispatcher.TASK_ABORTED_BY_ERROR);
                    if (throwable != null) {
                        // cleanup
                        ComboboxROIName[] comboboxROINames = overlayEditorPanelJAI.getAllCompositeROINamesAndColors();
                        for (int i = 0; i < comboboxROINames.length; i++) {
                            overlayEditorPanelJAI.deleteROIName(comboboxROINames[i]);
                        }
                        for (int i = 0; i < roiComposite.length; i++) {
                            byte[] roiBytes = ((DataBufferByte) roiComposite[i].getRaster().getDataBuffer()).getData();
                            Arrays.fill(roiBytes, (byte) 0);
                        }
                    }
                }
            };
            AsynchClientTask clearHighlightsTask = new AsynchClientTask("Apply distinct ROI...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

                @Override
                public void run(Hashtable<String, Object> hashTable) throws Exception {
                    overlayEditorPanelJAI.setHighliteInfo(null, OverlayEditorPanelJAI.FRAP_DATA_INIT_PROPERTY);
                }
            };
            ClientTaskDispatcher.dispatch(overlayEditorPanelJAI, new Hashtable<String, Object>(), new AsynchClientTask[] { createDistinctROI, applyDistinctROI, createRegionImageTask, failTask, clearHighlightsTask, saveSortRegionImageTask, updateList }, false, false, null, true);
        } else {
            try {
                addNewROI(overlayEditorPanelJAI.getAllCompositeROINamesAndColors(), null);
            } catch (Exception e) {
                e.printStackTrace();
                DialogUtils.showErrorDialog(overlayEditorPanelJAI, e.getMessage());
            }
        }
    } catch (UserCancelException e) {
    // do nothing
    }
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Hashtable(java.util.Hashtable) BitSet(java.util.BitSet) UserCancelException(org.vcell.util.UserCancelException) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) UtilCancelException(org.vcell.util.UtilCancelException) UserCancelException(org.vcell.util.UserCancelException) TreeSet(java.util.TreeSet)

Example 92 with AsynchClientTask

use of cbit.vcell.client.task.AsynchClientTask in project vcell by virtualcell.

the class ROIMultiPaintManager method resizeDataset.

private void resizeDataset() {
    final ISize currentISize = new ISize(roiComposite[0].getWidth(), roiComposite[0].getHeight(), roiComposite.length);
    ImageSizeInfo origImageSizeInfo = new ImageSizeInfo(null, currentISize, 0, new double[] { 0 }, 0);
    ImageSizeInfo newImageSizeInfo = null;
    try {
        newImageSizeInfo = ClientRequestManager.queryImageResize(overlayEditorPanelJAI, origImageSizeInfo, false);
    } catch (UserCancelException e) {
        return;
    }
    final ISize newImageSize = newImageSizeInfo.getiSize();
    AsynchClientTask resizeTask = new AsynchClientTask("Resizing...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            resizeImpl(ResizeInfo.createScaleResizeInfo(currentISize, newImageSize), getClientTaskStatusSupport());
        }
    };
    final AsynchClientTask updatePanelTask = getUpdateDisplayAfterCropTask();
    ClientTaskDispatcher.dispatch(overlayEditorPanelJAI, new Hashtable<String, Object>(), new AsynchClientTask[] { resizeTask, updatePanelTask }, false, false, null, true);
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) ISize(org.vcell.util.ISize) Hashtable(java.util.Hashtable) UserCancelException(org.vcell.util.UserCancelException) ImageSizeInfo(cbit.image.ImageSizeInfo)

Example 93 with AsynchClientTask

use of cbit.vcell.client.task.AsynchClientTask in project vcell by virtualcell.

the class ROIMultiPaintManager method mergeResolvedSelections.

private void mergeResolvedSelections(final RegionInfo[] selectedRegionInfos) throws Exception {
    if (selectedRegionInfos == null || selectedRegionInfos.length == 0) {
        return;
    }
    // final String UNUSED_ROI_PIXVAL = "UNUSED_ROI_INDEX";
    // final String MULTI_NEIGHBOR_MERGE = "MultiNeighborMerge";
    AsynchClientTask mergeTask = new AsynchClientTask("Merging " + selectedRegionInfos.length + " regions...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            // make new RegionImage with all selections converted to a single temporary ROI
            // 
            // if(isROINameUsed(overlayEditorPanelJAI.getAllCompositeROINamesAndColors(), MULTI_NEIGHBOR_MERGE)){
            // throw new Exception(MULTI_NEIGHBOR_MERGE+" exists, they must be reconciled before any new merge");
            // }
            // sort selected region index for fast lookup
            TreeSet<Integer> selectedRegionIndexesTS = new TreeSet<Integer>();
            for (int i = 0; i < selectedRegionInfos.length; i++) {
                selectedRegionIndexesTS.add(selectedRegionInfos[i].getRegionIndex());
            }
            // //find unused index we can use for temporary ROI
            // BitSet usedROIIndexes = new BitSet();
            // for (int i = 0; i < roiComposite.length; i++) {
            // byte[] sliceBytes = ((DataBufferByte)roiComposite[i].getRaster().getDataBuffer()).getData();
            // for (int j = 0; j < sliceBytes.length; j++) {
            // usedROIIndexes.set((int)(sliceBytes[j]&0x000000FF));
            // }
            // }
            int unusedROIPixelValue = getUnusedROIColorIndex(overlayEditorPanelJAI.getAllCompositeROINamesAndColors(), null);
            // if(usedROIIndexes.get(unusedROIPixelValue)){
            // throw new Exception("Error: Found unused color index but that ROI pixel value exists");
            // }
            // find image indexes of selected regions and fill new ROIImage with temporary ROI Index
            byte[] shortEncodedRegionIndexArr = regionImage.getShortEncodedRegionIndexImage();
            BufferedImage[] tempROI = new BufferedImage[roiComposite.length];
            int count = 0;
            for (int i = 0; i < tempROI.length; i++) {
                byte[] roiBytes = ((DataBufferByte) roiComposite[i].getRaster().getDataBuffer()).getData();
                tempROI[i] = new BufferedImage(roiComposite[i].getWidth(), roiComposite[i].getHeight(), BufferedImage.TYPE_BYTE_INDEXED, getContrastIndexColorModel());
                byte[] sliceBytes = ((DataBufferByte) tempROI[i].getRaster().getDataBuffer()).getData();
                System.arraycopy(roiBytes, 0, sliceBytes, 0, roiBytes.length);
                for (int j = 0; j < sliceBytes.length; j++) {
                    int regionIndex = (int) ((0x000000ff & shortEncodedRegionIndexArr[2 * count]) | ((0x000000ff & shortEncodedRegionIndexArr[2 * count + 1]) << 8));
                    if (selectedRegionIndexesTS.contains(regionIndex)) {
                        sliceBytes[j] = (byte) unusedROIPixelValue;
                    }
                    count++;
                }
            }
            if (getClientTaskStatusSupport() != null) {
                getClientTaskStatusSupport().setProgress(10);
            }
            // release memory
            shortEncodedRegionIndexArr = null;
            // get new regionImage and new selectedRegionInfos
            VCImage tempImage = ROIMultiPaintManager.createVCImageFromBufferedImages(ROIMultiPaintManager.DEFAULT_EXTENT, tempROI);
            RegionImage tempRegionImage = new RegionImage(tempImage, 0, /*0 means generate no surfacecollection*/
            tempImage.getExtent(), ROIMultiPaintManager.DEFAULT_ORIGIN, RegionImage.NO_SMOOTHING, null);
            // release memory
            tempImage = null;
            RegionInfo[] tempRegionInfos = tempRegionImage.getRegionInfos();
            if (tempRegionInfos.length == 1) {
                throw new Exception("No unselected neighbors to merge with.");
            }
            if (getClientTaskStatusSupport() != null) {
                getClientTaskStatusSupport().setProgress(20);
            }
            Vector<RegionImage.RegionInfo> tempSelectedRegionInfos = new Vector<RegionImage.RegionInfo>();
            HighlightROIInfo highlightROIInfo = generateHighlightROIInfo((byte) -1, tempROI, tempRegionImage, RegionAction.createCheckNeighborsOnlyRegionAction(tempRegionInfos), null);
            boolean bHasSelectionWithMoreThanOneNeighbor = false;
            for (int i = 0; i < tempRegionInfos.length; i++) {
                if (tempRegionInfos[i].getPixelValue() == unusedROIPixelValue) {
                    tempSelectedRegionInfos.add(tempRegionInfos[i]);
                // if (highlightROIInfo.neighborsForRegionsMap.get(tempRegionInfos[i]).size() > 1) {
                // hashTable.put(UNUSED_ROI_PIXVAL, new Integer(unusedROIPixelValue));
                // bHasSelectionWithMoreThanOneNeighbor = true;
                // } else {
                // tempSelectedRegionInfos.add(tempRegionInfos[i]);
                // }
                }
            }
            if (getClientTaskStatusSupport() != null) {
                getClientTaskStatusSupport().setProgress(50);
            }
            // final merge
            updateUndo(UNDO_INIT.ALLZ);
            generateHighlightROIInfo((byte) unusedROIPixelValue, tempROI, tempRegionImage, RegionAction.createMergeSelectedWithNeighborsRegionAction(tempRegionInfos, tempSelectedRegionInfos, highlightROIInfo.neighborsForRegionsMap), null);
            // copy merged bytes back to ROI
            for (int i = 0; i < tempROI.length; i++) {
                byte[] roiBytes = ((DataBufferByte) roiComposite[i].getRaster().getDataBuffer()).getData();
                byte[] sliceBytes = ((DataBufferByte) tempROI[i].getRaster().getDataBuffer()).getData();
                // for (int j = 0; j < sliceBytes.length; j++) {
                // if(sliceBytes[j] == (byte)unusedROIPixelValue){
                // System.out.println("Bad");
                // }
                // }
                System.arraycopy(sliceBytes, 0, roiBytes, 0, roiBytes.length);
            }
            if (getClientTaskStatusSupport() != null) {
                getClientTaskStatusSupport().setProgress(90);
            }
        }
    };
    AsynchClientTask updateGUITask = new AsynchClientTask("Updating display...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            // Integer unusedROIIndex = (Integer)hashTable.get(UNUSED_ROI_PIXVAL);
            // if(unusedROIIndex != null){
            // overlayEditorPanelJAI.addROIName(MULTI_NEIGHBOR_MERGE, true, MULTI_NEIGHBOR_MERGE,true,/*true,true,*/OverlayEditorPanelJAI.CONTRAST_COLORS[unusedROIIndex]);
            // }
            overlayEditorPanelJAI.setHighliteInfo(null, OverlayEditorPanelJAI.FRAP_DATA_RESOLVEDMERGE_PROPERTY);
            updateUndoAfterPrivate(true, false);
        }
    };
    Vector<AsynchClientTask> asynchClientTaskV = new Vector<AsynchClientTask>();
    asynchClientTaskV.add(mergeTask);
    asynchClientTaskV.add(updateGUITask);
    asynchClientTaskV.addAll(Arrays.asList(getRefreshObjectsTasks()));
    ClientTaskDispatcher.dispatch(overlayEditorPanelJAI, new Hashtable<String, Object>(), asynchClientTaskV.toArray(new AsynchClientTask[0]), true, false, null, true);
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Hashtable(java.util.Hashtable) VCImage(cbit.image.VCImage) RegionInfo(cbit.vcell.geometry.RegionImage.RegionInfo) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) UtilCancelException(org.vcell.util.UtilCancelException) UserCancelException(org.vcell.util.UserCancelException) TreeSet(java.util.TreeSet) RegionImage(cbit.vcell.geometry.RegionImage) Vector(java.util.Vector)

Example 94 with AsynchClientTask

use of cbit.vcell.client.task.AsynchClientTask in project vcell by virtualcell.

the class ROIMultiPaintManager method padCropDataset.

private void padCropDataset() {
    int xm = 0;
    int ym = 0;
    int zm = 0;
    int xp = 0;
    int yp = 0;
    int zp = 0;
    String result = null;
    do {
        try {
            result = DialogUtils.showInputDialog0(overlayEditorPanelJAI, "Enter the number of pixels to add or crop (negative number) at each border: (xlow,ylow,zlow,xhigh,yhigh,zhigh)", xm + "," + ym + "," + zm + "," + xp + "," + yp + "," + zp);
            if (result != null) {
                final String SEP = ",";
                result = result.trim();
                StringTokenizer st = new StringTokenizer(result, SEP);
                xm = Integer.parseInt(st.nextToken());
                ym = Integer.parseInt(st.nextToken());
                zm = Integer.parseInt(st.nextToken());
                xp = Integer.parseInt(st.nextToken());
                yp = Integer.parseInt(st.nextToken());
                zp = Integer.parseInt(st.nextToken());
                if (st.hasMoreElements() || result.endsWith(SEP)) {
                    throw new Exception("Some input was not parsed, check input.");
                }
                break;
            }
        } catch (UtilCancelException e) {
            throw UserCancelException.CANCEL_GENERIC;
        } catch (Exception e) {
            DialogUtils.showErrorDialog(overlayEditorPanelJAI, "Error parsing '" + result + "' Resetting to valid values.  Enter 6 comma separated integers.");
        }
    } while (true);
    final CoordinateIndex low = new CoordinateIndex(xm, ym, zm);
    final CoordinateIndex high = new CoordinateIndex(xp, yp, zp);
    final AsynchClientTask padTask = new AsynchClientTask("Changeing borders...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            resizeImpl(ResizeInfo.createPadCropResizeInfo(getImageDataSetChannel().getISize(), low, high), getClientTaskStatusSupport());
        }
    };
    final AsynchClientTask updatePanelTask = getUpdateDisplayAfterCropTask();
    ClientTaskDispatcher.dispatch(overlayEditorPanelJAI, new Hashtable<String, Object>(), new AsynchClientTask[] { padTask, updatePanelTask }, false, false, null, true);
}
Also used : StringTokenizer(java.util.StringTokenizer) AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) UtilCancelException(org.vcell.util.UtilCancelException) Hashtable(java.util.Hashtable) CoordinateIndex(org.vcell.util.CoordinateIndex) Point(java.awt.Point) UtilCancelException(org.vcell.util.UtilCancelException) UserCancelException(org.vcell.util.UserCancelException)

Example 95 with AsynchClientTask

use of cbit.vcell.client.task.AsynchClientTask in project vcell by virtualcell.

the class ROIMultiPaintManager method getUpdateDisplayAfterCropTask.

private AsynchClientTask getUpdateDisplayAfterCropTask() {
    return new AsynchClientTask("Updating display...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            // Update display with cropped images
            if (overlayEditorPanelJAI != null) {
                int currentContrast = overlayEditorPanelJAI.getDisplayContrastFactor();
                overlayEditorPanelJAI.setHighliteInfo(null, OverlayEditorPanelJAI.FRAP_DATA_CROP_PROPERTY);
                overlayEditorPanelJAI.setAllROICompositeImage(null, OverlayEditorPanelJAI.FRAP_DATA_CROP_PROPERTY);
                updateUnderlayHistogramDisplay();
                overlayEditorPanelJAI.setAllROICompositeImage(roiComposite, OverlayEditorPanelJAI.FRAP_DATA_CROP_PROPERTY);
                overlayEditorPanelJAI.setDisplayContrastFactor(currentContrast);
                updateUndoAfter(false);
            }
        }
    };
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Hashtable(java.util.Hashtable) Point(java.awt.Point)

Aggregations

AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)229 Hashtable (java.util.Hashtable)219 ArrayList (java.util.ArrayList)68 UserCancelException (org.vcell.util.UserCancelException)52 File (java.io.File)35 CSGObject (cbit.vcell.geometry.CSGObject)30 Point (java.awt.Point)23 DataAccessException (org.vcell.util.DataAccessException)22 SimulationContext (cbit.vcell.mapping.SimulationContext)21 FRAPStudy (cbit.vcell.microscopy.FRAPStudy)18 PropertyVetoException (java.beans.PropertyVetoException)18 ImageException (cbit.image.ImageException)17 IOException (java.io.IOException)17 GeometryThumbnailImageFactoryAWT (cbit.vcell.geometry.GeometryThumbnailImageFactoryAWT)14 Vector (java.util.Vector)14 DataFormatException (java.util.zip.DataFormatException)14 UtilCancelException (org.vcell.util.UtilCancelException)14 Geometry (cbit.vcell.geometry.Geometry)12 ActionEvent (java.awt.event.ActionEvent)12 BioModel (cbit.vcell.biomodel.BioModel)11