/**
  * Decode JBIG2 data using Java ImageIO library.
  *
  * <p>{@inheritDoc}
  */
 public void decode(
     InputStream compressedData, OutputStream result, COSDictionary options, int filterIndex)
     throws IOException {
   /**
    * A working JBIG2 ImageIO plugin is needed to decode JBIG2 encoded streams. The following is
    * known to be working. It can't be bundled with PDFBox because of an incompatible license.
    * http://code.google.com/p/jbig2-imageio/
    */
   Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JBIG2");
   if (!readers.hasNext()) {
     LOG.error("Can't find an ImageIO plugin to decode the JBIG2 encoded datastream.");
     return;
   }
   ImageReader reader = readers.next();
   COSDictionary decodeP = (COSDictionary) options.getDictionaryObject(COSName.DECODE_PARMS);
   COSInteger bits = (COSInteger) options.getDictionaryObject(COSName.BITS_PER_COMPONENT);
   COSStream st = null;
   if (decodeP != null) {
     st = (COSStream) decodeP.getDictionaryObject(COSName.JBIG2_GLOBALS);
   }
   if (st != null) {
     reader.setInput(
         ImageIO.createImageInputStream(
             new SequenceInputStream(st.getFilteredStream(), compressedData)));
   } else {
     reader.setInput(ImageIO.createImageInputStream(compressedData));
   }
   BufferedImage bi = reader.read(0);
   reader.dispose();
   if (bi != null) {
     // I am assuming since JBIG2 is always black and white
     // depending on your renderer this might or might be needed
     if (bi.getColorModel().getPixelSize() != bits.intValue()) {
       if (bits.intValue() != 1) {
         LOG.error("Do not know how to deal with JBIG2 with more than 1 bit");
         return;
       }
       BufferedImage packedImage =
           new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
       Graphics graphics = packedImage.getGraphics();
       graphics.drawImage(bi, 0, 0, null);
       graphics.dispose();
       bi = packedImage;
     }
     DataBuffer dBuf = bi.getData().getDataBuffer();
     if (dBuf.getDataType() == DataBuffer.TYPE_BYTE) {
       result.write(((DataBufferByte) dBuf).getData());
     } else {
       LOG.error("Image data buffer not of type byte but type " + dBuf.getDataType());
     }
   } else {
     LOG.error("Something went wrong when decoding the JBIG2 encoded datastream.");
   }
 }
 /**
  * Create Byte type raster iterator to follow from minX, minY raster and rectangle intersection
  * coordinate.
  *
  * @param raster will be followed by this iterator.
  * @param subArea {@code Rectangle} which define read iterator area.
  * @throws IllegalArgumentException if subArea don't intersect raster boundary.
  */
 DefaultDirectFloatIterator(final Raster raster, final Rectangle subArea) {
   super(raster, subArea);
   final DataBuffer databuf = raster.getDataBuffer();
   assert (databuf.getDataType() == DataBuffer.TYPE_FLOAT)
       : "raster data or not Byte type: " + databuf;
   this.currentDataArray = ((DataBufferFloat) databuf).getData();
 }
示例#3
0
    private long sizeOf(BufferedImage image) {
      if (image == null) {
        return 0L;
      }

      DataBuffer dataBuffer = image.getRaster().getDataBuffer();
      int dataTypeSize;
      switch (dataBuffer.getDataType()) {
        case DataBuffer.TYPE_BYTE:
          dataTypeSize = 1;
          break;
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_USHORT:
          dataTypeSize = 2;
          break;
        case DataBuffer.TYPE_INT:
        case DataBuffer.TYPE_FLOAT:
          dataTypeSize = 4;
          break;
        case DataBuffer.TYPE_DOUBLE:
        case DataBuffer.TYPE_UNDEFINED:
        default:
          dataTypeSize = 8;
          break;
      }
      return dataBuffer.getSize() * dataTypeSize;
    }
