| 1 | /* Copyright (C) 2000, 2003 Free Software Foundation
|
|---|
| 2 |
|
|---|
| 3 | This file is part of libgcj.
|
|---|
| 4 |
|
|---|
| 5 | This software is copyrighted work licensed under the terms of the
|
|---|
| 6 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 7 | details. */
|
|---|
| 8 |
|
|---|
| 9 | package gnu.awt.xlib;
|
|---|
| 10 |
|
|---|
| 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;
|
|---|
| 32 |
|
|---|
| 33 | public 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;
|
|---|
|
|---|