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

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

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