diff --git a/org.knime.knip.base/src/org/knime/knip/base/data/img/ImgPlusCell.java b/org.knime.knip.base/src/org/knime/knip/base/data/img/ImgPlusCell.java index 93dc913f..99462990 100644 --- a/org.knime.knip.base/src/org/knime/knip/base/data/img/ImgPlusCell.java +++ b/org.knime.knip.base/src/org/knime/knip/base/data/img/ImgPlusCell.java @@ -65,7 +65,6 @@ import org.knime.core.data.filestore.FileStoreCell; import org.knime.knip.base.KNIMEKNIPPlugin; import org.knime.knip.base.data.CachedObjectAccess; -import org.knime.knip.base.data.CachedObjectAccess.StreamSkipper; import org.knime.knip.base.data.FileStoreCellMetadata; import org.knime.knip.base.data.IntervalValue; import org.knime.knip.base.renderer.ThumbnailRenderer; @@ -73,7 +72,6 @@ import org.knime.knip.core.awt.AWTImageTools; import org.knime.knip.core.awt.Real2GreyColorRenderer; import org.knime.knip.core.data.img.DefaultImgMetadata; -import org.knime.knip.core.io.externalization.BufferedDataInputStream; import org.knime.knip.core.io.externalization.ExternalizerManager; import org.knime.knip.core.util.MinimaUtils; import org.scijava.Named; @@ -107,6 +105,8 @@ public class ImgPlusCell> extends FileStoreCell implements ImgPlusValue, StringValue, IntervalValue { + private static final String IMG_PLUS_CELL_KEY = "IP"; + /** * Type * @@ -122,7 +122,7 @@ public class ImgPlusCell> extends FileStoreCell /** * UID */ - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 2L; private FileStoreCellMetadata m_fileMetadata; @@ -158,19 +158,19 @@ protected ImgPlusCell(final Img img, final ImgPlusMetadata metadata, final Fi tmpImg = ((WrappedImg)tmpImg).getImg(); } - m_imgAccess = new CachedObjectAccess>(fileStore, tmpImg); + m_imgAccess = new CachedObjectAccess<>(fileStore, tmpImg); final long[] dimensions = new long[img.numDimensions()]; img.dimensions(dimensions); - m_metadataAccess = new CachedObjectAccess(fileStore, + m_metadataAccess = new CachedObjectAccess<>(fileStore, new ImgPlusCellMetadata( MetadataUtil.copyImgPlusMetadata(metadata, new DefaultImgMetadata(dimensions.length)), img.size(), getMinFromImg(img), dimensions, img.firstElement().getClass(), null)); m_fileMetadata = new FileStoreCellMetadata(-1, false, null); - CACHE.put(this, this); + CACHE.put(this.stringHashCode(), this); } /** @@ -241,7 +241,14 @@ private BufferedImage createThumbnail(final double factor) { */ @Override protected boolean equalsDataCell(final DataCell dc) { - return dc.hashCode() == hashCode(); + if (dc instanceof ImgPlusCell) { + ImgPlusCell dc2 = (ImgPlusCell)dc; + if (dc2.getFileStore().getFile().equals(this.getFileStore().getFile()) + && dc2.m_fileMetadata.getOffset() == this.m_fileMetadata.getOffset()) { + return true; + } + } + return false; } /** @@ -308,7 +315,7 @@ public synchronized ImgPlus getImgPlus() { @Override public synchronized ImgPlus getImgPlusCopy() { final ImgPlus source = getImgPlus(); - final ImgPlus dest = new ImgPlus(source.copy()); + final ImgPlus dest = new ImgPlus<>(source.copy()); MetadataUtil.copyImgPlusMetadata(source, dest); @@ -461,7 +468,7 @@ public synchronized Image getThumbnail(final RenderingHints renderingHints) { tmp = new ImgPlusCellMetadata(tmp.getMetadata(), tmp.getSize(), tmp.getMinimum(), tmp.getDimensions(), tmp.getPixelType(), createThumbnail(height / fullHeight))); // update cached object - CACHE.put(this, this); + CACHE.put(this.stringHashCode(), this); } return tmp.getThumbnail(); } @@ -482,7 +489,11 @@ private int getThumbnailWidth(final int height) { */ @Override public int hashCode() { - return (int)(getFileStore().getFile().hashCode() + (31 * m_fileMetadata.getOffset())); + return stringHashCode().hashCode(); + } + + private String stringHashCode() { + return IMG_PLUS_CELL_KEY + getFileStore().getFile().getName() + m_fileMetadata.getOffset(); } /** @@ -517,28 +528,20 @@ protected synchronized void load(final DataInput input) throws IOException { protected void postConstruct() { @SuppressWarnings("unchecked") - final ImgPlusCell tmp = (ImgPlusCell)CACHE.get(this); + final ImgPlusCell tmp = (ImgPlusCell)CACHE.get(this.stringHashCode()); if (tmp == null) { - m_metadataAccess = - new CachedObjectAccess(getFileStore(), null, m_fileMetadata.getOffset(), null); - - m_imgAccess = new CachedObjectAccess>(getFileStore(), null, m_fileMetadata.getOffset(), - new StreamSkipper() { - /** - * {@inheritDoc} - */ - @Override - public void skip(final BufferedDataInputStream in) { - try { - m_metadataAccess.setObject(ExternalizerManager.read(in)); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }); - - CACHE.put(this, this); + m_metadataAccess = new CachedObjectAccess<>(getFileStore(), null, m_fileMetadata.getOffset(), null); + + m_imgAccess = new CachedObjectAccess<>(getFileStore(), null, m_fileMetadata.getOffset(), in -> { + try { + m_metadataAccess.setObject(ExternalizerManager.read(in)); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + CACHE.put(this.stringHashCode(), this); } else { m_metadataAccess = tmp.m_metadataAccess; m_imgAccess = tmp.m_imgAccess; diff --git a/org.knime.knip.base/src/org/knime/knip/base/data/labeling/LabelingCell.java b/org.knime.knip.base/src/org/knime/knip/base/data/labeling/LabelingCell.java index c8870880..e9aad62d 100644 --- a/org.knime.knip.base/src/org/knime/knip/base/data/labeling/LabelingCell.java +++ b/org.knime.knip.base/src/org/knime/knip/base/data/labeling/LabelingCell.java @@ -64,7 +64,6 @@ import org.knime.core.data.filestore.FileStoreCell; import org.knime.knip.base.KNIMEKNIPPlugin; import org.knime.knip.base.data.CachedObjectAccess; -import org.knime.knip.base.data.CachedObjectAccess.StreamSkipper; import org.knime.knip.base.data.FileStoreCellMetadata; import org.knime.knip.base.data.IntervalValue; import org.knime.knip.base.renderer.ThumbnailRenderer; @@ -75,7 +74,6 @@ import org.knime.knip.core.awt.labelingcolortable.RandomMissingColorHandler; import org.knime.knip.core.data.LabelingView; import org.knime.knip.core.data.img.LabelingMetadata; -import org.knime.knip.core.io.externalization.BufferedDataInputStream; import org.knime.knip.core.io.externalization.ExternalizerManager; import org.scijava.Named; import org.scijava.cache.CacheService; @@ -102,6 +100,8 @@ */ public class LabelingCell extends FileStoreCell implements LabelingValue, StringValue, IntervalValue { + private static final String LABELING_CELL_KEY = "LC"; + /** * ObjectRepository */ @@ -110,7 +110,7 @@ public class LabelingCell extends FileStoreCell implements LabelingValue, /** * UID */ - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 2L; /** * Convenience access method for DataType.getType(ImageCell.class). @@ -154,12 +154,12 @@ protected LabelingCell(final RandomAccessibleInterval> labeling, super(fileStore); final long[] dimensions = new long[labeling.numDimensions()]; labeling.dimensions(dimensions); - m_metadataAccess = new CachedObjectAccess(fileStore, + m_metadataAccess = new CachedObjectAccess<>(fileStore, new LabelingCellMetadata(metadata, Views.iterable(labeling).size(), dimensions, null)); - m_labelingAccess = new CachedObjectAccess<>(fileStore, new LabelingView(labeling)); + m_labelingAccess = new CachedObjectAccess<>(fileStore, new LabelingView<>(labeling)); m_fileMetadata = new FileStoreCellMetadata(-1, false, null); - CACHE.put(this, this); + CACHE.put(this.stringHashCode(), this); } @@ -173,7 +173,7 @@ private BufferedImage createThumbnail(final double factor) { } // set the labeling mapping - final ColorLabelingRenderer rend = new ColorLabelingRenderer(); + final ColorLabelingRenderer rend = new ColorLabelingRenderer<>(); rend.setLabelMapping(lab2d.randomAccess().get().getMapping()); int i = 0; final long[] max = new long[lab2d.numDimensions()]; @@ -202,7 +202,14 @@ private BufferedImage createThumbnail(final double factor) { */ @Override protected boolean equalsDataCell(final DataCell dc) { - return dc.hashCode() == hashCode(); + if (dc instanceof LabelingCell) { + LabelingCell dc2 = (LabelingCell)dc; + if (dc2.getFileStore().getFile().equals(this.getFileStore().getFile()) + && dc2.m_fileMetadata.getOffset() == this.m_fileMetadata.getOffset()) { + return true; + } + } + return false; } /** @@ -356,17 +363,21 @@ public synchronized Image getThumbnail(final RenderingHints renderingHints) { fullHeight = tmp.getDimensions()[1]; } if ((tmp.getThumbnail() == null) || (tmp.getThumbnail().getHeight() != height)) { - m_metadataAccess = new CachedObjectAccess(getFileStore(), + m_metadataAccess = new CachedObjectAccess<>(getFileStore(), tmp = new LabelingCellMetadata(tmp.getLabelingMetadata(), tmp.getSize(), tmp.getDimensions(), createThumbnail(height / fullHeight))); // update cached object - CACHE.put(this, this); + CACHE.put(this.stringHashCode(), this); } return tmp.getThumbnail(); } } + private String stringHashCode() { + return LABELING_CELL_KEY + getFileStore().getFile().getName() + m_fileMetadata.getOffset(); + } + private int getThumbnailWidth(final int height) { final LabelingCellMetadata tmp = m_metadataAccess.get(); if (tmp.getDimensions().length == 1) { @@ -381,7 +392,7 @@ private int getThumbnailWidth(final int height) { */ @Override public int hashCode() { - return (int)(getFileStore().getFile().hashCode() + (31 * m_fileMetadata.getOffset())); + return stringHashCode().hashCode(); } /** @@ -415,27 +426,20 @@ protected synchronized void load(final DataInput input) throws IOException { protected void postConstruct() { @SuppressWarnings("unchecked") - final LabelingCell tmp = (LabelingCell)CACHE.get(this); + final LabelingCell tmp = (LabelingCell)CACHE.get(this.stringHashCode()); if (tmp == null) { // Creates empty CachedObjectAccesses which know how to reconstruct the managed objects. m_metadataAccess = new CachedObjectAccess<>(getFileStore(), null, m_fileMetadata.getOffset(), null); - m_labelingAccess = - new CachedObjectAccess<>(getFileStore(), null, m_fileMetadata.getOffset(), new StreamSkipper() { - /** - * {@inheritDoc} - */ - @Override - public void skip(final BufferedDataInputStream in) { - try { - m_metadataAccess.setObject(ExternalizerManager.read(in)); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }); - - CACHE.put(this, this); + m_labelingAccess = new CachedObjectAccess<>(getFileStore(), null, m_fileMetadata.getOffset(), in -> { + try { + m_metadataAccess.setObject(ExternalizerManager.read(in)); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + CACHE.put(this.stringHashCode(), this); } else { m_labelingAccess = tmp.m_labelingAccess; m_metadataAccess = tmp.m_metadataAccess; diff --git a/org.knime.knip.core/src/org/knime/knip/core/KNIPGuavaCacheService.java b/org.knime.knip.core/src/org/knime/knip/core/KNIPGuavaCacheService.java index 736532ae..64f3c08d 100644 --- a/org.knime.knip.core/src/org/knime/knip/core/KNIPGuavaCacheService.java +++ b/org.knime.knip.core/src/org/knime/knip/core/KNIPGuavaCacheService.java @@ -66,50 +66,44 @@ /** * {@link CacheService} implementation wrapping a guava {@link Cache}. */ -@Plugin(type = Service.class, priority = Priority.HIGH_PRIORITY) +@Plugin(type = Service.class, priority = Priority.HIGH) public class KNIPGuavaCacheService extends AbstractService implements CacheService { @Parameter private MemoryService ms; - private Cache cache; + private Cache cache; private final Semaphore gate = new Semaphore(1); @Override public void initialize() { //FIXME: Make parameters accessible via image processing config at some point - cache = CacheBuilder.newBuilder().maximumSize(1000).weakValues() - .build(); - - ms.register(new MemoryAlertable() { - - @Override - public void memoryLow() { - if (gate.tryAcquire()) { - cache.invalidateAll(); - cache.cleanUp(); - gate.release(); - } + cache = CacheBuilder.newBuilder().maximumSize(1000).weakValues().build(); + ms.register(() -> { + if (gate.tryAcquire()) { + cache.invalidateAll(); + cache.cleanUp(); + gate.release(); } }); } @Override public void put(final Object key, final Object value) { - cache.put(key.hashCode(), value); + cache.put(key.toString(), value); } @Override public Object get(final Object key) { - return cache.getIfPresent(key.hashCode()); + return cache.getIfPresent(key.toString()); } @SuppressWarnings("unchecked") @Override public V get(final Object key, final Callable valueLoader) throws ExecutionException { - return (V)cache.get(key.hashCode(), valueLoader); + return (V)cache.get(key.toString(), valueLoader); } /** diff --git a/org.knime.knip.core/src/org/knime/knip/core/ui/imgviewer/panels/providers/AWTImageProvider.java b/org.knime.knip.core/src/org/knime/knip/core/ui/imgviewer/panels/providers/AWTImageProvider.java index a6185955..e74b586d 100644 --- a/org.knime.knip.core/src/org/knime/knip/core/ui/imgviewer/panels/providers/AWTImageProvider.java +++ b/org.knime.knip.core/src/org/knime/knip/core/ui/imgviewer/panels/providers/AWTImageProvider.java @@ -92,6 +92,8 @@ public class AWTImageProvider extends HiddenViewerComponent { private static final NodeLogger LOGGER = NodeLogger.getLogger(AWTImageProvider.class); + private static final String AWT_IMAGE_PROVIDE_KEY = "AP"; + /** * Converts DoubleType to FloatType and preserves other types. This can be used to ensure that images (after calling * the method) are FloatType or smaller and thus can be normalized. However type safety is broken! @@ -158,13 +160,13 @@ private void renderAndCacheImg() { Image awtImage = null; if (m_isCachingActive) { - final int hash = (31 + m_renderUnit.generateHashCode()); - awtImage = (Image)KNIPGateway.cache().get(hash); + final String hashKey = AWT_IMAGE_PROVIDE_KEY + m_renderUnit.generateHashCode(); + awtImage = (Image)KNIPGateway.cache().get(hashKey); if (awtImage == null) { awtImage = m_renderUnit.createImage(); - KNIPGateway.cache().put(hash, awtImage); + KNIPGateway.cache().put(hashKey, awtImage); LOGGER.info("Caching Image ..."); } else { LOGGER.info("Image from Cache ..."); diff --git a/org.knime.knip.features/src/org/knime/knip/features/sets/optimizedfeatures/KNIPCachedOpEnvironment.java b/org.knime.knip.features/src/org/knime/knip/features/sets/optimizedfeatures/KNIPCachedOpEnvironment.java index f842e392..ab19d424 100644 --- a/org.knime.knip.features/src/org/knime/knip/features/sets/optimizedfeatures/KNIPCachedOpEnvironment.java +++ b/org.knime.knip.features/src/org/knime/knip/features/sets/optimizedfeatures/KNIPCachedOpEnvironment.java @@ -59,8 +59,6 @@ */ public class KNIPCachedOpEnvironment extends CustomOpEnvironment { - @Parameter - private CacheService cs; private Collection> ignored; public KNIPCachedOpEnvironment(final OpEnvironment parent, final Collection prioritizedInfos, diff --git a/org.knime.knip.io/src/org/knime/knip/io/ImgRefCell.java b/org.knime.knip.io/src/org/knime/knip/io/ImgRefCell.java index 83f6287c..58d1935d 100644 --- a/org.knime.knip.io/src/org/knime/knip/io/ImgRefCell.java +++ b/org.knime.knip.io/src/org/knime/knip/io/ImgRefCell.java @@ -105,6 +105,7 @@ * Zinsmaier */ @SuppressWarnings("serial") +@Deprecated public class ImgRefCell & NativeType> extends BlobDataCell implements ImgRefValue, ImgPlusValue, StringValue { @@ -185,6 +186,8 @@ public void serialize(final ImgRefCell cell, final DataCellDataOutput output) th */ public static final DataType TYPE = DataType.getType(ImgRefCell.class); + private static final String IMG_REF_KEY = "IR"; + /** * Returns the factory to read/write DataCells of this class from/to a * DataInput/DataOutput. This method is called via reflection. @@ -264,10 +267,10 @@ public long[] getDimensions() { return m_originalDims; } try { - long[] dim = (long[]) CACHE.get(m_imgRef + DIM_SUFFIX); + long[] dim = (long[]) CACHE.get(IMG_REF_KEY + m_imgRef + DIM_SUFFIX); if (dim == null) { dim = ImgSourcePool.getImgSource(m_sourceID).getDimensions(m_imgRef, 0); - CACHE.put(m_imgRef + DIM_SUFFIX, dim); + CACHE.put(IMG_REF_KEY + m_imgRef + DIM_SUFFIX, dim); } return dim; } catch (final Exception e) { @@ -291,10 +294,10 @@ public String getImageReference() { private Img getImg() { final long[] dims = getDimensions(); try { - Img img = (Img) CACHE.get(m_imgRef + IMG_SUFFIX); + Img img = (Img) CACHE.get(IMG_REF_KEY + m_imgRef + IMG_SUFFIX); if (img == null) { img = (Img) ImgSourcePool.getImgSource(m_sourceID).getImg(m_imgRef, 0); - CACHE.put(m_imgRef + IMG_SUFFIX, img); + CACHE.put(IMG_REF_KEY + m_imgRef + IMG_SUFFIX, img); } return img; } catch (final Exception e) { @@ -321,7 +324,7 @@ private Img getImgCopy() { */ @Override public ImgPlus getImgPlus() { - return new ImgPlus(getImg(), getMetadata()); + return new ImgPlus<>(getImg(), getMetadata()); } /** @@ -329,7 +332,7 @@ public ImgPlus getImgPlus() { */ @Override public ImgPlus getImgPlusCopy() { - return new ImgPlus(getImgCopy(), getMetadata()); + return new ImgPlus<>(getImgCopy(), getMetadata()); } /** @@ -342,21 +345,21 @@ public ImgPlusMetadata getMetadata() { List tmpAxes; try { - tmpAxes = (List) CACHE.get(m_imgRef + AXES_SUFFIX); + tmpAxes = (List) CACHE.get(IMG_REF_KEY + m_imgRef + AXES_SUFFIX); if (tmpAxes == null) { tmpAxes = ImgSourcePool.getImgSource(m_sourceID).getAxes(m_imgRef, 0); - CACHE.put(m_imgRef + AXES_SUFFIX, tmpAxes); + CACHE.put(IMG_REF_KEY + m_imgRef + AXES_SUFFIX, tmpAxes); } } catch (final Exception e) { noAccessWarning(e); - tmpAxes = new ArrayList(); + tmpAxes = new ArrayList<>(); for (int i = 0; i < getDimensions().length; i++) { tmpAxes.add(new DefaultLinearAxis(Axes.get("Unknown " + i))); } } // setting everything to metadata - final List axes = new ArrayList(tmpAxes); + final List axes = new ArrayList<>(tmpAxes); // TODO: Can be replaced by FinalMetadata?! return new ImgPlusMetadata() { @@ -516,15 +519,7 @@ public String getSource() { */ @Override public String getStringValue() { - final ImgSource fac = (ImgSource) CACHE.get(m_sourceID); String facDesc = "unknown source"; - if (fac != null) { - try { - facDesc = fac.getSource(m_sourceID); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } return m_imgRef + " (" + facDesc + ")"; } diff --git a/org.knime.knip.io/src/org/knime/knip/io/ImgRefValue.java b/org.knime.knip.io/src/org/knime/knip/io/ImgRefValue.java index d9537ff6..64f6506d 100644 --- a/org.knime.knip.io/src/org/knime/knip/io/ImgRefValue.java +++ b/org.knime.knip.io/src/org/knime/knip/io/ImgRefValue.java @@ -68,6 +68,7 @@ * @author Michael * Zinsmaier */ +@Deprecated public interface ImgRefValue extends DataValue { /** Gathers meta information to this type. */ diff --git a/org.knime.knip.io/src/org/knime/knip/io/nodes/IONodeSetFactory.java b/org.knime.knip.io/src/org/knime/knip/io/nodes/IONodeSetFactory.java index b435d714..46e87be2 100644 --- a/org.knime.knip.io/src/org/knime/knip/io/nodes/IONodeSetFactory.java +++ b/org.knime.knip.io/src/org/knime/knip/io/nodes/IONodeSetFactory.java @@ -122,7 +122,6 @@ public Collection getNodeFactoryIds() { // m_nodeFactories.put(ImgReaderNodeFactory.class.getCanonicalName(), // "/community/knip/io"); m_nodeFactories.put(ImgWriter2NodeFactory.class.getCanonicalName(), "/community/knip/io"); - m_nodeFactories.put(ImageFileRefNodeFactory.class.getCanonicalName(), "/community/knip/io/other"); m_nodeFactories.put(ImgImporterNodeFactory.class.getCanonicalName(), "/community/knip/io/other"); m_nodeFactories.put(OverlayAnnotatorNodeFactory.class.getCanonicalName(), "/community/knip/labeling"); m_nodeFactories.put(ImgReader2NodeFactory.class.getCanonicalName(), "/community/knip/io"); diff --git a/org.knime.knip.io/src/org/knime/knip/io/nodes/fileref/ImageFileRefNodeFactory.xml b/org.knime.knip.io/src/org/knime/knip/io/nodes/fileref/ImageFileRefNodeFactory.xml index fd4303c5..f0fc10fb 100644 --- a/org.knime.knip.io/src/org/knime/knip/io/nodes/fileref/ImageFileRefNodeFactory.xml +++ b/org.knime.knip.io/src/org/knime/knip/io/nodes/fileref/ImageFileRefNodeFactory.xml @@ -5,7 +5,7 @@ Contributors: IBM Corporation - initial API and implementation --> - + Image File Linker Creates references to image files.