示例#4
0
 /**
  * Returns a width- and height-aligned image representation sharing data w/ {@link #image}.
  *
  * @param width
  * @param height
  * @return
  * @throws IllegalArgumentException if requested size exceeds image size
  */
 public BufferedImage getAlignedImage(int width, int height) throws IllegalArgumentException {
   if (width * height > image.getWidth() * image.getHeight()) {
     throw new IllegalArgumentException(
         "Requested size exceeds image size: "
             + width
             + "x"
             + height
             + " > "
             + image.getWidth()
             + "x"
             + image.getHeight());
   }
   if (width == image.getWidth() && height == image.getHeight()) {
     return image;
   } else {
     final ColorModel cm = image.getColorModel();
     final WritableRaster raster0 = image.getRaster();
     final DataBuffer dataBuffer = raster0.getDataBuffer();
     final SinglePixelPackedSampleModel sppsm0 =
         (SinglePixelPackedSampleModel) raster0.getSampleModel();
     final SinglePixelPackedSampleModel sppsm1 =
         new SinglePixelPackedSampleModel(
             dataBuffer.getDataType(),
             width,
             height,
             width /* scanLineStride */,
             sppsm0.getBitMasks());
     final WritableRaster raster1 = WritableRaster.createWritableRaster(sppsm1, dataBuffer, null);
     return new BufferedImage(cm, raster1, cm.isAlphaPremultiplied(), null);
   }
 }
示例#5
0
文件: Raster.java 项目: OPSF/uClinux
 /**
  * Creates a new banded raster.
  *
  * @param dataBuffer the data buffer.
  * @param w the width.
  * @param h the height.
  * @param scanlineStride the number of data elements from a sample on one row to the corresponding
  *     sample on the next row.
  * @param bankIndices the index for each bank.
  * @param bandOffsets the band offsets.
  * @param location
  * @return The new raster.
  */
 public static WritableRaster createBandedRaster(
     DataBuffer dataBuffer,
     int w,
     int h,
     int scanlineStride,
     int[] bankIndices,
     int[] bandOffsets,
     Point location) {
   SampleModel sm =
       new BandedSampleModel(
           dataBuffer.getDataType(), w, h, scanlineStride, bankIndices, bandOffsets);
   return createWritableRaster(sm, dataBuffer, location);
 }
示例#6
0
文件: Raster.java 项目: OPSF/uClinux
 /**
  * Creates a new interleaved raster.
  *
  * @param dataBuffer the data buffer.
  * @param w the width.
  * @param h the height.
  * @param scanlineStride the number of data elements from a sample on one row to the corresponding
  *     sample on the next row.
  * @param pixelStride the number of elements from a sample in one pixel to the corresponding
  *     sample in the next pixel.
  * @param bandOffsets the offset for each band.
  * @param location
  * @return The new raster.
  */
 public static WritableRaster createInterleavedRaster(
     DataBuffer dataBuffer,
     int w,
     int h,
     int scanlineStride,
     int pixelStride,
     int[] bandOffsets,
     Point location) {
   SampleModel sm =
       new ComponentSampleModel(
           dataBuffer.getDataType(), w, h, pixelStride, scanlineStride, bandOffsets);
   return createWritableRaster(sm, dataBuffer, location);
 }
  /**
   * Returns a ByteBuffer of BufferedImage data. Ensure BufferedImage is of 4BYTE_ABGR type. If
   * imageFormat is set to GL_RGBA, byte stream will be converted.
   */
  private ByteBuffer getByteBuffer(final BufferedImage _image) {
    final DataBuffer buffer = _image.getRaster().getDataBuffer();
    final int type = buffer.getDataType();

    if (type == DataBuffer.TYPE_BYTE) {
      final byte[] data = ((DataBufferByte) buffer).getData();
      if (imageFormat == GL3.GL_RGBA) {
        convertABGRtoRGBA(data);
      }

      return ByteBuffer.wrap(data);
    }

    System.out.println("Failed to determine DataBuffer type.");
    return null;
  }
  /** Returns the data array from the <code>DataBuffer</code>. */
  private static final Object getDataBufferData(DataBuffer db) {
    Object data;

    int dType = db.getDataType();
    switch (dType) {
      case DataBuffer.TYPE_BYTE:
        data = ((DataBufferByte) db).getData();
        break;
      case DataBuffer.TYPE_USHORT:
        data = ((DataBufferUShort) db).getData();
        break;
      default:
        throw new IllegalArgumentException(I18N.getString("Generic0") + " " + dType);
    }

    return data;
  }
示例#9
0
 private static long sizeOfDataBuffer(java.awt.image.DataBuffer dataBuffer) {
   return sizeOfElement(dataBuffer.getDataType()) * dataBuffer.getSize();
 }
