Esempio n. 1
0
  // TODO: Candidate util method
  private static long skipToEOF(final ImageInputStream stream) throws IOException {
    long length = stream.length();

    if (length > 0) {
      // Known length, skip there and we're done.
      stream.seek(length);
    } else {
      // Otherwise, seek to EOF the hard way.
      // First, store stream position...
      long pos = stream.getStreamPosition();

      // ...skip 1k blocks until we're passed EOF...
      while (stream.skipBytes(1024l) > 0) {
        if (stream.read() == -1) {
          break;
        }

        pos = stream.getStreamPosition();
      }

      // ...go back to last known pos...
      stream.seek(pos);

      // ...finally seek until EOF one byte at a time. Done.
      while (stream.read() != -1) {}
    }

    return stream.getStreamPosition();
  }
  @Override
  public byte[] getPdfBuffer() {

    byte[] pdfByteArray = null;
    final FastByteArrayOutputStream os;

    try {
      os = new FastByteArrayOutputStream();

      // Download buffer
      final byte[] buffer = new byte[4096];

      // Download the PDF document
      int read;
      while ((read = iis.read(buffer)) != -1) {
        os.write(buffer, 0, read);
      }

      // Copy output stream to byte array
      pdfByteArray = os.toByteArray();

    } catch (final IOException e) {
      LogWriter.writeLog("[PDF] Exception " + e + " getting byte[] for " + fileName);
    }

    return pdfByteArray;
  }
Esempio n. 3
0
  public BufferedImage decode(ImageInputStream in) throws IOException, BMPException {
    if (!useDefaultMasks) bitmasks = readBitMasks(in);
    skipToImage(in);

    Dimension d = infoHeader.getSize();
    int h = (int) d.getHeight();
    int w = (int) d.getWidth();

    // BMP scanlines are padded to dword offsets
    int scansize = (w + (w & 1)) << 1;
    short[] data = new short[w * h];

    for (int y = h - 1; y >= 0; y--) {
      byte[] scanline = new byte[scansize];
      if (in.read(scanline) != scansize) throw new IOException("Couldn't read image data.");

      for (int x = 0; x < w; x++)
        data[x + y * w] =
            (short) ((scanline[x * 2] & (0xFF)) | ((scanline[x * 2 + 1] & (0xFF)) << 8));
    }

    ColorModel cm = new DirectColorModel(16, bitmasks[0], bitmasks[1], bitmasks[2]);
    SampleModel sm = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, w, h, bitmasks);
    DataBuffer db = new DataBufferUShort(data, w * h, 0);
    WritableRaster raster = Raster.createWritableRaster(sm, db, null);
    return new BufferedImage(cm, raster, false, null);
  }
  public boolean canDecodeInput(Object input) throws IOException {

    // The input source must be an ImageInputStream because the constructor
    // passes STANDARD_INPUT_TYPE (an array consisting of ImageInputStream)
    // as the only type of input source that it will deal with to its
    // superclass.

    if (!(input instanceof ImageInputStream)) return false;

    ImageInputStream stream = (ImageInputStream) input;

    /** Read and validate the input source's header. */
    byte[] header = new byte[8];
    try {
      // The input source's current position must be preserved so that
      // other ImageReaderSpis can determine if they can decode the input
      // source's format, should this input source be unable to handle the
      // decoding. Because the input source is an ImageInputStream, its
      // mark() and reset() methods are called to preserve the current
      // position.

      stream.mark();
      stream.read(header);
      stream.reset();
    } catch (IOException e) {
      return false;
    }

    byte[] controlHeader = new byte[] {(byte) 151, 74, 66, 50, 13, 10, 26, 10};

    return Arrays.equals(controlHeader, header);
  }
