1. /*
  2. * @(#)Image.java 1.39 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.image.ImageProducer;
  9. import java.awt.image.ImageObserver;
  10. import java.awt.image.ImageFilter;
  11. import java.awt.image.FilteredImageSource;
  12. import java.awt.image.AreaAveragingScaleFilter;
  13. import java.awt.image.ReplicateScaleFilter;
  14. /**
  15. * The abstract class <code>Image</code> is the superclass of all
  16. * classes that represent graphical images. The image must be
  17. * obtained in a platform-specific manner.
  18. *
  19. * @version 1.39, 12/19/03
  20. * @author Sami Shaio
  21. * @author Arthur van Hoff
  22. * @since JDK1.0
  23. */
  24. public abstract class Image {
  25. /**
  26. * convenience object; we can use this single static object for
  27. * all images that do not create their own image caps; it holds the
  28. * default (unaccelerated) properties.
  29. */
  30. private static ImageCapabilities defaultImageCaps =
  31. new ImageCapabilities(false);
  32. /**
  33. * Priority for accelerating this image. Subclasses are free to
  34. * set different default priorities and applications are free to
  35. * set the priority for specific images via the
  36. * <code>setAccelerationPriority(float)</code> method.
  37. * @since 1.5
  38. */
  39. protected float accelerationPriority = .5f;
  40. /**
  41. * Determines the width of the image. If the width is not yet known,
  42. * this method returns <code>-1</code> and the specified
  43. * <code>ImageObserver</code> object is notified later.
  44. * @param observer an object waiting for the image to be loaded.
  45. * @return the width of this image, or <code>-1</code>
  46. * if the width is not yet known.
  47. * @see java.awt.Image#getHeight
  48. * @see java.awt.image.ImageObserver
  49. */
  50. public abstract int getWidth(ImageObserver observer);
  51. /**
  52. * Determines the height of the image. If the height is not yet known,
  53. * this method returns <code>-1</code> and the specified
  54. * <code>ImageObserver</code> object is notified later.
  55. * @param observer an object waiting for the image to be loaded.
  56. * @return the height of this image, or <code>-1</code>
  57. * if the height is not yet known.
  58. * @see java.awt.Image#getWidth
  59. * @see java.awt.image.ImageObserver
  60. */
  61. public abstract int getHeight(ImageObserver observer);
  62. /**
  63. * Gets the object that produces the pixels for the image.
  64. * This method is called by the image filtering classes and by
  65. * methods that perform image conversion and scaling.
  66. * @return the image producer that produces the pixels
  67. * for this image.
  68. * @see java.awt.image.ImageProducer
  69. */
  70. public abstract ImageProducer getSource();
  71. /**
  72. * Creates a graphics context for drawing to an off-screen image.
  73. * This method can only be called for off-screen images.
  74. * @return a graphics context to draw to the off-screen image.
  75. * @exception UnsupportedOperationException if called for a
  76. * non-off-screen image.
  77. * @see java.awt.Graphics
  78. * @see java.awt.Component#createImage(int, int)
  79. */
  80. public abstract Graphics getGraphics();
  81. /**
  82. * Gets a property of this image by name.
  83. * <p>
  84. * Individual property names are defined by the various image
  85. * formats. If a property is not defined for a particular image, this
  86. * method returns the <code>UndefinedProperty</code> object.
  87. * <p>
  88. * If the properties for this image are not yet known, this method
  89. * returns <code>null</code>, and the <code>ImageObserver</code>
  90. * object is notified later.
  91. * <p>
  92. * The property name <code>"comment"</code> should be used to store
  93. * an optional comment which can be presented to the application as a
  94. * description of the image, its source, or its author.
  95. * @param name a property name.
  96. * @param observer an object waiting for this image to be loaded.
  97. * @return the value of the named property.
  98. * @throws <code>NullPointerException<code> if the property name is null.
  99. * @see java.awt.image.ImageObserver
  100. * @see java.awt.Image#UndefinedProperty
  101. */
  102. public abstract Object getProperty(String name, ImageObserver observer);
  103. /**
  104. * The <code>UndefinedProperty</code> object should be returned whenever a
  105. * property which was not defined for a particular image is fetched.
  106. */
  107. public static final Object UndefinedProperty = new Object();
  108. /**
  109. * Creates a scaled version of this image.
  110. * A new <code>Image</code> object is returned which will render
  111. * the image at the specified <code>width</code> and
  112. * <code>height</code> by default. The new <code>Image</code> object
  113. * may be loaded asynchronously even if the original source image
  114. * has already been loaded completely.
  115. *
  116. * <p>
  117. *
  118. * If either <code>width</code>
  119. * or <code>height</code> is a negative number then a value is
  120. * substituted to maintain the aspect ratio of the original image
  121. * dimensions. If both <code>width</code> and <code>height</code>
  122. * are negative, then the original image dimensions are used.
  123. *
  124. * @param width the width to which to scale the image.
  125. * @param height the height to which to scale the image.
  126. * @param hints flags to indicate the type of algorithm to use
  127. * for image resampling.
  128. * @return a scaled version of the image.
  129. * @exception IllegalArgumentException if <code>width</code>
  130. * or <code>height</code> is zero.
  131. * @see java.awt.Image#SCALE_DEFAULT
  132. * @see java.awt.Image#SCALE_FAST
  133. * @see java.awt.Image#SCALE_SMOOTH
  134. * @see java.awt.Image#SCALE_REPLICATE
  135. * @see java.awt.Image#SCALE_AREA_AVERAGING
  136. * @since JDK1.1
  137. */
  138. public Image getScaledInstance(int width, int height, int hints) {
  139. ImageFilter filter;
  140. if ((hints & (SCALE_SMOOTH | SCALE_AREA_AVERAGING)) != 0) {
  141. filter = new AreaAveragingScaleFilter(width, height);
  142. } else {
  143. filter = new ReplicateScaleFilter(width, height);
  144. }
  145. ImageProducer prod;
  146. prod = new FilteredImageSource(getSource(), filter);
  147. return Toolkit.getDefaultToolkit().createImage(prod);
  148. }
  149. /**
  150. * Use the default image-scaling algorithm.
  151. * @since JDK1.1
  152. */
  153. public static final int SCALE_DEFAULT = 1;
  154. /**
  155. * Choose an image-scaling algorithm that gives higher priority
  156. * to scaling speed than smoothness of the scaled image.
  157. * @since JDK1.1
  158. */
  159. public static final int SCALE_FAST = 2;
  160. /**
  161. * Choose an image-scaling algorithm that gives higher priority
  162. * to image smoothness than scaling speed.
  163. * @since JDK1.1
  164. */
  165. public static final int SCALE_SMOOTH = 4;
  166. /**
  167. * Use the image scaling algorithm embodied in the
  168. * <code>ReplicateScaleFilter</code> class.
  169. * The <code>Image</code> object is free to substitute a different filter
  170. * that performs the same algorithm yet integrates more efficiently
  171. * into the imaging infrastructure supplied by the toolkit.
  172. * @see java.awt.image.ReplicateScaleFilter
  173. * @since JDK1.1
  174. */
  175. public static final int SCALE_REPLICATE = 8;
  176. /**
  177. * Use the Area Averaging image scaling algorithm. The
  178. * image object is free to substitute a different filter that
  179. * performs the same algorithm yet integrates more efficiently
  180. * into the image infrastructure supplied by the toolkit.
  181. * @see java.awt.image.AreaAveragingScaleFilter
  182. * @since JDK1.1
  183. */
  184. public static final int SCALE_AREA_AVERAGING = 16;
  185. /**
  186. * Flushes all resources being used by this Image object. This
  187. * includes any pixel data that is being cached for rendering to
  188. * the screen as well as any system resources that are being used
  189. * to store data or pixels for the image. The image is reset to
  190. * a state similar to when it was first created so that if it is
  191. * again rendered, the image data will have to be recreated or
  192. * fetched again from its source.
  193. * <p>
  194. * This method always leaves the image in a state such that it can
  195. * be reconstructed. This means the method applies only to cached
  196. * or other secondary representations of images such as those that
  197. * have been generated from an <tt>ImageProducer</tt> (read from a
  198. * file, for example). It does nothing for off-screen images that
  199. * have only one copy of their data.
  200. */
  201. public abstract void flush();
  202. /**
  203. * Returns an ImageCapabilities object which can be
  204. * inquired as to the capabilities of this
  205. * Image on the specified GraphicsConfiguration.
  206. * This allows programmers to find
  207. * out more runtime information on the specific Image
  208. * object that they have created. For example, the user
  209. * might create a BufferedImage but the system may have
  210. * no video memory left for creating an image of that
  211. * size on the given GraphicsConfiguration, so although the object
  212. * may be acceleratable in general, it is
  213. * does not have that capability on this GraphicsConfiguration.
  214. * @param gc a <code>GraphicsConfiguration</code> object. A value of null
  215. * for this parameter will result in getting the image capabilities
  216. * for the default <code>GraphicsConfiguration</code>.
  217. * @return an <code>ImageCapabilities</code> object that contains
  218. * the capabilities of this <code>Image</code> on the specified
  219. * GraphicsConfiguration.
  220. * @see #java.awt.image.VolatileImage.getCapabilities()
  221. * VolatileImage.getCapabilities()
  222. * @since 1.5
  223. */
  224. public ImageCapabilities getCapabilities(GraphicsConfiguration gc) {
  225. // Note: this is just a default object that gets returned by the
  226. // base Image object. Subclasses of Image should override this
  227. // method and return an ImageCapabilities object that is appropriate
  228. // for a given instance of that subclass.
  229. return defaultImageCaps;
  230. }
  231. /**
  232. * Sets a hint for this image about how important acceleration is.
  233. * This priority hint is used to compare to the priorities of other
  234. * Image objects when determining how to use scarce acceleration
  235. * resources such as video memory. When and if it is possible to
  236. * accelerate this Image, if there are not enough resources available
  237. * to provide that acceleration but enough can be freed up by
  238. * de-acceleration some other image of lower priority, then that other
  239. * Image may be de-accelerated in deference to this one. Images
  240. * that have the same priority take up resources on a first-come,
  241. * first-served basis.
  242. * @param priority a value between 0 and 1, inclusive, where higher
  243. * values indicate more importance for acceleration. A value of 0
  244. * means that this Image should never be accelerated. Other values
  245. * are used simply to determine acceleration priority relative to other
  246. * Images.
  247. * @throws IllegalArgumentException if <code>priority</code> is less
  248. * than zero or greater than 1.
  249. * @since 1.5
  250. */
  251. public void setAccelerationPriority(float priority) {
  252. if (priority < 0 || priority > 1) {
  253. throw new IllegalArgumentException("Priority must be a value " +
  254. "between 0 and 1, inclusive");
  255. }
  256. accelerationPriority = priority;
  257. }
  258. /**
  259. * Returns the current value of the acceleration priority hint.
  260. * @see #setAccelerationPriority(float priority) setAccelerationPriority
  261. * @return value between 0 and 1, inclusive, which represents the current
  262. * priority value
  263. * @since 1.5
  264. */
  265. public float getAccelerationPriority() {
  266. return accelerationPriority;
  267. }
  268. }