use of net.pms.image.ImageIORuntimeException in project UniversalMediaServer by UniversalMediaServer.
the class ImageIOTools method readImageInfo.
/**
* Tries to gather the data needed to populate a {@link ImageInfo} instance
* describing the input image.
*
* <p>
* This method does not close {@code inputStream}.
*
* @param inputStream the image whose information to gather.
* @param size the size of the image in bytes or
* {@link ImageInfo#SIZE_UNKNOWN} if it can't be determined.
* @param metadata the {@link Metadata} instance to embed in the resulting
* {@link ImageInfo} instance.
* @param applyExifOrientation whether or not Exif orientation should be
* compensated for when setting width and height. This will also
* reset the Exif orientation information. <b>Changes will be
* applied to the {@code metadata} argument instance</b>.
* @return An {@link ImageInfo} instance describing the input image.
* @throws UnknownFormatException if the format could not be determined.
* @throws IOException if an IO error occurred.
*/
public static ImageInfo readImageInfo(InputStream inputStream, long size, Metadata metadata, boolean applyExifOrientation) throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("input == null!");
}
try (ImageInputStream stream = createImageInputStream(inputStream)) {
Iterator<?> iter = ImageIO.getImageReaders(stream);
if (!iter.hasNext()) {
throw new UnknownFormatException("Unable to find a suitable image reader");
}
ImageReader reader = (ImageReader) iter.next();
try {
int width = -1;
int height = -1;
ImageFormat format = ImageFormat.toImageFormat(reader.getFormatName());
if (format == null) {
throw new UnknownFormatException("Unable to determine image format");
}
ColorModel colorModel = null;
try {
reader.setInput(stream, true, true);
Iterator<ImageTypeSpecifier> iterator = reader.getImageTypes(0);
if (iterator.hasNext()) {
colorModel = iterator.next().getColorModel();
}
width = reader.getWidth(0);
height = reader.getHeight(0);
} catch (RuntimeException e) {
throw new ImageIORuntimeException("Error reading image information: " + e.getMessage(), e);
}
boolean imageIOSupport;
if (format == ImageFormat.TIFF) {
// but fails when it actually tries, so we have to test it.
try {
ImageReadParam param = reader.getDefaultReadParam();
param.setSourceRegion(new Rectangle(1, 1));
reader.read(0, param);
imageIOSupport = true;
} catch (Exception e) {
// Catch anything here, we simply want to test if it fails.
imageIOSupport = false;
}
} else {
imageIOSupport = true;
}
ImageInfo imageInfo = ImageInfo.create(width, height, format, size, colorModel, metadata, applyExifOrientation, imageIOSupport);
return imageInfo;
} finally {
reader.dispose();
}
}
}
use of net.pms.image.ImageIORuntimeException in project UniversalMediaServer by UniversalMediaServer.
the class ImageIOTools method read.
/**
* A copy of {@link ImageIO#read(ImageInputStream)} that returns
* {@link ImageReaderResult} instead of {@link BufferedImage}. This lets
* information about the detected format be retained.
*
* <b>
* This method consumes and closes {@code stream}.
* </b>
*
* @param stream an {@link ImageInputStream} to read from.
*
* @see ImageIO#read(ImageInputStream)
*/
public static ImageReaderResult read(ImageInputStream stream) throws IOException {
if (stream == null) {
throw new IllegalArgumentException("stream == null!");
}
try {
Iterator<?> iter = ImageIO.getImageReaders(stream);
if (!iter.hasNext()) {
throw new UnknownFormatException("Unable to find a suitable image reader");
}
ImageFormat inputFormat = null;
BufferedImage bufferedImage = null;
ImageReader reader = (ImageReader) iter.next();
try {
// Store the parsing result
inputFormat = ImageFormat.toImageFormat(reader.getFormatName());
reader.setInput(stream, true, true);
bufferedImage = reader.read(0, reader.getDefaultReadParam());
} finally {
reader.dispose();
}
return bufferedImage != null ? new ImageReaderResult(bufferedImage, inputFormat) : null;
} catch (RuntimeException e) {
throw new ImageIORuntimeException("An error occurred while trying to read image: " + e.getMessage(), e);
} finally {
stream.close();
}
}
use of net.pms.image.ImageIORuntimeException in project UniversalMediaServer by UniversalMediaServer.
the class ImageIOTools method detectFileFormat.
/**
* Tries to detect the input image file format using {@link ImageIO} and
* returns the result.
* <p>
* This method does not close {@code inputStream}.
*
* @param inputStream the image whose format to detect.
* @return The {@link ImageFormat} for the input.
* @throws UnknownFormatException if the format could not be determined.
* @throws IOException if an IO error occurred.
*/
public static ImageFormat detectFileFormat(InputStream inputStream) throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("input == null!");
}
try (ImageInputStream stream = createImageInputStream(inputStream)) {
Iterator<?> iter = ImageIO.getImageReaders(stream);
if (!iter.hasNext()) {
throw new UnknownFormatException("Unable to find a suitable image reader");
}
ImageReader reader = (ImageReader) iter.next();
ImageFormat format = ImageFormat.toImageFormat(reader.getFormatName());
if (format == null) {
throw new UnknownFormatException("Unable to determine image format");
}
return format;
} catch (RuntimeException e) {
throw new ImageIORuntimeException("An error occurred while trying to detect image format: " + e.getMessage(), e);
}
}
Aggregations