public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
              dstCM.isAlphaPremultiplied(),
              null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);
    inPixels = filterPixels(width, height, inPixels, transformedSpace);
    setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels);

    return dst;
  }
 public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
   if (dstCM == null) dstCM = src.getColorModel();
   return new BufferedImage(
       dstCM,
       dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()),
       dstCM.isAlphaPremultiplied(),
       null);
 }
  public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM) {
    Rectangle2D newBounds = getBounds2D(src);

    // Destination image should include (0,0) + positive part
    // of the area bounded by newBounds (in source coordinate system).
    double dstWidth = newBounds.getX() + newBounds.getWidth();
    double dstHeight = newBounds.getY() + newBounds.getHeight();

    if (dstWidth <= 0 || dstHeight <= 0) {
      // awt.251=Transformed width ({0}) and height ({1}) should be
      // greater than 0
      throw new RasterFormatException(
          Messages.getString("awt.251", dstWidth, dstHeight)); // $NON-NLS-1$
    }

    if (destCM != null) {
      return new BufferedImage(
          destCM,
          destCM.createCompatibleWritableRaster((int) dstWidth, (int) dstHeight),
          destCM.isAlphaPremultiplied(),
          null);
    }

    ColorModel cm = src.getColorModel();

    // Interpolation other than NN doesn't make any sense for index color
    if (iType != TYPE_NEAREST_NEIGHBOR && cm instanceof IndexColorModel) {
      return new BufferedImage((int) dstWidth, (int) dstHeight, BufferedImage.TYPE_INT_ARGB);
    }

    // OK, we can get source color model
    return new BufferedImage(
        cm,
        src.getRaster().createCompatibleWritableRaster((int) dstWidth, (int) dstHeight),
        cm.isAlphaPremultiplied(),
        null);
  }
Exemple #4
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int w = src.getWidth();
    int h = src.getHeight();

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(width, height),
              dstCM.isAlphaPremultiplied(),
              null);
    }

    Graphics2D g = dst.createGraphics();
    g.drawRenderedImage(src, AffineTransform.getTranslateInstance(-x, -y));
    g.dispose();

    return dst;
  }
  /**
   * 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);
          }
        }
      }
    }
  }
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
              dstCM.isAlphaPremultiplied(),
              null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);

    if (interpolation == NEAREST_NEIGHBOUR)
      return filterPixelsNN(dst, width, height, inPixels, transformedSpace);

    int srcWidth = width;
    int srcHeight = height;
    int srcWidth1 = width - 1;
    int srcHeight1 = height - 1;
    int outWidth = transformedSpace.width;
    int outHeight = transformedSpace.height;
    int outX, outY;
    int index = 0;
    int[] outPixels = new int[outWidth];

    outX = transformedSpace.x;
    outY = transformedSpace.y;
    float[] out = new float[2];

    for (int y = 0; y < outHeight; y++) {
      for (int x = 0; x < outWidth; x++) {
        transformInverse(outX + x, outY + y, out);
        int srcX = (int) Math.floor(out[0]);
        int srcY = (int) Math.floor(out[1]);
        float xWeight = out[0] - srcX;
        float yWeight = out[1] - srcY;
        int nw, ne, sw, se;

        if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
          // Easy case, all corners are in the image
          int i = srcWidth * srcY + srcX;
          nw = inPixels[i];
          ne = inPixels[i + 1];
          sw = inPixels[i + srcWidth];
          se = inPixels[i + srcWidth + 1];
        } else {
          // Some of the corners are off the image
          nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight);
          ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight);
          sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight);
          se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight);
        }
        outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
      }
      setRGB(dst, 0, y, transformedSpace.width, 1, outPixels);
    }
    return dst;
  }
  public BufferedImage decompress(final QuickTime.ImageDesc pDescription, final InputStream pStream)
      throws IOException {
    byte[] data = new byte[pDescription.dataSize];

    DataInputStream stream = new DataInputStream(pStream);
    try {
      stream.readFully(data, 0, pDescription.dataSize);
    } finally {
      stream.close();
    }

    DataBuffer buffer = new DataBufferByte(data, data.length);

    WritableRaster raster;

    // TODO: Depth parameter can be 1-32 (color) or 33-40 (gray scale)
    switch (pDescription.depth) {
      case 40: // 8 bit gray (untested)
        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width,
                1,
                new int[] {0},
                null);
        break;
      case 24: // 24 bit RGB
        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width * 3,
                3,
                new int[] {0, 1, 2},
                null);
        break;
      case 32: // 32 bit ARGB
        // WORKAROUND: There is a bug in the way Java 2D interprets the band offsets in
        // Raster.createInterleavedRaster (see below) before Java 6. So, instead of
        // passing a correct offset array below, we swap channel 1 & 3 to make it ABGR...
        for (int y = 0; y < pDescription.height; y++) {
          for (int x = 0; x < pDescription.width; x++) {
            int offset = 4 * y * pDescription.width + x * 4;
            byte temp = data[offset + 1];
            data[offset + 1] = data[offset + 3];
            data[offset + 3] = temp;
          }
        }

        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width * 4,
                4,
                new int[] {3, 2, 1, 0}, // B & R mixed up. {1, 2, 3, 0} is correct
                null);
        break;
      default:
        throw new IIOException("Unsupported RAW depth: " + pDescription.depth);
    }

    ColorModel cm =
        new ComponentColorModel(
            pDescription.depth <= 32
                ? ColorSpace.getInstance(ColorSpace.CS_sRGB)
                : ColorSpace.getInstance(ColorSpace.CS_GRAY),
            pDescription.depth == 32,
            false,
            pDescription.depth == 32 ? Transparency.TRANSLUCENT : Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
  }