Exemplo n.º 1
0
  /**
   * Loads an image from a URL
   *
   * @param bitmapFile the bitmap file
   * @return the bitmap as BufferedImage
   */
  public static BufferedImage getImage(File bitmapFile) {
    try {
      InputStream in = new java.io.FileInputStream(bitmapFile);
      try {
        in = new java.io.BufferedInputStream(in);

        ImageTagRegistry reg = ImageTagRegistry.getRegistry();
        Filter filt = reg.readStream(in);
        if (filt == null) {
          return null;
        }

        RenderedImage red = filt.createDefaultRendering();
        if (red == null) {
          return null;
        }

        BufferedImage img =
            new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
        red.copyData(img.getRaster());
        return img;
      } finally {
        IOUtils.closeQuietly(in);
      }
    } catch (IOException e) {
      return null;
    }
  }
Exemplo n.º 2
0
  public RenderedImage createRendering(RenderContext rc) {
    //
    // Get the mask content
    //
    Filter maskSrc = getMaskNode().getGraphicsNodeRable(true);
    PadRable maskPad = new PadRable8Bit(maskSrc, getBounds2D(), PadMode.ZERO_PAD);
    maskSrc = new FilterAsAlphaRable(maskPad);
    RenderedImage ri = maskSrc.createRendering(rc);
    if (ri == null) return null;

    CachableRed maskCr = RenderedImageCachableRed.wrap(ri);

    //
    // Get the masked content
    //
    PadRable maskedPad = new PadRable8Bit(getSource(), getBounds2D(), PadMode.ZERO_PAD);

    ri = maskedPad.createRendering(rc);
    if (ri == null) return null;

    CachableRed cr;
    cr = GraphicsUtil.wrap(ri);
    cr = GraphicsUtil.convertToLsRGB(cr);

    // org.apache.batik.test.gvt.ImageDisplay.showImage("Src: ", cr);
    // org.apache.batik.test.gvt.ImageDisplay.showImage("Mask: ", maskCr);

    CachableRed ret = new MultiplyAlphaRed(cr, maskCr);

    // org.apache.batik.test.gvt.ImageDisplay.showImage("Masked: ", ret);

    // ret = new PadRed(ret, cr.getBounds(), PadMode.ZERO_PAD, rh);

    return ret;
  }
  /**
   * Creates a <code>Filter</code> primitive according to the specified parameters.
   *
   * @param ctx the bridge context to use
   * @param filterElement the element that defines a filter
   * @param filteredElement the element that references the filter
   * @param filteredNode the graphics node to filter
   * @param inputFilter the <code>Filter</code> that represents the current filter input if the
   *     filter chain.
   * @param filterRegion the filter area defined for the filter chain the new node will be part of.
   * @param filterMap a map where the mediator can map a name to the <code>Filter</code> it creates.
   *     Other <code>FilterBridge</code>s can then access a filter node from the filterMap if they
   *     know its name.
   */
  public Filter createFilter(
      BridgeContext ctx,
      Element filterElement,
      Element filteredElement,
      GraphicsNode filteredNode,
      Filter inputFilter,
      Rectangle2D filterRegion,
      Map filterMap) {

    // 'scale' attribute - default is 0
    float scale = convertNumber(filterElement, SVG_SCALE_ATTRIBUTE, 0, ctx);

    // 'xChannelSelector' attribute - default is 'A'
    ARGBChannel xChannelSelector =
        convertChannelSelector(filterElement, SVG_X_CHANNEL_SELECTOR_ATTRIBUTE, ARGBChannel.A, ctx);

    // 'yChannelSelector' attribute - default is 'A'
    ARGBChannel yChannelSelector =
        convertChannelSelector(filterElement, SVG_Y_CHANNEL_SELECTOR_ATTRIBUTE, ARGBChannel.A, ctx);

    // 'in' attribute
    Filter in = getIn(filterElement, filteredElement, filteredNode, inputFilter, filterMap, ctx);
    if (in == null) {
      return null; // disable the filter
    }

    // 'in2' attribute - required
    Filter in2 = getIn2(filterElement, filteredElement, filteredNode, inputFilter, filterMap, ctx);
    if (in2 == null) {
      return null; // disable the filter
    }

    Rectangle2D defaultRegion;
    defaultRegion = (Rectangle2D) in.getBounds2D().clone();
    defaultRegion.add(in2.getBounds2D());
    // get filter primitive chain region
    Rectangle2D primitiveRegion =
        SVGUtilities.convertFilterPrimitiveRegion(
            filterElement, filteredElement, filteredNode, defaultRegion, filterRegion, ctx);

    PadRable pad = new PadRable8Bit(in, primitiveRegion, PadMode.ZERO_PAD);

    // build the displacement map filter
    List srcs = new ArrayList(2);
    srcs.add(pad);
    srcs.add(in2);
    Filter displacementMap =
        new DisplacementMapRable8Bit(srcs, scale, xChannelSelector, yChannelSelector);

    // handle the 'color-interpolation-filters' property
    handleColorInterpolationFilters(displacementMap, filterElement);

    PadRable filter = new PadRable8Bit(displacementMap, primitiveRegion, PadMode.ZERO_PAD);

    // update the filter Map
    updateFilterMap(filterElement, filter, filterMap);

    return filter;
  }
  /**
   * Creates a <code>Filter</code> primitive according to the specified parameters.
   *
   * @param ctx the bridge context to use
   * @param filterElement the element that defines a filter
   * @param filteredElement the element that references the filter
   * @param filteredNode the graphics node to filter
   * @param inputFilter the <code>Filter</code> that represents the current filter input if the
   *     filter chain.
   * @param filterRegion the filter area defined for the filter chain the new node will be part of.
   * @param filterMap a map where the mediator can map a name to the <code>Filter</code> it creates.
   *     Other <code>FilterBridge</code>s can then access a filter node from the filterMap if they
   *     know its name.
   */
  public Filter createFilter(
      BridgeContext ctx,
      Element filterElement,
      Element filteredElement,
      GraphicsNode filteredNode,
      Filter inputFilter,
      Rectangle2D filterRegion,
      Map filterMap) {

    // 'surfaceScale' attribute - default is 1
    float surfaceScale = convertNumber(filterElement, SVG_SURFACE_SCALE_ATTRIBUTE, 1, ctx);

    // 'specularConstant' attribute - default is 1
    float specularConstant = convertNumber(filterElement, SVG_SPECULAR_CONSTANT_ATTRIBUTE, 1, ctx);

    // 'specularExponent' attribute - default is 1
    float specularExponent = convertSpecularExponent(filterElement, ctx);

    // extract the light definition from the filterElement's children list
    Light light = extractLight(filterElement, ctx);

    // 'kernelUnitLength' attribute
    double[] kernelUnitLength = convertKernelUnitLength(filterElement, ctx);

    // 'in' attribute
    Filter in = getIn(filterElement, filteredElement, filteredNode, inputFilter, filterMap, ctx);
    if (in == null) {
      return null; // disable the filter
    }

    // Default region is the size of in (if in is SourceGraphic or
    // SourceAlpha it will already include a pad/crop to the
    // proper filter region size).
    Rectangle2D defaultRegion = in.getBounds2D();
    Rectangle2D primitiveRegion =
        SVGUtilities.convertFilterPrimitiveRegion(
            filterElement, filteredElement, filteredNode, defaultRegion, filterRegion, ctx);

    Filter filter =
        new SpecularLightingRable8Bit(
            in,
            primitiveRegion,
            light,
            specularConstant,
            specularExponent,
            surfaceScale,
            kernelUnitLength);

    // handle the 'color-interpolation-filters' property
    handleColorInterpolationFilters(filter, filterElement);

    // update the filter Map
    updateFilterMap(filterElement, filter, filterMap);

    return filter;
  }
