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();
}
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
}
}
}
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);
}
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];
}
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);
}
Aggregations