Search in sources :

Example 1 with PNGImageContent

use of org.knime.core.data.image.png.PNGImageContent in project knime-core by knime.

the class HistogramColumn method createPngImageCell.

/**
 * @param histogramData A {@link HistogramModel}.
 * @return The PNG image cell.
 */
private DataCell createPngImageCell(final HistogramModel<?> histogramData, final boolean paintLabels) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        try {
            BufferedImage image = new BufferedImage(m_width, m_height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = image.createGraphics();
            paint(histogramData, paintLabels, g);
            ImageIO.write(image, "png", baos);
        } finally {
            baos.close();
        }
    } catch (IOException e) {
        // Should not happen, all in-memory
        throw new IllegalStateException(e.getMessage(), e);
    }
    return new PNGImageContent(baos.toByteArray()).toImageCell();
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D)

Example 2 with PNGImageContent

use of org.knime.core.data.image.png.PNGImageContent in project knime-core by knime.

the class ReadPNGFromURLNodeModel method toPNGCell.

/**
 * Read image from URL.
 * @param urlValue The URL
 * @return A new image cell
 * @throws IOException
 * @throws IllegalArgumentException If the image is invalid
 * @see PNGImageContent#PNGImageContent(InputStream)
 */
private DataCell toPNGCell(final String urlValue) throws IOException {
    URL url = new URL(urlValue);
    InputStream in = FileUtil.openStreamWithTimeout(url);
    try {
        PNGImageContent pngImageContent = new PNGImageContent(in);
        return pngImageContent.toImageCell();
    } finally {
        try {
            in.close();
        } catch (IOException ioe) {
        // ignore
        }
    }
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with PNGImageContent

use of org.knime.core.data.image.png.PNGImageContent in project knime-core by knime.

the class ImagePortObject method collectImageContentFactories.

private static Map<String, ImageContentFactory> collectImageContentFactories() {
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint point = registry.getExtensionPoint(ImageContentFactory.EXT_POINT_ID);
    if (point == null) {
        LOGGER.error("Invalid extension point: " + ImageContentFactory.EXT_POINT_ID);
        return Collections.emptyMap();
    }
    Map<String, ImageContentFactory> resultList = new HashMap<>();
    for (IConfigurationElement elem : point.getConfigurationElements()) {
        String imageContentCLName = elem.getAttribute(ImageContentFactory.EXT_POINT_ATTR_CLASS_NAME);
        String decl = elem.getDeclaringExtension().getUniqueIdentifier();
        if (imageContentCLName == null || imageContentCLName.isEmpty()) {
            LOGGER.error("The extension '" + decl + "' doesn't provide the required attribute '" + ImageContentFactory.EXT_POINT_ATTR_CLASS_NAME + "' - ignoring it");
            continue;
        }
        ImageContentFactory instance = null;
        try {
            instance = (ImageContentFactory) elem.createExecutableExtension(ImageContentFactory.EXT_POINT_ATTR_CLASS_NAME);
        } catch (Throwable t) {
            LOGGER.error("Problems during initialization of image content factory (with id '" + imageContentCLName + "'.)", t);
            if (decl != null) {
                LOGGER.error("Extension " + decl + " ignored.");
            }
        }
        if (instance != null) {
            // We do not want to add invalid image content impls to this list.
            resultList.put(instance.getImageContentClass().getName(), instance);
        }
    }
    // add the image content implementations from core
    resultList.put(PNGImageContent.class.getName(), new ImageContentFactory() {

        @Override
        public Class<? extends ImageContent> getImageContentClass() {
            return PNGImageContent.class;
        }

        @Override
        public ImageContent create(final InputStream in) throws IOException {
            return new PNGImageContent(in);
        }
    });
    return Collections.unmodifiableMap(resultList);
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) HashMap(java.util.HashMap) PortObjectZipInputStream(org.knime.core.node.port.PortObjectZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) PNGImageContent(org.knime.core.data.image.png.PNGImageContent) ImageContent(org.knime.core.data.image.ImageContent) ImageContentFactory(org.knime.core.data.image.ImageContentFactory) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry)

