Skip to content

Commit f11733c

Browse files
MarcelWiedenmanndietzc
authored andcommitted
Only serialize actual image data, not the entire buffer
1 parent 27c4278 commit f11733c

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
lines changed

org.knime.knip.knimepython/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
point="org.knime.python.typeextension.knimetopython">
1212
<type
1313
id="org.knime.knip.knimepython.ImgPlusSerializer"
14-
java-serializer-factory="org.knime.knip.knimepython.ImgPlusSerializer"
14+
java-serializer-factory="org.knime.knip.knimepython.ImgPlusSerializerFactory"
1515
python-deserializer="py/KNIPImageDeserializer.py">
1616
</type>
1717
</extension>
1818
<extension
1919
point="org.knime.python.typeextension.pythontoknime">
2020
<type
2121
id="org.knime.knip.knimepython.ImgPlusDeserializer"
22-
java-deserializer-factory="org.knime.knip.knimepython.ImgPlusDeserializer"
22+
java-deserializer-factory="org.knime.knip.knimepython.ImgPlusDeserializerFactory"
2323
python-serializer="py/KNIPImageSerializer.py"
2424
python-type-identifier="KNIPImage.KNIPImage">
2525
</type>

org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializer.java renamed to org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusDeserializerFactory.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
*
1616
* @author Clemens von Schwerin, KNIME.com, Konstanz, Germany
1717
*/
18-
public class ImgPlusDeserializer extends DeserializerFactory {
18+
public class ImgPlusDeserializerFactory extends DeserializerFactory {
1919

20-
private final BytesToImgPlusConverter m_converter;
21-
22-
public ImgPlusDeserializer() {
20+
public ImgPlusDeserializerFactory() {
2321
super(ImgPlusCell.TYPE);
24-
m_converter = new BytesToImgPlusConverter();
2522
}
2623

2724
@Override
@@ -31,7 +28,7 @@ public Deserializer createDeserializer() {
3128
@Override
3229
public DataCell deserialize(final byte[] bytes, final FileStoreFactory fileStoreFactory)
3330
throws IOException {
34-
return m_converter.deserialize(bytes, fileStoreFactory);
31+
return new BytesToImgPlusConverter().deserialize(bytes, fileStoreFactory);
3532
}
3633
};
3734
}

org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializer.java renamed to org.knime.knip.knimepython/src/org/knime/knip/knimepython/ImgPlusSerializerFactory.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
* @author Clemens von Schwerin, KNIME.com, Konstanz, Germany
1414
*/
1515
@SuppressWarnings("rawtypes")
16-
public class ImgPlusSerializer extends SerializerFactory<ImgPlusValue> {
17-
18-
private final ImgPlusToBytesConverter m_converter;
16+
public class ImgPlusSerializerFactory extends SerializerFactory<ImgPlusValue> {
1917

2018
/**
2119
* Constructor
2220
*/
23-
public ImgPlusSerializer() {
21+
public ImgPlusSerializerFactory() {
2422
super(ImgPlusValue.class);
25-
m_converter = new ImgPlusToBytesConverter();
2623
}
2724

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

3229
@Override
3330
public byte[] serialize(final ImgPlusValue<?> value) throws IOException {
34-
return m_converter.serialize(value);
31+
return new ImgPlusToBytesConverter().serialize(value);
3532
}
3633
};
3734
}

org.knime.knip.knimepython/src/org/knime/knip/serialization/ImgPlusToBytesConverter.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.knime.knip.serialization;
22

33
import java.io.IOException;
4+
import java.nio.ByteBuffer;
45
import java.util.ArrayList;
56
import java.util.Arrays;
67
import java.util.List;
@@ -25,6 +26,7 @@
2526
import net.imagej.ImgPlus;
2627
import net.imagej.axis.Axes;
2728
import net.imagej.axis.CalibratedAxis;
29+
import net.imglib2.util.Intervals;
2830

2931
/**
3032
* Serializing ImgPlus instances to byte stream. Used format is .tif.
@@ -40,13 +42,13 @@ public class ImgPlusToBytesConverter {
4042
/**
4143
* ImgSaver to write ImgPlus to stream as tif
4244
*/
43-
private ImgSaver m_saver;
45+
private final ImgSaver m_saver;
4446

4547
/**
4648
* SCIFIO config to read/write images
4749
*/
48-
private SCIFIOConfig m_scifioConfig;
49-
50+
private final SCIFIOConfig m_scifioConfig;
51+
5052
private Writer m_writer;
5153

5254
public ImgPlusToBytesConverter() {
@@ -55,16 +57,18 @@ public ImgPlusToBytesConverter() {
5557
m_scifioConfig.groupableSetGroupFiles(false);
5658
m_scifioConfig.imgOpenerSetComputeMinMax(false);
5759
}
58-
60+
5961
public byte[] serialize(final ImgPlusValue<?> value) throws IOException {
60-
if(m_writer == null) {
62+
if (m_writer == null) {
6163
m_writer = createWriter();
6264
}
63-
65+
6466
final ImgPlus<?> imgPlus = TypeUtils.converted(value.getImgPlus());
6567

6668
try {
67-
final ByteArrayHandle handle = new ByteArrayHandle();
69+
final ByteBuffer buffer = ByteBuffer.allocate((int) Intervals.numElements(value.getDimensions()));
70+
buffer.limit(0);
71+
final ByteArrayHandle handle = new ByteArrayHandle(buffer);
6872
populateMeta(m_writer, imgPlus, m_scifioConfig, 0);
6973
// HACK Corresponds to filename
7074
m_writer.getMetadata().setDatasetName("");
@@ -73,24 +77,22 @@ public byte[] serialize(final ImgPlusValue<?> value) throws IOException {
7377
m_saver.saveImg(m_writer, imgPlus.getImg(), m_scifioConfig);
7478

7579
m_writer.close();
76-
7780
return handle.getBytes();
78-
} catch (Exception e) {
79-
e.printStackTrace();
81+
} catch (final Exception e) {
8082
throw new RuntimeException(
81-
"Could not serialize image. Possible reasons: Unsupported image type, dimensionality of the image,...");
83+
"Could not serialize image. Possible reasons: Unsupported image type, dimensionality of the image,...",
84+
e);
8285
}
8386
}
84-
85-
private Writer createWriter()
86-
{
87+
88+
private Writer createWriter() {
8789
try {
8890
return ScifioGateway.format().getWriterByExtension(".tif");
8991
} catch (FormatException e) {
9092
throw new RuntimeException(e);
9193
}
9294
}
93-
95+
9496
/**
9597
* This method is copied from SCIFIO
9698
*
@@ -110,7 +112,7 @@ private void populateMeta(final Writer w, final ImgPlus<?> img, final SCIFIOConf
110112
// Get format-specific metadata
111113
Metadata imgMeta = ScifioGateway.getSCIFIO().imgUtil().makeSCIFIOImgPlus(img).getMetadata();
112114

113-
final List<ImageMetadata> imageMeta = new ArrayList<ImageMetadata>();
115+
final List<ImageMetadata> imageMeta = new ArrayList<>();
114116

115117
if (imgMeta == null) {
116118
imgMeta = new DefaultMetadata();

0 commit comments

Comments
 (0)