Search in sources :

Example 26 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class PatchStack method setProcessor.

public void setProcessor(String title, ImageProcessor ip) {
    // so not applied to stacks
    if (1 != patch.length)
        return;
    Loader loader = patch[currentSlice - 1].getProject().getLoader();
    ImagePlus imp = loader.fetchImagePlus(patch[currentSlice - 1]);
    if (ip.getWidth() != imp.getWidth() || ip.getHeight() != imp.getHeight())
        throw new IllegalArgumentException("PatchStack: ip wrong size");
    // null means don't touch title
    imp.setProcessor(null, ip);
    patch[currentSlice - 1].updateInDatabase("tiff_working");
// loader.vacuum();
// repainting elsewhere
}
Also used : Loader(ini.trakem2.persistence.Loader) ImagePlus(ij.ImagePlus)

Example 27 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class PatchStack method saveImages.

/**
 * Store working copies and remake the awts and repaint.
 */
public void saveImages() {
    Utils.log2("PatchStack: calling saveImages");
    if (!this.changes) {
        Utils.log2("PatchStack.saveImages: nothing changed.");
        return;
    }
    Loader loader = patch[currentSlice - 1].getProject().getLoader();
    Utils.showProgress(0);
    for (int i = 0; i < patch.length; i++) {
        ImagePlus imp = loader.fetchImagePlus(patch[i]);
        Utils.log2("PatchStack.saveImages: patch imp " + i + " has the imp.changes=" + imp.changes + " and the called[i]=" + called[i]);
        if (imp.changes || called[i]) {
            // may be doing it twice, check TODO
            patch[i].updateInDatabase("tiff_working");
            /*
				patch[i].createImage(); //flushes the old awt, and creates the new one, and stores it in the cache.
				*/
            // just flush away all dependent images, will be recreated when needed on repaint
            patch[i].getProject().getLoader().decache(imp);
            Display.repaint(patch[i].getLayer(), patch[i], 0);
            // reset
            imp.changes = false;
            called[i] = false;
        }
        Utils.showProgress((i + 1.0) / patch.length);
    }
    this.changes = false;
    // patch[0].getProject().getLoader().vacuum();
    Utils.showProgress(1);
}
Also used : Loader(ini.trakem2.persistence.Loader) ImagePlus(ij.ImagePlus)

Example 28 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class StitchingTEM method makeStripe.

// static public ImageProcessor makeStripe(final Patch p, final double scale, final boolean ignore_patch_transform) {
// return makeStripe(p, null, scale, ignore_patch_transform);
// }
/**
 * @return FloatProcessor.
 * @param ignore_patch_transform will prevent resizing of the ImageProcessor in the event of the Patch having a transform different than identity.
 */
