001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.geom.*;
021import java.awt.image.*;
022
023/**
024 * A filter which composites two images together with an optional transform.
025 */
026public class CompositeFilter extends AbstractBufferedImageOp {
027
028        private Composite composite;
029    private AffineTransform transform;
030        
031        /**
032     * Construct a CompositeFilter.
033     */
034    public CompositeFilter() {
035        }
036        
037        /**
038     * Construct a CompositeFilter.
039     * @param composite the composite to use
040     */
041        public CompositeFilter( Composite composite ) {
042                this.composite = composite;
043        }
044        
045        /**
046     * Construct a CompositeFilter.
047     * @param composite the composite to use
048     * @param transform a transform for the composited image
049     */
050        public CompositeFilter( Composite composite, AffineTransform transform ) {
051                this.composite = composite;
052                this.transform = transform;
053        }
054        
055        /**
056     * Set the composite.
057     * @param composite the composite to use
058     * @see #getComposite
059     */
060        public void setComposite( Composite composite ) {
061                this.composite = composite;
062        }
063    
064        /**
065     * Get the composite.
066     * @return the composite to use
067     * @see #setComposite
068     */
069    public Composite getComposite() {
070        return composite;
071    }
072        
073        /**
074     * Set the transform.
075     * @param transform the transform to use
076     * @see #getTransform
077     */
078        public void setTransform( AffineTransform transform ) {
079                this.transform = transform;
080        }
081    
082        /**
083     * Get the transform.
084     * @return the transform to use
085     * @see #setTransform
086     */
087    public AffineTransform getTransform() {
088        return transform;
089    }
090        
091        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
092        if ( dst == null )
093            dst = createCompatibleDestImage( src, null );
094
095                Graphics2D g = dst.createGraphics();
096        g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
097        g.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
098        g.setComposite( composite );
099        g.drawRenderedImage( src, transform );
100        g.dispose();
101                return dst;
102        }
103
104        public String toString() {
105                return "Composite";
106        }
107}