Skip to content

Commit

Permalink
Only serialize actual image data, not the entire buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelWiedenmann authored and dietzc committed Sep 12, 2017
1 parent 27c4278 commit f11733c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions org.knime.knip.knimepython/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
point="org.knime.python.typeextension.knimetopython">
<type
id="org.knime.knip.knimepython.ImgPlusSerializer"
java-serializer-factory="org.knime.knip.knimepython.ImgPlusSerializer"
java-serializer-factory="org.knime.knip.knimepython.ImgPlusSerializerFactory"
python-deserializer="py/KNIPImageDeserializer.py">
</type>
</extension>
<extension
point="org.knime.python.typeextension.pythontoknime">
<type
id="org.knime.knip.knimepython.ImgPlusDeserializer"
java-deserializer-factory="org.knime.knip.knimepython.ImgPlusDeserializer"
java-deserializer-factory="org.knime.knip.knimepython.ImgPlusDeserializerFactory"
python-serializer="py/KNIPImageSerializer.py"
python-type-identifier="KNIPImage.KNIPImage">
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
* @author Clemens von Schwerin, KNIME.com, Konstanz, Germany
*/
@SuppressWarnings("rawtypes")
public class ImgPlusSerializer extends SerializerFactory<ImgPlusValue> {

private final ImgPlusToBytesConverter m_converter;
public class ImgPlusSerializerFactory extends SerializerFactory<ImgPlusValue> {

/**
* Constructor
*/
public ImgPlusSerializer() {
public ImgPlusSerializerFactory() {
super(ImgPlusValue.class);
m_converter = new ImgPlusToBytesConverter();
}

@Override
Expand All @@ -31,7 +28,7 @@ public Serializer<? extends ImgPlusValue<?>> createSerializer() {

@Override
public byte[] serialize(final ImgPlusValue<?> value) throws IOException {
return m_converter.serialize(value);
return new ImgPlusToBytesConverter().serialize(value);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -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() {
Expand All @@ -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("");
Expand All @@ -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
*
Expand All @@ -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<ImageMetadata> imageMeta = new ArrayList<ImageMetadata>();
final List<ImageMetadata> imageMeta = new ArrayList<>();

if (imgMeta == null) {
imgMeta = new DefaultMetadata();
Expand Down

0 comments on commit f11733c

Please sign in to comment.