source: trunk/src/gcc/libjava/gnu/awt/xlib/XGraphicsConfiguration.java@ 736

Last change on this file since 736 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 9.6 KB
Line 
1/* Copyright (C) 2000 Free Software Foundation
2
3 This file is part of libgcj.
4
5This software is copyrighted work licensed under the terms of the
6Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7details. */
8
9package gnu.awt.xlib;
10
11import java.awt.GraphicsConfiguration;
12import java.awt.Rectangle;
13import java.awt.Graphics2D;
14import java.awt.Graphics;
15import java.awt.Point;
16import java.awt.Color;
17import java.awt.color.ColorSpace;
18import java.awt.image.*;
19import java.awt.geom.AffineTransform;
20import gnu.gcj.xlib.GC;
21import gnu.gcj.xlib.Drawable;
22import gnu.gcj.xlib.Window;
23import gnu.gcj.xlib.XImage;
24import gnu.gcj.xlib.Visual;
25import gnu.gcj.xlib.Colormap;
26import gnu.gcj.xlib.XColor;
27import gnu.gcj.xlib.Screen;
28import gnu.gcj.xlib.Display;
29import gnu.java.awt.Buffers;
30import java.util.Hashtable;
31
32public class XGraphicsConfiguration extends GraphicsConfiguration
33{
34 //public abstract GraphicsDevice getDevice();
35
36 Visual visual;
37 int format;
38 Colormap colormap;
39 ColorModel imageCM;
40 ColorModel pixelCM;
41
42 public XGraphicsConfiguration(Visual visual)
43 {
44 this.visual = visual;
45 }
46
47 public BufferedImage createCompatibleImage(int width, int height)
48 {
49 XImage ximg = new XImage(visual, width, height,
50 false // do not auto allocate memory
51 );
52
53 Point origin = new Point(0, 0);
54 WritableRaster raster = createRasterForXImage(ximg, origin);
55
56 /* This is not a good way of doing this. Multiple toolkits may
57 want to share the BufferedImage. */
58 Hashtable props = new Hashtable();
59 props.put("gnu.gcj.xlib.XImage", ximg);
60 props.put("java.awt.GraphicsConfiguration", this);
61
62 BufferedImage bimg = new BufferedImage(imageCM,raster, false, props);
63
64 DataBuffer dataB = raster.getDataBuffer();
65 attachData(ximg, dataB, 0);
66 return bimg;
67 }
68
69 WritableRaster createRasterForXImage(XImage ximage, Point origin)
70 {
71 if (imageCM == null) prepareColorModel(ximage);
72
73 /*
74 This will not work, since it creates a sample model that
75 does not necessarily match the format of the XImage.
76
77 WritableRaster raster =
78 imageCM.createCompatibleWritableRaster(width, height); */
79
80 // Create a sample model matching the XImage:
81
82 SampleModel imageSM = null;
83
84 int width = ximage.getWidth();
85 int height = ximage.getHeight();
86 int bitsPerPixel = ximage.getBitsPerPixel();
87 int dataType =
88 Buffers.smallestAppropriateTransferType(bitsPerPixel);
89 int bitsPerDataElement = DataBuffer.getDataTypeSize(dataType);
90 int scanlineStride = ximage.getBytesPerLine()*8/bitsPerDataElement;
91
92 if (imageCM instanceof IndexColorModel)
93 {
94 int[] bandOffsets = {0};
95 imageSM = new ComponentSampleModel(dataType,
96 width, height,
97 1, // pixel stride
98 scanlineStride,
99 bandOffsets);
100 }
101 else if (imageCM instanceof PackedColorModel)
102 {
103 PackedColorModel pcm = (PackedColorModel) imageCM;
104 int[] masks = pcm.getMasks();
105
106 imageSM = new SinglePixelPackedSampleModel(dataType,
107 width, height,
108 scanlineStride,
109 masks);
110 }
111
112 if (imageSM == null)
113 {
114 throw new UnsupportedOperationException("creating sample model " +
115 "for " + imageCM +
116 " not implemented");
117 }
118
119 WritableRaster raster = Raster.createWritableRaster(imageSM, origin);
120 return raster;
121 }
122
123
124
125 /**
126 * Attach a the memory of a data buffer to an XImage
127 * structure. [This method is not gnu.awt.xlib specific, and should
128 * maybe be moved to a different location.]
129 *
130 * @param offset Offset to data. The given offset does not include
131 * data buffer offset, which will also be added. */
132 static void attachData(XImage ximage, DataBuffer dataB, int offset)
133 {
134 offset += dataB.getOffset();
135 switch (dataB.getDataType())
136 {
137 case DataBuffer.TYPE_BYTE:
138 ximage.setData(((DataBufferByte) dataB).getData(), offset);
139 break;
140 case DataBuffer.TYPE_USHORT:
141 ximage.setData(((DataBufferUShort) dataB).getData(), offset);
142 break;
143 case DataBuffer.TYPE_INT:
144 ximage.setData(((DataBufferInt) dataB).getData(), offset);
145 break;
146 default:
147 throw
148 new UnsupportedOperationException("Do not know how to " +
149 "set data for data " +
150 "type " +
151 dataB.getDataType());
152 }
153 }
154
155 void prepareColorModel(XImage ximage)
156 {
157 format = ximage.getFormat();
158 int bitsPerPixel = ximage.getBitsPerPixel();
159 switch (format) {
160 case XImage.ZPIXMAP_FORMAT:
161 calcZPixmapModels(bitsPerPixel);
162 break;
163
164 default:
165 throw new UnsupportedOperationException("unimplemented format");
166 }
167 }
168
169 void calcZPixmapModels(int bitsPerPixel)
170 {
171 switch (visual.getVisualClass())
172 {
173 case Visual.VC_TRUE_COLOR:
174 calcDecomposedRGBModels(bitsPerPixel);
175 break;
176 case Visual.VC_PSEUDO_COLOR:
177 calcPseudoColorModels(bitsPerPixel);
178 break;
179 default:
180 String msg = "unimplemented visual class";
181 throw new UnsupportedOperationException(msg);
182 }
183 }
184
185 void calcDecomposedRGBModels(int bitsPerPixel)
186 {
187 int dataType = Buffers.smallestAppropriateTransferType(bitsPerPixel);
188
189
190 if (DataBuffer.getDataTypeSize(dataType) == bitsPerPixel)
191 {
192 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
193
194 imageCM = new DirectColorModel(cs,
195 visual.getDepth(),
196 visual.getRedMask(),
197 visual.getGreenMask(),
198 visual.getBlueMask(),
199 0, // no alpha
200 false,
201 dataType);
202 }
203 else
204 {
205 throw new
206 UnsupportedOperationException("unimplemented bits per pixel");
207 }
208 }
209
210 void calcPseudoColorModels(int bitsPerPixel)
211 {
212 if (colormap == null)
213 colormap = visual.getScreen().getDefaultColormap();
214
215 XColor[] colArray = colormap.getXColors();
216
217 int numCol = colArray.length;
218 byte[] rmap = new byte[numCol];
219 byte[] gmap = new byte[numCol];
220 byte[] bmap = new byte[numCol];
221 byte[] amap = new byte[numCol];
222
223 for (int i=0; i < numCol; i++)
224 {
225 XColor color = colArray[i];
226 if (color.getFlags() == Colormap.FLAG_SHARED)
227 {
228 rmap[i] = (byte) (color.getRed() >> 8);
229 gmap[i] = (byte) (color.getGreen() >> 8);
230 bmap[i] = (byte) (color.getBlue() >> 8);
231 amap[i] = (byte) 0xff;
232 } // else, leave default zero values...
233 }
234
235 imageCM = new IndexColorModel(visual.getDepth(), numCol,
236 rmap, gmap, bmap, amap);
237 }
238
239 public BufferedImage createCompatibleImage(int width,
240 int height,
241 int transparency)
242 {
243 throw new UnsupportedOperationException("not implemented");
244 }
245
246 /**
247 * FIXME: I'm not sure which color model that should be returned here.
248 */
249 public ColorModel getColorModel()
250 {
251 if (pixelCM == null)
252 preparePixelCM();
253 return pixelCM;
254 }
255
256 void preparePixelCM()
257 {
258 switch (visual.getVisualClass())
259 {
260 case Visual.VC_TRUE_COLOR:
261 pixelCM = new DirectColorModel(visual.getDepth(),
262 visual.getRedMask(),
263 visual.getGreenMask(),
264 visual.getBlueMask());
265 break;
266 case Visual.VC_PSEUDO_COLOR:
267
268 if (colormap == null)
269 colormap = visual.getScreen().getDefaultColormap();
270
271 XColor[] colArray = colormap.getXColors();
272
273 int numCol = colArray.length;
274 byte[] rmap = new byte[numCol];
275 byte[] gmap = new byte[numCol];
276 byte[] bmap = new byte[numCol];
277 byte[] amap = new byte[numCol];
278
279 for (int i=0; i < numCol; i++)
280 {
281 XColor color = colArray[i];
282 if (color.getFlags() == Colormap.FLAG_SHARED) {
283 rmap[i] = (byte) (color.getRed() >> 8);
284 gmap[i] = (byte) (color.getGreen() >> 8);
285 bmap[i] = (byte) (color.getBlue() >> 8);
286 amap[i] = (byte) 0xff;
287 } // else, leave default zero values...
288
289 }
290
291 pixelCM = new IndexColorModel(visual.getDepth(), numCol,
292 rmap, gmap, bmap, amap);
293 break;
294 default:
295 throw new UnsupportedOperationException("not implemented");
296 }
297 }
298
299 public ColorModel getColorModel(int transparency)
300 {
301 throw new UnsupportedOperationException("not implemented");
302 }
303
304 public AffineTransform getDefaultTransform()
305 {
306 throw new UnsupportedOperationException("not implemented");
307 }
308
309 public AffineTransform getNormalizingTransform()
310 {
311 throw new UnsupportedOperationException("not implemented");
312 }
313
314 public Rectangle getBounds()
315 {
316 throw new UnsupportedOperationException("not implemented");
317 }
318
319 Visual getVisual()
320 {
321 return visual;
322 }
323
324 /* FIXME: This should be moved to XGraphicsDevice... */
325 XFontMetrics getXFontMetrics(java.awt.Font awtFont)
326 {
327 // FIXME: do caching...
328
329 String family = "*";
330 String name = awtFont.getName();
331 String weight = awtFont.isBold() ? "bold" : "medium";
332 String slant = awtFont.isItalic() ? "i" : "r";
333 String addStyle = "*";
334 String pixelSize = "*";
335 String pointSize = awtFont.getSize() + "0";
336 String xres = "*";
337 String yres = "*";
338 String spacing = "*";
339 String averageWidth = "*";
340 String charset = "*";
341
342 String logicalFontDescription =
343 family + "-" + name + "-" + weight + "-" +
344 slant + "-" + addStyle + "-" + pixelSize + "-" +
345 pointSize + "-" + xres + "-" + yres + "-" +
346 spacing + "-" + averageWidth + "-" + charset;
347
348 Display display = visual.getScreen().getDisplay();
349 gnu.gcj.xlib.Font xfont =
350 new gnu.gcj.xlib.Font(display, logicalFontDescription);
351 return new XFontMetrics(xfont, awtFont);
352 }
353
354 int getPixel(Color color)
355 {
356 int[] components =
357 {
358 color.getRed(),
359 color.getGreen(),
360 color.getBlue(),
361 0xff
362 };
363
364 ColorModel cm = getColorModel();
365 return cm.getDataElement(components, 0);
366 }
367}
Note: See TracBrowser for help on using the repository browser.