11package org .knime .knip .serialization ;
22
33import java .io .IOException ;
4+ import java .nio .ByteBuffer ;
45import java .util .ArrayList ;
56import java .util .Arrays ;
67import java .util .List ;
2526import net .imagej .ImgPlus ;
2627import net .imagej .axis .Axes ;
2728import 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