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
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();
43 public XGraphicsConfiguration(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);
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();
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
,
102 else if (imageCM
instanceof PackedColorModel
)
104 PackedColorModel pcm
= (PackedColorModel
) imageCM
;
105 int[] masks
= pcm
.getMasks();
107 imageSM
= new SinglePixelPackedSampleModel(dataType
,
115 throw new UnsupportedOperationException("creating sample model " +
120 WritableRaster raster
= Raster
.createWritableRaster(imageSM
, origin
);
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
);
142 case DataBuffer
.TYPE_USHORT
:
143 ximage
.setData(((DataBufferUShort
) dataB
).getData(), offset
);
145 case DataBuffer
.TYPE_INT
:
146 ximage
.setData(((DataBufferInt
) dataB
).getData(), offset
);
150 new UnsupportedOperationException("Do not know how to " +
151 "set data for data " +
153 dataB
.getDataType());
157 void prepareColorModel(XImage ximage
)
159 format
= ximage
.getFormat();
160 int bitsPerPixel
= ximage
.getBitsPerPixel();
162 case XImage
.ZPIXMAP_FORMAT
:
163 calcZPixmapModels(bitsPerPixel
);
167 throw new UnsupportedOperationException("unimplemented format");
171 void calcZPixmapModels(int bitsPerPixel
)
173 switch (visual
.getVisualClass())
175 case Visual
.VC_TRUE_COLOR
:
176 calcDecomposedRGBModels(bitsPerPixel
);
178 case Visual
.VC_PSEUDO_COLOR
:
179 calcPseudoColorModels(bitsPerPixel
);
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
,
199 visual
.getGreenMask(),
200 visual
.getBlueMask(),
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.
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
,
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)
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()
292 void preparePixelCM()
294 switch (visual
.getVisualClass())
296 case Visual
.VC_TRUE_COLOR
:
297 pixelCM
= new DirectColorModel(visual
.getDepth(),
299 visual
.getGreenMask(),
300 visual
.getBlueMask());
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
);
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");
360 /* FIXME: This should be moved to XGraphicsDevice... */
361 XFontMetrics
getXFontMetrics(java
.awt
.Font awtFont
)
363 // FIXME: do caching...
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";
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.
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);