diff --git a/org.knime.knip.knimepython/plugin.xml b/org.knime.knip.knimepython/plugin.xml index c61b3e6..a6b6784 100644 --- a/org.knime.knip.knimepython/plugin.xml +++ b/org.knime.knip.knimepython/plugin.xml @@ -11,7 +11,7 @@ point="org.knime.python.typeextension.knimetopython"> @@ -19,7 +19,7 @@ point="org.knime.python.typeextension.pythontoknime"> diff --git a/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializer.java b/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializerFactory.java similarity index 75% rename from org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializer.java rename to org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializerFactory.java index 4626b59..32f3517 100644 --- a/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializer.java +++ b/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializerFactory.java @@ -15,13 +15,10 @@ * * @author Clemens von Schwerin, KNIME.com, Konstanz, Germany */ -public class ImgPlusDeserializer extends DeserializerFactory { +public class ImgPlusDeserializerFactory extends DeserializerFactory { - private final BytesToImgPlusConverter m_converter; - - public ImgPlusDeserializer() { + public ImgPlusDeserializerFactory() { super(ImgPlusCell.TYPE); - m_converter = new BytesToImgPlusConverter(); } @Override @@ -31,7 +28,7 @@ public Deserializer createDeserializer() { @Override public DataCell deserialize(final byte[] bytes, final FileStoreFactory fileStoreFactory) throws IOException { - return m_converter.deserialize(bytes, fileStoreFactory); + return new BytesToImgPlusConverter().deserialize(bytes, fileStoreFactory); } }; } diff --git a/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializer.java b/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializerFactory.java similarity index 75% rename from org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializer.java rename to org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializerFactory.java index ab0d9c1..f12b3cf 100644 --- a/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializer.java +++ b/org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializerFactory.java @@ -13,16 +13,13 @@ * @author Clemens von Schwerin, KNIME.com, Konstanz, Germany */ @SuppressWarnings("rawtypes") -public class ImgPlusSerializer extends SerializerFactory { - - private final ImgPlusToBytesConverter m_converter; +public class ImgPlusSerializerFactory extends SerializerFactory { /** * Constructor */ - public ImgPlusSerializer() { + public ImgPlusSerializerFactory() { super(ImgPlusValue.class); - m_converter = new ImgPlusToBytesConverter(); } @Override @@ -31,7 +28,7 @@ public Serializer> createSerializer() { @Override public byte[] serialize(final ImgPlusValue value) throws IOException { - return m_converter.serialize(value); + return new ImgPlusToBytesConverter().serialize(value); } }; } diff --git a/org.knime.knip.knimepython/src/org/knime/knip/serialization/ImgPlusToBytesConverter.java b/org.knime.knip.knimepython/src/org/knime/knip/serialization/ImgPlusToBytesConverter.java index f88189b..6bc8cfe 100644 --- a/org.knime.knip.knimepython/src/org/knime/knip/serialization/ImgPlusToBytesConverter.java +++ b/org.knime.knip.knimepython/src/org/knime/knip/serialization/ImgPlusToBytesConverter.java @@ -1,6 +1,7 @@ package org.knime.knip.serialization; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -25,6 +26,7 @@ import net.imagej.ImgPlus; import net.imagej.axis.Axes; import net.imagej.axis.CalibratedAxis; +import net.imglib2.util.Intervals; /** * Serializing ImgPlus instances to byte stream. Used format is .tif. @@ -40,13 +42,13 @@ public class ImgPlusToBytesConverter { /** * ImgSaver to write ImgPlus to stream as tif */ - private ImgSaver m_saver; + private final ImgSaver m_saver; /** * SCIFIO config to read/write images */ - private SCIFIOConfig m_scifioConfig; - + private final SCIFIOConfig m_scifioConfig; + private Writer m_writer; public ImgPlusToBytesConverter() { @@ -55,16 +57,18 @@ public ImgPlusToBytesConverter() { m_scifioConfig.groupableSetGroupFiles(false); m_scifioConfig.imgOpenerSetComputeMinMax(false); } - + public byte[] serialize(final ImgPlusValue value) throws IOException { - if(m_writer == null) { + if (m_writer == null) { m_writer = createWriter(); } - + final ImgPlus imgPlus = TypeUtils.converted(value.getImgPlus()); try { - final ByteArrayHandle handle = new ByteArrayHandle(); + final ByteBuffer buffer = ByteBuffer.allocate((int) Intervals.numElements(value.getDimensions())); + buffer.limit(0); + final ByteArrayHandle handle = new ByteArrayHandle(buffer); populateMeta(m_writer, imgPlus, m_scifioConfig, 0); // HACK Corresponds to filename m_writer.getMetadata().setDatasetName(""); @@ -73,24 +77,22 @@ public byte[] serialize(final ImgPlusValue value) throws IOException { m_saver.saveImg(m_writer, imgPlus.getImg(), m_scifioConfig); m_writer.close(); - return handle.getBytes(); - } catch (Exception e) { - e.printStackTrace(); + } catch (final Exception e) { throw new RuntimeException( - "Could not serialize image. Possible reasons: Unsupported image type, dimensionality of the image,..."); + "Could not serialize image. Possible reasons: Unsupported image type, dimensionality of the image,...", + e); } } - - private Writer createWriter() - { + + private Writer createWriter() { try { return ScifioGateway.format().getWriterByExtension(".tif"); } catch (FormatException e) { throw new RuntimeException(e); } } - + /** * This method is copied from SCIFIO * @@ -110,7 +112,7 @@ private void populateMeta(final Writer w, final ImgPlus img, final SCIFIOConf // Get format-specific metadata Metadata imgMeta = ScifioGateway.getSCIFIO().imgUtil().makeSCIFIOImgPlus(img).getMetadata(); - final List imageMeta = new ArrayList(); + final List imageMeta = new ArrayList<>(); if (imgMeta == null) { imgMeta = new DefaultMetadata();