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