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