use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class CoreCommonImageMetadata method createCommonNativeTree.
/**
* Returns the XML DOM <code>Node</code> object that represents the root
* of a tree of metadata contained within this object on its native format.
*
* @return a root node containing common metadata exposed on its native
* format.
*/
protected Node createCommonNativeTree() {
// Create root node
final IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName);
// ////////////////////////////////////////////////////////////////////
//
// DatasetDescriptor
//
// ////////////////////////////////////////////////////////////////////
IIOMetadataNode node = new IIOMetadataNode("DatasetDescriptor");
node.setAttribute("name", datasetName);
node.setAttribute("description", datasetDescription);
node.setAttribute("driverName", driverName);
node.setAttribute("driverDescription", driverDescription);
node.setAttribute("projection", projection);
node.setAttribute("numGCPs", Integer.toString(gcpNumber));
node.setAttribute("gcpProjection", gcpProjection);
root.appendChild(node);
// ////////////////////////////////////////////////////////////////////
//
// RasterDimensions
//
// ////////////////////////////////////////////////////////////////////
node = new IIOMetadataNode("RasterDimensions");
node.setAttribute("width", Integer.toString(width));
node.setAttribute("height", Integer.toString(height));
node.setAttribute("tileWidth", Integer.toString(tileWidth));
node.setAttribute("tileHeight", Integer.toString(tileHeight));
node.setAttribute("numBands", Integer.toString(numBands));
root.appendChild(node);
// ////////////////////////////////////////////////////////////////////
//
// GeoTransform
//
// ////////////////////////////////////////////////////////////////////
node = new IIOMetadataNode("GeoTransform");
final boolean hasgeoTransform = geoTransformation != null && geoTransformation.length > 0;
node.setAttribute("m0", hasgeoTransform ? Double.toString(geoTransformation[0]) : null);
node.setAttribute("m1", hasgeoTransform ? Double.toString(geoTransformation[1]) : null);
node.setAttribute("m2", hasgeoTransform ? Double.toString(geoTransformation[2]) : null);
node.setAttribute("m3", hasgeoTransform ? Double.toString(geoTransformation[3]) : null);
node.setAttribute("m4", hasgeoTransform ? Double.toString(geoTransformation[4]) : null);
node.setAttribute("m5", hasgeoTransform ? Double.toString(geoTransformation[5]) : null);
root.appendChild(node);
// ////////////////////////////////////////////////////////////////////
if (gcpNumber > 0) {
IIOMetadataNode nodeGCPs = new IIOMetadataNode("GCPS");
final List<? extends GCP> gcps = getGCPs();
if (gcps != null && !gcps.isEmpty()) {
final Iterator<? extends GCP> it = gcps.iterator();
while (it.hasNext()) {
node = new IIOMetadataNode("GCP");
final GCP gcp = it.next();
node.setAttribute("column", Double.toString(gcp.getColumn()));
node.setAttribute("row", Double.toString(gcp.getRow()));
node.setAttribute("id", gcp.getId());
node.setAttribute("info", gcp.getDescription());
node.setAttribute("easting", Double.toString(gcp.getEasting()));
node.setAttribute("northing", Double.toString(gcp.getNorthing()));
node.setAttribute("elevation", Double.toString(gcp.getElevation()));
nodeGCPs.appendChild(node);
}
}
root.appendChild(nodeGCPs);
}
// ////////////////////////////////////////////////////////////////////
//
// BandsInfo
//
// ////////////////////////////////////////////////////////////////////
IIOMetadataNode bandsNode = new IIOMetadataNode("BandsInfo");
// //
for (int i = 0; i < numBands; i++) {
node = new IIOMetadataNode("BandInfo");
node.setAttribute("index", Integer.toString(i));
node.setAttribute("colorInterpretation", colorInterpretations != null && colorInterpretations.length > i ? Integer.toBinaryString(colorInterpretations[i]) : "");
node.setAttribute("noData", noDataValues != null && noDataValues.length > i && noDataValues[i] != null ? noDataValues[i].toString() : null);
node.setAttribute("maximum", maximums != null && maximums.length > i && maximums[i] != null ? maximums[i].toString() : null);
node.setAttribute("minimum", minimums != null && minimums.length > i && minimums[i] != null ? minimums[i].toString() : null);
node.setAttribute("scale", scales != null && scales.length > i && scales[i] != null ? scales[i].toString() : null);
node.setAttribute("offset", offsets != null && offsets.length > i && offsets[i] != null ? offsets[i].toString() : null);
node.setAttribute("numOverviews", numOverviews != null && numOverviews.length > i ? Integer.toString(numOverviews[i]) : null);
bandsNode.appendChild(node);
}
// ////////////////////////////////////////////////////////////////////
if (colorModel instanceof IndexColorModel) {
final IndexColorModel icm = (IndexColorModel) colorModel;
final int mapSize = icm.getMapSize();
final boolean hasAlpha = icm.hasAlpha();
IIOMetadataNode node1 = new IIOMetadataNode("ColorTable");
node1.setAttribute("sizeOfLocalColorTable", Integer.toString(mapSize));
final byte[][] rgb = new byte[3 + (hasAlpha ? 1 : 0)][mapSize];
icm.getReds(rgb[0]);
icm.getGreens(rgb[1]);
icm.getBlues(rgb[2]);
if (hasAlpha) {
icm.getAlphas(rgb[3]);
}
for (int i = 0; i < mapSize; i++) {
IIOMetadataNode nodeEntry = new IIOMetadataNode("ColorTableEntry");
nodeEntry.setAttribute("index", Integer.toString(i));
nodeEntry.setAttribute("red", Byte.toString(rgb[0][i]));
nodeEntry.setAttribute("green", Byte.toString(rgb[1][i]));
nodeEntry.setAttribute("blue", Byte.toString(rgb[2][i]));
if (hasAlpha) {
nodeEntry.setAttribute("alpha", Byte.toString(rgb[3][i]));
}
node1.appendChild(nodeEntry);
}
node.appendChild(node1);
}
root.appendChild(bandsNode);
return root;
}
use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class CoreCommonIIOStreamMetadata method getAsTree.
/**
* Returns an XML DOM <code>Node</code> object that represents the root of
* a tree of common stream metadata contained within this object according
* to the conventions defined by a given metadata format name.
*
* @param formatName
* the name of the requested metadata format. Note that
* actually, the only supported format name is the
* {@link CoreCommonIIOStreamMetadata#nativeMetadataFormatName}.
* Requesting other format names will result in an
* <code>IllegalArgumentException</code>
*/
public Node getAsTree(String formatName) {
if (!nativeMetadataFormatName.equalsIgnoreCase(formatName))
throw new IllegalArgumentException(formatName + " is not a recognized format name for gdal stream metadata.");
final IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName);
final IIOMetadataNode dataSetsNode = new IIOMetadataNode("DataSets");
root.appendChild(dataSetsNode);
// we need to take into account that when subdatasets are supported we
// have to remove 1 from the number of datasets we declare
int length = datasetNames.length;
if (length > 1)
length--;
dataSetsNode.setAttribute("number", Integer.toString(length));
for (int i = 0; i < length; i++) {
final IIOMetadataNode dataSetNode = new IIOMetadataNode("DataSet");
dataSetNode.setAttribute("name", datasetNames[i]);
dataSetsNode.appendChild(dataSetNode);
}
return root;
}
use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class AsciiGridsImageMetadata method getStandardCompressionNode.
protected IIOMetadataNode getStandardCompressionNode() {
IIOMetadataNode node = new IIOMetadataNode("Compression");
// CompressionTypeName
IIOMetadataNode subNode = new IIOMetadataNode("Lossless");
subNode.setAttribute("value", "TRUE");
node.appendChild(subNode);
return node;
}
use of javax.imageio.metadata.IIOMetadataNode in project ChatGameFontificator by GlitchCog.
the class AnimatedGifUtil method loadDittoAnimatedGif.
/**
* Fix (ditto) for Java's animated GIF interpretation
*
* Adapted from http://stackoverflow.com/questions/26801433/fix-frame-rate-of-animated-gif-in-java#answer-26829534
*
* @param url
* The URL for the animated GIF to be loaded
* @param dim
* The dimension object to be filled by the width and height of the loaded animated GIF
* @return The loaded animated GIF
* @throws Exception
*/
public static Image loadDittoAnimatedGif(final URL url, Dimension dim) {
final Image dimImage = new ImageIcon(url).getImage();
Image image = null;
try {
ImageReader gifReader = ImageIO.getImageReadersByFormatName(GIF_EXTENSION).next();
InputStream imageStream = url.openStream();
gifReader.setInput(ImageIO.createImageInputStream(imageStream));
IIOMetadata imageMetaData = gifReader.getImageMetadata(0);
String metaFormatName = imageMetaData.getNativeMetadataFormatName();
final int frameCount = gifReader.getNumImages(true);
ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream);
ImageWriter writer = ImageIO.getImageWriter(gifReader);
writer.setOutput(ios);
writer.prepareWriteSequence(null);
final int imgWidth = dimImage.getWidth(null);
final int imgHeight = dimImage.getHeight(null);
dim.setSize(imgWidth, imgHeight);
for (int i = 0; i < frameCount; i++) {
// This read method takes into account the frame's top and left coordinates
BufferedImage frame = gifReader.read(i);
IIOMetadataNode nodes = (IIOMetadataNode) gifReader.getImageMetadata(i).getAsTree(metaFormatName);
for (int j = 0; j < nodes.getLength(); j++) {
IIOMetadataNode node = (IIOMetadataNode) nodes.item(j);
if (GRAPHIC_CTRL_EXT.equalsIgnoreCase(node.getNodeName())) {
int delay = Integer.parseInt(node.getAttribute(ATTRIBUTE_DELAY_TIME));
// Minimum delay for browsers, which delay animated GIFs much more than would be to spec
if (delay < MIN_ANI_GIF_DELAY) {
node.setAttribute(ATTRIBUTE_DELAY_TIME, Integer.toString(MIN_ANI_GIF_DELAY));
}
// at least not for BTTV's (ditto)
if (node.getAttribute(ATTRIBUTE_DISPOSAL_METHOD).equals(DISPOSE_RESTORE_TO_PREVIOUS)) {
node.setAttribute(ATTRIBUTE_DISPOSAL_METHOD, DISPOSE_RESTORE_TO_BGCOLOR);
}
}
}
IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frame), null);
metadata.setFromTree(metadata.getNativeMetadataFormatName(), nodes);
// This modified frame is necessary to get the correct image placement, width, and height in the final GIF
BufferedImage frameMod = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics big = frameMod.getGraphics();
big.drawImage(frame, 0, 0, null);
IIOImage fixedFrame = new IIOImage(frameMod, null, metadata);
writer.writeToSequence(fixedFrame, writer.getDefaultWriteParam());
}
writer.endWriteSequence();
if (ios != null) {
ios.close();
}
image = Toolkit.getDefaultToolkit().createImage(baoStream.toByteArray());
} catch (Exception e) {
// If anything goes wrong, just load it normally
logger.error("Error loading animated GIF (ditto) from " + url, e);
image = new ImageIcon(url).getImage();
dim.setSize(image.getWidth(null), image.getHeight(null));
}
return image;
}
use of javax.imageio.metadata.IIOMetadataNode in project ChatGameFontificator by GlitchCog.
the class ControlWindow method generateMetadataNode.
private IIOMetadataNode generateMetadataNode(String key, String value) {
IIOMetadataNode node = new IIOMetadataNode("tEXtEntry");
node.setAttribute("keyword", key);
node.setAttribute("value", value);
return node;
}
Aggregations