Ejemplo n.º 1
0
  public AffineRed(CachableRed src, AffineTransform src2me, RenderingHints hints) {
    super(); // We _must_ call init...

    this.src2me = src2me;
    this.hints = hints;

    try {
      me2src = src2me.createInverse();
    } catch (NoninvertibleTransformException nite) {
      me2src = null;
    }

    // Calculate my bounds by applying the affine transform to
    // my input data..codec/
    Rectangle srcBounds = src.getBounds();
    // srcBounds.grow(-1,-1);
    Rectangle myBounds;
    myBounds = src2me.createTransformedShape(srcBounds).getBounds();

    // If the output buffer is not premultiplied in certain cases it
    // fails to properly divide out the Alpha (it always does
    // the affine on premultiplied data), hence you get ugly
    // back aliasing effects...
    ColorModel cm = fixColorModel(src);

    // fix my sample model so it makes sense given my size.
    SampleModel sm = fixSampleModel(src, cm, myBounds);

    Point2D pt = new Point2D.Float(src.getTileGridXOffset(), src.getTileGridYOffset());
    pt = src2me.transform(pt, null);

    // Finish initializing our base class...
    init(src, myBounds, cm, sm, (int) pt.getX(), (int) pt.getY(), null);
  }
Ejemplo n.º 2
0
  public WritableRaster copyData(WritableRaster wr) {
    // Get my source.
    CachableRed src = (CachableRed) getSources().get(0);

    Rectangle srcR = src.getBounds();
    Rectangle wrR = wr.getBounds();

    if (wrR.intersects(srcR)) {
      Rectangle r = wrR.intersection(srcR);

      // Limit the raster I send to my source to his rect.
      WritableRaster srcWR;
      srcWR = wr.createWritableChild(r.x, r.y, r.width, r.height, r.x, r.y, null);
      src.copyData(srcWR);
    }

    if (padMode == PadMode.ZERO_PAD) {
      handleZero(wr);
    } else if (padMode == PadMode.REPLICATE) {
      handleReplicate(wr);
    } else if (padMode == PadMode.WRAP) {
      handleWrap(wr);
    }

    return wr;
  }
Ejemplo n.º 3
0
  /**
   * Construct a blurred version of <tt>src</tt>, by blurring with a gaussian kernel with standard
   * Deviation of <tt>stdDev</tt> pixels.
   *
   * @param src The source image to blur
   * @param stdDevX The Standard Deviation of the Gaussian kernel in X
   * @param stdDevY The Standard Deviation of the Gaussian kernel in Y
   * @param rh Rendering hints.
   */
  public GaussianBlurRed8Bit(CachableRed src, double stdDevX, double stdDevY, RenderingHints rh) {
    super(); // Remember to call super.init()

    this.stdDevX = stdDevX;
    this.stdDevY = stdDevY;
    this.hints = rh;

    xinset = surroundPixels(stdDevX, rh);
    yinset = surroundPixels(stdDevY, rh);

    Rectangle myBounds = src.getBounds();
    myBounds.x += xinset;
    myBounds.y += yinset;
    myBounds.width -= 2 * xinset;
    myBounds.height -= 2 * yinset;
    if ((myBounds.width <= 0) || (myBounds.height <= 0)) {
      myBounds.width = 0;
      myBounds.height = 0;
    }

    ColorModel cm = fixColorModel(src);
    SampleModel sm = src.getSampleModel();
    int tw = sm.getWidth();
    int th = sm.getHeight();
    if (tw > myBounds.width) tw = myBounds.width;
    if (th > myBounds.height) th = myBounds.height;
    sm = cm.createCompatibleSampleModel(tw, th);

    init(
        src,
        myBounds,
        cm,
        sm,
        src.getTileGridXOffset() + xinset,
        src.getTileGridYOffset() + yinset,
        null);

    boolean highQuality =
        ((hints != null)
            && RenderingHints.VALUE_RENDER_QUALITY.equals(hints.get(RenderingHints.KEY_RENDERING)));

    // System.out.println("StdDev: " + stdDevX + "x" + stdDevY);
    if ((xinset != 0) && ((stdDevX < 2) || highQuality))
      convOp[0] = new ConvolveOp(makeQualityKernelX(xinset * 2 + 1));
    else dX = (int) Math.floor(DSQRT2PI * stdDevX + 0.5f);

    if ((yinset != 0) && ((stdDevY < 2) || highQuality))
      convOp[1] = new ConvolveOp(makeQualityKernelY(yinset * 2 + 1));
    else dY = (int) Math.floor(DSQRT2PI * stdDevY + 0.5f);
  }
