use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class ReadAsGrayTest method doTest.
private static void doTest(int type) throws IOException {
BufferedImage src = createTestImage(type);
File f = new File("test.jpg");
if (!ImageIO.write(src, "jpg", f)) {
throw new RuntimeException("Failed to write test image.");
}
ImageInputStream iis = ImageIO.createImageInputStream(f);
ImageReader reader = ImageIO.getImageReaders(iis).next();
reader.setInput(iis);
Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
ImageTypeSpecifier srgb = null;
ImageTypeSpecifier gray = null;
// look for gray and srgb types
while ((srgb == null || gray == null) && types.hasNext()) {
ImageTypeSpecifier t = types.next();
if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) {
gray = t;
}
if (t.getColorModel().getColorSpace() == sRGB) {
srgb = t;
}
}
if (gray == null) {
throw new RuntimeException("No gray type available.");
}
if (srgb == null) {
throw new RuntimeException("No srgb type available.");
}
System.out.println("Read as GRAY...");
testType(reader, gray, src);
System.out.println("Read as sRGB...");
testType(reader, srgb, src);
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class WBMPImageReaderSpi method canDecodeInput.
public boolean canDecodeInput(Object source) throws IOException {
if (!(source instanceof ImageInputStream)) {
return false;
}
ImageInputStream stream = (ImageInputStream) source;
stream.mark();
try {
// TypeField
int type = stream.readByte();
int fixHeaderField = stream.readByte();
// check WBMP "header"
if (type != 0 || fixHeaderField != 0) {
// while WBMP reader does not support ext WBMP headers
return false;
}
int width = ReaderUtil.readMultiByteInteger(stream);
int height = ReaderUtil.readMultiByteInteger(stream);
// check image dimension
if (width <= 0 || height <= 0) {
return false;
}
long dataLength = stream.length();
if (dataLength == -1) {
// let's reject images with dimension above the limit.
return (width < MAX_WBMP_WIDTH) && (height < MAX_WBMP_HEIGHT);
}
dataLength -= stream.getStreamPosition();
long scanSize = (width / 8) + ((width % 8) == 0 ? 0 : 1);
return (dataLength == scanSize * height);
} finally {
stream.reset();
}
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class ImageStreamFromRAF method main.
public static void main(String[] args) {
try {
File f = new File("ImageInputStreamFromRAF.tmp");
RandomAccessFile raf = new RandomAccessFile(f, "rw");
ImageInputStream istream = ImageIO.createImageInputStream(raf);
ImageOutputStream ostream = ImageIO.createImageOutputStream(raf);
f.delete();
if (istream == null) {
throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!");
}
if (ostream == null) {
throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!");
}
if (!(istream instanceof FileImageInputStream)) {
throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!");
}
if (!(ostream instanceof FileImageOutputStream)) {
throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!");
}
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class ConcurrentReadingTest method run.
public void run() {
try {
ImageInputStream iis = ImageIO.createImageInputStream(file);
r.setInput(iis);
ImageReadParam p = r.getDefaultReadParam();
Thread.sleep(70);
BufferedImage res = r.read(0, p);
Thread.sleep(70);
r.reset();
} catch (IllegalStateException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
} catch (Throwable e) {
// Unexpected exception. Test failed.
throw new RuntimeException("Test failed.", e);
} finally {
synchronized (lock) {
completeCount++;
}
}
}
use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.
the class ConcurrentReadingTest method main.
public static void main(String[] args) throws Exception {
createTestFile();
ImageInputStream iis = ImageIO.createImageInputStream(file);
r = ImageIO.getImageReaders(iis).next();
iis.close();
for (int i = 0; i < MAX_THREADS; i++) {
(new ConcurrentReadingTest()).start();
}
// wait for started threads
boolean needWait = true;
while (needWait) {
Thread.sleep(100);
synchronized (lock) {
needWait = completeCount < MAX_THREADS;
}
}
System.out.println("Test PASSED.");
}
Aggregations