24c4b01fc0afc4e9c899cf0b01cf993f7557f02c
[org.ibex.core.git] / src / org / ibex / graphics / PixelBuffer.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 package org.ibex.graphics;
6 import org.ibex.util.*;
7
8 /**
9  *  <p>
10  *  A block of pixels which can be drawn on.
11  *  </p>
12  *
13  *  <p>
14  *  Implementations of the Platform class should return objects
15  *  supporting this interface from the _createPixelBuffer()
16  *  method. These implementations may choose to use off-screen video
17  *  ram for this purpose (for example, a Pixmap on X11).
18  *  </p>
19  */
20 public interface PixelBuffer {
21     public abstract void drawLine(int x1, int y1, int x2, int y2, int color);
22     public abstract void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
23     public abstract void drawPicture(Picture p, int x, int y, int x1, int y1, int w, int h);  // names may be wrong
24     public abstract void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb, int bg);
25     public abstract void stroke(Polygon p, int color);
26     public abstract void fill(Polygon p, Paint paint);
27 }
28
29
30
31
32 /*
33     // FEATURE: we want floats (inter-pixel spacing) for antialiasing, but this hoses the fastpath line drawing... argh!
34     // draws a line of width <tt>w</tt>; note that the coordinates here are <i>post-transform</i> 
35     public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
36
37         if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
38
39         if (x1 == x2) {
40             fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
41                           x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
42             return;
43         }
44
45         // fastpath for single-pixel width lines
46         if (w == 1) {
47             float slope = (float)(y2 - y1) / (float)(x2 - x1);
48             int last_x = x1;
49             for(int y=y1; y<=y2; y++) {
50                 int new_x = (int)((float)(y - y1) / slope) + x1;
51                 if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
52                                               last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
53                 else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
54                                    y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
55                 last_x = new_x;
56             }
57             return;
58         }
59
60         // actually half-width
61         float width = (float)w / 2;
62         float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
63         if (phi < 0.0) phi += (float)Math.PI * 2;
64         float theta = (float)Math.PI / 2 - phi;
65
66         // dx and dy are the x and y distance between each endpoint and the corner of the stroke
67         int dx = (int)(width * Math.cos(theta));
68         int dy = (int)(width * Math.sin(theta));
69
70         // slice is the longest possible length of a horizontal line across the stroke
71         int slice = (int)(2 * width / Math.cos(theta));
72
73         if (capped) {
74             x1 -= width * Math.cos(phi);
75             x2 += width * Math.cos(phi);
76             y1 -= width * Math.sin(phi);
77             y2 += width * Math.sin(phi);
78         }
79
80         fillTrapezoid(x1 + dx, x1 + dx, y1 - dy, x1 - dx, x1 - dx + slice, y1 + dy, color);           // top corner
81         fillTrapezoid(x2 + dx - slice, x2 + dx, y2 - dy, x2 - dx, x2 - dx, y2 + dy, color);           // bottom corner
82         fillTrapezoid(x1 - dx, x1 - dx + slice, y1 + dy, x2 + dx - slice, x2 + dx, y2 - dy, color);   // middle
83     }
84
85 }
86 */