|
7 | 7 |
|
8 | 8 | import org.knime.knip.base.data.img.ImgPlusValue; |
9 | 9 | import org.knime.knip.io.ScifioGateway; |
| 10 | +import org.knime.knip.serialization.ImgPlusToBytesConvertor; |
10 | 11 | import org.knime.python.typeextension.Serializer; |
11 | 12 | import org.knime.python.typeextension.SerializerFactory; |
12 | 13 |
|
|
28 | 29 | import net.imagej.axis.CalibratedAxis; |
29 | 30 |
|
30 | 31 | /** |
31 | | - * Serializing ImgPlus instances to byte stream. Used format is .tif. |
32 | | - * |
33 | | - * TODO: Use a Scijava Service in the background such that one could replace the |
34 | | - * way images are serialized/deserialized |
| 32 | + * Serializer using the extension points in org.knime.python. |
35 | 33 | * |
36 | | - * @author Christian Dietz (University of Konstanz) |
37 | | - * |
| 34 | + * @author Clemens von Schwerin, KNIME.com, Konstanz, Germany |
38 | 35 | */ |
39 | 36 | @SuppressWarnings("rawtypes") |
40 | 37 | public class ImgPlusSerializer extends SerializerFactory<ImgPlusValue> { |
41 | 38 |
|
42 | | - /** |
43 | | - * ImgSaver to write ImgPlus to stream as tif |
44 | | - */ |
45 | | - private ImgSaver m_saver; |
46 | | - |
47 | | - /** |
48 | | - * SCIFIO config to read/write images |
49 | | - */ |
50 | | - private SCIFIOConfig m_scifioConfig; |
51 | | - |
| 39 | + private final ImgPlusToBytesConvertor m_convertor; |
52 | 40 | /** |
53 | 41 | * Constructor |
54 | 42 | */ |
55 | 43 | public ImgPlusSerializer() { |
56 | 44 | super(ImgPlusValue.class); |
57 | | - m_saver = new ImgSaver(ScifioGateway.getSCIFIO().getContext()); |
58 | | - m_scifioConfig = new SCIFIOConfig(); |
59 | | - m_scifioConfig.groupableSetGroupFiles(false); |
60 | | - m_scifioConfig.imgOpenerSetComputeMinMax(false); |
| 45 | + m_convertor = new ImgPlusToBytesConvertor(); |
61 | 46 | } |
62 | 47 |
|
63 | 48 | @Override |
64 | 49 | public Serializer<? extends ImgPlusValue<?>> createSerializer() { |
65 | 50 |
|
66 | 51 | return new Serializer<ImgPlusValue<?>>() { |
67 | 52 |
|
68 | | - private final Writer m_writer; |
69 | | - |
70 | | - { |
71 | | - try { |
72 | | - m_writer = ScifioGateway.format().getWriterByExtension(".tif"); |
73 | | - } catch (FormatException e) { |
74 | | - throw new RuntimeException(e); |
75 | | - } |
76 | | - } |
77 | | - |
78 | 53 | @Override |
79 | 54 | public byte[] serialize(final ImgPlusValue<?> value) throws IOException { |
80 | | - final ImgPlus<?> imgPlus = TypeUtils.converted(value.getImgPlus()); |
81 | | - |
82 | | - try { |
83 | | - final ByteArrayHandle handle = new ByteArrayHandle(); |
84 | | - populateMeta(m_writer, imgPlus, m_scifioConfig, 0); |
85 | | - // HACK Corresponds to filename |
86 | | - m_writer.getMetadata().setDatasetName(""); |
87 | | - m_writer.setDest(new RandomAccessOutputStream(handle), 0); |
88 | | - |
89 | | - m_saver.saveImg(m_writer, imgPlus.getImg(), m_scifioConfig); |
90 | | - |
91 | | - m_writer.close(); |
92 | | - |
93 | | - return handle.getBytes(); |
94 | | - } catch (Exception e) { |
95 | | - e.printStackTrace(); |
96 | | - throw new RuntimeException( |
97 | | - "Could not serialize image. Possible reasons: Unsupported image type, dimensionality of the image,..."); |
98 | | - } |
| 55 | + return m_convertor.serialize(value); |
99 | 56 | } |
100 | 57 | }; |
101 | 58 | } |
102 | | - |
103 | | - /** |
104 | | - * This method is copied from SCIFIO |
105 | | - * |
106 | | - * FIXME/TODO: Remove when method available in SCIFIO (see |
107 | | - * https://github.com/scifio/scifio/issues/233) |
108 | | - * |
109 | | - * Uses the provided {@link SCIFIOImgPlus} to populate the minimum metadata |
110 | | - * fields necessary for writing. |
111 | | - * |
112 | | - * @param imageIndex |
113 | | - */ |
114 | | - private void populateMeta(final Writer w, final ImgPlus<?> img, final SCIFIOConfig config, final int imageIndex) |
115 | | - throws FormatException, IOException, ImgIOException { |
116 | | - |
117 | | - final Metadata meta = w.getFormat().createMetadata(); |
118 | | - |
119 | | - // Get format-specific metadata |
120 | | - Metadata imgMeta = ScifioGateway.getSCIFIO().imgUtil().makeSCIFIOImgPlus(img).getMetadata(); |
121 | | - |
122 | | - final List<ImageMetadata> imageMeta = new ArrayList<ImageMetadata>(); |
123 | | - |
124 | | - if (imgMeta == null) { |
125 | | - imgMeta = new DefaultMetadata(); |
126 | | - imgMeta.createImageMetadata(1); |
127 | | - imageMeta.add(imgMeta.get(0)); |
128 | | - } else { |
129 | | - for (int i = 0; i < imgMeta.getImageCount(); i++) { |
130 | | - imageMeta.add(new DefaultImageMetadata()); |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - // Create Img-specific ImageMetadatas |
135 | | - final int pixelType = ScifioGateway.getSCIFIO().imgUtil().makeType(img.firstElement()); |
136 | | - |
137 | | - // TODO is there some way to consolidate this with the isCompressible |
138 | | - // method? |
139 | | - final CalibratedAxis[] axes = new CalibratedAxis[img.numDimensions()]; |
140 | | - img.axes(axes); |
141 | | - |
142 | | - final long[] axisLengths = new long[img.numDimensions()]; |
143 | | - img.dimensions(axisLengths); |
144 | | - |
145 | | - for (int i = 0; i < imageMeta.size(); i++) { |
146 | | - final ImageMetadata iMeta = imageMeta.get(i); |
147 | | - iMeta.populate(img.getName(), Arrays.asList(axes), axisLengths, pixelType, true, false, false, false, true); |
148 | | - |
149 | | - // Adjust for RGB information |
150 | | - if (img.getCompositeChannelCount() > 1) { |
151 | | - if (config.imgSaverGetWriteRGB()) { |
152 | | - iMeta.setPlanarAxisCount(3); |
153 | | - } |
154 | | - iMeta.setAxisType(2, Axes.CHANNEL); |
155 | | - // Split Axes.CHANNEL if necessary |
156 | | - if (iMeta.getAxisLength(Axes.CHANNEL) > img.getCompositeChannelCount()) { |
157 | | - iMeta.addAxis(Axes.get("Channel-planes", false), |
158 | | - iMeta.getAxisLength(Axes.CHANNEL) / img.getCompositeChannelCount()); |
159 | | - iMeta.setAxisLength(Axes.CHANNEL, img.getCompositeChannelCount()); |
160 | | - } |
161 | | - } |
162 | | - } |
163 | | - |
164 | | - // Translate to the output metadata |
165 | | - final Translator t = ScifioGateway.getSCIFIO().translator().findTranslator(imgMeta, meta, false); |
166 | | - |
167 | | - t.translate(imgMeta, imageMeta, meta); |
168 | | - |
169 | | - w.setMetadata(meta); |
170 | | - } |
171 | 59 | } |
0 commit comments