use of javax.imageio.ImageWriter in project jdk8u_jdk by JetBrains.
the class WriteMetadataTest method main.
public static void main(String[] args) {
ImageWriter w = ImageIO.getImageWritersByFormatName(format).next();
if (w == null) {
throw new RuntimeException("No available writers for format " + format);
}
ImageWriteParam p = w.getDefaultWriteParam();
ImageTypeSpecifier t = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata m = w.getDefaultImageMetadata(t, p);
System.out.println("Default image metadata is " + m);
testWritableMetadata(m);
IIOMetadata sm = w.getDefaultStreamMetadata(p);
System.out.println("Default stream metadata is " + sm);
testWritableMetadata(sm);
}
use of javax.imageio.ImageWriter in project jdk8u_jdk by JetBrains.
the class ITXtTest method writeTo.
private static void writeTo(File f, ITXtTest t) {
BufferedImage src = createBufferedImage();
try {
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(f);
ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(src);
ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("PNG").next();
imageWriter.setOutput(imageOutputStream);
IIOMetadata m = imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
String format = m.getNativeMetadataFormatName();
Node root = m.getAsTree(format);
IIOMetadataNode iTXt = t.getNode();
root.appendChild(iTXt);
m.setFromTree(format, root);
imageWriter.write(new IIOImage(src, null, m));
imageOutputStream.close();
System.out.println("Writing done.");
} catch (Throwable e) {
throw new RuntimeException("Writing test failed.", e);
}
}
use of javax.imageio.ImageWriter in project jdk8u_jdk by JetBrains.
the class ItxtUtf8Test method runTest.
public static void runTest(boolean dump, boolean truncate) throws Exception {
String format = "javax_imageio_png_1.0";
BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
iw.setOutput(ios);
IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
DOMImplementationRegistry registry;
registry = DOMImplementationRegistry.newInstance();
DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
Document doc = impl.createDocument(null, format, null);
Element root, itxt, entry;
root = doc.getDocumentElement();
root.appendChild(itxt = doc.createElement("iTXt"));
itxt.appendChild(entry = doc.createElement("iTXtEntry"));
entry.setAttribute("keyword", "verbatim");
entry.setAttribute("compressionFlag", "false");
entry.setAttribute("compressionMethod", "0");
entry.setAttribute("languageTag", "x-circled");
entry.setAttribute("translatedKeyword", VERBATIM);
entry.setAttribute("text", TEXT);
itxt.appendChild(entry = doc.createElement("iTXtEntry"));
entry.setAttribute("keyword", "compressed");
entry.setAttribute("compressionFlag", "true");
entry.setAttribute("compressionMethod", "0");
entry.setAttribute("languageTag", "x-circled");
entry.setAttribute("translatedKeyword", COMPRESSED);
entry.setAttribute("text", TEXT);
meta.mergeTree(format, root);
iw.write(new IIOImage(img, null, meta));
iw.dispose();
byte[] bytes = os.toByteArray();
if (dump)
System.out.write(bytes);
if (findBytes(VBYTES, bytes) < 0)
throw new AssertionError("verbatim block not found");
if (findBytes(CBYTES, bytes) < 0)
throw new AssertionError("compressed block not found");
int length = bytes.length;
if (truncate)
length = findBytes(VBYTES, bytes) + 32;
ImageReader ir = ImageIO.getImageReader(iw);
ByteArrayInputStream is = new ByteArrayInputStream(bytes, 0, length);
ImageInputStream iis = new MemoryCacheImageInputStream(is);
ir.setInput(iis);
meta = ir.getImageMetadata(0);
Node node = meta.getAsTree(format);
for (node = node.getFirstChild(); !"iTXt".equals(node.getNodeName()); node = node.getNextSibling()) ;
boolean verbatimSeen = false, compressedSeen = false;
for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) {
entry = (Element) node;
String keyword = entry.getAttribute("keyword");
String translatedKeyword = entry.getAttribute("translatedKeyword");
String text = entry.getAttribute("text");
if ("verbatim".equals(keyword)) {
if (verbatimSeen)
throw new AssertionError("Duplicate");
verbatimSeen = true;
if (!VERBATIM.equals(translatedKeyword))
throw new AssertionError("Wrong translated keyword");
if (!TEXT.equals(text))
throw new AssertionError("Wrong text");
} else if ("compressed".equals(keyword)) {
if (compressedSeen)
throw new AssertionError("Duplicate");
compressedSeen = true;
if (!COMPRESSED.equals(translatedKeyword))
throw new AssertionError("Wrong translated keyword");
if (!TEXT.equals(text))
throw new AssertionError("Wrong text");
} else {
throw new AssertionError("Unexpected keyword");
}
}
if (!(verbatimSeen && compressedSeen))
throw new AssertionError("Missing chunk");
}
use of javax.imageio.ImageWriter in project jdk8u_jdk by JetBrains.
the class CrashAfterDispose method main.
public static void main(String[] args) throws IOException {
InputStream bais = new ByteArrayInputStream(new byte[100]);
ImageInputStream iis = ImageIO.createImageInputStream(bais);
// find the JPEG reader
ImageReader reader = null;
Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
if (readers.hasNext()) {
reader = (ImageReader) readers.next();
} else {
throw new RuntimeException("Unable to find a reader!");
}
// dispose the reader, then poke and prod it... the reader should
// throw exceptions (which will be caught by this test), but it
// should not crash the VM
reader.dispose();
try {
reader.setInput(iis);
} catch (IllegalStateException e) {
}
try {
reader.read(0);
} catch (IllegalStateException e) {
}
try {
reader.abort();
} catch (IllegalStateException e) {
}
try {
reader.reset();
} catch (IllegalStateException e) {
}
try {
reader.dispose();
} catch (IllegalStateException e) {
}
// find the JPEG writer
ImageWriter writer = null;
Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
if (writers.hasNext()) {
writer = (ImageWriter) writers.next();
} else {
throw new RuntimeException("Unable to find a writer!");
}
// set up output stream
OutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
// dispose the writer, then poke and prod it... the writer should
// throw exceptions (which will be caught by this test), but it
// should not crash the VM
writer.dispose();
try {
writer.setOutput(ios);
} catch (IllegalStateException e) {
}
try {
writer.write(bi);
} catch (IllegalStateException e) {
}
try {
writer.abort();
} catch (IllegalStateException e) {
}
try {
writer.reset();
} catch (IllegalStateException e) {
}
try {
writer.dispose();
} catch (IllegalStateException e) {
}
}
use of javax.imageio.ImageWriter in project jdk8u_jdk by JetBrains.
the class JPEGsNotAcceleratedTest method writeTestImage.
public static void writeTestImage(String fileName) {
BufferedImage bi = new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(new Color(testRGB));
g.fillRect(0, 0, TEST_W, TEST_H);
try {
System.err.printf("Writing %s\n", fileName);
if (lowCompression) {
ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("jpeg").next();
if (iw == null) {
throw new RuntimeException("No available image writer for " + "jpeg " + " Test failed.");
}
File file = new File(fileName);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
ImageWriteParam param = iw.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1);
IIOImage iioImg = new IIOImage(bi, null, null);
iw.write(null, iioImg, param);
} else {
ImageIO.write(bi, "jpeg", new File(fileName));
}
} catch (IOException e) {
System.err.println("Error " + e + " when writing file: " + fileName);
throw new RuntimeException(e);
}
}
Aggregations