/** * Returns the raw color model of the reader at the specified image index * * @param reader * @param imageIndex * @return */ private ColorModel getRawColorModel(ImageReader reader, int imageIndex) { try { ImageTypeSpecifier imageType = reader.getRawImageType(imageIndex); if (imageType == null) { return null; } ColorModel cm = imageType.getColorModel(); return cm; } catch (Exception e) { LOGGER.log(Level.FINE, "Failed to determine the native color model of the reader", e); } return null; }
param.setDestinationType(reader.getRawImageType(0));
@Override @Test public void testGetTypeSpecifiers() throws IOException { final ImageReader reader = createReader(); for (TestData data : getTestData()) { reader.setInput(data.getInputStream()); ImageTypeSpecifier rawType = reader.getRawImageType(0); // As the JPEGImageReader we delegate to returns null for YCbCr, we'll have to do the same if (rawType == null && data.getInput().toString().contains("jpeg")) { continue; } assertNotNull(rawType); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); assertNotNull(types); assertTrue(types.hasNext()); // TODO: This might fail even though the specifiers are obviously equal, if the // color spaces they use are not the SAME instance, as ColorSpace uses identity equals // and Interleaved ImageTypeSpecifiers are only equal if color spaces are equal... boolean rawFound = false; while (types.hasNext()) { ImageTypeSpecifier type = types.next(); if (type.equals(rawType)) { rawFound = true; break; } } assertTrue("ImageTypeSepcifier from getRawImageType should be in the iterator from getImageTypes", rawFound); } }
@Test public void testGetTypeSpecifiers() throws IOException { final ImageReader reader = createReader(); for (TestData data : getTestData()) { reader.setInput(data.getInputStream()); ImageTypeSpecifier rawType = reader.getRawImageType(0); if (rawType == null && allowsNullRawImageType()) { continue; } assertNotNull(rawType); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); assertNotNull(types); assertTrue(types.hasNext()); // TODO: This might fail even though the specifiers are obviously equal, if the // color spaces they use are not the SAME instance, as ColorSpace uses identity equals // and Interleaved ImageTypeSpecifiers are only equal if color spaces are equal... boolean rawFound = false; while (types.hasNext()) { ImageTypeSpecifier type = types.next(); if (type.equals(rawType)) { rawFound = true; break; } } assertTrue("ImageTypeSepcifier from getRawImageType should be in the iterator from getImageTypes", rawFound); } reader.dispose(); }
@Test public void testSetDestinationRaw() throws IOException { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); ImageTypeSpecifier type = reader.getRawImageType(0); if (type != null) { BufferedImage destination = type.createBufferedImage(reader.getWidth(0), reader.getHeight(0)); param.setDestination(destination); BufferedImage result = null; try { result = reader.read(0, param); } catch (Exception e) { failBecause("Image could not be read", e); } assertSame(destination, result); } else { System.err.println("WARNING: Test skipped due to reader.getRawImageType(0) returning null"); } reader.dispose(); }
@Override public ImageTypeSpecifier getRawImageType(int imageIndex) throws IOException { checkBounds(imageIndex); initHeader(imageIndex); // If delegate can determine the spec, we'll just go with that try { ImageTypeSpecifier rawType = delegate.getRawImageType(0); if (rawType != null) { return rawType; } } catch (IIOException | NullPointerException | ArrayIndexOutOfBoundsException | NegativeArraySizeException ignore) { // Fall through } // Otherwise, consult the image metadata JPEGColorSpace csType = getSourceCSType(getJFIF(), getAdobeDCT(), getSOF()); switch (csType) { case CMYK: // Create based on embedded profile if exists, or create from "Generic CMYK" ICC_Profile profile = getEmbeddedICCProfile(false); if (profile != null && profile.getNumComponents() == 4) { return ImageTypeSpecifiers.createInterleaved(ColorSpaces.createColorSpace(profile), new int[]{3, 2, 1, 0}, DataBuffer.TYPE_BYTE, false, false); } return ImageTypeSpecifiers.createInterleaved(ColorSpaces.getColorSpace(ColorSpaces.CS_GENERIC_CMYK), new int[] {3, 2, 1, 0}, DataBuffer.TYPE_BYTE, false, false); default: // For other types, we probably can't give a proper type, return null return null; } }
return initReaderDelegate(header.getCompression()).getRawImageType(0);
public ArrayList<BufferedImage> getFrames(File gif) throws IOException{ ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>(); ImageReader ir = new GIFImageReader(new GIFImageReaderSpi()); ir.setInput(ImageIO.createImageInputStream(gif)); for(int i = 0; i < ir.getNumImages(true); i++) frames.add(ir.getRawImageType(i).createBufferedImage(ir.getWidth(i), ir.getHeight(i))); return frames; }
ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(imgData); // assuming imgData is byte[] as in your question Iterator<ImageReader> readers = ImageIO.getImageReaders(stream); if (!readers.hasNext()) { // We don't know about this format, give up } ImageReader reader = readers.next(); reader.setInput(stream); // Now query for the properties you like // Dimensions: int width = reader.getWidth(0); int height = reader.getHeight(0); // File format (you can typically use the first element in the array): String[] formats = reader.getOriginatingProvider().getFormatNames(); // Color model (note that this will return null for most JPEGs, as Java has no YCbCr color model, but at least this should get you going): ImageTypeSpecifier type = reader.getRawImageType(0); ColorModel cm = type.getColorModel(); // ...etc...
param.setDestinationType(reader.getRawImageType(0));
ImageTypeSpecifier type = reader.getRawImageType(imageNo); if (type != null) { param.setDestination(type.createBufferedImage(width, height));
private synchronized void createReader(final ImageReader iioReader) throws IOException { reader = iioReader; initReader(); numImages = reader.getNumImages(!reader.isSeekForwardOnly()); if(numImages < 0) numImages = 1; numBands = 3; dataType = ProductData.TYPE_INT32; final ImageTypeSpecifier its = reader.getRawImageType(0); if (its != null) { numBands = reader.getRawImageType(0).getNumBands(); dataType = bufferImageTypeToProductType(its.getBufferedImageType()); if (its.getBufferedImageType() == BufferedImage.TYPE_BYTE_INDEXED) { isIndexed = true; createIndexedImageInfo(its.getColorModel()); } } }
@Override public ImageTypeSpecifier getRawImageType(int frameIndex) throws IOException { readMetadata(); checkIndex(frameIndex); if (decompressor == null) return createImageType(bitsStored, dataType, banded); if (rle) return createImageType(bitsStored, dataType, true); openiis(); try { decompressor.setInput(iisOfFrame(0)); return decompressor.getRawImageType(0); } finally { closeiis(); } }
public void testGetTypeSpecifiers() throws IOException { final ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageTypeSpecifier rawType = reader.getRawImageType(0); assertNotNull(rawType); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); assertNotNull(types); assertTrue(types.hasNext()); // TODO: This might fail even though the specifiers are obviously equal, if the // color spaces they use are not the SAME instance, as ColorSpace uses identity equals // and Interleaved ImageTypeSpecifiers are only equal if color spaces are equal... boolean rawFound = false; while (types.hasNext()) { ImageTypeSpecifier type = types.next(); if (type.equals(rawType)) { rawFound = true; break; } } assertTrue("ImageTypeSepcifier from getRawImageType should be in the iterator from getImageTypes", rawFound); }
public void testSetDestinationRaw() throws IOException { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); ImageTypeSpecifier type = reader.getRawImageType(0); BufferedImage destination = type.createBufferedImage(reader.getWidth(0), reader.getHeight(0)); param.setDestination(destination); BufferedImage result = reader.read(0, param); assertSame(destination, result); }
@Test public void testGetTypeSpecifiers() throws IOException { final ImageReader reader = createReader(); for (TestData data : getTestData()) { reader.setInput(data.getInputStream()); ImageTypeSpecifier rawType = reader.getRawImageType(0); if (rawType == null && allowsNullRawImageType()) { continue; } assertNotNull(rawType); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); assertNotNull(types); assertTrue(types.hasNext()); // TODO: This might fail even though the specifiers are obviously equal, if the // color spaces they use are not the SAME instance, as ColorSpace uses identity equals // and Interleaved ImageTypeSpecifiers are only equal if color spaces are equal... boolean rawFound = false; while (types.hasNext()) { ImageTypeSpecifier type = types.next(); if (type.equals(rawType)) { rawFound = true; break; } } assertTrue("ImageTypeSepcifier from getRawImageType should be in the iterator from getImageTypes", rawFound); } }
@Test public void testSetDestinationRaw() throws IOException { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); ImageTypeSpecifier type = reader.getRawImageType(0); if (type != null) { BufferedImage destination = type.createBufferedImage(reader.getWidth(0), reader.getHeight(0)); param.setDestination(destination); BufferedImage result = null; try { result = reader.read(0, param); } catch (Exception e) { failBecause("Image could not be read", e); } assertSame(destination, result); } else { System.err.println("WARNING: Test skipped due to reader.getRawImageType(0) returning null"); } }
@Override public ImageTypeSpecifier getRawImageType(int imageIndex) throws IOException { checkBounds(imageIndex); initHeader(imageIndex); // If delegate can determine the spec, we'll just go with that try { ImageTypeSpecifier rawType = delegate.getRawImageType(0); if (rawType != null) { return rawType; } } catch (IIOException | NullPointerException | ArrayIndexOutOfBoundsException | NegativeArraySizeException ignore) { // Fall through } // Otherwise, consult the image metadata JPEGColorSpace csType = getSourceCSType(getJFIF(), getAdobeDCT(), getSOF()); switch (csType) { case CMYK: // Create based on embedded profile if exists, or create from "Generic CMYK" ICC_Profile profile = getEmbeddedICCProfile(false); if (profile != null && profile.getNumComponents() == 4) { return ImageTypeSpecifiers.createInterleaved(ColorSpaces.createColorSpace(profile), new int[]{3, 2, 1, 0}, DataBuffer.TYPE_BYTE, false, false); } return ImageTypeSpecifiers.createInterleaved(ColorSpaces.getColorSpace(ColorSpaces.CS_GENERIC_CMYK), new int[] {3, 2, 1, 0}, DataBuffer.TYPE_BYTE, false, false); default: // For other types, we probably can't give a proper type, return null return null; } }
return initReaderDelegate(header.getCompression()).getRawImageType(0);
: reader.getRawImageType(imageIndex);