1 | /*
|
---|
2 | * PatternPaintContext.java
|
---|
3 | *
|
---|
4 | *
|
---|
5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
---|
6 | * Copyright (C) 2004 Mark McKay
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Lesser General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2.1 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Lesser General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Lesser General Public
|
---|
19 | * License along with this library; if not, write to the Free Software
|
---|
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
21 | *
|
---|
22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
---|
23 | * projects can be found at http://www.kitfox.com
|
---|
24 | *
|
---|
25 | * Created on April 1, 2004, 3:37 AM
|
---|
26 | */
|
---|
27 |
|
---|
28 | package com.kitfox.svg.pattern;
|
---|
29 |
|
---|
30 | import java.awt.*;
|
---|
31 | import java.awt.geom.*;
|
---|
32 | import java.awt.image.*;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @author Mark McKay
|
---|
36 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
---|
37 | */
|
---|
38 | public class PatternPaintContext implements PaintContext
|
---|
39 | {
|
---|
40 | BufferedImage source; //Image we're rendering from
|
---|
41 | Rectangle deviceBounds; //int size of rectangle we're rendering to
|
---|
42 | // AffineTransform userXform; //xform from user space to device space
|
---|
43 | // AffineTransform distortXform; //distortion applied to this pattern
|
---|
44 |
|
---|
45 | AffineTransform xform; //distortion applied to this pattern
|
---|
46 |
|
---|
47 | int sourceWidth;
|
---|
48 | int sourceHeight;
|
---|
49 |
|
---|
50 | //Raster we use to build tile
|
---|
51 | BufferedImage buf;
|
---|
52 |
|
---|
53 | /** Creates a new instance of PatternPaintContext */
|
---|
54 | public PatternPaintContext(BufferedImage source, Rectangle deviceBounds, AffineTransform userXform, AffineTransform distortXform)
|
---|
55 | {
|
---|
56 | //System.err.println("Bounds " + deviceBounds);
|
---|
57 | this.source = source;
|
---|
58 | this.deviceBounds = deviceBounds;
|
---|
59 | try {
|
---|
60 | // this.distortXform = distortXform.createInverse();
|
---|
61 | // this.userXform = userXform.createInverse();
|
---|
62 |
|
---|
63 | // xform = userXform.createInverse();
|
---|
64 | // xform.concatenate(distortXform.createInverse());
|
---|
65 | xform = distortXform.createInverse();
|
---|
66 | xform.concatenate(userXform.createInverse());
|
---|
67 | }
|
---|
68 | catch (Exception e) { e.printStackTrace(); }
|
---|
69 |
|
---|
70 | sourceWidth = source.getWidth();
|
---|
71 | sourceHeight = source.getHeight();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void dispose() {
|
---|
75 | }
|
---|
76 |
|
---|
77 | public ColorModel getColorModel() {
|
---|
78 | return source.getColorModel();
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Raster getRaster(int x, int y, int w, int h)
|
---|
82 | {
|
---|
83 | //System.err.println("" + x + ", " + y + ", " + w + ", " + h);
|
---|
84 | if (buf == null || buf.getWidth() != w || buf.getHeight() != buf.getHeight())
|
---|
85 | {
|
---|
86 | buf = new BufferedImage(w, h, source.getType());
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Point2D.Float srcPt = new Point2D.Float(), srcPt2 = new Point2D.Float(), destPt = new Point2D.Float();
|
---|
90 | Point2D.Float srcPt = new Point2D.Float(), destPt = new Point2D.Float();
|
---|
91 | for (int j = 0; j < h; j++)
|
---|
92 | {
|
---|
93 | for (int i = 0; i < w; i++)
|
---|
94 | {
|
---|
95 | destPt.setLocation(i + x, j + y);
|
---|
96 |
|
---|
97 | xform.transform(destPt, srcPt);
|
---|
98 |
|
---|
99 | // userXform.transform(destPt, srcPt2);
|
---|
100 | // distortXform.transform(srcPt2, srcPt);
|
---|
101 |
|
---|
102 | int ii = ((int)srcPt.x) % sourceWidth;
|
---|
103 | if (ii < 0) ii += sourceWidth;
|
---|
104 | int jj = ((int)srcPt.y) % sourceHeight;
|
---|
105 | if (jj < 0) jj += sourceHeight;
|
---|
106 |
|
---|
107 | buf.setRGB(i, j, source.getRGB(ii, jj));
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return buf.getData();
|
---|
112 | }
|
---|
113 |
|
---|
114 | public static void main(String[] argv)
|
---|
115 | {
|
---|
116 | int i = -4;
|
---|
117 | System.err.println("Hello " + (i % 4));
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|