1 /* Copyright (C) 2000 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.gcj.xlib; 10 11 import gnu.gcj.RawData; 12 13 /** 14 * Structure containing image data that resides on the client side. 15 * The format, depth and offset attributes of an XImage determines how 16 * bitfields are encoded in a raster image. However, it does not 17 * determine how a color is encoded into a bitfield. I.e. the XImage 18 * pixel values in a specific structure, but does not determine what 19 * colors that will be used to represent these pixel values on the 20 * screen. 21 * 22 * @author Rolf W. Rasmussen <rolfwr@ii.uib.no> 23 */ 24 public class XImage 25 { 26 /** This object reference points to the data, hindering garbage 27 collection of the data. */ 28 Object dataRef; 29 30 // Must match definitions in X.h: 31 public static final int XYBITMAP_FORMAT = 0, 32 XYPIXMAP_FORMAT = 1, 33 ZPIXMAP_FORMAT = 2; 34 35 // Must match definitions in X.h: 36 public static final int LEAST_SIGNIFICANT_B_FIRST_ORDER = 0, 37 MOST_SIGNIFICANT_B_FIRST_ORDER = 1; 38 XImage(Visual visual, int depth, int format, int xoffset, int width, int height, int bitmapPad, int bytesPerLine)39 public XImage(Visual visual, int depth, int format, int xoffset, 40 int width, int height, int bitmapPad, 41 int bytesPerLine) 42 { 43 this(visual, depth, format, xoffset, width, height, bitmapPad, 44 bytesPerLine, 45 0 // bitsPerPixel 46 ); 47 } 48 XImage(Visual visual, int depth, int format, int xoffset, int width, int height, int bitmapPad, int bytesPerLine, int bitsPerPixel)49 public XImage(Visual visual, int depth, int format, int xoffset, 50 int width, int height, int bitmapPad, 51 int bytesPerLine, int bitsPerPixel) 52 { 53 if (visual == null) throw new 54 NullPointerException("a visual must be specified"); 55 56 init(visual, depth, format, xoffset, width, height, 57 bitmapPad, bytesPerLine, bitsPerPixel); 58 } 59 init(Visual visual, int depth, int format, int xoffset, int width, int height, int bitmapPad, int bytesPerLine, int bitsPerPixel)60 public native void init(Visual visual, int depth, int format, int xoffset, 61 int width, int height, int bitmapPad, 62 int bytesPerLine, int bitsPerPixel); 63 init(Visual visual, int width, int height)64 private native void init(Visual visual, int width, int height); 65 66 XImage(Visual visual, int width, int height)67 public XImage(Visual visual, int width, int height) 68 { 69 this(visual, width, height, 70 true // Automatically allocate memory 71 ); 72 } 73 74 /** 75 * Create a new XImage. 76 * 77 * @param allocate specifies whether to automatically allocate 78 * memory for the image. It is possible to create the data array 79 * elsewhere, so that we can for instance use a DataBufferUShort as 80 * data. Ie. not limit ourself to byte arrays. This is done by 81 * passing false and calling a setData() method manually after 82 * creation. 83 */ XImage(Visual visual, int width, int height, boolean allocate)84 public XImage(Visual visual, int width, int height, boolean allocate) 85 { 86 if (visual == null) 87 throw new NullPointerException("a visual must be specified"); 88 89 init(visual, width, height); 90 91 if (allocate) 92 { 93 /* Now that Xlib has figured out the appropriate bytes per 94 line, we can allocate memory for the image. */ 95 // FIXME: What about formats with several layers/bands? 96 byte[] data = new byte[getBytesPerLine()*height]; 97 98 setData(data, 0); 99 } 100 } 101 102 /** 103 * Attach image data to this XImage. 104 * 105 * @param offset the index of the first actual data element in the array. 106 */ setData(byte[] data, int offset)107 public void setData(byte[] data, int offset) 108 { 109 dataRef = data; 110 internalSetData(data, offset); 111 } 112 113 /** 114 * Attach image data to this XImage. 115 * 116 * @param offset the index of the first actual data element in the 117 * array. Note: this is short offset, not a byte offset. 118 */ setData(short[] data, int offset)119 public void setData(short[] data, int offset) 120 { 121 dataRef = data; 122 internalSetData(data, offset); 123 } 124 125 /** 126 * Attach image data to this XImage 127 * 128 * @param offset the index of the first actual data element in the array. 129 * Note: this is not a byte offset. 130 */ setData(int[] data, int offset)131 public void setData(int[] data, int offset) 132 { 133 dataRef = data; 134 internalSetData(data, offset); 135 } 136 internalSetData(byte[] data, int offset)137 private native void internalSetData(byte[] data, int offset); internalSetData(short[] data, int offset)138 private native void internalSetData(short[] data, int offset); internalSetData(int[] data, int offset)139 private native void internalSetData(int[] data, int offset); 140 finalize()141 protected native void finalize(); 142 143 boolean ownsData = false; 144 RawData structure = null; 145 getWidth()146 public final native int getWidth(); getHeight()147 public final native int getHeight(); getDepth()148 public final native int getDepth(); getFormat()149 public final native int getFormat(); 150 isZPixmapFormat()151 public final boolean isZPixmapFormat() 152 { 153 return getFormat() == ZPIXMAP_FORMAT; 154 } 155 156 157 /** 158 * Get the xoffset. The xoffset avoids the need of shifting the 159 * scanlines into place. 160 */ getXOffset()161 public final native int getXOffset(); 162 getBytesPerLine()163 public native final int getBytesPerLine(); getBitsPerPixel()164 public native final int getBitsPerPixel(); 165 getImageByteOrder()166 public native final int getImageByteOrder(); getBitmapBitOrder()167 public native final int getBitmapBitOrder(); getBitmapUnit()168 public native final int getBitmapUnit(); getBitmapPad()169 public native final int getBitmapPad(); 170 171 172 // True/Direct Color specific: getRedMask()173 public native int getRedMask(); getGreenMask()174 public native int getGreenMask(); getBlueMask()175 public native int getBlueMask(); 176 177 178 /** 179 * Set a pixel value at a given position in the image. This method 180 * is slow. Don't use it, except as a fall-back. 181 */ setPixel(int x, int y, int pixel)182 public native final void setPixel(int x, int y, int pixel); 183 toString()184 public String toString() 185 { 186 String format; 187 switch(getFormat()) 188 { 189 case ZPIXMAP_FORMAT: 190 format = "ZPixmapFormat"; 191 break; 192 default: 193 format = "unknown"; 194 } 195 196 String imageByteOrder; 197 switch(getImageByteOrder()) 198 { 199 case LEAST_SIGNIFICANT_B_FIRST_ORDER: 200 imageByteOrder = "leastSignificantByteFirst"; 201 break; 202 case MOST_SIGNIFICANT_B_FIRST_ORDER: 203 imageByteOrder = "mostSignificantByteFirst"; 204 break; 205 default: 206 imageByteOrder = "unknwon"; 207 } 208 209 String bitmapBitOrder; 210 switch(getBitmapBitOrder()) 211 { 212 case LEAST_SIGNIFICANT_B_FIRST_ORDER: 213 bitmapBitOrder = "leastSignificantBitFirst"; 214 break; 215 case MOST_SIGNIFICANT_B_FIRST_ORDER: 216 bitmapBitOrder = "mostSignificantBitFirst"; 217 break; 218 default: 219 bitmapBitOrder = "unknown"; 220 } 221 222 return getClass().getName() + "[" + format + 223 ", width=" + getWidth() + 224 ", height=" + getHeight() + 225 ", bytesPerLine=" + getBytesPerLine() + 226 ", xoffset=" + getXOffset() + 227 ", depth=" + getDepth() + 228 ", bitsPerPixel=" + getBitsPerPixel() + 229 ", bitmapUnit=" + getBitmapUnit() + 230 ", bitmapPad=" + getBitmapPad() + 231 ", byteOrder=" + imageByteOrder + 232 ", bitOrder=" + bitmapBitOrder + 233 "]"; 234 } 235 } 236