1 | /*
|
---|
2 | * SVG Salamander
|
---|
3 | * Copyright (c) 2004, Mark McKay
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or
|
---|
7 | * without modification, are permitted provided that the following
|
---|
8 | * conditions are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above
|
---|
11 | * copyright notice, this list of conditions and the following
|
---|
12 | * disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above
|
---|
14 | * copyright notice, this list of conditions and the following
|
---|
15 | * disclaimer in the documentation and/or other materials
|
---|
16 | * provided with the distribution.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
---|
22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
---|
23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
---|
27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
---|
29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | *
|
---|
31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
32 | * projects can be found at http://www.kitfox.com
|
---|
33 | *
|
---|
34 | * Created on April 1, 2004, 3:37 AM
|
---|
35 | */
|
---|
36 |
|
---|
37 | package com.kitfox.svg.pattern;
|
---|
38 |
|
---|
39 | import com.kitfox.svg.SVGConst;
|
---|
40 | import java.awt.*;
|
---|
41 | import java.awt.geom.*;
|
---|
42 | import java.awt.image.*;
|
---|
43 | import java.util.logging.Level;
|
---|
44 | import java.util.logging.Logger;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * @author Mark McKay
|
---|
48 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
49 | */
|
---|
50 | public class PatternPaintContext implements PaintContext
|
---|
51 | {
|
---|
52 | BufferedImage source; //Image we're rendering from
|
---|
53 | Rectangle deviceBounds; //int size of rectangle we're rendering to
|
---|
54 | // AffineTransform userXform; //xform from user space to device space
|
---|
55 | // AffineTransform distortXform; //distortion applied to this pattern
|
---|
56 |
|
---|
57 | AffineTransform xform; //distortion applied to this pattern
|
---|
58 |
|
---|
59 | int sourceWidth;
|
---|
60 | int sourceHeight;
|
---|
61 |
|
---|
62 | //Raster we use to build tile
|
---|
63 | BufferedImage buf;
|
---|
64 |
|
---|
65 | /** Creates a new instance of PatternPaintContext */
|
---|
66 | public PatternPaintContext(BufferedImage source, Rectangle deviceBounds, AffineTransform userXform, AffineTransform distortXform)
|
---|
67 | {
|
---|
68 | //System.err.println("Bounds " + deviceBounds);
|
---|
69 | this.source = source;
|
---|
70 | this.deviceBounds = deviceBounds;
|
---|
71 | try {
|
---|
72 | // this.distortXform = distortXform.createInverse();
|
---|
73 | // this.userXform = userXform.createInverse();
|
---|
74 |
|
---|
75 | // xform = userXform.createInverse();
|
---|
76 | // xform.concatenate(distortXform.createInverse());
|
---|
77 | xform = distortXform.createInverse();
|
---|
78 | xform.concatenate(userXform.createInverse());
|
---|
79 | }
|
---|
80 | catch (Exception e)
|
---|
81 | {
|
---|
82 | Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);
|
---|
83 | }
|
---|
84 |
|
---|
85 | sourceWidth = source.getWidth();
|
---|
86 | sourceHeight = source.getHeight();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void dispose() {
|
---|
90 | }
|
---|
91 |
|
---|
92 | public ColorModel getColorModel() {
|
---|
93 | return source.getColorModel();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public Raster getRaster(int x, int y, int w, int h)
|
---|
97 | {
|
---|
98 | //System.err.println("" + x + ", " + y + ", " + w + ", " + h);
|
---|
99 | 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 | }
|
---|