1. /*
  2. * @(#)JPEGCodec.java 1.5 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. import sun.awt.image.codec.JPEGImageDecoderImpl;
  21. import sun.awt.image.codec.JPEGImageEncoderImpl;
  22. import sun.awt.image.codec.JPEGParam;
  23. import java.awt.image.BufferedImage;
  24. import java.awt.image.Raster;
  25. import java.awt.image.ColorModel;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;
  28. /**
  29. * This class is a factory for implementations of the JPEG Image
  30. * Decoder/Encoder.
  31. * <p>
  32. * Note that the classes in the com.sun.image.codec.jpeg package are not
  33. * part of the core Java APIs. They are a part of Sun's JDK and JRE
  34. * distributions. Although other licensees may choose to distribute these
  35. * classes, developers cannot depend on their availability in non-Sun
  36. * implementations. We expect that equivalent functionality will eventually
  37. * be available in a core API or standard extension.
  38. * <p>
  39. *
  40. * @see JPEGImageDecoder
  41. * @see JPEGImageEncoder
  42. */
  43. public class JPEGCodec {
  44. private JPEGCodec() { }
  45. /**
  46. * This creates an instance of a JPEGImageDecoder that can be used
  47. * to decode JPEG Data streams.
  48. */
  49. public static JPEGImageDecoder createJPEGDecoder(InputStream src) {
  50. return new JPEGImageDecoderImpl(src);
  51. }
  52. /**
  53. * This creates an instance of a JPEGImageDecoder that can be used
  54. * to decode JPEG Data streams.
  55. */
  56. public static JPEGImageDecoder createJPEGDecoder(InputStream src,
  57. JPEGDecodeParam jdp) {
  58. return new JPEGImageDecoderImpl(src, jdp);
  59. }
  60. /**
  61. * This creates an instance of a JPEGImageEncoder that can be used
  62. * to encode image data as JPEG Data streams.
  63. */
  64. public static JPEGImageEncoder createJPEGEncoder(OutputStream dest) {
  65. return new JPEGImageEncoderImpl(dest);
  66. }
  67. /**
  68. * This creates an instance of a JPEGImageEncoder that can be used
  69. * to encode image data as JPEG Data streams.
  70. */
  71. public static JPEGImageEncoder createJPEGEncoder(OutputStream dest,
  72. JPEGEncodeParam jep) {
  73. return new JPEGImageEncoderImpl(dest, jep);
  74. }
  75. /**
  76. * This is a factory method for creating JPEGEncodeParam objects.
  77. * The returned object should do a credible job of encoding the
  78. * given BufferedImage.
  79. * @param bi A BufferedImage that is similar to the BufferedImage(s)
  80. * that will encoded using the returned JPEGEncodeParam object.
  81. */
  82. public static JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi)
  83. {
  84. int colorID = JPEGParam.getDefaultColorId(bi.getColorModel());
  85. return getDefaultJPEGEncodeParam(bi.getRaster(), colorID);
  86. }
  87. /**
  88. * This is a factory method for creating JPEGEncodeParam objects.
  89. * It is the users responsiblity to match the colorID with the
  90. * data contained in the Raster. Failure to do so may lead to
  91. * either poor compression or poor image quality. If you don't
  92. * understand much about JPEG it is strongly recommended that you
  93. * stick to the BufferedImage interface.
  94. * @param ras Raster that is similar to those to be encoded later.
  95. * @param colorID the COLOR_ID for the encoded data. This should
  96. * match the data in the raster.
  97. */
  98. public static JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras,
  99. int colorID)
  100. {
  101. JPEGParam ret = new JPEGParam(colorID, ras.getNumBands());
  102. ret.setWidth(ras.getWidth());
  103. ret.setHeight(ras.getHeight());
  104. return ret;
  105. }
  106. /**
  107. * This is a factory method for creating JPEGEncodeParam objects. It
  108. * is the users responsiblity to match the colorID with the given
  109. * number of bands, which should match the data being encoded.
  110. * Failure to do so may lead to poor compression and/or poor image
  111. * quality. If you don't understand much about JPEG it is strongly
  112. * recommended that you stick to the BufferedImage interface.
  113. *
  114. * This can also be used as a factory for a JPEGDecodeParam object.
  115. * However this usage is extremely rare, as one needs to be decoding
  116. * abbreviated JPEG streams where the JPEG tables are coming from
  117. * some source other than a JPEG tables only stream.
  118. *
  119. * @param numBands the number of bands that will be encoded (max of four).
  120. * @param colorID the COLOR_ID for the encoded data. This is used to
  121. * set reasonable defaults in the parameter object. This must match
  122. * the number of bands given.
  123. */
  124. public static JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands,
  125. int colorID)
  126. throws ImageFormatException
  127. {
  128. return new JPEGParam(colorID, numBands);
  129. }
  130. /**
  131. * This is a factory method for creating a JPEGEncodeParam from a
  132. * JPEGDecodeParam. This will return a new JPEGEncodeParam object
  133. * that is initialized from the JPEGDecodeParam object. All major
  134. * pieces of information will be initialized from the DecodeParam
  135. * (Markers, Tables, mappings).
  136. * @param jdp The JPEGDecodeParam object to copy.
  137. */
  138. public static JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp)
  139. throws ImageFormatException {
  140. return new JPEGParam(jdp);
  141. }
  142. }