Ejemplo n.º 4
0
  /**
   * Construct a luminace image from src.
   *
   * @param src The image to convert to a luminance image
   */
  public Any2LsRGBRed(CachableRed src) {
    super(
        src,
        src.getBounds(),
        fixColorModel(src),
        fixSampleModel(src),
        src.getTileGridXOffset(),
        src.getTileGridYOffset(),
        null);

    ColorModel srcCM = src.getColorModel();
    if (srcCM == null) return;
    ColorSpace srcCS = srcCM.getColorSpace();
    if (srcCS == ColorSpace.getInstance(ColorSpace.CS_sRGB)) srcIssRGB = true;
  }
Ejemplo n.º 5
0
  public WritableRaster INT_PACK_BYTE_COMP_Impl(WritableRaster wr) {
    // Get my source.
    CachableRed srcRed = (CachableRed) getSources().get(0);
    CachableRed alphaRed = (CachableRed) getSources().get(1);

    // Already has alpha channel so we use it.
    srcRed.copyData(wr);

    Rectangle rgn = wr.getBounds();
    rgn = rgn.intersection(alphaRed.getBounds());

    Raster r = alphaRed.getData(rgn);

    ComponentSampleModel csm;
    csm = (ComponentSampleModel) r.getSampleModel();
    final int alpScanStride = csm.getScanlineStride();

    DataBufferByte alpDB = (DataBufferByte) r.getDataBuffer();
    final int alpBase =
        (alpDB.getOffset()
            + csm.getOffset(
                rgn.x - r.getSampleModelTranslateX(), rgn.y - r.getSampleModelTranslateY()));

    // Access the pixel data array
    final byte[] alpPixels = alpDB.getBankData()[0];

    SinglePixelPackedSampleModel sppsm;
    sppsm = (SinglePixelPackedSampleModel) wr.getSampleModel();
    final int srcScanStride = sppsm.getScanlineStride();

    DataBufferInt srcDB = (DataBufferInt) wr.getDataBuffer();
    final int srcBase =
        (srcDB.getOffset()
            + sppsm.getOffset(
                rgn.x - wr.getSampleModelTranslateX(), rgn.y - wr.getSampleModelTranslateY()));

    // Access the pixel data array
    final int[] srcPixels = srcDB.getBankData()[0];

    ColorModel cm = srcRed.getColorModel();

    if (cm.isAlphaPremultiplied()) {
      // For alpha premult we need to multiply all comps.
      for (int y = 0; y < rgn.height; y++) {
        int sp = srcBase + y * srcScanStride;
        int ap = alpBase + y * alpScanStride;
        int end = sp + rgn.width;

        while (sp < end) {
          int a = ((int) alpPixels[ap++]) & 0xFF;
          final int pix = srcPixels[sp];
          srcPixels[sp] =
              ((((((pix >>> 24)) * a) & 0xFF00) << 16)
                  | (((((pix >>> 16) & 0xFF) * a) & 0xFF00) << 8)
                  | (((((pix >>> 8) & 0xFF) * a) & 0xFF00))
                  | (((((pix) & 0xFF) * a) & 0xFF00) >> 8));
          sp++;
        }
      }

    } else {
      // For non-alpha premult we only need to multiply alpha.
      for (int y = 0; y < rgn.height; y++) {
        int sp = srcBase + y * srcScanStride;
        int ap = alpBase + y * alpScanStride;
        int end = sp + rgn.width;
        while (sp < end) {
          int a = ((int) alpPixels[ap++]) & 0xFF;
          int sa = srcPixels[sp] >>> 24;
          srcPixels[sp] = ((((sa * a) & 0xFF00) << 16) | srcPixels[sp] & 0x00FFFFFF);
          sp++;
        }
      }
    }

    return wr;
  }
