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 jdk8u_jdk by JetBrains.
the class BMPWriteParamTest method getWriteResult.
private static BufferedImage getWriteResult(BufferedImage img, String format) throws IOException {
ImageWriter iw = null;
Iterator writers = ImageIO.getImageWritersByFormatName(format);
while (writers.hasNext()) {
iw = (ImageWriter) writers.next();
System.out.println(format + " -> " + iw.toString());
}
if (iw == null) {
throw new RuntimeException("No available Image writer for " + format);
}
ImageWriteParam param = iw.getDefaultWriteParam();
param.setSourceRegion(new Rectangle(10, 10, 31, 31));
param.setSourceSubsampling(3, 3, 0, 0);
IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
IIOImage iio_img = new IIOImage(img, null, meta);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
iw.setOutput(ios);
iw.write(meta, iio_img, param);
ios.flush();
byte[] ba_image = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
ImageReader ir = null;
Iterator readers = ImageIO.getImageReadersByFormatName(format);
while (readers.hasNext()) {
ir = (ImageReader) readers.next();
System.out.println(format + " -> " + ir.toString());
}
if (ir == null) {
throw new RuntimeException("No available Image reader for " + format);
}
ir.setInput(ImageIO.createImageInputStream(bais));
BufferedImage res = ir.read(0);
return res;
}
use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.
the class LogicalScreenDimensionTest method main.
public static void main(String[] args) throws IOException {
String format = "GIF";
ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
if (writer == null) {
throw new RuntimeException("No available writers for " + format);
}
BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
ImageWriteParam p = writer.getDefaultWriteParam();
ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(img);
IIOMetadata inImageMetadata = writer.getDefaultImageMetadata(type, p);
IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);
// write and read image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);
ios.flush();
ios.close();
// read result
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
ImageReader reader = ImageIO.getImageReader(writer);
reader.setInput(iis);
IIOMetadata outStreamMetadata = reader.getStreamMetadata();
GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata) outStreamMetadata;
if (gifStreamMetadata.logicalScreenWidth != img.getWidth() || gifStreamMetadata.logicalScreenHeight != img.getHeight()) {
throw new RuntimeException("Test failed due to wrong logical screen dimension.");
}
}
Aggregations