示例#10
0
文件: Raster.java 项目: OPSF/uClinux
 /**
  * Creates a new packed raster.
  *
  * @param dataBuffer the data buffer.
  * @param w the width.
  * @param h the height.
  * @param bitsPerPixel the number of bits per pixel.
  * @param location
  * @return The new raster.
  */
 public static WritableRaster createPackedRaster(
     DataBuffer dataBuffer, int w, int h, int bitsPerPixel, Point location) {
   SampleModel sm = new MultiPixelPackedSampleModel(dataBuffer.getDataType(), w, h, bitsPerPixel);
   return createWritableRaster(sm, dataBuffer, location);
 }
示例#11
0
文件: Raster.java 项目: OPSF/uClinux
 /**
  * Creates a new packed raster.
  *
  * @param dataBuffer the data buffer.
  * @param w the width.
  * @param h the height.
  * @param scanlineStride the number of data elements from a sample on one row to the corresponding
  *     sample on the next row.
  * @param bandMasks the bit mask for each band.
  * @param location
  * @return The new raster.
  */
 public static WritableRaster createPackedRaster(
     DataBuffer dataBuffer, int w, int h, int scanlineStride, int[] bandMasks, Point location) {
   SampleModel sm =
       new SinglePixelPackedSampleModel(dataBuffer.getDataType(), w, h, scanlineStride, bandMasks);
   return createWritableRaster(sm, dataBuffer, location);
 }
  @Override
  protected void readBandRasterDataImpl(
      int sourceOffsetX,
      int sourceOffsetY,
      int sourceWidth,
      int sourceHeight,
      int sourceStepX,
      int sourceStepY,
      Band destBand,
      int destOffsetX,
      int destOffsetY,
      int destWidth,
      int destHeight,
      ProductData destBuffer,
      ProgressMonitor pm)
      throws IOException {

    if (isGlobalShifted180) {
      // SPECIAL CASE of a global geographic lat/lon with lon from 0..360 instead of -180..180
      readBandRasterDataImplGlobalShifted180(
          sourceOffsetX,
          sourceOffsetY,
          sourceWidth,
          sourceHeight,
          sourceStepX,
          sourceStepY,
          destBand,
          destOffsetX,
          destOffsetY,
          destWidth,
          destHeight,
          destBuffer,
          pm);
    } else {
      // the normal case!!
      final int destSize = destWidth * destHeight;
      pm.beginTask("Reading data...", 3);
      try {
        final Raster data =
            readRect(
                sourceOffsetX,
                sourceOffsetY,
                sourceStepX,
                sourceStepY,
                destOffsetX,
                destOffsetY,
                destWidth,
                destHeight);
        pm.worked(1);

        Integer bandIdx = bandMap.get(destBand);
        if (bandIdx == null) {
          bandIdx = 0;
        }
        final DataBuffer dataBuffer = data.getDataBuffer();
        final SampleModel sampleModel = data.getSampleModel();
        final int dataBufferType = dataBuffer.getDataType();

        boolean isInteger =
            dataBufferType == DataBuffer.TYPE_SHORT
                || dataBufferType == DataBuffer.TYPE_USHORT
                || dataBufferType == DataBuffer.TYPE_INT;
        boolean isIntegerTarget = destBuffer.getElems() instanceof int[];
        if (isInteger && isIntegerTarget) {
          sampleModel.getSamples(
              0,
              0,
              data.getWidth(),
              data.getHeight(),
              bandIdx,
              (int[]) destBuffer.getElems(),
              dataBuffer);
        } else if (dataBufferType == DataBuffer.TYPE_FLOAT
            && destBuffer.getElems() instanceof float[]) {
          sampleModel.getSamples(
              0,
              0,
              data.getWidth(),
              data.getHeight(),
              bandIdx,
              (float[]) destBuffer.getElems(),
              dataBuffer);
        } else {
          final double[] dArray = new double[destSize];
          sampleModel.getSamples(
              0, 0, data.getWidth(), data.getHeight(), bandIdx, dArray, dataBuffer);

          if (destBuffer.getElems() instanceof double[]) {
            //noinspection SuspiciousSystemArraycopy
            System.arraycopy(dArray, 0, destBuffer.getElems(), 0, dArray.length);
          } else {
            int i = 0;
            for (double value : dArray) {
              destBuffer.setElemDoubleAt(i++, value);
            }
          }
        }
        pm.worked(1);
      } finally {
        pm.done();
      }
    }
  }