1
1
package org .knime .knip .serialization ;
2
2
3
3
import java .io .IOException ;
4
+ import java .nio .ByteBuffer ;
4
5
import java .util .ArrayList ;
5
6
import java .util .Arrays ;
6
7
import java .util .List ;
25
26
import net .imagej .ImgPlus ;
26
27
import net .imagej .axis .Axes ;
27
28
import net .imagej .axis .CalibratedAxis ;
29
+ import net .imglib2 .util .Intervals ;
28
30
29
31
/**
30
32
* Serializing ImgPlus instances to byte stream. Used format is .tif.
@@ -40,13 +42,13 @@ public class ImgPlusToBytesConverter {
40
42
/**
41
43
* ImgSaver to write ImgPlus to stream as tif
42
44
*/
43
- private ImgSaver m_saver ;
45
+ private final ImgSaver m_saver ;
44
46
45
47
/**
46
48
* SCIFIO config to read/write images
47
49
*/
48
- private SCIFIOConfig m_scifioConfig ;
49
-
50
+ private final SCIFIOConfig m_scifioConfig ;
51
+
50
52
private Writer m_writer ;
51
53
52
54
public ImgPlusToBytesConverter () {
@@ -55,16 +57,18 @@ public ImgPlusToBytesConverter() {
55
57
m_scifioConfig .groupableSetGroupFiles (false );
56
58
m_scifioConfig .imgOpenerSetComputeMinMax (false );
57
59
}
58
-
60
+
59
61
public byte [] serialize (final ImgPlusValue <?> value ) throws IOException {
60
- if (m_writer == null ) {
62
+ if (m_writer == null ) {
61
63
m_writer = createWriter ();
62
64
}
63
-
65
+
64
66
final ImgPlus <?> imgPlus = TypeUtils .converted (value .getImgPlus ());
65
67
66
68
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 );
68
72
populateMeta (m_writer , imgPlus , m_scifioConfig , 0 );
69
73
// HACK Corresponds to filename
70
74
m_writer .getMetadata ().setDatasetName ("" );
@@ -73,24 +77,22 @@ public byte[] serialize(final ImgPlusValue<?> value) throws IOException {
73
77
m_saver .saveImg (m_writer , imgPlus .getImg (), m_scifioConfig );
74
78
75
79
m_writer .close ();
76
-
77
80
return handle .getBytes ();
78
- } catch (Exception e ) {
79
- e .printStackTrace ();
81
+ } catch (final Exception e ) {
80
82
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 );
82
85
}
83
86
}
84
-
85
- private Writer createWriter ()
86
- {
87
+
88
+ private Writer createWriter () {
87
89
try {
88
90
return ScifioGateway .format ().getWriterByExtension (".tif" );
89
91
} catch (FormatException e ) {
90
92
throw new RuntimeException (e );
91
93
}
92
94
}
93
-
95
+
94
96
/**
95
97
* This method is copied from SCIFIO
96
98
*
@@ -110,7 +112,7 @@ private void populateMeta(final Writer w, final ImgPlus<?> img, final SCIFIOConf
110
112
// Get format-specific metadata
111
113
Metadata imgMeta = ScifioGateway .getSCIFIO ().imgUtil ().makeSCIFIOImgPlus (img ).getMetadata ();
112
114
113
- final List <ImageMetadata > imageMeta = new ArrayList <ImageMetadata >();
115
+ final List <ImageMetadata > imageMeta = new ArrayList <>();
114
116
115
117
if (imgMeta == null ) {
116
118
imgMeta = new DefaultMetadata ();
0 commit comments