예제 #1
0
  private synchronized void computeHistogram(Rectangle visibleRect, PlanarImage image) {
    int channels = image.getSampleModel().getNumBands();

    Histogram hist = new Histogram(256, 256, 512, channels);

    bins = hist.getBins();

    // Raster raster = image.getData(visibleRect);
    Rectangle bounds = visibleRect; // image.getBounds();
    int pixel[] = null;

    int maxPixels = 256;
    int incX = bounds.width >= 2 * maxPixels ? bounds.width / maxPixels : 1;
    int incY = bounds.height >= 2 * maxPixels ? bounds.height / maxPixels : 1;

    double log2 = Math.log(2);

    int minTileX = image.XToTileX(bounds.x);
    int maxTileX = image.XToTileX(bounds.x + bounds.width - 1);
    int minTileY = image.YToTileY(bounds.y);
    int maxTileY = image.YToTileY(bounds.y + bounds.height - 1);

    for (int tx = minTileX; tx <= maxTileX; tx++)
      for (int ty = minTileY; ty <= maxTileY; ty++) {
        Raster raster = image.getTile(tx, ty);

        int minX = Math.max(bounds.x, raster.getMinX());
        int maxX = Math.min(bounds.x + bounds.width, raster.getMinX() + raster.getWidth());
        int minY = Math.max(bounds.y, raster.getMinY());
        int maxY = Math.min(bounds.y + bounds.height, raster.getMinY() + raster.getHeight());

        for (int x = minX; x < maxX; x += incX)
          for (int y = minY; y < maxY; y += incY) {
            pixel = raster.getPixel(x, y, pixel);
            for (int c = 0; c < channels; c++) {
              int v = (int) (511 * logTable[pixel[c]] / (16 * log2));
              if (v > 255) bins[c][v - 256]++;
              else bins[c][0]++;
            }
          }
      }

    bins = hist.getBins();
  }
예제 #2
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);
          }
        }
      }
    }
  }