Ejemplo n.º 6
0
 public static Rectangle makeBounds(CachableRed src1, CachableRed src2) {
   Rectangle r1 = src1.getBounds();
   Rectangle r2 = src2.getBounds();
   return r1.intersection(r2);
 }
Ejemplo n.º 7
0
  public WritableRaster copyData(WritableRaster wr) {
    // Get my source.
    CachableRed srcRed = (CachableRed) getSources().get(0);
    CachableRed alphaRed = (CachableRed) getSources().get(1);

    if (is_INT_PACK_BYTE_COMP(srcRed.getSampleModel(), alphaRed.getSampleModel()))
      return INT_PACK_BYTE_COMP_Impl(wr);

    ColorModel cm = srcRed.getColorModel();
    if (cm.hasAlpha()) {
      // Already has alpha channel so we use it.
      srcRed.copyData(wr);

      Rectangle rgn = wr.getBounds();
      if (rgn.intersects(alphaRed.getBounds())) rgn = rgn.intersection(alphaRed.getBounds());
      else return wr;

      int[] wrData = null;
      int[] alphaData = null;

      Raster r = alphaRed.getData(rgn);
      int w = rgn.width;

      final int bands = wr.getSampleModel().getNumBands();

      if (cm.isAlphaPremultiplied()) {
        for (int y = rgn.y; y < rgn.y + rgn.height; y++) {
          wrData = wr.getPixels(rgn.x, y, w, 1, wrData);
          alphaData = r.getSamples(rgn.x, y, w, 1, 0, alphaData);
          int i = 0, a, b;
          // 4 is the most common case.
          // 2 is probably next most common...
          switch (bands) {
            case 2:
              for (int x = 0; x < alphaData.length; x++) {
                a = alphaData[x] & 0xFF;
                wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                ++i;
                wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                ++i;
              }
              break;
            case 4:
              for (int x = 0; x < alphaData.length; x++) {
                a = alphaData[x] & 0xFF;
                wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                ++i;
                wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                ++i;
                wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                ++i;
                wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                ++i;
              }
              break;
            default:
              for (int x = 0; x < alphaData.length; x++) {
                a = alphaData[x] & 0xFF;
                for (b = 0; b < bands; b++) {
                  wrData[i] = ((wrData[i] & 0xFF) * a) >> 8;
                  ++i;
                }
              }
          }
          wr.setPixels(rgn.x, y, w, 1, wrData);
        }
      } else {
        int b = srcRed.getSampleModel().getNumBands() - 1;
        for (int y = rgn.y; y < rgn.y + rgn.height; y++) {
          wrData = wr.getSamples(rgn.x, y, w, 1, b, wrData);
          alphaData = r.getSamples(rgn.x, y, w, 1, 0, alphaData);
          for (int i = 0; i < wrData.length; i++) {
            wrData[i] = ((wrData[i] & 0xFF) * (alphaData[i] & 0xFF)) >> 8;
          }
          wr.setSamples(rgn.x, y, w, 1, b, wrData);
        }
      }

      return wr;
    }

    // No alpha in source, so we hide the alpha channel in wr and
    // have our source fill wr with color info...
    int[] bands = new int[wr.getNumBands() - 1];
    for (int i = 0; i < bands.length; i++) bands[i] = i;

    WritableRaster subWr;
    subWr =
        wr.createWritableChild(
            wr.getMinX(),
            wr.getMinY(),
            wr.getWidth(),
            wr.getHeight(),
            wr.getMinX(),
            wr.getMinY(),
            bands);

    srcRed.copyData(subWr);

    Rectangle rgn = wr.getBounds();
    rgn = rgn.intersection(alphaRed.getBounds());

    bands = new int[] {wr.getNumBands() - 1};
    subWr = wr.createWritableChild(rgn.x, rgn.y, rgn.width, rgn.height, rgn.x, rgn.y, bands);
    alphaRed.copyData(subWr);

    return wr;
  }
