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