use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.
the class BooleanAttributes method test.
public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception {
BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
iw.setOutput(ios);
ImageWriteParam param = null;
IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
Source src = new StreamSource(new StringReader(metaXml));
DOMResult dst = new DOMResult();
transform(src, dst);
Document doc = (Document) dst.getNode();
Element node = doc.getDocumentElement();
String metaFormat = node.getNodeName();
// Verify that the default metadata gets formatted correctly.
verify(meta.getAsTree(metaFormat), boolXpaths, false);
meta.mergeTree(metaFormat, node);
// Verify that the merged metadata gets formatte correctly.
verify(meta.getAsTree(metaFormat), boolXpaths, true);
iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
iw.dispose();
ios.close();
ImageReader ir = ImageIO.getImageReader(iw);
byte[] bytes = os.toByteArray();
if (bytes.length == 0)
throw new AssertionError("Zero length image file");
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ImageInputStream iis = new MemoryCacheImageInputStream(is);
ir.setInput(iis);
if (useStreamMeta)
meta = ir.getStreamMetadata();
else
meta = ir.getImageMetadata(0);
// Verify again after writing and re-reading the image
verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.
the class ImageReaderGetDestination method main.
public static void main(String[] argv) {
Vector imageTypes = new Vector();
boolean gotIAE1 = false;
boolean gotIAE2 = false;
boolean gotIAE3 = false;
boolean gotIAE4 = false;
try {
DummyImageReaderImpl.getDestination(null, null, 5, 10);
} catch (IllegalArgumentException iae) {
gotIAE1 = true;
} catch (Throwable ee) {
System.out.println("Unexpected exception 1:");
ee.printStackTrace();
}
if (!gotIAE1) {
throw new RuntimeException("Failed to get IAE #1!");
}
try {
DummyImageReaderImpl.getDestination(null, imageTypes.iterator(), 5, 10);
} catch (IllegalArgumentException iae) {
gotIAE2 = true;
} catch (Throwable ee) {
System.out.println("Unexpected exception 2:");
ee.printStackTrace();
}
if (!gotIAE2) {
throw new RuntimeException("Failed to get IAE #2!");
}
imageTypes.add("abc");
try {
DummyImageReaderImpl.getDestination(null, imageTypes.iterator(), 5, 10);
} catch (IllegalArgumentException iae) {
gotIAE3 = true;
} catch (Throwable ee) {
System.out.println("Unexpected exception 3:");
ee.printStackTrace();
}
if (!gotIAE3) {
throw new RuntimeException("Failed to get IAE #3!");
}
imageTypes.clear();
ImageTypeSpecifier its = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
imageTypes.add(its);
try {
DummyImageReaderImpl.getDestination(null, imageTypes.iterator(), Integer.MAX_VALUE, Integer.MAX_VALUE);
} catch (IllegalArgumentException iae) {
gotIAE4 = true;
} catch (Throwable ee) {
System.out.println("Unexpected exception 4: ");
ee.printStackTrace();
}
if (!gotIAE4) {
throw new RuntimeException("Failed to get IAE #4!");
}
}
use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.
the class ImageTypeSpecifierBitsPerBand method main.
public static void main(String[] args) {
int biType = BufferedImage.TYPE_USHORT_565_RGB;
ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(biType);
int b0 = type.getBitsPerBand(0);
int b1 = type.getBitsPerBand(1);
int b2 = type.getBitsPerBand(2);
if (b0 != 5 || b1 != 6 || b2 != 5) {
throw new RuntimeException("Got incorrect bits per band value!");
}
boolean gotIAE = false;
try {
int b3 = type.getBitsPerBand(3);
} catch (IllegalArgumentException e) {
gotIAE = true;
}
if (!gotIAE) {
throw new RuntimeException("Failed to get IllegalArgumentException for band == 3!");
}
}
use of javax.imageio.ImageTypeSpecifier 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.ImageTypeSpecifier in project android by JetBrains.
the class WebpConvertedFile method convert.
public boolean convert(@NotNull BufferedImage image, @NotNull WebpConversionSettings settings) {
try {
// See if we find an alpha channel in this image and if so, return null
if (settings.skipTransparentImages) {
String name = sourceFile.getName();
if (name.endsWith(DOT_PNG) || name.endsWith(DOT_GIF)) {
if (ImageUtils.isNonOpaque(image)) {
return false;
}
}
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream((int) sourceFileSize);
ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(image);
if (!WebpImageWriterSpi.canWriteImage(type)) {
return false;
}
WebpImageWriterSpi.writeImage(image, byteArrayOutputStream, settings.lossless, settings.quality);
encoded = byteArrayOutputStream.toByteArray();
saved = sourceFileSize - encoded.length;
return true;
} catch (IOException e) {
Logger.getInstance(WebpConvertedFile.class).error("Can't convert " + sourceFile.getPath(), e);
return false;
}
}
Aggregations