Esempio n. 5
0
    PSDResource(final short resourceId, final ImageInputStream input) throws IOException {
      id = resourceId;

      name = readPascalString(input);

      // Skip pad
      int nameSize = name.length() + 1;
      if (nameSize % 2 != 0) {
        input.readByte();
      }

      size = input.readUnsignedInt();
      long startPos = input.getStreamPosition();

      readData(new SubImageInputStream(input, size));

      // NOTE: This should never happen, however it's safer to keep it here for future compatibility
      if (input.getStreamPosition() != startPos + size) {
        input.seek(startPos + size);
      }

      // Data is even-padded (word aligned)
      if (size % 2 != 0) {
        input.read();
      }
    }
 public void runTest(Object ctx, int numReps) {
   final Context ictx = (Context) ctx;
   final ImageInputStream iis = ictx.inputStream;
   final int length = ictx.length;
   int pos = 0;
   try {
     iis.mark();
     do {
       if (pos >= length) {
         iis.reset();
         iis.mark();
         pos = 0;
       }
       iis.read();
       pos++;
     } while (--numReps >= 0);
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     try {
       iis.reset();
     } catch (IOException e) {
     }
   }
 }
 public void runTest(Object ctx, int numReps) {
   final Context ictx = (Context) ctx;
   final ImageInputStream iis = ictx.inputStream;
   final byte[] buf = ictx.byteBuf;
   final int scanlineStride = ictx.scanlineStride;
   final int length = ictx.length;
   int pos = 0;
   try {
     iis.mark();
     do {
       if (pos + scanlineStride > length) {
         iis.reset();
         iis.mark();
         pos = 0;
       }
       iis.read(buf);
       pos += scanlineStride;
     } while (--numReps >= 0);
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     try {
       iis.reset();
     } catch (IOException e) {
     }
   }
 }
  private static void stream(ImageInputStream is, OutputStream os, int length) throws IOException {
    for (long i = 0; i < length; i++) {
      int b = is.read();
      os.write(b);
    }

    os.close();
  }
 @Override
 public void renderPixelBand(
     final DataBuffer data,
     final int pixelIndex,
     final ImageInputStream imageInputStream,
     final int bandIndex)
     throws IOException {
   data.setElem(
       pixelIndex,
       ALPHA_MASK
           | data.getElem(pixelIndex)
           | (imageInputStream.read() << bandMapping.get(bandIndex)));
 }
Esempio n. 10
0
    BufferedImage getThumbnail(ImageInputStream iis, JPEGImageReader reader) throws IOException {
      iis.mark();
      iis.seek(streamPos);
      // read the palette
      byte[] palette = new byte[PALETTE_SIZE];
      float palettePart = ((float) PALETTE_SIZE) / getLength();
      readByteBuffer(iis, palette, reader, palettePart, 0.0F);
      DataBufferByte buffer = new DataBufferByte(thumbWidth * thumbHeight);
      readByteBuffer(iis, buffer.getData(), reader, 1.0F - palettePart, palettePart);
      iis.read();
      iis.reset();

      IndexColorModel cm = new IndexColorModel(8, 256, palette, 0, false);
      SampleModel sm = cm.createCompatibleSampleModel(thumbWidth, thumbHeight);
      WritableRaster raster = Raster.createWritableRaster(sm, buffer, null);
      return new BufferedImage(cm, raster, false, null);
    }
Esempio n. 11
0
 void readByteBuffer(
     ImageInputStream iis,
     byte[] data,
     JPEGImageReader reader,
     float workPortion,
     float workOffset)
     throws IOException {
   int progInterval = Math.max((int) (data.length / 20 / workPortion), 1);
   for (int offset = 0; offset < data.length; ) {
     int len = Math.min(progInterval, data.length - offset);
     iis.read(data, offset, len);
     offset += progInterval;
     float percentDone = ((float) offset * 100) / data.length * workPortion + workOffset;
     if (percentDone > 100.0F) {
       percentDone = 100.0F;
     }
     reader.thumbnailProgress(percentDone);
   }
 }
Esempio n. 12
0
 @Override
 public int getRecordCount() throws IOException {
   int count = 1;
   byte[] buffer = new byte[100 * 1024];
   int readChars;
   long currentPosInStream = stream.getStreamPosition();
   stream.seek(0);
   while ((readChars = stream.read(buffer)) != -1) {
     for (int i = 0; i < readChars - 1; ++i) {
       if (buffer[i] == '\n') {
         ++count;
       }
     }
   }
   count -= properties.size();
   final int headerLineCount = 1;
   count -= headerLineCount;
   stream.seek(currentPosInStream);
   return count;
 }