// TODO 2: there is a combination of options that ends up resulting in the actual ImageProcessor of the Patch being returned as is, which is DANGEROUS because it can potentially result in changes in the data.
public static ImageProcessor makeStripe(final Patch p, final Roi roi, final double scale, final boolean ignore_patch_transform) {
    ImagePlus imp = null;
    ImageProcessor ip = null;
    final Loader loader = p.getProject().getLoader();
    // check if using mipmaps and if there is a file for it. If there isn't, most likely this method is being called in an import sequence as grid procedure.
    if (loader.isMipMapsRegenerationEnabled() && loader.checkMipMapFileExists(p, scale)) {
        // Read the transform image from the patch (this way we avoid the JPEG artifacts)
        final Patch.PatchImage pai = p.createTransformedImage();
        pai.target.setMinAndMax(p.getMin(), p.getMax());
        // p.getProject().getLoader().fetchImage(p, scale);
        Image image = pai.target.createImage();
        // Utils.log2("patch w,h " + p.getWidth() + ", " + p.getHeight() + " fetched image w,h: " + image.getWidth(null) + ", " + image.getHeight(null));
        if (Math.abs(p.getWidth() * scale - image.getWidth(null)) > 0.001 || Math.abs(p.getHeight() * scale - image.getHeight(null)) > 0.001) {
            // slow but good quality. Makes an RGB image, but it doesn't matter.
            image = image.getScaledInstance((int) (p.getWidth() * scale), (int) (p.getHeight() * scale), Image.SCALE_AREA_AVERAGING);
        // Utils.log2("   resizing, now image w,h: " + image.getWidth(null) + ", " + image.getHeight(null));
        }
        try {
            imp = new ImagePlus("s", image);
            ip = imp.getProcessor();
        // imp.show();
        } catch (final Exception e) {
            IJError.print(e);
        }
        // cut
        if (null != roi) {
            // scale ROI!
            Rectangle rb = roi.getBounds();
            final Roi roi2 = new Roi((int) (rb.x * scale), (int) (rb.y * scale), (int) (rb.width * scale), (int) (rb.height * scale));
            rb = roi2.getBounds();
            if (ip.getWidth() != rb.width || ip.getHeight() != rb.height) {
                ip.setRoi(roi2);
                ip = ip.crop();
            }
        }
    // Utils.log2("scale: " + scale + "  ip w,h: " + ip.getWidth() + ", " + ip.getHeight());
    } else {
        final Patch.PatchImage pai = p.createTransformedImage();
        pai.target.setMinAndMax(p.getMin(), p.getMax());
        ip = pai.target;
        imp = new ImagePlus("", ip);
        // compare and adjust
        if (!ignore_patch_transform && p.getAffineTransform().getType() != AffineTransform.TYPE_TRANSLATION) {
            // if it's not only a translation:
            final Rectangle b = p.getBoundingBox();
            ip = ip.resize(b.width, b.height);
        // Utils.log2("resizing stripe for patch: " + p);
        // the above is only meant to correct for improperly acquired images at the microscope, the scale only.
        }
        // cut
        if (null != roi) {
            final Rectangle rb = roi.getBounds();
            if (ip.getWidth() != rb.width || ip.getHeight() != rb.height) {
                ip.setRoi(roi);
                ip = ip.crop();
            }
        }
        // scale
        if (scale < 1) {
            // floats have 4 bytes, plus some java peripherals correction factor
            p.getProject().getLoader().releaseToFit((long) (ip.getWidth() * ip.getHeight() * 4 * 1.2));
            ip = ip.convertToFloat();
            // scaling with area averaging is the same as a gaussian of sigma 0.5/scale and then resize with nearest neightbor So this line does the gaussian, and line below does the neares-neighbor scaling
            ip.setPixels(ImageFilter.computeGaussianFastMirror(new FloatArray2D((float[]) ip.getPixels(), ip.getWidth(), ip.getHeight()), Math.sqrt(0.25 / scale / scale - 0.25)).data);
            // scale maintaining aspect ratio
            ip = ip.resize((int) (ip.getWidth() * scale));
        }
    }
    // return a FloatProcessor
    if (imp.getType() != ImagePlus.GRAY32)
        return ip.convertToFloat();
    return ip;
}
Also used : ImageProcessor(ij.process.ImageProcessor) FloatArray2D(mpi.fruitfly.math.datastructures.FloatArray2D) Rectangle(java.awt.Rectangle) Loader(ini.trakem2.persistence.Loader) Image(java.awt.Image) ImagePlus(ij.ImagePlus) Patch(ini.trakem2.display.Patch) Roi(ij.gui.Roi)

Example 29 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class Util method deserializeFeatures.

/**
 * Retrieve a {@link Collection} of {@link Feature Features} from the
 * TrakEM2 project folder.  The {@link Collection} is only returned if
 * <ol>
 * <li>the file as identified by project, prefix, and id exists and</li>
 * <li>its contained key {@link Object#equals(Object) equals} key.</li>
 * </ol>
 *
 * @param project
 * @param key
 * @param prefix
 * @param id
 * @return
 */
protected static final ArrayList<Feature> deserializeFeatures(final Project project, final Object key, final String prefix, final long id) {
    final String name = prefix == null ? "features" : prefix + ".features";
    final Loader loader = project.getLoader();
    final Object ob = loader.deserialize(new StringBuilder(loader.getUNUIdFolder()).append("features.ser/").append(FSLoader.createIdPath(Long.toString(id), name, ".ser")).toString());
    if (ob != null) {
        try {
            final Features fe = (Features) ob;
            // Utils.log( fe.key == null ? "key is null" : key.equals( fe.key ) ? "key is equal" : "key is not equal" );
            if (fe.key != null && key.equals(fe.key))
                return fe.features;
        } catch (final Exception e) {
            Utils.log("Exception during feature deserialization.");
            e.printStackTrace();
        }
    } else
        Utils.log("features file null");
    return null;
}
Also used : Loader(ini.trakem2.persistence.Loader) FSLoader(ini.trakem2.persistence.FSLoader)

Example 30 with Loader

use of ini.trakem2.persistence.Loader in project TrakEM2 by trakem2.

the class ExportARGB method makeFlatImageARGBFromMipMaps.

/**
 * Returns nonsense or throws an Exception if mipmaps are not available.
 * Limited to 2GB arrays for the final image.
 *
 * @param patches
 * @param roi
 * @param backgroundValue
 * @param scale
 * @return
 */
