001    /*
002     * AdobeCompositeContext.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on April 1, 2004, 6:41 AM
026     */
027    
028    package com.kitfox.svg.composite;
029    
030    import java.awt.*;
031    import java.awt.image.*;
032    
033    /**
034     * @author Mark McKay
035     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
036     */
037    public class AdobeCompositeContext implements CompositeContext
038    {
039        final int compositeType;
040        final float extraAlpha;
041    
042        float[] rgba_src = new float[4];
043        float[] rgba_dstIn = new float[4];
044        float[] rgba_dstOut = new float[4];
045    
046        /** Creates a new instance of AdobeCompositeContext */
047        public AdobeCompositeContext(int compositeType, float extraAlpha)
048        {
049            this.compositeType = compositeType;
050            this.extraAlpha = extraAlpha;
051    
052            rgba_dstOut[3] = 1f;
053        }
054    
055        public void compose(Raster src, Raster dstIn, WritableRaster dstOut)
056        {
057            int width = src.getWidth();
058            int height = src.getHeight();
059    
060            for (int j = 0; j < height; j++)
061            {
062                for (int i = 0; i < width; i++)
063                {
064                    src.getPixel(i, j, rgba_src);
065                    dstIn.getPixel(i, j, rgba_dstIn);
066    
067                    //Ignore transparent pixels
068                    if (rgba_src[3] == 0)
069                    {
070    //                    dstOut.setPixel(i, j, rgba_dstIn);
071                        continue;
072                    }
073    
074                    float alpha = rgba_src[3];
075    
076                    switch (compositeType)
077                    {
078                        default:
079                        case AdobeComposite.CT_NORMAL:
080                            rgba_dstOut[0] = rgba_src[0] * alpha + rgba_dstIn[0] * (1f - alpha);
081                            rgba_dstOut[1] = rgba_src[1] * alpha + rgba_dstIn[1] * (1f - alpha);
082                            rgba_dstOut[2] = rgba_src[2] * alpha + rgba_dstIn[2] * (1f - alpha);
083                            break;
084                        case AdobeComposite.CT_MULTIPLY:
085                            rgba_dstOut[0] = rgba_src[0] * rgba_dstIn[0] * alpha + rgba_dstIn[0] * (1f - alpha);
086                            rgba_dstOut[1] = rgba_src[1] * rgba_dstIn[1] * alpha + rgba_dstIn[1] * (1f - alpha);
087                            rgba_dstOut[2] = rgba_src[2] * rgba_dstIn[2] * alpha + rgba_dstIn[2] * (1f - alpha);
088                            break;
089                    }
090                }
091            }
092        }
093    
094        public void dispose() {
095        }
096    
097    }