001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on April 1, 2004, 3:37 AM
035 */
036
037package com.kitfox.svg.pattern;
038
039import com.kitfox.svg.SVGConst;
040import java.awt.*;
041import java.awt.geom.*;
042import java.awt.image.*;
043import java.util.logging.Level;
044import java.util.logging.Logger;
045
046/**
047 * @author Mark McKay
048 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
049 */
050public class PatternPaintContext implements PaintContext
051{
052    BufferedImage source;  //Image we're rendering from
053    Rectangle deviceBounds;  //int size of rectangle we're rendering to
054//    AffineTransform userXform;  //xform from user space to device space
055//    AffineTransform distortXform;  //distortion applied to this pattern
056
057    AffineTransform xform;  //distortion applied to this pattern
058
059    int sourceWidth;
060    int sourceHeight;
061
062    //Raster we use to build tile
063    BufferedImage buf;
064
065    /** Creates a new instance of PatternPaintContext */
066    public PatternPaintContext(BufferedImage source, Rectangle deviceBounds, AffineTransform userXform, AffineTransform distortXform)
067    {
068//System.err.println("Bounds " + deviceBounds);
069        this.source = source;
070        this.deviceBounds = deviceBounds;
071        try {
072//            this.distortXform = distortXform.createInverse();
073//            this.userXform = userXform.createInverse();
074
075//            xform = userXform.createInverse();
076//            xform.concatenate(distortXform.createInverse());
077            xform = distortXform.createInverse();
078            xform.concatenate(userXform.createInverse());
079        }
080        catch (Exception e) 
081        {
082            Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);
083        }
084
085        sourceWidth = source.getWidth();
086        sourceHeight = source.getHeight();
087    }
088
089    public void dispose() {
090    }
091
092    public ColorModel getColorModel() {
093        return source.getColorModel();
094    }
095
096    public Raster getRaster(int x, int y, int w, int h)
097    {
098//System.err.println("" + x + ", " + y + ", " + w + ", " + h);
099        if (buf == null || buf.getWidth() != w || buf.getHeight() != h)
100        {
101            buf = new BufferedImage(w, h, source.getType());
102        }
103
104//        Point2D.Float srcPt = new Point2D.Float(), srcPt2 = new Point2D.Float(), destPt = new Point2D.Float();
105        Point2D.Float srcPt = new Point2D.Float(), destPt = new Point2D.Float();
106        for (int j = 0; j < h; j++)
107        {
108            for (int i = 0; i < w; i++)
109            {
110                destPt.setLocation(i + x, j + y);
111
112                xform.transform(destPt, srcPt);
113
114//                userXform.transform(destPt, srcPt2);
115//                distortXform.transform(srcPt2, srcPt);
116
117                int ii = ((int)srcPt.x) % sourceWidth;
118                if (ii < 0) ii += sourceWidth;
119                int jj = ((int)srcPt.y) % sourceHeight;
120                if (jj < 0) jj += sourceHeight;
121
122                buf.setRGB(i, j, source.getRGB(ii, jj));
123            }
124        }
125
126        return buf.getData();
127    }
128
129    public static void main(String[] argv)
130    {
131        int i = -4;
132        System.err.println("Hello " + (i % 4));
133    }
134
135}