Esempio n. 13
0
  private void readFile() {
    // Do not allow this header to be read more than once.

    if (readFile) return;

    // Make sure that the application has set the input source.

    if (stream == null) throw new IllegalStateException("No input stream!");

    // Read the header.

    decoder = new JBIG2Decoder();

    try {
      byte[] data;
      int size = (int) stream.length();
      if (size == -1) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] temp = new byte[8192];
        for (int len = 0; (len = stream.read(temp)) > 0; ) {
          bos.write(temp, 0, len);
        }
        bos.close();
        data = bos.toByteArray();
      } else {
        data = new byte[size];
        stream.readFully(data);
      }

      decoder.decodeJBIG2(data);

    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JBIG2Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    readFile = true;
  }
 @Override
 public int read(final byte[] b) throws IOException {
   return iis.read(b);
 }
 @Override
 public int read() throws IOException {
   return iis.read();
 }
Esempio n. 16
0
  private void readHeader() throws IOException {
    if (gotHeader) {
      iis.seek(128);
      return;
    }

    metadata = new PCXMetadata();

    manufacturer = iis.readByte(); // manufacturer
    if (manufacturer != MANUFACTURER) throw new IllegalStateException("image is not a PCX file");
    metadata.version = iis.readByte(); // version
    encoding = iis.readByte(); // encoding
    if (encoding != ENCODING)
      throw new IllegalStateException("image is not a PCX file, invalid encoding " + encoding);

    metadata.bitsPerPixel = iis.readByte();

    metadata.xmin = iis.readShort();
    metadata.ymin = iis.readShort();
    xmax = iis.readShort();
    ymax = iis.readShort();

    metadata.hdpi = iis.readShort();
    metadata.vdpi = iis.readShort();

    iis.readFully(smallPalette);

    iis.readByte(); // reserved

    colorPlanes = iis.readByte();
    bytesPerLine = iis.readShort();
    paletteType = iis.readShort();

    metadata.hsize = iis.readShort();
    metadata.vsize = iis.readShort();

    iis.skipBytes(54); // skip filler

    width = xmax - metadata.xmin + 1;
    height = ymax - metadata.ymin + 1;

    if (colorPlanes == 1) {
      if (paletteType == PALETTE_GRAYSCALE) {
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        int[] nBits = {8};
        colorModel =
            new ComponentColorModel(
                cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        sampleModel =
            new ComponentSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, new int[] {0});
      } else {
        if (metadata.bitsPerPixel == 8) {
          // read palette from end of file, then reset back to image data
          iis.mark();

          if (iis.length() == -1) {
            // read until eof, and work backwards
            while (iis.read() != -1) ;
            iis.seek(iis.getStreamPosition() - 256 * 3 - 1);
          } else {
            iis.seek(iis.length() - 256 * 3 - 1);
          }

          int palletteMagic = iis.read();
          if (palletteMagic != 12)
            processWarningOccurred(
                "Expected palette magic number 12; instead read "
                    + palletteMagic
                    + " from this image.");

          iis.readFully(largePalette);
          iis.reset();

          colorModel = new IndexColorModel(metadata.bitsPerPixel, 256, largePalette, 0, false);
          sampleModel = colorModel.createCompatibleSampleModel(width, height);
        } else {
          int msize = metadata.bitsPerPixel == 1 ? 2 : 16;
          colorModel = new IndexColorModel(metadata.bitsPerPixel, msize, smallPalette, 0, false);
          sampleModel = colorModel.createCompatibleSampleModel(width, height);
        }
      }
    } else {
      ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
      int[] nBits = {8, 8, 8};
      colorModel =
          new ComponentColorModel(
              cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
      sampleModel =
          new ComponentSampleModel(
              DataBuffer.TYPE_BYTE,
              width,
              height,
              1,
              width * colorPlanes,
              new int[] {0, width, width * 2});
    }

    originalSampleModel = sampleModel;
    originalColorModel = colorModel;

    gotHeader = true;
  }
 public int read(byte b[], int off, int len) throws IOException {
   return src.read(b, off, len);
 }
 public int read(byte b[]) throws IOException {
   return src.read(b, 0, b.length);
 }
 // Override the methods defined in <code>InputStream</code>
 public int read() throws IOException {
   return src.read();
 }