Ejemplo n.º 8
0
  public void genRect(WritableRaster wr) {
    if (me2src == null) return;

    Rectangle srcR = me2src.createTransformedShape(wr.getBounds()).getBounds();

    // System.out.println("Affine wrR: " + wr.getBounds());
    // System.out.println("Affine srcR: " + srcR);

    // Outset by two pixels so we get context for interpolation...
    srcR.setBounds(srcR.x - 1, srcR.y - 1, srcR.width + 2, srcR.height + 2);

    // Don't try and get data from src that it doesn't have...
    CachableRed src = (CachableRed) getSources().get(0);

    // Raster srcRas = src.getData(srcR);

    if (!srcR.intersects(src.getBounds())) return;
    Raster srcRas = src.getData(srcR.intersection(src.getBounds()));

    if (srcRas == null) return;

    // This works around the problem that the buffered ops
    // completely ignore the coords of the Rasters passed in.
    AffineTransform aff = (AffineTransform) src2me.clone();

    // Translate what is at 0,0 (which will be what our current
    // minX/Y is) to our current minX,minY.
    aff.concatenate(AffineTransform.getTranslateInstance(srcRas.getMinX(), srcRas.getMinY()));

    Point2D srcPt = new Point2D.Float(wr.getMinX(), wr.getMinY());
    srcPt = me2src.transform(srcPt, null);

    Point2D destPt =
        new Point2D.Double(srcPt.getX() - srcRas.getMinX(), srcPt.getY() - srcRas.getMinY());

    destPt = aff.transform(destPt, null);

    // Translate what will be at minX,minY to zero, zero
    // which where java2d will think the real minX,minY is.
    aff.preConcatenate(AffineTransform.getTranslateInstance(-destPt.getX(), -destPt.getY()));

    AffineTransformOp op = new AffineTransformOp(aff, hints);

    BufferedImage srcBI, myBI;
    ColorModel srcCM = src.getColorModel();
    ColorModel myCM = getColorModel();

    WritableRaster srcWR = (WritableRaster) srcRas;
    // If the output buffer is not premultiplied in certain cases
    // it fails to properly divide out the Alpha (it always does
    // the affine on premultiplied data). We help it out by
    // premultiplying for it.
    srcCM = GraphicsUtil.coerceData(srcWR, srcCM, true);
    srcBI =
        new BufferedImage(
            srcCM, srcWR.createWritableTranslatedChild(0, 0), srcCM.isAlphaPremultiplied(), null);

    myBI =
        new BufferedImage(
            myCM, wr.createWritableTranslatedChild(0, 0), myCM.isAlphaPremultiplied(), null);

    op.filter(srcBI, myBI);

    // if ((count % 40) == 0) {
    //     org.apache.batik.ImageDisplay.showImage("Src: " , srcBI);
    //     org.apache.batik.ImageDisplay.showImage("Dst: " , myBI);
    // }
    // count++;
  }
