1. /*
  2. * @(#)JPEGImageEncoder.java 1.3 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /**********************************************************************
  8. **********************************************************************
  9. **********************************************************************
  10. *** COPYRIGHT (c) 1997-1998 Eastman Kodak Company. ***
  11. *** As an unpublished work pursuant to Title 17 of the United ***
  12. *** States Code. All rights reserved. ***
  13. **********************************************************************
  14. **********************************************************************
  15. **********************************************************************/
  16. package com.sun.image.codec.jpeg;
  17. /**
  18. * JPEGImageEncoder Interface
  19. *
  20. * JPEGImageEncoder compresses images into JPEG data streams and
  21. * writes the JPEG stream to an OutputStream. Image data that is to
  22. * be encoded can be passed in as a Raster of image data or as a
  23. * BufferedImage. Encoding or the image data into the output JPEG
  24. * stream is controlled by the parameters setting found in the
  25. * JPEGEncodeParam object.<P>
  26. *
  27. * ColorSpace comments: First off JPEG by specification is color
  28. * blind! That said, this interface will perform some color space
  29. * conversion in the name of better compression ratios. There is no
  30. * explicit mechanism in the JPEGEncodeParam interface for controlling
  31. * the Encoded ColorSpace of the data when it is written to the JPEG
  32. * data stream. If an approriate colorspace setting is not already
  33. * defined it is recommended that colorspace unknown is used. Some
  34. * updates to the standard color space designations have been made to
  35. * allow this decoder to handle alpha channels. See the
  36. * JPEGEncodeParam description for more details on additional color
  37. * space designations ( @see JPEGEncodeParam ).<P>
  38. *
  39. * This encoder will process interchange, and abbreviated JPEG
  40. * streams.
  41. */
  42. import java.io.OutputStream;
  43. import java.io.IOException;
  44. import java.awt.image.BufferedImage;
  45. import java.awt.image.ColorModel;
  46. import java.awt.image.Raster;
  47. /**
  48. * JPEGImageEncoder encodes buffers of image data into JPEG data
  49. * streams. Users of this interface are required to provide image data in
  50. * a Raster or a BufferedImage, set the necessary parameters in the
  51. * JPEGEncodeParams object and successfully open the
  52. * <code>OutputStream</code> that is the destination of the encoded
  53. * JPEG stream.
  54. *
  55. * The JPEGImageEncoder interface can encode image data into interchange,
  56. * and abbreviated JPEG data streams that are written to the
  57. * OutputStream provided to the encoder.
  58. * <p>
  59. * Note that the classes in the com.sun.image.codec.jpeg package are not
  60. * part of the core Java APIs. They are a part of Sun's JDK and JRE
  61. * distributions. Although other licensees may choose to distribute these
  62. * classes, developers cannot depend on their availability in non-Sun
  63. * implementations. We expect that equivalent functionality will eventually
  64. * be available in a core API or standard extension.
  65. * <p>
  66. *
  67. * @see JPEGCodec
  68. * @see JPEGEncodeParam
  69. * @see Raster
  70. * @see BufferedImage
  71. * @see OutputStream
  72. */
  73. public interface JPEGImageEncoder {
  74. /**
  75. * Return the stream the Encoder is currenlt associated with.
  76. */
  77. public OutputStream getOutputStream();
  78. /**
  79. * Set the JPEGEncodeParam object that is to be used for future
  80. * encoding operations. 'jep' is copied so changes will not be
  81. * tracked, unless you call this method again.
  82. * @param jep The JPEGEncodeParam object to use for future encodings.
  83. *
  84. */
  85. public void setJPEGEncodeParam(JPEGEncodeParam jep);
  86. /**
  87. * This returns a copy of the current JPEGEncodeParam object, if
  88. * you want changes to affect the encoding process you must 'set'
  89. * it back into the encoder (either through setJPEGEncodeParam or
  90. * by providing the modified param object in the call to encode.
  91. * @return A copy of the current JPEGEncodeParam object
  92. */
  93. public JPEGEncodeParam getJPEGEncodeParam();
  94. /**
  95. * This is a factory method for creating JPEGEncodeParam objects.
  96. * The returned object will do a credible job of encoding the
  97. * given BufferedImage.
  98. */
  99. public JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi)
  100. throws ImageFormatException;
  101. /**
  102. * Encode a BufferedImage as a JPEG data stream. Note, some color
  103. * conversions may takes place. The current JPEGEncodeParam's
  104. * encoded COLOR_ID should match the value returned by
  105. * getDefaultColorID when given the BufferedImage's ColorModel.<P>
  106. * If no JPEGEncodeParam object has been provided yet a default
  107. * one will be created by calling getDefaultJPEGEncodeParam with
  108. * bi.
  109. * @param bi The BufferedImage to encode.
  110. */
  111. public void encode(BufferedImage bi)
  112. throws IOException, ImageFormatException;
  113. /**
  114. * Encode a BufferedImage as a JPEG data stream. Note, some color
  115. * conversions may takes place. The jep's encoded COLOR_ID should
  116. * match the value returned by getDefaultColorID when given the
  117. * BufferedImage's ColorModel.<P>
  118. * This call also sets the current JPEGEncodeParam object. The
  119. * given JPEGEncodeParam object will be used for this and future
  120. * encodings. If jep is null then a new JPEGEncodeParam object
  121. * will be created by calling getDefaultJPEGEncodeParam with bi.
  122. * @param bi The BufferedImage to encode.
  123. * @param jep The JPEGEncodeParam object used to control the encoding.
  124. */
  125. public void encode(BufferedImage bi, JPEGEncodeParam jep)
  126. throws IOException, ImageFormatException;
  127. /**
  128. * Returns the 'default' encoded COLOR_ID for a given ColorModel.
  129. * This method is not needed in the simple case of encoding
  130. * Buffered Images (the library will figure things out for you).
  131. * It can be useful for encoding Rasters. To determine what needs
  132. * to be done to the image prior to encoding.
  133. * @param cm The ColorModel to map to an jpeg encoded COLOR_ID.
  134. * @return The default mapping of cm to a jpeg Color_ID note that
  135. * in a few cases color conversion is required.
  136. */
  137. public int getDefaultColorId(ColorModel cm);
  138. /**
  139. * This is a factory method for creating JPEGEncodeParam objects.
  140. * It is the users responsiblity to match the colorID with the
  141. * data contained in the Raster. Failure to do so may lead to
  142. * either poor compression or poor image quality. If you don't
  143. * understand much about JPEG it is strongly reccomended that you
  144. * stick to the BufferedImage interfaces.
  145. */
  146. public JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras, int colorID)
  147. throws ImageFormatException;
  148. /**
  149. * This is a factory method for creating JPEGEncodeParam objects. It
  150. * is the users responsiblity to match the colorID with the given
  151. * number of bands, which should match the data being encoded.
  152. * Failure to do so may lead to poor compression and/or poor image
  153. * quality. If you don't understand much about JPEG it is strongly
  154. * recommended that you stick to the BufferedImage interface.
  155. *
  156. * @param numBands the number of bands that will be encoded (max of
  157. * four).
  158. * @param colorID the COLOR_ID for the encoded data. This is used to
  159. * set reasonable defaults in the parameter object. This must match
  160. * the number of bands given.
  161. */
  162. public JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands,
  163. int colorID)
  164. throws ImageFormatException;
  165. /**
  166. * This is a factory method for creating a JPEGEncodeParam from a
  167. * JPEGDecodeParam. This will return a new JPEGEncodeParam object
  168. * that is initialized from the JPEGDecodeParam object. All major
  169. * pieces of information will be initialized from the DecodeParam
  170. * (Markers, Tables, mappings).
  171. * @param jdp The JPEGDecodeParam object to copy.
  172. */
  173. public JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp)
  174. throws ImageFormatException;
  175. /**
  176. * Encode a Raster as a JPEG data stream. Note that no color
  177. * conversion takes place. It is required that you match the
  178. * Raster to the encoded COLOR_ID contained in the current
  179. * JPEGEncodeParam object.<P>
  180. * If no JPEGEncodeParam object has been provided yet a
  181. * new JPEGEncodeParam object will be created by calling
  182. * getDefaultJPEGEncodeParam with ras and COLOR_ID_UNKNOWN.
  183. * @param ras The Raster to encode.
  184. */
  185. public void encode(Raster ras)
  186. throws IOException, ImageFormatException;
  187. /**
  188. * Encode a Raster as a JPEG data stream. Note that no color
  189. * conversion takes place. It is required that you match the
  190. * Raster to the encoded COLOR_ID contained in the JPEGEncodeParam
  191. * object.
  192. * If jep is null a new JPEGEncodeParam object will be created by
  193. * calling getDefaultJPEGEncodeParam with ras and
  194. * COLOR_ID_UNKNOWN.
  195. * @param ras The Raster to encode.
  196. * @param jep The JPEGEncodeParam object used to control the encoding.
  197. */
  198. public void encode(Raster ras, JPEGEncodeParam jep)
  199. throws IOException, ImageFormatException;
  200. }