Search in sources :

Example 81 with Patch

use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.

the class FSLoader method addToDatabase.

/* GENERIC, from DBObject calls. Records the id of the object in the HashMap ht_dbo.
	 * Always returns true. Does not check if another object has the same id.
	 */
public boolean addToDatabase(final DBObject ob) {
    synchronized (db_lock) {
        setChanged(true);
        final long id = ob.getId();
        if (id > max_id) {
            max_id = id;
        }
        if (ob.getClass() == Patch.class) {
            final Patch p = (Patch) ob;
            if (p.hasCoordinateTransform()) {
                max_blob_id = Math.max(p.getCoordinateTransformId(), max_blob_id);
            }
            if (p.hasAlphaMask()) {
                max_blob_id = Math.max(p.getAlphaMaskId(), max_blob_id);
            }
        }
    }
    return true;
}
Also used : Patch(ini.trakem2.display.Patch)

Example 82 with Patch

use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.

the class FSLoader method removeFromDatabase.

public boolean removeFromDatabase(final DBObject ob) {
    synchronized (db_lock) {
        setChanged(true);
        // remove from the hashtable
        final long loid = ob.getId();
        Utils.log2("removing " + Project.getName(ob.getClass()) + " " + ob);
        if (ob.getClass() == Patch.class) {
            try {
                // STRATEGY change: images are not owned by the FSLoader.
                Patch p = (Patch) ob;
                if (!ob.getProject().getBooleanProperty("keep_mipmaps"))
                    removeMipMaps(p);
                // after removeMipMaps !
                ht_paths.remove(p.getId());
                mawts.remove(loid);
                cannot_regenerate.remove(p);
                // locks on its own
                flushMipMaps(p.getId());
                touched_mipmaps.remove(p);
                return true;
            } catch (Throwable t) {
                handleCacheError(t);
            }
        }
    }
    return true;
}
Also used : Patch(ini.trakem2.display.Patch)

Example 83 with Patch

use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.

the class FSLoader method destroy.

