Ejemplo n.º 1
0
  public RenderedImage decodeAsRenderedImage(int page) throws IOException {
    if (page != 0) {
      throw new IOException(JaiI18N.getString(JaiI18N.getString("WBMPImageDecoder0")));
    }

    input.read(); // TypeField
    input.read(); // FixHeaderField

    // Image width
    int value = input.read();
    int width = value & 0x7f;
    while ((value & 0x80) == 0x80) {
      width <<= 7;
      value = input.read();
      width |= (value & 0x7f);
    }

    // Image height
    value = input.read();
    int height = value & 0x7f;
    while ((value & 0x80) == 0x80) {
      height <<= 7;
      value = input.read();
      height |= (value & 0x7f);
    }

    // Create byte-packed bilevel image width an IndexColorModel
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);

    // Get the image tile.
    WritableRaster tile = bi.getWritableTile(0, 0);

    // Get the SampleModel.
    MultiPixelPackedSampleModel sm = (MultiPixelPackedSampleModel) bi.getSampleModel();

    // Read the data.
    input.readFully(
        ((DataBufferByte) tile.getDataBuffer()).getData(), 0, height * sm.getScanlineStride());

    return bi;
  }
Ejemplo n.º 2
0
  /**
   * Constructs a BytePackedRaster with the given SampleModel, DataBuffer, and parent. DataBuffer
   * must be a DataBufferByte and SampleModel must be of type MultiPixelPackedSampleModel. When
   * translated into the base Raster's coordinate system, aRegion must be contained by the base
   * Raster. Origin is the coordinate in the new Raster's coordinate system of the origin of the
   * base Raster. (The base Raster is the Raster's ancestor which has no parent.)
   *
   * <p>Note that this constructor should generally be called by other constructors or create
   * methods, it should not be used directly.
   *
   * @param sampleModel The SampleModel that specifies the layout.
   * @param dataBuffer The DataBufferShort that contains the image data.
   * @param aRegion The Rectangle that specifies the image area.
   * @param origin The Point that specifies the origin.
   * @param parent The parent (if any) of this raster.
   * @exception RasterFormatException if the parameters do not conform to requirements of this
   *     Raster type.
   */
  public BytePackedRaster(
      SampleModel sampleModel,
      DataBuffer dataBuffer,
      Rectangle aRegion,
      Point origin,
      BytePackedRaster parent) {
    super(sampleModel, dataBuffer, aRegion, origin, parent);
    this.maxX = minX + width;
    this.maxY = minY + height;

    if (!(dataBuffer instanceof DataBufferByte)) {
      throw new RasterFormatException("BytePackedRasters must have" + "byte DataBuffers");
    }
    DataBufferByte dbb = (DataBufferByte) dataBuffer;
    this.data = stealData(dbb, 0);
    if (dbb.getNumBanks() != 1) {
      throw new RasterFormatException(
          "DataBuffer for BytePackedRasters" + " must only have 1 bank.");
    }
    int dbOffset = dbb.getOffset();

    if (sampleModel instanceof MultiPixelPackedSampleModel) {
      MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sampleModel;
      this.type = IntegerComponentRaster.TYPE_BYTE_BINARY_SAMPLES;
      pixelBitStride = mppsm.getPixelBitStride();
      if (pixelBitStride != 1 && pixelBitStride != 2 && pixelBitStride != 4) {
        throw new RasterFormatException("BytePackedRasters must have a bit depth of 1, 2, or 4");
      }
      scanlineStride = mppsm.getScanlineStride();
      dataBitOffset = mppsm.getDataBitOffset() + dbOffset * 8;
      int xOffset = aRegion.x - origin.x;
      int yOffset = aRegion.y - origin.y;
      dataBitOffset += xOffset * pixelBitStride + yOffset * scanlineStride * 8;
      bitMask = (1 << pixelBitStride) - 1;
      shiftOffset = 8 - pixelBitStride;
    } else {
      throw new RasterFormatException(
          "BytePackedRasters must have" + "MultiPixelPackedSampleModel");
    }
    verify(false);
  }
Ejemplo n.º 3
0
  /** Constructs a DirectRasterAccessor object */
  public DirectRasterAccessor(Raster raster, ColorModel cm) {
    DataBuffer db = raster.getDataBuffer();

    offsetX = raster.getMinX() - raster.getSampleModelTranslateX();
    offsetY = raster.getMinY() - raster.getSampleModelTranslateY();

    if (!(db instanceof DataBufferByte)) {
      throw new RuntimeException(
          "DataBuffer of Raster not of correct type "
              + "(expected DataBufferByte, got "
              + db.getClass().getName()
              + ")");
    }

    DataBufferByte dbb = (DataBufferByte) db;

    SampleModel sm = raster.getSampleModel();

    if (!(sm instanceof MultiPixelPackedSampleModel)) {
      throw new RuntimeException(
          "SampleModel of Raster not of correct type "
              + "(expected MultiPixelPackedSampleModel, got "
              + sm.getClass().getName()
              + ")");
    }

    MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sm;

    data = dbb.getData();
    scanlineStride = mppsm.getScanlineStride();

    if (cm.getRGB(0) == Color.white.getRGB()) {
      white = 0;
      black = 1;
    } else {
      white = 1;
      black = 0;
    }
  }