use of javax.imageio.IIOException in project jdk8u_jdk by JetBrains.
the class ValidWbmpTest method main.
public static void main(String[] args) {
try {
String[] formats = { "JPEG", "PNG", "BMP" };
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = img.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100);
g.setColor(Color.black);
g.fillRect(10, 10, 80, 80);
ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("WBMP").next();
if (ir == null) {
throw new RuntimeException("No readers for WBMP format!");
}
for (int i = 0; i < formats.length; i++) {
System.out.println("Test " + formats[i] + " stream...");
boolean passed = false;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, formats[i], baos);
baos.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ImageInputStream iis = null;
iis = ImageIO.createImageInputStream(bais);
ir.setInput(iis);
try {
BufferedImage res = ir.read(0);
} catch (IIOException e) {
StackTraceElement[] stack = e.getStackTrace();
if (ir.getClass().getName().equals(stack[0].getClassName()) && "readHeader".equals(stack[0].getMethodName())) {
passed = true;
}
} catch (Throwable t) {
t.printStackTrace();
}
if (!passed) {
throw new RuntimeException("Test failed!");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Unexpected exception. Test failed.");
}
}
use of javax.imageio.IIOException in project frostwire by frostwire.
the class MP4Parser method getArtworkFromMP4.
static BufferedImage getArtworkFromMP4(File file) {
BufferedImage image = null;
try {
RandomAccessFile iso = new RandomAccessFile(file, "r");
LinkedList<Box> boxes = IsoFile.head(iso, ByteBuffer.allocate(100 * 1024));
try {
AppleCoverBox data = Box.findFirst(boxes, Box.covr);
if (data != null) {
byte[] imageData = data.value();
if (data.dataType() == 13) {
// jpg
image = imageFromData(imageData);
} else if (data.dataType() == 14) {
// png
try {
image = ImageIO.read(new ByteArrayInputStream(imageData, 0, imageData.length));
} catch (IIOException e) {
LOG.warn("Unable to decode png image from data tag");
}
}
}
} finally {
IOUtils.closeQuietly(iso);
}
} catch (Throwable e) {
// LOG.error("Unable to read cover art from mp4 file: " + file);
}
return image;
}
use of javax.imageio.IIOException in project jdk8u_jdk by JetBrains.
the class BMPImageReader method read.
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
if (iis == null) {
throw new IllegalStateException(I18N.getString("BMPImageReader5"));
}
checkIndex(imageIndex);
clearAbortRequest();
processImageStarted(imageIndex);
if (param == null)
param = getDefaultReadParam();
//read header
try {
readHeader();
} catch (IllegalArgumentException e) {
throw new IIOException(I18N.getString("BMPImageReader6"), e);
}
sourceRegion = new Rectangle(0, 0, 0, 0);
destinationRegion = new Rectangle(0, 0, 0, 0);
computeRegions(param, this.width, this.height, param.getDestination(), sourceRegion, destinationRegion);
scaleX = param.getSourceXSubsampling();
scaleY = param.getSourceYSubsampling();
// If the destination band is set used it
sourceBands = param.getSourceBands();
destBands = param.getDestinationBands();
seleBand = (sourceBands != null) && (destBands != null);
noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) || seleBand;
if (!seleBand) {
sourceBands = new int[numBands];
destBands = new int[numBands];
for (int i = 0; i < numBands; i++) destBands[i] = sourceBands[i] = i;
}
// If the destination is provided, then use it. Otherwise, create new one
bi = param.getDestination();
// Get the image data.
WritableRaster raster = null;
if (bi == null) {
if (sampleModel != null && colorModel != null) {
sampleModel = sampleModel.createCompatibleSampleModel(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height);
if (seleBand)
sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
raster = Raster.createWritableRaster(sampleModel, new Point());
bi = new BufferedImage(colorModel, raster, false, null);
}
} else {
raster = bi.getWritableTile(0, 0);
sampleModel = bi.getSampleModel();
colorModel = bi.getColorModel();
noTransform &= destinationRegion.equals(raster.getBounds());
}
// buffer for byte data
byte[] bdata = null;
// buffer for short data
short[] sdata = null;
// buffer for int data
int[] idata = null;
// the sampleModel can be null in case of embedded image
if (sampleModel != null) {
if (sampleModel.getDataType() == DataBuffer.TYPE_BYTE)
bdata = (byte[]) ((DataBufferByte) raster.getDataBuffer()).getData();
else if (sampleModel.getDataType() == DataBuffer.TYPE_USHORT)
sdata = (short[]) ((DataBufferUShort) raster.getDataBuffer()).getData();
else if (sampleModel.getDataType() == DataBuffer.TYPE_INT)
idata = (int[]) ((DataBufferInt) raster.getDataBuffer()).getData();
}
// There should only be one tile.
switch(imageType) {
case VERSION_2_1_BIT:
// no compression
read1Bit(bdata);
break;
case VERSION_2_4_BIT:
// no compression
read4Bit(bdata);
break;
case VERSION_2_8_BIT:
// no compression
read8Bit(bdata);
break;
case VERSION_2_24_BIT:
// no compression
read24Bit(bdata);
break;
case VERSION_3_1_BIT:
// 1-bit images cannot be compressed.
read1Bit(bdata);
break;
case VERSION_3_4_BIT:
switch((int) compression) {
case BI_RGB:
read4Bit(bdata);
break;
case BI_RLE4:
readRLE4(bdata);
break;
default:
throw new IIOException(I18N.getString("BMPImageReader1"));
}
break;
case VERSION_3_8_BIT:
switch((int) compression) {
case BI_RGB:
read8Bit(bdata);
break;
case BI_RLE8:
readRLE8(bdata);
break;
default:
throw new IIOException(I18N.getString("BMPImageReader1"));
}
break;
case VERSION_3_24_BIT:
// 24-bit images are not compressed
read24Bit(bdata);
break;
case VERSION_3_NT_16_BIT:
read16Bit(sdata);
break;
case VERSION_3_NT_32_BIT:
read32Bit(idata);
break;
case VERSION_3_XP_EMBEDDED:
case VERSION_4_XP_EMBEDDED:
case VERSION_5_XP_EMBEDDED:
bi = readEmbedded((int) compression, bi, param);
break;
case VERSION_4_1_BIT:
read1Bit(bdata);
break;
case VERSION_4_4_BIT:
switch((int) compression) {
case BI_RGB:
read4Bit(bdata);
break;
case BI_RLE4:
readRLE4(bdata);
break;
default:
throw new IIOException(I18N.getString("BMPImageReader1"));
}
case VERSION_4_8_BIT:
switch((int) compression) {
case BI_RGB:
read8Bit(bdata);
break;
case BI_RLE8:
readRLE8(bdata);
break;
default:
throw new IIOException(I18N.getString("BMPImageReader1"));
}
break;
case VERSION_4_16_BIT:
read16Bit(sdata);
break;
case VERSION_4_24_BIT:
read24Bit(bdata);
break;
case VERSION_4_32_BIT:
read32Bit(idata);
break;
}
if (abortRequested())
processReadAborted();
else
processImageComplete();
return bi;
}
use of javax.imageio.IIOException in project jdk8u_jdk by JetBrains.
the class JPEGImageWriter method collectHTablesFromMetadata.
/**
* Finds all DHT marker segments and returns all the q
* tables as a single array of JPEGQTables. The metadata
* must not be for a progressive image, or an exception
* will be thrown when two Huffman tables with the same
* table id are encountered.
*/
private JPEGHuffmanTable[] collectHTablesFromMetadata(JPEGMetadata metadata, boolean wantDC) throws IIOException {
ArrayList tables = new ArrayList();
Iterator iter = metadata.markerSequence.iterator();
while (iter.hasNext()) {
MarkerSegment seg = (MarkerSegment) iter.next();
if (seg instanceof DHTMarkerSegment) {
DHTMarkerSegment dht = (DHTMarkerSegment) seg;
for (int i = 0; i < dht.tables.size(); i++) {
DHTMarkerSegment.Htable htable = (DHTMarkerSegment.Htable) dht.tables.get(i);
if (htable.tableClass == (wantDC ? 0 : 1)) {
tables.add(htable);
}
}
}
}
JPEGHuffmanTable[] retval = null;
if (tables.size() != 0) {
DHTMarkerSegment.Htable[] htables = new DHTMarkerSegment.Htable[tables.size()];
tables.toArray(htables);
retval = new JPEGHuffmanTable[tables.size()];
for (int i = 0; i < retval.length; i++) {
retval[i] = null;
for (int j = 0; j < tables.size(); j++) {
if (htables[j].tableID == i) {
if (retval[i] != null) {
throw new IIOException("Metadata has duplicate Htables!");
}
retval[i] = new JPEGHuffmanTable(htables[j].numCodes, htables[j].values);
}
}
}
}
return retval;
}
use of javax.imageio.IIOException in project jdk8u_jdk by JetBrains.
the class GIFImageReader method readHeader.
// END LZW STUFF
private void readHeader() throws IIOException {
if (gotHeader) {
return;
}
if (stream == null) {
throw new IllegalStateException("Input not set!");
}
// Create an object to store the stream metadata
this.streamMetadata = new GIFStreamMetadata();
try {
stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
byte[] signature = new byte[6];
stream.readFully(signature);
StringBuffer version = new StringBuffer(3);
version.append((char) signature[3]);
version.append((char) signature[4]);
version.append((char) signature[5]);
streamMetadata.version = version.toString();
streamMetadata.logicalScreenWidth = stream.readUnsignedShort();
streamMetadata.logicalScreenHeight = stream.readUnsignedShort();
int packedFields = stream.readUnsignedByte();
boolean globalColorTableFlag = (packedFields & 0x80) != 0;
streamMetadata.colorResolution = ((packedFields >> 4) & 0x7) + 1;
streamMetadata.sortFlag = (packedFields & 0x8) != 0;
int numGCTEntries = 1 << ((packedFields & 0x7) + 1);
streamMetadata.backgroundColorIndex = stream.readUnsignedByte();
streamMetadata.pixelAspectRatio = stream.readUnsignedByte();
if (globalColorTableFlag) {
streamMetadata.globalColorTable = new byte[3 * numGCTEntries];
stream.readFully(streamMetadata.globalColorTable);
} else {
streamMetadata.globalColorTable = null;
}
// Found position of metadata for image 0
imageStartPosition.add(Long.valueOf(stream.getStreamPosition()));
} catch (IOException e) {
throw new IIOException("I/O error reading header!", e);
}
gotHeader = true;
}
Aggregations