Example 4 with PNGImageContent

use of org.knime.core.data.image.png.PNGImageContent in project knime-core by knime.

the class WritePNGNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    String outPath = m_fileOutSettings.getStringValue();
    ImagePortObject imageObj = (ImagePortObject) inObjects[0];
    DataCell imageCellDC = imageObj.toDataCell();
    if (!(imageCellDC instanceof ImageValue)) {
        throw new InvalidSettingsException("Image object does not produce" + " valid image object but " + imageCellDC == null ? null : imageCellDC.getClass().getName());
    }
    // overwrite check done in configure()
    ImageValue v = (ImageValue) imageCellDC;
    ImageContent content = v.getImageContent();
    if (content instanceof PNGImageContent) {
        byte[] bytes = ((PNGImageContent) content).getByteArrayReference();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        FileOutputStream out = new FileOutputStream(new File(outPath));
        FileUtil.copy(in, out);
        in.close();
        out.close();
    } else {
        throw new InvalidSettingsException("Unsupported image type: " + content.getClass().getName() + " (expected PNG)");
    }
    return new PortObject[0];
}
Also used : PNGImageContent(org.knime.core.data.image.png.PNGImageContent) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PNGImageContent(org.knime.core.data.image.png.PNGImageContent) ImageContent(org.knime.core.data.image.ImageContent) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) PNGImageValue(org.knime.core.data.image.png.PNGImageValue) ImageValue(org.knime.core.data.image.ImageValue) File(java.io.File) PortObject(org.knime.core.node.port.PortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject)

Example 5 with PNGImageContent

use of org.knime.core.data.image.png.PNGImageContent in project knime-core by knime.

the class DecTreeToImageNodeModel method loadInternals.

/**
 * Load internals.
 *
 * @param nodeInternDir The intern node directory to load tree from.
 * @param exec Used to report progress or cancel saving.
 * @throws IOException Always, since this method has not been implemented
 *             yet.
 * @see org.knime.core.node.NodeModel
 *      #loadInternals(java.io.File,ExecutionMonitor)
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
    // read the decision tree
    File internalsFile = new File(nodeInternDir, DEC_TREE_FILE_NAME);
    if (!internalsFile.exists()) {
        // file to load internals from not available
        setWarningMessage("Internal model could not be loaded.");
        return;
    }
    BufferedInputStream in2 = new BufferedInputStream(new GZIPInputStream(new FileInputStream(internalsFile)));
    ModelContentRO binModel = ModelContent.loadFromXML(in2);
    try {
        m_decTree = new DecisionTree(binModel);
    } catch (InvalidSettingsException ise) {
        LOGGER.warn("Model (internals) could not be loaded.", ise);
        setWarningMessage("Internal model could not be loaded.");
    }
    exec.setProgress(0.5);
    // read image content
    File f = new File(nodeInternDir, IMAGE_FILE_NAME);
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    try {
        m_imageContent = new PNGImageContent(in);
    } catch (Exception e) {
        in.close();
        LOGGER.warn("Model (internals) could not be loaded.", e);
        setWarningMessage("Internal model could not be loaded.");
    }
    in.close();
    exec.setProgress(1.0);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) PNGImageContent(org.knime.core.data.image.png.PNGImageContent) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) File(java.io.File) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

PNGImageContent (org.knime.core.data.image.png.PNGImageContent)6 IOException (java.io.IOException)4 Graphics2D (java.awt.Graphics2D)2 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ImageContent (org.knime.core.data.image.ImageContent)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 PortObject (org.knime.core.node.port.PortObject)2 ImagePortObject (org.knime.core.node.port.image.ImagePortObject)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)1 GraphicsEnvironment (java.awt.GraphicsEnvironment)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1