Package javax.imageio.stream

Examples of javax.imageio.stream.ImageInputStream


              logger.debug(fileName + " ext = " + getFileExtensionForImageReader(fileName));
              Iterator readers = ImageIO.getImageReadersBySuffix(getFileExtensionForImageReader(fileName));
              ImageReader reader = (ImageReader) readers.next();
          
               try {
                  ImageInputStream iis = ImageIO.createImageInputStream(uploadedFile.getInputStream());
                  reader.setInput(iis, true);
                  int width = reader.getWidth(0);
                  int height = reader.getHeight(0);
                  logger.debug(uploadedFile.getFileName() + ": width=" + width + ", height=" + height);
                 
View Full Code Here


    @Override
    public final DecodeResult decode(InputStream encoded, OutputStream decoded,
                                         COSDictionary parameters, int index) throws IOException
    {
        ImageReader reader = findImageReader("JPEG", "a suitable JAI I/O image filter is not installed");
        ImageInputStream iis = null;
        try
        {
            iis = ImageIO.createImageInputStream(encoded);
            reader.setInput(iis);

            // get the raster using horrible JAI workarounds
            ImageIO.setUseCache(false);
            Raster raster;
            try
            {
                // I'd like to use ImageReader#readRaster but it is buggy and can't read RGB correctly
                BufferedImage image = reader.read(0);
                raster = image.getRaster();
            }
            catch (IIOException e)
            {
                // JAI can't read CMYK JPEGs using ImageReader#read or ImageIO.read but
                // fortunately ImageReader#readRaster isn't buggy when reading 4-channel files
                raster = reader.readRaster(0, null);
            }

            // special handling for 4-component images
            if (raster.getNumBands() == 4)
            {
                // get APP14 marker
                Integer transform;
                try
                {
                    transform = getAdobeTransform(reader.getImageMetadata(0));
                }
                catch (IIOException e)
                {
                    // catches the error "Inconsistent metadata read from stream"
                    // which seems to be present indicate a YCCK image, but who knows?
                    LOG.warn("Inconsistent metadata read from JPEG stream");
                    transform = 2; // YCCK
                }
                int colorTransform = transform != null ? transform : 0;

                // 0 = Unknown (RGB or CMYK), 1 = YCbCr, 2 = YCCK
                switch (colorTransform)
                {
                    case 0: break; // already CMYK
                    case 1: LOG.warn("YCbCr JPEGs not implemented"); break; // TODO YCbCr
                    case 2: raster = fromYCCKtoCMYK(raster); break;
                }
            }
            else if (raster.getNumBands() == 3)
            {
                // BGR to RGB
                raster = fromBGRtoRGB(raster);
            }

            DataBufferByte dataBuffer = (DataBufferByte)raster.getDataBuffer();
            decoded.write(dataBuffer.getData());
        }
        finally
        {
            if (iis != null)
            {
                iis.close();
            }
            reader.dispose();
        }
        return new DecodeResult(parameters);
    }
View Full Code Here

        if (params != null)
        {
            globals = (COSStream) params.getDictionaryObject(COSName.JBIG2_GLOBALS);
        }

        ImageInputStream iis = null;
        try
        {
            if (globals != null)
            {
                iis = ImageIO.createImageInputStream(
                        new SequenceInputStream(globals.getUnfilteredStream(), encoded));
                reader.setInput(iis);
            }
            else
            {
                iis = ImageIO.createImageInputStream(encoded);
                reader.setInput(iis);
            }

            BufferedImage image;
            try
            {
                image = reader.read(0);
            }
            catch (Exception e)
            {
                // wrap and rethrow any exceptions
                throw new IOException("Could not read JBIG2 image", e);
            }

            // I am assuming since JBIG2 is always black and white
            // depending on your renderer this might or might be needed
            if (image.getColorModel().getPixelSize() != bits.intValue())
            {
                if (bits.intValue() != 1)
                {
                    LOG.warn("Attempting to handle a JBIG2 with more than 1-bit depth");
                }
                BufferedImage packedImage = new BufferedImage(image.getWidth(), image.getHeight(),
                        BufferedImage.TYPE_BYTE_BINARY);
                Graphics graphics = packedImage.getGraphics();
                graphics.drawImage(image, 0, 0, null);
                graphics.dispose();
                image = packedImage;
            }

            DataBuffer dBuf = image.getData().getDataBuffer();
            if (dBuf.getDataType() == DataBuffer.TYPE_BYTE)
            {
                decoded.write(((DataBufferByte) dBuf).getData());
            }
            else
            {
                throw new IOException("Unexpected image buffer type");
            }
        }
        finally
        {
            if (iis != null)
            {
                iis.close();
            }
            reader.dispose();
        }

        // repair missing color space
View Full Code Here

    // try to read using JAI Image I/O
    private BufferedImage readJPX(InputStream input, DecodeResult result) throws IOException
    {
        ImageReader reader = findImageReader("JPEG2000", "Java Advanced Imaging (JAI) Image I/O Tools are not installed");
        ImageInputStream iis = null;
        try
        {
            iis = ImageIO.createImageInputStream(input);
            reader.setInput(iis, true, true);

            BufferedImage image;
            try
            {
                image = reader.read(0);
            }
            catch (Exception e)
            {
                // wrap and rethrow any exceptions
                throw new IOException("Could not read JPEG 2000 (JPX) image", e);
            }

            COSDictionary parameters = result.getParameters();

            // "If the image stream uses the JPXDecode filter, this entry is optional
            // and shall be ignored if present"
            parameters.setInt(COSName.BITS_PER_COMPONENT, image.getColorModel().getComponentSize(0));

            // "Decode shall be ignored, except in the case where the image is treated as a mask"
            if (!parameters.getBoolean(COSName.IMAGE_MASK, false))
            {
                parameters.setItem(COSName.DECODE, null);
            }

            // override dimensions, see PDFBOX-1735
            parameters.setInt(COSName.WIDTH, image.getWidth());
            parameters.setInt(COSName.HEIGHT, image.getHeight());

            // extract embedded color space
            if (!parameters.containsKey(COSName.COLORSPACE))
            {
                result.setColorSpace(new PDJPXColorSpace(image.getColorModel().getColorSpace()));
            }

            return image;
        }
        finally
        {
            if (iis != null)
            {
                iis.close();
            }
            reader.dispose();
        }
    }
View Full Code Here

    public boolean canDecodeInput(Object input) throws IOException {
        if (!(input instanceof ImageInputStream))
            return false;

        ImageInputStream in = (ImageInputStream) input;
        boolean retval;

        in.mark();
        try {
            //todo implement a less expensive canDecode()
            new JPEGDecoderAdapter(in).decode();
            retval = true;
        } catch (JPEGException e) {
            retval = false;
        }
        in.reset();

        return retval;
    }
View Full Code Here

            (byte) 0x3b,
            (byte) 0x67
          };

        ByteArrayInputStream bs = new ByteArrayInputStream(b);
        ImageInputStream i = new MemoryCacheImageInputStream(bs);

        // Test ByteOrder.BIG_ENDIAN, the default.

        h.check(i.getByteOrder() == ByteOrder.BIG_ENDIAN);

        h.check(i.read() == 114);
        i.seek(0);
        h.check(i.readBoolean() == true);
        i.seek(0);
        h.check(i.readByte() == 114);
        i.seek(0);
        h.check(i.readChar() == '\u7270');
        i.seek(0);
        h.check(Double.compare(i.readDouble(), 1.709290273164385E243) == 0);
        i.seek(0);
        h.check(Float.compare(i.readFloat(), 4.7541126E30f) == 0);
        i.seek(0);
        h.check(i.readInt() == 1919944055);
        i.seek(0);
        h.check(i.readLong() == 8246096929276181351L);
        i.seek(0);
        h.check(i.readShort() == 29296);
        i.seek(0);
        h.check(i.readUnsignedByte() == 114);
        i.seek(0);
        h.check(i.readUnsignedInt() == 1919944055);
        i.seek(0);
        h.check(i.readUnsignedShort() == 29296);

        // Test ByteOrder.LITTLE_ENDIAN.
        i.setByteOrder(ByteOrder.LITTLE_ENDIAN);

        h.check(i.getByteOrder() == ByteOrder.LITTLE_ENDIAN);
        i.seek(0);
        h.check(i.read() == 114);
        i.seek(0);
        h.check(i.readBoolean() == true);
        i.seek(0);
        h.check(i.readByte() == 114);
        i.seek(0);
        h.check(i.readChar() == '\u7072');
        i.seek(0);
        h.check(Double.compare(i.readDouble(), 1.9456609400629563E189) == 0);
        i.seek(0);
        h.check(Float.compare(i.readFloat(), 2.7064693E33f) == 0);
        i.seek(0);
        h.check(i.readInt() == 1996845170);
        i.seek(0);
        h.check(i.readLong() == 7438806032077647986L);
        i.seek(0);
        h.check(i.readShort() == 28786);
        i.seek(0);
        h.check(i.readUnsignedByte() == 114);
        i.seek(0);
        h.check(i.readUnsignedInt() == 1996845170);
        i.seek(0);
        h.check(i.readUnsignedShort() == 28786);

        // Test unsigned values.
        b = new byte[]
          {
            (byte) 0x92,
            (byte) 0x80,
            (byte) 0x05,
            (byte) 0x77,
            (byte) 0xac,
            (byte) 0xf2,
            (byte) 0x8b,
            (byte) 0xa7
          };

        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);

        // Test ByteOrder.BIG_ENDIAN, the default.
        h.check(i.read() == 146);
        i.seek(0);
        h.check(i.readBoolean() == true);
        i.seek(0);
        h.check(i.readByte() == -110);
        i.seek(0);
        h.check(i.readChar() == '\u9280');
        i.seek(0);
        h.check(Double.compare(i.readDouble(), -1.4183142849706364E-219) == 0);
        i.seek(0);
        h.check(Float.compare(i.readFloat(), -8.079283E-28f) == 0);
        i.seek(0);
        h.check(i.readInt() == -1837103753);
        i.seek(0);
        h.check(i.readLong() == -7890300535592285273L);
        i.seek(0);
        h.check(i.readShort() == -28032);
        i.seek(0);
        h.check(i.readUnsignedByte() == 146);
        i.seek(0);
        h.check(i.readUnsignedInt() == 2457863543L);
        i.seek(0);
        h.check(i.readUnsignedShort() == 37504);

        // Test ByteOrder.LITTLE_ENDIAN.
        i.setByteOrder(ByteOrder.LITTLE_ENDIAN);
        i.seek(0);
        h.check(i.read() == 146);
        i.seek(0);
        h.check(i.readBoolean() == true);
        i.seek(0);
        h.check(i.readByte() == -110);
        i.seek(0);
        h.check(i.readChar() == '\u8092');
        i.seek(0);
        h.check(Double.compare(i.readDouble(), -3.463391436203922E-118) == 0);
        i.seek(0);
        h.check(Float.compare(i.readFloat(), 2.707747E33f) == 0);
        i.seek(0);
        h.check(i.readInt() == 1996849298);
        i.seek(0);
        h.check(i.readLong() == -6373734025067659118L);
        i.seek(0);
        h.check(i.readShort() == -32622);
        i.seek(0);
        h.check(i.readUnsignedByte() == 146);
        i.seek(0);
        h.check(i.readUnsignedInt() == 1996849298);
        i.seek(0);
        h.check(i.readUnsignedShort() == 32914);

        // Test flush().

        i.seek(4);

        h.check(i.getStreamPosition() == 4);

        i.flush();

        h.check(i.getFlushedPosition() == 4);
       
        boolean exceptionThrown = false;
        try
          {
            i.flushBefore(3);
          }
        catch (IndexOutOfBoundsException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        exceptionThrown = false;
        try
          {
            i.flushBefore(5);
          }
        catch (IndexOutOfBoundsException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        exceptionThrown = false;
        try
          {
            i.seek(2);
          }
        catch (IndexOutOfBoundsException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        exceptionThrown = false;
        try
          {
            i.seek(3);
          }
        catch (IndexOutOfBoundsException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        exceptionThrown = false;
        try
          {
            i.setBitOffset(-1);
          }
        catch (IllegalArgumentException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        exceptionThrown = false;
        try
          {
            i.setBitOffset(8);
          }
        catch (IllegalArgumentException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        i.setBitOffset(4);
        h.check(i.getBitOffset() == 4);

        // A MemoryCacheImageInputStream is cached in memory.
        h.check(i.isCached() == true);
        h.check(i.isCachedFile() == false);
        h.check(i.isCachedMemory() == true);

        h.check(i.length() == -1);

        // Test mark() and reset().
        i.seek(4);
        i.mark();

        i.read();
        i.read();

        i.reset();
        h.check(i.getStreamPosition() == 4);

        // Test readBit().
        // We're currently at b[4], byte 0xac.
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 0);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 0);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 0);

        // Check that the bit offset is incremented.
        h.check(i.getBitOffset() == 7);

        // Roll back the bit offset within the same byte.
        i.setBitOffset(2);

        // R-read some bits within the same byte.
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 0);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 0);

        // Check that the stream position is still 4.
        h.check(i.getStreamPosition() == 4);

        // Read the final bit in b[4].
        h.check(i.readBit() == 0);

        // Check that readBit on the 8th bit increments the stream
        // position.
        h.check(i.getStreamPosition() == 5);

        // Read the bits from b[5], byte 0xf2.
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 1);

        h.check(i.getBitOffset() == 4);

        h.check(i.readBit() == 0);
        h.check(i.readBit() == 0);
        h.check(i.readBit() == 1);
        h.check(i.readBit() == 0);

        // Check that the bit offset is reset and the position
        // incremented.
        h.check(i.getBitOffset() == 0);
        h.check(i.getStreamPosition() == 6);

        h.check(i.length() == -1);

        // Test close().
        i.close();

        // Test checkClosed().
        exceptionThrown = false;
        try
          {
            i.close();
          }
        catch (IOException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        // 64 bytes of data.
        b = new byte[]
          {
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
          };

        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);

        // Test readBits().
        i.seek(5);
        i.setBitOffset(6);

        exceptionThrown = false;
        try
          {
            i.readBits(-1);
          }
        catch (IllegalArgumentException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        exceptionThrown = false;
        try
          {
            i.readBits(65);
          }
        catch (IllegalArgumentException e)
          {
            exceptionThrown = true;
          }
        h.check(exceptionThrown);

        h.check(i.readBits(59) == 366848453836545810L);

        i.seek(5);
        i.setBitOffset(6);
        h.check(i.readBits(58) == 183424226918272905L);

        b = new byte[]
          {
            (byte) 0xa2, (byte) 0xe9, (byte) 0xd7, (byte) 0x34,
            (byte) 0x2a, (byte) 0x83, (byte) 0xe2, (byte) 0x40
          };
        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);
        h.check(i.readBits(59) == 366848453836545810L);

        b = new byte[]
          {
            (byte) 0xa2, (byte) 0x02
          };
        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);

        long[] res = new long[8];

        i.seek(0);

        h.check(i.readBits(0) == 0);
        i.seek(0);

        for (k = 0; k < 8; k++)
          {
            i.setBitOffset(k);
            res[k] = i.readBits(8);
            i.seek(0);
          }
        i.seek(0);
        h.check(Arrays.equals(res, new long[] { 162, 68, 136, 16,
                                                32, 64, 128, 1 }));

        // 64 bytes of data.
        b = new byte[]
          {
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            // Here is a two-character-long UTF String to be read by
            // DataInputStream.
            (byte) 0x00, (byte) 0x02, (byte) 0x12, (byte) 0x21,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
          };

        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);

        // Test mark() and reset().
        i.seek(17);
        h.check(i.getStreamPosition() == 17);
        i.mark();
        i.seek(49);
        h.check(i.getStreamPosition() == 49);
        i.reset();
        h.check(i.getStreamPosition() == 17);

        // Test skipBytes().
        i.setBitOffset(3);
        i.skipBytes(20);
        h.check(i.getStreamPosition() == 37);
        h.check(i.getBitOffset() == 0);

        // Test readUTF().
        i.seek(12);
        String str = i.readUTF();
        h.check(str.codePointAt(0) == 18);
        h.check(str.codePointAt(1) == 33);

        b = new byte[]
          { (byte) 0x47, (byte) 0x4e, (byte) 0x55,
            '\r',
            (byte) 0x43, (byte) 0x6c, (byte) 0x61, (byte) 0x73, (byte) 0x73,
            (byte) 0x70, (byte) 0x61, (byte) 0x74, (byte) 0x68,
            '\r', '\n',
            (byte) 0x52, (byte) 0x75, (byte) 0x6c,
            (byte) 0x65, (byte) 0x7a,
            '\n',
            (byte) 0x44, (byte) 0x75, (byte) 0x64, (byte) 0x65, (byte) 0x7a,
            (byte) 0x21,
            '\r'
          };

        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);

        h.check(i.readLine().equals("GNU"));
        h.check(i.readLine().equals("Classpath"));
        h.check(i.readLine().equals("Rulez"));
        h.check(i.readLine().equals("Dudez!"));
        h.check(i.readLine() == null);

        // 64 bytes of data.
        b = new byte[]
          {
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
            (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
            (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
            (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
            (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
            (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
          };

        bs = new ByteArrayInputStream(b);
        i = new MemoryCacheImageInputStream(bs);

        byte[] fullB = new byte[26];
        i.seek(0);
        i.readFully(fullB);
        h.check(Arrays.equals(fullB,
                              new byte[]
            {
              (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
              (byte) 0xac, (byte) 0xf2, (byte) 0x8b, (byte) 0xa7,
              (byte) 0x5c, (byte) 0xd0, (byte) 0xaa, (byte) 0x0f,
              (byte) 0x89, (byte) 0x00, (byte) 0x12, (byte) 0xf1,
              (byte) 0xa1, (byte) 0xef, (byte) 0x82, (byte) 0x00,
              (byte) 0x92, (byte) 0x80, (byte) 0x05, (byte) 0x77,
              (byte) 0xac, (byte) 0xf2
            }));

        for (k = 0; k < fullB.length; k++)
          fullB[k] = 0;

        i.seek(0);
        i.readFully(fullB, 5, 13);

        h.check(Arrays.equals(fullB, new byte[]
            {
              (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
              (byte) 0x00, (byte) 0x92, (byte) 0x80, (byte) 0x05,
              (byte) 0x77, (byte) 0xac, (byte) 0xf2, (byte) 0x8b,
              (byte) 0xa7, (byte) 0x5c, (byte) 0xd0, (byte) 0xaa,
              (byte) 0x0f, (byte) 0x89, (byte) 0x00, (byte) 0x00,
              (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
              (byte) 0x00, (byte) 0x00
            }));

        char[] fullC = new char[15];
        i.seek(0);
        i.readFully(fullC, 0, 15);

        h.check(Arrays.equals(fullC, new char[]
            {
              (char) 37504, (char) 1399, (char) 44274,
              (char) 35751, (char) 23760, (char) 43535,
              (char) 35072, (char) 4849, (char) 41455,
              (char) 33280, (char) 37504, (char) 1399,
              (char) 44274, (char) 35751, (char) 23760
            }));

        double[] fullD = new double[8];
        i.seek(0);
        i.readFully(fullD, 0, 8);

        h.check(Arrays.equals(fullD, new double[] {
                                -1.4183142849706364E-219,
                                1.2402952421911034E139,
                                -3.154063812740471E-145,
                                -3.556316535750171E-92,
                                -2.4925149951304603E-265,
                                -1.4183142849706364E-219,
                                1.2402952421911034E139,
                                -3.154063812740471E-145
                              }));

        float[] fullF = new float[16];
        i.seek(0);
        i.readFully(fullF, 0, 16);

        h.check(Arrays.equals(fullF, new float[] {
                                -8.079283E-28f,
                                -6.893558E-12f,
                                4.69870212E17f,
                                -1.5416346E-33f,
                                -1.6229681E-18f,
                                -8.079283E-28f,
                                -6.893558E-12f,
                                4.69870212E17f,
                                -1.5416346E-33f,
                                -1.6229681E-18f,
                                -8.079283E-28f,
                                -6.893558E-12f,
                                4.69870212E17f,
                                -1.5416346E-33f,
                                -1.6229681E-18f,
                                -8.079283E-28f
                              }));

        int[] fullI = new int[16];
        i.seek(0);
        i.readFully(fullI, 0, 16);

        h.check(Arrays.equals(fullI, new int[] {
                                -1837103753,
                                -1393390681,
                                1557178895,
                                -1996483855,
                                -1578139136,
                                -1837103753,
                                -1393390681,
                                1557178895,
                                -1996483855,
                                -1578139136,
                                -1837103753,
                                -1393390681,
                                1557178895,
                                -1996483855,
                                -1578139136,
                                -1837103753,
                              }));

        long[] fullL = new long[8];
        i.seek(0);
        i.readFully(fullL, 0, 8);

        h.check(Arrays.equals(fullL, new long[] {
                                -7890300535592285273L,
                                6688032430344901361L,
                                -6778055975199832713L,
                                -5984567403888989681L,
                                -8574832861500177920L,
                                -7890300535592285273L,
                                6688032430344901361L,
                                -6778055975199832713L,
                              }));

        short[] fullS = new short[32];
        i.seek(0);
        i.readFully(fullS, 0, 32);

        h.check(Arrays.equals(fullS, new short[] {
                                -28032,
                                1399,
                                -21262,
View Full Code Here

            return;
        }
        Iterator readers = ImageIO.getImageReadersBySuffix(suffix);
        assertTrue("No image reader found for suffix " + suffix, readers.hasNext());
        ImageReader reader = (ImageReader) readers.next();
        ImageInputStream iis = ImageIO.createImageInputStream(new File(filename));
        assertNotNull("No ImageInputStream created for file " + filename, iis);
        reader.setInput(iis);
        IIOMetadata imageMetadata = reader.getImageMetadata(0);
        Element root = (Element) imageMetadata.getAsTree(STANDARD_METADATA_FORMAT);
        NodeList dimensionNodes = root.getElementsByTagName("Dimension");
        assertTrue("No resolution found in image file " + filename, dimensionNodes.getLength() > 0);
        Element dimensionElement = (Element) dimensionNodes.item(0);

        NodeList pixelSizeNodes = dimensionElement.getElementsByTagName("HorizontalPixelSize");
        assertTrue("No X resolution found in image file " + filename, pixelSizeNodes.getLength() > 0);
        Node pixelSizeNode = pixelSizeNodes.item(0);
        String val = pixelSizeNode.getAttributes().getNamedItem("value").getNodeValue();
        int actualResolution = (int) Math.round(25.4 / Double.parseDouble(val));
        assertEquals("X resolution doesn't match in image file " + filename, expectedResolution, actualResolution);

        pixelSizeNodes = dimensionElement.getElementsByTagName("VerticalPixelSize");
        assertTrue("No Y resolution found in image file " + filename, pixelSizeNodes.getLength() > 0);
        pixelSizeNode = pixelSizeNodes.item(0);
        val = pixelSizeNode.getAttributes().getNamedItem("value").getNodeValue();
        actualResolution = (int) Math.round(25.4 / Double.parseDouble(val));
        assertEquals("Y resolution doesn't match", expectedResolution, actualResolution);

        iis.close();
        reader.dispose();
    }
View Full Code Here

     */
    void checkTiffCompression(String filename, String expectedCompression) throws IOException
    {
        Iterator readers = ImageIO.getImageReadersBySuffix("tiff");
        ImageReader reader = (ImageReader) readers.next();
        ImageInputStream iis = ImageIO.createImageInputStream(new File(filename));
        reader.setInput(iis);
        IIOMetadata imageMetadata = reader.getImageMetadata(0);
        Element root = (Element) imageMetadata.getAsTree(STANDARD_METADATA_FORMAT);
        Element comprElement = (Element) root.getElementsByTagName("Compression").item(0);
        Node comprTypeNode = comprElement.getElementsByTagName("CompressionTypeName").item(0);
        String actualCompression = comprTypeNode.getAttributes().getNamedItem("value").getNodeValue();
        assertEquals("Incorrect TIFF compression in file " + filename, expectedCompression, actualCompression);
        iis.close();
        reader.dispose();
    }
View Full Code Here

        ArrayList<Vertex> vlist = new ArrayList<Vertex>();
        long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
        long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
        COSStream cosStream = (COSStream) cosDictionary;

        ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.getUnfilteredStream());
        while (true)
        {
            Vertex p;
            try
            {
View Full Code Here

        final int height = pdImage.getHeight();
        final int bitsPerComponent = pdImage.getBitsPerComponent();
        final float[] decode = getDecodeArray(pdImage);

        // read bit stream
        ImageInputStream iis = null;
        try
        {
            // create stream
            iis = new MemoryCacheImageInputStream(pdImage.getStream().createInputStream());
            final float sampleMax = (float)Math.pow(2, bitsPerComponent) - 1f;
            final boolean isIndexed = colorSpace instanceof PDIndexed;

            // init color key mask
            float[] colorKeyRanges = null;
            BufferedImage colorKeyMask = null;
            if (colorKey != null)
            {
                colorKeyRanges = colorKey.toFloatArray();
                colorKeyMask = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            }

            // calculate row padding
            int padding = 0;
            if (width * numComponents * bitsPerComponent % 8 > 0)
            {
                padding = 8 - (width * numComponents * bitsPerComponent % 8);
            }

            // read stream
            byte[] srcColorValues = new byte[numComponents];
            byte[] alpha = new byte[1];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    boolean isMasked = true;
                    for (int c = 0; c < numComponents; c++)
                    {
                        int value = (int)iis.readBits(bitsPerComponent);

                        // color key mask requires values before they are decoded
                        if (colorKeyRanges != null)
                        {
                            isMasked &= value >= colorKeyRanges[c * 2] &&
                                        value <= colorKeyRanges[c * 2 + 1];
                        }

                        // decode array
                        final float dMin = decode[c * 2];
                        final float dMax = decode[(c * 2) + 1];

                        // interpolate to domain
                        float output = dMin + (value * ((dMax - dMin) / sampleMax));

                        if (isIndexed)
                        {
                            // indexed color spaces get the raw value, because the TYPE_BYTE
                            // below cannot be reversed by the color space without it having
                            // knowledge of the number of bits per component
                            srcColorValues[c] = (byte)Math.round(output);
                        }
                        else
                        {
                            // interpolate to TYPE_BYTE
                            int outputByte = Math.round(((output - Math.min(dMin, dMax)) /
                                    Math.abs(dMax - dMin)) * 255f);

                            srcColorValues[c] = (byte)outputByte;
                        }
                    }
                    raster.setDataElements(x, y, srcColorValues);

                    // set alpha channel in color key mask, if any
                    if (colorKeyMask != null)
                    {
                        alpha[0] = (byte)(isMasked ? 255 : 0);
                        colorKeyMask.getRaster().setDataElements(x, y, alpha);
                    }
                }

                // rows are padded to the nearest byte
                iis.readBits(padding);
            }

            // use the color space to convert the image to RGB
            BufferedImage rgbImage = colorSpace.toRGBImage(raster);

            // apply color mask, if any
            if (colorKeyMask != null)
            {
                return applyColorKeyMask(rgbImage, colorKeyMask);
            }
            else
            {
                return rgbImage;
            }
        }
        finally
        {
            if (iis != null)
            {
                iis.close();
            }
        }
    }
View Full Code Here

TOP

Related Classes of javax.imageio.stream.ImageInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.