Ejemplo n.º 9
0
  protected void handleReplicate(WritableRaster wr) {
    // Get my source.
    CachableRed src = (CachableRed) getSources().get(0);
    Rectangle srcR = src.getBounds();
    Rectangle wrR = wr.getBounds();

    int x = wrR.x;
    int y = wrR.y;
    int width = wrR.width;
    int height = wrR.height;

    Rectangle r;
    {
      // Calculate an intersection that makes some sense
      // even when the rects don't really intersect
      // (The x and y ranges will be correct if they
      // overlap in one dimension even if they don't
      // intersect in both dimensions).
      int minX = (srcR.x > x) ? srcR.x : x;
      int maxX =
          (((srcR.x + srcR.width - 1) < (x + width - 1))
              ? (srcR.x + srcR.width - 1)
              : (x + width - 1));
      int minY = (srcR.y > y) ? srcR.y : y;
      int maxY =
          (((srcR.y + srcR.height - 1) < (y + height - 1))
              ? (srcR.y + srcR.height - 1)
              : (y + height - 1));

      int x0 = minX;
      int w = maxX - minX + 1;
      int y0 = minY;
      int h = maxY - minY + 1;
      if (w < 0) {
        x0 = 0;
        w = 0;
      }
      if (h < 0) {
        y0 = 0;
        h = 0;
      }
      r = new Rectangle(x0, y0, w, h);
    }

    // We split the edge drawing up into four parts.
    //
    //  +-----------------------------+
    //  | 3    | 1             | 4    |
    //  |      +---------------+      |
    //  /      /               /      /
    //  /      / src           /      /
    //  /      /               /      /
    //  /      /               /      /
    //  |      +---------------+      |
    //  |      | 2             |      |
    //  +-----------------------------+
    //

    // Draw #1
    if (y < srcR.y) {
      int repW = r.width;
      int repX = r.x;
      int wrX = r.x;
      int wrY = y;
      if (x + width - 1 <= srcR.x) {
        // we are off to the left of src. so set repX to the
        // left most pixel...
        repW = 1;
        repX = srcR.x;
        wrX = x + width - 1;
      } else if (x >= srcR.x + srcR.width) {
        // we are off to the right of src, so set repX to
        // the right most pixel
        repW = 1;
        repX = srcR.x + srcR.width - 1;
        wrX = x;
      }

      // This fills the top row of section 1 from src (we
      // go to src instead of getting the data from wr because
      // in some cases wr will be completely off the top of src
      WritableRaster wr1 = wr.createWritableChild(wrX, wrY, repW, 1, repX, srcR.y, null);
      src.copyData(wr1);
      wrY++;

      int endY = srcR.y;
      if (y + height < endY) endY = y + height;

      if (wrY < endY) {
        int[] pixels = wr.getPixels(wrX, wrY - 1, repW, 1, (int[]) null);
        while (wrY < srcR.y) {
          wr.setPixels(wrX, wrY, repW, 1, pixels);
          wrY++;
        }
      }
    }

    // Draw #2
    if ((y + height) > (srcR.y + srcR.height)) {
      int repW = r.width;
      int repX = r.x;
      int repY = srcR.y + srcR.height - 1;

      int wrX = r.x;
      int wrY = srcR.y + srcR.height;
      if (wrY < y) wrY = y;

      if (x + width <= srcR.x) {
        // we are off to the left of src. so set repX to the
        // left most pixel...
        repW = 1;
        repX = srcR.x;
        wrX = x + width - 1;
      } else if (x >= srcR.x + srcR.width) {
        // we are off to the right of src, so set repX to
        // the right most pixel
        repW = 1;
        repX = srcR.x + srcR.width - 1;
        wrX = x;
      }

      if (DEBUG) {
        System.out.println("wr: " + wr.getBounds());
        System.out.println("req: [" + wrX + ", " + wrY + ", " + repW + ", 1]");
      }

      // First we get the top row of pixels from src. (we
      // go to src instead of getting the data from wr because
      // in some cases wr will be completely off the bottom of src).
      WritableRaster wr1 = wr.createWritableChild(wrX, wrY, repW, 1, repX, repY, null);
      // This fills the top row of section 2 from src
      src.copyData(wr1);
      wrY++;

      int endY = y + height;
      if (wrY < endY) {
        // This fills the rest of section 2 from the first line.
        int[] pixels = wr.getPixels(wrX, wrY - 1, repW, 1, (int[]) null);
        while (wrY < endY) {
          wr.setPixels(wrX, wrY, repW, 1, pixels);
          wrY++;
        }
      }
    }

    // Draw #3
    if (x < srcR.x) {
      // We are garunteed that we have a column of pixels down
      // the edge of 1 and src.  We simply replicate this column
      // out to the edges of 2.
      int wrX = srcR.x;
      if (x + width <= srcR.x) {
        wrX = x + width - 1;
      }

      int xLoc = x;
      int[] pixels = wr.getPixels(wrX, y, 1, height, (int[]) null);
      while (xLoc < wrX) {
        wr.setPixels(xLoc, y, 1, height, pixels);
        xLoc++;
      }
    }

    // Draw #4
    if (x + width > srcR.x + srcR.width) {
      // We are garunteed that we have a column of pixels down
      // the edge of 1 and src.  We simply replicate this column
      // out to the edges of 3.
      int wrX = srcR.x + srcR.width - 1;
      if (x >= srcR.x + srcR.width) {
        wrX = x;
      }

      int xLoc = wrX + 1;
      int endX = x + width - 1;
      int[] pixels = wr.getPixels(wrX, y, 1, height, (int[]) null);
      while (xLoc < endX) {
        wr.setPixels(xLoc, y, 1, height, pixels);
        xLoc++;
      }
    }
  }
