Search in sources :

Example 1 with InverseTransformMapping

use of mpicbg.ij.stack.InverseTransformMapping in project TrakEM2 by trakem2.

the class Stack method fetchFutureImage.

private final Future<Image> fetchFutureImage(final Long imageId, final double magnification, final Layer active_layer, final boolean trigger_repaint_event) {
    synchronized (futureImages) {
        Future<Image> fu = futureImages.get(imageId);
        if (null == fu) {
            fu = project.getLoader().doLater(new Callable<Image>() {

                @Override
                public Image call() {
                    final InvertibleCoordinateTransformList<mpicbg.models.InvertibleCoordinateTransform> ictl = new InvertibleCoordinateTransformList<mpicbg.models.InvertibleCoordinateTransform>();
                    if (ict != null) {
                        // Utils.log2( "ictScale of " + getTitle() + " is " + ictScale );
                        ictl.add(ict);
                        /* Remove boundingBox shift ict ... */
                        final TranslationModel3D unShiftBounds = new TranslationModel3D();
                        unShiftBounds.set(-boundsMin[0], -boundsMin[1], 0);
                        ictl.add(unShiftBounds);
                        if (ictScale != 1.0) {
                            final AffineModel3D unScaleXY = new AffineModel3D();
                            unScaleXY.set(1.0 / ictScale, 0, 0, 0, 0, 1.0 / ictScale, 0, 0, 0, 0, 1.0, 0);
                            ictl.add(unScaleXY);
                        }
                    }
                    /* TODO remove that scale from ict and put it into atp */
                    final ImagePlus imp = project.getLoader().fetchImagePlus(Stack.this);
                    final ImageProcessor ip = imp.getStack().getProcessor(1).createProcessor((int) Math.ceil((boundsMax[0] - boundsMin[0]) / ictScale), (int) Math.ceil((boundsMax[1] - boundsMin[1]) / ictScale));
                    // Utils.log2( "ictScale is " + ictScale );
                    // Utils.log2( "rendering an image of " + ip.getWidth() + " x " + ip.getHeight() + " px" );
                    final double currentZ = active_layer.getZ();
                    final TranslationModel3D sliceShift = new TranslationModel3D();
                    sliceShift.set(0, 0, -currentZ);
                    ictl.add(sliceShift);
                    /* optimization: if ict is affine, reduce ictl into a single affine */
                    final InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform> mapping;
                    if (AffineModel3D.class.isInstance(ict)) {
                        final AffineModel3D ictAffine = new AffineModel3D();
                        boolean isAffine = true;
                        for (final mpicbg.models.InvertibleCoordinateTransform t : ictl.getList(null)) {
                            if (AffineModel3D.class.isInstance(t))
                                ictAffine.preConcatenate((AffineModel3D) t);
                            else if (TranslationModel3D.class.isInstance(t))
                                ictAffine.preConcatenate((TranslationModel3D) t);
                            else {
                                isAffine = false;
                                break;
                            }
                        }
                        if (isAffine)
                            mapping = new InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform>(ictAffine);
                        else
                            mapping = new InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform>(ictl);
                    } else
                        mapping = new InverseTransformMapping<mpicbg.models.InvertibleCoordinateTransform>(ictl);
                    mapping.mapInterpolated(imp.getStack(), ip);
                    // wast: atp
                    final double s = estimateAffineScale(new AffineTransform(at));
                    final double smoothMag = magnification * s * ictScale;
                    if (smoothMag < 1.0f) {
                        Filter.smoothForScale(ip, smoothMag, 0.5f, 0.5f);
                    }
                    final Image image = ip.createImage();
                    if (null == image) {
                        Utils.log2("Stack.paint: null image, returning");
                        // TEMPORARY from lazy
                        return null;
                    // repaints after closing a
                    // Project
                    }
                    project.getLoader().cacheAWT(imageId, image);
                    synchronized (futureImages) {
                        futureImages.remove(imageId);
                    }
                    if (trigger_repaint_event) {
                        // Display.repaint( active_layer, Stack.this );
                        Display.repaint(active_layer);
                    }
                    return image;
                }
            });
        }
        // else {
        // Utils.log2( "fu is not null" );
        // // We don't do anything: we wait for itself to launch a
        // repaint event
        // }
        futureImages.put(imageId, fu);
        return fu;
    }
}
Also used : Image(java.awt.Image) ImagePlus(ij.ImagePlus) Callable(java.util.concurrent.Callable) InvertibleCoordinateTransformList(mpicbg.models.InvertibleCoordinateTransformList) ImageProcessor(ij.process.ImageProcessor) InvertibleCoordinateTransform(mpicbg.trakem2.transform.InvertibleCoordinateTransform) TranslationModel3D(mpicbg.models.TranslationModel3D) AffineTransform(java.awt.geom.AffineTransform) AffineModel3D(mpicbg.models.AffineModel3D) InverseTransformMapping(mpicbg.ij.stack.InverseTransformMapping)

Aggregations

ImagePlus (ij.ImagePlus)1 ImageProcessor (ij.process.ImageProcessor)1 Image (java.awt.Image)1 AffineTransform (java.awt.geom.AffineTransform)1 Callable (java.util.concurrent.Callable)1 InverseTransformMapping (mpicbg.ij.stack.InverseTransformMapping)1 AffineModel3D (mpicbg.models.AffineModel3D)1 InvertibleCoordinateTransformList (mpicbg.models.InvertibleCoordinateTransformList)1 TranslationModel3D (mpicbg.models.TranslationModel3D)1 InvertibleCoordinateTransform (mpicbg.trakem2.transform.InvertibleCoordinateTransform)1