FSF GCC merge 02/23/03
[official-gcc.git] / libjava / gnu / awt / xlib / XGraphicsConfiguration.java
blob7d7d5159fe0ccde8d2aff8542dc8429048b0434a
1 /* Copyright (C) 2000, 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.awt.xlib;
11 import java.awt.GraphicsConfiguration;
12 import java.awt.Rectangle;
13 import java.awt.Graphics2D;
14 import java.awt.Graphics;
15 import java.awt.GraphicsDevice;
16 import java.awt.Point;
17 import java.awt.Color;
18 import java.awt.color.ColorSpace;
19 import java.awt.image.*;
20 import java.awt.geom.AffineTransform;
21 import gnu.gcj.xlib.GC;
22 import gnu.gcj.xlib.Drawable;
23 import gnu.gcj.xlib.Window;
24 import gnu.gcj.xlib.XImage;
25 import gnu.gcj.xlib.Visual;
26 import gnu.gcj.xlib.Colormap;
27 import gnu.gcj.xlib.XColor;
28 import gnu.gcj.xlib.Screen;
29 import gnu.gcj.xlib.Display;
30 import gnu.java.awt.Buffers;
31 import java.util.Hashtable;
33 public class XGraphicsConfiguration extends GraphicsConfiguration
35 //public abstract GraphicsDevice getDevice();
37 Visual visual;
38 int format;
39 Colormap colormap;
40 ColorModel imageCM;
41 ColorModel pixelCM;
43 public XGraphicsConfiguration(Visual visual)
45 this.visual = visual;
48 public BufferedImage createCompatibleImage(int width, int height)
50 XImage ximg = new XImage(visual, width, height,
51 false // do not auto allocate memory
54 Point origin = new Point(0, 0);
55 WritableRaster raster = createRasterForXImage(ximg, origin);
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);
63 BufferedImage bimg = new BufferedImage(imageCM,raster, false, props);
65 DataBuffer dataB = raster.getDataBuffer();
66 attachData(ximg, dataB, 0);
67 return bimg;
70 WritableRaster createRasterForXImage(XImage ximage, Point origin)
72 if (imageCM == null) prepareColorModel(ximage);
75 This will not work, since it creates a sample model that
76 does not necessarily match the format of the XImage.
78 WritableRaster raster =
79 imageCM.createCompatibleWritableRaster(width, height); */
81 // Create a sample model matching the XImage:
83 SampleModel imageSM = null;
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;
93 if (imageCM instanceof IndexColorModel)
95 int[] bandOffsets = {0};
96 imageSM = new ComponentSampleModel(dataType,
97 width, height,
98 1, // pixel stride
99 scanlineStride,
100 bandOffsets);
102 else if (imageCM instanceof PackedColorModel)
104 PackedColorModel pcm = (PackedColorModel) imageCM;
105 int[] masks = pcm.getMasks();
107 imageSM = new SinglePixelPackedSampleModel(dataType,
108 width, height,
109 scanlineStride,
110 masks);
113 if (imageSM == null)
115 throw new UnsupportedOperationException("creating sample model " +
116 "for " + imageCM +
117 " not implemented");
120 WritableRaster raster = Raster.createWritableRaster(imageSM, origin);
121 return raster;
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.]
131 * @param offset Offset to data. The given offset does not include
132 * data buffer offset, which will also be added.
134 static void attachData(XImage ximage, DataBuffer dataB, int offset)
136 offset += dataB.getOffset();
137 switch (dataB.getDataType())
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());
157 void prepareColorModel(XImage ximage)
159 format = ximage.getFormat();
160 int bitsPerPixel = ximage.getBitsPerPixel();
161 switch (format) {
162 case XImage.ZPIXMAP_FORMAT:
163 calcZPixmapModels(bitsPerPixel);
164 break;
166 default:
167 throw new UnsupportedOperationException("unimplemented format");
171 void calcZPixmapModels(int bitsPerPixel)
173 switch (visual.getVisualClass())
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);
187 void calcDecomposedRGBModels(int bitsPerPixel)
189 int dataType = Buffers.smallestAppropriateTransferType(bitsPerPixel);
192 if (DataBuffer.getDataTypeSize(dataType) == bitsPerPixel)
194 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
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);
205 else
207 throw new
208 UnsupportedOperationException("unimplemented bits per pixel");
212 void calcPseudoColorModels(int bitsPerPixel)
214 if (colormap == null)
215 colormap = visual.getScreen().getDefaultColormap();
217 XColor[] colArray = colormap.getXColors();
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];
225 for (int i=0; i < numCol; i++)
227 XColor color = colArray[i];
228 if (color.getFlags() == Colormap.FLAG_SHARED)
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...
237 imageCM = new IndexColorModel(visual.getDepth(), numCol,
238 rmap, gmap, bmap, amap);
242 * Gets the associated device that this configuration describes.
244 * @return the device
246 public GraphicsDevice getDevice()
248 throw new UnsupportedOperationException("not implemented");
252 * Returns a buffered image optimized to this device, so that blitting can
253 * be supported in the buffered image.
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
259 public BufferedImage createCompatibleImage(int width,
260 int height,
261 int transparency)
263 throw new UnsupportedOperationException("not implemented");
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.
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
277 public VolatileImage createCompatibleVolatileImage(int w, int h)
279 throw new UnsupportedOperationException("not implemented");
283 * FIXME: I'm not sure which color model that should be returned here.
285 public ColorModel getColorModel()
287 if (pixelCM == null)
288 preparePixelCM();
289 return pixelCM;
292 void preparePixelCM()
294 switch (visual.getVisualClass())
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:
304 if (colormap == null)
305 colormap = visual.getScreen().getDefaultColormap();
307 XColor[] colArray = colormap.getXColors();
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];
315 for (int i=0; i < numCol; i++)
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...
327 pixelCM = new IndexColorModel(visual.getDepth(), numCol,
328 rmap, gmap, bmap, amap);
329 break;
330 default:
331 throw new UnsupportedOperationException("not implemented");
335 public ColorModel getColorModel(int transparency)
337 throw new UnsupportedOperationException("not implemented");
340 public AffineTransform getDefaultTransform()
342 throw new UnsupportedOperationException("not implemented");
345 public AffineTransform getNormalizingTransform()
347 throw new UnsupportedOperationException("not implemented");
350 public Rectangle getBounds()
352 throw new UnsupportedOperationException("not implemented");
355 Visual getVisual()
357 return visual;
360 /* FIXME: This should be moved to XGraphicsDevice... */
361 XFontMetrics getXFontMetrics(java.awt.Font awtFont)
363 // FIXME: do caching...
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 = "*";
378 String logicalFontDescription =
379 family + "-" + name + "-" + weight + "-" +
380 slant + "-" + addStyle + "-" + pixelSize + "-" +
381 pointSize + "-" + xres + "-" + yres + "-" +
382 spacing + "-" + averageWidth + "-" + charset;
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);
390 int getPixel(Color color)
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 =
400 color.getRed (),
401 color.getGreen (),
402 color.getBlue (),
403 0xff
407 float[] normalizedComponents =
409 ((float)color.getRed ()) / 255F,
410 ((float)color.getGreen ()) / 255F,
411 ((float)color.getBlue ()) / 255F,
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);