Ejemplo n.º 10
0
  protected void handleZero(WritableRaster wr) {
    // Get my source.
    CachableRed src = (CachableRed) getSources().get(0);
    Rectangle srcR = src.getBounds();
    Rectangle wrR = wr.getBounds();

    ZeroRecter zr = ZeroRecter.getZeroRecter(wr);

    // area rect (covers the area left to handle).
    Rectangle ar = new Rectangle(wrR.x, wrR.y, wrR.width, wrR.height);
    // draw rect (used for calls to zeroRect);
    Rectangle dr = new Rectangle(wrR.x, wrR.y, wrR.width, wrR.height);

    // We split the edge drawing up into four parts.
    //
    //  +-----------------------------+
    //  | 1    | 2                    |
    //  |      +---------------+------|
    //  /      /               /4     /
    //  /      /               /      /
    //  /      /               /      /
    //  /      /               /      /
    //  |      +---------------+------|
    //  |      |  3                   |
    //  +-----------------------------+
    //
    //  We update our x,y, width, height as we go along so
    //  we 'forget' about the parts we have already painted...

    // Draw #1
    if (DEBUG) {
      System.out.println("WrR: " + wrR + " srcR: " + srcR);
      // g2d.setColor(new Color(255,0,0,128));
    }
    if (ar.x < srcR.x) {
      int w = srcR.x - ar.x;
      if (w > ar.width) w = ar.width;
      // g2d.fillRect(x, y, w, height);
      dr.width = w;
      zr.zeroRect(dr);

      ar.x += w;
      ar.width -= w;
    }

    // Draw #2
    if (DEBUG) {
      System.out.println(
          "WrR: [" + ar.x + "," + ar.y + "," + ar.width + "," + ar.height + "] s rcR: " + srcR);
      // g2d.setColor(new Color(0,0,255,128));
    }
    if (ar.y < srcR.y) {
      int h = srcR.y - ar.y;
      if (h > ar.height) h = ar.height;
      // g2d.fillRect(x, y, width, h);
      dr.x = ar.x;
      dr.y = ar.y;
      dr.width = ar.width;
      dr.height = h;
      zr.zeroRect(dr);

      ar.y += h;
      ar.height -= h;
    }

    // Draw #3
    if (DEBUG) {
      System.out.println(
          "WrR: [" + ar.x + "," + ar.y + "," + ar.width + "," + ar.height + "] srcR: " + srcR);
      // g2d.setColor(new Color(0,255,0,128));
    }
    if (ar.y + ar.height > srcR.y + srcR.height) {
      int h = (ar.y + ar.height) - (srcR.y + srcR.height);
      if (h > ar.height) h = ar.height;

      int y0 = ar.y + ar.height - h; // the +/-1 cancel (?)

      // g2d.fillRect(x, y0, width, h);
      dr.x = ar.x;
      dr.y = y0;
      dr.width = ar.width;
      dr.height = h;
      zr.zeroRect(dr);

      ar.height -= h;
    }

    // Draw #4
    if (DEBUG) {
      System.out.println(
          "WrR: [" + ar.x + "," + ar.y + "," + ar.width + "," + ar.height + "] srcR: " + srcR);
      // g2d.setColor(new Color(255,255,0,128));
    }
    if (ar.x + ar.width > srcR.x + srcR.width) {
      int w = (ar.x + ar.width) - (srcR.x + srcR.width);
      if (w > ar.width) w = ar.width;
      int x0 = ar.x + ar.width - w; // the +/-1 cancel (?)

      // g2d.fillRect(x0, y, w, height);
      dr.x = x0;
      dr.y = ar.y;
      dr.width = w;
      dr.height = ar.height;
      zr.zeroRect(dr);

      ar.width -= w;
    }
  }