public static final Pair<ColorProcessor, ByteProcessor> makeFlatImageARGBFromMipMaps(final List<Patch> patches, final Rectangle roi, final double backgroundValue, final double scale) {
    final int width = (int) (roi.width * scale);
    final int height = (int) (roi.height * scale);
    // Process the three channels separately in order to use proper alpha composition
    final ColorProcessor target = new ColorProcessor(width, height);
    target.setInterpolationMethod(ImageProcessor.BILINEAR);
    final ByteProcessor targetMask = new ByteProcessor(width, height);
    targetMask.setInterpolationMethod(ImageProcessor.BILINEAR);
    final Loader loader = patches.get(0).getProject().getLoader();
    for (final Patch patch : patches) {
        // MipMap image, already including any coordinate transforms and the alpha mask (if any), by definition.
        final MipMapImage mipMap = loader.fetchImage(patch, scale);
        // / DEBUG: is there an alpha channel at all?
        // new ij.ImagePlus("alpha of " + patch.getTitle(), new ByteProcessor( mipMap.image.getWidth(null), mipMap.image.getHeight(null), new ColorProcessor( mipMap.image ).getChannel( 4 ))).show();
        // Yes, there is, even though the mipmap images have the alpha pre-multiplied
        // Work-around strange bug that makes mipmap-loaded images paint with 7-bit depth instead of 8-bit depth
        final BufferedImage bi = new BufferedImage(mipMap.image.getWidth(null), mipMap.image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(mipMap.image, 0, 0, null);
        g2d.dispose();
        final int[] pix = extractARGBIntArray(bi);
        bi.flush();
        // DEBUG: does the BufferedImage have the alpha channel?
        // {
        // final byte[] aa = new byte[pix.length];
        // for (int i=0; i<aa.length; ++i) aa[i] = (byte)((pix[i] & 0xff000000) >> 24);
        // new ij.ImagePlus("alpha of BI of " + patch.getTitle(), new ByteProcessor(bi.getWidth(), bi.getHeight(), aa)).show();
        // }
        // YES: the alpha, containing the outside too. All fine.
        final ByteProcessor alpha;
        final ColorProcessor rgb = new ColorProcessor(bi.getWidth(), bi.getHeight(), pix);
        if (patch.hasAlphaChannel()) {
            // The mipMap has the alpha channel in it, even if the alpha is pre-multiplied as well onto the images.
            final byte[] a = new byte[pix.length];
            for (int i = 0; i < a.length; ++i) {
                a[i] = (byte) ((pix[i] & 0xff000000) >> 24);
            }
            alpha = new ByteProcessor(bi.getWidth(), bi.getHeight(), a);
        } else {
            alpha = new ByteProcessor(bi.getWidth(), bi.getHeight());
            Arrays.fill((byte[]) alpha.getPixels(), (byte) 255);
        }
        // The affine to apply to the MipMap.image
        final AffineTransform atc = new AffineTransform();
        atc.scale(scale, scale);
        atc.translate(-roi.x, -roi.y);
        final AffineTransform at = new AffineTransform();
        at.preConcatenate(atc);
        at.concatenate(patch.getAffineTransform());
        at.scale(mipMap.scaleX, mipMap.scaleY);
        final AffineModel2D aff = new AffineModel2D();
        aff.set(at);
        final CoordinateTransformMesh mesh = new CoordinateTransformMesh(aff, patch.getMeshResolution(), bi.getWidth(), bi.getHeight());
        final TransformMeshMappingWithMasks<CoordinateTransformMesh> mapping = new TransformMeshMappingWithMasks<CoordinateTransformMesh>(mesh);
        // no interpolation
        alpha.setInterpolationMethod(ImageProcessor.NEAREST_NEIGHBOR);
        rgb.setInterpolationMethod(ImageProcessor.BILINEAR);
        mapping.map(rgb, alpha, target, targetMask);
    }
    return new Pair<ColorProcessor, ByteProcessor>(target, targetMask);
}
Also used : ByteProcessor(ij.process.ByteProcessor) Loader(ini.trakem2.persistence.Loader) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ColorProcessor(ij.process.ColorProcessor) MipMapImage(ini.trakem2.display.MipMapImage) CoordinateTransformMesh(mpicbg.models.CoordinateTransformMesh) AffineTransform(java.awt.geom.AffineTransform) Patch(ini.trakem2.display.Patch) Pair(mpicbg.trakem2.util.Pair)

Aggregations

Loader (ini.trakem2.persistence.Loader)20 ImagePlus (ij.ImagePlus)13 Patch (ini.trakem2.display.Patch)11 FSLoader (ini.trakem2.persistence.FSLoader)11 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 LayerThing (ini.trakem2.tree.LayerThing)6 Rectangle (java.awt.Rectangle)6 ImageProcessor (ij.process.ImageProcessor)5 Layer (ini.trakem2.display.Layer)5 TemplateThing (ini.trakem2.tree.TemplateThing)5 ZDisplayable (ini.trakem2.display.ZDisplayable)4 ProjectThing (ini.trakem2.tree.ProjectThing)4 IllDefinedDataPointsException (mpicbg.models.IllDefinedDataPointsException)4 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)4 ImageStack (ij.ImageStack)3 GenericDialog (ij.gui.GenericDialog)3 Displayable (ini.trakem2.display.Displayable)3 LayerSet (ini.trakem2.display.LayerSet)3 LayerTree (ini.trakem2.tree.LayerTree)3