use of javax.imageio.IIOException in project UniversalMediaServer by UniversalMediaServer.
the class ImagesUtil method parseImage.
/**
* Parses an image file and stores the results in the given
* {@link DLNAMediaInfo}. Parsing is performed using both
* <a href=https://github.com/drewnoakes/metadata-extractor>Metadata Extractor</a>
* and {@link ImageIO}. While Metadata Extractor offers more detailed
* information, {@link ImageIO} offers information that is convenient for
* image transformation with {@link ImageIO}. Parsing will be performed if
* just one of the two methods produces results, but some details will be
* missing if either one failed.
* <p><b>
* This method consumes and closes {@code inputStream}.
* </b>
* @param file the {@link File} to parse.
* @param media the {@link DLNAMediaInfo} instance to store the parsing
* results to.
* @throws IOException if an IO error occurs or no information can be parsed.
*/
public static void parseImage(File file, DLNAMediaInfo media) throws IOException {
// 1 MB
final int MAX_BUFFER = 1048576;
if (file == null) {
throw new IllegalArgumentException("parseImage: file cannot be null");
}
if (media == null) {
throw new IllegalArgumentException("parseImage: media cannot be null");
}
boolean trace = LOGGER.isTraceEnabled();
if (trace) {
LOGGER.trace("Parsing image file \"{}\"", file.getAbsolutePath());
}
long size = file.length();
ResettableInputStream inputStream = new ResettableInputStream(Files.newInputStream(file.toPath()), MAX_BUFFER);
try {
Metadata metadata = null;
FileType fileType = null;
try {
fileType = FileTypeDetector.detectFileType(inputStream);
metadata = getMetadata(inputStream, fileType);
} catch (IOException e) {
metadata = new Metadata();
LOGGER.debug("Error reading \"{}\": {}", file.getAbsolutePath(), e.getMessage());
LOGGER.trace("", e);
} catch (ImageProcessingException e) {
metadata = new Metadata();
LOGGER.debug("Error parsing {} metadata for \"{}\": {}", fileType.toString().toUpperCase(Locale.ROOT), file.getAbsolutePath(), e.getMessage());
LOGGER.trace("", e);
}
ImageFormat format = ImageFormat.toImageFormat(fileType);
if (format == null || format == ImageFormat.TIFF) {
ImageFormat tmpformat = ImageFormat.toImageFormat(metadata);
if (tmpformat != null) {
format = tmpformat;
}
}
if (inputStream.isFullResetAvailable()) {
inputStream.fullReset();
} else {
// If we can't reset it, close it and create a new
inputStream.close();
inputStream = new ResettableInputStream(Files.newInputStream(file.toPath()), MAX_BUFFER);
}
ImageInfo imageInfo = null;
try {
imageInfo = ImageIOTools.readImageInfo(inputStream, size, metadata, false);
} catch (UnknownFormatException | IIOException | ParseException e) {
if (format == null) {
throw new UnknownFormatException("Unable to recognize image format for \"" + file.getAbsolutePath() + "\" - parsing failed", e);
}
LOGGER.debug("Unable to parse \"{}\" with ImageIO because the format is unsupported, image information will be limited", file.getAbsolutePath());
LOGGER.trace("ImageIO parse failure reason: {}", e.getMessage());
// Gather basic information from the data we have
if (metadata != null) {
try {
imageInfo = ImageInfo.create(metadata, format, size, true, true);
} catch (ParseException pe) {
LOGGER.debug("Unable to parse metadata for \"{}\": {}", file.getAbsolutePath(), pe.getMessage());
LOGGER.trace("", pe);
}
}
}
if (imageInfo == null && format == null) {
throw new ParseException("Parsing of \"" + file.getAbsolutePath() + "\" failed");
}
if (format == null) {
format = imageInfo.getFormat();
} else if (imageInfo != null && imageInfo.getFormat() != null && format != imageInfo.getFormat()) {
if (imageInfo.getFormat() == ImageFormat.TIFF && format.isRaw()) {
if (format == ImageFormat.ARW && !isARW(metadata)) {
// XXX Remove this if https://github.com/drewnoakes/metadata-extractor/issues/217 is fixed
// Metadata extractor misidentifies some Photoshop created TIFFs for ARW, correct it
format = ImageFormat.toImageFormat(metadata);
if (format == null) {
format = ImageFormat.TIFF;
}
LOGGER.trace("Correcting misidentified image format ARW to {} for \"{}\"", format, file.getAbsolutePath());
} else {
/*
* ImageIO recognizes many RAW formats as TIFF because
* of their close relationship let's treat them as what
* they really are.
*/
imageInfo = ImageInfo.create(imageInfo.getWidth(), imageInfo.getHeight(), format, size, imageInfo.getBitDepth(), imageInfo.getNumComponents(), imageInfo.getColorSpace(), imageInfo.getColorSpaceType(), metadata, false, imageInfo.isImageIOSupported());
LOGGER.trace("Correcting misidentified image format TIFF to {} for \"{}\"", format.toString(), file.getAbsolutePath());
}
} else {
LOGGER.debug("Image parsing for \"{}\" was inconclusive, metadata parsing " + "detected {} format while ImageIO detected {}. Choosing {}.", file.getAbsolutePath(), format, imageInfo.getFormat(), imageInfo.getFormat());
format = imageInfo.getFormat();
}
}
media.setImageInfo(imageInfo);
if (format != null) {
media.setCodecV(format.toFormatConfiguration());
media.setContainer(format.toFormatConfiguration());
}
if (trace) {
LOGGER.trace("Parsing of image \"{}\" completed", file.getName());
}
} finally {
inputStream.close();
}
}
use of javax.imageio.IIOException in project imageio-ext by geosolutions-it.
the class TIFFFaxDecompressor method decodeWhiteCodeWord.
// Returns run length
private int decodeWhiteCodeWord() throws IIOException {
int current, entry, bits, isT, twoBits, code = -1;
int runLength = 0;
boolean isWhite = true;
while (isWhite) {
current = nextNBits(10);
entry = white[current];
// Get the 3 fields from the entry
isT = entry & 0x0001;
bits = (entry >>> 1) & 0x0f;
if (bits == 12) {
// Additional Make up code
// Get the next 2 bits
twoBits = nextLesserThan8Bits(2);
// Consolidate the 2 new bits and last 2 bits into 4 bits
current = ((current << 2) & 0x000c) | twoBits;
entry = additionalMakeup[current];
// 3 bits 0000 0111
bits = (entry >>> 1) & 0x07;
// 12 bits
code = (entry >>> 4) & 0x0fff;
runLength += code;
updatePointer(4 - bits);
} else if (bits == 0) {
// ERROR
throw new IIOException("Error 0");
} else if (bits == 15) {
// EOL
throw new IIOException("Error 1");
} else {
// 11 bits - 0000 0111 1111 1111 = 0x07ff
code = (entry >>> 5) & 0x07ff;
runLength += code;
updatePointer(10 - bits);
if (isT == 0) {
isWhite = false;
}
}
}
return runLength;
}
use of javax.imageio.IIOException in project imageio-ext by geosolutions-it.
the class TIFFFaxDecompressor method decodeRaw.
public void decodeRaw(byte[] b, int dstOffset, // will always be 1
int pixelBitStride, int scanlineStride) throws IOException {
this.buffer = b;
this.w = srcWidth;
this.h = srcHeight;
this.bitsPerScanline = scanlineStride * 8;
this.lineBitNum = 8 * dstOffset;
this.data = new byte[(int) byteCount];
this.bitPointer = 0;
this.bytePointer = 0;
this.prevChangingElems = new int[w + 1];
this.currChangingElems = new int[w + 1];
stream.seek(offset);
stream.readFully(data);
try {
if (compression == BaselineTIFFTagSet.COMPRESSION_CCITT_RLE) {
decodeRLE();
} else if (compression == BaselineTIFFTagSet.COMPRESSION_CCITT_T_4) {
decodeT4();
} else if (compression == BaselineTIFFTagSet.COMPRESSION_CCITT_T_6) {
this.uncompressedMode = (int) ((t6Options & 0x02) >> 1);
decodeT6();
} else {
throw new IIOException("Unknown compression type " + compression);
}
} catch (ArrayIndexOutOfBoundsException e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(baos));
String s = new String(baos.toByteArray());
warning("Ignoring exception:\n " + s);
}
}
use of javax.imageio.IIOException in project imageio-ext by geosolutions-it.
the class TIFFFaxDecompressor method nextNBits.
private int nextNBits(int bitsToGet) throws IIOException {
byte b, next, next2next;
int l = data.length - 1;
int bp = this.bytePointer;
if (fillOrder == 1) {
b = data[bp];
if (bp == l) {
next = 0x00;
next2next = 0x00;
} else if ((bp + 1) == l) {
next = data[bp + 1];
next2next = 0x00;
} else {
next = data[bp + 1];
next2next = data[bp + 2];
}
} else if (fillOrder == 2) {
b = flipTable[data[bp] & 0xff];
if (bp == l) {
next = 0x00;
next2next = 0x00;
} else if ((bp + 1) == l) {
next = flipTable[data[bp + 1] & 0xff];
next2next = 0x00;
} else {
next = flipTable[data[bp + 1] & 0xff];
next2next = flipTable[data[bp + 2] & 0xff];
}
} else {
throw new IIOException("Invalid FillOrder");
}
int bitsLeft = 8 - bitPointer;
int bitsFromNextByte = bitsToGet - bitsLeft;
int bitsFromNext2NextByte = 0;
if (bitsFromNextByte > 8) {
bitsFromNext2NextByte = bitsFromNextByte - 8;
bitsFromNextByte = 8;
}
bytePointer++;
int i1 = (b & table1[bitsLeft]) << (bitsToGet - bitsLeft);
int i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
int i3 = 0;
if (bitsFromNext2NextByte != 0) {
i2 <<= bitsFromNext2NextByte;
i3 = (next2next & table2[bitsFromNext2NextByte]) >>> (8 - bitsFromNext2NextByte);
i2 |= i3;
bytePointer++;
bitPointer = bitsFromNext2NextByte;
} else {
if (bitsFromNextByte == 8) {
bitPointer = 0;
bytePointer++;
} else {
bitPointer = bitsFromNextByte;
}
}
int i = i1 | i2;
return i;
}
use of javax.imageio.IIOException in project imageio-ext by geosolutions-it.
the class TIFFFaxDecompressor method nextLesserThan8Bits.
private int nextLesserThan8Bits(int bitsToGet) throws IIOException {
byte b, next;
int l = data.length - 1;
int bp = this.bytePointer;
if (fillOrder == 1) {
b = data[bp];
if (bp == l) {
next = 0x00;
} else {
next = data[bp + 1];
}
} else if (fillOrder == 2) {
b = flipTable[data[bp] & 0xff];
if (bp == l) {
next = 0x00;
} else {
next = flipTable[data[bp + 1] & 0xff];
}
} else {
throw new IIOException("Invalid FillOrder");
}
int bitsLeft = 8 - bitPointer;
int bitsFromNextByte = bitsToGet - bitsLeft;
int shift = bitsLeft - bitsToGet;
int i1, i2;
if (shift >= 0) {
i1 = (b & table1[bitsLeft]) >>> shift;
bitPointer += bitsToGet;
if (bitPointer == 8) {
bitPointer = 0;
bytePointer++;
}
} else {
i1 = (b & table1[bitsLeft]) << (-shift);
i2 = (next & table2[bitsFromNextByte]) >>> (8 - bitsFromNextByte);
i1 |= i2;
bytePointer++;
bitPointer = bitsFromNextByte;
}
return i1;
}
Aggregations