3ba71b09ea2f9be8b43f61babf2105ffe48c8461
[org.ibex.core.git] / src / org / ibex / graphics / Paint.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 // FIXME
6 package org.ibex.graphics;
7 import java.util.*;
8
9 public abstract class Paint {
10     public abstract void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf);
11
12     public static class SingleColorPaint extends Paint {
13         public int color;
14         public SingleColorPaint(int color) { this.color = color; }
15         public void fillTrapezoid(float x1, float x2, float y1, float x3, float x4, float y2, PixelBuffer buf) {
16             buf.fillTrapezoid((int)Math.round(x1), (int)Math.round(x2), (int)Math.round(y1),
17                               (int)Math.round(x3), (int)Math.round(x4), (int)Math.round(y2), color);
18         }
19     }
20
21     public static class TexturePaint extends Paint {
22         Affine a, invert;
23         Picture p;
24         public TexturePaint(Picture p, Affine a) { this.p = p; this.a = a.copy(); this.invert = a.copy().invert(); }
25         public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
26             /*
27             float x1 = invert.multiply_px(tx1, ty1);
28             float x2 = invert.multiply_px(tx2, ty1);
29             float x3 = invert.multiply_px(tx3, ty2);
30             float x4 = invert.multiply_px(tx4, ty2);
31             float y1 = invert.multiply_py(tx1, ty1);
32             float y2 = invert.multiply_py(tx3, ty2);
33             */
34             //buf.paintTrapezoid((int)tx1, (int)tx2, (int)ty1, (int)tx3, (int)tx4, (int)ty2, p, a);
35         }
36     }
37
38     public static abstract class GradientPaint extends Paint {
39         public GradientPaint(boolean reflect, boolean repeat, Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
40             this.reflect = reflect;
41             this.repeat = repeat;
42             this.gradientTransform = gradientTransform;
43             this.stop_colors = stop_colors;
44             this.stop_offsets = stop_offsets;
45         }
46
47         Affine gradientTransform = Affine.identity();
48         boolean useBoundingBox = false;               // FIXME not supported
49         boolean patternUseBoundingBox = false;        // FIXME not supported
50         
51         // it's invalid for both of these to be true
52         boolean reflect = false;                   // FIXME not supported
53         boolean repeat = false;                    // FIXME not supported
54         int[] stop_colors;
55         float[] stop_offsets;
56         float cx, cy, r, fx, fy;
57         
58         public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
59             /*
60             Affine a = buf.a;
61             Affine inverse = a.copy().invert();
62             float slope1 = (tx3 - tx1) / (ty2 - ty1);
63             float slope2 = (tx4 - tx2) / (ty2 - ty1);
64             for(float y=ty1; y<ty2; y++) {
65                 float _x1 = (y - ty1) * slope1 + tx1;
66                 float _x2 = (y - ty1) * slope2 + tx2;
67                 if (_x1 > _x2) { float _x0 = _x1; _x1 = _x2; _x2 = _x0; }
68                 
69                 for(float x=_x1; x<_x2; x++) {
70                     
71                     float distance = isLinear ?
72                         // length of projection of <x,y> onto the gradient vector == {<x,y> \dot {grad \over |grad|}}
73                         (x * (x2 - x1) + y * (y2 - y1)) / (float)Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) :
74                         
75                         // radial form is simple! FIXME, not quite right
76                         (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
77                         
78                     // FIXME: offsets are 0..1, not 0..length(gradient)
79                     int i = 0; for(; i<stop_offsets.length; i++) if (distance < stop_offsets[i]) break;
80
81                     // FIXME: handle points beyond the bounds
82                     if (i < 0 || i >= stop_offsets.length) continue;
83
84                     // gradate from offsets[i - 1] to offsets[i]
85                     float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
86
87                     int a = (int)((((stop_colors[i] >> 24) & 0xff) - ((stop_colors[i - 1] >> 24) & 0xff)) * percentage) +
88                         ((stop_colors[i - 1] >> 24) & 0xff);
89                     int r = (int)((((stop_colors[i] >> 16) & 0xff) - ((stop_colors[i - 1] >> 16) & 0xff)) * percentage) +
90                         ((stop_colors[i - 1] >> 16) & 0xff);
91                     int g = (int)((((stop_colors[i] >> 8) & 0xff)  - ((stop_colors[i - 1] >> 8) & 0xff)) * percentage) +
92                         ((stop_colors[i - 1] >> 8) & 0xff);
93                     int b = (int)((((stop_colors[i] >> 0) & 0xff)  - ((stop_colors[i - 1] >> 0) & 0xff)) * percentage) +
94                         ((stop_colors[i - 1] >> 0) & 0xff);
95                     int argb = (a << 24) | (r << 16) | (g << 8) | b;
96                     buf.drawPoint((int)x, (int)Math.floor(y), argb);
97                 }
98             }
99             */
100         }
101     }
102     /*
103     public static class LinearGradientPaint extends GradientPaint {
104         public LinearGradientPaint(float x1, float y1, float x2, float y2, boolean reflect, boolean repeat,
105                                    Affine gradientTransform,  int[] stop_colors, float[] stop_offsets) {
106             super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
107             this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
108         }
109         float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
110     }
111
112     public static class RadialGradientPaint extends GradientPaint {
113         public RadialGradientPaint(float cx, float cy, float fx, float fy, float r, boolean reflect, boolean repeat,
114                                    Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
115             super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
116             this.cx = cx; this.cy = cy; this.fx = fx; this.fy = fy; this.r = r;
117         }
118             
119     }
120     */
121 }