Exemplo n.º 5
0
  /**
   * Returns a Filter that represents an raster image (JPG or PNG).
   *
   * @param ctx the bridge context
   * @param primitiveRegion the primitive region
   * @param purl the url of the image
   */
  protected static Filter createRasterFeImage(
      BridgeContext ctx, Rectangle2D primitiveRegion, ParsedURL purl) {

    // Need to fit the raster image to the filter region so that
    // we have the same behavior as raster images in the <image> element.
    Filter filter = ImageTagRegistry.getRegistry().readURL(purl);

    Rectangle2D bounds = filter.getBounds2D();
    AffineTransform scale = new AffineTransform();
    scale.translate(primitiveRegion.getX(), primitiveRegion.getY());
    scale.scale(
        primitiveRegion.getWidth() / (bounds.getWidth() - 1),
        primitiveRegion.getHeight() / (bounds.getHeight() - 1));
    scale.translate(-bounds.getX(), -bounds.getY());

    return new AffineRable8Bit(filter, scale);
  }
Exemplo n.º 6
0
  /**
   * Loads an image from a URL
   *
   * @param url the URL to the image
   * @return the bitmap as BufferedImage TODO This method doesn't close the InputStream opened by
   *     the URL.
   */
  public static BufferedImage getImage(URL url) {
    ImageTagRegistry reg = ImageTagRegistry.getRegistry();
    Filter filt = reg.readURL(new ParsedURL(url));
    if (filt == null) {
      return null;
    }

    RenderedImage red = filt.createDefaultRendering();
    if (red == null) {
      return null;
    }

    BufferedImage img =
        new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
    red.copyData(img.getRaster());

    return img;
  }