@Override
public synchronized void destroy() {
    super.destroy();
    Utils.showStatus("", false);
    // delete mipmap files that where touched and not cleared as saved (i.e. the project was not saved)
    touched_mipmaps.addAll(mipmaps_to_remove);
    Set<Patch> touched = new HashSet<Patch>();
    synchronized (touched_mipmaps) {
        touched.addAll(touched_mipmaps);
    }
    for (final Patch p : touched) {
        // with slice info appended
        File f = new File(getAbsolutePath(p));
        // Utils.log2("File f is " + f);
        Utils.log2("Removing mipmaps for " + p);
        // Cannot run in the remover: is a daemon, and would be interrupted.
        removeMipMaps(createIdPath(Long.toString(p.getId()), f.getName(), mExt), (int) p.getWidth(), (int) p.getHeight());
    }
    // remove empty trakem2.mipmaps folder if any
    if (null != dir_mipmaps && !dir_mipmaps.equals(dir_storage)) {
        File f = new File(dir_mipmaps);
        if (f.isDirectory() && 0 == f.list(new FilenameFilter() {

            public boolean accept(File fdir, String name) {
                File file = new File(dir_mipmaps + name);
                if (file.isHidden() || '.' == name.charAt(0))
                    return false;
                return true;
            }
        }).length) {
            try {
                f.delete();
            } catch (Exception e) {
                Utils.log("Could not remove empty trakem2.mipmaps directory.");
            }
        }
    }
    // remove crash detector
    try {
        File fm = new File(dir_mipmaps + ".open.t2");
        if (!fm.delete()) {
            Utils.log2("WARNING: could not delete crash detector file .open.t2 from trakem2.mipmaps folder at " + dir_mipmaps);
        }
    } catch (Exception e) {
        Utils.log2("WARNING: crash detector file trakem.mipmaps/.open.t2 may NOT have been deleted.");
        IJError.print(e);
    }
    if (null == ControlWindow.getProjects() || 1 == ControlWindow.getProjects().size()) {
        destroyStaticServices();
    }
    // remove unuid dir if xml_path is empty (i.e. never saved and not opened from an .xml file)
    if (null == project_file_path) {
        Utils.log2("Removing unuid dir, since project was never saved.");
        final File f = new File(getUNUIdFolder());
        if (null != dir_mipmaps)
            Utils.removePrefixedFiles(f, "trakem2.mipmaps", null);
        if (null != dir_masks)
            Utils.removePrefixedFiles(f, "trakem2.masks", null);
        Utils.removePrefixedFiles(f, "features.ser", null);
        Utils.removePrefixedFiles(f, "pointmatches.ser", null);
        // Only if empty:
        if (f.isDirectory()) {
            try {
                if (!f.delete()) {
                    Utils.log2("Could not delete unuid directory: likely not empty!");
                }
            } catch (Exception e) {
                Utils.log2("Could not delete unuid directory: " + e);
            }
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) Patch(ini.trakem2.display.Patch) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 84 with Patch

use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.

the class FSLoader method fetchMipMapAWT.

/**
 * Will NOT free memory.
 */
private final MipMapImage fetchMipMapAWT(final Patch patch, final int level, final long n_bytes, final int retries) {
    if (null == dir_mipmaps) {
        Utils.log2("null dir_mipmaps");
        return null;
    }
    while (retries < MAX_RETRIES) {
        try {
            // TODO should wait if the file is currently being generated
            final MipMapImage mipMap = fetchMipMap(patch, level, n_bytes);
            if (null != mipMap)
                return mipMap;
            // if we got so far ... try to regenerate the mipmaps
            if (!mipmaps_regen) {
                return null;
            }
            // check that REALLY the file doesn't exist.
            if (cannot_regenerate.contains(patch)) {
                Utils.log("Cannot regenerate mipmaps for patch " + patch);
                return null;
            }
            // Utils.log2("getMipMapAwt: imp is " + imp + " for path " +  dir_mipmaps + level + "/" + new File(getAbsolutePath(patch)).getName() + "." + patch.getId() + mExt);
            // Regenerate in the case of not asking for an image under 32x32
            double scale = 1 / Math.pow(2, level);
            if (level >= 0 && patch.getWidth() * scale >= 32 && patch.getHeight() * scale >= 32 && isMipMapsRegenerationEnabled()) {
                // regenerate in a separate thread
                regenerateMipMaps(patch);
                return new MipMapImage(REGENERATING, patch.getWidth() / REGENERATING.getWidth(), patch.getHeight() / REGENERATING.getHeight());
            }
        } catch (OutOfMemoryError oome) {
            Utils.log2("fetchMipMapAWT: recovering from OutOfMemoryError");
            recoverOOME();
            Thread.yield();
            // Retry:
            return fetchMipMapAWT(patch, level, n_bytes, retries + 1);
        } catch (Throwable t) {
            IJError.print(t);
        }
    }
    return null;
}
Also used : MipMapImage(ini.trakem2.display.MipMapImage)

Example 85 with Patch

use of ini.trakem2.display.Patch in project TrakEM2 by trakem2.

the class FSLoader method fetchDataImage.

/**
 * Waits until a proper image of the desired size or larger can be returned, which is never the Loader.REGENERATING image.
 *  If no image can be loaded, returns Loader.NOT_FOUND.
 *  If the Patch is undergoing mipmap regeneration, it waits until done.
 */
@Override
public MipMapImage fetchDataImage(final Patch p, final double mag) {
    Future<Boolean> fu = null;
    MipMapImage mipMap = null;
    synchronized (gm_lock) {
        fu = regenerating_mipmaps.get(p);
    }
    if (null == fu) {
        // Patch is currently not under regeneration
        mipMap = fetchImage(p, mag);
        // and img will be now Loader.REGENERATING
        if (Loader.REGENERATING != mipMap.image) {
            return mipMap;
        } else {
            synchronized (gm_lock) {
                fu = regenerating_mipmaps.get(p);
            }
        }
    }
    if (null != fu) {
        try {
            if (!fu.get()) {
                Utils.log("Loader.fetchDataImage: could not regenerate mipmaps and get an image for patch " + p);
                return new MipMapImage(NOT_FOUND, p.getWidth() / NOT_FOUND.getWidth(), p.getHeight() / NOT_FOUND.getHeight());
            }
            // Now the image should be good:
            mipMap = fetchImage(p, mag);
            // Check in any case:
            if (Loader.isSignalImage(mipMap.image)) {
                // Attempt to create from scratch
                return new MipMapImage(p.createTransformedImage().createImage(p.getMin(), p.getMax()), 1, 1);
            } else {
                return mipMap;
            }
        } catch (Throwable e) {
            IJError.print(e);
        }
    }
    // else:
    Utils.log("Loader.fetchDataImage: could not get a data image for patch " + p);
    return new MipMapImage(NOT_FOUND, p.getWidth() / NOT_FOUND.getWidth(), p.getHeight() / NOT_FOUND.getHeight());
}
Also used : MipMapImage(ini.trakem2.display.MipMapImage)

Aggregations

Patch (ini.trakem2.display.Patch)69 ArrayList (java.util.ArrayList)46 Layer (ini.trakem2.display.Layer)39 ImagePlus (ij.ImagePlus)34 Rectangle (java.awt.Rectangle)28 Point (mpicbg.models.Point)26 HashSet (java.util.HashSet)24 Displayable (ini.trakem2.display.Displayable)23 AffineTransform (java.awt.geom.AffineTransform)20 Loader (ini.trakem2.persistence.Loader)15 File (java.io.File)15 Future (java.util.concurrent.Future)15 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)15 PointMatch (mpicbg.models.PointMatch)14 Worker (ini.trakem2.utils.Worker)13 HashMap (java.util.HashMap)13 ExecutorService (java.util.concurrent.ExecutorService)12 ImageProcessor (ij.process.ImageProcessor)11 AffineModel2D (mpicbg.models.AffineModel2D)11 GenericDialog (ij.gui.GenericDialog)10