001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image.renderable;
020:
021: import java.awt.Rectangle;
022: import java.awt.RenderingHints;
023: import java.awt.Shape;
024: import java.awt.geom.AffineTransform;
025: import java.awt.geom.Rectangle2D;
026: import java.awt.image.RenderedImage;
027: import java.awt.image.renderable.RenderContext;
028:
029: import org.apache.batik.ext.awt.image.rendered.AffineRed;
030: import org.apache.batik.ext.awt.image.rendered.CachableRed;
031: import org.apache.batik.ext.awt.image.rendered.TranslateRed;
032:
033: /**
034: * RasterRable This is used to wrap a Rendered Image back into the
035: * RenderableImage world.
036: *
037: * @author <a href="mailto:Thomas.DeWeese@Kodak.com">Thomas DeWeese</a>
038: * @version $Id: RedRable.java 478276 2006-11-22 18:33:37Z dvholten $
039: */
040: public class RedRable extends AbstractRable {
041: CachableRed src;
042:
043: public RedRable(CachableRed src) {
044: super ((Filter) null);
045: this .src = src;
046: }
047:
048: public CachableRed getSource() {
049: return src;
050: }
051:
052: public Object getProperty(String name) {
053: return src.getProperty(name);
054: }
055:
056: public String[] getPropertyNames() {
057: return src.getPropertyNames();
058: }
059:
060: public Rectangle2D getBounds2D() {
061: return getSource().getBounds();
062: }
063:
064: public RenderedImage createDefaultRendering() {
065: return getSource();
066: }
067:
068: public RenderedImage createRendering(RenderContext rc) {
069: // System.out.println("RedRable Create Rendering: " + this);
070:
071: // Just copy over the rendering hints.
072: RenderingHints rh = rc.getRenderingHints();
073: if (rh == null)
074: rh = new RenderingHints(null);
075:
076: Shape aoi = rc.getAreaOfInterest();
077: Rectangle aoiR;
078: if (aoi != null)
079: aoiR = aoi.getBounds();
080: else
081: aoiR = getBounds2D().getBounds();
082:
083: // get the current affine transform
084: AffineTransform at = rc.getTransform();
085:
086: // For high quality output we should really apply a Gaussian
087: // Blur when we are scaling the image down significantly this
088: // helps to prevent aliasing in the result image.
089: CachableRed cr = getSource();
090:
091: if (!aoiR.intersects(cr.getBounds()))
092: return null;
093:
094: if (at.isIdentity()) {
095: // System.out.println("Using as is");
096: return cr;
097: }
098:
099: if ((at.getScaleX() == 1.0) && (at.getScaleY() == 1.0)
100: && (at.getShearX() == 0.0) && (at.getShearY() == 0.0)) {
101: int xloc = (int) (cr.getMinX() + at.getTranslateX());
102: int yloc = (int) (cr.getMinY() + at.getTranslateY());
103: double dx = xloc - (cr.getMinX() + at.getTranslateX());
104: double dy = yloc - (cr.getMinY() + at.getTranslateY());
105: if (((dx > -0.0001) && (dx < 0.0001))
106: && ((dy > -0.0001) && (dy < 0.0001))) {
107: // System.out.println("Using TranslateRed");
108: return new TranslateRed(cr, xloc, yloc);
109: }
110: }
111:
112: // System.out.println("Using Full affine: " + at);
113: return new AffineRed(cr, at, rh);
114: }
115: }
|