/** Initializes the ImageDisplay. */
  private synchronized void initialize() {
    if (source == null) return;

    componentWidth = source.getWidth();
    componentHeight = source.getHeight();

    setPreferredSize(new Dimension(componentWidth, componentHeight));

    this.sampleModel = source.getSampleModel();

    // First check whether the opimage has already set a suitable ColorModel
    this.colorModel = source.getColorModel();
    if (this.colorModel == null) {
      // If not, then create one.
      this.colorModel = PlanarImage.createColorModel(this.sampleModel);
      if (this.colorModel == null) {
        throw new IllegalArgumentException("no color model");
      }
    }

    minTileX = source.getMinTileX();
    maxTileX = source.getMinTileX() + source.getNumXTiles() - 1;
    minTileY = source.getMinTileY();
    maxTileY = source.getMinTileY() + source.getNumYTiles() - 1;
    tileWidth = source.getTileWidth();
    tileHeight = source.getTileHeight();
    tileGridXOffset = source.getTileGridXOffset();
    tileGridYOffset = source.getTileGridYOffset();
  }
  /** Records a new size. Called by the AWT. */
  public void setBounds(int x, int y, int width, int height) {
    Insets insets = getInsets();
    int w;
    int h;

    if (source == null) {
      w = width;
      h = height;
    } else {
      w = source.getWidth();
      h = source.getHeight();

      if (width < w) {
        w = width;
      }

      if (height < h) {
        h = height;
      }
    }

    componentWidth = w + insets.left + insets.right;
    componentHeight = h + insets.top + insets.bottom;

    super.setBounds(x + shift_x, y + shift_y, componentWidth, componentHeight);
  }
  /**
   * 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);
          }
        }
      }
    }
  }