/**
   * Creates a {@link WritableRaster writable raster}.
   *
   * @param width width of the raster to create.
   * @param height height of the raster to create.
   * @param dataClass data type for the raster. If <code>null</code>, defaults to double.
   * @param sampleModel the samplemodel to use. If <code>null</code>, defaults to <code>
   *     new ComponentSampleModel(dataType, width, height, 1, width, new int[]{0});</code>.
   * @param value value to which to set the raster to. If null, the default of the raster creation
   *     is used, which is 0.
   * @return a {@link WritableRaster writable raster}.
   */
  public static WritableRaster createDoubleWritableRaster(
      int width, int height, Class<?> dataClass, SampleModel sampleModel, Double value) {
    int dataType = DataBuffer.TYPE_DOUBLE;
    if (dataClass != null) {
      if (dataClass.isAssignableFrom(Integer.class)) {
        dataType = DataBuffer.TYPE_INT;
      } else if (dataClass.isAssignableFrom(Float.class)) {
        dataType = DataBuffer.TYPE_FLOAT;
      } else if (dataClass.isAssignableFrom(Byte.class)) {
        dataType = DataBuffer.TYPE_BYTE;
      }
    }
    if (sampleModel == null) {
      sampleModel = new ComponentSampleModel(dataType, width, height, 1, width, new int[] {0});
    }

    WritableRaster raster = RasterFactory.createWritableRaster(sampleModel, null);
    if (value != null) {
      // autobox only once
      double v = value;

      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          raster.setSample(x, y, 0, v);
        }
      }
    }
    return raster;
  }
 /**
  * Creates a test paletted image with translucency.
  *
  * @return
  */
 private static BufferedImage getSyntheticTranslucentIndexed() {
   final byte bb[] = new byte[256];
   for (int i = 0; i < 256; i++) bb[i] = (byte) i;
   final IndexColorModel icm = new IndexColorModel(8, 256, bb, bb, bb, bb);
   final WritableRaster raster =
       RasterFactory.createWritableRaster(icm.createCompatibleSampleModel(1024, 1024), null);
   for (int i = raster.getMinX(); i < raster.getMinX() + raster.getWidth(); i++)
     for (int j = raster.getMinY(); j < raster.getMinY() + raster.getHeight(); j++)
       raster.setSample(i, j, 0, (i + j) / 32);
   return new BufferedImage(icm, raster, false, null);
 }
  protected RenderedImage createRenderedImage(
      RenderedImage renderedImage, int height, int width, DataBuffer dataBuffer) {

    SampleModel sampleModel =
        RasterFactory.createPixelInterleavedSampleModel(
            DataBuffer.TYPE_BYTE, width, height, _NUM_OF_BANDS);
    ColorModel colorModel = PlanarImage.createColorModel(sampleModel);

    TiledImage tiledImage = new TiledImage(0, 0, width, height, 0, 0, sampleModel, colorModel);

    Raster raster = RasterFactory.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));

    tiledImage.setData(raster);

    /*javax.media.jai.JAI.create(
    	"filestore", tiledImage, "test.png", "PNG");

    printImage(renderedImage);
    printImage(tiledImage);}*/

    return tiledImage;
  }
Esempio n. 4
0
  /**
   * Paint the image onto a Graphics object. The painting is performed tile-by-tile, and includes a
   * grey region covering the unused portion of image tiles as well as the general background. At
   * this point the image must be byte data.
   */
  public synchronized void paintComponent(Graphics g) {

    Graphics2D g2D = null;
    if (g instanceof Graphics2D) {
      g2D = (Graphics2D) g;
    } else {
      return;
    }

    // if source is null, it's just a component
    if (source == null) {
      g2D.setColor(getBackground());
      g2D.fillRect(0, 0, componentWidth, componentHeight);
      return;
    }

    int transX = -originX;
    int transY = -originY;

    // Get the clipping rectangle and translate it into image coordinates.
    Rectangle clipBounds = g.getClipBounds();

    if (clipBounds == null) {
      clipBounds = new Rectangle(0, 0, componentWidth, componentHeight);
    }

    // clear the background (clip it) [minimal optimization here]
    if (transX > 0
        || transY > 0
        || transX < (componentWidth - source.getWidth())
        || transY < (componentHeight - source.getHeight())) {
      g2D.setColor(getBackground());
      g2D.fillRect(0, 0, componentWidth, componentHeight);
    }

    clipBounds.translate(-transX, -transY);

    // Determine the extent of the clipping region in tile coordinates.
    int txmin, txmax, tymin, tymax;
    int ti, tj;

    txmin = XtoTileX(clipBounds.x);
    txmin = Math.max(txmin, minTileX);
    txmin = Math.min(txmin, maxTileX);

    txmax = XtoTileX(clipBounds.x + clipBounds.width - 1);
    txmax = Math.max(txmax, minTileX);
    txmax = Math.min(txmax, maxTileX);

    tymin = YtoTileY(clipBounds.y);
    tymin = Math.max(tymin, minTileY);
    tymin = Math.min(tymin, maxTileY);

    tymax = YtoTileY(clipBounds.y + clipBounds.height - 1);
    tymax = Math.max(tymax, minTileY);
    tymax = Math.min(tymax, maxTileY);
    Insets insets = getInsets();

    // Loop over tiles within the clipping region
    for (tj = tymin; tj <= tymax; tj++) {
      for (ti = txmin; ti <= txmax; ti++) {
        int tx = TileXtoX(ti);
        int ty = TileYtoY(tj);

        Raster tile = source.getTile(ti, tj);
        if (tile != null) {
          DataBuffer dataBuffer = tile.getDataBuffer();

          WritableRaster wr = tile.createWritableRaster(sampleModel, dataBuffer, null);

          BufferedImage bi =
              new BufferedImage(colorModel, wr, colorModel.isAlphaPremultiplied(), null);

          // correctly handles band offsets
          if (brightnessEnabled == true) {
            SampleModel sm =
                sampleModel.createCompatibleSampleModel(tile.getWidth(), tile.getHeight());

            WritableRaster raster = RasterFactory.createWritableRaster(sm, null);

            BufferedImage bimg =
                new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);

            // don't move this code
            ByteLookupTable lutTable = new ByteLookupTable(0, lutData);
            LookupOp lookup = new LookupOp(lutTable, null);
            lookup.filter(bi, bimg);

            g2D.drawImage(bimg, biop, tx + transX + insets.left, ty + transY + insets.top);
          } else {
            AffineTransform transform;

            transform =
                AffineTransform.getTranslateInstance(
                    tx + transX + insets.left, ty + transY + insets.top);

            g2D.drawRenderedImage(bi, transform);
          }
        }
      }
    }
  }