XOffScreenImage.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Copyright (C) 2000, 2003 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.awt.xlib;
  7. import java.awt.Image;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.GraphicsConfiguration;
  11. import java.awt.image.ColorModel;
  12. import java.awt.image.ImageObserver;
  13. import java.awt.image.ImageProducer;
  14. import java.awt.image.ImageConsumer;
  15. import java.util.Hashtable;
  16. import gnu.awt.j2d.DirectRasterGraphics;
  17. import gnu.awt.j2d.Graphics2DImpl;
  18. import gnu.awt.j2d.IntegerGraphicsState;
  19. import gnu.gcj.xlib.Drawable;
  20. import gnu.gcj.xlib.Pixmap;
  21. import gnu.gcj.xlib.Screen;
  22. import gnu.gcj.xlib.Visual;
  23. import gnu.gcj.xlib.GC;
  24. /** Image class for xlib off-screen buffers.
  25. * The image is stored in a server-side pixmap for best performance.
  26. * This class supports getGraphics, so you can draw on the pixmap, and is
  27. * specially handled when doing drawImage, so that the image copy is done
  28. * entirely in the X server.
  29. * This class does not support rasterization, for which you'd need an XImage.
  30. *
  31. * @author scott gilbertson <scottg@mantatest.com> <sgilbertson@cogeco.ca>
  32. */
  33. public class XOffScreenImage extends Image
  34. implements IntegerGraphicsState.ScreenCoupledImage,
  35. ImageConsumer
  36. {
  37. private Pixmap pixmap;
  38. private XGraphicsConfiguration config;
  39. private int width;
  40. private int height;
  41. private Drawable drawable;
  42. private ImageProducer prod;
  43. private GC gc;
  44. private ColorModel pixmapColorModel;
  45. /** Create a new XOffScreenImage
  46. * @param config Graphics configuration, to compare against on-screen
  47. * components and to create the appropriate Graphics
  48. * @param drawable The drawable with which the image is compatible
  49. * @param width The width of the image
  50. * @param height The height of the image
  51. * @param cm The ColorModel associated with drawable
  52. */
  53. XOffScreenImage (XGraphicsConfiguration config, Drawable drawable, int width, int height, ColorModel cm)
  54. {
  55. this.config = config;
  56. this.width = width;
  57. this.height = height;
  58. this.drawable = drawable;
  59. pixmapColorModel = cm;
  60. pixmap = new Pixmap (drawable, width, height, drawable.getDepth ());
  61. gc = GC.create (pixmap);
  62. }
  63. /** Create a new XOffScreenImage and obtain image data from an ImageProducer
  64. * @param config Graphics configuration, to compare against on-screen
  65. * components and to create the appropriate Graphics
  66. * @param drawable The drawable with which the image is compatible
  67. * @param prod The source of image data for this image
  68. * @param cm The ColorModel associated with drawable
  69. */
  70. XOffScreenImage (XGraphicsConfiguration config, Drawable drawable, ImageProducer prod, ColorModel cm)
  71. {
  72. this.config = config;
  73. this.width = 0; // size will be overridden in a moment
  74. this.height = 0;
  75. this.drawable = drawable;
  76. this.prod = prod;
  77. pixmapColorModel = cm;
  78. prod.startProduction (this);
  79. }
  80. /** Get the pixmap which contains this image
  81. * @return The pixmap
  82. */
  83. public Pixmap getPixmap ()
  84. {
  85. return pixmap;
  86. }
  87. /** Flushes (that is, destroys) any resources used for this image. This
  88. * includes the actual image data.
  89. */
  90. public void flush ()
  91. {
  92. // FIXME: should dispose pixmap
  93. pixmap = null;
  94. }
  95. /** Returns a graphics context object for drawing an off-screen object.
  96. * This method is only valid for off-screen objects.
  97. *
  98. * @return a graphics context object for an off-screen object
  99. * @see Graphics#createImage(int, int)
  100. */
  101. public Graphics getGraphics ()
  102. {
  103. DirectRasterGraphics gfxDevice = new XGraphics (pixmap, config);
  104. IntegerGraphicsState igState = new IntegerGraphicsState (gfxDevice);
  105. Graphics2DImpl gfx2d = new Graphics2DImpl (config);
  106. gfx2d.setState (igState);
  107. return gfx2d;
  108. }
  109. /** Returns the height of the image, or -1 if it is unknown. If the
  110. * image height is unknown, the observer object will be notified when
  111. * the value is known.
  112. *
  113. * @param observer the image observer for this object
  114. * @return the height in pixels
  115. * @see #getWidth(ImageObserver)
  116. */
  117. public int getHeight (ImageObserver observer)
  118. {
  119. return height;
  120. }
  121. /** Returns the height of the image, or -1 if it is unknown. If the
  122. * image height is unknown, the observer object will be notified when
  123. * the value is known.
  124. *
  125. * @return the height in pixels
  126. * @see #getWidth()
  127. */
  128. public int getHeight ()
  129. {
  130. return height;
  131. }
  132. /** Returns the image producer object for this object. The producer is the
  133. * object which generates pixels for this image.
  134. *
  135. * @return the image producer for this object
  136. */
  137. public ImageProducer getSource ()
  138. {
  139. if (prod == null)
  140. throw new UnsupportedOperationException ("getSource not supported");
  141. else
  142. return prod;
  143. }
  144. /** Returns the width of the image, or -1 if it is unknown. If the
  145. * image width is unknown, the observer object will be notified when
  146. * the value is known.
  147. *
  148. * @param observer the image observer for this object
  149. * @return the width in pixels
  150. * @see #getHeight(ImageObserver)
  151. */
  152. public int getWidth (ImageObserver observer)
  153. {
  154. return width;
  155. }
  156. /** Returns the width of the image, or -1 if it is unknown. If the
  157. * image width is unknown, the observer object will be notified when
  158. * the value is known.
  159. *
  160. * @return the width in pixels
  161. * @see #getHeight()
  162. */
  163. public int getWidth ()
  164. {
  165. return width;
  166. }
  167. /** This method requests a named property for an object. The value of the
  168. * property is returned. The value <code>UndefinedProperty</code> is
  169. * returned if there is no property with the specified name. The value
  170. * <code>null</code> is returned if the properties for the object are
  171. * not yet known. In this case, the specified image observer is notified
  172. * when the properties are known.
  173. *
  174. * @param name the requested property name
  175. * @param observer the image observer for this object
  176. * @return the named property, if available
  177. * @see #UndefinedProperty
  178. */
  179. public Object getProperty (String name, ImageObserver observer)
  180. {
  181. return null;
  182. }
  183. /** Get the GraphicsConfiguration to which this image is coupled
  184. * @return the GraphicsConfiguration
  185. */
  186. public GraphicsConfiguration getGraphicsConfiguration ()
  187. {
  188. return config;
  189. }
  190. public void imageComplete (int status)
  191. {
  192. }
  193. public void setColorModel (ColorModel model)
  194. {
  195. }
  196. public void setDimensions (int width, int height)
  197. {
  198. this.width = width;
  199. this.height = height;
  200. pixmap = new Pixmap (drawable, width, height, drawable.getDepth ());
  201. gc = GC.create (pixmap);
  202. }
  203. public void setHints (int flags)
  204. {
  205. }
  206. public void setPixels (int x, int y, int w, int h, ColorModel model, int[] pixels, int offset, int scansize)
  207. {
  208. int idx = 0;
  209. float[] normalizedComponents = new float [4];
  210. int[] unnormalizedComponents = { 0, 0, 0, 0xff };
  211. normalizedComponents[3] = 1;
  212. for (int yp=y; yp < (y + h); yp++)
  213. {
  214. for (int xp=x; xp < (x + w); xp++)
  215. {
  216. int p = (yp - y) * scansize + (xp - x) + offset;
  217. // FIXME: there HAS to be a more efficient mechanism for color mapping
  218. normalizedComponents[0] = (float)model.getRed (pixels[p]) / 255F;
  219. normalizedComponents[1] = (float)model.getGreen (pixels[p]) / 255F;
  220. normalizedComponents[2] = (float)model.getBlue (pixels[p]) / 255F;
  221. pixmapColorModel.getUnnormalizedComponents (normalizedComponents, 0,
  222. unnormalizedComponents, 0);
  223. int pixelColor = pixmapColorModel.getDataElement (unnormalizedComponents, 0);
  224. gc.setForeground (pixelColor);
  225. gc.drawPoint (xp, yp);
  226. }
  227. }
  228. }
  229. public void setPixels (int x, int y, int w, int h, ColorModel model, byte[] pixels, int offset, int scansize)
  230. {
  231. int idx = 0;
  232. float[] normalizedComponents = new float [4];
  233. int[] unnormalizedComponents = { 0, 0, 0, 0xff };
  234. normalizedComponents[3] = 1;
  235. for (int yp=y; yp < (y + h); yp++)
  236. {
  237. for (int xp=x; xp < (x + w); xp++)
  238. {
  239. // FIXME: there HAS to be a more efficient mechanism for color mapping
  240. int p = (yp - y) * scansize + (xp - x) + offset;
  241. normalizedComponents[0] = (float)model.getRed (pixels[p]) / 255F;
  242. normalizedComponents[1] = (float)model.getGreen (pixels[p]) / 255F;
  243. normalizedComponents[2] = (float)model.getBlue (pixels[p]) / 255F;
  244. pixmapColorModel.getUnnormalizedComponents (normalizedComponents, 0,
  245. unnormalizedComponents, 0);
  246. int pixelColor = pixmapColorModel.getDataElement (unnormalizedComponents, 0);
  247. gc.setForeground (pixelColor);
  248. gc.drawPoint (xp, yp);
  249. }
  250. }
  251. }
  252. public void setProperties (Hashtable props)
  253. {
  254. }
  255. }