Exemplo n.º 7
0
  /**
   * This returns the dirty region for gn in the coordinate system given by <code>at</code>.
   *
   * @param gn Node tree to return dirty region for.
   * @param at Affine transform to coordinate space to accumulate dirty regions in.
   */
  public Rectangle2D getNodeDirtyRegion(GraphicsNode gn, AffineTransform at) {
    WeakReference gnWRef = gn.getWeakReference();
    AffineTransform nat = (AffineTransform) dirtyNodes.get(gnWRef);
    if (nat == null) nat = gn.getTransform();
    if (nat != null) {
      at = new AffineTransform(at);
      at.concatenate(nat);
    }

    Filter f = gn.getFilter();
    Rectangle2D ret = null;
    if (gn instanceof CompositeGraphicsNode) {
      CompositeGraphicsNode cgn = (CompositeGraphicsNode) gn;
      Iterator iter = cgn.iterator();

      while (iter.hasNext()) {
        GraphicsNode childGN = (GraphicsNode) iter.next();
        Rectangle2D r2d = getNodeDirtyRegion(childGN, at);
        if (r2d != null) {
          if (f != null) {
            // If we have a filter and a change region
            // Update our full filter extents.
            Shape s = at.createTransformedShape(f.getBounds2D());
            ret = s.getBounds2D();
            break;
          }
          if ((ret == null) || (ret == NULL_RECT)) ret = r2d;
          else ret = ret.createUnion(r2d);
        }
      }
    } else {
      ret = (Rectangle2D) fromBounds.remove(gnWRef);
      if (ret == null) {
        if (f != null) ret = f.getBounds2D();
        else ret = gn.getBounds();
      } else if (ret == NULL_RECT) ret = null;
      if (ret != null) ret = at.createTransformedShape(ret).getBounds2D();
    }
    return ret;
  }
Exemplo n.º 8
0
  /** Returns the list of dirty areas on GVT. */
  public List getDirtyAreas() {
    if (dirtyNodes == null) return null;

    List ret = new LinkedList();
    Set keys = dirtyNodes.keySet();
    Iterator i = keys.iterator();
    while (i.hasNext()) {
      WeakReference gnWRef = (WeakReference) i.next();
      GraphicsNode gn = (GraphicsNode) gnWRef.get();
      // GraphicsNode  srcGN  = gn;

      // if the weak ref has been cleared then this node is no
      // longer part of the GVT tree (and the change should be
      // reflected in some ancestor that should also be in the
      // dirty list).
      if (gn == null) continue;

      AffineTransform oat;
      oat = (AffineTransform) dirtyNodes.get(gnWRef);
      if (oat != null) {
        oat = new AffineTransform(oat);
      }

      Rectangle2D srcORgn = (Rectangle2D) fromBounds.remove(gnWRef);

      Rectangle2D srcNRgn = null;
      AffineTransform nat = null;
      if (!(srcORgn instanceof ChngSrcRect)) {
        // For change srcs don't use the new bounds of parent node.
        srcNRgn = gn.getBounds();
        nat = gn.getTransform();
        if (nat != null) nat = new AffineTransform(nat);
      }

      // System.out.println("Rgns: " + srcORgn + " - " + srcNRgn);
      // System.out.println("ATs: " + oat + " - " + nat);
      do {
        // f.invalidateCache(oRng);
        // f.invalidateCache(nRng);

        // f = gn.getEnableBackgroundGraphicsNodeRable(false);
        // (need to push rgn through filter chain if any...)
        // f.invalidateCache(oRng);
        // f.invalidateCache(nRng);

        gn = gn.getParent();
        if (gn == null) break; // We reached the top of the tree

        Filter f = gn.getFilter();
        if (f != null) {
          srcNRgn = f.getBounds2D();
          nat = null;
        }

        // Get the parent's current Affine
        AffineTransform at = gn.getTransform();
        // Get the parent's Affine last time we rendered.
        gnWRef = gn.getWeakReference();
        AffineTransform poat = (AffineTransform) dirtyNodes.get(gnWRef);
        if (poat == null) poat = at;
        if (poat != null) {
          if (oat != null) oat.preConcatenate(poat);
          else oat = new AffineTransform(poat);
        }

        if (at != null) {
          if (nat != null) nat.preConcatenate(at);
          else nat = new AffineTransform(at);
        }
      } while (true);

      if (gn == null) {
        // We made it to the root graphics node so add them.
        // System.out.println
        //      ("Adding: " + oat + " - " + nat + "\n" +
        //       srcORgn + "\n" + srcNRgn + "\n");
        // <!>
        Shape oRgn = srcORgn;
        if ((oRgn != null) && (oRgn != NULL_RECT)) {
          if (oat != null) oRgn = oat.createTransformedShape(srcORgn);
          // System.err.println("GN: " + srcGN);
          // System.err.println("Src: " + oRgn.getBounds2D());
          ret.add(oRgn);
        }

        if (srcNRgn != null) {
          Shape nRgn = srcNRgn;
          if (nat != null) nRgn = nat.createTransformedShape(srcNRgn);
          if (nRgn != null) ret.add(nRgn);
        }
      }
    }

    fromBounds.clear();
    dirtyNodes.clear();
    return ret;
  }