use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.
the class JpegWriterLeakTest method main.
public static void main(String[] args) {
final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>();
final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>();
int count = 2;
do {
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
final WeakReference<? extends ImageWriter> ref = new WeakReference<>(writer, queue);
refs.add(ref);
try {
final ImageOutputStream os = ImageIO.createImageOutputStream(new ByteArrayOutputStream());
writer.setOutput(os);
writer.write(getImage());
// NB: dispose() or reset() workarounds the problem.
} catch (IOException e) {
} finally {
writer = null;
}
count--;
} while (count > 0);
System.out.println("Wait for GC...");
final long testTimeOut = 60000L;
final long startTime = System.currentTimeMillis();
while (!refs.isEmpty()) {
// check for the test timeout
final long now = System.currentTimeMillis();
if (now - startTime > testTimeOut) {
System.out.println();
throw new RuntimeException("Test FAILED.");
}
System.gc();
try {
System.out.print(".");
Thread.sleep(1000);
} catch (InterruptedException e) {
}
;
Reference<? extends ImageWriter> r = queue.poll();
if (r != null) {
System.out.println("Got reference: " + r);
refs.remove(r);
}
}
System.out.println("Test PASSED.");
}
use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.
the class MergeTreeTest method main.
public static void main(String[] args) throws IOException {
ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
ImageTypeSpecifier type = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
iw.setOutput(ios);
IIOMetadata meta = iw.getDefaultImageMetadata(type, null);
boolean isFailed = false;
String[] fmts = meta.getMetadataFormatNames();
for (int i = 0; i < fmts.length; i++) {
System.out.print("Format: " + fmts[i] + " ... ");
Node root = meta.getAsTree(fmts[i]);
try {
meta.mergeTree(fmts[i], root);
} catch (NullPointerException e) {
throw new RuntimeException("Test failed for format " + fmts[i], e);
}
System.out.println("PASSED");
}
}
use of javax.imageio.stream.ImageOutputStream 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.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.
the class RleEncodingTest method doTest.
private static void doTest(BufferedImage src, String compressionType, int compressionMode) throws IOException {
ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("bmp").next();
if (iw == null) {
throw new RuntimeException("No available writer. Test failed.");
}
IIOImage iioImg = new IIOImage(src, null, null);
ImageWriteParam param = iw.getDefaultWriteParam();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
iw.setOutput(ios);
System.out.println("Compression Type is " + compressionType);
System.out.println("Compression Mode is " + compressionMode);
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType(compressionType);
if (compressionMode != ImageWriteParam.MODE_EXPLICIT) {
param.setCompressionMode(compressionMode);
}
try {
iw.write(null, iioImg, param);
} catch (IOException e) {
int bpp = src.getColorModel().getPixelSize();
if (compressionMode == ImageWriteParam.MODE_EXPLICIT) {
if ((compressionType.equals("BI_RLE4") && bpp != 4) || (compressionType.equals("BI_RLE8") && bpp != 8)) {
System.out.println("Can not encode " + bpp + "bpp image as" + compressionType);
return;
} else {
throw new RuntimeException("Unable to encode " + bpp + "bpp image as " + compressionType + ". Test failed");
}
}
}
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = ImageIO.createImageInputStream(bais);
BufferedImage dst = ImageIO.read(iis);
int w = src.getWidth();
int h = src.getHeight();
Object dstPixel = dst.getRaster().getDataElements(w / 2, h / 2, null);
Object srcPixel = src.getRaster().getDataElements(w / 2, h / 2, null);
if ((src.getColorModel().getRed(srcPixel) != dst.getColorModel().getRed(dstPixel)) || (src.getColorModel().getGreen(srcPixel) != dst.getColorModel().getGreen(dstPixel)) || (src.getColorModel().getBlue(srcPixel) != dst.getColorModel().getBlue(dstPixel)) || (src.getColorModel().getAlpha(srcPixel) != dst.getColorModel().getAlpha(dstPixel))) {
showPixel(src, w / 2, h / 2);
showPixel(dst, w / 2, h / 2);
throw new RuntimeException("Colors are different: " + Integer.toHexString(src.getColorModel().getRGB(srcPixel)) + " and " + Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));
}
}
use of javax.imageio.stream.ImageOutputStream in project jdk8u_jdk by JetBrains.
the class RLECompressionTest method doTest.
public void doTest(int type) throws IOException {
BufferedImage src = getTestImage(type);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
writer.setOutput(ios);
ImageWriteParam wparam = writer.getDefaultWriteParam();
wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
switch(type) {
case TEST_RLE8:
wparam.setCompressionType("BI_RLE8");
break;
case TEST_RLE4:
wparam.setCompressionType("BI_RLE4");
break;
default:
throw new IllegalArgumentException("Wrong test type: " + type);
}
writer.write(null, new IIOImage(src, null, null), wparam);
ios.close();
baos.close();
// read result
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BufferedImage dst = ImageIO.read(bais);
checkResult(src, dst);
}
Aggregations